Documentation
¶
Overview ¶
Package render provides optimized software asterization routines using edge function rasterization. Uses incremental updates to avoid recomputing barycentric coordinates per pixel.
Index ¶
- Constants
- func RGB(r, g, b uint8) color.RGBA
- func RGBA(r, g, b, a uint8) color.RGBA
- type AABB
- type BoundedMeshRenderer
- type Camera
- func (c *Camera) Forward() math3d.Vec3
- func (c *Camera) GetFrustum() Frustum
- func (c *Camera) LookAt(target math3d.Vec3)
- func (c *Camera) MoveForward(distance float64)
- func (c *Camera) MoveRight(distance float64)
- func (c *Camera) MoveUp(distance float64)
- func (c *Camera) ProjectionMatrix() math3d.Mat4
- func (c *Camera) Right() math3d.Vec3
- func (c *Camera) Rotate(deltaPitch, deltaYaw, deltaRoll float64)
- func (c *Camera) SetAspectRatio(aspect float64)
- func (c *Camera) SetClipPlanes(near, far float64)
- func (c *Camera) SetFOV(fov float64)
- func (c *Camera) SetPosition(pos math3d.Vec3)
- func (c *Camera) SetRotation(pitch, yaw, roll float64)
- func (c *Camera) Up() math3d.Vec3
- func (c *Camera) ViewMatrix() math3d.Mat4
- func (c *Camera) ViewProjectionMatrix() math3d.Mat4
- func (c *Camera) WorldToScreen(worldPos math3d.Vec3, screenWidth, screenHeight int) (x, y, depth float64, visible bool)
- type Color
- type CullingStats
- type FilterMode
- type Framebuffer
- func (fb *Framebuffer) Clear()
- func (fb *Framebuffer) DrawLine(x0, y0, x1, y1 int, c color.RGBA)
- func (fb *Framebuffer) DrawRect(x, y, w, h int, c color.RGBA)
- func (fb *Framebuffer) DrawRectOutline(x, y, w, h int, c color.RGBA)
- func (fb *Framebuffer) GetPixel(x, y int) color.RGBA
- func (fb *Framebuffer) Resize(width, height int)
- func (fb *Framebuffer) SavePNG(path string) error
- func (fb *Framebuffer) SetPixel(x, y int, c color.RGBA)
- func (fb *Framebuffer) ToImage() *image.RGBA
- type Frustum
- type MeshRenderer
- type Plane
- type Rasterizer
- func (r *Rasterizer) ClearDepth()
- func (r *Rasterizer) DrawCube(center math3d.Vec3, size float64, color Color, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawMesh(mesh MeshRenderer, transform math3d.Mat4, color Color, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawMeshCulled(mesh MeshRenderer, transform math3d.Mat4, localBounds AABB, color Color, ...) bool
- func (r *Rasterizer) DrawMeshGouraud(mesh MeshRenderer, transform math3d.Mat4, color Color, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawMeshGouraudCulled(mesh MeshRenderer, transform math3d.Mat4, localBounds AABB, color Color, ...) bool
- func (r *Rasterizer) DrawMeshGouraudOpt(mesh MeshRenderer, transform math3d.Mat4, color Color, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawMeshTextured(mesh MeshRenderer, transform math3d.Mat4, tex *Texture, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawMeshTexturedCulled(mesh MeshRenderer, transform math3d.Mat4, localBounds AABB, tex *Texture, ...) bool
- func (r *Rasterizer) DrawMeshTexturedGouraud(mesh MeshRenderer, transform math3d.Mat4, tex *Texture, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawMeshTexturedGouraudCulled(mesh MeshRenderer, transform math3d.Mat4, localBounds AABB, tex *Texture, ...) bool
- func (r *Rasterizer) DrawMeshTexturedOpt(mesh MeshRenderer, transform math3d.Mat4, tex *Texture, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawMeshWireframe(mesh MeshRenderer, transform math3d.Mat4, color Color)
- func (r *Rasterizer) DrawQuad(v0, v1, v2, v3 math3d.Vec3, color Color)
- func (r *Rasterizer) DrawTexturedCube(transform math3d.Mat4, size float64, tex *Texture, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawTransformedCube(transform math3d.Mat4, size float64, color Color, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawTransformedCubeGouraud(transform math3d.Mat4, size float64, color Color, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawTriangle(tri Triangle)
- func (r *Rasterizer) DrawTriangleFlat(v0, v1, v2 math3d.Vec3, color Color)
- func (r *Rasterizer) DrawTriangleGouraud(tri Triangle, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawTriangleGouraudOpt(tri Triangle, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawTriangleLit(v0, v1, v2 math3d.Vec3, baseColor Color, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawTriangleTextured(tri Triangle, tex *Texture, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawTriangleTexturedGouraud(tri Triangle, tex *Texture, lightDir math3d.Vec3)
- func (r *Rasterizer) DrawTriangleTexturedOpt(tri Triangle, tex *Texture, lightDir math3d.Vec3)
- func (r *Rasterizer) GetFrustum() Frustum
- func (r *Rasterizer) Height() int
- func (r *Rasterizer) InvalidateFrustum()
- func (r *Rasterizer) IsVisible(worldBounds AABB) bool
- func (r *Rasterizer) IsVisibleTransformed(localBounds AABB, transform math3d.Mat4) bool
- func (r *Rasterizer) ResetCullingStats()
- func (r *Rasterizer) Resize()
- func (r *Rasterizer) UpdateFrustum()
- func (r *Rasterizer) Width() int
- type Texture
- type Triangle
- type Vertex
- type Wireframe
- func (w *Wireframe) DrawAxes(length float64)
- func (w *Wireframe) DrawCube(center math3d.Vec3, size float64, color Color)
- func (w *Wireframe) DrawGrid(size, step float64, color Color)
- func (w *Wireframe) DrawLine3D(p1, p2 math3d.Vec3, color Color)
- func (w *Wireframe) DrawPoint(pos math3d.Vec3, size float64, color Color)
- func (w *Wireframe) DrawTransformedCube(transform math3d.Mat4, size float64, color Color)
- type WrapMode
Constants ¶
const ( FrustumLeft = iota FrustumRight FrustumBottom FrustumTop FrustumNear FrustumFar )
FrustumPlane indices for clarity.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AABB ¶
AABB represents an axis-aligned bounding box.
func TransformAABB ¶
TransformAABB transforms an AABB by a matrix and returns the new bounds. This is a convenience function wrapping AABB.Transform.
func (AABB) ContainsPoint ¶
ContainsPoint returns true if the point is inside the AABB.
type BoundedMeshRenderer ¶
type BoundedMeshRenderer interface {
MeshRenderer
GetBounds() (minV, maxV math3d.Vec3)
}
BoundedMeshRenderer extends MeshRenderer with bounding box support for frustum culling.
type Camera ¶
type Camera struct {
// Position in world space
Position math3d.Vec3
// Orientation (Euler angles in radians)
Pitch float64 // Rotation around X axis (look up/down)
Yaw float64 // Rotation around Y axis (look left/right)
Roll float64 // Rotation around Z axis (tilt)
// Projection parameters
FOV float64 // Vertical field of view in radians
AspectRatio float64 // Width / Height
Near float64 // Near clipping plane
Far float64 // Far clipping plane
// contains filtered or unexported fields
}
Camera represents a 3D camera with position and orientation.
func (*Camera) GetFrustum ¶
GetFrustum returns the current view frustum from the camera.
func (*Camera) MoveForward ¶
MoveForward moves the camera forward (or backward if negative).
func (*Camera) ProjectionMatrix ¶
ProjectionMatrix returns the projection matrix.
func (*Camera) SetAspectRatio ¶
SetAspectRatio sets the aspect ratio.
func (*Camera) SetClipPlanes ¶
SetClipPlanes sets the near and far clipping planes.
func (*Camera) SetPosition ¶
SetPosition sets the camera position.
func (*Camera) SetRotation ¶
SetRotation sets the camera rotation (pitch, yaw, roll in radians).
func (*Camera) ViewMatrix ¶
ViewMatrix returns the view matrix.
func (*Camera) ViewProjectionMatrix ¶
ViewProjectionMatrix returns the combined view-projection matrix.
type Color ¶
Color is an alias for color.RGBA for convenience.
func ModulateColor ¶
ModulateColor modulates one color by another (texture * vertex color).
func MultiplyColor ¶
MultiplyColor multiplies a color by a scalar (for lighting).
type CullingStats ¶
type CullingStats struct {
MeshesTested int // Total meshes tested for culling
MeshesCulled int // Meshes culled (not rendered)
MeshesDrawn int // Meshes that passed culling
}
CullingStats tracks frustum culling performance.
type FilterMode ¶
type FilterMode int
FilterMode determines how texture sampling is performed.
const ( FilterNearest FilterMode = iota // Nearest-neighbor (pixelated) FilterBilinear // Bilinear interpolation (smooth) )
type Framebuffer ¶
type Framebuffer struct {
Width int // Width in "pixels" (same as terminal columns)
Height int // Height in "pixels" (2x terminal rows due to half-blocks)
Pixels []color.RGBA // Row-major pixel data
BG color.RGBA // Background color for transparent pixels
}
Framebuffer is a 2D array of pixels that can be rendered to the terminal. We use double vertical resolution by using half-block characters (▀▄).
func NewFramebuffer ¶
func NewFramebuffer(width, height int) *Framebuffer
NewFramebuffer creates a new framebuffer with the given dimensions. Height should be 2x the desired terminal rows for half-block rendering.
func (*Framebuffer) Clear ¶
func (fb *Framebuffer) Clear()
Clear fills the framebuffer with a solid color.
func (*Framebuffer) DrawLine ¶
func (fb *Framebuffer) DrawLine(x0, y0, x1, y1 int, c color.RGBA)
DrawLine draws a line from (x0, y0) to (x1, y1) using Bresenham's algorithm.
func (*Framebuffer) DrawRect ¶
func (fb *Framebuffer) DrawRect(x, y, w, h int, c color.RGBA)
DrawRect draws a filled rectangle.
func (*Framebuffer) DrawRectOutline ¶
func (fb *Framebuffer) DrawRectOutline(x, y, w, h int, c color.RGBA)
DrawRectOutline draws a rectangle outline.
func (*Framebuffer) GetPixel ¶
func (fb *Framebuffer) GetPixel(x, y int) color.RGBA
GetPixel returns the color at (x, y). Returns transparent black if out of bounds.
func (*Framebuffer) Resize ¶
func (fb *Framebuffer) Resize(width, height int)
Resize changes the size of the framebuffer, reallocating pixel data.
func (*Framebuffer) SavePNG ¶
func (fb *Framebuffer) SavePNG(path string) error
SavePNG saves the framebuffer as a PNG file.
func (*Framebuffer) SetPixel ¶
func (fb *Framebuffer) SetPixel(x, y int, c color.RGBA)
SetPixel sets a pixel at (x, y) to the given color. Bounds checking is performed.
func (*Framebuffer) ToImage ¶
func (fb *Framebuffer) ToImage() *image.RGBA
ToImage converts the framebuffer to a standard Go image.RGBA.
type Frustum ¶
type Frustum struct {
Planes [6]Plane
}
Frustum represents the 6 planes of a view frustum. Planes are ordered: Left, Right, Bottom, Top, Near, Far. Each plane's normal points inward (toward the center of the frustum).
func ExtractFrustum ¶
ExtractFrustum is an alias for NewFrustumFromMatrix for API consistency.
func NewFrustumFromMatrix ¶
NewFrustumFromMatrix extracts frustum planes from a view-projection matrix. Uses the Gribb/Hartmann method for extracting planes from the combined matrix. The resulting planes have normals pointing inward.
func (Frustum) ContainsAABB ¶
ContainsAABB tests if the AABB is completely inside the frustum. Returns true only if all 8 corners are inside all 6 planes.
func (Frustum) ContainsPoint ¶
ContainsPoint tests if a point is inside the frustum.
func (Frustum) IntersectAABB ¶
IntersectAABB tests if the AABB intersects or is inside the frustum. Returns true if any part of the AABB is visible. Uses the "positive vertex" optimization for faster rejection.
func (Frustum) IntersectsFrustum ¶
IntersectsFrustum is an alias for IntersectAABB for API consistency.
type MeshRenderer ¶
type MeshRenderer interface {
VertexCount() int
TriangleCount() int
GetVertex(i int) (pos, normal math3d.Vec3, uv math3d.Vec2)
GetFace(i int) [3]int
}
MeshRenderer is imported from models to avoid circular deps. This interface allows drawing meshes without importing the models package.
type Plane ¶
Plane represents a plane in 3D space using the equation: Ax + By + Cz + D = 0 where (A, B, C) is the normal and D is the distance from origin.
func (*Plane) DistanceToPoint ¶
DistanceToPoint returns the signed distance from the plane to a point. Positive = in front (same side as normal), negative = behind.
type Rasterizer ¶
type Rasterizer struct {
CullingStats CullingStats // Statistics for debugging/benchmarking
DisableBackfaceCulling bool // If true, render both sides of triangles
// contains filtered or unexported fields
}
Rasterizer handles software triangle rasterization.
func NewRasterizer ¶
func NewRasterizer(camera *Camera, fb *Framebuffer) *Rasterizer
NewRasterizer creates a new rasterizer.
func (*Rasterizer) ClearDepth ¶
func (r *Rasterizer) ClearDepth()
ClearDepth clears the Z-buffer (call before each frame).
func (*Rasterizer) DrawMesh ¶
func (r *Rasterizer) DrawMesh(mesh MeshRenderer, transform math3d.Mat4, color Color, lightDir math3d.Vec3)
DrawMesh renders a mesh with the given transform and color. Automatically performs frustum culling if the mesh provides bounds.
func (*Rasterizer) DrawMeshCulled ¶
func (r *Rasterizer) DrawMeshCulled( mesh MeshRenderer, transform math3d.Mat4, localBounds AABB, color Color, lightDir math3d.Vec3, ) bool
DrawMeshCulled renders a mesh with frustum culling. Returns true if the mesh was drawn, false if it was culled.
func (*Rasterizer) DrawMeshGouraud ¶
func (r *Rasterizer) DrawMeshGouraud(mesh MeshRenderer, transform math3d.Mat4, color Color, lightDir math3d.Vec3)
DrawMeshGouraud renders a mesh with Gouraud shading (per-vertex lighting). This produces smoother shading than flat shading by interpolating lighting across triangles. Automatically performs frustum culling if the mesh provides bounds.
func (*Rasterizer) DrawMeshGouraudCulled ¶
func (r *Rasterizer) DrawMeshGouraudCulled( mesh MeshRenderer, transform math3d.Mat4, localBounds AABB, color Color, lightDir math3d.Vec3, ) bool
DrawMeshGouraudCulled renders a mesh with Gouraud shading, with frustum culling. localBounds should be the mesh's local-space bounding box (e.g., mesh.BoundsMin/Max). Returns true if the mesh was drawn, false if it was culled.
func (*Rasterizer) DrawMeshGouraudOpt ¶
func (r *Rasterizer) DrawMeshGouraudOpt(mesh MeshRenderer, transform math3d.Mat4, color Color, lightDir math3d.Vec3)
DrawMeshGouraudOpt renders a mesh with optimized Gouraud shading.
func (*Rasterizer) DrawMeshTextured ¶
func (r *Rasterizer) DrawMeshTextured(mesh MeshRenderer, transform math3d.Mat4, tex *Texture, lightDir math3d.Vec3)
DrawMeshTextured renders a mesh with texture mapping. Automatically performs frustum culling if the mesh provides bounds.
func (*Rasterizer) DrawMeshTexturedCulled ¶
func (r *Rasterizer) DrawMeshTexturedCulled( mesh MeshRenderer, transform math3d.Mat4, localBounds AABB, tex *Texture, lightDir math3d.Vec3, ) bool
DrawMeshTexturedCulled renders a textured mesh with frustum culling. Returns true if the mesh was drawn, false if it was culled.
func (*Rasterizer) DrawMeshTexturedGouraud ¶
func (r *Rasterizer) DrawMeshTexturedGouraud(mesh MeshRenderer, transform math3d.Mat4, tex *Texture, lightDir math3d.Vec3)
DrawMeshTexturedGouraud renders a mesh with texture mapping and Gouraud shading. Combines perspective-correct texture mapping with smooth per-vertex lighting. Automatically performs frustum culling if the mesh provides bounds.
func (*Rasterizer) DrawMeshTexturedGouraudCulled ¶
func (r *Rasterizer) DrawMeshTexturedGouraudCulled( mesh MeshRenderer, transform math3d.Mat4, localBounds AABB, tex *Texture, lightDir math3d.Vec3, ) bool
DrawMeshTexturedGouraudCulled renders a textured mesh with Gouraud shading, with frustum culling. localBounds should be the mesh's local-space bounding box (e.g., mesh.BoundsMin/Max). Returns true if the mesh was drawn, false if it was culled.
func (*Rasterizer) DrawMeshTexturedOpt ¶
func (r *Rasterizer) DrawMeshTexturedOpt(mesh MeshRenderer, transform math3d.Mat4, tex *Texture, lightDir math3d.Vec3)
DrawMeshTexturedOpt renders a textured mesh with optimized rasterization.
func (*Rasterizer) DrawMeshWireframe ¶
func (r *Rasterizer) DrawMeshWireframe(mesh MeshRenderer, transform math3d.Mat4, color Color)
DrawMeshWireframe renders a mesh as wireframe. Automatically performs frustum culling if the mesh provides bounds.
func (*Rasterizer) DrawQuad ¶
func (r *Rasterizer) DrawQuad(v0, v1, v2, v3 math3d.Vec3, color Color)
DrawQuad draws a quad as two triangles.
func (*Rasterizer) DrawTexturedCube ¶
func (r *Rasterizer) DrawTexturedCube(transform math3d.Mat4, size float64, tex *Texture, lightDir math3d.Vec3)
DrawTexturedCube draws a cube with texture mapping.
func (*Rasterizer) DrawTransformedCube ¶
func (r *Rasterizer) DrawTransformedCube(transform math3d.Mat4, size float64, color Color, lightDir math3d.Vec3)
DrawTransformedCube draws a cube with a transformation matrix.
func (*Rasterizer) DrawTransformedCubeGouraud ¶
func (r *Rasterizer) DrawTransformedCubeGouraud(transform math3d.Mat4, size float64, color Color, lightDir math3d.Vec3)
DrawTransformedCubeGouraud draws a cube with Gouraud shading (smooth corners). Uses per-vertex normals averaged at corners for smoother lighting transitions.
func (*Rasterizer) DrawTriangle ¶
func (r *Rasterizer) DrawTriangle(tri Triangle)
DrawTriangle rasterizes a single triangle.
func (*Rasterizer) DrawTriangleFlat ¶
func (r *Rasterizer) DrawTriangleFlat(v0, v1, v2 math3d.Vec3, color Color)
DrawTriangleFlat draws a triangle with flat shading (single color).
func (*Rasterizer) DrawTriangleGouraud ¶
func (r *Rasterizer) DrawTriangleGouraud(tri Triangle, lightDir math3d.Vec3)
DrawTriangleGouraud rasterizes a triangle with Gouraud shading (per-vertex lighting). Lighting is calculated at each vertex and interpolated across the triangle.
func (*Rasterizer) DrawTriangleGouraudOpt ¶
func (r *Rasterizer) DrawTriangleGouraudOpt(tri Triangle, lightDir math3d.Vec3)
DrawTriangleGouraudOpt is an optimized version using edge functions with incremental updates.
func (*Rasterizer) DrawTriangleLit ¶
DrawTriangleLit draws a triangle with simple directional lighting.
func (*Rasterizer) DrawTriangleTextured ¶
func (r *Rasterizer) DrawTriangleTextured(tri Triangle, tex *Texture, lightDir math3d.Vec3)
DrawTriangleTextured rasterizes a textured triangle with perspective-correct UV interpolation.
func (*Rasterizer) DrawTriangleTexturedGouraud ¶
func (r *Rasterizer) DrawTriangleTexturedGouraud(tri Triangle, tex *Texture, lightDir math3d.Vec3)
DrawTriangleTexturedGouraud rasterizes a textured triangle with Gouraud shading. Per-vertex lighting is calculated and interpolated, then modulated with texture.
func (*Rasterizer) DrawTriangleTexturedOpt ¶
func (r *Rasterizer) DrawTriangleTexturedOpt(tri Triangle, tex *Texture, lightDir math3d.Vec3)
DrawTriangleTexturedOpt is an optimized textured triangle rasterizer with Gouraud shading.
func (*Rasterizer) GetFrustum ¶
func (r *Rasterizer) GetFrustum() Frustum
GetFrustum returns the current frustum (updating if needed).
func (*Rasterizer) Height ¶
func (r *Rasterizer) Height() int
Height returns the framebuffer height.
func (*Rasterizer) InvalidateFrustum ¶
func (r *Rasterizer) InvalidateFrustum()
InvalidateFrustum marks the frustum as needing recalculation. Call this when the camera moves or rotates.
func (*Rasterizer) IsVisible ¶
func (r *Rasterizer) IsVisible(worldBounds AABB) bool
IsVisible tests if a world-space AABB is visible in the frustum.
func (*Rasterizer) IsVisibleTransformed ¶
func (r *Rasterizer) IsVisibleTransformed(localBounds AABB, transform math3d.Mat4) bool
IsVisibleTransformed tests if a local-space AABB is visible after transformation.
func (*Rasterizer) ResetCullingStats ¶
func (r *Rasterizer) ResetCullingStats()
ResetCullingStats resets the culling statistics (call once per frame).
func (*Rasterizer) Resize ¶
func (r *Rasterizer) Resize()
Resize resizes the rasterizer's buffer to match the framebuffer.
func (*Rasterizer) UpdateFrustum ¶
func (r *Rasterizer) UpdateFrustum()
UpdateFrustum recalculates the frustum planes from the camera.
type Texture ¶
type Texture struct {
Width int
Height int
Pixels []Color // Row-major pixel data
WrapU WrapMode // Horizontal wrap mode
WrapV WrapMode // Vertical wrap mode
FilterMode FilterMode // Sampling filter mode
}
Texture holds a 2D image for texture mapping.
func LoadTexture ¶
LoadTexture loads a texture from an image file.
func NewCheckerTexture ¶
NewCheckerTexture creates a procedural checkerboard texture.
func NewGradientTexture ¶
NewGradientTexture creates a horizontal gradient texture.
func NewTexture ¶
NewTexture creates an empty texture with the given dimensions.
func TextureFromImage ¶
TextureFromImage creates a texture from an image.Image.
type Triangle ¶
type Triangle struct {
V [3]Vertex
}
Triangle represents a triangle to be rasterized.
type Vertex ¶
type Vertex struct {
Position math3d.Vec3 // World position
Normal math3d.Vec3 // Normal vector (for lighting)
UV math3d.Vec2 // Texture coordinates
Color Color // Vertex color
}
Vertex represents a vertex with all attributes needed for rasterization.
type Wireframe ¶
type Wireframe struct {
// contains filtered or unexported fields
}
Wireframe renders 3D wireframe objects.
func NewWireframe ¶
func NewWireframe(camera *Camera, fb *Framebuffer) *Wireframe
NewWireframe creates a new wireframe renderer.
func (*Wireframe) DrawLine3D ¶
DrawLine3D draws a line in 3D space.