Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. //ex2.
  2.  
  3. #include <GL/freeglut.h>
  4. #include <stdlib.h>
  5.  
  6. #include <stdio.h>
  7. #include <math.h>
  8. using namespace std;
  9. #include "camera.h"
  10. #include "keyboard.h"
  11. #include "mouse.h"
  12.  
  13. Camera *cam;
  14.  
  15. const GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
  16. const GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
  17. const GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
  18. const GLfloat light_position[] = {2.0f, 5.0f, 5.0f, 0.0f};
  19.  
  20. void initGL(int width, int height)
  21. {
  22. cam = new Camera();
  23.  
  24. glEnable(GL_CULL_FACE);
  25. glCullFace(GL_BACK);
  26.  
  27. glEnable(GL_DEPTH_TEST);
  28. glDepthFunc(GL_LESS);
  29.  
  30. glEnable(GL_COLOR_MATERIAL);
  31.  
  32. glMatrixMode(GL_PROJECTION);
  33.  
  34. glEnable(GL_LIGHT0);
  35. glEnable(GL_LIGHTING);
  36.  
  37. glLoadIdentity();
  38. gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 2.0f,100.0f);
  39. glMatrixMode(GL_MODELVIEW);
  40. }
  41.  
  42. static void resize(int width, int height)
  43. {
  44. glViewport(0,0,width,height);
  45. glMatrixMode(GL_PROJECTION);
  46. glLoadIdentity();
  47. gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 2.0f,100.0f);
  48. glMatrixMode(GL_MODELVIEW);
  49. }
  50.  
  51. void renderBitmapString(float x, float y, void *font, char *string)
  52. {
  53. glMatrixMode(GL_MODELVIEW);
  54. glPushMatrix();
  55. glLoadIdentity();
  56. glMatrixMode(GL_PROJECTION);
  57. glPushMatrix();
  58. glLoadIdentity();
  59. glDisable(GL_LIGHTING);
  60. glColor3f(0.0f,1.0f,0.0f);
  61. glRasterPos2f(x,y);
  62. char *c;
  63. for(c=string; *c != '\0';c++)
  64. {
  65. glutBitmapCharacter(font, *c);
  66. }
  67. glEnable(GL_LIGHTING);
  68. glPopMatrix();
  69. glMatrixMode(GL_MODELVIEW);
  70. glPopMatrix();
  71. }
  72.  
  73. static void display(void)
  74. {
  75. static int frame, timebase=0;
  76. int time;
  77. char s[100];
  78. frame++;
  79. time=glutGet(GLUT_ELAPSED_TIME);
  80.  
  81. if(time - timebase>1000)
  82. {
  83. sprintf(s,"[FPS:%4.2f] Lab 4: Navigare", frame*1000.0/(time-timebase));
  84. glutSetWindowTitle(s);
  85. timebase= time;
  86. frame=0;
  87. }
  88. static int lasttime=0;
  89. int rightnow = glutGet(GLUT_ELAPSED_TIME);
  90. float dt = ((float)(rightnow-lasttime))/1000.0f;
  91. lasttime = rightnow;
  92. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  93. glLoadIdentity();
  94.  
  95. cam->applyTransformation();
  96. cam->updateInteraction(dt);
  97.  
  98. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  99. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  100. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  101. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  102.  
  103. static float axisRot = 0.0f;
  104. static float globRotR = 0.0f;
  105. static float globRotG = 120.0f;
  106. static float globRotB = 240.0f;
  107.  
  108. glColor3f(1.0f,0.0f,0.0f);
  109. glPushMatrix();
  110. glTranslatef(0.0f,0.0f,-20);
  111. glRotatef(globRotR, 0, 0, 1);
  112. glTranslatef(5.0f,0.0f,0.0f);
  113. glRotatef(axisRot,0,1,0);
  114. glutSolidCube(2);
  115. glPopMatrix();
  116.  
  117. glColor3f(0.0f,1.0f,0.0f);
  118. glPushMatrix();
  119. glTranslatef(0.0f,0.0f,-20);
  120. glRotatef(globRotG, 0, 0, 1);
  121. glTranslatef(5.0f,0.0f,0.0f);
  122. glRotatef(axisRot,0,1,0);
  123. glutSolidCube(2);
  124. glPopMatrix();
  125.  
  126. glColor3f(0.0f,0.0f,1.0f);
  127. glPushMatrix();
  128. glTranslatef(0.0f,0.0f,-20);
  129. glRotatef(globRotB, 0, 0, 1);
  130. glTranslatef(5.0f,0.0f,0.0f);
  131. glRotatef(axisRot,0,1,0);
  132. glutSolidCube(2);
  133. glPopMatrix();
  134.  
  135. axisRot += 1.0f; axisRot=fmod(axisRot,360.0f);
  136. globRotR += 0.5f; globRotR=fmod(globRotR,360.0f);
  137. globRotG += 0.5f; globRotG=fmod(globRotG,360.0f);
  138. globRotB += 0.5f; globRotB=fmod(globRotB,360.0f);
  139.  
  140. //punct de origine
  141. glColor3f(1.0f,1.0f,1.0f);
  142. glPushMatrix();
  143. glutSolidSphere(1,10,10);
  144. glPopMatrix();
  145.  
  146. glutSwapBuffers();
  147. }
  148.  
  149. static void idle(void)
  150. {
  151. glutPostRedisplay();
  152. }
  153.  
  154. int main(int argc, char *argv[])
  155. {
  156. int width=640;
  157. int height=480;
  158. glutInit(&argc, argv);
  159. glutInitWindowSize(width, height);
  160. glutInitWindowPosition(10,10);
  161. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  162. glutCreateWindow("");
  163. glutReshapeFunc(resize);
  164. glutDisplayFunc(display);
  165. glutKeyboardFunc(Keyboard::keyDown);
  166. glutKeyboardUpFunc(Keyboard::keyUp);
  167. glutIdleFunc(idle);
  168. initGL(width, height);
  169.  
  170. glutMainLoop();
  171.  
  172. return EXIT_SUCCESS;
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement