Creating a 3D terrain generator with a movable camera in Go (Golang) without using any 3D graphics libraries is a bold and challenging task—especially since we'll need to implement everything from scratch, including rendering, perspective projection, and basic rasterization in 2D.
Since using no graphical libraries for 3D rendering implies we rely on something like a simple 2D drawing surface (e.g., raster onto an image buffer and display it using SDL or even native image output), we must draw pixels manually.
We can use basic image output packages available from the Go standard library like image, image/color, and image/png. Anything more (like OpenGL, raylib-go, or go-gl)—is not allowed per your instruction.
✅ What This Will Include:
- Custom renderer (perspective projection, triangle rasterizer)
- Simple terrain generation (heightmap or Perlin/simplex noise)
- 3D camera movement (WASD and mouse for look around)
- Framebuffer drawn to a
.png file or shown in a GUI window with minimal deps (optional)
📁 Project Structure
terrain3d/
│
├── main.go # Entry point
├── go.mod # Go module
│
├── core/
│ ├── camera.go # 3D camera code
│ ├── renderer.go # Custom 3D renderer
│ ├── math.go # Vector & matrix math
│ └── raster.go # Triangle rasterization
│
├── terrain/
│ └── generator.go # Terrain generation code (noise, etc)
│
├── data/
│ └── heightmap.png # Optional: Load heightmap from file
│
└── output/
└── frame.png # Output of 3D render (if using image generation)
Everything will be custom-built—camera, transformation matrices, 3D to 2D projection, and pixel drawing.
🧠 Step-by-Step: Building Core Modules
1. core/math.go: Basic 3D vector and matrix
package core
import "math"
type Vec3 struct {
X, Y, Z float64
}
type Vec2 struct {
X, Y float64
}
func (v Vec3) Add(o Vec3) Vec3 {
return Vec3{v.X + o.X, v.Y + o.Y, v.Z + o.Z}
}
func (v Vec3) Sub(o Vec3) Vec3 {
return Vec3{v.X - o.X, v.Y - o.Y, v.Z - o.Z}
}
func (v Vec3) Mul(s float64) Vec3 {
return Vec3{v.X * s, v.Y * s, v.Z * s}
}
func (v Vec3) Dot(o Vec3) float64 {
return v.X*o.X + v.Y*o.Y + v.Z*o.Z
}
func (v Vec3) Cross(o Vec3) Vec3 {
return Vec3{
v.Y*o.Z - v.Z*o.Y,
v.Z*o.X - v.X*o.Z,
v.X*o.Y - v.Y*o.X,
}
}
func (v Vec3) Normalize() Vec3 {
length := math.Sqrt(v.X*v.X + v.Y*v.Y + v.Z*v.Z)
if length == 0 {
return v
}
return v.Mul(1 / length)
}
2. core/camera.go
package core
import "math"
type Camera struct {
Position Vec3
Yaw, Pitch float64
FOV, Aspect, Near, Far float64
}
func (c *Camera) GetDirection() Vec3 {
yawRad := c.Yaw * math.Pi / 180.0
pitchRad := c.Pitch * math.Pi / 180.0
return Vec3{
X: math.Cos(pitchRad) * math.Sin(yawRad),
Y: math.Sin(pitchRad),
Z: math.Cos(pitchRad) * math.Cos(yawRad),
}.Normalize()
}
func (c *Camera) Move(dir Vec3, amt float64) {
c.Position = c.Position.Add(dir.Mul(amt))
}
3. terrain/generator.go
package terrain
import "terrain3d/core"
const SIZE = 64
func GenerateHeightMap() [][]float64 {
height := make([][]float64, SIZE)
for x := 0; x < SIZE; x++ {
height[x] = make([]float64, SIZE)
for z := 0; z < SIZE; z++ {
height[x][z] = float64((x ^ z) % 8) // Very simple "noise"
}
}
return height
}
func GenerateMesh(heightMap [][]float64) []Triangle {
var mesh []Triangle
for x := 0; x < SIZE-1; x++ {
for z := 0; z < SIZE-1; z++ {
v0 := core.Vec3{float64(x), heightMap[x][z], float64(z)}
v1 := core.Vec3{float64(x + 1), heightMap[x + 1][z], float64(z)}
v2 := core.Vec3{float64(x), heightMap[x][z + 1], float64(z + 1)}
v3 := core.Vec3{float64(x + 1), heightMap[x + 1][z + 1], float64(z + 1)}
mesh = append(mesh, Triangle{v0, v1, v2})
mesh = append(mesh, Triangle{v2, v1, v3})
}
}
return mesh
}
type Triangle struct {
V0, V1, V2 core.Vec3
}
4. core/renderer.go
package core
import (
"image"
"image/color"
"math"
)
func Project(v Vec3, width, height int, fov, near, far float64) (Vec2, bool) {
// Perspective projection
if v.Z == 0 {
v.Z = 0.001
}
f := 1.0 / math.Tan(fov/2.0)
x := (v.X / v.Z) * f
y := (v.Y / v.Z) * f * float64(height) / float64(width)
screenX := int((x+1.0)*float64(width)/2.0)
screenY := int((1.0-y)*float64(height)/2.0)
return Vec2{float64(screenX), float64(screenY)}, v.Z > near && v.Z < far
}
func DrawTriangle(img *image.RGBA, v0, v1, v2 Vec2, col color.RGBA) {
// TODO: add triangle rasterizer here
img.Set(int(v0.X), int(v0.Y), col)
img.Set(int(v1.X), int(v1.Y), col)
img.Set(int(v2.X), int(v2.Y), col)
}
5. main.go
package main
import (
"image"
"image/color"
"image/png"
"os"
"terrain3d/core"
"terrain3d/terrain"
)
const (
Width = 800
Height = 600
)
func main() {
img := image.NewRGBA(image.Rect(0, 0, Width, Height))
camera := core.Camera{
Position: core.Vec3{32, 20, -40},
Yaw: 45,
Pitch: -20,
FOV: 1.0,
Aspect: float64(Width) / float64(Height),
Near: 0.1,
Far: 1000,
}
hmap := terrain.GenerateHeightMap()
mesh := terrain.GenerateMesh(hmap)
for _, tri := range mesh {
toCamera := func(v core.Vec3) core.Vec3 {
dir := camera.GetDirection()
return v.Sub(camera.Position)
}
p0, ok0 := core.Project(toCamera(tri.V0), Width, Height, camera.FOV, camera.Near, camera.Far)
p1, ok1 := core.Project(toCamera(tri.V1), Width, Height, camera.FOV, camera.Near, camera.Far)
p2, ok2 := core.Project(toCamera(tri.V2), Width, Height, camera.FOV, camera.Near, camera.Far)
if ok0 && ok1 && ok2 {
core.DrawTriangle(img, p0, p1, p2, color.RGBA{0, 255, 0, 255})
}
}
os.Mkdir("output", 0755)
f, _ := os.Create("output/frame.png")
defer f.Close()
png.Encode(f, img)
}
🟡 Features Missing / Next Steps
| Feature | Status | Notes |
|---|
| Movement input (WASD) | ❌ | Requires terminal/GUI event handling |
| Triangle rasterizer | ⚠️ | Placeholder: sample pixel only |
| Shading or lighting | ❌ | Could use basic normals |
| GUI window | ❌ | Consider using github.com/veandco/go-sdl2 for real-time draw |
| Accurate matrix transforms | ⚠️ | Projection and View matrices should be abstracted |
🚀 Running
go mod init terrain3d
go run main.go
Check output/frame.png for the image result.
🧠 Conclusion
Even with no 3D graphics libraries, it's possible to implement the fundamental building blocks of a 3D renderer in Go. To get a real-time display, you'd eventually need something like SDL or write code to continually update a display window—still doing all 3D math manually.
Would you like to continue this into interactive mode with live rendering (possibly using a minimal SDL wrapper for keyboard/mouse input, but rendering still manual)? Or build a basic game loop simulation?