Skip to content
Saarrthi Rana

School project / Summer 2024

OpenGL terrain and a jellyfish

Assignments from Game Graphics 2 and Game Physics 2, in C++ and OpenGL.

Role
Solo
Team
Game Graphics 2 and Game Physics 2
When
Summer 2024
C++OpenGLShadersPhysics
Videos
WASD moves the head and the tentacles swing along behind it.
Dragging the mouse turns the view. The ground further away fades into the blue fog.
What it is

My first year graphics and physics work, done as weekly assignments.

For graphics I made an underwater scene where bumpy ground fades into fog in the distance. For physics I made a jellyfish whose tentacles are chains of small balls, and a ball rolling on a tilting table.

What I did
  1. Made the ground bumpy with a height map, with the detail level set from the keyboard.
  2. Added fog, so the ground further away fades into deep blue water.
  3. Built the jellyfish out of a head and ten tentacles. Each tentacle is a chain of ten small balls that swings behind it.
  4. Added camera controls to the jellyfish scene, including a smooth glide between two saved views.
  5. Wrote ball to ball collisions, and a table you tilt with WASD that the ball rolls down.
How it works

Bumpy ground

The graphics card cuts the flat ground into lots of small triangles, then each new point reads the height map and moves up. A and S change how many triangles it makes.

Tentacles

Every frame, each ball gets gravity and drag first, then it's pulled back so it stays one unit from the ball above it. Doing it in that order stops the chain from stretching apart.

Code

Code I wrote

Short parts of my own code from this project. I left out anything the course gave us.

Jellyfish tentacles

Scene3p.cpp, C++ and OpenGL

Forces first, then keeping each ball attached. This file isn't in the starter.
void Scene3p::Update(const float deltaTime) {
    Vec3 gravityAccel(0, -9.8, 0); // Gravity acceleration vector
    float c = 2.5f; // Drag coefficient
    // ...
    // Apply gravity and drag to tentacle spheres
    for (size_t anchorIndex = 0; anchorIndex < anchors.size(); ++anchorIndex) {
        for (int sphereIndex = 0; sphereIndex < numSpheresPerAnchor; ++sphereIndex) {
            int index = anchorIndex * numSpheresPerAnchor + sphereIndex;

            Vec3 gravityForce = tentacleSpheres[index]->mass * gravityAccel; // Calculate gravity force
            Vec3 dragForce = -c * tentacleSpheres[index]->vel; // Calculate drag force (laminar flow)

            // Switch to turbulent drag if velocity is high
            if (VMath::mag(tentacleSpheres[index]->vel) > 1.0f) {
                dragForce = -c * tentacleSpheres[index]->vel * VMath::mag(tentacleSpheres[index]->vel); // Calculate drag force (turbulent flow)
            }

            tentacleSpheres[index]->ApplyForce(gravityForce + dragForce); // Apply combined forces to the tentacle sphere
            tentacleSpheres[index]->UpdateVel(deltaTime); // Update velocity of the tentacle sphere
        }
    }

    // Enforce rod constraints and update positions
    for (size_t anchorIndex = 0; anchorIndex < anchors.size(); ++anchorIndex) {
        for (int sphereIndex = 0; sphereIndex < numSpheresPerAnchor; ++sphereIndex) {
            int index = anchorIndex * numSpheresPerAnchor + sphereIndex;

            // Apply rod constraint
            if (sphereIndex > 0) {
                int aboveIndex = index - 1;
                tentacleSpheres[index]->RodConstraint(deltaTime, tentacleSpheres[aboveIndex]->pos, spacing); // Apply rod constraint to keep tentacle sphere at a fixed distance from the sphere above
            }
            else {
                tentacleSpheres[index]->RodConstraint(deltaTime, anchors[anchorIndex]->pos, spacing); // Apply rod constraint to keep tentacle sphere at a fixed distance from the anchor
            }

            tentacleSpheres[index]->UpdatePos(deltaTime); // Update position of the tentacle sphere

            // Update model matrix for rendering
            modelMatricesTentacleSpheres[index] = MMath::translate(tentacleSpheres[index]->pos) * MMath::scale(tentacleSpheres[index]->radius, tentacleSpheres[index]->radius, tentacleSpheres[index]->radius);
        }
    }

Height and fog in the shader

TessillationEvaluator.glsl, C++ and OpenGL

Only the lines I added to my earlier shader.
out vec3 eyeDirFromEval;
out vec3 lightFromEval;
out float fogValFromEval;
// ...
const float fogDensity = 0.02;
const float fogGradient = 1.5;
void main() {
    // ...
    vec4 height = vec4(texture(textureData,uvCoordFromEval));
    position.z =  height.r;
    
    vec3 vertPos = vec3(viewMatrix * modelMatrix * position);
    vec3 vertDir = normalize(vertPos);
    eyeDirFromEval = -vertDir;
    lightFromEval = normalize(lightPos - vertPos);

    float distance = length(vertPos);
    fogValFromEval = clamp(exp(-pow((distance * fogDensity), fogGradient)), 0.0, 1.0);

Built on our instructor's OpenGL framework. I made the scenes, the shader changes and the physics. To record these in 2026 I added the SDL2 files the old builds were missing.

Next school project

Goblin Escape

A team game for our Artificial Intelligence course, made in Unreal Engine 5.6.