School project / Fall 2025
Vulkan renderer
Three GPU Programming assignments in C++ and Vulkan.
In Vulkan, each model needs its own setup before it can be drawn, and finding what the mouse is pointing at has to be worked out with maths.
Over three assignments I added three coloured lights, gave every model its own setup so I could draw as many as I wanted, and made it possible to click a model to select it.
- Lit the scene with red, green and purple lights. The starter had one light.
- Wrote an Actor class, so each model (Mario, a skull and a ball) sets itself up and draws itself.
- Wrote the camera code, so the picture doesn't stretch when you resize the window.
- Added clicking on models. A click becomes a line from the camera into the scene, and the closest model it hits is selected.
- Started a second way of picking using colours drawn off screen, but didn't finish it.
Clicking on a model
The mouse position is turned back through the camera into a line in the 3D world. I treat each model as a sphere and pick the closest one the line hits.
One Actor per model
Each Actor holds its own mesh, texture and position, so adding a model takes one more line of code.
Code I wrote
Short parts of my own code from this project. I left out anything the course gave us.
From a mouse click to a line in the world
Scene0.cpp, C++ and Vulkan
void Scene0::DoPicking(int mouseX, int mouseY)
{
// ...
// 1) Screen -> NDC
float x = (2.0f * static_cast<float>(mouseX) / static_cast<float>(width)) - 1.0f;
float y = 1.0f - (2.0f * static_cast<float>(mouseY) / static_cast<float>(height));
Vec4 rayClip(x, y, -1.0f, 1.0f);
// 2) Clip -> Eye
Matrix4 invProj = MMath::inverse(camera->camerag.projectionMatrix);
Vec4 rayEye4 = invProj * rayClip;
rayEye4.z = -1.0f;
rayEye4.w = 0.0f;
// 3) Eye -> World
Matrix4 invView = MMath::inverse(camera->camerag.viewMatrix);
Vec4 rayWorld4 = invView * rayEye4;
Vec3 rayDir = VMath::normalize(Vec3(rayWorld4.x, rayWorld4.y, rayWorld4.z));
// camera position = inverse(view) * (0,0,0,1)
Vec4 camPos4 = invView * Vec4(0.0f, 0.0f, 0.0f, 1.0f);
Vec3 rayOrigin(camPos4.x, camPos4.y, camPos4.z);
// ...
auto testActor = [&](Actor* a) {
if (!a) return;
float tHit = 0.0f;
if (RaySphereIntersect(rayOrigin, rayDir,
a->GetPosition(),
a->GetPickRadius(),
tHit))
{
if (!best.actor || tHit < best.t) {
best.actor = a;
best.t = tHit;
}
}
};
testActor(actor);
testActor(skull);
testActor(ball);
Line and sphere hit test
Scene0.cpp, C++ and Vulkan
namespace {
using namespace MATH;
bool RaySphereIntersect(const Vec3& rayOrigin,
const Vec3& rayDir,
const Vec3& center,
float radius,
float& tHit)
{
Vec3 oc = rayOrigin - center;
float a = VMath::dot(rayDir, rayDir);
float b = 2.0f * VMath::dot(oc, rayDir);
float c = VMath::dot(oc, oc) - radius * radius;
float discriminant = b * b - 4.0f * a * c;
if (discriminant < 0.0f) return false;
float sqrtD = sqrtf(discriminant);
float t0 = (-b - sqrtD) / (2.0f * a);
float t1 = (-b + sqrtD) / (2.0f * a);
// take the smallest positive t
float t = (t0 > 0.0f) ? t0 : t1;
if (t <= 0.0f) return false;
tHit = t;
return true;
}
}
Built on our instructor's Vulkan starter. I added the lights, the Actor class, the camera code and the picking.
Next school project
Grab and drag physics
Assignments for Game Physics 3, in C++ and OpenGL.