Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.54 KB | None | 0 0
  1. #include "glfw-3.3.2/include/GLFW/glfw3.h"
  2. //#include <GLFW/glfw3.h>
  3. #include <iostream>
  4. #include <math.h>
  5.  
  6. #define LEFT_BOTTOM_NEAR  -1.0, -1.0,  1.0
  7. #define LEFT_BOTTOM_FAR   -1.0, -1.0, -1.0
  8. #define LEFT_TOP_NEAR     -1.0,  1.0,  1.0
  9. #define LEFT_TOP_FAR      -1.0,  1.0, -1.0
  10. #define RIGHT_BOTTOM_NEAR  1.0, -1.0,  1.0
  11. #define RIGHT_BOTTOM_FAR   1.0, -1.0, -1.0
  12. #define RIGHT_TOP_NEAR     1.0,  1.0,  1.0
  13. #define RIGHT_TOP_FAR      1.0,  1.0, -1.0
  14.  
  15. using namespace std;
  16.  
  17. double rotateX, rotateY, rotateZ;
  18. double shiftX, shiftY, shiftZ;
  19. double scale = 0.6;
  20.  
  21. void key_callback(GLFWwindow*, int, int, int, int);
  22. void mouseButton_callback(GLFWwindow*, int, int, int);
  23. void render(const string type);
  24.  
  25. int main()
  26. {
  27.     if (!glfwInit()) exit(EXIT_FAILURE);
  28.     int width = 1024, height = 850;
  29.     GLFWwindow* window = glfwCreateWindow(width, height, "Lab_2", NULL, NULL);
  30.    
  31.     if (!window)
  32.     {
  33.         glfwTerminate();
  34.         exit(EXIT_FAILURE);
  35.     }
  36.  
  37.     glfwMakeContextCurrent(window);
  38.     glfwSetKeyCallback(window , key_callback);
  39.     glfwSetMouseButtonCallback(window, mouseButton_callback);
  40.    
  41.     while (!glfwWindowShouldClose(window))
  42.     {  
  43.         glEnable(GL_DEPTH_TEST);
  44.         glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  45.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  46.         glfwGetFramebufferSize(window, &width, &height);
  47.         glMatrixMode(GL_PROJECTION);
  48.         glLoadIdentity();
  49.         glViewport(0, 0, width, height);
  50.         glOrtho(-2 * ((double)width / height), 2 * ((double)width / height),
  51.              -2, 2, -2 * ((double)width / height), 2 * ((double)width / height));
  52.  
  53.         glRotatef(asin(tan(30 * M_PI/180)) * 180 / M_PI, 3, 0, 0);
  54.         glRotatef(45, 0, 1, 0);
  55.        
  56.         glMatrixMode(GL_MODELVIEW);
  57.         glLoadIdentity();
  58.    
  59.         render("dynamic");
  60.         render("static");
  61.  
  62.         glfwSwapBuffers(window);
  63.         glfwPollEvents();
  64.     }
  65.  
  66.     glfwDestroyWindow(window);
  67.     glfwTerminate();
  68.     exit(EXIT_SUCCESS);
  69. }
  70.  
  71. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  72. {
  73.     if (key == GLFW_KEY_R && action == GLFW_PRESS)
  74.     {
  75.         rotateY += 5;
  76.         if (rotateY >= 360) rotateY -= 360;
  77.     }
  78.     if (key == GLFW_KEY_E && action == GLFW_PRESS)
  79.     {
  80.         rotateY -= 5;
  81.         if (rotateY < 0) rotateY += 360;
  82.     }
  83.     if (key == GLFW_KEY_G && action == GLFW_PRESS)
  84.     {
  85.         rotateX += 5;
  86.         if (rotateX >= 360) rotateX -= 360;
  87.     }
  88.     if (key == GLFW_KEY_F && action == GLFW_PRESS)
  89.     {
  90.         rotateX -= 5;
  91.         if (rotateX < 0) rotateX += 360;
  92.     }
  93.     if (key == GLFW_KEY_B && action == GLFW_PRESS)
  94.     {
  95.         rotateZ += 5;
  96.         if (rotateZ >= 360) rotateZ -= 360;
  97.     }
  98.     if (key == GLFW_KEY_V && action == GLFW_PRESS)
  99.     {
  100.         rotateZ -= 5;
  101.         if (rotateZ < 0) rotateZ += 360;
  102.     }
  103.     if (key == GLFW_KEY_1 && action == GLFW_PRESS)
  104.     {
  105.         shiftY -= 0.1;
  106.     }
  107.     if (key == GLFW_KEY_2 && action == GLFW_PRESS)
  108.     {
  109.         shiftY += 0.1;
  110.     }
  111.     if (key == GLFW_KEY_3 && action == GLFW_PRESS)
  112.     {
  113.         shiftX -= 0.1;
  114.     }
  115.     if (key == GLFW_KEY_4 && action == GLFW_PRESS)
  116.     {
  117.         shiftX += 0.1;
  118.     }
  119.     if (key == GLFW_KEY_5 && action == GLFW_PRESS)
  120.     {
  121.         shiftZ -= 0.1;
  122.     }
  123.     if (key == GLFW_KEY_6 && action == GLFW_PRESS)
  124.     {
  125.         shiftZ += 0.1;
  126.     }
  127.     if (key == GLFW_KEY_Z && action == GLFW_PRESS)
  128.     {
  129.         scale += 0.05;
  130.     }
  131.     if (key == GLFW_KEY_X && action == GLFW_PRESS)
  132.     {
  133.         scale -= 0.05;
  134.     }
  135. }
  136.  
  137. void mouseButton_callback(GLFWwindow* window, int button, int action, int mods) {
  138.     if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  139.         glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
  140.     }
  141.     if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
  142.         glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  143.     }
  144. }
  145.  
  146. void render(const string type)
  147. {
  148.     glPushMatrix();
  149.     if (type == "dynamic")
  150.     {
  151.         glRotated(rotateX, 2, 0, 0);
  152.         glRotated(rotateY, 0, 2, 0);
  153.         glRotated(rotateZ, 0, 0, 2);
  154.         glScaled(scale, scale, scale);
  155.         glTranslated(shiftX, shiftY, shiftZ);
  156.     }
  157.     else
  158.     {
  159.         glTranslated(-2, 0, 0);  
  160.         glScaled(0.2, 0.2, 0.2);
  161.     }
  162.  
  163.     glBegin(GL_QUADS);
  164.     glColor3f(1.0, 0.0, 0.0);
  165.     glVertex3f(LEFT_BOTTOM_FAR);
  166.     glVertex3f(LEFT_BOTTOM_NEAR);
  167.     glVertex3f(RIGHT_BOTTOM_NEAR);
  168.     glVertex3f(RIGHT_BOTTOM_FAR);
  169.     glEnd();
  170.  
  171.     glBegin(GL_QUADS);
  172.     glColor3f(1.0, 0.0, 0.0);
  173.     glVertex3f(LEFT_TOP_FAR);
  174.     glVertex3f(LEFT_TOP_NEAR);
  175.     glVertex3f(RIGHT_TOP_NEAR);
  176.     glVertex3f(RIGHT_TOP_FAR);
  177.     glEnd();
  178.  
  179.     glBegin(GL_QUADS);
  180.     glColor3f(0.0, 0.0, 1.0);
  181.     glVertex3f(RIGHT_BOTTOM_NEAR);
  182.     glVertex3f(LEFT_BOTTOM_NEAR);
  183.     glVertex3f(LEFT_TOP_NEAR);
  184.     glVertex3f(RIGHT_TOP_NEAR);
  185.     glEnd();
  186.  
  187.     glBegin(GL_QUADS);
  188.     glColor3f(0.0, 0.0, 1.0);
  189.     glVertex3f(RIGHT_BOTTOM_FAR);
  190.     glVertex3f(LEFT_BOTTOM_FAR);
  191.     glVertex3f(LEFT_TOP_FAR);
  192.     glVertex3f(RIGHT_TOP_FAR);
  193.     glEnd();
  194.  
  195.     glBegin(GL_QUADS);
  196.     glColor3f(0.0, 1.0, 0.0);
  197.     glVertex3f(RIGHT_BOTTOM_FAR);
  198.     glVertex3f(RIGHT_BOTTOM_NEAR);
  199.     glVertex3f(RIGHT_TOP_NEAR);
  200.     glVertex3f(RIGHT_TOP_FAR);
  201.     glEnd();
  202.  
  203.     glBegin(GL_QUADS);
  204.     glColor3f(0.0, 1.0, 0.0);
  205.     glVertex3f(LEFT_BOTTOM_FAR);
  206.     glVertex3f(LEFT_BOTTOM_NEAR);
  207.     glVertex3f(LEFT_TOP_NEAR);
  208.     glVertex3f(LEFT_TOP_FAR);
  209.     glEnd();
  210.  
  211.     glPopMatrix();
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement