Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 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.         glMatrixMode(GL_MODELVIEW);
  53.         glLoadIdentity();
  54.         glRotatef(asin(tan(30 * M_PI/180)) * 180 / M_PI, 3, 0, 0);
  55.         glRotatef(45, 0, 3, 0);
  56.    
  57.         render("dynamic");
  58.         render("static");
  59.  
  60.         glfwSwapBuffers(window);
  61.         glfwPollEvents();
  62.     }
  63.  
  64.     glfwDestroyWindow(window);
  65.     glfwTerminate();
  66.     exit(EXIT_SUCCESS);
  67. }
  68.  
  69. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  70. {
  71.     if (key == GLFW_KEY_R && action == GLFW_PRESS)
  72.     {
  73.         rotateY += 5;
  74.         if (rotateY >= 360) rotateY -= 360;
  75.     }
  76.     if (key == GLFW_KEY_E && action == GLFW_PRESS)
  77.     {
  78.         rotateY -= 5;
  79.         if (rotateY < 0) rotateY += 360;
  80.     }
  81.     if (key == GLFW_KEY_G && action == GLFW_PRESS)
  82.     {
  83.         rotateX += 5;
  84.         if (rotateX >= 360) rotateX -= 360;
  85.     }
  86.     if (key == GLFW_KEY_F && action == GLFW_PRESS)
  87.     {
  88.         rotateX -= 5;
  89.         if (rotateX < 0) rotateX += 360;
  90.     }
  91.     if (key == GLFW_KEY_B && action == GLFW_PRESS)
  92.     {
  93.         rotateZ += 5;
  94.         if (rotateZ >= 360) rotateZ -= 360;
  95.     }
  96.     if (key == GLFW_KEY_V && action == GLFW_PRESS)
  97.     {
  98.         rotateZ -= 5;
  99.         if (rotateZ < 0) rotateZ += 360;
  100.     }
  101.     if (key == GLFW_KEY_1 && action == GLFW_PRESS)
  102.     {
  103.         shiftY -= 0.1;
  104.     }
  105.     if (key == GLFW_KEY_2 && action == GLFW_PRESS)
  106.     {
  107.         shiftY += 0.1;
  108.     }
  109.     if (key == GLFW_KEY_3 && action == GLFW_PRESS)
  110.     {
  111.         shiftX -= 0.1;
  112.     }
  113.     if (key == GLFW_KEY_4 && action == GLFW_PRESS)
  114.     {
  115.         shiftX += 0.1;
  116.     }
  117.     if (key == GLFW_KEY_5 && action == GLFW_PRESS)
  118.     {
  119.         shiftZ -= 0.1;
  120.     }
  121.     if (key == GLFW_KEY_6 && action == GLFW_PRESS)
  122.     {
  123.         shiftZ += 0.1;
  124.     }
  125.     if (key == GLFW_KEY_Z && action == GLFW_PRESS)
  126.     {
  127.         scale += 0.05;
  128.     }
  129.     if (key == GLFW_KEY_X && action == GLFW_PRESS)
  130.     {
  131.         scale -= 0.05;
  132.     }
  133. }
  134.  
  135. void mouseButton_callback(GLFWwindow* window, int button, int action, int mods) {
  136.     if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  137.         glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
  138.     }
  139.     if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
  140.         glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  141.     }
  142. }
  143.  
  144. void render(const string type)
  145. {
  146.     glPushMatrix();
  147.     if (type == "dynamic")
  148.     {
  149.         glRotated(rotateX, 2, 0, 0);
  150.         glRotated(rotateY, 0, 2, 0);
  151.         glRotated(rotateZ, 0, 0, 2);
  152.         glScaled(scale, scale, scale);
  153.         glTranslated(shiftX, shiftY, shiftZ);
  154.     }
  155.     else
  156.     {
  157.         glTranslated(-2, 0, 0);  
  158.         glScaled(0.2, 0.2, 0.2);
  159.     }
  160.  
  161.     glBegin(GL_QUADS);
  162.     glColor3f(1.0, 0.0, 0.0);
  163.     glVertex3f(LEFT_BOTTOM_FAR);
  164.     glVertex3f(LEFT_BOTTOM_NEAR);
  165.     glVertex3f(RIGHT_BOTTOM_NEAR);
  166.     glVertex3f(RIGHT_BOTTOM_FAR);
  167.     glEnd();
  168.  
  169.     glBegin(GL_QUADS);
  170.     glColor3f(1.0, 0.0, 0.0);
  171.     glVertex3f(LEFT_TOP_FAR);
  172.     glVertex3f(LEFT_TOP_NEAR);
  173.     glVertex3f(RIGHT_TOP_NEAR);
  174.     glVertex3f(RIGHT_TOP_FAR);
  175.     glEnd();
  176.  
  177.     glBegin(GL_QUADS);
  178.     glColor3f(0.0, 0.0, 1.0);
  179.     glVertex3f(RIGHT_BOTTOM_NEAR);
  180.     glVertex3f(LEFT_BOTTOM_NEAR);
  181.     glVertex3f(LEFT_TOP_NEAR);
  182.     glVertex3f(RIGHT_TOP_NEAR);
  183.     glEnd();
  184.  
  185.     glBegin(GL_QUADS);
  186.     glColor3f(0.0, 0.0, 1.0);
  187.     glVertex3f(RIGHT_BOTTOM_FAR);
  188.     glVertex3f(LEFT_BOTTOM_FAR);
  189.     glVertex3f(LEFT_TOP_FAR);
  190.     glVertex3f(RIGHT_TOP_FAR);
  191.     glEnd();
  192.  
  193.     glBegin(GL_QUADS);
  194.     glColor3f(0.0, 1.0, 0.0);
  195.     glVertex3f(RIGHT_BOTTOM_FAR);
  196.     glVertex3f(RIGHT_BOTTOM_NEAR);
  197.     glVertex3f(RIGHT_TOP_NEAR);
  198.     glVertex3f(RIGHT_TOP_FAR);
  199.     glEnd();
  200.  
  201.     glBegin(GL_QUADS);
  202.     glColor3f(0.0, 1.0, 0.0);
  203.     glVertex3f(LEFT_BOTTOM_FAR);
  204.     glVertex3f(LEFT_BOTTOM_NEAR);
  205.     glVertex3f(LEFT_TOP_NEAR);
  206.     glVertex3f(LEFT_TOP_FAR);
  207.     glEnd();
  208.  
  209.     glPopMatrix();
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement