Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.69 KB | None | 0 0
  1. #include <iostream>
  2. #define GLEW_STATIC
  3. #include <GL/glew.h>
  4. #include <GLFW/glfw3.h>
  5. //#include <GL/GL.h>
  6. //#include <GL/GLU.h>
  7. #include <SOIL.h>
  8.  
  9. #include <glm/glm.hpp>
  10. #include <glm/gtc/matrix_transform.hpp>
  11. #include <glm/gtc/type_ptr.hpp>
  12.  
  13. #include "Shader.h"
  14.  
  15.  
  16. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
  17. void mouse_callback(GLFWwindow* window, double xpos, double ypos);
  18. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
  19. void do_movement();
  20.  
  21. const GLuint WIDTH = 800, HEIGHT = 600;
  22.  
  23. glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
  24. glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
  25. glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
  26. GLfloat yaw = -90.0f;   // Yaw is initialized to -90.0 degrees since a yaw of 0.0 results in a direction vector pointing to the right (due to how Eular angles work) so we initially rotate a bit to the left.
  27. GLfloat pitch = 0.0f;
  28. GLfloat lastX = WIDTH / 2.0;
  29. GLfloat lastY = HEIGHT / 2.0;
  30. GLfloat fov = 45.0f;
  31. bool keys[1024];
  32.  
  33. // Deltatime
  34. GLfloat deltaTime = 0.0f;   // Time between current frame and last frame
  35. GLfloat lastFrame = 0.0f;   // Time of last frame
  36.  
  37. int main()
  38. {
  39.     //Инициализация GLFW
  40.     glfwInit();
  41.     //Настройка GLFW
  42.     //Задается минимальная требуемая версия OpenGL.
  43.     //Мажорная
  44.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  45.     //Минорная
  46.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
  47.     //Установка профайла для которого создается контекст
  48.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  49.     //Выключение возможности изменения размера окна
  50.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  51.  
  52.     GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "ProjectKovaleva", nullptr, nullptr);
  53.     glfwMakeContextCurrent(window);
  54.  
  55.     glfwSetKeyCallback(window, key_callback);
  56.     glfwSetCursorPosCallback(window, mouse_callback);
  57.     glfwSetScrollCallback(window, scroll_callback);
  58.  
  59.     // GLFW Options
  60.     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  61.  
  62.     glewExperimental = GL_TRUE;
  63.  
  64.     glewInit();
  65.  
  66.  
  67. //  int width, height;
  68. //  glfwGetFramebufferSize(window, &width, &height);
  69. //  glViewport(0, 0, width, height);
  70.     glViewport(0, 0, WIDTH, HEIGHT);
  71.     glEnable(GL_DEPTH_TEST);
  72.  
  73.     Shader ourShader("C:\\Users\\Виктория\\Desktop\\проги\\MashGraph\\MashGraph\\textures.vs.txt", "C:\\Users\\Виктория\\Desktop\\проги\\MashGraph\\MashGraph\\textures.frag.txt");
  74.  
  75.  
  76.     /*GLfloat vertices[] = {
  77.         // Positions          // Texture Coords
  78.          0.5f,  0.5f, 0.0f,   1.0f, 1.0f, // Top Right
  79.          0.5f, -0.5f, 0.0f,   1.0f, 0.0f, // Bottom Right
  80.         -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, // Bottom Left
  81.         -0.5f,  0.5f, 0.0f,   0.0f, 1.0f  // Top Left
  82.     };
  83.     GLuint indices[] = {  // Note that we start from 0!
  84.         0, 1, 3, // First Triangle
  85.         1, 2, 3  // Second Triangle
  86.     };*/
  87.  
  88.     GLfloat vertices[] = {
  89.         -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
  90.          0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
  91.          0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
  92.          0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
  93.         -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
  94.         -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
  95.  
  96.         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
  97.          0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
  98.          0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
  99.          0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
  100.         -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
  101.         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
  102.  
  103.         -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  104.         -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
  105.         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  106.         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  107.         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
  108.         -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  109.  
  110.          0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  111.          0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
  112.          0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  113.          0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  114.          0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
  115.          0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  116.  
  117.         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  118.          0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
  119.          0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
  120.          0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
  121.         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
  122.         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
  123.  
  124.         -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
  125.          0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
  126.          0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  127.          0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
  128.         -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
  129.         -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
  130.     };
  131.     // World space positions of our cubes
  132.     glm::vec3 cubePositions[] = {
  133.         glm::vec3(0.0f,  0.0f,  0.0f),
  134.         glm::vec3(2.0f,  5.0f, -15.0f),
  135.         glm::vec3(-1.5f, -2.2f, -2.5f),
  136.         glm::vec3(-3.8f, -2.0f, -12.3f),
  137.         glm::vec3(2.4f, -0.4f, -3.5f),
  138.         glm::vec3(-1.7f,  3.0f, -7.5f),
  139.         glm::vec3(1.3f, -2.0f, -2.5f),
  140.         glm::vec3(1.5f,  2.0f, -2.5f),
  141.         glm::vec3(1.5f,  0.2f, -1.5f),
  142.         glm::vec3(-1.3f,  1.0f, -1.5f)
  143.     };
  144.  
  145.     GLuint VBO, VAO/*, EBO*/;
  146.     glGenVertexArrays(1, &VAO);
  147.     glGenBuffers(1, &VBO);
  148. //  glGenBuffers(1, &EBO);
  149.  
  150.     glBindVertexArray(VAO);
  151.  
  152.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  153.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  154.  
  155. //  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  156. //  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  157.    
  158.  
  159.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
  160.     glEnableVertexAttribArray(0);
  161.  
  162. //  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
  163. //  glEnableVertexAttribArray(1);
  164.    
  165.     glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
  166.     glEnableVertexAttribArray(2);
  167.    
  168.     glBindVertexArray(0);
  169.  
  170.  
  171.     // Загрузим и создадим текстуру
  172.     GLuint texture1;
  173.     GLuint texture2;
  174.  
  175.     ////////////////// Texture1
  176.     glGenTextures(1, &texture1);
  177.     glBindTexture(GL_TEXTURE_2D, texture1);
  178.  
  179.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  180.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  181.  
  182.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  183.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  184.  
  185.     int width, height;
  186.     unsigned char* image = SOIL_load_image("C:\\Users\\Виктория\\Desktop\\проги\\MashGraph\\MashGraph\\wood.jpg", &width, &height, 0, SOIL_LOAD_RGB);
  187.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  188.     glGenerateMipmap(GL_TEXTURE_2D);
  189.     SOIL_free_image_data(image);
  190.     glBindTexture(GL_TEXTURE_2D, 0);
  191.    
  192.  
  193.     /////////////////////////// Texture2
  194.     glGenTextures(1, &texture2);
  195.     glBindTexture(GL_TEXTURE_2D, texture2);
  196.  
  197.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  198.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  199.  
  200.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  201.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  202.  
  203.    
  204.     image = SOIL_load_image("C:\\Users\\Виктория\\Desktop\\проги\\MashGraph\\MashGraph\\200px-Awesome.svg.png", &width, &height, 0, SOIL_LOAD_RGB);
  205.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  206.     glGenerateMipmap(GL_TEXTURE_2D);
  207.     SOIL_free_image_data(image);
  208.     glBindTexture(GL_TEXTURE_2D, 0);
  209.  
  210.  
  211.     while (!glfwWindowShouldClose(window))
  212.     {
  213.         // Calculate deltatime of current frame
  214.         GLfloat currentFrame = glfwGetTime();
  215.         deltaTime = currentFrame - lastFrame;
  216.         lastFrame = currentFrame;
  217.  
  218.         glfwPollEvents();
  219.         do_movement();
  220.  
  221.         glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  222.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  223.  
  224.    
  225.         glActiveTexture(GL_TEXTURE0);
  226.         glBindTexture(GL_TEXTURE_2D, texture1);
  227.         glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0);
  228.         glActiveTexture(GL_TEXTURE1);
  229.         glBindTexture(GL_TEXTURE_2D, texture2);
  230.         glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture2"), 1);
  231.  
  232.         ourShader.Use();
  233.  
  234.     //  glm::mat4 transform;
  235.     //  transform = glm::translate(transform, glm::vec3(0.5f, -0.5f, 0.0f));
  236.     //  transform = glm::rotate(transform, (GLfloat)glfwGetTime() * 10.0f, glm::vec3(0.0f, 0.0f, 1.0f));
  237.  
  238.  
  239.     //  GLuint transformLoc = glGetUniformLocation(ourShader.Program, "transform");
  240.     //  glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
  241.        
  242.  
  243.         glm::mat4 view;
  244.         view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
  245.         // Projection
  246.         glm::mat4 projection;
  247.         projection = glm::perspective(fov, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
  248.         // Get the uniform locations
  249.         GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
  250.         GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
  251.         GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
  252.         // Pass the matrices to the shader
  253.         glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
  254.         glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
  255.  
  256.         glBindVertexArray(VAO);
  257.  
  258.         for (GLuint i = 0; i < 10; i++)
  259.         {
  260.             // Calculate the model matrix for each object and pass it to shader before drawing
  261.             glm::mat4 model;
  262.             model = glm::translate(model, cubePositions[i]);
  263.             GLfloat angle = 20.0f * i;
  264.             model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
  265.             glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  266.  
  267.             glDrawArrays(GL_TRIANGLES, 0, 36);
  268.         }
  269.  
  270.     //  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  271.     //  glDrawArrays(GL_TRIANGLES, 0, 36);
  272.        
  273.        
  274.         /////////////// Вторая трансформация
  275.     //  transform = glm::mat4(1.0f);
  276.     //  transform = glm::translate(transform, glm::vec3(-0.5f, 0.5f, 0.0f));
  277.     //  GLfloat scaleAmount = sin(glfwGetTime());
  278.     //  transform = glm::scale(transform, glm::vec3(scaleAmount, scaleAmount, scaleAmount));
  279.     //  glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
  280.  
  281.     //  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  282.        
  283.         glBindVertexArray(0);
  284.  
  285.         glfwSwapBuffers(window);
  286.     }
  287.  
  288.     glDeleteVertexArrays(1, &VAO);
  289.     glDeleteBuffers(1, &VBO);
  290. //  glDeleteBuffers(1, &EBO);
  291.  
  292.     glfwTerminate();
  293.     return 0;
  294. }
  295.  
  296.  
  297. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  298. {
  299.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  300.         glfwSetWindowShouldClose(window, GL_TRUE);
  301.     if (key >= 0 && key < 1024)
  302.     {
  303.         if (action == GLFW_PRESS)
  304.             keys[key] = true;
  305.         else if (action == GLFW_RELEASE)
  306.             keys[key] = false;
  307.     }
  308. }
  309.  
  310. void do_movement()
  311. {
  312.     // Camera controls
  313.     GLfloat cameraSpeed = 5.0f * deltaTime;
  314.     if (keys[GLFW_KEY_W])
  315.         cameraPos += cameraSpeed * cameraFront;
  316.     if (keys[GLFW_KEY_S])
  317.         cameraPos -= cameraSpeed * cameraFront;
  318.     if (keys[GLFW_KEY_A])
  319.         cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
  320.     if (keys[GLFW_KEY_D])
  321.         cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
  322. }
  323.  
  324. bool firstMouse = true;
  325. void mouse_callback(GLFWwindow* window, double xpos, double ypos)
  326. {
  327.     if (firstMouse)
  328.     {
  329.         lastX = xpos;
  330.         lastY = ypos;
  331.         firstMouse = false;
  332.     }
  333.  
  334.     GLfloat xoffset = xpos - lastX;
  335.     GLfloat yoffset = lastY - ypos; // Reversed since y-coordinates go from bottom to left
  336.     lastX = xpos;
  337.     lastY = ypos;
  338.  
  339.     GLfloat sensitivity = 0.05; // Change this value to your liking
  340.     xoffset *= sensitivity;
  341.     yoffset *= sensitivity;
  342.  
  343.     yaw += xoffset;
  344.     pitch += yoffset;
  345.  
  346.     // Make sure that when pitch is out of bounds, screen doesn't get flipped
  347.     if (pitch > 89.0f)
  348.         pitch = 89.0f;
  349.     if (pitch < -89.0f)
  350.         pitch = -89.0f;
  351.  
  352.     glm::vec3 front;
  353.     front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
  354.     front.y = sin(glm::radians(pitch));
  355.     front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
  356.     cameraFront = glm::normalize(front);
  357. }
  358.  
  359. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
  360. {
  361.     if (fov >= 1.0f && fov <= 45.0f)
  362.         fov -= yoffset;
  363.     if (fov <= 1.0f)
  364.         fov = 1.0f;
  365.     if (fov >= 45.0f)
  366.         fov = 45.0f;
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement