LinAGKar

hello triangle

Nov 8th, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3.  
  4. #include <cstdlib>
  5. #include <iostream>
  6.  
  7. static void error_callback(int error, const char *description) {
  8.     std::cerr << description;
  9. }
  10.  
  11. static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  12.     if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action == GLFW_PRESS) {
  13.         glfwSetWindowShouldClose(window, GL_TRUE);
  14.     }
  15. }
  16.  
  17. static void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
  18.     glViewport(0, 0, width, height);
  19. }
  20.  
  21.  
  22. int main(int argc, char const *argv[]) {
  23.     // start GL context and O/S window using the GLFW helper library
  24.    
  25.     glfwSetErrorCallback(error_callback);
  26.     if(!glfwInit()) {
  27.         exit(EXIT_FAILURE);
  28.     }
  29.  
  30.     GLFWwindow *window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
  31.     glfwSetKeyCallback(window, key_callback);
  32.     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  33.    
  34.     if (!window) {
  35.         glfwTerminate();
  36.         exit(EXIT_FAILURE);
  37.     }
  38.     glfwMakeContextCurrent(window);
  39.    
  40.     // start GLEW extension handler
  41.     glewExperimental = GL_TRUE;
  42.     glewInit();
  43.  
  44.     //-------------------------------------------------------------------------//
  45.     // Set up geometry, VBO, VAO
  46.     //-------------------------------------------------------------------------//
  47.    
  48.     const GLfloat vertices[9] = {
  49.         -1, -1, 0,
  50.         0, 1, 0,
  51.         1, -1, 0
  52.     };
  53.  
  54.     GLuint GLVertexArrayID;
  55.     //glCreateVertexArrays(1, &GLVertexArrayID);
  56.     glGenVertexArrays(1, &GLVertexArrayID);
  57.     glBindVertexArray(GLVertexArrayID);
  58.  
  59.     GLuint GLBufferID;
  60.     //glCreateBuffers(1, &GLBufferID);
  61.     glGenBuffers(1, &GLBufferID);
  62.     glBindBuffer(GL_ARRAY_BUFFER, GLBufferID);
  63.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  64.  
  65.     glEnableVertexAttribArray(0);
  66.     glVertexAttribPointer(
  67.         0,
  68.         3,
  69.         GL_FLOAT,
  70.         GL_FALSE,
  71.         0,
  72.         (void*)0
  73.     );
  74.  
  75.     const char* vertex_shader =
  76.         "#version 330\n"
  77.         "in vec3 vp;"
  78.         "void main() {"
  79.         "    gl_Position = vec4(vp, 1.0);"
  80.         "}";
  81.    
  82.     const char* fragment_shader =
  83.         "#version 330\n"
  84.         "out vec4 frag_colour;"
  85.         "void main() {"
  86.         "    frag_colour = vec4(0.5, 0.0, 0.5, 1.0);"
  87.         "}";
  88.  
  89.     GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  90.     glShaderSource(vs, 1, &vertex_shader, NULL);
  91.     glCompileShader(vs);
  92.    
  93.     GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
  94.     glShaderSource(fs, 1, &fragment_shader, NULL);
  95.     glCompileShader(fs);
  96.  
  97.     GLuint shader_programme = glCreateProgram();
  98.     glAttachShader(shader_programme, fs);
  99.     glAttachShader(shader_programme, vs);
  100.     glLinkProgram(shader_programme);
  101.  
  102.     glDeleteShader(vs);
  103.     glDeleteShader(fs);
  104.  
  105.     glUseProgram(shader_programme);
  106.  
  107.     while (!glfwWindowShouldClose(window)) {
  108.         // update other events like input handling
  109.         glfwPollEvents();
  110.         // clear the drawing surface
  111.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  112.        
  113.         //-------------------------------------------------------------------------//
  114.         // Issue an appropriate glDraw*() command.
  115.         //-------------------------------------------------------------------------//
  116.         glDrawArrays(GL_TRIANGLES, 0, 3);
  117.  
  118.         //refresh the displayed image
  119.         glfwSwapBuffers(window);
  120.     }
  121.  
  122.     // close GL context and any other GLFW resources
  123.     glfwTerminate();
  124.     exit(EXIT_SUCCESS);
  125. }
Advertisement
Add Comment
Please, Sign In to add comment