Advertisement
Guest User

new

a guest
Apr 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <windows.h>
  2. #include <GL/glut.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6.  
  7. #define PI 3.1415
  8.  
  9. GLfloat angle=45.0f;
  10. GLfloat cubesize;
  11. boolean option=false;
  12.  
  13.  
  14. void demo_menu(int id){
  15. if(id==3)
  16. exit(0);
  17. else if (id==1) {
  18. option=false;
  19. }
  20. else if (id==2){
  21. option=true;
  22. }
  23.  
  24. glutPostRedisplay();
  25. }
  26.  
  27. void timer(){
  28.  
  29. glutPostRedisplay();
  30.  
  31. angle+=0.2;
  32. if(angle>=360.0) angle-=360.0;
  33.  
  34. cubesize=3+2*fabs(sin(angle*PI/180));
  35.  
  36. }
  37.  
  38. void myinit(void)
  39. {
  40. typedef GLfloat point[3];
  41. /* attributes */
  42. glEnable(GL_BLEND);
  43. glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */
  44. glColor3f(0.0, 0.0, 1.0); /* draw in red */
  45.  
  46. /* set up viewing */
  47. /* 500 x 500 window with origin lower left */
  48.  
  49. glMatrixMode(GL_PROJECTION);
  50. glLoadIdentity();
  51. glOrtho(-100.0,100.0,-100.0,100.0,-200.0,200.0);
  52. glMatrixMode(GL_MODELVIEW);
  53.  
  54. glNewList(1,GL_COMPILE);
  55.  
  56. point vertices[4]= {{1.0,1.0,1.0},{1.0,-1.0,1.0},{-1.0,-1.0,1.0},{-1.0,1.0,1.0}};
  57.  
  58. glBegin(GL_POLYGON);
  59. glVertex3fv(vertices[0]);
  60. glVertex3fv(vertices[1]);
  61. glVertex3fv(vertices[2]);
  62. glVertex3fv(vertices[3]);
  63. glEnd();
  64.  
  65.  
  66.  
  67. glEndList();
  68.  
  69.  
  70. }
  71.  
  72. void cubefunc() {
  73. glLoadIdentity();
  74. if (option){
  75. glTranslatef(0,0,-22);
  76. glRotatef(angle,1,0,1);
  77. glTranslatef(0,0,-88);
  78. }
  79. else{
  80. glTranslatef(0,0,-110);
  81. glRotatef(angle,1,0,1);
  82. }
  83. glScalef(cubesize,cubesize,cubesize);
  84. }
  85.  
  86.  
  87. void pleures(){
  88. glColor3f(1.0,0.0,0.0); //red
  89. glCallList(1); //z
  90.  
  91. glPushMatrix();
  92. glRotatef(90,1,0,0);//-y
  93. glColor3f(1.0,0.0,1.0); //purple
  94. glCallList(1);
  95.  
  96. glColor3f(1.0,1.0,0.0); //yellow
  97. glRotatef(90, 1,0,0);
  98. glCallList(1); // -z
  99.  
  100. glColor3f(0.0,0.0,1.0); //blue
  101. glRotatef(90, 1,0,0);
  102. glCallList(1); // y
  103.  
  104. glPopMatrix();
  105. glColor3f(0.0,1.0,0.0); //green
  106.  
  107. glPushMatrix();
  108. glRotatef(90,0,1,0);
  109. glCallList(1);//-x
  110.  
  111. glColor3f(0.0,1.0,1.0); //cyan
  112. glRotatef(180,0,1,0);
  113. glCallList(1);//-x
  114. glPopMatrix();
  115.  
  116. }
  117.  
  118. void display( void )
  119. {
  120.  
  121. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  122. /* define a point data type */
  123. glColor3f(0.0,0.0, 1.0);
  124. glLoadIdentity();
  125. glTranslatef(0,0,-88);
  126. //glCallList(1);
  127. //glPushMatrix();
  128.  
  129. cubefunc();
  130. pleures();
  131.  
  132. //glPopMatrix();
  133.  
  134.  
  135. glFlush();
  136. }
  137.  
  138.  
  139.  
  140. int main(int argc, char** argv)
  141. {
  142.  
  143. /* Standard GLUT initialization */
  144.  
  145. glutInit(&argc,argv);
  146. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); /* default, not needed */
  147. // glDrawBuffer(GL_BACK) ;
  148. glutInitWindowSize(500.0,500.0); /* 500 x 500 pixel window */
  149. glutInitWindowPosition(0,0); /* place window top left on display */
  150. glutCreateWindow("Moving square"); /* window title */
  151. glutDisplayFunc(display); /* display callback invoked when window opened */
  152.  
  153. myinit(); /* set attributes */
  154.  
  155. glEnable(GL_DEPTH_TEST);
  156. glutIdleFunc(timer);
  157.  
  158. glutCreateMenu(demo_menu);
  159. glutAddMenuEntry("a)",1);
  160. glutAddMenuEntry("b)",2);
  161. glutAddMenuEntry("Exit",3);
  162. glutAttachMenu(GLUT_RIGHT_BUTTON);
  163.  
  164. glutMainLoop(); /* enter event loop */
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement