Abhijit Joshi

ENGINEER. PROGRAMMER. ARTIST.

OpenGL: Make your own 2D/3D graphics and animations

This page contains a mosaic of renderings and animations that you can explore to master the fundamentals of using OpenGL in your project.

For the newcomer to OpenGL, it is recommended that you progress in a step-by-step fashion (001, 002, 003 and so on). For the experienced OpenGL ninja, feel free to jump in directly to something you like.

Drawing Geometric Primitives - Lines, Triangles, Quads

001: Open GLFW Window

Source Code

002: Moving Triangle

Source Code

003: Zooming In and Out

Source Code

004: Moving Camera

Source Code

005: Using Vertex Arrays

Source Code

006: Using Vertex Arrays & Indices

Source Code

OpenGL Projects

I've collected some sample projects here that illustrate how to build more complex animations and simulations using ideas developed above. You will continue to learn more about OpenGL and also see how it can be used in some real-life applications.

007: STL Geometry Viewer

Source Code

Let's look at an OpenGL application that reads in a 3D geometry from an STL file and allows the user to rotate the geometry in real time using the arrow keys on the keyboard.

This was one of my motivations for learning OpenGL, apart from writing computer games.

Spend some time going through the code and you will learn how to

  1. store 3D geometry data in STL format.
  2. use Vertex Buffer Objects (VBOs).
  3. use Perspective Projection.

008: Rolling Without Slipping

Source Code

This OpenGL project simulates a circular disc that rolls without slipping on a larger disc. The question is this: If the smaller (rolling) disc has half the radius of the larger (stationary) disc, how many complete rotations of the smaller disc will occur by the time it completes one run around the bigger disc?

Play the movie to find out the answer. Then take a look at the source code.

009: Exploring the Mandelbrot Set

Source Code

The Mandelbrot set is, of course, one of the most famous fractals invented. You can essentially keep zooming in and discover a glorious world of endlessly wonderful geometric patterns.

The idea is to check all pixels in your display window for membership in the set and devise a practical criterion to distinguish points inside the set (black), points outside the set (blue) and points close to the border (smooth color gradient).

Because this is just a C++ code, I refer to this as the CPU-version. Later on, you will learn how to use CUDA and OpenCL to accelerate both the calculations that check for membership in the set and the graphics for rendering the image in the display window.

010: Driven Cavity Flow

Source Code

Flow inside a lid-driven cavity remains one of the most widely used benchmarks for validating new computational fluid dynamics (CFD) codes. This simulation uses the lattice Boltzmann method (LBM) for simulating high Reynolds number flow in a square cavity.

The core LBM algorithm is based on a single-relaxation-time approach that uses the Smagorinsky model for changing fluid viscosity as a function of the rate-of-strain.

In this tutorial, OpenGL is used for visualizing the flow field in real-time, while the code is running. Specifically, we plot the vorticity field to show rotating eddies:

  • red = clockwise eddy
  • blue = anti-clockwise eddy

This simulation was run using a 128 x 128 lattice for Re = 1,000,000.

Because the lattice is coarse compared to the pixel counts along X and Y, bilinear interpolation is used to find the vorticity magnitude at each pixel location, based on vorticity values at the 4 nearest lattice points.