Skip to content

098tarik/OpenGLPractice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

OpenGL Practice

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.

hello_triangle.cpp Walkthrough

1. Initialization (GLFW + GLAD)

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.

2. Shader Compilation & Linking

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.

3. Vertex Data & Buffer Setup

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.

4. Performance Timing

The time taken for shader compilation and buffer setup is measured with std::chrono::high_resolution_clock and printed to the console on startup.

5. Render Loop

The polygon mode is set to GL_LINE (wireframe). Each frame:

  1. An FPS counter prints the frame count to the console every second.
  2. Input is polled — pressing Escape closes the window.
  3. The screen is cleared to a dark teal color (0.2, 0.3, 0.3).
  4. The shader program is activated, the VAO is bound, and glDrawArrays draws the triangle.
  5. Buffers are swapped and events are polled.

6. Cleanup

When the window is closed, the VAO, VBO, and shader program are deleted, and GLFW is terminated.

Helper Functions

  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages