Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.66 KB | None | 0 0
  1. #define GLEW_STATIC
  2. #include <glew.h>
  3. // GLFW
  4. #include <glfw3.h>
  5. #include <iostream>
  6. #include "Shader.h"
  7. #include "Camera.h"
  8. #include <SOIL.h>
  9.  
  10. #include <glm/glm.hpp>
  11. #include <glm/gtc/matrix_transform.hpp>
  12. #include <glm/gtc/type_ptr.hpp>
  13.  
  14. bool keys[1024];
  15.  
  16. const GLuint WIDTH = 800, HEIGHT = 600;
  17.  
  18. Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
  19. GLfloat lastX = WIDTH / 2;
  20. GLfloat lastY = HEIGHT / 2;
  21.  
  22. GLfloat deltaTime = 0.0f;   // Time between current frame and last frame
  23. GLfloat lastFrame = 0.0f;
  24.  
  25. glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
  26.  
  27. void Do_Movement()
  28. {
  29.     // Camera controls
  30.     if (keys[GLFW_KEY_W])
  31.         camera.ProcessKeyboard(FORWARD, deltaTime);
  32.     if (keys[GLFW_KEY_S])
  33.         camera.ProcessKeyboard(BACKWARD, deltaTime);
  34.     if (keys[GLFW_KEY_A])
  35.         camera.ProcessKeyboard(LEFT, deltaTime);
  36.     if (keys[GLFW_KEY_D])
  37.         camera.ProcessKeyboard(RIGHT, deltaTime);
  38. }
  39.  
  40. // Is called whenever a key is pressed/released via GLFW
  41. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  42. {
  43.     //cout << key << endl;
  44.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  45.         glfwSetWindowShouldClose(window, GL_TRUE);
  46.     if (key >= 0 && key < 1024)
  47.     {
  48.         if (action == GLFW_PRESS)
  49.             keys[key] = true;
  50.         else if (action == GLFW_RELEASE)
  51.             keys[key] = false;
  52.     }
  53. }
  54.  
  55. bool firstMouse = true;
  56. void mouse_callback(GLFWwindow* window, double xpos, double ypos)
  57. {
  58.     if (firstMouse)
  59.     {
  60.         lastX = xpos;
  61.         lastY = ypos;
  62.         firstMouse = false;
  63.     }
  64.  
  65.     GLfloat xoffset = xpos - lastX;
  66.     GLfloat yoffset = lastY - ypos;  // Reversed since y-coordinates go from bottom to left
  67.  
  68.     lastX = xpos;
  69.     lastY = ypos;
  70.  
  71.     camera.ProcessMouseMovement(xoffset, yoffset);
  72. }
  73.  
  74.  
  75. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
  76. {
  77.     camera.ProcessMouseScroll(yoffset);
  78. }
  79.  
  80. GLuint InitVBO(GLfloat vertices[])
  81. {
  82.     GLuint VBO;
  83.     glGenBuffers(1, &VBO);
  84.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  85.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  86.  
  87.     return VBO;
  88. }
  89.  
  90. int main()
  91. {
  92.     glfwInit();
  93.  
  94.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  95.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  96.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  97.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  98.  
  99.     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
  100.     if (window == nullptr)
  101.     {
  102.         std::cout << "Failed to create GLFW window" << std::endl;
  103.         glfwTerminate();
  104.         return -1;
  105.     }
  106.     glfwMakeContextCurrent(window);
  107.     glfwSetKeyCallback(window, key_callback);
  108.     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  109.     glfwSetCursorPosCallback(window, mouse_callback);
  110.     glfwSetScrollCallback(window, scroll_callback);
  111.  
  112.     glewExperimental = GL_TRUE;
  113.     if (glewInit() != GLEW_OK)
  114.     {
  115.         std::cout << "Failed to initialize GLEW" << std::endl;
  116.         return -1;
  117.     }
  118.  
  119.     glViewport(0, 0, WIDTH, HEIGHT);
  120.  
  121.     Shader ourShader("vertexShader.txt", "fragmentShader.txt");
  122.     Shader lampShader("lampVertexShader.txt", "lampFragmentShader.txt");
  123.  
  124.     float vertices[] = {
  125.         -0.5f, -0.5f, -0.5f,
  126.          0.5f, -0.5f, -0.5f,
  127.          0.5f,  0.5f, -0.5f,
  128.          0.5f,  0.5f, -0.5f,
  129.         -0.5f,  0.5f, -0.5f,
  130.         -0.5f, -0.5f, -0.5f,
  131.  
  132.         -0.5f, -0.5f,  0.5f,
  133.          0.5f, -0.5f,  0.5f,
  134.          0.5f,  0.5f,  0.5f,
  135.          0.5f,  0.5f,  0.5f,
  136.         -0.5f,  0.5f,  0.5f,
  137.         -0.5f, -0.5f,  0.5f,
  138.  
  139.         -0.5f,  0.5f,  0.5f,
  140.         -0.5f,  0.5f, -0.5f,
  141.         -0.5f, -0.5f, -0.5f,
  142.         -0.5f, -0.5f, -0.5f,
  143.         -0.5f, -0.5f,  0.5f,
  144.         -0.5f,  0.5f,  0.5f,
  145.  
  146.          0.5f,  0.5f,  0.5f,
  147.          0.5f,  0.5f, -0.5f,
  148.          0.5f, -0.5f, -0.5f,
  149.          0.5f, -0.5f, -0.5f,
  150.          0.5f, -0.5f,  0.5f,
  151.          0.5f,  0.5f,  0.5f,
  152.  
  153.         -0.5f, -0.5f, -0.5f,
  154.          0.5f, -0.5f, -0.5f,
  155.          0.5f, -0.5f,  0.5f,
  156.          0.5f, -0.5f,  0.5f,
  157.         -0.5f, -0.5f,  0.5f,
  158.         -0.5f, -0.5f, -0.5f,
  159.  
  160.         -0.5f,  0.5f, -0.5f,
  161.          0.5f,  0.5f, -0.5f,
  162.          0.5f,  0.5f,  0.5f,
  163.          0.5f,  0.5f,  0.5f,
  164.         -0.5f,  0.5f,  0.5f,
  165.         -0.5f,  0.5f, -0.5f
  166.  
  167.     };
  168.  
  169.     glm::vec3 cubePositions[] = {
  170.         glm::vec3(0.0f,  0.0f,  0.0f),
  171.         glm::vec3(2.0f,  5.0f, -15.0f),
  172.         glm::vec3(-1.5f, -2.2f, -2.5f),
  173.         glm::vec3(-3.8f, -2.0f, -12.3f),
  174.         glm::vec3(2.4f, -0.4f, -3.5f),
  175.         glm::vec3(-1.7f,  3.0f, -7.5f),
  176.         glm::vec3(1.3f, -2.0f, -2.5f),
  177.         glm::vec3(1.5f,  2.0f, -2.5f),
  178.         glm::vec3(1.5f,  0.2f, -1.5f),
  179.         glm::vec3(-1.3f,  1.0f, -1.5f)
  180.     };
  181.  
  182.     GLuint indices[] = {  // Note that we start from 0!
  183.         0, 1, 3, // First Triangle
  184.         1, 2, 3  // Second Triangle
  185.     };
  186.  
  187.     GLuint VBO, VAO, EBO;
  188.     glGenVertexArrays(1, &VAO);
  189.     glGenBuffers(1, &VBO);
  190.     glGenBuffers(1, &EBO);
  191.  
  192.     glBindVertexArray(VAO);
  193.  
  194.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  195.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  196.  
  197.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  198.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  199.  
  200.     // Position attribute
  201.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
  202.     glEnableVertexAttribArray(0);
  203.  
  204.     glBindVertexArray(0); // Unbind VAO
  205.  
  206.     GLuint lightVAO;
  207.     glGenVertexArrays(1, &lightVAO);
  208.     glBindVertexArray(lightVAO);
  209.     // Так как VBO объекта-контейнера уже содержит все необходимые данные, то нам нужно только связать с ним новый VAO
  210.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  211.     // Настраиваем атрибуты (нашей лампе понадобятся только координаты вершин)
  212.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
  213.     glEnableVertexAttribArray(0);
  214.     glBindVertexArray(0);
  215.  
  216.     // Container
  217.     int width, height;
  218.     unsigned char* image = SOIL_load_image("container.jpg", &width, &height, 0, SOIL_LOAD_RGB);
  219.    
  220.     glEnable(GL_DEPTH_TEST);
  221.    
  222.  
  223.     while (!glfwWindowShouldClose(window))
  224.     {
  225.         glfwPollEvents();
  226.         Do_Movement();
  227.  
  228.         GLfloat currentFrame = glfwGetTime();
  229.         deltaTime = currentFrame - lastFrame;
  230.         lastFrame = currentFrame;
  231.  
  232.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  233.  
  234.         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  235.         glClear(GL_COLOR_BUFFER_BIT);
  236.  
  237.         // Activate shader
  238.         ourShader.Use();   
  239.  
  240.         GLint objectColorLoc = glGetUniformLocation(ourShader.Program, "objectColor");
  241.         GLint lightColorLoc = glGetUniformLocation(ourShader.Program, "lightColor");
  242.         glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f);
  243.         glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f);
  244.  
  245.         glm::mat4 view;
  246.         view = camera.GetViewMatrix();
  247.  
  248.         glm::mat4 projection;
  249.         projection = glm::perspective(camera.Zoom, (float) HEIGHT / (float) WIDTH, 0.1f, 1000.0f);
  250.  
  251.         GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
  252.  
  253.         GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
  254.         glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
  255.  
  256.         GLint projectionLoc = glGetUniformLocation(ourShader.Program, "projection");
  257.         glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
  258.        
  259.  
  260.         // Draw container
  261.         glBindVertexArray(VAO);
  262.         for (GLuint i = 0; i < 10; i++)
  263.         {
  264.             glm::mat4 model = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };
  265.             model = glm::translate(model, cubePositions[i]);
  266.             model = glm::rotate(model, glm::radians((GLfloat)glfwGetTime() * 50.0f),
  267.                 glm::vec3(0.5f, 1.0f, 0.5f + sin(glm::radians((GLfloat)glfwGetTime() * 50.0f + i * 20.0f)) / 2));
  268.             glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  269.  
  270.             glDrawArrays(GL_TRIANGLES, 0, 36);
  271.         }
  272.         glBindVertexArray(0);
  273.         glBindVertexArray(0);
  274.  
  275.         lampShader.Use();
  276.         // Get location objects for the matrices on the lamp shader (these could be different on a different shader)
  277.         modelLoc = glGetUniformLocation(lampShader.Program, "model");
  278.         viewLoc = glGetUniformLocation(lampShader.Program, "view");
  279.         projectionLoc = glGetUniformLocation(lampShader.Program, "projection");
  280.         // Set matrices
  281.         glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
  282.         glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
  283.  
  284.         glm::mat4 model = glm::mat4();
  285.         model = glm::translate(model, lightPos);
  286.         model = glm::scale(model, glm::vec3(0.2f)); // Make it a smaller cube
  287.         glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  288.         // Draw the light object (using light's vertex attributes)
  289.         glBindVertexArray(lightVAO);
  290.         glDrawArrays(GL_TRIANGLES, 0, 36);
  291.         glBindVertexArray(0);
  292.  
  293.  
  294.  
  295.         glfwSwapBuffers(window);
  296.     }
  297.  
  298.     glDeleteVertexArrays(1, &VAO);
  299.     glDeleteBuffers(1, &VBO);
  300.     glDeleteBuffers(1, &EBO);
  301.  
  302.     glfwTerminate();
  303.  
  304.     return 0;
  305. }
  306. /*
  307.     GLuint texture1, texture2;
  308.  
  309.     glGenTextures(1, &texture1);
  310.     glBindTexture(GL_TEXTURE_2D, texture1);
  311.  
  312.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  313.     glGenerateMipmap(GL_TEXTURE_2D);
  314.  
  315.     SOIL_free_image_data(image);
  316.     glBindTexture(GL_TEXTURE_2D, 0);
  317.  
  318.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  319.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  320.  
  321.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  322.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  323.  
  324.     // Awesomeface
  325.     image = SOIL_load_image("pidor.jpg", &width, &height, 0, SOIL_LOAD_RGB);
  326.  
  327.     glGenTextures(1, &texture2);
  328.     glBindTexture(GL_TEXTURE_2D, texture2);
  329.  
  330.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  331.     glGenerateMipmap(GL_TEXTURE_2D);
  332.  
  333.     SOIL_free_image_data(image);
  334.     glBindTexture(GL_TEXTURE_2D, 0);
  335.  
  336.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  337.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  338.  
  339.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  340.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  341.     */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement