Advertisement
Guest User

minimal opengl test

a guest
Dec 26th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <GL/glew.h>
  4.  
  5. #include <GLFW/glfw3.h>
  6.  
  7. bool install_shader(GLuint where, std::string const& code, GLuint* attach_to) {
  8.   GLuint shader_id = glCreateShader(where);
  9.   {
  10.     auto const p = code.c_str();
  11.     glShaderSource(shader_id,1,&p,nullptr); // nullptr indicates null-termination here
  12.   }
  13.   glCompileShader(shader_id);
  14.   GLint out;
  15.   glGetShaderiv(shader_id, GL_COMPILE_STATUS, &out);
  16.   if (out == GL_FALSE) {
  17.     // something went wrong with the shader compilation
  18.     glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &out);
  19.     if (out == 0) {
  20.       std::cout << "Unknown Error during shader compilation\n";
  21.     } else {
  22.       std::string error;
  23.       error.resize(out-1);
  24.       glGetShaderInfoLog(shader_id, out, nullptr, error.data());
  25.       std::cout << "Shader Compilation failed with error: " << error;
  26.     }
  27.     return false;
  28.   } else {
  29.     std::cout << "shader(" << int(shader_id) << "@" << int(where) << ") compiled fine\n";
  30.  
  31.     if (attach_to) {
  32.       glAttachShader(*attach_to, shader_id);
  33.     }
  34.  
  35.     // XXX THE SHADERS SHOULD BE DELETED AFTER LINKING !!!!
  36.     // glDeleteShader(shader_id)
  37.  
  38.     return true;
  39.   }
  40. }
  41.  
  42. bool install_program(GLuint program_id) {
  43.   glLinkProgram(program_id);
  44.   GLint out;
  45.   glGetProgramiv(program_id, GL_LINK_STATUS, &out);
  46.   if (out == GL_FALSE) {
  47.     // something went wrong with the program linking
  48.     glGetProgramiv(program_id, GL_INFO_LOG_LENGTH, &out);
  49.     if (out == 0) {
  50.       std::cout << "Unknown link-Error in shader program\n";
  51.     } else {
  52.       std::string error;
  53.       error.resize(out-1);
  54.       glGetProgramInfoLog(program_id, out, nullptr, error.data());
  55.       std::cout << "Program linking failed with error: " << error;
  56.     }
  57.     return false;
  58.   } else {
  59.     std::cout << "program(" << int(program_id) << ") compiled fine\n";
  60.     return true;
  61.   }
  62. }
  63.  
  64. int main() {
  65.   if (glfwInit()) {
  66.     std::cout <<  "Initialisation fine\n";
  67.   } else {
  68.     std::cout <<  "Initialisation failed\n";
  69.     return 1;
  70.   }
  71.  
  72.   glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
  73.   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
  74.   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  75.  
  76.   if (GLFWwindow* window = glfwCreateWindow(800,600,"testwindow",nullptr,nullptr)) {
  77.     glfwMakeContextCurrent(window);
  78.  
  79.     if (glewInit() == GLEW_OK) {
  80.       std::cout << "GLEW also fine\n";
  81.     } else {
  82.       std::cout << "GLEW says nope!\n";
  83.       goto die;
  84.       return 2;
  85.     }
  86.  
  87.     //std::cout << "Ensure we can capture the escape key being pressed below...\n";
  88.     glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  89.     /*business*/
  90.     // (1) specify the triangle to draw
  91.     // An array of 3 vectors which represents 3 vertices
  92.     GLfloat g_vertex_buffer_data[] = {
  93.        -0.4f, -0.8f, 0.f,
  94.         0.4f, -0.8f, 0.f,
  95.         0.0f,  0.8f, 0.f,
  96.     };
  97.     GLuint vertexbuffer;
  98.     glGenBuffers(1, &vertexbuffer);
  99.  
  100.     GLuint program_id = glCreateProgram();
  101.  
  102.     // (3) vertex shader
  103.     std::string vshader_code = "#version 330 core\n"
  104.       "layout (location = 0) in vec3 aPos;\n"
  105.       "\n"
  106.       "void main()\n"
  107.       "{\n"
  108.       "    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
  109.       "}\n";
  110.  
  111.     if (!install_shader(GL_VERTEX_SHADER,vshader_code,&program_id)) {
  112.       goto die;
  113.     }
  114.  
  115.     std::string fshader_code = "#version 330 core\n"
  116.       "out vec3 color;\n"
  117.       "\n"
  118.       "void main()\n"
  119.       "{\n"
  120.       "    color = vec3(0.9,0.8,0.1);\n"
  121.       "}\n";
  122.    
  123.     if (!install_shader(GL_FRAGMENT_SHADER,fshader_code,&program_id)) {
  124.       goto die;
  125.     }
  126.  
  127.     if (!install_program(program_id)) {
  128.       goto die;
  129.     }
  130.  
  131.     glClearColor(0.3, 0.5, 0.5, 1.0);
  132.  
  133.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  134.     glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
  135.  
  136.     do {
  137.       glClear(GL_COLOR_BUFFER_BIT);
  138.  
  139.       glUseProgram(program_id);
  140.  
  141.  
  142.       glEnableVertexAttribArray(0); // this 0 also refes to 'layout (location = 0)' in the vertex shader
  143.  
  144.       glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  145.       glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
  146.  
  147.       glVertexAttribPointer(
  148.         0,        // this refes to 'layout (location = 0)' in the vertex shader
  149.         3,        // each vertex has 3 coordinates (2d data is also possible)
  150.         GL_FLOAT, // coordinates are of type GLfloat (GL_FLOAT)
  151.         GL_FALSE, // we don't need the input data to be nomalised,
  152.         0, //&g_vertex_buffer_data[3] - &g_vertex_buffer_data[0], // stride
  153.         nullptr        // offset ptr
  154.       );
  155.  
  156.       // Draw the triangle !
  157.       glDrawArrays(GL_TRIANGLES, 0, 3);
  158.       glDisableVertexAttribArray(0);
  159.  
  160.       glfwSwapBuffers(window);
  161.  
  162.       glfwPollEvents();
  163.     } // Check if the ESC key was pressed or the window was closed
  164.     while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 );
  165.  
  166.   } else {
  167.     std::cout << "Could not create window\n";
  168.   }
  169.  
  170.   die:
  171.   glfwTerminate();
  172.   std::cout <<  "Terminated\n";
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement