A simple OpenGL project that renders a triangle using modern OpenGL (3.3 Core Profile). Built with C++, GLFW, and GLAD, following the LearnOpenGL tutorials.
The program begins by initializing GLFW and requesting an OpenGL 3.3 Core Profile context. An 800×600 window titled "LearnOpenGL" is created, and a framebuffer resize callback is registered so the viewport adjusts when the user resizes the window. GLAD is then loaded to resolve all OpenGL function pointers at runtime.
Two shaders are compiled from inline GLSL source strings:
- Vertex shader — passes each vertex position straight through to
gl_Position. - Fragment shader — outputs a fixed orange color (
vec4(1.0, 0.5, 0.2, 1.0)).
Both shaders are checked for compile errors. They are then attached to a shader program, linked, and the individual shader objects are deleted since they are no longer needed.
A single triangle is defined by three vertices:
| Vertex | X | Y | Z |
|---|---|---|---|
| Left | −0.5 | −0.5 | 0.0 |
| Right | 0.5 | −0.5 | 0.0 |
| Top | 0.0 | 0.5 | 0.0 |
A VAO (Vertex Array Object) and VBO (Vertex Buffer Object) are created. The vertex data is uploaded to the GPU via glBufferData, and a single vertex attribute (position, location 0) is configured with glVertexAttribPointer. The vertex array is defined inside a scoped block so the CPU-side data is freed immediately after upload.
The time taken for shader compilation and buffer setup is measured with std::chrono::high_resolution_clock and printed to the console on startup.
The polygon mode is set to GL_LINE (wireframe). Each frame:
- An FPS counter prints the frame count to the console every second.
- Input is polled — pressing
Escapecloses the window. - The screen is cleared to a dark teal color (
0.2, 0.3, 0.3). - The shader program is activated, the VAO is bound, and
glDrawArraysdraws the triangle. - Buffers are swapped and events are polled.
When the window is closed, the VAO, VBO, and shader program are deleted, and GLFW is terminated.
processInput— checks if the Escape key is pressed and signals the window to close.framebuffer_size_callback— called by GLFW whenever the window is resized; updates the OpenGL viewport to match the new dimensions.