Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /*
  2. * GLUT Shapes Demo
  3. *
  4. * Written by Nigel Stewart November 2003
  5. *
  6. * This program is test harness for the sphere, cone
  7. * and torus shapes in GLUT.
  8. *
  9. * Spinning wireframe and smooth shaded shapes are
  10. * displayed until the ESC or q key is pressed. The
  11. * number of geometry stacks and slices can be adjusted
  12. * using the + and - keys.
  13. */
  14.  
  15. #ifdef __APPLE__
  16. #include <GLUT/glut.h>
  17. #else
  18. #include <GL/glut.h>
  19. #endif // __APPLE__
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. float counter = 0;
  24.  
  25. void initGL() //Setbackgroundcolortoblackandopaque
  26. {
  27. glClearColor(0.0f,0.0f,0.0f,1.0f); //Setbackgrounddepthtofarthest
  28. glClearDepth(1.0f);
  29. glEnable(GL_DEPTH_TEST);//Enabledepthtestingforz-culling
  30. glDepthFunc(GL_LEQUAL);//Setthetypeofdepth-test
  31. glShadeModel(GL_SMOOTH);//Enablesmoothshading //Niceperspectivecorrections
  32. glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
  33. }
  34.  
  35.  
  36. void reshape(int width,int height)
  37. {
  38. glViewport(0,0,width,height);
  39. glMatrixMode(GL_PROJECTION);
  40. glLoadIdentity();
  41. gluPerspective(120.0,(GLfloat)height/(GLfloat)width,1.0,128.0);
  42. glMatrixMode(GL_MODELVIEW);
  43. glLoadIdentity();
  44. gluLookAt(0.0,1.0,3.0,0.0,0.0,0.0,0.0,1.0,0.0);
  45. }
  46.  
  47. void keyboard(unsigned char key,int x,int y)
  48. {
  49. switch(key)
  50. {
  51. case'h':
  52. printf("help\n\n");
  53. printf("c-Toggleculling\n");
  54. printf("q/escape-Quit\n\n");
  55. break;
  56. case'c':
  57. if(glIsEnabled(GL_CULL_FACE))
  58. glDisable(GL_CULL_FACE);
  59. else
  60. glEnable(GL_CULL_FACE);
  61. break;
  62. case(1):
  63. glRotatef(1.0,1.,0.,0.);
  64. break;
  65. case'2':
  66. glRotatef(1.0,0.,1.,0.);
  67. break;
  68. case'q':
  69. case 27:
  70. exit(0);
  71. break;
  72. }
  73. glutPostRedisplay();
  74. }
  75.  
  76.  
  77.  
  78.  
  79. void display()
  80. {
  81. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  82. glMatrixMode(GL_MODELVIEW);
  83. glLoadIdentity();
  84. glTranslatef(0.3f,0.0f,-9.0f);
  85. glRotatef(counter,0.0,1.0,0.0);
  86. counter+=0.01;
  87. glBegin(GL_TRIANGLES);
  88.  
  89. glColor3f(0.0f,0.5f,1.0f);
  90. glVertex3f(0.0f,1.0f,0.0f);
  91. glColor3f(1.0f,0.5f,0.0f);
  92. glVertex3f(-1.0f,-1.0f,1.0f);
  93. glColor3f(0.0f,0.5f,1.0f);
  94. glVertex3f(1.0f,-1.0f,1.0f);
  95.  
  96. glColor3f(0.75f,0.68f,1.0f);
  97. glVertex3f(0.0f,1.0f,0.0f);
  98. glColor3f(0.4f,1.0f,0.79f);
  99. glVertex3f(1.0f,-1.0f,1.0f);
  100. glColor3f(0.0f,1.0f,0.0f);
  101. glVertex3f(1.0f,-1.0f,-1.0f);
  102.  
  103. glColor3f(1.0f,0.0f,0.0f);
  104. glVertex3f(0.0f,1.0f,0.0f);
  105. glColor3f(0.0f,1.0f,0.0f);
  106. glVertex3f(1.0f,-1.0f,-1.0f);
  107. glColor3f(0.0f,0.0f,1.0f);
  108. glVertex3f(-1.0f,-1.0f,-1.0f);
  109.  
  110. glColor3f(-1.0f,0.0f,1.0f); //Red
  111. glVertex3f(0.0f,1.0f,0.0f);
  112. glColor3f(0.3f,0.25f,1.0f); //Blue
  113. glVertex3f(-1.0f,-1.0f,-1.0f);
  114. glColor3f(0.1f,1.0f,0.55f); //Green
  115. glVertex3f(-1.0f,-1.0f,1.0f);
  116.  
  117. glEnd();
  118.  
  119. glutSwapBuffers();
  120. }
  121.  
  122. int main(int argc,char**argv)
  123. {
  124.  
  125.  
  126.  
  127.  
  128.  
  129. glutInit(&argc,argv);
  130. glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);
  131. glutInitWindowSize(512,512);
  132. glutInitWindowPosition(20,20);
  133. glutCreateWindow("Torus");
  134. glutDisplayFunc(display);
  135. glutIdleFunc(display);
  136. glutReshapeFunc(reshape);
  137. glutKeyboardFunc(keyboard);
  138. initGL();
  139. glutMainLoop();
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement