Documentation
¶
Overview ¶
Package soft3d is a minimal, dependency-free software 3D rasterizer.
It implements just enough linear algebra and triangle rasterization to render a rotating, flat-shaded, perspective-projected, z-buffered cube directly into a caller-supplied BGRA byte buffer. The buffer layout matches go-virtio/gpu's VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM: for a pixel (x, y) in a w×h image the byte offset is (y*w+x)*4, with
pix[off+0] = B pix[off+1] = G pix[off+2] = R pix[off+3] = A (always 0xFF, fully opaque)
soft3d imports nothing outside the Go standard library (in fact only math): it is pure math plus rasterization. It does NOT depend on go-virtio/common or go-virtio/gpu, so it can be reused anywhere a BGRA framebuffer is available.
Typical wiring against go-virtio/gpu (illustrative, not executed here):
vg, _ := gpu.OpenVirtioGPU(t); fb, _ := vg.SetupFramebuffer(0, 640, 480) soft3d.RenderCube(fb.Pix, 640, 480, angle); _ = fb.Flush()
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RenderCube ¶
RenderCube renders a rotating unit cube ([-1,1]^3) into the w×h BGRA buffer pix. The cube is rotated by RotateY(angleRad)·RotateX(angleRad*0.5), translated to z = -4 (camera at the origin looking down -Z), and perspective-projected. Visible faces are backface-culled (faces whose screen-space signed area indicates they point away from the camera are skipped) and flat-shaded by (face-normal · light) with a fixed light direction. The z-buffer is reset on every call.
Types ¶
type Color ¶
type Color struct {
R, G, B uint8
}
Color is an opaque RGB color; the alpha channel is always written as 0xFF.
type Mat4 ¶
type Mat4 [16]float64
Mat4 is a 4×4 matrix stored row-major: element (row r, column c) lives at index r*4+c. A vector is treated as a column and transformed as M·v, so Translate puts the translation in the last column (indices 3, 7, 11).
func Perspective ¶
Perspective returns a right-handed perspective projection matrix.
fovYRad is the vertical field of view in radians, aspect is width/height, and near/far are positive distances along -Z. Points in front of the camera have negative Z (the camera looks down -Z); after projection and the perspective divide by w (= -Z), such points map into the canonical view volume with -1 at the near plane and +1 at the far plane.