# OPENGL

ROLE

Graphics Programmer

DESCRIPTION

A simple fly-through environment made from scratch in C++ with OpenGL.

## CONCEPT AND GOAL

For the graphics programming module we were tasked to create a simple world from scratch in OpenGL. My goal was to apply some layers of post processing in order to get a better understanding about the technology.

I also wanted to work with anti aliasing to understand the methodology behind it. People have always told me that it is not a complex algorithm so I wanted to work with it in order to gain some experience as well.

Since it was my first time working with OpenGL I was lacking in the basics of graphics programming. So understanding the basics was my first step in order to eventually work on anti aliasing and post processing.

In Unity I always use post processing in my projects since it can enhance your project significantly, however it are just modules that you have to add and tweak. It isn't code that you write so while I understand what it should do I don't understand how it actually works. Hence my curiosity to this.

## TECHNICAL PART

Anti aliasing is in theory quite simple. Each texel has 4 points. The model iterates through it in which the overlapping points calculate the opacity for that specific texel. Full opacity edges should look softer which makes the model itself looking more polished without the hard edges. Eventually I managed to get it partially working. I also tried to solve it, but it caused other issues.

My first few iterations had a lot of artifacts because I didn't have a full understanding about the algorithm behind it. This caused for example a blue dirt texture with a lot of weird artifacts with still hard texels that doesn't look smooth.

After a lot brainstorming I realized that my approach would never work. This is due to the textures already having a blend that is based on the height of the texture.

I want to try the anti aliasing logic on the height of the terrain by modifying the vertices to make it feel more smooth so that the height of a vertex would be closer to it's neighbours so that there wouldn't be pointy spots. The effect however was the opposite of what I intended.

The problem with this was that it calculated the original heights and not the modified heights that were based on the normal map. If it had 4 neighbours it didn't divide it properly by 4 in order to get a good average.

Eventually I tried to work with MSAA which also caused a similar issue. Reflecting on it afterwards made me think that it was also related to dividing by 4. Of course this is a bit more complex, but that was the culprit. Without it all the texels with MSAA were always pure white as an effect.

The second part is Post Processing. This is the part that I was most interested in. Post Processing makes your game a lot better with minimal effort... at least when you don't have to write the effects by yourself.

I did follow a lot tutorials in order to get a better understanding about this, mainly through the opengl tutorials.

In order to be satisfied with the post processing I focussed on bloom, camera distortion and chromatic abberation. My first iterations were a failure because I did not fully grasp that you had to "wrap" the effects around the model. After a healthy session of frustration and guides I managed to render images.

This gave an effect of bending the world around you based on a sine calculation. Through a cosine calculation I managed to duplicate the screens from various points around which I couldn't fully grasp why it resulted in this effect. I was expecting it to bend in a different manner compared to the sine calculation.

Eventually I thought of using the arc cosine in order to get a proper bending effect and this worked! This was a massive motivation boost for me because A, it proved that I am understanding the mathematics part and B I managed to get close to the effect I wanted.

In the end I used the atan in order to get the exact effect that I envisioned.

Lastly there is also a reflection system for the water. This had some artifacts which caused the white lines. I think that this is related to the offset for the wave effect in the water where certain pixels get a too high value causing the texels to be solid white.

As interesting as this was I don't think that I enjoy writing an entire rendering engine from scratch. The post processing bit was very satisfying though.

## PROGRESS

###### The first iteration with AA (texture wise)

###### Iteration with AA (vertex wise)

###### A working iteration with MSAA

###### My first attempt at post processing

###### Working with the sine related calculations

###### The effect of arc cosine

###### Final iteration with all its effects

## SOURCES

OpenGL was completely new for me and I would not get anywhere without proper tutorials and online help.

The youtuber and my teacher XeartV.

TheCherno (mainly for the ray tracing part, did not use it in the end though).

Digital Foundry (about anti-aliasing).

The OpenGL tutorial website.

Stack for bugs related research.

Reddit for post processing related issues.

The potential MSAA solution.