opengl

OpenGL - SDL
Log | Files | Refs

triangle.c (2718B)


      1 #include "triangle.h"
      2 // #include "window.h"
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <GL/glew.h>
      6 #include <GLFW/glfw3.h>
      7 
      8 
      9 const char *vertexShaderSource = "#version 330 core\n"
     10     "layout (location = 0) in vec3 aPos;\n"
     11     "void main()\n"
     12     "{\n"
     13     "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
     14     "}\0";
     15 const char *fragmentShaderSource = "#version 330 core\n"
     16     "out vec4 FragColor;\n"
     17     "void main()\n"
     18     "{\n"
     19     "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
     20     "}\n\0";
     21 
     22 void triangle()
     23 {
     24 
     25     vertexShader = glCreateShader(GL_VERTEX_SHADER);
     26     glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
     27     glCompileShader(vertexShader);
     28 
     29     int success;
     30     glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
     31 
     32     fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
     33     glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
     34     glCompileShader(fragmentShader);
     35 
     36     shaderProgram = glCreateProgram();
     37     glAttachShader(shaderProgram, vertexShader);
     38     glAttachShader(shaderProgram, fragmentShader);
     39     glLinkProgram(shaderProgram);
     40     glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
     41     if (!success) {
     42         exit(fprintf(stderr, "ERROR::SHADER::PROGRAM::LINKING_FAILED\n"));
     43     }
     44 
     45     glDeleteShader(vertexShader);
     46     glDeleteShader(fragmentShader);
     47 
     48     // set up vertex data (and buffer(s)) and configure vertex attributes
     49     // ------------------------------------------------------------------
     50     float vertices[] = {
     51         -0.5f, -0.5f, 0.0f, // left
     52          0.5f, -0.5f, 0.0f, // right
     53          0.0f,  0.5f, 0.0f  // top
     54     };
     55 
     56     glGenVertexArrays(1, &VAO);
     57     glGenBuffers(1, &VBO);
     58     // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
     59     glBindVertexArray(VAO);
     60 
     61     glBindBuffer(GL_ARRAY_BUFFER, VBO);
     62     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
     63 
     64     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
     65     glEnableVertexAttribArray(0);
     66 
     67     // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
     68     glBindBuffer(GL_ARRAY_BUFFER, 0);
     69 
     70     // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
     71     // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
     72     glBindVertexArray(0);
     73 }
     74 
     75 void draw_triangle()
     76 {
     77         glUseProgram(shaderProgram);
     78         glBindVertexArray(VAO);
     79         glDrawArrays(GL_TRIANGLES, 0, 3);
     80 
     81 }