Advertisement
beefviper

LearnOpenGL Triangle

Sep 5th, 2024 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | Source Code | 0 0
  1. // main.cpp
  2. // LearnOpenGL.com
  3. // author: beefviper
  4. // date: Sept 5, 2024
  5.  
  6. #include <glad/glad.h>
  7. #include <GLFW/glfw3.h>
  8.  
  9. #include <iostream>
  10.  
  11. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  12. void processInput(GLFWwindow* window);
  13.  
  14. const char* vertexShaderSource = "#version 330 core\n"
  15.     "layout (location = 0) in vec3 aPos;\n"
  16.     "void main()\n"
  17.     "{\n"
  18.     "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
  19.     "}\n";
  20.  
  21. const char* fragmentShaderSource = "#version 330 core\n"
  22.     "out vec4 FragColor;\n"
  23.     "void main()\n"
  24.     "{\n"
  25.     "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
  26.     "}\n";
  27.  
  28. int main()
  29. {
  30.     glfwInit();
  31.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  32.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  33.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  34.     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  35.  
  36.     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
  37.     if (window == NULL)
  38.     {
  39.         std::cout << "Failed to create GLFW window" << std::endl;
  40.         glfwTerminate();
  41.         return -1;
  42.     }
  43.     glfwMakeContextCurrent(window);
  44.     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  45.  
  46.     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  47.     {
  48.         std::cout << "Failed to initialize GLAD" << std::endl;
  49.         return -1;
  50.     }
  51.  
  52.     float vertices[] =
  53.     {
  54.         -0.5f, -0.5f, 0.0f,
  55.          0.5f, -0.5f, 0.0f,
  56.          0.0f,  0.5f, 0.0f
  57.     };
  58.  
  59.     unsigned int VBO;
  60.     glGenBuffers(1, &VBO);
  61.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  62.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  63.  
  64.     unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
  65.     glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
  66.     glCompileShader(vertexShader);
  67.  
  68.     int success{};
  69.     char infoLog[512]{};
  70.  
  71.     glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  72.     if (!success)
  73.     {
  74.         glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  75.         std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
  76.     }
  77.  
  78.     unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  79.     glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
  80.     glCompileShader(fragmentShader);
  81.  
  82.     glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  83.     if (!success)
  84.     {
  85.         glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
  86.         std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
  87.     }
  88.  
  89.     unsigned int shaderProgram = glCreateProgram();
  90.     glAttachShader(shaderProgram, vertexShader);
  91.     glAttachShader(shaderProgram, fragmentShader);
  92.     glLinkProgram(shaderProgram);
  93.  
  94.     glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  95.     if (!success)
  96.     {
  97.         glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
  98.         std::cout << "ERROR::SHADER::PROGRAM::COMPILATION_FAILED\n" << infoLog << std::endl;
  99.     }
  100.  
  101.     glUseProgram(shaderProgram);
  102.  
  103.     glDeleteShader(vertexShader);
  104.     glDeleteShader(fragmentShader);
  105.  
  106.     unsigned int VAO;
  107.     glGenVertexArrays(1, &VAO);
  108.  
  109.     glBindVertexArray(VAO);
  110.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  111.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  112.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  113.     glEnableVertexAttribArray(0);
  114.  
  115.     // render loop
  116.     while (!glfwWindowShouldClose(window))
  117.     {
  118.         // input
  119.         processInput(window);
  120.  
  121.         // rendering commands here
  122.         glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  123.         glClear(GL_COLOR_BUFFER_BIT);
  124.  
  125.         glUseProgram(shaderProgram);
  126.         glBindVertexArray(VAO);
  127.         glDrawArrays(GL_TRIANGLES, 0, 3);
  128.  
  129.         // check and call events and swap the buffers
  130.         glfwSwapBuffers(window);
  131.         glfwPollEvents();
  132.     }
  133.  
  134.     glfwTerminate();
  135.     return 0;
  136. }
  137.  
  138. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  139. {
  140.     glViewport(0, 0, width, height);
  141. }
  142.  
  143. void processInput(GLFWwindow* window)
  144. {
  145.     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  146.     {
  147.         glfwSetWindowShouldClose(window, true);
  148.     }
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement