Tutorial — making good-looking images
This guide teaches you how to compose scenes with the povray library, from the simplest to the most advanced. Each level builds on the previous one; every code sample is complete and renders as is (go run in the project folder).
The finished examples are in examples/ — when a level feels unclear, read the matching file.
Level 0 — Your first image
A scene, a sphere, a light, a camera. That's all.
package main
import (
"log"
povray "rickub.com/bots-garden/pov-ray-go"
)
func main() {
scene := povray.NewScene()
scene.AddObject(
povray.Sphere{Center: povray.Vec3{0, 1, 0}, Radius: 1},
povray.Material{Diffuse: povray.Red, Specular: povray.White, Shininess: 30},
)
scene.AddObject(povray.NewPlaneXY(0),
povray.Material{Diffuse: povray.Gray, Ambient: 0.3})
scene.Lights = []povray.Light{povray.NewLight(povray.Vec3{4, 8, 6}, povray.White)}
cam := povray.NewCamera(povray.Vec3{0, 2, 8})
r := povray.Renderer{Scene: scene, Width: 800, Height: 600, Samples: 2}
if err := r.Render(cam).SavePNG("hello.png"); err != nil {
log.Fatal(err)
}
}
What to remember:
NewScene()creates a scene (white background, gamma 2.2).AddObject(obj, mat)adds an object and its material.Lightsis a list of point lights.Renderer:Width/Heightin pixels,Samples: 2= 4 rays per pixel (anti-aliasing). WithoutSamples, edges are jagged.SavePNGwrites the file.
Level 1 — Materials and colors
The material is what makes the image. The useful fields:
| Field | Effect | Typical values |
|---|---|---|
Diffuse |
base color | povray.Red, povray.Color{0.8, 0.2, 0.1} |
Specular |
highlight color | povray.White |
Shininess |
highlight size (higher = smaller) | 10 (matte) to 100 (chrome) |
Ambient |
residual light in the shadow | 0.1 to 0.4 |
Reflectivity |
mirror strength | 0 to 1 |
Transparency |
glass strength | 0 to 1 |
IOR |
glass index of refraction | 1.5 (glass), 1.33 (water) |
Golden rule of colors: Diffuse values are linear. A 0.5 gray renders darker than it looks (gamma 2.2 brightens it at render time, but not as much as you would expect). For "vivid" colors, raise the values: Color{0.9, 0.1, 0.1} is redder than Color{0.5, 0.05, 0.05}.
Two opposite materials on neighboring objects create contrast — the basis of every good image:
// A dark floor carries light objects (and vice versa).
scene.AddObject(povray.NewPlaneXY(0),
povray.Material{Diffuse: povray.Color{0.12, 0.12, 0.15}, Ambient: 0.3})
scene.AddObject(povray.Sphere{Center: povray.Vec3{-2, 1, 0}, Radius: 1},
povray.Material{Diffuse: povray.Color{0.9, 0.3, 0.2}, Specular: povray.White, Shininess: 40})
scene.AddObject(povray.Sphere{Center: povray.Vec3{2, 1, 0}, Radius: 1},
povray.Material{Diffuse: povray.Color{0.2, 0.7, 0.9}, Specular: povray.White, Shininess: 40})
See examples/spheres/ for a complete scene.
Level 2 — Camera and composition
The camera is a pinhole: it sits at Position, looks at LookAt, with Fov (vertical field of view in degrees).
cam := povray.NewCamera(povray.Vec3{0, 3, 10}) // move back and up
cam.LookAt = povray.Vec3{0, 1, -2} // look further than the object
cam.Fov = 50 // < 60 = telephoto, > 70 = wide angle
Composition principles:
- Move the camera back rather than zooming: a wide Fov (wide angle) distorts nearby objects; a narrow Fov compresses them and flattens the scene.
- Look slightly above or below the subject, not exactly at its middle: a
LookAta bit higher than the objects' center gives a natural "from above" view. - Leave some room: a subject at the exact center is rarely pretty. Shift it by a third (rule of thirds) by moving
LookAtin the opposite direction. - The floor plane must extend out of the frame: if the horizon is visible, the scene looks small. Raise the camera or bring the floor closer.
Level 3 — Transformations: placing objects
Raw objects live in world space. To tilt them, stretch them or lay them out in a loop, wrap them in a Transform:
// A box tilted 30° around X, moved to (-3, 0, -2).
tilted := povray.Transform{
Object: povray.Box{Min: povray.Vec3{-1, 0, -1}, Max: povray.Vec3{1, 2, 1}},
Matrix: povray.Translate(-3, 0, -2).Multiply(povray.RotateX(math.Pi / 6)),
}
scene.AddObject(tilted, mat)
Order matters: Multiply applies right to left. Translate(...).Multiply(RotateX(...)) = rotation first, then translation. To "move, then rotate in place", swap them: RotateX(...).Multiply(Translate(...)).
The basic primitives:
Translate(x, y, z)— moveRotateX/Y/Z(angle)— rotate (radians:math.Pi/2= 90°)Scale(x, y, z)— stretch (a scaled sphere = an ellipsoid)Identity()— nothing
Laying out in a loop is the simplest way to build rich scenes:
var ring []povray.Object
for i := 0; i < 12; i++ {
a := 2 * math.Pi * float64(i) / 12
m := povray.RotateY(a).Multiply(povray.Translate(0, 0, -5.5))
ring = append(ring, povray.Transform{
Object: povray.Box{Min: povray.Vec3{-0.4, 0, -0.4}, Max: povray.Vec3{0.4, 1.6, 0.4}},
Matrix: m,
})
}
scene.AddObjects(ring, povray.Material{Diffuse: povray.Gold, ...})
See examples/transform/ (ring of boxes, mirror ellipsoid) and examples/procedural/ (a grid of 200 spheres whose color varies with position).
Level 4 — Mirrors and glass
Two material fields are enough:
// Mirror: high Reflectivity, dark Diffuse.
mirror := povray.Material{
Diffuse: povray.Color{0.1, 0.1, 0.12},
Reflectivity: 0.9,
Specular: povray.White, Shininess: 80,
}
// Glass: high Transparency + IOR.
glass := povray.Material{
Diffuse: povray.White,
Transparency: 0.95,
IOR: 1.5,
Specular: povray.White, Shininess: 100,
Ambient: 0.05,
}
Tips:
- A pure mirror (
Reflectivity: 1,Diffuse: Black) is pretty but flat. AReflectivity: 0.5with a coloredDiffusegives a very photogenic "tinted metal". - Glass looks better when it contains or hides something colorful. A glass sphere on its own is almost invisible.
- The "mirror floor" effect: a plane with
Reflectivity: 0.3and a darkDiffusemakes soft reflections under the objects.
See examples/mirror/.
Level 5 — Textures: color by position
A material is normally one fixed Diffuse color. To make the color depend on where on the surface a ray lands, set the material's Texture field. The texture is evaluated in world space at the hit point, so it repeats across every object that shares the material.
// Checkerboard floor: two colors, one cell per world unit.
floor := povray.Material{Specular: povray.White, Shininess: 20, Ambient: 0.2}
floor.Texture = povray.Checker{
ColorA: povray.Color{0.85, 0.85, 0.9},
ColorB: povray.Color{0.08, 0.08, 0.1},
Scale: 1, // cells per unit; 2 = twice as many, smaller cells
}
scene.AddObject(povray.NewPlaneXY(0), floor)
The built-in textures:
| Texture | Look | Key fields |
|---|---|---|
Checker |
two-color checkerboard | ColorA, ColorB, Scale (cells per unit) |
Gradient |
smooth blend between two colors | ColorA, ColorB, Axis (0=X, 1=Y, 2=Z) |
ValueNoise |
soft mottling (stone, clouds) | ColorA, ColorB, Scale (feature size), Seed |
Noise |
fine per-point grain (speckle) | ColorA, ColorB, Scale (feature size) |
Tips:
- A texture replaces
Diffuse— theDiffusefield is ignored whileTextureis set. KeepSpecular,Shininess,Ambientetc. as normal. ValueNoiseis smooth (interpolated);Noiseis grainy (no interpolation). UseValueNoisefor stone or marble,Noisefor a speckled or sandy look.- Textures work identically in the
Rendererand thePathTracer— the textured color is what bounces as diffuse light, so a red/green checker floor tints the surrounding surfaces in a path-traced scene. - Because textures are in world space, a scaled or rotated object still samples the same pattern — move the object to reposition the pattern, not the
Transform.
See examples/textures/ (checkerboard floor, gradient wall, noise box, speckled sphere) and examples/textures-big/ (the same textures on large objects so each pattern reads clearly).
Level 6 — Path tracing: realistic light
The classic Renderer lights surfaces one by one. The PathTracer bounces light from surface to surface: indirect lighting (a red sphere tints the floor around it), soft shadows, colored light. This is what makes images look "real".
// Path tracer lights are EMISSIVE SURFACES.
panel := povray.Transform{
Object: povray.Box{Min: povray.Vec3{-1.5, 0, -1.5}, Max: povray.Vec3{1.5, 0.15, 1.5}},
Matrix: povray.Translate(0, 9, -2),
}
scene.AddObject(panel, povray.LightMaterial(povray.Color{1, 0.85, 0.65}, 14))
tracer := povray.PathTracer{Scene: scene, Width: 800, Height: 600, Samples: 64}
img := tracer.Render(cam)
Differences from the Renderer:
Scene.Lightsare ignored — light comes from materials withEmission(useLightMaterial(color, intensity)).Samples= number of rays per pixel, not a grid. 16 = fast (a bit of noise), 64 = clean, 256 = publication quality.- Noise is normal: it is a random estimator. More samples = less noise. Rendering is deterministic (the same image every time for the same configuration).
- Intensities: a light panel needs an intensity of 10 to 20 to light a scene of this scale well.
See examples/pathtrace/: a room with a warm panel + a cool strip (the light bounces and tints the walls), a red sphere, a blue mirror, glass.
Level 7 — Recipes for good-looking images
The palette
Pick 2 to 3 base colors and a neutral. Complementary colors face each other:
var (
warm = povray.Color{0.9, 0.45, 0.2} // orange
cool = povray.Color{0.2, 0.4, 0.85} // blue
neutral = povray.Color{0.25, 0.25, 0.28} // dark gray for the floor
)
A dark, neutral floor is the most versatile background: it does not compete with the objects.
The light
- Two lights are better than one: a key light (strong, warm, high up) + a fill light (weak, cool, in front):
scene.Lights = []povray.Light{
povray.NewLight(povray.Vec3{6, 10, 8}, povray.Color{1, 0.95, 0.85}),
povray.NewLight(povray.Vec3{-8, 4, -4}, povray.Color{0.5, 0.6, 1.0}),
}
- Place the light high and to the side: from the front, shadows disappear behind the objects; from straight above, everything is flat.
- In the path tracer, the light's color matters more than its intensity: a
Color{1, 0.85, 0.65}panel (warm) + aColor{0.4, 0.6, 1}strip (cool) sets a mood immediately.
Depth
- Varied sizes: one large + one small object give a sense of scale.
- Spread along Z: objects at different distances create depth; at the same Z, the scene is flat.
- A foreground element (partly out of frame) gives a "looking through" effect.
Final settings
| Setting | For… |
|---|---|
Samples: 2 (Renderer) |
quick, clean preview |
Samples: 4 (Renderer) |
final Renderer output |
Samples: 16 (PathTracer) |
iterating (visible noise) |
Samples: 64 (PathTracer) |
final path-traced output |
Width: 1920, Height: 1080 |
large image |
Fov: 45 |
compressed, elegant "photo" |
Fov: 75 |
immersive view, wide scene |
What makes an image "finished"
- The subject takes up about a third of the frame.
- There is contrast: at least one light area and one dark area.
- The colors are balanced: no area shouts louder than the subject.
- The floor has a tint (never pure white, never pure black).
- You have looked at the render at a reduced size: if the image works at 200 px wide, it works large.
Where to go next
examples/— seven complete scenes to take apart.README.md— API reference.ROADMAP.md— what the library cannot do yet (.objmeshes, area lights, normal mapping…).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|