bots-garden/pov-ray-gopublic Fork
⑂ c7cc910
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/pov-ray-go.git
git clone ssh://git@rickub.com/bots-garden/pov-ray-go.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

saveUnverified

k33g committed 2026-11-21T15:33:24+01:00 Browse files
c7cc910 parents: a75fb3d f32f333
modified .mm/README.en.md +2 -2
@@ -41,10 +41,10 @@ From the project root:
4141
4242 ```bash
4343 cd pov-ray-go
44-mm -config .mm/agent.yaml
44+mm tui
4545 ```
4646
47-In the terminal, `skillsDir: .mm/skills` and `sessions.dir: .mm/sessions` are resolved from the directory mm is started in: always start it from the project root.
47+mm detects the configuration in `.mm/` automatically, so there is no need to pass `-config`. In the terminal, `skillsDir: .mm/skills` and `sessions.dir: .mm/sessions` are resolved from the directory mm is started in: always start it from the project root.
4848
4949 Like Qwen Code, the agent **asks before every change** (`yolo: false`): `[a]` once, `[s]` this tool for the session, `[r]` reject, `[n]` never. For an unattended run:
5050
@@ -41,10 +41,10 @@ From the project root:
41 41
42 ```bash42 ```bash
43 cd pov-ray-go43 cd pov-ray-go
44-mm -config .mm/agent.yaml44+mm tui
45 ```45 ```
46 46
47-In the terminal, `skillsDir: .mm/skills` and `sessions.dir: .mm/sessions` are resolved from the directory mm is started in: always start it from the project root.47+mm detects the configuration in `.mm/` automatically, so there is no need to pass `-config`. In the terminal, `skillsDir: .mm/skills` and `sessions.dir: .mm/sessions` are resolved from the directory mm is started in: always start it from the project root.
48 48
49 Like Qwen Code, the agent **asks before every change** (`yolo: false`): `[a]` once, `[s]` this tool for the session, `[r]` reject, `[n]` never. For an unattended run:49 Like Qwen Code, the agent **asks before every change** (`yolo: false`): `[a]` once, `[s]` this tool for the session, `[r]` reject, `[n]` never. For an unattended run:
50 50
modified .mm/README.fr.md +2 -2
@@ -41,10 +41,10 @@ Depuis la racine du projet :
4141
4242 ```bash
4343 cd pov-ray-go
44-mm -config .mm/agent.yaml
44+mm tui
4545 ```
4646
47-En terminal, `skillsDir: .mm/skills` et `sessions.dir: .mm/sessions` se résolvent depuis le répertoire où mm est lancé : lancez-le toujours depuis la racine du projet.
47+mm détecte automatiquement la configuration dans `.mm/` : inutile de passer `-config`. En terminal, `skillsDir: .mm/skills` et `sessions.dir: .mm/sessions` se résolvent depuis le répertoire où mm est lancé : lancez-le toujours depuis la racine du projet.
4848
4949 Comme Qwen Code, l'agent **demande avant chaque modification** (`yolo: false`) : `[a]` une fois, `[s]` cet outil pour la session, `[r]` refuser, `[n]` jamais. Pour une exécution sans surveillance :
5050
@@ -41,10 +41,10 @@ Depuis la racine du projet :
41 41
42 ```bash42 ```bash
43 cd pov-ray-go43 cd pov-ray-go
44-mm -config .mm/agent.yaml44+mm tui
45 ```45 ```
46 46
47-En terminal, `skillsDir: .mm/skills` et `sessions.dir: .mm/sessions` se résolvent depuis le répertoire où mm est lancé : lancez-le toujours depuis la racine du projet.47+mm détecte automatiquement la configuration dans `.mm/` : inutile de passer `-config`. En terminal, `skillsDir: .mm/skills` et `sessions.dir: .mm/sessions` se résolvent depuis le répertoire où mm est lancé : lancez-le toujours depuis la racine du projet.
48 48
49 Comme Qwen Code, l'agent **demande avant chaque modification** (`yolo: false`) : `[a]` une fois, `[s]` cet outil pour la session, `[r]` refuser, `[n]` jamais. Pour une exécution sans surveillance :49 Comme Qwen Code, l'agent **demande avant chaque modification** (`yolo: false`) : `[a]` une fois, `[s]` cet outil pour la session, `[r]` refuser, `[n]` jamais. Pour une exécution sans surveillance :
50 50
modified .mm/agent.yaml +1 -1
@@ -20,7 +20,7 @@
2020 # the current directory), or from this directory to try it:
2121 #
2222 # cd pov-ray-go # project root
23-# mm -config .mm/agent.yaml
23+# mm tui # finds .mm/agent.yaml by itself
2424 #
2525 # What changed from Qwen Code's prompt, and why: see README.en.md.
2626 # Every key is optional; reference: docs/en/reference/configuration.md.
@@ -20,7 +20,7 @@
20 # the current directory), or from this directory to try it:20 # the current directory), or from this directory to try it:
21 #21 #
22 # cd pov-ray-go # project root22 # cd pov-ray-go # project root
23-# mm -config .mm/agent.yaml23+# mm tui # finds .mm/agent.yaml by itself
24 #24 #
25 # What changed from Qwen Code's prompt, and why: see README.en.md.25 # What changed from Qwen Code's prompt, and why: see README.en.md.
26 # Every key is optional; reference: docs/en/reference/configuration.md.26 # Every key is optional; reference: docs/en/reference/configuration.md.
modified README.md +10 -2
@@ -11,6 +11,7 @@ pov-ray-go/
1111 ├── vector.go Vec3 math, Ray, Hit
1212 ├── objects.go Sphere, Plane, Box, Group + the Object interface
1313 ├── material.go Color, Material, Light
14+├── texture.go Texture interface + Checker, Gradient, ValueNoise, Noise
1415 ├── camera.go pinhole Camera
1516 ├── transform.go Mat4 (translate/rotate/scale, inverse) + Transform object
1617 ├── renderer.go Scene, Renderer, Image, PPM writer
@@ -23,6 +24,8 @@ pov-ray-go/
2324 ├── mirror/ reflections + a glass sphere
2425 ├── procedural/ a scene generated in loops (hue sweep grid)
2526 ├── transform/ tilted boxes, ellipsoid, ring of rotated boxes
27+ ├── textures/ checkerboard floor, gradient wall, noise box, speckled sphere
28+ ├── textures-big/ the same textures on large objects (big sphere, tower, slab)
2629 └── pathtrace/ path-traced room with emissive light panels
2730 ```
2831
@@ -47,6 +50,7 @@ img.SavePNG("out.png") // or img.SavePPM("out.ppm")
4750 - **Objects** implement `Object` (`Intersect(Ray) (Hit, bool)`). Built in: `Sphere`, `Plane` (infinite), `Box` (axis-aligned), `Group` (closest of many). Add your own by implementing the interface.
4851 - **Transforms** place any object anywhere: `Translate`, `RotateX/Y/Z`, `Scale` build a `Mat4` (composed with `Multiply`, applied inside-out), and `Transform{Object, Matrix}` maps the ray into the object's local space, intersects there, and maps the hit point and normal back to world space — normals go through the inverse transpose, so non-uniform scale stays correct.
4952 - **Materials** control the look: `Diffuse` (base color), `Specular` + `Shininess` (highlights), `Ambient`, `Reflectivity` (mirror, 0..1) and `Transparency` + `IOR` (glass, 0..1).
53+- **Textures** color a surface by position: set `Material.Texture` to a `Checker`, `Gradient`, `ValueNoise` or `Noise` and the surface color is looked up at the hit point (world space) instead of the fixed `Diffuse`. Works in both the `Renderer` and the `PathTracer`.
5054 - **Lights** are colored point lights with inverse-square falloff; the renderer casts shadow rays for each.
5155 - **Camera** is a pinhole camera: position, look-at point, up vector and vertical field of view.
5256 - **Renderer** is parallel (row chunks across goroutines) and limits reflection/refraction bounces with `Scene.MaxDepth`. Set `Samples` for anti-aliasing: `Samples: 2` shoots a 2×2 grid of rays per pixel and averages them (0 or 1 = one ray per pixel, no anti-aliasing).
@@ -59,6 +63,8 @@ go run ./examples/spheres # writes spheres.png
5963 go run ./examples/mirror # writes mirror.png
6064 go run ./examples/procedural # writes procedural.png
6165 go run ./examples/transform # writes transform.png
66+go run ./examples/textures # writes textures.png
67+go run ./examples/textures-big # writes textures-big.png
6268 go run ./examples/pathtrace # writes pathtrace.png (slower)
6369 ```
6470
@@ -71,9 +77,11 @@ Output is PNG (8-bit truecolor, written by the library's own encoder — no depe
7177 The project ships its own `mm` (mini-me) coding agent configuration in `.mm/`: Qwen3.8 27B served by llmman, with Qwen Code's system prompt. Start it from the project root:
7278
7379 ```sh
74-mm -config .mm/agent.yaml
80+mm tui
7581 ```
7682
83+mm detects the configuration in `.mm/` automatically, so there is no need to pass `-config`.
84+
7785 Details (prerequisites, ACP, language servers, measured results) in [.mm/README.en.md](.mm/README.en.md) / [.mm/README.fr.md](.mm/README.fr.md).
7886
7987 ## How this project was built
@@ -104,6 +112,6 @@ If you want to go further, *Ray Tracing in One Weekend* (and its sequel, *Realis
104112
105113 ## Limitations
106114
107-No textures, no area lights (emissive surfaces are the light source for the path tracer), no triangle meshes. Anti-aliasing is grid supersampling (`Renderer.Samples`) plus stochastic sampling in the `PathTracer`. Transforms are per-object (`Transform` wrapper); there is no scene-wide model/view matrix yet.
115+No normal mapping, no area lights (emissive surfaces are the light source for the path tracer), no triangle meshes. Textures are position-based (checker, gradient, noise) in world space. Anti-aliasing is grid supersampling (`Renderer.Samples`) plus stochastic sampling in the `PathTracer`. Transforms are per-object (`Transform` wrapper); there is no scene-wide model/view matrix yet.
108116
109117 Note: `Vec3` and `Color` are intentionally written unkeyed (`povray.Vec3{1, 0, 0}`) — `go vet` will flag this in example code; that's expected for a math library.
@@ -11,6 +11,7 @@ pov-ray-go/
11 ├── vector.go Vec3 math, Ray, Hit11 ├── vector.go Vec3 math, Ray, Hit
12 ├── objects.go Sphere, Plane, Box, Group + the Object interface12 ├── objects.go Sphere, Plane, Box, Group + the Object interface
13 ├── material.go Color, Material, Light13 ├── material.go Color, Material, Light
14+├── texture.go Texture interface + Checker, Gradient, ValueNoise, Noise
14 ├── camera.go pinhole Camera15 ├── camera.go pinhole Camera
15 ├── transform.go Mat4 (translate/rotate/scale, inverse) + Transform object16 ├── transform.go Mat4 (translate/rotate/scale, inverse) + Transform object
16 ├── renderer.go Scene, Renderer, Image, PPM writer17 ├── renderer.go Scene, Renderer, Image, PPM writer
@@ -23,6 +24,8 @@ pov-ray-go/
23 ├── mirror/ reflections + a glass sphere24 ├── mirror/ reflections + a glass sphere
24 ├── procedural/ a scene generated in loops (hue sweep grid)25 ├── procedural/ a scene generated in loops (hue sweep grid)
25 ├── transform/ tilted boxes, ellipsoid, ring of rotated boxes26 ├── transform/ tilted boxes, ellipsoid, ring of rotated boxes
27+ ├── textures/ checkerboard floor, gradient wall, noise box, speckled sphere
28+ ├── textures-big/ the same textures on large objects (big sphere, tower, slab)
26 └── pathtrace/ path-traced room with emissive light panels29 └── pathtrace/ path-traced room with emissive light panels
27 ```30 ```
28 31
@@ -47,6 +50,7 @@ img.SavePNG("out.png") // or img.SavePPM("out.ppm")
47 - **Objects** implement `Object` (`Intersect(Ray) (Hit, bool)`). Built in: `Sphere`, `Plane` (infinite), `Box` (axis-aligned), `Group` (closest of many). Add your own by implementing the interface.50 - **Objects** implement `Object` (`Intersect(Ray) (Hit, bool)`). Built in: `Sphere`, `Plane` (infinite), `Box` (axis-aligned), `Group` (closest of many). Add your own by implementing the interface.
48 - **Transforms** place any object anywhere: `Translate`, `RotateX/Y/Z`, `Scale` build a `Mat4` (composed with `Multiply`, applied inside-out), and `Transform{Object, Matrix}` maps the ray into the object's local space, intersects there, and maps the hit point and normal back to world space — normals go through the inverse transpose, so non-uniform scale stays correct.51 - **Transforms** place any object anywhere: `Translate`, `RotateX/Y/Z`, `Scale` build a `Mat4` (composed with `Multiply`, applied inside-out), and `Transform{Object, Matrix}` maps the ray into the object's local space, intersects there, and maps the hit point and normal back to world space — normals go through the inverse transpose, so non-uniform scale stays correct.
49 - **Materials** control the look: `Diffuse` (base color), `Specular` + `Shininess` (highlights), `Ambient`, `Reflectivity` (mirror, 0..1) and `Transparency` + `IOR` (glass, 0..1).52 - **Materials** control the look: `Diffuse` (base color), `Specular` + `Shininess` (highlights), `Ambient`, `Reflectivity` (mirror, 0..1) and `Transparency` + `IOR` (glass, 0..1).
53+- **Textures** color a surface by position: set `Material.Texture` to a `Checker`, `Gradient`, `ValueNoise` or `Noise` and the surface color is looked up at the hit point (world space) instead of the fixed `Diffuse`. Works in both the `Renderer` and the `PathTracer`.
50 - **Lights** are colored point lights with inverse-square falloff; the renderer casts shadow rays for each.54 - **Lights** are colored point lights with inverse-square falloff; the renderer casts shadow rays for each.
51 - **Camera** is a pinhole camera: position, look-at point, up vector and vertical field of view.55 - **Camera** is a pinhole camera: position, look-at point, up vector and vertical field of view.
52 - **Renderer** is parallel (row chunks across goroutines) and limits reflection/refraction bounces with `Scene.MaxDepth`. Set `Samples` for anti-aliasing: `Samples: 2` shoots a 2×2 grid of rays per pixel and averages them (0 or 1 = one ray per pixel, no anti-aliasing).56 - **Renderer** is parallel (row chunks across goroutines) and limits reflection/refraction bounces with `Scene.MaxDepth`. Set `Samples` for anti-aliasing: `Samples: 2` shoots a 2×2 grid of rays per pixel and averages them (0 or 1 = one ray per pixel, no anti-aliasing).
@@ -59,6 +63,8 @@ go run ./examples/spheres # writes spheres.png
59 go run ./examples/mirror # writes mirror.png63 go run ./examples/mirror # writes mirror.png
60 go run ./examples/procedural # writes procedural.png64 go run ./examples/procedural # writes procedural.png
61 go run ./examples/transform # writes transform.png65 go run ./examples/transform # writes transform.png
66+go run ./examples/textures # writes textures.png
67+go run ./examples/textures-big # writes textures-big.png
62 go run ./examples/pathtrace # writes pathtrace.png (slower)68 go run ./examples/pathtrace # writes pathtrace.png (slower)
63 ```69 ```
64 70
@@ -71,9 +77,11 @@ Output is PNG (8-bit truecolor, written by the library's own encoder — no depe
71 The project ships its own `mm` (mini-me) coding agent configuration in `.mm/`: Qwen3.8 27B served by llmman, with Qwen Code's system prompt. Start it from the project root:77 The project ships its own `mm` (mini-me) coding agent configuration in `.mm/`: Qwen3.8 27B served by llmman, with Qwen Code's system prompt. Start it from the project root:
72 78
73 ```sh79 ```sh
74-mm -config .mm/agent.yaml80+mm tui
75 ```81 ```
76 82
83+mm detects the configuration in `.mm/` automatically, so there is no need to pass `-config`.
84+
77 Details (prerequisites, ACP, language servers, measured results) in [.mm/README.en.md](.mm/README.en.md) / [.mm/README.fr.md](.mm/README.fr.md).85 Details (prerequisites, ACP, language servers, measured results) in [.mm/README.en.md](.mm/README.en.md) / [.mm/README.fr.md](.mm/README.fr.md).
78 86
79 ## How this project was built87 ## How this project was built
@@ -104,6 +112,6 @@ If you want to go further, *Ray Tracing in One Weekend* (and its sequel, *Realis
104 112
105 ## Limitations113 ## Limitations
106 114
107-No textures, no area lights (emissive surfaces are the light source for the path tracer), no triangle meshes. Anti-aliasing is grid supersampling (`Renderer.Samples`) plus stochastic sampling in the `PathTracer`. Transforms are per-object (`Transform` wrapper); there is no scene-wide model/view matrix yet.115+No normal mapping, no area lights (emissive surfaces are the light source for the path tracer), no triangle meshes. Textures are position-based (checker, gradient, noise) in world space. Anti-aliasing is grid supersampling (`Renderer.Samples`) plus stochastic sampling in the `PathTracer`. Transforms are per-object (`Transform` wrapper); there is no scene-wide model/view matrix yet.
108 116
109 Note: `Vec3` and `Color` are intentionally written unkeyed (`povray.Vec3{1, 0, 0}`) — `go vet` will flag this in example code; that's expected for a math library.117 Note: `Vec3` and `Color` are intentionally written unkeyed (`povray.Vec3{1, 0, 0}`) — `go vet` will flag this in example code; that's expected for a math library.
modified ROADMAP.md +3 -2
@@ -6,7 +6,7 @@ Status snapshot of the `povray` library and the work that remains, in order of i
66
77 - **Transformations** (done — see `transform.go`). Objects can now be translated, rotated and (non-uniformly) scaled via the `Transform` wrapper; normals are mapped with the inverse transpose so lighting stays correct under non-uniform scale.
88 - **Anti-aliasing** (done — see `Renderer.Samples`). Grid supersampling: `Samples: n` shoots an n×n grid of rays per pixel and averages them. All examples now render with `Samples: 2`.
9-- **Textures** — color by position (checker, gradient, noise). Today a material is a single fixed color.
9+- **Textures** (done — see `texture.go`). Color by position: `Checker`, `Gradient`, `ValueNoise` and `Noise`, set on `Material.Texture` and evaluated in world space at the hit point. Works in both the `Renderer` and the `PathTracer`.
1010 - **Normal mapping / bump** — non-smooth surfaces without meshes.
1111 - **Path tracing** (done — see `pathtracer.go`). The `PathTracer` bounces rays between surfaces: global illumination, soft shadows, colored bounce light. Lights are emissive surfaces (`LightMaterial`); renders are deterministic.
1212 - **Area lights** — partially covered: emissive surfaces (boxes) act as area lights in the path tracer. A dedicated, more efficient area-light sampler (importance sampling over the light) is still missing.
@@ -19,7 +19,7 @@ Status snapshot of the `povray` library and the work that remains, in order of i
1919
2020 ## 3. Engineering — what a publishable package needs
2121
22-- **Real module path** — `github.com/user/povray` is a placeholder; nobody can `go get` the package.
22+- **Real module path** — `go-povray` is a placeholder; nobody can `go get` the package.
2323 - **CI** — no `.github/workflows`; a simple `go test` + `go vet` + `gofmt -l` on GitHub Actions.
2424 - **Test coverage** — examples have no tests; missing cases: `Box` seen from inside, nested `Group`, refraction through the glass sphere, `SavePNG` on a rendered (not 4×3) image.
2525 - **`go vet` clean everywhere** — unkeyed `Vec3{...}`/`Color{...}` literals in examples are still flagged; key them, add a `//nolint`, or provide constructors.
@@ -32,5 +32,6 @@ Status snapshot of the `povray` library and the work that remains, in order of i
3232 1. ~~**Transformations**~~ — done (`transform.go`, `examples/transform`).
3333 2. ~~**Anti-aliasing**~~ — done (`Renderer.Samples`, grid supersampling).
3434 3. ~~**Path tracing**~~ — done (`pathtracer.go`, `examples/pathtrace`).
35+4. ~~**Textures**~~ — done (`texture.go`, `examples/textures`).
3536
3637 Package hygiene (real module path + `LICENSE` + CI + `Makefile`) is about an hour of work and makes the package publishable.
@@ -6,7 +6,7 @@ Status snapshot of the `povray` library and the work that remains, in order of i
6 6
7 - **Transformations** (done — see `transform.go`). Objects can now be translated, rotated and (non-uniformly) scaled via the `Transform` wrapper; normals are mapped with the inverse transpose so lighting stays correct under non-uniform scale.7 - **Transformations** (done — see `transform.go`). Objects can now be translated, rotated and (non-uniformly) scaled via the `Transform` wrapper; normals are mapped with the inverse transpose so lighting stays correct under non-uniform scale.
8 - **Anti-aliasing** (done — see `Renderer.Samples`). Grid supersampling: `Samples: n` shoots an n×n grid of rays per pixel and averages them. All examples now render with `Samples: 2`.8 - **Anti-aliasing** (done — see `Renderer.Samples`). Grid supersampling: `Samples: n` shoots an n×n grid of rays per pixel and averages them. All examples now render with `Samples: 2`.
9-- **Textures** — color by position (checker, gradient, noise). Today a material is a single fixed color.9+- **Textures** (done — see `texture.go`). Color by position: `Checker`, `Gradient`, `ValueNoise` and `Noise`, set on `Material.Texture` and evaluated in world space at the hit point. Works in both the `Renderer` and the `PathTracer`.
10 - **Normal mapping / bump** — non-smooth surfaces without meshes.10 - **Normal mapping / bump** — non-smooth surfaces without meshes.
11 - **Path tracing** (done — see `pathtracer.go`). The `PathTracer` bounces rays between surfaces: global illumination, soft shadows, colored bounce light. Lights are emissive surfaces (`LightMaterial`); renders are deterministic.11 - **Path tracing** (done — see `pathtracer.go`). The `PathTracer` bounces rays between surfaces: global illumination, soft shadows, colored bounce light. Lights are emissive surfaces (`LightMaterial`); renders are deterministic.
12 - **Area lights** — partially covered: emissive surfaces (boxes) act as area lights in the path tracer. A dedicated, more efficient area-light sampler (importance sampling over the light) is still missing.12 - **Area lights** — partially covered: emissive surfaces (boxes) act as area lights in the path tracer. A dedicated, more efficient area-light sampler (importance sampling over the light) is still missing.
@@ -19,7 +19,7 @@ Status snapshot of the `povray` library and the work that remains, in order of i
19 19
20 ## 3. Engineering — what a publishable package needs20 ## 3. Engineering — what a publishable package needs
21 21
22-- **Real module path** — `github.com/user/povray` is a placeholder; nobody can `go get` the package.22+- **Real module path** — `go-povray` is a placeholder; nobody can `go get` the package.
23 - **CI** — no `.github/workflows`; a simple `go test` + `go vet` + `gofmt -l` on GitHub Actions.23 - **CI** — no `.github/workflows`; a simple `go test` + `go vet` + `gofmt -l` on GitHub Actions.
24 - **Test coverage** — examples have no tests; missing cases: `Box` seen from inside, nested `Group`, refraction through the glass sphere, `SavePNG` on a rendered (not 4×3) image.24 - **Test coverage** — examples have no tests; missing cases: `Box` seen from inside, nested `Group`, refraction through the glass sphere, `SavePNG` on a rendered (not 4×3) image.
25 - **`go vet` clean everywhere** — unkeyed `Vec3{...}`/`Color{...}` literals in examples are still flagged; key them, add a `//nolint`, or provide constructors.25 - **`go vet` clean everywhere** — unkeyed `Vec3{...}`/`Color{...}` literals in examples are still flagged; key them, add a `//nolint`, or provide constructors.
@@ -32,5 +32,6 @@ Status snapshot of the `povray` library and the work that remains, in order of i
32 1. ~~**Transformations**~~ — done (`transform.go`, `examples/transform`).32 1. ~~**Transformations**~~ — done (`transform.go`, `examples/transform`).
33 2. ~~**Anti-aliasing**~~ — done (`Renderer.Samples`, grid supersampling).33 2. ~~**Anti-aliasing**~~ — done (`Renderer.Samples`, grid supersampling).
34 3. ~~**Path tracing**~~ — done (`pathtracer.go`, `examples/pathtrace`).34 3. ~~**Path tracing**~~ — done (`pathtracer.go`, `examples/pathtrace`).
35+4. ~~**Textures**~~ — done (`texture.go`, `examples/textures`).
35 36
36 Package hygiene (real module path + `LICENSE` + CI + `Makefile`) is about an hour of work and makes the package publishable.37 Package hygiene (real module path + `LICENSE` + CI + `Makefile`) is about an hour of work and makes the package publishable.
modified docs/TUTORIAL.md +38 -5
@@ -14,7 +14,7 @@ package main
1414 import (
1515 "log"
1616
17- "github.com/user/povray"
17+ "go-povray"
1818 )
1919
2020 func main() {
@@ -160,7 +160,40 @@ glass := povray.Material{
160160
161161 See `examples/mirror/`.
162162
163-## Level 5 — Path tracing: realistic light
163+## Level 5 — Textures: color by position
164+
165+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.
166+
167+```go
168+// Checkerboard floor: two colors, one cell per world unit.
169+floor := povray.Material{Specular: povray.White, Shininess: 20, Ambient: 0.2}
170+floor.Texture = povray.Checker{
171+ ColorA: povray.Color{0.85, 0.85, 0.9},
172+ ColorB: povray.Color{0.08, 0.08, 0.1},
173+ Scale: 1, // cells per unit; 2 = twice as many, smaller cells
174+}
175+scene.AddObject(povray.NewPlaneXY(0), floor)
176+```
177+
178+The built-in textures:
179+
180+| Texture | Look | Key fields |
181+|---|---|---|
182+| `Checker` | two-color checkerboard | `ColorA`, `ColorB`, `Scale` (cells per unit) |
183+| `Gradient` | smooth blend between two colors | `ColorA`, `ColorB`, `Axis` (0=X, 1=Y, 2=Z) |
184+| `ValueNoise` | soft mottling (stone, clouds) | `ColorA`, `ColorB`, `Scale` (feature size), `Seed` |
185+| `Noise` | fine per-point grain (speckle) | `ColorA`, `ColorB`, `Scale` (feature size) |
186+
187+**Tips**:
188+
189+- A texture **replaces** `Diffuse` — the `Diffuse` field is ignored while `Texture` is set. Keep `Specular`, `Shininess`, `Ambient` etc. as normal.
190+- `ValueNoise` is smooth (interpolated); `Noise` is grainy (no interpolation). Use `ValueNoise` for stone or marble, `Noise` for a speckled or sandy look.
191+- Textures work identically in the `Renderer` and the `PathTracer` — the textured color is what bounces as diffuse light, so a red/green checker floor tints the surrounding surfaces in a path-traced scene.
192+- 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`.
193+
194+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).
195+
196+## Level 6 — Path tracing: realistic light
164197
165198 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".
166199
@@ -185,7 +218,7 @@ Differences from the `Renderer`:
185218
186219 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.
187220
188-## Level 6 — Recipes for good-looking images
221+## Level 7 — Recipes for good-looking images
189222
190223 ### The palette
191224
@@ -243,6 +276,6 @@ scene.Lights = []povray.Light{
243276
244277 ## Where to go next
245278
246-- `examples/` — five complete scenes to take apart.
279+- `examples/` — seven complete scenes to take apart.
247280 - `README.md` — API reference.
248-- `ROADMAP.md` — what the library cannot do yet (textures, `.obj` meshes, area lights…).
281+- `ROADMAP.md` — what the library cannot do yet (`.obj` meshes, area lights, normal mapping…).
@@ -14,7 +14,7 @@ package main
14 import (14 import (
15 "log"15 "log"
16 16
17- "github.com/user/povray"17+ "go-povray"
18 )18 )
19 19
20 func main() {20 func main() {
@@ -160,7 +160,40 @@ glass := povray.Material{
160 160
161 See `examples/mirror/`.161 See `examples/mirror/`.
162 162
163-## Level 5 — Path tracing: realistic light163+## Level 5 — Textures: color by position
164+
165+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.
166+
167+```go
168+// Checkerboard floor: two colors, one cell per world unit.
169+floor := povray.Material{Specular: povray.White, Shininess: 20, Ambient: 0.2}
170+floor.Texture = povray.Checker{
171+ ColorA: povray.Color{0.85, 0.85, 0.9},
172+ ColorB: povray.Color{0.08, 0.08, 0.1},
173+ Scale: 1, // cells per unit; 2 = twice as many, smaller cells
174+}
175+scene.AddObject(povray.NewPlaneXY(0), floor)
176+```
177+
178+The built-in textures:
179+
180+| Texture | Look | Key fields |
181+|---|---|---|
182+| `Checker` | two-color checkerboard | `ColorA`, `ColorB`, `Scale` (cells per unit) |
183+| `Gradient` | smooth blend between two colors | `ColorA`, `ColorB`, `Axis` (0=X, 1=Y, 2=Z) |
184+| `ValueNoise` | soft mottling (stone, clouds) | `ColorA`, `ColorB`, `Scale` (feature size), `Seed` |
185+| `Noise` | fine per-point grain (speckle) | `ColorA`, `ColorB`, `Scale` (feature size) |
186+
187+**Tips**:
188+
189+- A texture **replaces** `Diffuse` — the `Diffuse` field is ignored while `Texture` is set. Keep `Specular`, `Shininess`, `Ambient` etc. as normal.
190+- `ValueNoise` is smooth (interpolated); `Noise` is grainy (no interpolation). Use `ValueNoise` for stone or marble, `Noise` for a speckled or sandy look.
191+- Textures work identically in the `Renderer` and the `PathTracer` — the textured color is what bounces as diffuse light, so a red/green checker floor tints the surrounding surfaces in a path-traced scene.
192+- 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`.
193+
194+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).
195+
196+## Level 6 — Path tracing: realistic light
164 197
165 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".198 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".
166 199
@@ -185,7 +218,7 @@ Differences from the `Renderer`:
185 218
186 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.219 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.
187 220
188-## Level 6 — Recipes for good-looking images221+## Level 7 — Recipes for good-looking images
189 222
190 ### The palette223 ### The palette
191 224
@@ -243,6 +276,6 @@ scene.Lights = []povray.Light{
243 276
244 ## Where to go next277 ## Where to go next
245 278
246-- `examples/` — five complete scenes to take apart.279+- `examples/` — seven complete scenes to take apart.
247 - `README.md` — API reference.280 - `README.md` — API reference.
248-- `ROADMAP.md` — what the library cannot do yet (textures, `.obj` meshes, area lights…).281+- `ROADMAP.md` — what the library cannot do yet (`.obj` meshes, area lights, normal mapping…).
modified examples/mirror/main.go +1 -1
@@ -6,7 +6,7 @@ import (
66 "fmt"
77 "log"
88
9- "github.com/user/povray"
9+ "go-povray"
1010 )
1111
1212 func main() {
@@ -6,7 +6,7 @@ import (
6 "fmt"6 "fmt"
7 "log"7 "log"
8 8
9- "github.com/user/povray"9+ "go-povray"
10 )10 )
11 11
12 func main() {12 func main() {
modified examples/pathtrace/main.go +1 -1
@@ -8,7 +8,7 @@ import (
88 "log"
99 "math"
1010
11- "github.com/user/povray"
11+ "go-povray"
1212 )
1313
1414 func main() {
@@ -8,7 +8,7 @@ import (
8 "log"8 "log"
9 "math"9 "math"
10 10
11- "github.com/user/povray"11+ "go-povray"
12 )12 )
13 13
14 func main() {14 func main() {
modified examples/procedural/main.go +1 -1
@@ -8,7 +8,7 @@ import (
88 "log"
99 "math"
1010
11- "github.com/user/povray"
11+ "go-povray"
1212 )
1313
1414 func main() {
@@ -8,7 +8,7 @@ import (
8 "log"8 "log"
9 "math"9 "math"
10 10
11- "github.com/user/povray"11+ "go-povray"
12 )12 )
13 13
14 func main() {14 func main() {
modified examples/spheres/main.go +1 -1
@@ -6,7 +6,7 @@ import (
66 "fmt"
77 "log"
88
9- "github.com/user/povray"
9+ "go-povray"
1010 )
1111
1212 func main() {
@@ -6,7 +6,7 @@ import (
6 "fmt"6 "fmt"
7 "log"7 "log"
8 8
9- "github.com/user/povray"9+ "go-povray"
10 )10 )
11 11
12 func main() {12 func main() {
added examples/textures-big/main.go +106 -0
new file mode 100644
@@ -0,0 +1,106 @@
1+// Command textures-big renders the same texture types as examples/textures
2+// but on large objects, so each pattern reads clearly: a big checkerboard
3+// sphere, a gradient tower, a noise slab, a speckled sphere and a checker
4+// floor with wide cells.
5+package main
6+
7+import (
8+ "fmt"
9+ "log"
10+ "math"
11+
12+ "go-povray"
13+)
14+
15+func main() {
16+ scene := povray.NewScene()
17+ scene.Background = povray.Color{0.03, 0.04, 0.08}
18+ scene.Lights = []povray.Light{
19+ povray.NewLight(povray.Vec3{10, 16, 10}, povray.White),
20+ povray.NewLight(povray.Vec3{-12, 6, -6}, povray.Color{0.5, 0.6, 1.0}),
21+ }
22+ // Two extra fill lights, brighter than the default to offset the
23+ // inverse-square falloff at their distance.
24+ front := povray.NewLight(povray.Vec3{0, 10, 14}, povray.Color{1.0, 0.9, 0.7})
25+ front.Intensity = 3
26+ back := povray.NewLight(povray.Vec3{14, 8, -10}, povray.Color{0.7, 0.9, 1.0})
27+ back.Intensity = 3
28+ scene.Lights = append(scene.Lights, front, back)
29+
30+ // Checkerboard floor with wide cells (0.5 cells per unit = 2-unit cells).
31+ floor := povray.Material{Specular: povray.White, Shininess: 20, Ambient: 0.2}
32+ floor.Texture = povray.Checker{
33+ ColorA: povray.Color{0.85, 0.85, 0.9},
34+ ColorB: povray.Color{0.08, 0.08, 0.1},
35+ Scale: 0.5,
36+ }
37+ scene.AddObject(povray.NewPlaneXY(0), floor)
38+
39+ // A big checkerboard sphere: the pattern wraps the whole ball.
40+ ball := povray.Material{Specular: povray.White, Shininess: 40, Ambient: 0.1}
41+ ball.Texture = povray.Checker{
42+ ColorA: povray.Color{0.9, 0.85, 0.8},
43+ ColorB: povray.Color{0.1, 0.15, 0.4},
44+ Scale: 1,
45+ }
46+ scene.AddObject(povray.Sphere{Center: povray.Vec3{-4, 2.5, -3}, Radius: 2.5}, ball)
47+
48+ // A tall gradient tower: warm at the base, cool at the top.
49+ tower := povray.Material{Specular: povray.White, Shininess: 15, Ambient: 0.2}
50+ tower.Texture = povray.Gradient{
51+ ColorA: povray.Color{0.95, 0.55, 0.15},
52+ ColorB: povray.Color{0.1, 0.2, 0.75},
53+ Axis: 1,
54+ }
55+ scene.AddObject(povray.Box{
56+ Min: povray.Vec3{0.5, 0, -5.5}, Max: povray.Vec3{4.5, 9, -1.5},
57+ }, tower)
58+
59+ // A large noise slab: soft mottling like weathered stone.
60+ slab := povray.Material{Specular: povray.White, Shininess: 12, Ambient: 0.25}
61+ slab.Texture = povray.ValueNoise{
62+ ColorA: povray.Color{0.3, 0.28, 0.27},
63+ ColorB: povray.Color{0.8, 0.76, 0.7},
64+ Scale: 0.8,
65+ Seed: 7.3,
66+ }
67+ scene.AddObject(povray.Box{
68+ Min: povray.Vec3{6, 0, -4.5}, Max: povray.Vec3{11, 4, 0.5},
69+ }, slab)
70+
71+ // A big speckled sphere resting on the floor (fine grain).
72+ dome := povray.Material{Specular: povray.White, Shininess: 30, Ambient: 0.1}
73+ dome.Texture = povray.Noise{
74+ ColorA: povray.Color{0.15, 0.5, 0.85},
75+ ColorB: povray.Color{0.95, 0.95, 0.98},
76+ Scale: 4,
77+ }
78+ scene.AddObject(povray.Sphere{Center: povray.Vec3{2, 1.8, 2}, Radius: 1.8}, dome)
79+
80+ // A ring of large checker columns for depth.
81+ const n = 8
82+ for i := 0; i < n; i++ {
83+ a := 2 * math.Pi * float64(i) / n
84+ x, z := 14*math.Cos(a), 14*math.Sin(a)
85+ col := povray.Material{Specular: povray.White, Shininess: 20, Ambient: 0.15}
86+ col.Texture = povray.Checker{
87+ ColorA: povray.Color{0.75, 0.75, 0.8},
88+ ColorB: povray.Color{0.12, 0.12, 0.15},
89+ Scale: 0.6,
90+ }
91+ scene.AddObject(povray.Box{
92+ Min: povray.Vec3{x - 0.8, 0, z - 0.8}, Max: povray.Vec3{x + 0.8, 5, z + 0.8},
93+ }, col)
94+ }
95+
96+ cam := povray.NewCamera(povray.Vec3{0, 6, 18})
97+ cam.LookAt = povray.Vec3{0, 2.5, -4}
98+ cam.Fov = 55
99+
100+ renderer := povray.Renderer{Scene: scene, Width: 800, Height: 600, Samples: 2}
101+ img := renderer.Render(cam)
102+ if err := img.SavePNG("textures-big.png"); err != nil {
103+ log.Fatal(err)
104+ }
105+ fmt.Println("wrote textures-big.png")
106+}
new file mode 100644
@@ -0,0 +1,106 @@
1+// Command textures-big renders the same texture types as examples/textures
2+// but on large objects, so each pattern reads clearly: a big checkerboard
3+// sphere, a gradient tower, a noise slab, a speckled sphere and a checker
4+// floor with wide cells.
5+package main
6+
7+import (
8+ "fmt"
9+ "log"
10+ "math"
11+
12+ "go-povray"
13+)
14+
15+func main() {
16+ scene := povray.NewScene()
17+ scene.Background = povray.Color{0.03, 0.04, 0.08}
18+ scene.Lights = []povray.Light{
19+ povray.NewLight(povray.Vec3{10, 16, 10}, povray.White),
20+ povray.NewLight(povray.Vec3{-12, 6, -6}, povray.Color{0.5, 0.6, 1.0}),
21+ }
22+ // Two extra fill lights, brighter than the default to offset the
23+ // inverse-square falloff at their distance.
24+ front := povray.NewLight(povray.Vec3{0, 10, 14}, povray.Color{1.0, 0.9, 0.7})
25+ front.Intensity = 3
26+ back := povray.NewLight(povray.Vec3{14, 8, -10}, povray.Color{0.7, 0.9, 1.0})
27+ back.Intensity = 3
28+ scene.Lights = append(scene.Lights, front, back)
29+
30+ // Checkerboard floor with wide cells (0.5 cells per unit = 2-unit cells).
31+ floor := povray.Material{Specular: povray.White, Shininess: 20, Ambient: 0.2}
32+ floor.Texture = povray.Checker{
33+ ColorA: povray.Color{0.85, 0.85, 0.9},
34+ ColorB: povray.Color{0.08, 0.08, 0.1},
35+ Scale: 0.5,
36+ }
37+ scene.AddObject(povray.NewPlaneXY(0), floor)
38+
39+ // A big checkerboard sphere: the pattern wraps the whole ball.
40+ ball := povray.Material{Specular: povray.White, Shininess: 40, Ambient: 0.1}
41+ ball.Texture = povray.Checker{
42+ ColorA: povray.Color{0.9, 0.85, 0.8},
43+ ColorB: povray.Color{0.1, 0.15, 0.4},
44+ Scale: 1,
45+ }
46+ scene.AddObject(povray.Sphere{Center: povray.Vec3{-4, 2.5, -3}, Radius: 2.5}, ball)
47+
48+ // A tall gradient tower: warm at the base, cool at the top.
49+ tower := povray.Material{Specular: povray.White, Shininess: 15, Ambient: 0.2}
50+ tower.Texture = povray.Gradient{
51+ ColorA: povray.Color{0.95, 0.55, 0.15},
52+ ColorB: povray.Color{0.1, 0.2, 0.75},
53+ Axis: 1,
54+ }
55+ scene.AddObject(povray.Box{
56+ Min: povray.Vec3{0.5, 0, -5.5}, Max: povray.Vec3{4.5, 9, -1.5},
57+ }, tower)
58+
59+ // A large noise slab: soft mottling like weathered stone.
60+ slab := povray.Material{Specular: povray.White, Shininess: 12, Ambient: 0.25}
61+ slab.Texture = povray.ValueNoise{
62+ ColorA: povray.Color{0.3, 0.28, 0.27},
63+ ColorB: povray.Color{0.8, 0.76, 0.7},
64+ Scale: 0.8,
65+ Seed: 7.3,
66+ }
67+ scene.AddObject(povray.Box{
68+ Min: povray.Vec3{6, 0, -4.5}, Max: povray.Vec3{11, 4, 0.5},
69+ }, slab)
70+
71+ // A big speckled sphere resting on the floor (fine grain).
72+ dome := povray.Material{Specular: povray.White, Shininess: 30, Ambient: 0.1}
73+ dome.Texture = povray.Noise{
74+ ColorA: povray.Color{0.15, 0.5, 0.85},
75+ ColorB: povray.Color{0.95, 0.95, 0.98},
76+ Scale: 4,
77+ }
78+ scene.AddObject(povray.Sphere{Center: povray.Vec3{2, 1.8, 2}, Radius: 1.8}, dome)
79+
80+ // A ring of large checker columns for depth.
81+ const n = 8
82+ for i := 0; i < n; i++ {
83+ a := 2 * math.Pi * float64(i) / n
84+ x, z := 14*math.Cos(a), 14*math.Sin(a)
85+ col := povray.Material{Specular: povray.White, Shininess: 20, Ambient: 0.15}
86+ col.Texture = povray.Checker{
87+ ColorA: povray.Color{0.75, 0.75, 0.8},
88+ ColorB: povray.Color{0.12, 0.12, 0.15},
89+ Scale: 0.6,
90+ }
91+ scene.AddObject(povray.Box{
92+ Min: povray.Vec3{x - 0.8, 0, z - 0.8}, Max: povray.Vec3{x + 0.8, 5, z + 0.8},
93+ }, col)
94+ }
95+
96+ cam := povray.NewCamera(povray.Vec3{0, 6, 18})
97+ cam.LookAt = povray.Vec3{0, 2.5, -4}
98+ cam.Fov = 55
99+
100+ renderer := povray.Renderer{Scene: scene, Width: 800, Height: 600, Samples: 2}
101+ img := renderer.Render(cam)
102+ if err := img.SavePNG("textures-big.png"); err != nil {
103+ log.Fatal(err)
104+ }
105+ fmt.Println("wrote textures-big.png")
106+}
added examples/textures/main.go +76 -0
new file mode 100644
@@ -0,0 +1,76 @@
1+// Command textures shows position-based textures: a checkerboard floor,
2+// a gradient wall, a noise "stone" box and a speckled sphere, all driven
3+// by a Material's Texture field instead of a single Diffuse color.
4+package main
5+
6+import (
7+ "fmt"
8+ "log"
9+
10+ "go-povray"
11+)
12+
13+func main() {
14+ scene := povray.NewScene()
15+ scene.Background = povray.Color{0.04, 0.05, 0.09}
16+ scene.Lights = []povray.Light{
17+ povray.NewLight(povray.Vec3{6, 10, 8}, povray.White),
18+ povray.NewLight(povray.Vec3{-8, 4, -4}, povray.Color{0.5, 0.6, 1.0}),
19+ }
20+
21+ // Checkerboard floor.
22+ floor := povray.Material{Specular: povray.White, Shininess: 20, Ambient: 0.2}
23+ floor.Texture = povray.Checker{
24+ ColorA: povray.Color{0.85, 0.85, 0.9},
25+ ColorB: povray.Color{0.08, 0.08, 0.1},
26+ Scale: 1,
27+ }
28+ scene.AddObject(povray.NewPlaneXY(0), floor)
29+
30+ // A gradient wall in the back: warm at the bottom, cool at the top.
31+ wall := povray.Material{Ambient: 0.2, Specular: povray.White, Shininess: 10}
32+ wall.Texture = povray.Gradient{
33+ ColorA: povray.Color{0.9, 0.5, 0.2},
34+ ColorB: povray.Color{0.15, 0.25, 0.7},
35+ Axis: 1, // blend along Y (height)
36+ }
37+ scene.AddObject(povray.Box{
38+ Min: povray.Vec3{-10, 0, -9}, Max: povray.Vec3{10, 8, -8},
39+ }, wall)
40+
41+ // A "stone" box with soft noise mottling.
42+ stone := povray.Material{Specular: povray.White, Shininess: 15, Ambient: 0.25}
43+ stone.Texture = povray.ValueNoise{
44+ ColorA: povray.Color{0.35, 0.33, 0.32},
45+ ColorB: povray.Color{0.75, 0.72, 0.68},
46+ Scale: 2,
47+ Seed: 3.7,
48+ }
49+ scene.AddObject(povray.Box{
50+ Min: povray.Vec3{-3.4, 0, -2.4}, Max: povray.Vec3{-1.6, 1.8, -0.6},
51+ }, stone)
52+
53+ // A sphere with a fine speckled texture (Noise).
54+ speck := povray.Material{Specular: povray.White, Shininess: 50, Ambient: 0.1}
55+ speck.Texture = povray.Noise{
56+ ColorA: povray.Color{0.2, 0.6, 0.9},
57+ ColorB: povray.Color{0.9, 0.9, 0.95},
58+ Scale: 12,
59+ }
60+ scene.AddObject(povray.Sphere{Center: povray.Vec3{2.5, 1.1, -1.5}, Radius: 1.1}, speck)
61+
62+ // A plain gold sphere for contrast against the textured ones.
63+ scene.AddObject(povray.Sphere{Center: povray.Vec3{0, 1, 1.5}, Radius: 1},
64+ povray.Material{Diffuse: povray.Gold, Specular: povray.White, Shininess: 60, Ambient: 0.1})
65+
66+ cam := povray.NewCamera(povray.Vec3{0, 3, 9})
67+ cam.LookAt = povray.Vec3{0, 1, -2}
68+ cam.Fov = 55
69+
70+ renderer := povray.Renderer{Scene: scene, Width: 800, Height: 600, Samples: 2}
71+ img := renderer.Render(cam)
72+ if err := img.SavePNG("textures.png"); err != nil {
73+ log.Fatal(err)
74+ }
75+ fmt.Println("wrote textures.png")
76+}
new file mode 100644
@@ -0,0 +1,76 @@
1+// Command textures shows position-based textures: a checkerboard floor,
2+// a gradient wall, a noise "stone" box and a speckled sphere, all driven
3+// by a Material's Texture field instead of a single Diffuse color.
4+package main
5+
6+import (
7+ "fmt"
8+ "log"
9+
10+ "go-povray"
11+)
12+
13+func main() {
14+ scene := povray.NewScene()
15+ scene.Background = povray.Color{0.04, 0.05, 0.09}
16+ scene.Lights = []povray.Light{
17+ povray.NewLight(povray.Vec3{6, 10, 8}, povray.White),
18+ povray.NewLight(povray.Vec3{-8, 4, -4}, povray.Color{0.5, 0.6, 1.0}),
19+ }
20+
21+ // Checkerboard floor.
22+ floor := povray.Material{Specular: povray.White, Shininess: 20, Ambient: 0.2}
23+ floor.Texture = povray.Checker{
24+ ColorA: povray.Color{0.85, 0.85, 0.9},
25+ ColorB: povray.Color{0.08, 0.08, 0.1},
26+ Scale: 1,
27+ }
28+ scene.AddObject(povray.NewPlaneXY(0), floor)
29+
30+ // A gradient wall in the back: warm at the bottom, cool at the top.
31+ wall := povray.Material{Ambient: 0.2, Specular: povray.White, Shininess: 10}
32+ wall.Texture = povray.Gradient{
33+ ColorA: povray.Color{0.9, 0.5, 0.2},
34+ ColorB: povray.Color{0.15, 0.25, 0.7},
35+ Axis: 1, // blend along Y (height)
36+ }
37+ scene.AddObject(povray.Box{
38+ Min: povray.Vec3{-10, 0, -9}, Max: povray.Vec3{10, 8, -8},
39+ }, wall)
40+
41+ // A "stone" box with soft noise mottling.
42+ stone := povray.Material{Specular: povray.White, Shininess: 15, Ambient: 0.25}
43+ stone.Texture = povray.ValueNoise{
44+ ColorA: povray.Color{0.35, 0.33, 0.32},
45+ ColorB: povray.Color{0.75, 0.72, 0.68},
46+ Scale: 2,
47+ Seed: 3.7,
48+ }
49+ scene.AddObject(povray.Box{
50+ Min: povray.Vec3{-3.4, 0, -2.4}, Max: povray.Vec3{-1.6, 1.8, -0.6},
51+ }, stone)
52+
53+ // A sphere with a fine speckled texture (Noise).
54+ speck := povray.Material{Specular: povray.White, Shininess: 50, Ambient: 0.1}
55+ speck.Texture = povray.Noise{
56+ ColorA: povray.Color{0.2, 0.6, 0.9},
57+ ColorB: povray.Color{0.9, 0.9, 0.95},
58+ Scale: 12,
59+ }
60+ scene.AddObject(povray.Sphere{Center: povray.Vec3{2.5, 1.1, -1.5}, Radius: 1.1}, speck)
61+
62+ // A plain gold sphere for contrast against the textured ones.
63+ scene.AddObject(povray.Sphere{Center: povray.Vec3{0, 1, 1.5}, Radius: 1},
64+ povray.Material{Diffuse: povray.Gold, Specular: povray.White, Shininess: 60, Ambient: 0.1})
65+
66+ cam := povray.NewCamera(povray.Vec3{0, 3, 9})
67+ cam.LookAt = povray.Vec3{0, 1, -2}
68+ cam.Fov = 55
69+
70+ renderer := povray.Renderer{Scene: scene, Width: 800, Height: 600, Samples: 2}
71+ img := renderer.Render(cam)
72+ if err := img.SavePNG("textures.png"); err != nil {
73+ log.Fatal(err)
74+ }
75+ fmt.Println("wrote textures.png")
76+}
modified examples/transform/main.go +1 -1
@@ -8,7 +8,7 @@ import (
88 "log"
99 "math"
1010
11- "github.com/user/povray"
11+ "go-povray"
1212 )
1313
1414 func main() {
@@ -8,7 +8,7 @@ import (
8 "log"8 "log"
9 "math"9 "math"
10 10
11- "github.com/user/povray"11+ "go-povray"
12 )12 )
13 13
14 func main() {14 func main() {
modified go.mod +1 -1
@@ -1,3 +1,3 @@
1-module github.com/user/povray
1+module go-povray
22
33 go 1.27.3
@@ -1,3 +1,3 @@
1-module github.com/user/povray1+module go-povray
2 2
3 go 1.27.33 go 1.27.3
modified material.go +13 -0
@@ -48,6 +48,19 @@ type Material struct {
4848 // path tracer; see LightMaterial). Values above 1 make a brighter
4949 // light.
5050 Emission Color
51+ // Texture, if set, replaces the fixed Diffuse color: the surface
52+ // color is looked up at the hit point (see Checker, Gradient,
53+ // ValueNoise, Noise).
54+ Texture Texture
55+}
56+
57+// diffuseAt returns the material's diffuse color at the hit point:
58+// the texture's color when a Texture is set, Diffuse otherwise.
59+func (m Material) diffuseAt(p Vec3) Color {
60+ if m.Texture != nil {
61+ return m.Texture.ColorAt(p)
62+ }
63+ return m.Diffuse
5164 }
5265
5366 // DefaultMaterial is a plain white matte surface.
@@ -48,6 +48,19 @@ type Material struct {
48 // path tracer; see LightMaterial). Values above 1 make a brighter48 // path tracer; see LightMaterial). Values above 1 make a brighter
49 // light.49 // light.
50 Emission Color50 Emission Color
51+ // Texture, if set, replaces the fixed Diffuse color: the surface
52+ // color is looked up at the hit point (see Checker, Gradient,
53+ // ValueNoise, Noise).
54+ Texture Texture
55+}
56+
57+// diffuseAt returns the material's diffuse color at the hit point:
58+// the texture's color when a Texture is set, Diffuse otherwise.
59+func (m Material) diffuseAt(p Vec3) Color {
60+ if m.Texture != nil {
61+ return m.Texture.ColorAt(p)
62+ }
63+ return m.Diffuse
51 }64 }
52 65
53 // DefaultMaterial is a plain white matte surface.66 // DefaultMaterial is a plain white matte surface.
modified pathtracer.go +7 -4
@@ -110,10 +110,13 @@ func (p *PathTracer) trace(ray Ray, depth int, rng *rand.Rand) Color {
110110 // Diffuse: scatter in a cosine-weighted random direction. This is
111111 // what carries light from one surface to another (global
112112 // illumination).
113- if depth < 8 && (mat.Diffuse.R > 0 || mat.Diffuse.G > 0 || mat.Diffuse.B > 0) {
114- d := cosineSample(hit.Normal, rng)
115- dr := Ray{Origin: hit.Point.Add(hit.Normal.Scale(1e-4)), Dir: d}
116- col = col.Add(p.trace(dr, depth+1, rng).Mul(mat.Diffuse))
113+ if depth < 8 {
114+ diffuse := mat.diffuseAt(hit.Point)
115+ if diffuse.R > 0 || diffuse.G > 0 || diffuse.B > 0 {
116+ d := cosineSample(hit.Normal, rng)
117+ dr := Ray{Origin: hit.Point.Add(hit.Normal.Scale(1e-4)), Dir: d}
118+ col = col.Add(p.trace(dr, depth+1, rng).Mul(diffuse))
119+ }
117120 }
118121
119122 // Russian roulette: terminate the path with probability 1-max(col),
@@ -110,10 +110,13 @@ func (p *PathTracer) trace(ray Ray, depth int, rng *rand.Rand) Color {
110 // Diffuse: scatter in a cosine-weighted random direction. This is110 // Diffuse: scatter in a cosine-weighted random direction. This is
111 // what carries light from one surface to another (global111 // what carries light from one surface to another (global
112 // illumination).112 // illumination).
113- if depth < 8 && (mat.Diffuse.R > 0 || mat.Diffuse.G > 0 || mat.Diffuse.B > 0) {113+ if depth < 8 {
114- d := cosineSample(hit.Normal, rng)114+ diffuse := mat.diffuseAt(hit.Point)
115- dr := Ray{Origin: hit.Point.Add(hit.Normal.Scale(1e-4)), Dir: d}115+ if diffuse.R > 0 || diffuse.G > 0 || diffuse.B > 0 {
116- col = col.Add(p.trace(dr, depth+1, rng).Mul(mat.Diffuse))116+ d := cosineSample(hit.Normal, rng)
117+ dr := Ray{Origin: hit.Point.Add(hit.Normal.Scale(1e-4)), Dir: d}
118+ col = col.Add(p.trace(dr, depth+1, rng).Mul(diffuse))
119+ }
117 }120 }
118 121
119 // Russian roulette: terminate the path with probability 1-max(col),122 // Russian roulette: terminate the path with probability 1-max(col),
modified povray_test.go +155 -0
@@ -300,6 +300,161 @@ func TestRenderSmoke(t *testing.T) {
300300 }
301301 }
302302
303+func TestTextureChecker(t *testing.T) {
304+ c := Checker{ColorA: White, ColorB: Black, Scale: 1}
305+ if c.ColorAt(Vec3{0, 0, 0}) != (White) {
306+ t.Errorf("cell (0,0,0) = %v, want white", c.ColorAt(Vec3{0, 0, 0}))
307+ }
308+ if c.ColorAt(Vec3{0.5, 0, 0}) != (White) {
309+ t.Errorf("cell (0,0,0) = %v, want white", c.ColorAt(Vec3{0.5, 0, 0}))
310+ }
311+ // One cell across in any axis flips the color.
312+ if c.ColorAt(Vec3{1.5, 0, 0}) != (Black) {
313+ t.Errorf("cell (1,0,0) = %v, want black", c.ColorAt(Vec3{1.5, 0, 0}))
314+ }
315+ if c.ColorAt(Vec3{0, 1.5, 0}) != (Black) {
316+ t.Errorf("cell (0,1,0) = %v, want black", c.ColorAt(Vec3{0, 1.5, 0}))
317+ }
318+ // Two cells across is the first color again.
319+ if c.ColorAt(Vec3{2.5, 0, 0}) != (White) {
320+ t.Errorf("cell (2,0,0) = %v, want white", c.ColorAt(Vec3{2.5, 0, 0}))
321+ }
322+ // Scale doubles the cell count: the same point is now in the second
323+ // cell.
324+ s := Checker{ColorA: White, ColorB: Black, Scale: 2}
325+ if s.ColorAt(Vec3{0.5, 0, 0}) != (Black) {
326+ t.Errorf("scaled cell (0.5) = %v, want black", s.ColorAt(Vec3{0.5, 0, 0}))
327+ }
328+}
329+
330+func TestTextureGradient(t *testing.T) {
331+ g := Gradient{ColorA: Color{1, 0, 0}, ColorB: Color{0, 0, 1}, Axis: 0}
332+ // At x=-1 it is pure ColorA, at x=+1 pure ColorB, at x=0 the midpoint.
333+ if g.ColorAt(Vec3{-1, 0, 0}) != (Color{1, 0, 0}) {
334+ t.Errorf("x=-1 = %v, want (1,0,0)", g.ColorAt(Vec3{-1, 0, 0}))
335+ }
336+ if g.ColorAt(Vec3{1, 0, 0}) != (Color{0, 0, 1}) {
337+ t.Errorf("x=+1 = %v, want (0,0,1)", g.ColorAt(Vec3{1, 0, 0}))
338+ }
339+ mid := g.ColorAt(Vec3{0, 0, 0})
340+ if math.Abs(mid.R-0.5) > 1e-9 || math.Abs(mid.B-0.5) > 1e-9 || mid.G != 0 {
341+ t.Errorf("x=0 = %v, want (0.5,0,0.5)", mid)
342+ }
343+ // Out of range clamps to the end colors.
344+ if g.ColorAt(Vec3{5, 0, 0}) != (Color{0, 0, 1}) {
345+ t.Errorf("x=5 = %v, want (0,0,1)", g.ColorAt(Vec3{5, 0, 0}))
346+ }
347+}
348+
349+func TestTextureNoise(t *testing.T) {
350+ // ValueNoise is smooth: neighboring points are close, and the color
351+ // always lies between ColorA and ColorB.
352+ n := ValueNoise{ColorA: Black, ColorB: White, Scale: 1}
353+ for i := 0; i < 200; i++ {
354+ p := Vec3{float64(i) * 0.37, float64(i) * 0.11, float64(i) * 0.53}
355+ c := n.ColorAt(p)
356+ if c.R < 0 || c.R > 1 || c.G != c.R || c.B != c.R {
357+ t.Fatalf("noise color out of range or not gray: %v", c)
358+ }
359+ }
360+ // Deterministic: the same point gives the same color.
361+ a := n.ColorAt(Vec3{1.3, 2.1, 0.7})
362+ b := n.ColorAt(Vec3{1.3, 2.1, 0.7})
363+ if a != b {
364+ t.Errorf("noise not deterministic: %v vs %v", a, b)
365+ }
366+ // Smoothness: a small step changes the value only a little.
367+ near := n.ColorAt(Vec3{1.3, 2.1, 0.7})
368+ far := n.ColorAt(Vec3{1.31, 2.1, 0.7})
369+ if math.Abs(near.R-far.R) > 0.2 {
370+ t.Errorf("noise not smooth: %v vs %v", near, far)
371+ }
372+ // Fine Noise also stays in range and is deterministic.
373+ f := Noise{ColorA: Color{0, 0, 0}, ColorB: Color{1, 0, 0}, Scale: 1}
374+ for i := 0; i < 100; i++ {
375+ p := Vec3{float64(i) * 0.5, 0.2, 0.9}
376+ c := f.ColorAt(p)
377+ if c.R < 0 || c.R > 1 || c.G != 0 || c.B != 0 {
378+ t.Fatalf("fine noise out of range: %v", c)
379+ }
380+ }
381+ if f.ColorAt(Vec3{0.3, 0.2, 0.9}) != f.ColorAt(Vec3{0.3, 0.2, 0.9}) {
382+ t.Error("fine noise not deterministic")
383+ }
384+}
385+
386+func TestTextureRender(t *testing.T) {
387+ // A checkerboard floor: the rendered image must show both colors,
388+ // and the fixed-Diffuse version must show only one.
389+ build := func(tex Texture) *Scene {
390+ s := NewScene()
391+ m := Material{Diffuse: Gray, Ambient: 0.3, Specular: White, Shininess: 10}
392+ m.Texture = tex
393+ s.AddObject(NewPlaneXY(0), m)
394+ s.Lights = []Light{NewLight(Vec3{0, 6, 4}, White)}
395+ return s
396+ }
397+ cam := NewCamera(Vec3{0, 2, 6})
398+ cam.Fov = 40
399+
400+ r := Renderer{Scene: build(Checker{ColorA: White, ColorB: Color{0.05, 0.05, 0.05}, Scale: 1}), Width: 64, Height: 48}
401+ img := r.Render(cam)
402+
403+ // Count distinct brightness levels in the lower half (the floor).
404+ var min, max byte
405+ for y := 24; y < 48; y++ {
406+ for x := 0; x < 64; x++ {
407+ v := img.Pix[(y*64+x)*3]
408+ if x == 0 || v < min {
409+ min = v
410+ }
411+ if x == 0 || v > max {
412+ max = v
413+ }
414+ }
415+ }
416+ if max-min < 60 {
417+ t.Errorf("checker floor has little contrast: min=%d max=%d", min, max)
418+ }
419+}
420+
421+func TestTexturePathTracer(t *testing.T) {
422+ // A red/green checker floor lit by a panel must carry both hues into
423+ // the render (the texture color feeds the diffuse bounce).
424+ s := NewScene()
425+ s.Background = Black
426+ m := Material{Diffuse: White}
427+ m.Texture = Checker{ColorA: Color{0.9, 0.1, 0.1}, ColorB: Color{0.1, 0.9, 0.1}, Scale: 1}
428+ s.AddObject(NewPlaneXY(0), m)
429+ panel := Transform{
430+ Object: Box{Min: Vec3{-2, 0, -2}, Max: Vec3{2, 0.1, 2}},
431+ Matrix: Translate(0, 8, 0),
432+ }
433+ s.AddObject(panel, LightMaterial(White, 12))
434+
435+ cam := NewCamera(Vec3{0, 2, 8})
436+ pt := PathTracer{Scene: s, Width: 64, Height: 48, Samples: 8}
437+ img := pt.Render(cam)
438+
439+ // Somewhere on the floor a pixel must be red-dominant and another
440+ // green-dominant.
441+ red, green := false, false
442+ for y := 24; y < 48; y++ {
443+ for x := 0; x < 64; x++ {
444+ i := (y*64 + x) * 3
445+ if img.Pix[i] > img.Pix[i+1]+20 {
446+ red = true
447+ }
448+ if img.Pix[i+1] > img.Pix[i]+20 {
449+ green = true
450+ }
451+ }
452+ }
453+ if !red || !green {
454+ t.Errorf("path-traced checker floor missing a hue: red=%v green=%v", red, green)
455+ }
456+}
457+
303458 func TestSavePNG(t *testing.T) {
304459 img := NewImage(4, 3)
305460 for y := 0; y < 3; y++ {
@@ -300,6 +300,161 @@ func TestRenderSmoke(t *testing.T) {
300 }300 }
301 }301 }
302 302
303+func TestTextureChecker(t *testing.T) {
304+ c := Checker{ColorA: White, ColorB: Black, Scale: 1}
305+ if c.ColorAt(Vec3{0, 0, 0}) != (White) {
306+ t.Errorf("cell (0,0,0) = %v, want white", c.ColorAt(Vec3{0, 0, 0}))
307+ }
308+ if c.ColorAt(Vec3{0.5, 0, 0}) != (White) {
309+ t.Errorf("cell (0,0,0) = %v, want white", c.ColorAt(Vec3{0.5, 0, 0}))
310+ }
311+ // One cell across in any axis flips the color.
312+ if c.ColorAt(Vec3{1.5, 0, 0}) != (Black) {
313+ t.Errorf("cell (1,0,0) = %v, want black", c.ColorAt(Vec3{1.5, 0, 0}))
314+ }
315+ if c.ColorAt(Vec3{0, 1.5, 0}) != (Black) {
316+ t.Errorf("cell (0,1,0) = %v, want black", c.ColorAt(Vec3{0, 1.5, 0}))
317+ }
318+ // Two cells across is the first color again.
319+ if c.ColorAt(Vec3{2.5, 0, 0}) != (White) {
320+ t.Errorf("cell (2,0,0) = %v, want white", c.ColorAt(Vec3{2.5, 0, 0}))
321+ }
322+ // Scale doubles the cell count: the same point is now in the second
323+ // cell.
324+ s := Checker{ColorA: White, ColorB: Black, Scale: 2}
325+ if s.ColorAt(Vec3{0.5, 0, 0}) != (Black) {
326+ t.Errorf("scaled cell (0.5) = %v, want black", s.ColorAt(Vec3{0.5, 0, 0}))
327+ }
328+}
329+
330+func TestTextureGradient(t *testing.T) {
331+ g := Gradient{ColorA: Color{1, 0, 0}, ColorB: Color{0, 0, 1}, Axis: 0}
332+ // At x=-1 it is pure ColorA, at x=+1 pure ColorB, at x=0 the midpoint.
333+ if g.ColorAt(Vec3{-1, 0, 0}) != (Color{1, 0, 0}) {
334+ t.Errorf("x=-1 = %v, want (1,0,0)", g.ColorAt(Vec3{-1, 0, 0}))
335+ }
336+ if g.ColorAt(Vec3{1, 0, 0}) != (Color{0, 0, 1}) {
337+ t.Errorf("x=+1 = %v, want (0,0,1)", g.ColorAt(Vec3{1, 0, 0}))
338+ }
339+ mid := g.ColorAt(Vec3{0, 0, 0})
340+ if math.Abs(mid.R-0.5) > 1e-9 || math.Abs(mid.B-0.5) > 1e-9 || mid.G != 0 {
341+ t.Errorf("x=0 = %v, want (0.5,0,0.5)", mid)
342+ }
343+ // Out of range clamps to the end colors.
344+ if g.ColorAt(Vec3{5, 0, 0}) != (Color{0, 0, 1}) {
345+ t.Errorf("x=5 = %v, want (0,0,1)", g.ColorAt(Vec3{5, 0, 0}))
346+ }
347+}
348+
349+func TestTextureNoise(t *testing.T) {
350+ // ValueNoise is smooth: neighboring points are close, and the color
351+ // always lies between ColorA and ColorB.
352+ n := ValueNoise{ColorA: Black, ColorB: White, Scale: 1}
353+ for i := 0; i < 200; i++ {
354+ p := Vec3{float64(i) * 0.37, float64(i) * 0.11, float64(i) * 0.53}
355+ c := n.ColorAt(p)
356+ if c.R < 0 || c.R > 1 || c.G != c.R || c.B != c.R {
357+ t.Fatalf("noise color out of range or not gray: %v", c)
358+ }
359+ }
360+ // Deterministic: the same point gives the same color.
361+ a := n.ColorAt(Vec3{1.3, 2.1, 0.7})
362+ b := n.ColorAt(Vec3{1.3, 2.1, 0.7})
363+ if a != b {
364+ t.Errorf("noise not deterministic: %v vs %v", a, b)
365+ }
366+ // Smoothness: a small step changes the value only a little.
367+ near := n.ColorAt(Vec3{1.3, 2.1, 0.7})
368+ far := n.ColorAt(Vec3{1.31, 2.1, 0.7})
369+ if math.Abs(near.R-far.R) > 0.2 {
370+ t.Errorf("noise not smooth: %v vs %v", near, far)
371+ }
372+ // Fine Noise also stays in range and is deterministic.
373+ f := Noise{ColorA: Color{0, 0, 0}, ColorB: Color{1, 0, 0}, Scale: 1}
374+ for i := 0; i < 100; i++ {
375+ p := Vec3{float64(i) * 0.5, 0.2, 0.9}
376+ c := f.ColorAt(p)
377+ if c.R < 0 || c.R > 1 || c.G != 0 || c.B != 0 {
378+ t.Fatalf("fine noise out of range: %v", c)
379+ }
380+ }
381+ if f.ColorAt(Vec3{0.3, 0.2, 0.9}) != f.ColorAt(Vec3{0.3, 0.2, 0.9}) {
382+ t.Error("fine noise not deterministic")
383+ }
384+}
385+
386+func TestTextureRender(t *testing.T) {
387+ // A checkerboard floor: the rendered image must show both colors,
388+ // and the fixed-Diffuse version must show only one.
389+ build := func(tex Texture) *Scene {
390+ s := NewScene()
391+ m := Material{Diffuse: Gray, Ambient: 0.3, Specular: White, Shininess: 10}
392+ m.Texture = tex
393+ s.AddObject(NewPlaneXY(0), m)
394+ s.Lights = []Light{NewLight(Vec3{0, 6, 4}, White)}
395+ return s
396+ }
397+ cam := NewCamera(Vec3{0, 2, 6})
398+ cam.Fov = 40
399+
400+ r := Renderer{Scene: build(Checker{ColorA: White, ColorB: Color{0.05, 0.05, 0.05}, Scale: 1}), Width: 64, Height: 48}
401+ img := r.Render(cam)
402+
403+ // Count distinct brightness levels in the lower half (the floor).
404+ var min, max byte
405+ for y := 24; y < 48; y++ {
406+ for x := 0; x < 64; x++ {
407+ v := img.Pix[(y*64+x)*3]
408+ if x == 0 || v < min {
409+ min = v
410+ }
411+ if x == 0 || v > max {
412+ max = v
413+ }
414+ }
415+ }
416+ if max-min < 60 {
417+ t.Errorf("checker floor has little contrast: min=%d max=%d", min, max)
418+ }
419+}
420+
421+func TestTexturePathTracer(t *testing.T) {
422+ // A red/green checker floor lit by a panel must carry both hues into
423+ // the render (the texture color feeds the diffuse bounce).
424+ s := NewScene()
425+ s.Background = Black
426+ m := Material{Diffuse: White}
427+ m.Texture = Checker{ColorA: Color{0.9, 0.1, 0.1}, ColorB: Color{0.1, 0.9, 0.1}, Scale: 1}
428+ s.AddObject(NewPlaneXY(0), m)
429+ panel := Transform{
430+ Object: Box{Min: Vec3{-2, 0, -2}, Max: Vec3{2, 0.1, 2}},
431+ Matrix: Translate(0, 8, 0),
432+ }
433+ s.AddObject(panel, LightMaterial(White, 12))
434+
435+ cam := NewCamera(Vec3{0, 2, 8})
436+ pt := PathTracer{Scene: s, Width: 64, Height: 48, Samples: 8}
437+ img := pt.Render(cam)
438+
439+ // Somewhere on the floor a pixel must be red-dominant and another
440+ // green-dominant.
441+ red, green := false, false
442+ for y := 24; y < 48; y++ {
443+ for x := 0; x < 64; x++ {
444+ i := (y*64 + x) * 3
445+ if img.Pix[i] > img.Pix[i+1]+20 {
446+ red = true
447+ }
448+ if img.Pix[i+1] > img.Pix[i]+20 {
449+ green = true
450+ }
451+ }
452+ }
453+ if !red || !green {
454+ t.Errorf("path-traced checker floor missing a hue: red=%v green=%v", red, green)
455+ }
456+}
457+
303 func TestSavePNG(t *testing.T) {458 func TestSavePNG(t *testing.T) {
304 img := NewImage(4, 3)459 img := NewImage(4, 3)
305 for y := 0; y < 3; y++ {460 for y := 0; y < 3; y++ {
modified renderer.go +6 -5
@@ -120,13 +120,13 @@ func (r *Renderer) trace(ray Ray, depth int) Color {
120120 hit.Flip(ray)
121121 mat := s.Materials[idx]
122122
123- col := r.shade(ray, hit, mat, depth)
123+ col := r.shade(ray, hit, mat, hit.Point, depth)
124124
125125 // Reflection: mirror the ray, keep the surface color as a tint.
126126 if mat.Reflectivity > 0 {
127127 reflect := ray.Dir.Sub(hit.Normal.Scale(2 * ray.Dir.Dot(hit.Normal))).Norm()
128128 rr := Ray{Origin: hit.Point.Add(hit.Normal.Scale(1e-4)), Dir: reflect}
129- col = col.Add(r.trace(rr, depth+1).Mul(mat.Diffuse).Scale(mat.Reflectivity))
129+ col = col.Add(r.trace(rr, depth+1).Mul(mat.diffuseAt(hit.Point)).Scale(mat.Reflectivity))
130130 }
131131
132132 // Refraction: bend the ray through the surface.
@@ -145,9 +145,10 @@ func (r *Renderer) trace(ray Ray, depth int) Color {
145145 }
146146
147147 // shade computes ambient + diffuse + specular with shadows for one hit.
148-func (r *Renderer) shade(ray Ray, hit Hit, mat Material, depth int) Color {
148+func (r *Renderer) shade(ray Ray, hit Hit, mat Material, p Vec3, depth int) Color {
149149 s := r.Scene
150- col := mat.Diffuse.Scale(mat.Ambient)
150+ diffuse := mat.diffuseAt(p)
151+ col := diffuse.Scale(mat.Ambient)
151152 for _, l := range s.Lights {
152153 toLight := l.Position.Sub(hit.Point)
153154 dist := toLight.Len()
@@ -164,7 +165,7 @@ func (r *Renderer) shade(ray Ray, hit Hit, mat Material, depth int) Color {
164165 lc := l.Color.Scale(l.Intensity * fall)
165166
166167 diff := math.Max(0, hit.Normal.Dot(dir))
167- col = col.Add(mat.Diffuse.Mul(lc).Scale(diff))
168+ col = col.Add(diffuse.Mul(lc).Scale(diff))
168169
169170 if mat.Shininess > 0 && diff > 0 {
170171 half := dir.Add(ray.Dir.Neg()).Norm()
@@ -120,13 +120,13 @@ func (r *Renderer) trace(ray Ray, depth int) Color {
120 hit.Flip(ray)120 hit.Flip(ray)
121 mat := s.Materials[idx]121 mat := s.Materials[idx]
122 122
123- col := r.shade(ray, hit, mat, depth)123+ col := r.shade(ray, hit, mat, hit.Point, depth)
124 124
125 // Reflection: mirror the ray, keep the surface color as a tint.125 // Reflection: mirror the ray, keep the surface color as a tint.
126 if mat.Reflectivity > 0 {126 if mat.Reflectivity > 0 {
127 reflect := ray.Dir.Sub(hit.Normal.Scale(2 * ray.Dir.Dot(hit.Normal))).Norm()127 reflect := ray.Dir.Sub(hit.Normal.Scale(2 * ray.Dir.Dot(hit.Normal))).Norm()
128 rr := Ray{Origin: hit.Point.Add(hit.Normal.Scale(1e-4)), Dir: reflect}128 rr := Ray{Origin: hit.Point.Add(hit.Normal.Scale(1e-4)), Dir: reflect}
129- col = col.Add(r.trace(rr, depth+1).Mul(mat.Diffuse).Scale(mat.Reflectivity))129+ col = col.Add(r.trace(rr, depth+1).Mul(mat.diffuseAt(hit.Point)).Scale(mat.Reflectivity))
130 }130 }
131 131
132 // Refraction: bend the ray through the surface.132 // Refraction: bend the ray through the surface.
@@ -145,9 +145,10 @@ func (r *Renderer) trace(ray Ray, depth int) Color {
145 }145 }
146 146
147 // shade computes ambient + diffuse + specular with shadows for one hit.147 // shade computes ambient + diffuse + specular with shadows for one hit.
148-func (r *Renderer) shade(ray Ray, hit Hit, mat Material, depth int) Color {148+func (r *Renderer) shade(ray Ray, hit Hit, mat Material, p Vec3, depth int) Color {
149 s := r.Scene149 s := r.Scene
150- col := mat.Diffuse.Scale(mat.Ambient)150+ diffuse := mat.diffuseAt(p)
151+ col := diffuse.Scale(mat.Ambient)
151 for _, l := range s.Lights {152 for _, l := range s.Lights {
152 toLight := l.Position.Sub(hit.Point)153 toLight := l.Position.Sub(hit.Point)
153 dist := toLight.Len()154 dist := toLight.Len()
@@ -164,7 +165,7 @@ func (r *Renderer) shade(ray Ray, hit Hit, mat Material, depth int) Color {
164 lc := l.Color.Scale(l.Intensity * fall)165 lc := l.Color.Scale(l.Intensity * fall)
165 166
166 diff := math.Max(0, hit.Normal.Dot(dir))167 diff := math.Max(0, hit.Normal.Dot(dir))
167- col = col.Add(mat.Diffuse.Mul(lc).Scale(diff))168+ col = col.Add(diffuse.Mul(lc).Scale(diff))
168 169
169 if mat.Shininess > 0 && diff > 0 {170 if mat.Shininess > 0 && diff > 0 {
170 half := dir.Add(ray.Dir.Neg()).Norm()171 half := dir.Add(ray.Dir.Neg()).Norm()
added texture.go +134 -0
new file mode 100644
@@ -0,0 +1,134 @@
1+package povray
2+
3+import "math"
4+
5+// Texture colors a surface by position: a Material with a Texture set
6+// uses the texture's color at the hit point as its diffuse color
7+// instead of the fixed Diffuse field. Textures are evaluated in world
8+// space, so a texture repeats across every object that shares the
9+// material.
10+type Texture interface {
11+ // ColorAt returns the texture color at the world-space point p.
12+ ColorAt(p Vec3) Color
13+}
14+
15+// Checker is a two-color checkerboard. The cell size is 1/scale in
16+// world units.
17+type Checker struct {
18+ ColorA, ColorB Color
19+ // Scale is the number of cells per world unit (default 1).
20+ Scale float64
21+}
22+
23+func (c Checker) ColorAt(p Vec3) Color {
24+ s := c.Scale
25+ if s <= 0 {
26+ s = 1
27+ }
28+ a := int(math.Floor(p.X*s)) + int(math.Floor(p.Y*s)) + int(math.Floor(p.Z*s))
29+ if a%2 == 0 {
30+ return c.ColorA
31+ }
32+ return c.ColorB
33+}
34+
35+// Gradient blends from ColorA to ColorB along one axis of the world
36+// (0 = X, 1 = Y, 2 = Z).
37+type Gradient struct {
38+ ColorA, ColorB Color
39+ // Axis selects the direction of the blend (0 = X, 1 = Y, 2 = Z).
40+ Axis int
41+}
42+
43+func (g Gradient) ColorAt(p Vec3) Color {
44+ v := [3]float64{p.X, p.Y, p.Z}[g.Axis]
45+ t := (v + 1) / 2 // 0 at -1, 1 at +1, clamped outside
46+ if t < 0 {
47+ t = 0
48+ }
49+ if t > 1 {
50+ t = 1
51+ }
52+ return g.ColorA.Scale(1 - t).Add(g.ColorB.Scale(t))
53+}
54+
55+// ValueNoise is smooth random color between ColorA and ColorB: a
56+// lattice of random values, trilinearly interpolated with a smoothstep,
57+// so it looks like soft mottling rather than grain.
58+type ValueNoise struct {
59+ ColorA, ColorB Color
60+ // Scale is the feature size in world units (default 1: one cell per
61+ // unit).
62+ Scale float64
63+ // Seed shifts the lattice (any value; 0 is fine).
64+ Seed float64
65+}
66+
67+func (n ValueNoise) ColorAt(p Vec3) Color {
68+ s := n.Scale
69+ if s <= 0 {
70+ s = 1
71+ }
72+ x, y, z := p.X*s, p.Y*s, p.Z*s
73+ xi, yi, zi := int(math.Floor(x)), int(math.Floor(y)), int(math.Floor(z))
74+ fx, fy, fz := x-float64(xi), y-float64(yi), z-float64(zi)
75+ fx = smoothstep(fx)
76+ fy = smoothstep(fy)
77+ fz = smoothstep(fz)
78+
79+ lerp3 := func(a, b float64, t float64) float64 { return a + (b-a)*t }
80+ c000 := latticeNoise(xi, yi, zi, n.Seed)
81+ c100 := latticeNoise(xi+1, yi, zi, n.Seed)
82+ c010 := latticeNoise(xi, yi+1, zi, n.Seed)
83+ c110 := latticeNoise(xi+1, yi+1, zi, n.Seed)
84+ c001 := latticeNoise(xi, yi, zi+1, n.Seed)
85+ c101 := latticeNoise(xi+1, yi, zi+1, n.Seed)
86+ c011 := latticeNoise(xi, yi+1, zi+1, n.Seed)
87+ c111 := latticeNoise(xi+1, yi+1, zi+1, n.Seed)
88+
89+ c00 := lerp3(c000, c100, fx)
90+ c10 := lerp3(c010, c110, fx)
91+ c01 := lerp3(c001, c101, fx)
92+ c11 := lerp3(c011, c111, fx)
93+ c0 := lerp3(c00, c10, fy)
94+ c1 := lerp3(c01, c11, fy)
95+ v := lerp3(c0, c1, fz)
96+
97+ return n.ColorA.Scale(1 - v).Add(n.ColorB.Scale(v))
98+}
99+
100+// Noise is fine per-point grain: a pseudorandom color between ColorA
101+// and ColorB that changes at every point. Useful for a speckled look;
102+// ValueNoise is smoother.
103+type Noise struct {
104+ ColorA, ColorB Color
105+ // Scale is the feature size in world units (default 1).
106+ Scale float64
107+}
108+
109+func (n Noise) ColorAt(p Vec3) Color {
110+ s := n.Scale
111+ if s <= 0 {
112+ s = 1
113+ }
114+ x, y, z := p.X*s, p.Y*s, p.Z*s
115+ xi, yi, zi := int(math.Floor(x)), int(math.Floor(y)), int(math.Floor(z))
116+ fx, fy, fz := x-float64(xi), y-float64(yi), z-float64(zi)
117+ v := latticeNoise(xi, yi, zi, fx*101+fy*131+fz*17)
118+ return n.ColorA.Scale(1 - v).Add(n.ColorB.Scale(v))
119+}
120+
121+// latticeNoise returns a deterministic pseudorandom value in [0,1) for
122+// the lattice cell (xi, yi, zi), shifted by seed.
123+func latticeNoise(xi, yi, zi int, seed float64) float64 {
124+ h := float64(xi*374761393+yi*668265263+zi*2147483629) + seed
125+ h = math.Mod(h, 1e9)
126+ if h < 0 {
127+ h += 1e9
128+ }
129+ return h / 1e9
130+}
131+
132+func smoothstep(t float64) float64 {
133+ return t * t * (3 - 2*t)
134+}
new file mode 100644
@@ -0,0 +1,134 @@
1+package povray
2+
3+import "math"
4+
5+// Texture colors a surface by position: a Material with a Texture set
6+// uses the texture's color at the hit point as its diffuse color
7+// instead of the fixed Diffuse field. Textures are evaluated in world
8+// space, so a texture repeats across every object that shares the
9+// material.
10+type Texture interface {
11+ // ColorAt returns the texture color at the world-space point p.
12+ ColorAt(p Vec3) Color
13+}
14+
15+// Checker is a two-color checkerboard. The cell size is 1/scale in
16+// world units.
17+type Checker struct {
18+ ColorA, ColorB Color
19+ // Scale is the number of cells per world unit (default 1).
20+ Scale float64
21+}
22+
23+func (c Checker) ColorAt(p Vec3) Color {
24+ s := c.Scale
25+ if s <= 0 {
26+ s = 1
27+ }
28+ a := int(math.Floor(p.X*s)) + int(math.Floor(p.Y*s)) + int(math.Floor(p.Z*s))
29+ if a%2 == 0 {
30+ return c.ColorA
31+ }
32+ return c.ColorB
33+}
34+
35+// Gradient blends from ColorA to ColorB along one axis of the world
36+// (0 = X, 1 = Y, 2 = Z).
37+type Gradient struct {
38+ ColorA, ColorB Color
39+ // Axis selects the direction of the blend (0 = X, 1 = Y, 2 = Z).
40+ Axis int
41+}
42+
43+func (g Gradient) ColorAt(p Vec3) Color {
44+ v := [3]float64{p.X, p.Y, p.Z}[g.Axis]
45+ t := (v + 1) / 2 // 0 at -1, 1 at +1, clamped outside
46+ if t < 0 {
47+ t = 0
48+ }
49+ if t > 1 {
50+ t = 1
51+ }
52+ return g.ColorA.Scale(1 - t).Add(g.ColorB.Scale(t))
53+}
54+
55+// ValueNoise is smooth random color between ColorA and ColorB: a
56+// lattice of random values, trilinearly interpolated with a smoothstep,
57+// so it looks like soft mottling rather than grain.
58+type ValueNoise struct {
59+ ColorA, ColorB Color
60+ // Scale is the feature size in world units (default 1: one cell per
61+ // unit).
62+ Scale float64
63+ // Seed shifts the lattice (any value; 0 is fine).
64+ Seed float64
65+}
66+
67+func (n ValueNoise) ColorAt(p Vec3) Color {
68+ s := n.Scale
69+ if s <= 0 {
70+ s = 1
71+ }
72+ x, y, z := p.X*s, p.Y*s, p.Z*s
73+ xi, yi, zi := int(math.Floor(x)), int(math.Floor(y)), int(math.Floor(z))
74+ fx, fy, fz := x-float64(xi), y-float64(yi), z-float64(zi)
75+ fx = smoothstep(fx)
76+ fy = smoothstep(fy)
77+ fz = smoothstep(fz)
78+
79+ lerp3 := func(a, b float64, t float64) float64 { return a + (b-a)*t }
80+ c000 := latticeNoise(xi, yi, zi, n.Seed)
81+ c100 := latticeNoise(xi+1, yi, zi, n.Seed)
82+ c010 := latticeNoise(xi, yi+1, zi, n.Seed)
83+ c110 := latticeNoise(xi+1, yi+1, zi, n.Seed)
84+ c001 := latticeNoise(xi, yi, zi+1, n.Seed)
85+ c101 := latticeNoise(xi+1, yi, zi+1, n.Seed)
86+ c011 := latticeNoise(xi, yi+1, zi+1, n.Seed)
87+ c111 := latticeNoise(xi+1, yi+1, zi+1, n.Seed)
88+
89+ c00 := lerp3(c000, c100, fx)
90+ c10 := lerp3(c010, c110, fx)
91+ c01 := lerp3(c001, c101, fx)
92+ c11 := lerp3(c011, c111, fx)
93+ c0 := lerp3(c00, c10, fy)
94+ c1 := lerp3(c01, c11, fy)
95+ v := lerp3(c0, c1, fz)
96+
97+ return n.ColorA.Scale(1 - v).Add(n.ColorB.Scale(v))
98+}
99+
100+// Noise is fine per-point grain: a pseudorandom color between ColorA
101+// and ColorB that changes at every point. Useful for a speckled look;
102+// ValueNoise is smoother.
103+type Noise struct {
104+ ColorA, ColorB Color
105+ // Scale is the feature size in world units (default 1).
106+ Scale float64
107+}
108+
109+func (n Noise) ColorAt(p Vec3) Color {
110+ s := n.Scale
111+ if s <= 0 {
112+ s = 1
113+ }
114+ x, y, z := p.X*s, p.Y*s, p.Z*s
115+ xi, yi, zi := int(math.Floor(x)), int(math.Floor(y)), int(math.Floor(z))
116+ fx, fy, fz := x-float64(xi), y-float64(yi), z-float64(zi)
117+ v := latticeNoise(xi, yi, zi, fx*101+fy*131+fz*17)
118+ return n.ColorA.Scale(1 - v).Add(n.ColorB.Scale(v))
119+}
120+
121+// latticeNoise returns a deterministic pseudorandom value in [0,1) for
122+// the lattice cell (xi, yi, zi), shifted by seed.
123+func latticeNoise(xi, yi, zi int, seed float64) float64 {
124+ h := float64(xi*374761393+yi*668265263+zi*2147483629) + seed
125+ h = math.Mod(h, 1e9)
126+ if h < 0 {
127+ h += 1e9
128+ }
129+ return h / 1e9
130+}
131+
132+func smoothstep(t float64) float64 {
133+ return t * t * (3 - 2*t)
134+}