Guest User

Untitled

a guest
Dec 1st, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <GL/glew.h>
  3. #include <GLFW/glfw3.h>
  4. #include <glm/glm.hpp>
  5. #include <glm/gtc/matrix_transform.hpp>
  6. #include <glm/gtc/type_ptr.hpp>
  7.  
  8. #include "common/shader.h"
  9.  
  10. //This is something you can’t change, it’s built in your graphics card. So (-1,-1) is the bottom left corner of your screen.
  11. // (1,-1) is the bottom right, and (0,1) is the middle top. So this triangle should take most of the screen.
  12.  
  13. // If w == 1, then the vector (x,y,z,1) is a position in space.
  14. // If w == 0, then the vector(x, y, z, 0) is a direction.
  15.  
  16. int main()
  17. {
  18.  
  19.     // Initialise GLFW
  20.     glewExperimental = true; // Needed for core profile
  21.     if (!glfwInit())
  22.     {
  23.         std::cerr<< "Failed to initialize GLFW\n";
  24.         return -1;
  25.     }
  26.  
  27.     glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
  28.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);  // OpenGL 3.3
  29.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  30.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  31.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Don't want the old OpenGL
  32.  
  33.  
  34.     // Open a window and create its OpenGL context
  35.     std::unique_ptr<GLFWwindow, decltype(&glfwDestroyWindow)> window(
  36.         glfwCreateWindow(1600, 768, "Tutorial 01", NULL, NULL),
  37.         glfwDestroyWindow
  38.     );
  39.  
  40.     if (window == NULL)
  41.     {
  42.         std::cerr << "Failed to open GLFW window.\n";
  43.         glfwTerminate();
  44.         return -1;
  45.     }
  46.  
  47.     glfwMakeContextCurrent(window.get()); // Initialize GLEW
  48.  
  49.     glewExperimental = true; // Needed for core profile
  50.     if (glewInit() != GLEW_OK)
  51.     {
  52.         std::cerr << "Failed to initialize GLEW\n";
  53.         return -1;
  54.     }
  55.  
  56.     GLuint VertexArrayID{};
  57.     glGenVertexArrays(1, &VertexArrayID);
  58.     glBindVertexArray(VertexArrayID);
  59.  
  60.  
  61.     static const GLfloat gVertexBufferDataSquareOne[] = {
  62.         -1.0f,-1.0f,-1.0f, // triangle 1 : begin
  63.         -1.0f,-1.0f, 1.0f,
  64.         -1.0f, 1.0f, 1.0f, // triangle 1 : end
  65.         1.0f, 1.0f,-1.0f, // triangle 2 : begin
  66.         -1.0f,-1.0f,-1.0f,
  67.         -1.0f, 1.0f,-1.0f, // triangle 2 : end
  68.         1.0f,-1.0f, 1.0f,
  69.         -1.0f,-1.0f,-1.0f,
  70.         1.0f,-1.0f,-1.0f,
  71.         1.0f, 1.0f,-1.0f,
  72.         1.0f,-1.0f,-1.0f,
  73.         -1.0f,-1.0f,-1.0f,
  74.         -1.0f,-1.0f,-1.0f,
  75.         -1.0f, 1.0f, 1.0f,
  76.         -1.0f, 1.0f,-1.0f,
  77.         1.0f,-1.0f, 1.0f,
  78.         -1.0f,-1.0f, 1.0f,
  79.         -1.0f,-1.0f,-1.0f,
  80.         -1.0f, 1.0f, 1.0f,
  81.         -1.0f,-1.0f, 1.0f,
  82.         1.0f,-1.0f, 1.0f,
  83.         1.0f, 1.0f, 1.0f,
  84.         1.0f,-1.0f,-1.0f,
  85.         1.0f, 1.0f,-1.0f,
  86.         1.0f,-1.0f,-1.0f,
  87.         1.0f, 1.0f, 1.0f,
  88.         1.0f,-1.0f, 1.0f,
  89.         1.0f, 1.0f, 1.0f,
  90.         1.0f, 1.0f,-1.0f,
  91.         -1.0f, 1.0f,-1.0f,
  92.         1.0f, 1.0f, 1.0f,
  93.         -1.0f, 1.0f,-1.0f,
  94.         -1.0f, 1.0f, 1.0f,
  95.         1.0f, 1.0f, 1.0f,
  96.         -1.0f, 1.0f, 1.0f,
  97.         1.0f,-1.0f, 1.0f
  98.     };
  99.  
  100.     static const GLfloat gVertexBufferDataTriangle[] = {
  101.        -1.0f, -1.0f, 0.0f,
  102.        1.0f, -1.0f, 0.0f,
  103.        0.0f,  1.0f, 0.0f,
  104.     };
  105.     static const GLfloat gVertexBufferDataSquareTwo[] = {
  106.         -1.0f, -1.0f, -1.0f, // triangle 1 : begin
  107.             -1.0f, -1.0f, 1.0f,
  108.             -1.0f, 1.0f, 1.0f, // triangle 1 : end
  109.             1.0f, 1.0f, -1.0f, // triangle 2 : begin
  110.             -1.0f, -1.0f, -1.0f,
  111.             -1.0f, 1.0f, -1.0f, // triangle 2 : end
  112.             1.0f, -1.0f, 1.0f,
  113.             -1.0f, -1.0f, -1.0f,
  114.             1.0f, -1.0f, -1.0f,
  115.             1.0f, 1.0f, -1.0f,
  116.             1.0f, -1.0f, -1.0f,
  117.             -1.0f, -1.0f, -1.0f,
  118.             -1.0f, -1.0f, -1.0f,
  119.             -1.0f, 1.0f, 1.0f,
  120.             -1.0f, 1.0f, -1.0f,
  121.             1.0f, -1.0f, 1.0f,
  122.             -1.0f, -1.0f, 1.0f,
  123.             -1.0f, -1.0f, -1.0f,
  124.             -1.0f, 1.0f, 1.0f,
  125.             -1.0f, -1.0f, 1.0f,
  126.             1.0f, -1.0f, 1.0f,
  127.             1.0f, 1.0f, 1.0f,
  128.             1.0f, -1.0f, -1.0f,
  129.             1.0f, 1.0f, -1.0f,
  130.             1.0f, -1.0f, -1.0f,
  131.             1.0f, 1.0f, 1.0f,
  132.             1.0f, -1.0f, 1.0f,
  133.             1.0f, 1.0f, 1.0f,
  134.             1.0f, 1.0f, -1.0f,
  135.             -1.0f, 1.0f, -1.0f,
  136.             1.0f, 1.0f, 1.0f,
  137.             -1.0f, 1.0f, -1.0f,
  138.             -1.0f, 1.0f, 1.0f,
  139.             1.0f, 1.0f, 1.0f,
  140.             -1.0f, 1.0f, 1.0f,
  141.             1.0f, -1.0f, 1.0f
  142.     };
  143.  
  144.     static const GLfloat gColorBufferData[] = {
  145.     0.583f,  0.771f,  0.014f,
  146.     0.609f,  0.115f,  0.436f,
  147.     0.327f,  0.483f,  0.844f,
  148.     0.822f,  0.569f,  0.201f,
  149.     0.435f,  0.602f,  0.223f,
  150.     0.310f,  0.747f,  0.185f,
  151.     0.597f,  0.770f,  0.761f,
  152.     0.559f,  0.436f,  0.730f,
  153.     0.359f,  0.583f,  0.152f,
  154.     0.483f,  0.596f,  0.789f,
  155.     0.559f,  0.861f,  0.639f,
  156.     0.195f,  0.548f,  0.859f,
  157.     0.014f,  0.184f,  0.576f,
  158.     0.771f,  0.328f,  0.970f,
  159.     0.406f,  0.615f,  0.116f,
  160.     0.676f,  0.977f,  0.133f,
  161.     0.971f,  0.572f,  0.833f,
  162.     0.140f,  0.616f,  0.489f,
  163.     0.997f,  0.513f,  0.064f,
  164.     0.945f,  0.719f,  0.592f,
  165.     0.543f,  0.021f,  0.978f,
  166.     0.279f,  0.317f,  0.505f,
  167.     0.167f,  0.620f,  0.077f,
  168.     0.347f,  0.857f,  0.137f,
  169.     0.055f,  0.953f,  0.042f,
  170.     0.714f,  0.505f,  0.345f,
  171.     0.783f,  0.290f,  0.734f,
  172.     0.722f,  0.645f,  0.174f,
  173.     0.302f,  0.455f,  0.848f,
  174.     0.225f,  0.587f,  0.040f,
  175.     0.517f,  0.713f,  0.338f,
  176.     0.053f,  0.959f,  0.120f,
  177.     0.393f,  0.621f,  0.362f,
  178.     0.673f,  0.211f,  0.457f,
  179.     0.820f,  0.883f,  0.371f,
  180.     0.982f,  0.099f,  0.879f
  181.     };
  182.  
  183.     GLuint vertexBufferSquareOne{};
  184.     glGenBuffers(1, &vertexBufferSquareOne);
  185.     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferSquareOne);
  186.     glBufferData(GL_ARRAY_BUFFER, sizeof(gVertexBufferDataSquareOne), gVertexBufferDataSquareOne, GL_STATIC_DRAW);
  187.  
  188.     GLuint vertexBufferTriangle{};
  189.     glGenBuffers(1, &vertexBufferTriangle);  
  190.     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferTriangle);
  191.     glBufferData(GL_ARRAY_BUFFER, sizeof(gVertexBufferDataTriangle), gVertexBufferDataTriangle, GL_STATIC_DRAW);
  192.  
  193.     GLuint vertexBufferSquareTwo{};
  194.     glGenBuffers(1, &vertexBufferSquareTwo);  
  195.     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferSquareTwo);  
  196.     glBufferData(GL_ARRAY_BUFFER, sizeof(gVertexBufferDataSquareTwo), gVertexBufferDataSquareTwo, GL_STATIC_DRAW);  
  197.  
  198.  
  199.     GLuint colorbuffer{};
  200.     glGenBuffers(1, &colorbuffer);
  201.     glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
  202.     glBufferData(GL_ARRAY_BUFFER, sizeof(gColorBufferData), gColorBufferData, GL_STATIC_DRAW);
  203.  
  204.     GLuint programID = LoadShaders("SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader");
  205.  
  206.     glfwSetInputMode(window.get(), GLFW_STICKY_KEYS, GL_TRUE);
  207.  
  208.     glm::mat4 myMatrix{};
  209.     glm::vec4 myVector{};
  210.     // fill myMatrix and myVector somehow
  211.     glm::vec4 transformedVector = myMatrix * myVector;
  212.  
  213.  
  214.     do {
  215.         // Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
  216.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  217.         glUseProgram(programID);
  218.  
  219.         // Enable depth test
  220.         glEnable(GL_DEPTH_TEST);
  221.         // Accept fragment if it closer to the camera than the former one
  222.         glDepthFunc(GL_LESS);
  223.  
  224.  
  225.         int width, height;
  226.         glfwGetFramebufferSize(window.get(), &width, &height);
  227.  
  228.         GLuint MatrixIDS = glGetUniformLocation(programID, "MVP");
  229.  
  230.         // The perspective matrix simulates the way the human eye sees things: objects farther away appear smaller.
  231.         glm::mat4 Projection = glm::perspective(glm::radians(90.0f), (float)width / height, 0.1f, 100.0f);
  232.  
  233.         //which defines the camera's position and orientation in the scene.
  234.         // It’s like setting up where your camera is and where it’s looking.
  235.         //camera pos, object pos, upwards direction in world
  236.         glm::mat4 View = glm::lookAt(glm::vec3(4, 3, -6), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
  237.  
  238.         // creates a model matrix. This matrix is used to position,
  239.         // scale, and rotate objects in the world. By default, it's the identity matrix (1.0f),
  240.         // which means no transformation. If you wanted to move or rotate your object, you would modify this matrix.
  241.         glm::mat4 ModelSquareOne = glm::mat4(1.0f);  
  242.        
  243.         glm::vec3 translateSquareOne(-3.0f, 0.0f, 0.0f);
  244.         ModelSquareOne = glm::translate(ModelSquareOne, translateSquareOne);       
  245.         glm::mat4 MVPOne = Projection * View * ModelSquareOne; // Combine them into the MVP matrix    
  246.  
  247.         // Get the location of the MVP uniform
  248.         glm::mat4 ModelTriangle = glm::mat4(1.0f);
  249.         glm::vec3 translateTriangle(2.0f, 0.0f, 0.0f);
  250.         ModelTriangle = glm::translate(ModelTriangle, translateTriangle);
  251.         constexpr float angle = glm::radians(90.0f);
  252.         ModelTriangle = glm::rotate(ModelTriangle, angle, glm::vec3(0.0f, 1.0f, 0.0f));
  253.         glm::mat4 MVPTwo = Projection * View * ModelTriangle; // Combine them into the MVP matrix    
  254.  
  255.         glm::mat4 ModelSquareTwo = glm::mat4(1.0f);
  256.         glm::vec3 translateSquareTwo(6.0f, 0.0f, 0.0f);
  257.         ModelSquareTwo = glm::translate(ModelSquareTwo, translateSquareTwo);
  258.         glm::mat4 MVPThree = Projection * View * ModelSquareTwo; // Combine them into the MVP matrix  
  259.        
  260.         // Send the MVP matrix to the shader
  261.         glUniformMatrix4fv(MatrixIDS, 1, GL_FALSE, glm::value_ptr(MVPOne));
  262.  
  263.         glEnableVertexAttribArray(0);
  264.         glBindBuffer(GL_ARRAY_BUFFER, vertexBufferSquareOne);
  265.         glVertexAttribPointer(
  266.             0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
  267.             3,                  // size
  268.             GL_FLOAT,           // type
  269.             GL_FALSE,           // normalized?  
  270.             0,                  // stride
  271.             (void*)0            // array buffer offset
  272.         );
  273.  
  274.  
  275.         glEnableVertexAttribArray(1);
  276.         glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
  277.         glVertexAttribPointer(
  278.             1,                                // attribute. No particular reason for 1, but must match the layout in the shader.
  279.             3,                                // size
  280.             GL_FLOAT,                         // type
  281.             GL_FALSE,                         // normalized?
  282.             0,                                // stride
  283.             (void*)0                          // array buffer offset
  284.         );
  285.  
  286.         glDrawArrays(GL_TRIANGLES, 0, 12 * 3);  // 12*3 indices starting at 0 -> 12 triangles -> 6 squares
  287.  
  288.  
  289.         glUniformMatrix4fv(MatrixIDS, 1, GL_FALSE, glm::value_ptr(MVPTwo));
  290.  
  291.         glEnableVertexAttribArray(2);
  292.         glBindBuffer(GL_ARRAY_BUFFER, vertexBufferTriangle);
  293.         glVertexAttribPointer(
  294.             2,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
  295.             3,                  // size
  296.             GL_FLOAT,           // type  
  297.             GL_FALSE,           // normalized?  
  298.             0,                  // stride
  299.             (void*)0            // array buffer offset
  300.         );
  301.  
  302.         glEnableVertexAttribArray(1);
  303.         glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
  304.         glVertexAttribPointer(
  305.             1,                                // attribute. No particular reason for 1, but must match the layout in the shader.
  306.             3,                                // size
  307.             GL_FLOAT,                         // type
  308.             GL_FALSE,                         // normalized?
  309.             0,                                // stride
  310.             (void*)0                          // array buffer offset
  311.         );
  312.  
  313.         glDrawArrays(GL_TRIANGLES, 0, 3);  // 12*3 indices starting at 0 -> 12 triangles -> 6 squares  
  314.  
  315.         glUniformMatrix4fv(MatrixIDS, 1, GL_FALSE, glm::value_ptr(MVPThree));
  316.  
  317.         glEnableVertexAttribArray(3);
  318.         glBindBuffer(GL_ARRAY_BUFFER, vertexBufferSquareTwo);
  319.         glVertexAttribPointer(
  320.             2,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
  321.             3,                  // size
  322.             GL_FLOAT,           // type  
  323.             GL_FALSE,           // normalized?  
  324.             0,                  // stride
  325.             (void*)0            // array buffer offset
  326.         );
  327.  
  328.         glEnableVertexAttribArray(1);
  329.         glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
  330.         glVertexAttribPointer(
  331.             1,                                // attribute. No particular reason for 1, but must match the layout in the shader.
  332.             3,                                // size
  333.             GL_FLOAT,                         // type
  334.             GL_FALSE,                         // normalized?
  335.             0,                                // stride
  336.             (void*)0                          // array buffer offset
  337.         );
  338.  
  339.         glDrawArrays(GL_TRIANGLES, 0, 12 * 3);  // 12*3 indices starting at 0 -> 12 triangles -> 6 squares  
  340.  
  341.  
  342.         glDisableVertexAttribArray(0);
  343.         glDisableVertexAttribArray(1);
  344.  
  345.  
  346.  
  347.         // Swap buffers
  348.         glfwSwapBuffers(window.get());
  349.         glfwPollEvents();
  350.  
  351.     } // Check if the ESC key was pressed or the window was closed
  352.     while (glfwGetKey(window.get(), GLFW_KEY_ESCAPE) != GLFW_PRESS &&
  353.         glfwWindowShouldClose(window.get()) == 0);
  354.  
  355.  
  356. }
  357.  
  358. -------------------Shader code
  359.  
  360. #version 330 core
  361. in vec3 fragmentColor;
  362. out vec3 color;
  363. void main()
  364. {
  365.     color = fragmentColor;
  366. }
  367.  
  368.  
  369. #version 330 core
  370.  
  371. layout(location = 0) in vec3 vertexPosition_modelspace;
  372. layout(location = 1) in vec3 vertexColor;
  373. uniform mat4 MVP; // Model-View-Projection matrix
  374.  
  375. out vec3 fragmentColor;
  376. void main()
  377. {
  378.     // Output position of the vertex, in clip space
  379.     gl_Position = MVP * vec4(vertexPosition_modelspace, 1.0);
  380.     fragmentColor = vertexColor;
  381. }
  382.  
  383.  
  384.  
  385. ---------------Shader CPP and header
  386.  
  387. #include "shader.h"
  388. #include <fstream>
  389. #include <sstream>
  390. #include <vector>
  391. #include <GL/glew.h>
  392.  
  393. GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path) {
  394.  
  395.     // Create the shaders
  396.     GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  397.     GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  398.  
  399.     // Read the Vertex Shader code from the file
  400.     std::string VertexShaderCode;
  401.     std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
  402.     if (VertexShaderStream.is_open()) {
  403.         std::stringstream sstr;
  404.         sstr << VertexShaderStream.rdbuf();
  405.         VertexShaderCode = sstr.str();
  406.         VertexShaderStream.close();
  407.     }
  408.     else {
  409.         printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
  410.         getchar();
  411.         return 0;
  412.     }
  413.  
  414.     // Read the Fragment Shader code from the file
  415.     std::string FragmentShaderCode;
  416.     std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
  417.     if (FragmentShaderStream.is_open()) {
  418.         std::stringstream sstr;
  419.         sstr << FragmentShaderStream.rdbuf();
  420.         FragmentShaderCode = sstr.str();
  421.         FragmentShaderStream.close();
  422.     }
  423.  
  424.     GLint Result = GL_FALSE;
  425.     int InfoLogLength;
  426.  
  427.     // Compile Vertex Shader
  428.     printf("Compiling shader : %s\n", vertex_file_path);
  429.     char const* VertexSourcePointer = VertexShaderCode.c_str();
  430.     glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
  431.     glCompileShader(VertexShaderID);
  432.  
  433.     // Check Vertex Shader
  434.     glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
  435.     glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  436.     if (InfoLogLength > 0) {
  437.         std::vector<char> VertexShaderErrorMessage(InfoLogLength + 1);
  438.         glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
  439.         printf("%s\n", &VertexShaderErrorMessage[0]);
  440.     }
  441.  
  442.     // Compile Fragment Shader
  443.     printf("Compiling shader : %s\n", fragment_file_path);
  444.     char const* FragmentSourcePointer = FragmentShaderCode.c_str();
  445.     glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
  446.     glCompileShader(FragmentShaderID);
  447.  
  448.     // Check Fragment Shader
  449.     glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  450.     glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  451.     if (InfoLogLength > 0) {
  452.         std::vector<char> FragmentShaderErrorMessage(InfoLogLength + 1);
  453.         glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
  454.         printf("%s\n", &FragmentShaderErrorMessage[0]);
  455.     }
  456.  
  457.     // Link the program
  458.     printf("Linking program\n");
  459.     GLuint ProgramID = glCreateProgram();
  460.     glAttachShader(ProgramID, VertexShaderID);
  461.     glAttachShader(ProgramID, FragmentShaderID);
  462.     glLinkProgram(ProgramID);
  463.  
  464.     // Check the program
  465.     glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
  466.     glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  467.     if (InfoLogLength > 0) {
  468.         std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
  469.         glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
  470.         printf("%s\n", &ProgramErrorMessage[0]);
  471.     }
  472.  
  473.     glDetachShader(ProgramID, VertexShaderID);
  474.     glDetachShader(ProgramID, FragmentShaderID);
  475.  
  476.     glDeleteShader(VertexShaderID);
  477.     glDeleteShader(FragmentShaderID);
  478.  
  479.     return ProgramID;
  480. }
  481.  
  482.  
  483.  
  484. ---------
  485. #ifndef SHADER_H
  486. #define SHADER_H
  487.  
  488. #include <GL/glew.h>
  489.  
  490. GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path);
  491.  
  492. #endif // SHADER_H
  493.  
  494.  
Advertisement
Add Comment
Please, Sign In to add comment