Back to home

kronk3d

A super fast and modern 3D hardware rasterizer.

Overview

kronk3d is a small C++20 3D rasterization library with two interchangeable backends behind a single interface. The software backend is a multithreaded, tile-based CPU rasterizer that renders into memory and needs no GPU or window. The OpenGL 3.3 backend renders into the context your application already has.

Both produce the same image: the shading code is mirrored, and tests compare their outputs pixel by pixel. It covers Blinn-Phong lighting, cascaded shadows, fog, skyboxes, transparency, anti-aliasing (FXAA, SSAA), OBJ and glTF loading, and skeletal animation.

Examples

Quick start
#include <kronk3d/Kronk3d.hpp>

// Assets
auto model = k3::ObjLoader::load("assets/spot/spot.obj");     // k3::Result<k3::Model>
if (!model)
    return std::cerr << model.error() << '\n', 1;

// Backend. Software needs nothing. OpenGL needs a 3.3 core context to be current.
auto rasterizer = k3::createRasterizer(k3::Backend::OpenGL, {
    .width = 800, .height = 600,
    .glLoader = [](const char* name) { return sf::Context::getFunction(name); },
});
if (!rasterizer)
    return std::cerr << rasterizer.error() << '\n', 1;

// Scene
k3::Camera camera;
camera.position = {0.f, 1.f, 4.f};
camera.lookAt({0.f, 0.f, 0.f});

k3::Environment environment;
environment.lights.push_back(k3::Light::directional({-0.5f, -1.f, -0.6f}));
environment.lights.back().castShadows = true;

// Frame
(*rasterizer)->beginFrame(camera, environment);
(*rasterizer)->draw(*model, k3::Math::Matrix4::rotateZX(angle));
(*rasterizer)->endFrame();

k3::Image frame = (*rasterizer)->readPixels();   // sRGB RGBA8, top row first
frame.savePNG("frame.png");
An animated character (glTF)
auto loaded = k3::GltfLoader::load("assets/hero.glb");          // k3::Result<k3::AnimatedModel>
if (!loaded)
    return std::cerr << loaded.error() << '\n', 1;

k3::Animator hero(std::make_shared<const k3::AnimatedModel>(std::move(*loaded)));
hero.play("Walk");                                               // Loops by default

// Each frame
if (jumpPressed)
    hero.play("Jump", {.loop = false, .fade = 0.1f});            // Crossfades from the current clip
else if (hero.clip() == "Jump" && hero.finished())
    hero.play("Walk");
hero.update(deltaSeconds);

rasterizer->beginFrame(camera, environment);
hero.draw(*rasterizer, k3::Math::Matrix4::translate(position));  // Place the character in the world
rasterizer->endFrame();