opengl 正方体+模拟视角旋转

 // first_3D.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <GL/glut.h>
#include <math.h> // 将立方体的八个顶点保存到一个数组里面
static const GLfloat vertex_list[][] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
};
//变换后数组
GLfloat vertex_list_new[][] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
}; void work(float T[][])
{
GLfloat list_temp[][];
for(int i=;i<;i++)
for(int j=;j<;j++)
list_temp[i][j] = vertex_list_new[i][j];
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
float sum = ;
for(int k=;k<;k++)
{
sum+=list_temp[i][k]*T[k][j];
}
vertex_list_new[i][j] = sum;
}
}
return;
}
// 将要使用的顶点的序号保存到一个数组里面
static const GLint index_list[][] = {
, , , ,
, , , ,
, , , ,
, , , ,
, , , ,
, , , ,
}; void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(1.5,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);//视点 /* /坐标轴
glLineWidth(1);
glColor3f( 0.0, 0.0, 0.0); // 黑色
glBegin(GL_LINES);
glVertex3f(-0xFFFFFFF,0,0);
glVertex3f(0xFFFFFFF,0,0);
glVertex3f(0,-0xFFFFFFF,0);
glVertex3f(0,0xFFFFFFF,0);
glVertex3f(0,0,-0xFFFFFFF);
glVertex3f(0,0,0xFFFFFFF);
glEnd();
//坐标轴绘制结束*/ /* /绘制原立方体
glColor3f( 1.0, 1.0, 0.0);
//glBegin(GL_QUADS);
for(int i=0; i<6; ++i) // 有六个面,循环六次
{
glBegin( GL_LINE_LOOP);
for(int j=0; j<4; ++j) // 每个面有四个顶点,循环四次
{
glVertex3fv(vertex_list[index_list[i][j]]);
}
glEnd();
}
//立方体绘制结束*/ //绘制新立方体
glColor3f( 1.0, 0.0, 0.0);
//glBegin(GL_QUADS);
for(int i=; i<; ++i) // 有六个面,循环六次
{
glBegin( GL_LINE_LOOP);
for(int j=; j<; ++j) // 每个面有四个顶点,循环四次
{
glVertex3fv(vertex_list_new[index_list[i][j]]);
}
glEnd();
}
//立方体绘制结束
glFlush();
glutSwapBuffers();
} void reshape(int w,int h)
{
glViewport(,,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
} void init()
{
glClearColor( , , , 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-4.0,4.0,-4.0,4.0,-4.0,4.0);
glMatrixMode(GL_MODELVIEW);
} int main(int argc,char** argv)
{
//计算部分
float a,b,c;//Ov
a = ;
b = ;
c = ;
float R = sqrt(a*a+b*b+c*c);
float d = 1.0;
float costh = c/sqrt(a*a+c*c);
float sinth = a/sqrt(a*a+c*c);
float cosfy = b/R;
float sinfy = sqrt(a*a+c*c)/R; float T1[][] = {{,,,},{,,,},{,,,},{-a,-b,-c,}}; //原点到视点平移变换矩阵
float T2[][] = {{-costh,,-sinth,},{,,,},{sinth,,-costh,},{,,,}}; //绕y1轴旋转变换
float T3[][] = {{,,,},{,sinfy,-cosfy,},{,cosfy,sinfy,},{,,,}}; //绕x2轴旋转变换
float T4[][] = {{-,,,},{,,,},{,,,},{,,,}}; //关于y3Ovz3面的反射变换
//work(T1);work(T2);work(T3);work(T4); //世界--观察坐标系 合成矩阵
float Tv[][] = {{cosfy,-cosfy*sinth,-sinfy*sinth,},{,sinfy,-cosfy,},{-sinth,-cosfy*costh,-sinfy*costh,},{,,R,}};
//work(Tv); float Tpersp[][] = {{,,,},{,,,},{,,,/d},{,,,}}; //透视矩阵
float Tproj[][] = {{-,,,},{,,,},{,,,},{,,,}}; //投影矩阵
//work(Tpersp);work(Tproj);
float Ts[][] = {{,,,},{,,,},{,,,/d},{,,,}}; //合成
//work(Ts); //总合成
float T[][] = {{costh,-costh*sinth,,-sinfy*sinth/d},{,sinfy,,-cosfy/d},{-sinth,-cosfy*costh,,-sinfy*costh/d},{,,,R/d}};
work(T); float T_1[][] = {{,,,},{,,,},{,,,-/d},{,,,R/d}};
//work(T_1); glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(,);
glutInitWindowPosition(,);
glutCreateWindow("立方体");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
init();
glutMainLoop();
return ;
}
上一篇:MySql绿色版应用


下一篇:洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)