Real-Time 3D: My Journey in Building a C++ OpenGL Rendering Engine
Tags: gamedev, cpp, programming, devjournal
Published: 2024-07-13 20:02:33


![Showcase of Real-Time Rendering Capabilities: Dynamic Lighting in Engine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6iwc3ikzrkfvvt09bu1u.png)

## What is a Rendering Engine?
 A rendering engine abstracts the process of drawing objects on the screen. It uses the GPU to convert logic (e.g., a 3D model) into a 2D image displayed on a screen.

### Motivation:
 I've always loved graphics, especially in video games. As a child, I was fascinated by how future graphics would look and how realistic they could become. Besides my personal love for graphics, it's a challenging field. The development of graphics involves interesting mathematical challenges (linear algebra) and requires efficient, optimized coding, often in fast "low-level" languages like C++.

## Game Development:
 Beyond the engine and its basic supported capabilities, I developed a scene/game - a kind of maze in a 3D world with impassable objects. I implemented the A* algorithm to find the shortest path between two points. The algorithm is an extension of Dijkstra's algorithm and is widely used in video games, such as finding a path between a player character and a target.

#### Challenges:
Implementing the algorithm was slightly challenging despite its simplicity. The algorithm searches a graph, but I didn't have nodes; I had a 3D world with infinite points. Therefore, I had to convert the world into nodes by defining squares on a map, with each point belonging to a specific square.

#### Optimization:
Deciding the size of the squares was critical. If a blocking object partially occupies a square, should the whole square be considered blocked? Reducing the size of each square increases accuracy but slows down the algorithm. For example, with a 64x64 grid (4,096 squares), reducing the square size by half increases the grid to 128x128 (16,000 squares), significantly impacting algorithm speed and data structure choice.


![an image from my engine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvu0boh76ibsofobqed8.jpg)



#### Debugging Tools:
OpenGL is a state machine, making it hard to debug or understand its current state. While it provides basic output for incorrect input, understanding why things don't appear on the screen as expected is challenging. Hence, I decided to develop visual debugging tools to display each square on the map, mark objects and their occupied squares, and visualize algorithm paths. This decision saved time by providing visual confirmation of what was happening on the screen.

## Video Demonstration:
The attached video demonstrates dynamic lighting, various backgrounds and textures, model loading, and a scene with a sofa, computer, and other elements. Towards the end, a penguin aims to reach a bench along the shortest path. The A* algorithm is visually represented, with red nodes indicating closed nodes and green nodes representing the shortest path. The algorithm operates instantly, but the animation is intentionally slowed down for clarity.

Note: I later updated the interface, so the video is from before the change. Therefore, the images of the engine are different.

{% embed https://youtu.be/4DB4dDj0CCI %}

I will be publishing more of my journey, so you are welcome to follow me at https://x.com/yuvSouce.