Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include "glut.h"
  2. #include <ctype.h>
  3.  
  4. // camera position
  5. float camY = 5.f;
  6. float camZ = 10.f;
  7.  
  8. void init()
  9. {
  10. // rendering states
  11. glEnable(GL_DEPTH_TEST);
  12. glEnable(GL_NORMALIZE);
  13. glShadeModel(GL_SMOOTH);
  14. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  15. glEnable(GL_LIGHTING);
  16. glEnable(GL_LIGHT0);
  17.  
  18. GLfloat clrWhite[] = { 1.0f, 1.0f, 1.0f };
  19. GLfloat posLight[] = { 3.0f, 3.0f, 0.0f, 1.0f };
  20. glLightfv(GL_LIGHT0, GL_AMBIENT, clrWhite); // ambient light colour
  21. glLightfv(GL_LIGHT0, GL_DIFFUSE, clrWhite); // diffuse light colour
  22. glLightfv(GL_LIGHT0, GL_POSITION, posLight); // light position
  23.  
  24.  
  25. }
  26. void render()
  27. {
  28. static float theta = 0.0f;
  29.  
  30. // clear screen and buffers
  31. glClearColor(0.18f, 0.25f, 0.22f, 1.0f); // deep grey background
  32. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  33.  
  34. // setup the view point
  35. glMatrixMode(GL_MODELVIEW);
  36. glLoadIdentity();
  37. gluLookAt(0.0, camY, camZ,
  38. 0.0, camY - 5, camZ - 15,
  39. 0.0, 1.0, 0.0);
  40.  
  41. //camera
  42. glRotatef(theta, 0.0f, 1.0f, 0.0f);
  43.  
  44. //material
  45. float clr2[] = { 0.0f, 0.0f, 0.0f };
  46. float clr[] = { 0.8f, 0.8f, 0.8f };
  47.  
  48.  
  49. //render
  50. glTranslatef(3.0f, -1.0f, 1.0f);
  51. glRotatef(-theta, 1.0f, 1.0f, 0.0f);
  52. glMaterialfv(GL_FRONT, GL_DIFFUSE, clr);
  53. glutSolidTeapot(2.0);
  54. glMaterialfv(GL_FRONT, GL_DIFFUSE, clr2);
  55. glutWireTeapot(2.02);
  56.  
  57.  
  58.  
  59. //animation
  60. static GLint prev_time = 0;
  61. int time = glutGet(GLUT_ELAPSED_TIME);
  62. theta += (time - prev_time) * 0.01;
  63. prev_time = time;
  64. glutPostRedisplay();
  65.  
  66. // essential for double-buffering technique
  67. glutSwapBuffers();
  68.  
  69. }
  70. void reshape(int w, int h)
  71. {
  72. // find screen aspect ratio
  73. float ratio = w * 1.0 / h; // we hope that h is not zero
  74.  
  75. // setup the projection matrix
  76. glMatrixMode(GL_PROJECTION);
  77. glLoadIdentity();
  78. glViewport(0, 0, w, h);
  79. gluPerspective(60.0f, ratio, 1.0f, 100.0f);
  80.  
  81. // set model/view matrix for rendering
  82. glMatrixMode(GL_MODELVIEW);
  83.  
  84. }
  85. void onKeyDown(unsigned char key, int x, int y)
  86. {
  87.  
  88. switch (tolower(key))
  89. {
  90. case 'w': camZ -= 0.4; break;
  91. case 's': camZ += 0.4; break;
  92. case 'x': camY += 0.4; break;
  93. case 'z': camY -= 0.4; break;
  94. }
  95.  
  96. }
  97. int main(int argc, char **argv)
  98. {
  99.  
  100.  
  101. // init GLUT and create Window
  102. glutInit(&argc, argv);
  103. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  104. glutInitWindowPosition(100, 100);
  105. glutInitWindowSize(800, 600);
  106. glutCreateWindow("CI5520 3D Graphics Programming");
  107.  
  108. // register callbacks
  109. glutDisplayFunc(render);
  110. glutReshapeFunc(reshape);
  111. glutKeyboardFunc(onKeyDown);
  112.  
  113. // init light and everything – not a GLUT or callback function!
  114. init();
  115.  
  116. // enter GLUT event processing cycle
  117. glutMainLoop();
  118.  
  119.  
  120. return 1;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement