SHARE
TWEET

Untitled

a guest Jan 20th, 2020 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <vector>
  4.  
  5. // Include GLEW
  6. #include <GL/glew.h>
  7.  
  8. // Include GLFW
  9. #include <glfw3.h>
  10. GLFWwindow* window;
  11.  
  12. // Include GLM
  13. #include <glm/glm.hpp>
  14. #include <glm/gtc/matrix_transform.hpp>
  15. using namespace glm;
  16.  
  17. int main()
  18. {
  19.     glewExperimental = true; // Needed for core profile
  20.     if (!glfwInit())
  21.     {
  22.         if (!glfwInit())
  23.         {
  24.             fprintf(stderr, "Failed to initialize GLFW\n");
  25.             getchar();
  26.             return -1;
  27.         }
  28.  
  29.         glfwWindowHint(GLFW_SAMPLES, 4);
  30.         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  31.         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
  32.  
  33.  
  34.         // Open a window and create its OpenGL context
  35.         window = glfwCreateWindow(1024, 768, "Tutorial 07 - Model Loading", NULL, NULL);
  36.         if (window == NULL) {
  37.             fprintf(stderr, "Failed to open GLFW window.\n");
  38.             getchar();
  39.             glfwTerminate();
  40.             return -1;
  41.         }
  42.         glfwMakeContextCurrent(window);
  43.  
  44.         // Initialize GLEW
  45.         if (glewInit() != GLEW_OK) {
  46.             fprintf(stderr, "Failed to initialize GLEW\n");
  47.             getchar();
  48.             glfwTerminate();
  49.             return -1;
  50.         }
  51.  
  52.         // Ensure we can capture the escape key being pressed below
  53.         glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  54.         // Hide the mouse and enable unlimited mouvement
  55.         glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  56.  
  57.         // Set the mouse at the center of the screen
  58.         glfwPollEvents();
  59.         glfwSetCursorPos(window, 1024 / 2, 768 / 2);
  60.  
  61.         // Dark blue background
  62.         glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  63.  
  64.         // Enable depth test
  65.         glEnable(GL_DEPTH_TEST);
  66.         // Accept fragment if it closer to the camera than the former one
  67.         glDepthFunc(GL_LESS);
  68.  
  69.         // Cull triangles which normal is not towards the camera
  70.         glEnable(GL_CULL_FACE);
  71.  
  72.         // Create and compile our GLSL program from the shaders
  73.         GLuint programID = LoadShaders("TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader");
  74.  
  75.         // Get a handle for our "MVP" uniform
  76.         GLuint MatrixID = glGetUniformLocation(programID, "MVP");
  77.  
  78.         // Get a handle for our buffers
  79.         GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
  80.         GLuint vertexUVID = glGetAttribLocation(programID, "vertexUV");
  81.  
  82.         // Load the texture
  83.         GLuint Texture = loadDDS("uvmap.DDS");
  84.  
  85.         // Get a handle for our "myTextureSampler" uniform
  86.         GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
  87.  
  88.         // Read our .obj file
  89.         std::vector<glm::vec3> vertices;
  90.         std::vector<glm::vec2> uvs;
  91.         std::vector<glm::vec3> normals; // Won't be used at the moment.
  92.         bool res = loadOBJ("cube.obj", vertices, uvs, normals);
  93.  
  94.         // Load it into a VBO
  95.  
  96.         GLuint vertexbuffer;
  97.         glGenBuffers(1, &vertexbuffer);
  98.         glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  99.         glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
  100.  
  101.         GLuint uvbuffer;
  102.         glGenBuffers(1, &uvbuffer);
  103.         glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
  104.         glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
  105.  
  106.         do {
  107.  
  108.             // Clear the screen
  109.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  110.  
  111.             // Use our shader
  112.             glUseProgram(programID);
  113.  
  114.             // Compute the MVP matrix from keyboard and mouse input
  115.             computeMatricesFromInputs();
  116.             glm::mat4 ProjectionMatrix = getProjectionMatrix();
  117.             glm::mat4 ViewMatrix = getViewMatrix();
  118.             glm::mat4 ModelMatrix = glm::mat4(1.0);
  119.             glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
  120.  
  121.             // Send our transformation to the currently bound shader,
  122.             // in the "MVP" uniform
  123.             glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
  124.  
  125.             // Bind our texture in Texture Unit 0
  126.             glActiveTexture(GL_TEXTURE0);
  127.             glBindTexture(GL_TEXTURE_2D, Texture);
  128.             // Set our "myTextureSampler" sampler to user Texture Unit 0
  129.             glUniform1i(TextureID, 0);
  130.  
  131.             // 1rst attribute buffer : vertices
  132.             glEnableVertexAttribArray(vertexPosition_modelspaceID);
  133.             glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  134.             glVertexAttribPointer(
  135.                 vertexPosition_modelspaceID,  // The attribute we want to configure
  136.                 3,                            // size
  137.                 GL_FLOAT,                     // type
  138.                 GL_FALSE,                     // normalized?
  139.                 0,                            // stride
  140.                 (void*)0                      // array buffer offset
  141.             );
  142.  
  143.             // 2nd attribute buffer : UVs
  144.             glEnableVertexAttribArray(vertexUVID);
  145.             glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
  146.             glVertexAttribPointer(
  147.                 vertexUVID,                   // The attribute we want to configure
  148.                 2,                            // size : U+V => 2
  149.                 GL_FLOAT,                     // type
  150.                 GL_FALSE,                     // normalized?
  151.                 0,                            // stride
  152.                 (void*)0                      // array buffer offset
  153.             );
  154.  
  155.             // Draw the triangles !
  156.             glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  157.  
  158.             glDisableVertexAttribArray(vertexPosition_modelspaceID);
  159.             glDisableVertexAttribArray(vertexUVID);
  160.  
  161.             // Swap buffers
  162.             glfwSwapBuffers(window);
  163.             glfwPollEvents();
  164.  
  165.         } // Check if the ESC key was pressed or the window was closed
  166.         while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
  167.             glfwWindowShouldClose(window) == 0);
  168.  
  169.         // Cleanup VBO and shader
  170.         glDeleteBuffers(1, &vertexbuffer);
  171.         glDeleteBuffers(1, &uvbuffer);
  172.         glDeleteProgram(programID);
  173.         glDeleteTextures(1, &TextureID);
  174.  
  175.         // Close OpenGL window and terminate GLFW
  176.         glfwTerminate();
  177.  
  178.         return 0;
  179.     }
  180. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top