Skip to content

Rasterizer3D

Index · Français

Description

CPU filled/wireframe triangle rasterizer with perspective projection, depth testing and near-plane clipping. Use it to draw meshes in a Framebuffer.

gaspardos::graphics::Rasterizer3D — bindings/cpp/raster3d.hpp, C++98.

Simple program example

#include "raster3d.hpp"

int main()
{
    gaspardos::Framebuffer            image(320, 240);
    gaspardos::graphics::Rasterizer3D renderer(image);
    gaspardos::graphics::Pose         pose;
    pose.rotation = gaspardos::graphics::Vec3(.4f, .6f, .1f);
    renderer.clear();
    renderer.draw(gaspardos::graphics::Shapes3D::cube(), pose, false, true);
    return image.pixel(160, 120) != 0x00201810 ? 0 : 1;
}

Building from Windows

From the repository root, save the program as build/api-examples/Rasterizer3D.cpp.

New-Item -ItemType Directory -Force build/api-examples | Out-Null
& .\build-x64-install\bin\x86_64-gaspardos-g++.exe -std=c++98 -O2 -Ibindings/cpp build/api-examples/Rasterizer3D.cpp bindings/cpp/raster3d.cpp -o build/api-examples/Rasterizer3D.elf
if ($LASTEXITCODE -ne 0) { throw "C++ build failed" }

Executable for GaspardOS. Presentation requires WINDOW_BLIT=146 in the kernel; -38 means the extension is absent. Source files.

Methods and public data

explicit Rasterizer3D(Framebuffer& target)

Non-owning reference; the framebuffer must outlive the rasterizer. Initialize depth and background. Noncopyable.

void clear(uint32_t color=0x00201810)

Fill the background and reset depth; call each frame and after framebuffer resize.

void draw(const Mesh& mesh, const Pose& pose=Pose(), bool wireframe=false, bool hollowVertices=true)

Rotate XYZ in radians then translate; camera faces +Z, near plane z=0.1, vertical field of view 60°. Filled triangles use flat shading and interpolated inverse depth; wireframe draws triangle edges. hollowVertices marks vertices with hollow radius-four circles without filling their centers; filled mode depth-tests occluded points. No antialiasing or textures.