DBUFCOLR.C
上传用户:tengyuc
上传日期:2007-08-14
资源大小:722k
文件大小:5k
源码类别:

OpenGL

开发平台:

Visual C++

  1. #include "windows.h"
  2. #include <GL/gl.h>
  3. #include <GL/glu.h>
  4. #include <GL/glaux.h>
  5. GLfloat step=0.0,s=0.1;
  6. static GLfloat  p1[]={0.5,-0.5,-0.5}, p2[]={0.5,0.5,-0.5},
  7.                 p3[]={0.5,0.5,0.5},   p4[]={0.5,-0.5,0.5},
  8.                 p5[]={-0.5,-0.5,0.5}, p6[]={-0.5,0.5,0.5},
  9.                 p7[]={-0.5,0.5,-0.5}, p8[]={-0.5,-0.5,-0.5};
  10. static GLfloat  m1[]={1.0,0.0,0.0}, m2[]={-1.0,0.0,0.0},
  11. m3[]={0.0,1.0,0.0}, m4[]={0.0,-1.0,0.0},
  12. m5[]={0.0,0.0,1.0}, m6[]={0.0,0.0,-1.0};
  13.    
  14. static GLfloat  c1[]={0.0,0.0,1.0},  c2[]={0.0,1.0,1.0},
  15.                 c3[]={1.0,1.0,1.0},  c4[]={1.0,0.0,1.0},
  16.                 c5[]={1.0,0.0,0.0},  c6[]={1.0,1.0,0.0},
  17.                 c7[]={0.0,1.0,0.0},  c8[]={1.0,1.0,1.0};
  18.               
  19. void myinit(void);
  20. void CALLBACK myReshape(GLsizei w, GLsizei h);
  21. void CALLBACK stepDisplay (void);
  22. void CALLBACK startIdleFunc (AUX_EVENTREC *event);
  23. void CALLBACK stopIdleFunc (AUX_EVENTREC *event);
  24. void CALLBACK display(void);
  25. void DrawColorBox(void);
  26. //  初始化材质特性、光源、光照模型和深度缓冲区
  27. void myinit(void)
  28.     GLfloat light_ambient[]={0.3,0.2,0.5};
  29. GLfloat light_diffuse[]={1.0,1.0,1.0};
  30.     GLfloat light_position[] = { 2.0, 2.0, 2.0, 1.0 };
  31.     GLfloat light1_ambient[]={0.3,0.3,0.2};
  32. GLfloat light1_diffuse[]={1.0,1.0,1.0};
  33.     GLfloat light1_position[] = { -2.0, -2.0, -2.0, 1.0 };
  34.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  35. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  36. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  37.     glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
  38. glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
  39. glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  40. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  41.     glEnable(GL_LIGHTING);
  42.     glEnable(GL_LIGHT0);
  43.  glEnable(GL_LIGHT1);
  44.     glDepthFunc(GL_LESS);
  45.     glEnable(GL_DEPTH_TEST);
  46.      glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);
  47.      glEnable(GL_COLOR_MATERIAL);
  48. }
  49. void CALLBACK display(void)
  50. {
  51. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  52.     
  53.       s+=0.005;
  54.       if(s>1.0)s=0.1;
  55.     glPushMatrix();
  56.     glScalef(s,s,s);
  57.      glRotatef(step,0.0,1.0,0.0);
  58.      glRotatef(step,0.0,0.0,1.0);
  59.     glRotatef(step,1.0,0.0,0.0);
  60.      DrawColorBox();
  61.     glPopMatrix();
  62.     
  63.     glFlush();
  64. //  交换缓冲区
  65. auxSwapBuffers();
  66. }
  67. void CALLBACK stepDisplay (void)
  68. {
  69.     step = step + 1.0;
  70.     if (step > 360.0)
  71.     step = step - 360.0;
  72.     display();
  73. }
  74. void CALLBACK startIdleFunc (AUX_EVENTREC *event)
  75. {
  76.     auxIdleFunc(stepDisplay);
  77. }
  78. void CALLBACK stopIdleFunc (AUX_EVENTREC *event)
  79. {
  80.     auxIdleFunc(0);
  81. }
  82. void CALLBACK myReshape(GLsizei w, GLsizei h)
  83. {
  84.     glViewport(0, 0, w, h);
  85.     glMatrixMode(GL_PROJECTION);
  86.     glLoadIdentity();
  87.     if (w <= h) 
  88.     glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w, 
  89.         1.50*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
  90.     else 
  91.     glOrtho (-1.5*(GLfloat)w/(GLfloat)h, 
  92.         1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
  93.     glMatrixMode(GL_MODELVIEW);
  94.     glLoadIdentity ();
  95. }
  96.   void DrawColorBox(void)
  97. {
  98.  
  99.     glBegin (GL_QUADS);
  100.         
  101.   glColor3fv(c1);
  102.          glNormal3fv(m1);
  103.         glVertex3fv(p1);
  104.   glColor3fv(c2);
  105.         glVertex3fv(p2);
  106.   glColor3fv(c3);
  107.         glVertex3fv(p3);
  108.   glColor3fv(c4);
  109.         glVertex3fv(p4);
  110.   glColor3fv(c5);
  111.          glNormal3fv(m5);
  112.         glVertex3fv(p5);
  113.   glColor3fv(c6);
  114.         glVertex3fv(p6);
  115.   glColor3fv(c7);
  116.         glVertex3fv(p7);
  117.   glColor3fv(c8);
  118.         glVertex3fv(p8);
  119.   glColor3fv(c5);
  120.          glNormal3fv(m3);
  121.         glVertex3fv(p5);
  122.   glColor3fv(c6);
  123.         glVertex3fv(p6);
  124.   glColor3fv(c3);
  125.         glVertex3fv(p3);
  126.   glColor3fv(c4);
  127.         glVertex3fv(p4);
  128.   glColor3fv(c1);
  129.          glNormal3fv(m4);
  130.         glVertex3fv(p1);
  131.   glColor3fv(c2);
  132.         glVertex3fv(p2);
  133.   glColor3fv(c7);
  134.         glVertex3fv(p7);
  135.   glColor3fv(c8);
  136.         glVertex3fv(p8);
  137.   glColor3fv(c2);
  138.          glNormal3fv(m5);
  139.         glVertex3fv(p2);
  140.   glColor3fv(c3);
  141.         glVertex3fv(p3);
  142.   glColor3fv(c6);
  143.         glVertex3fv(p6);
  144.   glColor3fv(c7);
  145.         glVertex3fv(p7);
  146.   glColor3fv(c1);
  147.          glNormal3fv(m6);
  148.         glVertex3fv(p1);
  149.   glColor3fv(c4);
  150.         glVertex3fv(p4);
  151.   glColor3fv(c5);
  152.         glVertex3fv(p5);
  153.   glColor3fv(c8);
  154.         glVertex3fv(p8);
  155.   glEnd();
  156. }
  157.   
  158. void main(void)
  159. {
  160. //  设置显示模式为双缓冲模式和RGBA模式
  161.     auxInitDisplayMode (AUX_DOUBLE | AUX_RGBA);
  162.     auxInitPosition (0, 0, 500,400);
  163.     auxInitWindow ("Double_Buffer_Animation");
  164.     myinit();
  165.     auxReshapeFunc (myReshape);
  166.     auxIdleFunc (stepDisplay);
  167.     auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, startIdleFunc);
  168.     auxMouseFunc (AUX_RIGHTBUTTON, AUX_MOUSEDOWN, stopIdleFunc);
  169.     auxMainLoop(display);
  170. }