Advertisement
Guest User

OpenGL

a guest
Jul 2nd, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <GL/glew.h>  
  2. #include <GLFW/glfw3.h>  
  3. #include <stdio.h>  
  4. #include <iostream>
  5. #include <glm.hpp>
  6.  
  7. void error_callback(int error, const char* desc) {
  8.  
  9.     std::cout << desc;
  10.  
  11. }
  12.  
  13. int main() {
  14.  
  15.     GLFWwindow* window;
  16.  
  17.     if (!glfwInit()) {
  18.    
  19.         return 1;
  20.  
  21.     }
  22.  
  23.     glfwWindowHint(GLFW_SAMPLES, 4);
  24.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  25.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  26.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  27.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  28.     window = glfwCreateWindow(800, 640, "OpenGL Window", NULL, NULL);
  29.     glfwMakeContextCurrent(window);
  30.  
  31.     glewExperimental = GL_TRUE;
  32.     GLenum err = glewInit();
  33.  
  34.     if (GLEW_OK != err) {
  35.    
  36.         std::cout << "Failed to init.";
  37.         return 1;
  38.    
  39.     }
  40.  
  41.  
  42.     float points[] = {
  43.  
  44.        -0.5f,  0.5f, 0.0f, // top left      = 0
  45.         0.5f,  0.5f, 0.0f, // top right     = 1
  46.         0.5f, -0.5f, 0.0f, // bottom right  = 2
  47.        -0.5f, -0.5f, 0.0f, // bottom left   = 3
  48.  
  49.     };
  50.  
  51.     GLuint elements[] = {
  52.  
  53.         0, 1, 2,
  54.         2, 3, 0,
  55.     };
  56.  
  57.     // generate vbo (point buffer)
  58.     GLuint pb = 0;
  59.     glGenBuffers(1, &pb);
  60.     glBindBuffer(GL_ARRAY_BUFFER, pb);
  61.     glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
  62.  
  63.     // generate element buffer object (ibo/ebo)
  64.     GLuint ebo = 0;
  65.     glGenBuffers(1, &ebo);
  66.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  67.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
  68.  
  69.     // generate vao
  70.     GLuint vao = 0;
  71.     glGenVertexArrays(1, &vao);
  72.     glBindVertexArray(vao);
  73.  
  74.  
  75.     glBindBuffer(GL_ARRAY_BUFFER, pb);
  76.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  77.  
  78.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  79.  
  80.     glEnableVertexAttribArray(0);
  81.     glEnableVertexAttribArray(1);
  82.  
  83.  
  84.     // only draw the outlines of the shapes
  85.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  86.  
  87.  
  88.     const char* vertex_shader =
  89.         "#version 400\n"
  90.         "layout(location = 0) in vec3 vp;"
  91.         "void main () {"
  92.         "  gl_Position = vec4 (vp, 1.0);"
  93.         "}";
  94.  
  95.     const char* fragment_shader =
  96.         "#version 400\n"
  97.         "out vec4 frag_colour;"
  98.         "void main () {"
  99.         "  frag_colour = vec4 (1.0, 0.0, 0.0, 1.0);"
  100.         "}";
  101.  
  102.     // create shader program and compile
  103.     GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  104.     glShaderSource(vs, 1, &vertex_shader, NULL);
  105.     glCompileShader(vs);
  106.     GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
  107.     glShaderSource(fs, 1, &fragment_shader, NULL);
  108.     glCompileShader(fs);
  109.  
  110.     // attach the prorgram
  111.     GLuint sp = glCreateProgram();
  112.     glAttachShader(sp, fs);
  113.     glAttachShader(sp, vs);
  114.     glLinkProgram(sp);
  115.  
  116.     while (!glfwWindowShouldClose(window)) {
  117.  
  118.         glClear(GL_COLOR_BUFFER_BIT);
  119.         glUseProgram(sp);
  120.         glBindVertexArray(vao);
  121.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  122.         glfwSwapBuffers(window);
  123.         glfwPollEvents();
  124.  
  125.     }
  126.  
  127.     glfwTerminate();
  128.     glfwDestroyWindow(window);
  129.  
  130.     return 0;
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement