loadomain

博客园 首页 新随笔 联系 订阅 管理
  1 #pragma comment(lib,"glut32.lib")
  2 #pragma comment(lib,"glut.lib")
  3 #pragma comment(lib,"GlU32.lib")
  4 #include<GL\glut.h>
  5 #include<Windows.h>
  6 //use vertex_array way to draw primitives
  7 //globle variable
  8 //立方体八个定点的三维数据坐标
  9 GLfloat VertexData[] = {
 10     -0.5f, -0.5f, -0.5f,
 11      0.5f, -0.5f, -0.5f,
 12     -0.5f,  0.5f, -0.5f,
 13      0.5f,  0.5f, -0.5f,
 14     -0.5f, -0.5f,  0.5f,
 15      0.5f, -0.5f,  0.5f,
 16     -0.5f,  0.5f,  0.5f,
 17      0.5f,  0.5f,  0.5f,
 18 };
 19 //立方体六个表面的定点索引数据,无论坐标系如何变幻,
 20 //任何时刻最多显示3个面,因此如果在数据处理多面体时候只
 21 //处理显示的“正面”数据则可以大大减少3d图像处理的开销和功耗
 22 //当初做过实验,在glutIdleFunc()函数中不断调用画图子程序,使得cpu的内核温度飙升到90°……
 23 //那么,GL中规定逆时针的面为正面,因此面对3d图形时候,其顶点索引顺时针填写将在
 24 //后续处理中设定为背面,而逆时针面被设定为正面
 25 static GLubyte VertexIndex[][4] = {
 26     0, 2, 3, 1,
 27     0, 4, 6, 2,
 28     0, 1, 5, 4,
 29     4, 5, 7, 6,
 30     1, 3, 7, 5,
 31     2, 6, 7, 3,
 32 };
 33 float rotate;
 34 //protype
 35 void DrawCube(void);
 36 void Timer0(int id);
 37 void DisplayFunc(void);
 38 float GetRand(int,int);
 39 //main function
 40 void main(int argc, char **argv) 
 41 {
 42     //Init windows
 43     glutInit(&argc, argv);
 44     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
 45     glutInitWindowPosition(0,0);
 46     glutInitWindowSize(800,800);
 47     glutCreateWindow("Cube");
 48     //register display
 49     glutDisplayFunc(DisplayFunc);
 50     glutTimerFunc(20,Timer0,0);
 51     //message loop
 52     glutMainLoop();
 53 }
 54 //sub-function implement
 55 void DisplayFunc(void)
 56 {
 57     glClearColor(0.0,0.0,0.0,0.0);
 58     glClear(GL_COLOR_BUFFER_BIT);
 59     glFlush();
 60 }
 61 //此处引用定点数据进行绘制,定点数组绘制相比用普通的点点绘制最终
 62 //的效果是一致的,优化在于减少了函数开销,比如使用普通点点绘制
 63 //对于立方体的一个定点,在完成一个完整立方体的时候被重复调用3次
 64 //全遍历。
 65 //在此说明opengl是基于客户和服务器的架构,对于本地运行的opengl应用程序来讲
 66 //应用程序可以看成是客户,GL内核是服务器(更准确是包括GPU部分)。之所以在此
 67 //提及服务器和客户的概念,是为后续缓冲对象的应用背景做个铺垫。
 68 //定点数组数据以及定点索引数据是存储在客户端。
 69 //对于室内物体无线定位来讲,一个立方体就足够标记一个物体了,使用定点数据将数据存储在客户端就已经够用了,需要的时候
 70 //发往GPU的内存里就行(送到服务器端)。
 71 void DrawCube(void)
 72 {
 73     //enable vertex_array
 74     glEnableClientState(GL_VERTEX_ARRAY);
 75     //load vertex_array data
 76     glVertexPointer(3,GL_FLOAT,3*sizeof(GLfloat),VertexData);
 77     //drawing operation
 78     glDrawElements(GL_QUADS,sizeof(VertexData),GL_UNSIGNED_BYTE,VertexIndex);
 79 }
 80 void Timer0(int id)
 81 {
 82     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 83     glMatrixMode(GL_MODELVIEW); 
 84     glLoadIdentity();
 85     glPushMatrix();
 86     glLineWidth(2.4);
 87 //默认的多边形表面是以填充的形式绘制,此处设置为轮廓线绘制
 88     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
 89     //设定逆时针为正面
 90     glFrontFace(GL_CCW);
 91 //由于采用了矩阵压栈和出栈的处理,使得此处旋转的角度采用全局变量,存储角度
 92     //旋转角度实则对360°为一个周期。
 93     rotate += GetRand(0,6)/10;
 94     glRotatef(rotate,1,0,0);
 95     glRotatef(rotate,0,1,0);
 96     glRotatef(rotate,0,0,1);
 97     DrawCube();
 98     glPopMatrix();
 99     glutSwapBuffers();
100     glutTimerFunc(20,Timer0,0);//for continue timer counting
101 }
102 float GetRand(int start,int end)
103 {
104     return start + (end - start)*rand()/(RAND_MAX + 1.0);
105 }

 

posted on 2013-12-01 09:46  loadomain  阅读(341)  评论(0)    收藏  举报