Simple triangle example

Make sure you have a valid OpenGL 4.5 context and it is active.

// -------------------------------------------------------------------------
// Create and initialize Vertex Buffer Object (VBO).
//
GLuint vbo {};
glCreateBuffers(1, &vbo);

struct vertex {
    float x, y, z;
    float r, g, b;
};

const std::vector<vertex> vertices {
    {-1.0f, -1.0f, 1.0f,  1.0f, 0.0f, 0.0f }, // Vertex 1
    { 0.0f,  1.0f, 1.0f,  0.0f, 1.0f, 0.0f }, // Vertex 2
    { 1.0f, -1.0f, 1.0f,  0.0f, 0.0f, 1.0f }  // Vertex 3
};

glNamedBufferStorage(vbo, vertices.size() * sizeof(vertex), vertices.data(), 0);

// VBO initialization finished. --------------------------------------------

// -------------------------------------------------------------------------
// Create and initialize vertex shaders.
//
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);

const GLchar* vertexShaderSource = {
    "#version 450\n"
    "layout(location = 0) in vec3 iposition;\n"
    "layout(location = 1) in vec3 icolor;\n"
    "out vec4 color;"
    "void main() {\n"
    "color = vec4(icolor, 1.0f);\n"
    "gl_Position = vec4(iposition, 1.0f);\n"
    "}"
};

glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
checkStatusFor(vertexShader);

//
// Create and initialize fragment shaders.
//
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

const GLchar* fragmentShaderSource = {
    "#version 450\n"
    "in vec4 color;\n"
    "layout(location = 0) out vec4 ocolor;\n"
    "void main() {\n"
    "  ocolor = color;\n"
    "}"
};

glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
checkStatusFor(fragmentShader);

//
// Create and initialize shader program.
//
GLuint shaderProgram = glCreateProgram();

glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);

// Shader initialization finished. -----------------------------------------

// -------------------------------------------------------------------------
// Create and initialize Vertex Array Object (VAO).
//
GLuint vao {};
glCreateVertexArrays(1, &vao);

glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(vertex));
glVertexArrayAttribBinding(vao, 0, 0);
glVertexArrayAttribBinding(vao, 1, 0);
glVertexArrayAttribFormat(vao, 0, 3, GL_FLOAT, GL_FALSE, offsetof(vertex, x));
glVertexArrayAttribFormat(vao, 1, 3, GL_FLOAT, GL_FALSE, offsetof(vertex, r));
glVertexArrayBindingDivisor(vao, 0, 0);
glEnableVertexArrayAttrib(vao, 0);
glEnableVertexArrayAttrib(vao, 1);

// VAO initialization finished. --------------------------------------------

while(system->update()) {

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glViewport(0, 0, windowWidth, windowHeight);

    glBindVertexArray(vao);
    glUseProgram(shaderProgram);
    glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.size());

    // Maybe you might want to do a swap buffer here.
}

// -------------------------------------------------------------------------
// Delete all created objects.
//
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteProgram(shaderProgram);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);

Last updated