More OpenGL: Getting Advanced!

Languages/Frameworks: C,C++ and OpenGL

Posted on October 27, 2021 · 4 mins read

Overview

Over the summer I did another dive into OpenGL. Graphics still fascinate me, especially seeing the beautiful effects that can be purely mathematically generated.

I learned some more advanced features this time around, including making algorithms for generating all sorts of shapes/meshes, lighting, and instancing (a way to render many of a similar graphic primitive in parallel)!

Getting Some 3D Meshes

I first had to relearn the basics of OpenGL (making 2D triangles, adding colors, rendering lines and points, etc.), but eventually (or maybe a few eventuallies) I was able to generate some points with math, connect those points together correctly, add some color, and do some movement of the generated mesh.

Here's what some of those looked like:


Some hill like surface generated with sines, cosines, and color determined by the height of the suface (red towards the bottom and darker towards the top).


Similiar kind of thing, just a different color scheme.

Adding Some Lighting

Next I added some Phong Lighting, which basically light objects based off how a light source hits an object, the object's natural luminescense.
Cubes moving around, acting as light sources, their color adds together on the white surface.

As a few final demos for lighting I:

  • generated some hills using Perlin Noise (a type of random number system where numbers next to each other have some wave-like correllation)
  • and added camera control, so you can use the keyboard to move around the world


Hills, lighting, and camera control.


Extra smooth hills (extra poor video quality!).

Instancing (Many Blades of Grass)

One final topic I looked at was instancing, which is the ability to efficiently draw many of the same graphics primitives, such as grass. This is done by giving the GPU some mesh/item you want to draw, and instruction for how and where you want to draw some number of that item.

I generated a little map and put as many blades of grass as my integrated graphics card could seem to handle, and voila, we have an FPS (sort of?).


Kudos for reading till the end ;)