Guest User

my code

a guest
Jan 4th, 2026
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | Source Code | 0 0
  1. #define GLFW_INCLUDE_NONE
  2. #include <glad/gl.h>
  3. #include <GLFW/glfw3.h>
  4. #include <iostream>
  5. #include <string>
  6.  
  7.  
  8.  
  9. void error_callback(int error, const char* description) {
  10.     fprintf(stderr, "Error: %s\n", description);
  11. }
  12.  
  13. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  14. {
  15.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  16.         glfwSetWindowShouldClose(window, GLFW_TRUE);
  17. }
  18.  
  19. int main(void) {
  20.     const int windowWidth = 640;
  21.     const int windowHeight = 480;
  22.     const char* windowTitle = "GLFW TEST";
  23.  
  24.     //GLFW Init
  25.     std::cout << "Starting GLFW..." << std::endl;
  26.     if (!glfwInit()) {
  27.         //Init Failed
  28.  
  29.     }
  30.  
  31.     //Error Setup
  32.  
  33.     glfwSetErrorCallback(error_callback);
  34.  
  35.     //Window setup
  36.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Sets Minimum OpenGL version
  37.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //Sets Minimum OpenGL version, why twice?
  38.     GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, windowTitle, NULL, NULL);
  39.     if (!window) {
  40.         //Window creation Failed
  41.         std::cout << "wtf i wanna die why tf is my window not init???" << std::endl;
  42.     }
  43.     glfwMakeContextCurrent(window);
  44.     gladLoadGL(glfwGetProcAddress); //Loads OpenGL with glad I guess?? REQUIRES CONTEXT TO BE FIRST!!!
  45.    
  46.     //Learn what the hell this means!!!
  47.     int width, height;
  48.     glfwGetFramebufferSize(window, &width, &height);
  49.     glViewport(0, 0, width, height);
  50.     std::cout << "RENDERER: " << glGetString(GL_RENDERER) << std::endl;
  51.     std::cout << "OpenGL version supported: " << glGetString(GL_VERSION) << std::endl;
  52.     //Keyboard Input
  53.     glfwSetKeyCallback(window, key_callback);
  54.  
  55.     glfwSwapInterval(1); //swap shit
  56.     //Triangle Shit.
  57.     float points[] = {
  58.         0.5f,  0.5f,  0.0f, // x,y,z of first point.
  59.         0.5f, -0.5f,  0.0f, // x,y,z of second point.
  60.         -0.5f, -0.5f,  0.0f  // x,y,z of third point.
  61.     };
  62.     float pointsb[] = {
  63.         -0.5f,  -0.5f,  0.0f, // x,y,z of first point.
  64.         -0.5f, 0.5f,  0.0f, // x,y,z of second point.
  65.         0.5f, 0.5f,  0.0f  // x,y,z of third point.
  66.     };
  67.  
  68.     //ts is complicated study it and read the tut: https://antongerdelan.net/opengl/hellotriangle.html
  69.  
  70.  
  71.     GLuint vbo = 0;
  72.     glGenBuffers(1, &vbo);
  73.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  74.     glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
  75.     GLuint vbob = 0;
  76.     glGenBuffers(1, &vbob);
  77.     glBindBuffer(GL_ARRAY_BUFFER, vbob);
  78.     glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), pointsb, GL_STATIC_DRAW);
  79.     GLuint vao = 0;
  80.     glGenVertexArrays(1, &vao);
  81.     glBindVertexArray(vao);
  82.     glEnableVertexAttribArray(0);
  83.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  84.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  85.  
  86.     GLuint vaob = 0;
  87.     glGenVertexArrays(1, &vaob);
  88.     glBindVertexArray(vaob);
  89.     glEnableVertexAttribArray(0);
  90.     glBindBuffer(GL_ARRAY_BUFFER, vbob);
  91.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  92.  
  93.     const char* vertex_shader =
  94.         "#version 410 core\n"
  95.         "in vec3 vp;"
  96.         "void main() {"
  97.         "gl_Position = vec4( vp, 1.0);"
  98.         "}";
  99.     const char* fragment_shader =
  100.         "#version 410 core\n"
  101.         "out vec4 frag_colour;"
  102.         "void main() {"
  103.         "  frag_colour = vec4( 1.0, 0.0, 0.0, 1.0 );"
  104.         "}";
  105.  
  106.     //loads the shaders and compiles the,.
  107.     GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  108.     glShaderSource(vs, 1, &vertex_shader, NULL);
  109.     glCompileShader(vs);
  110.     GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
  111.     glShaderSource(fs, 1, &fragment_shader, NULL);
  112.     glCompileShader(fs);
  113.     //Combines tyhe shaders into a single shader and links them.
  114.     GLuint shader_program = glCreateProgram();
  115.     glAttachShader(shader_program, fs);
  116.     glAttachShader(shader_program, vs);
  117.     glLinkProgram(shader_program);
  118.  
  119.     while (!glfwWindowShouldClose(window)) {
  120.         // keep running window.
  121.         glfwPollEvents();
  122.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  123.  
  124.         // Put the shader program, and the VAO, in focus in OpenGL's state machine.
  125.         glUseProgram(shader_program);
  126.         glBindVertexArray(vao);
  127.         glBindVertexArray(vaob);
  128.  
  129.         // Draw points 0-3 from the currently bound VAO with current in-use shader.
  130.         glDrawArrays(GL_TRIANGLES, 0, 3);
  131.         glfwSwapBuffers(window); //Swaps buffers
  132.        
  133.     }
  134.    
  135.     glfwTerminate();
  136.     return 0;
  137. }
  138.  
Tags: opengl
Advertisement
Add Comment
Please, Sign In to add comment