Guest User

Untitled

a guest
Jun 17th, 2020
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.27 KB | None | 0 0
  1.  
  2. #version 330 core
  3. layout (location = 0) in vec3 aPos;
  4. layout (location = 2) in mat4 instanceMatrix;
  5.  
  6. uniform mat4 view;
  7. uniform mat4 projection;
  8.  
  9. void main()
  10. {
  11. gl_Position = projection * view * instanceMatrix * vec4(aPos, 1.0f);
  12. }
  13.  
  14.  
  15. #include <glew.h>
  16. #include <glfw3.h>
  17. #include <iostream>
  18. #include <glm.hpp>
  19. #include <gtc/matrix_transform.hpp>
  20. #include <gtc/type_ptr.hpp>
  21.  
  22. #include "Shaders/Functions/loadShader.h"
  23. #include "Camera.h"
  24. #include "Input/Input.h"
  25.  
  26. //Function used to resize the window appropriately.
  27. void FrameBufferSizeCallback(GLFWwindow* window, int width, int height);
  28. inline void Mouse(GLFWwindow* window, double xPos, double yPos);
  29.  
  30. //Global screen settings.
  31. const unsigned int SCR_WIDTH  = 800;
  32. const unsigned int SCR_HEIGHT = 600;
  33.  
  34. //Global camera variables.
  35. Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
  36.  
  37. //Global timing variables.
  38. float deltaTime = 0.0f;         //Time difference of current frame and last frame.
  39. float lastTime = 0.0f;         //Keeps track of the time of the last frame. Used to calculate deltaTime.
  40.  
  41. int main(void)
  42. {
  43.     /* Initialize the library */
  44.     if (!glfwInit())
  45.     {
  46.         std::cout << "GLFW initialization failed." << std::endl;
  47.         return -1;
  48.     }
  49.  
  50.     /*       OpenGLtutorials.org tutorial       */
  51.     //Triangle doesn't show if other 3 are uncommented
  52.     glfwWindowHint(GLFW_SAMPLES, 4);                                    //4x antialiasing
  53.     //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);                      //OpenGL 3.3
  54.     //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  55.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);      //We don't want the old OpenGL
  56.     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  57.     /*       OpenGLtutorials.org tutorial end       */
  58.  
  59.     GLFWwindow* window;
  60.  
  61.     /* Create a windowed mode window and its OpenGL context */
  62.     window = glfwCreateWindow(1280, 1024, "Window of the Gods!", NULL, NULL);
  63.    
  64.     if (!window)
  65.     {
  66.         glfwTerminate();
  67.         std::cout << "GLFW window creation failed." << std::endl;
  68.         return -1;
  69.     }
  70.  
  71.     /* Make the window's context current */
  72.     glfwMakeContextCurrent(window);
  73.  
  74.     //Added code
  75.     glfwSetFramebufferSizeCallback(window, FrameBufferSizeCallback);
  76.     glfwSetCursorPosCallback(window, Mouse);
  77.  
  78.     //Tells GLFW to capture our mouse.
  79.     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  80.  
  81.     //Check if GLEW is initialized
  82.     if (glewInit() != GLEW_OK)
  83.     {
  84.         std::cout << "GLEW failed to initialize." << std::endl;
  85.     }
  86.  
  87.     glEnable(GL_DEPTH_TEST);
  88.     glEnable(GL_CULL_FACE);
  89.  
  90.     //Compile and load shaders and store the program id
  91.     GLuint programID = LoadShaders("Shaders/Vertex/CameraShader.vert", "Shaders/Fragment/SimpleFragmentShader.frag");
  92.  
  93.     //Prints the GLEW and openGL versions
  94.     std::cout << "Using GLEW version :" << glewGetString(GLEW_VERSION) << std::endl;
  95.     std::cout << "Using openGL version: " << glGetString(GL_VERSION) << std::endl;
  96.  
  97.     /*
  98.       *******CUBE*******
  99.       An array of 3 vectors which represents 3 vertices; 6 to make a rectangle; Each segment represents a face of a cube, made of two triangles;
  100.       Looking at the face, the first vertex of each triangle is top left, triangle formed counter-clockwise; First vertex on top is (-0.5, 0.5, -0.5);
  101.       First vertex on bottom is (-0.5, -0.5, 0.5);
  102.    
  103.         1         3
  104.       4    
  105.              
  106.                
  107.                   2
  108.       5         6
  109.     */
  110.     static const GLfloat cubeVertexBuffer[] =
  111.     {
  112.          //Front
  113.         -0.5f,  0.5f,  0.5f,
  114.          0.5f, -0.5f,  0.5f,
  115.          0.5f,  0.5f,  0.5f,
  116.         -0.5f,  0.5f,  0.5f,
  117.         -0.5f, -0.5f,  0.5f,
  118.          0.5f, -0.5f,  0.5f,
  119.          //Right
  120.          0.5f,  0.5f,  0.5f,
  121.          0.5f, -0.5f, -0.5f,
  122.          0.5f,  0.5f, -0.5f,
  123.          0.5f,  0.5f,  0.5f,
  124.          0.5f, -0.5f,  0.5f,
  125.          0.5f, -0.5f, -0.5f,
  126.          //Back
  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.          0.5f, -0.5f, -0.5f,
  132.         -0.5f, -0.5f, -0.5f,
  133.          //Left
  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.         -0.5f, -0.5f, -0.5f,
  139.         -0.5f, -0.5f,  0.5f,
  140.          //Top
  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.         -0.5f,  0.5f,  0.5f,
  146.          0.5f,  0.5f,  0.5f,
  147.          //Bottom
  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.         -0.5f, -0.5f, -0.5f,
  153.          0.5f, -0.5f, -0.5f,
  154.     };
  155.  
  156.     //World space position of our cube.
  157.     /*glm::vec3 cubePosition[] =
  158.     {  
  159.         glm::vec3( 0.0f,  0.0f,  0.0f),
  160.     };*/
  161.  
  162.     //INSTANCING TEST
  163.     //Need to do: make sure the coordinates right.
  164.     unsigned int cubeGridXCoord = 10;
  165.     unsigned int cubeGridYCoord = 10;
  166.     unsigned int cubeGridZCoord = 10;
  167.     float displacement = 4.0f;
  168.     unsigned int currentIndex = 0;
  169.     glm::mat4* modelMatrices;
  170.  
  171.     modelMatrices = new glm::mat4[cubeGridXCoord * cubeGridYCoord * cubeGridZCoord];
  172.     for (unsigned int i = 0; i < cubeGridXCoord; i++)
  173.     {
  174.         for (unsigned int j = 0; j < cubeGridYCoord; j++)
  175.         {
  176.             for (unsigned int k = 0; k < cubeGridZCoord; k++)
  177.             {
  178.                 glm::mat4 model = glm::mat4(1.0f);
  179.                 model = glm::translate(model, glm::vec3((float)i / displacement, (float)j / displacement, (float)k / displacement));
  180.                 model = glm::scale(model, glm::vec3(0.1f));
  181.                 modelMatrices[currentIndex++] = model;
  182.             }
  183.         }
  184.     }
  185.  
  186.     //INSTANCING TEST END
  187.  
  188.     unsigned int VAO;
  189.     glGenVertexArrays(1, &VAO);
  190.     glBindVertexArray(VAO);
  191.  
  192.     //Identify vertex buffer
  193.     GLuint vertexbuffer;
  194.     //Generate 1 buffer, put the resulting identifier in vertexbuffer
  195.     glGenBuffers(1, &vertexbuffer);
  196.  
  197.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  198.     //Give the vertices to OpenGL
  199.     //glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertexBuffer), cubeVertexBuffer, GL_STATIC_DRAW);
  200.     glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertexBuffer), cubeVertexBuffer, GL_STATIC_DRAW);
  201.  
  202.     //Added code end
  203.     //First attribute buffer : vertices
  204.     glEnableVertexAttribArray(0);
  205.     glVertexAttribPointer
  206.     (
  207.         0,              //attribute 0. No reason 0, but must match layout in shader.
  208.         3,              //size
  209.         GL_FLOAT,       //type
  210.         GL_FALSE,       //normalized?
  211.         0,              //stride
  212.         (void*)0        //array buffer offset
  213.         );
  214.  
  215.     glBindVertexArray(0);
  216.  
  217.     unsigned int matricesBuffer;
  218.     glGenBuffers(1, &matricesBuffer);
  219.     glBindBuffer(GL_ARRAY_BUFFER, matricesBuffer);
  220.     glBufferData(GL_ARRAY_BUFFER, cubeGridXCoord * cubeGridYCoord * cubeGridZCoord * sizeof(glm::mat4), &modelMatrices[0], GL_STATIC_DRAW);
  221.  
  222.     glBindVertexArray(VAO);
  223.  
  224.     // vertex attributes
  225.  
  226.     glEnableVertexAttribArray(2);
  227.     glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)0);
  228.     glEnableVertexAttribArray(3);
  229.     glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(1 * sizeof(glm::vec4)));
  230.     glEnableVertexAttribArray(4);
  231.     glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(2 * sizeof(glm::vec4)));
  232.     glEnableVertexAttribArray(5);
  233.     glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(3 * sizeof(glm::vec4)));
  234.  
  235.     glVertexAttribDivisor(2, 1);
  236.     glVertexAttribDivisor(3, 1);
  237.     glVertexAttribDivisor(4, 1);
  238.     glVertexAttribDivisor(5, 1);
  239.  
  240.     glBindVertexArray(0);
  241.  
  242.     double previousFPSTime = glfwGetTime();
  243.     int frameCount = 0;
  244.  
  245.     /* Loop until the user closes the window */
  246.     while (!glfwWindowShouldClose(window))
  247.     {
  248.         //Get the time variables and display fps
  249.         float currentTime = glfwGetTime();
  250.         deltaTime = currentTime - lastTime;
  251.         lastTime= currentTime;
  252.         frameCount++;
  253.         if (currentTime - previousFPSTime >= 1.0f)
  254.         {
  255.             std::cout << "FPS: " << frameCount << "\r";
  256.             frameCount = 0;
  257.             previousFPSTime = currentTime;
  258.         }
  259.  
  260.         //Input
  261.         ProcessInput(window, camera, deltaTime);
  262.  
  263.  
  264.         /* Render here */
  265.         glClearColor(0.0f, 0.0f, 0.5f, 0.0f);
  266.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  267.  
  268.  
  269.         //Added code
  270.         glUseProgram(programID);
  271.  
  272.  
  273.         //Pass the projection matrix to shader ( in this case could change every frame )
  274.         glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
  275.         glUniformMatrix4fv(glGetUniformLocation(programID, "projection"), 1, GL_FALSE, &projection[0][0]);
  276.  
  277.         //Camera/view transformation.
  278.         glm::mat4 view = camera.GetViewMatrix();
  279.         glUniformMatrix4fv(glGetUniformLocation(programID, "view"), 1, GL_FALSE, &view[0][0]);
  280.        
  281.         //for (unsigned int i = 0; i < 1000; i++)
  282.         //{
  283.         //    //Calculate model matrix and initialize.
  284.         //    glm::mat4 model = glm::mat4(1.0f);
  285.         //    model = glm::translate(model, cubePosition[0] + glm::vec3(glm::cos(i)* 0.2f + (glm::cos(i)), i * 0.1f, glm::sin(i) * 0.2f + (glm::sin(i))));
  286.         //    //model = glm::rotate(model, glm::radians(0.0f), glm::vec3(1.0f, 0.3f, 0.5f));
  287.         //    model = glm::scale(model, glm::vec3(0.1f));
  288.         //    glUniformMatrix4fv(glGetUniformLocation(programID, "model"), 1, GL_FALSE, &model[0][0]);
  289.  
  290.         //    //Draw the triangle
  291.         //    glDrawArrays(GL_TRIANGLES, 0, 36); // Starting from vertex 0; 3 vertices = one triangle, 6 = one face, 36 = one cube;
  292.         //}
  293.  
  294.         glBindVertexArray(VAO);
  295.         glDrawElementsInstanced(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0, cubeGridXCoord * cubeGridYCoord * cubeGridZCoord);
  296.         glBindVertexArray(0);
  297.  
  298.         //Added code end
  299.  
  300.         /* Swap front and back buffers */
  301.         glfwSwapBuffers(window);
  302.  
  303.         /* Poll for and process events */
  304.         glfwPollEvents();
  305.     }
  306.    
  307.     glDisableVertexAttribArray(0);
  308.     glfwTerminate();
  309.     return 0;
  310. }
Add Comment
Please, Sign In to add comment