Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1.  
  2. #include <glfw3.h>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #define SCREEN_WIDTH 1024
  6. #define SCREEN_HEIGHT 720
  7.  
  8. void keyCallback( GLFWwindow *window, int key, int scancode, int action, int mods );
  9. void DrawCube( GLfloat centerPosX, GLfloat centerPosY, GLfloat centerPosZ, GLfloat edgeLength );
  10. void reSizeWindowCallback(GLFWwindow *window, int width, int height);
  11. void drawLines();
  12.  
  13. GLfloat rotationX = 0.0f;
  14. GLfloat rotationY = 0.0f;
  15. GLfloat rotationZ = 0.0f;
  16. GLfloat halfScreenWidth = SCREEN_WIDTH / 2;
  17. GLfloat halfScreenHeight = SCREEN_HEIGHT / 2;
  18. GLfloat CubsLength = -500;
  19. GLfloat CubsHeight = 200;
  20. int gradation = 20;
  21.  
  22. int main( void ){
  23. GLFWwindow *window;
  24. //инициализируем библиотеку
  25. if ( !glfwInit( ) ){
  26. return -1;
  27. }
  28.  
  29. // Настраиваем размер окна
  30. window = glfwCreateWindow( SCREEN_WIDTH, SCREEN_HEIGHT, "Rotating Hyperboloid", NULL, NULL );
  31. glfwSetKeyCallback( window, keyCallback );
  32. glfwSetWindowSizeCallback(window, reSizeWindowCallback);
  33. glfwSetInputMode( window, GLFW_STICKY_KEYS, 1 );
  34. int screenWidth, screenHeight;
  35.  
  36. glfwGetFramebufferSize( window, &screenWidth, &screenHeight );
  37. if (!window ){
  38. glfwTerminate( );
  39. return -1;
  40. }
  41.  
  42. glfwMakeContextCurrent( window );
  43. glViewport( 0.0f, 0.0f, screenWidth, screenHeight );
  44. glMatrixMode( GL_PROJECTION );
  45. glLoadIdentity( );
  46. //создает наклонную проекцию
  47. glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1000 );
  48. glMatrixMode( GL_MODELVIEW );
  49. glLoadIdentity( );
  50.  
  51.  
  52.  
  53. while ( !glfwWindowShouldClose(window)){
  54. glClearColor(0, 0,0 , 0);
  55. glClear( GL_COLOR_BUFFER_BIT );
  56. glPushMatrix();
  57.  
  58. //Смещение координатной оси
  59. glTranslatef(halfScreenWidth, halfScreenHeight, -500 );
  60. // glOrtho(-100, 100, -100, 100, -100, 100);
  61. glRotatef(rotationX, 1, 0, 0 );
  62. glRotatef(rotationY, 0, 1, 0 );
  63. glRotatef(rotationZ, 0, 0, 1);
  64.  
  65. glTranslatef(-halfScreenWidth, -halfScreenHeight, 500 );
  66.  
  67. DrawCube(halfScreenWidth, halfScreenHeight, CubsLength, CubsHeight);
  68. // drawLines();
  69. glPopMatrix();
  70.  
  71. glfwSwapBuffers(window);
  72. glfwPollEvents();
  73. }
  74. glfwTerminate();
  75. return 0;
  76. }
  77.  
  78. void reSizeWindowCallback(GLFWwindow *window, int width, int height){
  79. glViewport(0.0f, 0.0f, width, height);
  80. glMatrixMode( GL_PROJECTION );
  81. glLoadIdentity( );
  82. //создает наклонную проекцию
  83. glOrtho(0, width, 0, height, 0, 1000 );
  84. glMatrixMode(GL_PROJECTION);
  85. glLoadIdentity();
  86. halfScreenHeight = SCREEN_HEIGHT/2;
  87. halfScreenWidth = SCREEN_WIDTH/2;
  88. DrawCube(width/2, height/2, CubsLength, CubsHeight);
  89. }
  90.  
  91.  
  92. void keyCallback( GLFWwindow *window, int key, int scancode, int action, int mods ){
  93. if (action == GLFW_PRESS || action == GLFW_REPEAT ){
  94. switch (key){
  95. case GLFW_KEY_UP:
  96. rotationX -= 20;
  97. break;
  98. case GLFW_KEY_DOWN:
  99. rotationX += 20;
  100. break;
  101. case GLFW_KEY_RIGHT:
  102. rotationY += 20;
  103. break;
  104. case GLFW_KEY_LEFT:
  105. rotationY -= 20;
  106. break;
  107. case GLFW_KEY_Z:
  108. rotationZ +=20;
  109. break;
  110. case GLFW_KEY_X:
  111. rotationZ -=20;
  112. break;
  113. case GLFW_KEY_EQUAL:
  114. CubsHeight += 4;
  115. break;
  116. case GLFW_KEY_MINUS:
  117. CubsHeight -= 4;
  118. break;
  119. case GLFW_KEY_W:
  120. halfScreenHeight +=5;
  121. break;
  122. case GLFW_KEY_A:
  123. halfScreenWidth -=5;
  124. break;
  125. case GLFW_KEY_S:
  126. halfScreenHeight -=5;
  127. break;
  128. case GLFW_KEY_D:
  129. halfScreenWidth +=5;
  130. break;
  131. case GLFW_KEY_F:
  132. glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  133. break;
  134. case GLFW_KEY_L:
  135. glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
  136. break;
  137. case GLFW_KEY_ESCAPE:
  138. glfwSetWindowShouldClose(window,GL_TRUE);
  139. break;
  140. }
  141. }
  142. }
  143.  
  144. void DrawCube( GLfloat X, GLfloat Y, GLfloat Z, GLfloat edgeLength ){
  145.  
  146. GLfloat x, y, z, alpha, beta; // Storage for coordinates and angles
  147. int gradation = 50;
  148.  
  149. glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
  150. for (alpha = - M_PI/2; alpha < M_PI/2; alpha += M_PI/gradation){
  151. glBegin(GL_TRIANGLE_STRIP);
  152. for (beta = -M_PI; beta < M_PI; beta += M_PI/gradation){
  153. x = 10 * cos(beta)/cos(alpha);
  154. y = 10 * sin(beta)/ cos(alpha);
  155. z = 10 * tan(alpha);
  156.  
  157. glVertex3f(x + 300, y + 400, z);
  158. glColor3f(1, 1, 1);
  159. }
  160. glEnd();
  161. }
  162.  
  163.  
  164. glEnable(GL_DEPTH_TEST);
  165. // glEnableClientState( GL_COLOR_ARRAY );
  166. // glEnableClientState( GL_VERTEX_ARRAY );
  167. // glVertexPointer( 3, GL_FLOAT, 0, vertices );
  168. // glColorPointer(3, GL_FLOAT, 0, cubeColors);
  169. // glDrawArrays( GL_QUADS, 0, 24 );
  170. // glMatrixMode(GL_PROJECTION);
  171. // glPushMatrix();
  172. // glLoadIdentity();
  173.  
  174. // glMatrixMode(GL_MODELVIEW);
  175. // glLoadIdentity();
  176. // glDisable(GL_CULL_FACE);
  177.  
  178.  
  179. // glClear(GL_DEPTH_BUFFER_BIT);
  180.  
  181.  
  182.  
  183. //drawing cube
  184. glBegin(GL_QUADS);
  185. glColor3f(1.0f, 0.0f, 0.0);
  186. glVertex2f(850.0, 600.0);
  187. glVertex2f(950.0, 600.0);
  188. glVertex2f(950.0, 700.0);
  189. glVertex2f(850.0, 700.0);
  190.  
  191. glColor3f(1.0f, 0.0f, 0.0);
  192. glVertex2f(950.0, 700.0);
  193. glVertex2f(975.0, 675.0);
  194. glVertex2f(975.0, 575.0);
  195. glVertex2f(950.0, 600.0);
  196.  
  197. glColor3f(0.0f, 0.0f, 1.0);
  198. glVertex2f(950.0, 600.0);
  199. glVertex2f(975.0, 575.0 );
  200. glVertex2f(875.0, 575.0);
  201. glVertex2f(850.0, 600.0);
  202. glEnd();
  203.  
  204.  
  205.  
  206. glMatrixMode(GL_PROJECTION);
  207. glPopMatrix();
  208. glMatrixMode(GL_MODELVIEW);
  209.  
  210.  
  211. glDisableClientState( GL_VERTEX_ARRAY );
  212. glDisableClientState(GL_COLOR_ARRAY);
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement