Advertisement
SrinjoySS01

main.cpp

Dec 5th, 2020
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. static unsigned int CompileShader(unsigned int type, const string& source){
  7.     unsigned int id = glCreateShader(type);
  8.     const char* src = source.c_str();
  9.     glShaderSource(id, 1, &src, nullptr);
  10.     glCompileShader(id);
  11.    
  12.     int result;
  13.     glGetShaderiv(id, GL_COMPILE_STATUS, &result);
  14.     if (result == GL_FALSE) {
  15.         int length;
  16.         glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
  17.         char* message = (char*) alloca(length * sizeof(char));
  18.         glGetShaderInfoLog(id, length, &length, message);
  19.         cout << "Failed to compile" << (type == GL_VERTEX_SHADER ? " vertex" : " fragment") << " shader!" << endl;
  20.         cout << message << endl;
  21.         glDeleteShader(id);
  22.         return 0;
  23.     }
  24.     return id;
  25. }
  26.  
  27. static unsigned int CreateShader(const string& vertexShader, const string& fragmentShader) {
  28.     unsigned int program = glCreateProgram();
  29.     unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
  30.     unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
  31.    
  32.     glAttachShader(program, vs);
  33.     glAttachShader(program, fs);
  34.     glLinkProgram(program);
  35.     glValidateProgram(program);
  36.    
  37.     glDeleteShader(vs);
  38.     glDeleteShader(fs);
  39.    
  40.     return program;
  41. }
  42.  
  43. int main() {
  44.     GLFWwindow* window;
  45.     if (!glfwInit()) return -1;
  46.    
  47.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  48.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
  49.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  50.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  51.    
  52.     window = glfwCreateWindow(640, 480, "Srinjoy", NULL, NULL);
  53.     if (!window) {
  54.         glfwTerminate();
  55.         return -1;
  56.     }
  57.     glfwMakeContextCurrent(window);
  58.     if (glewInit() != GLEW_OK) {
  59.         cout << "Error initialising glew" << endl;
  60.         return -1;
  61.     }
  62.    
  63.     cout << glGetString(GL_VERSION) << endl;
  64.    
  65.     float position[6] = {
  66.         -0.5F, -0.5F,
  67.          0.0F,  0.5F,
  68.          0.5F, -0.5F
  69.     };
  70.    
  71.     unsigned int buffer;
  72.     glGenBuffers(1, &buffer);
  73.     glBindBuffer(GL_ARRAY_BUFFER, buffer);
  74.     glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), position, GL_STATIC_DRAW);
  75.    
  76.     glEnableVertexAttribArray(0);
  77.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
  78.    
  79.     string vertexShader =
  80.         "#version 330 core\n"
  81.         "layout(location = 0) in vec4 position;\n"
  82.         "void main() {\n"
  83.         "   gl_Position = position;\n"
  84.         "}";
  85.     string fragmentShader =
  86.         "#version 330 core\n"
  87.         "layout(location = 0) out vec4 color;\n"
  88.         "void main() {\n"
  89.         "   color = vec4(1.0, 1.0, 0.0, 1.0);\n"
  90.         "}";
  91.    
  92.     unsigned int shader = CreateShader(vertexShader, fragmentShader);
  93.     glUseProgram(shader);
  94.    
  95.    
  96.     while (!glfwWindowShouldClose(window)){
  97.         glClear(GL_COLOR_BUFFER_BIT);
  98.         glDrawArrays(GL_TRIANGLES, 0, 3);
  99.         glfwSwapBuffers(window);
  100.         glfwPollEvents();
  101.     }
  102.     glfwTerminate();
  103.     return 0;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement