Advertisement
hnOsmium0001

gl test

Oct 14th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <GL/glew.h>
  5.  
  6. #include <GLFW/glfw3.h>
  7.  
  8. #include <glm/glm.hpp>
  9. using namespace glm;
  10.  
  11. #include "../common/shader.hpp"
  12.  
  13. GLFWwindow* window;
  14. int main( void )
  15. {
  16.     // Initialise GLFW
  17.     if( !glfwInit() )
  18.     {
  19.         fprintf( stderr, "Failed to initialize GLFW\n" );
  20.         getchar();
  21.         return -1;
  22.     }
  23.  
  24.     glfwWindowHint(GLFW_SAMPLES, 4);
  25.     glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
  26.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  27.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  28.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
  29.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  30.  
  31.     // Open a window and create its OpenGL context
  32.     window = glfwCreateWindow( 1024, 768, "Playground", nullptr, nullptr);
  33.     if(window == nullptr){
  34.         fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
  35.         getchar();
  36.         glfwTerminate();
  37.         return -1;
  38.     }
  39.     glfwMakeContextCurrent(window);
  40.  
  41.     // Initialize GLEW
  42.     // IMPORTANT: if this is not enable this might create SegmentationFault
  43.     glewExperimental = true; // For core profile
  44.     if (glewInit() != GLEW_OK) {
  45.         fprintf(stderr, "Failed to initialize GLEW\n");
  46.         getchar();
  47.         glfwTerminate();
  48.         return -1;
  49.     }
  50.  
  51.     // Ensure we can capture the escape key being pressed below
  52.     glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  53.  
  54.     // Dark blue background
  55.     glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  56.    
  57.     GLuint vao;
  58.     glGenVertexArrays(1, &vao);
  59.     glBindVertexArray(vao);
  60.  
  61.     GLuint programID = LoadShaders("shader.vert", "shader.frag");
  62.    
  63.     static const GLfloat vertices[] = {
  64.        -1.0f, -1.0f, 0.0f,
  65.        1.0f, -1.0f, 0.0f,
  66.        0.0f,  1.0f, 0.0f,
  67.     };
  68.  
  69.     GLuint vbo;
  70.     glGenBuffers(1, &vbo);
  71.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  72.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  73.  
  74.     do{
  75.         glClear(GL_COLOR_BUFFER_BIT);
  76.  
  77.         glUseProgram(programID);
  78.  
  79.         glEnableVertexAttribArray(0);
  80.         glBindBuffer(GL_ARRAY_BUFFER, vbo);
  81.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  82.         glDrawArrays(GL_TRIANGLES, 0, 3);
  83.         glDisableVertexAttribArray(0);
  84.  
  85.         // Swap buffers
  86.         glfwSwapBuffers(window);
  87.         glfwPollEvents();
  88.  
  89.     } // Check if the ESC key was pressed or the window was closed
  90.     while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
  91.            glfwWindowShouldClose(window) == 0 );
  92.    
  93.     glDeleteBuffers(1, &vbo);
  94.     glDeleteVertexArrays(1, &vao);
  95.     glDeleteProgram(programID);
  96.  
  97.     // Close OpenGL window and terminate GLFW
  98.     glfwTerminate();
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement