Advertisement
Brejlounek

Untitled

Oct 25th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. // Include standard headers
  2. #include <stdio.h>
  3. #include <stdlib.h>
  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. using namespace glm;
  15.  
  16. #include <common/shader.hpp>
  17.  
  18. int main( void )
  19. {
  20.     // Initialise GLFW
  21.     if( !glfwInit() )
  22.     {
  23.         fprintf( stderr, "Failed to initialize GLFW\n" );
  24.         return -1;
  25.     }
  26.  
  27.     glfwWindowHint(GLFW_SAMPLES, 4);
  28.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  29.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  30.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  31.  
  32.     // Open a window and create its OpenGL context
  33.     window = glfwCreateWindow( 1024, 768, "Tutorial 02 - Red triangle", NULL, NULL);
  34.     if( window == NULL ){
  35.         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" );
  36.         glfwTerminate();
  37.         return -1;
  38.     }
  39.     glfwMakeContextCurrent(window);
  40.  
  41.     // Initialize GLEW
  42.     glewExperimental = true; // Needed for core profile
  43.     if (glewInit() != GLEW_OK) {
  44.         fprintf(stderr, "Failed to initialize GLEW\n");
  45.         return -1;
  46.     }
  47.  
  48.     // Ensure we can capture the escape key being pressed below
  49.     glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  50.  
  51.     // Dark blue background
  52.     glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  53.  
  54.     GLuint VertexArrayID;
  55.     glGenVertexArrays(1, &VertexArrayID);
  56.     glBindVertexArray(VertexArrayID);
  57.  
  58.     // Create and compile our GLSL program from the shaders
  59.     GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader" );
  60.  
  61.  
  62.     static const GLfloat g_vertex_buffer_data[] = {
  63.         -1.0f, -1.0f, 0.0f,
  64.          1.0f, -1.0f, 0.0f,
  65.          0.0f,  1.0f, 0.0f,
  66.     };
  67.  
  68.     GLuint vertexbuffer;
  69.     glGenBuffers(1, &vertexbuffer);
  70.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  71.     glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
  72.  
  73.     do{
  74.  
  75.         // Clear the screen
  76.         glClear( GL_COLOR_BUFFER_BIT );
  77.  
  78.         // Use our shader
  79.         glUseProgram(programID);
  80.  
  81.         // 1rst attribute buffer : vertices
  82.         glEnableVertexAttribArray(0);
  83.         glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  84.         glVertexAttribPointer(
  85.             0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
  86.             3,                  // size
  87.             GL_FLOAT,           // type
  88.             GL_FALSE,           // normalized?
  89.             0,                  // stride
  90.             (void*)0            // array buffer offset
  91.         );
  92.  
  93.         // Draw the triangle !
  94.         glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle
  95.  
  96.         glDisableVertexAttribArray(0);
  97.  
  98.         // Swap buffers
  99.         glfwSwapBuffers(window);
  100.         glfwPollEvents();
  101.  
  102.     } // Check if the ESC key was pressed or the window was closed
  103.     while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
  104.            glfwWindowShouldClose(window) == 0 );
  105.  
  106.     // Cleanup VBO
  107.     glDeleteBuffers(1, &vertexbuffer);
  108.     glDeleteVertexArrays(1, &VertexArrayID);
  109.     glDeleteProgram(programID);
  110.  
  111.     // Close OpenGL window and terminate GLFW
  112.     glfwTerminate();
  113.  
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement