From bf6560bb2007581892932a367f4319f2f2a01919 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Thu, 27 Aug 2026 11:23:39 +0200 Subject: Make old project runnable on Go 1.27 with mise and README - init go.mod (Go 1.27, progressbar/v3 v3.19.1) and fix import - fix FMin returning max, remove deprecated RenderBlank, apply go fix range loops - add mise.toml (go 1.27, tasks: build/run/check with go fix -diff/clean) - add focused README and update .gitignore for build artifacts --- .gitignore | 4 +++- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 12 ++++++++++++ go.sum | 22 ++++++++++++++++++++++ main.go | 9 ++++----- mise.toml | 23 +++++++++++++++++++++++ utils.go | 2 +- 7 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 mise.toml diff --git a/.gitignore b/.gitignore index f90281d..563d89d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ frames -.DS_Store \ No newline at end of file +.DS_Store +img.png +goraytracer diff --git a/README.md b/README.md new file mode 100644 index 0000000..6999bd6 --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# GoRayTracer + +CPU ray tracer in Go — a port of Peter Shirley's *Ray Tracing in One Weekend*. Renders spheres with material scattering and outputs a PNG. + +## What it does + +- **Geometry:** `Vec3D`/`Ray`, `Sphere` + `HitableList` (closest-hit search) — `hitable.go:19`, `camera.go:12` +- **Camera:** pinhole with vertical FOV (90° by default) and aspect ratio — `camera.go:12` +- **Materials:** `Lambertian` (diffuse), `Metal` (fuzzed reflection), `Dielectric` (glass, Schlick approximation) — `material.go:13` +- **Rendering:** recursive `RayColor` with `MaxDepth=50`, `RaysPerPixel=10` stochastic anti-aliasing, gamma correction (`sqrt`) — `color.go:34` +- **Output:** `Frame [1000][2000]Pixel` sky gradient background, saved as PNG via `image/png` — `image.go:19` + +Default scene (`main.go:35`): two `Lambertian` spheres (blue/red, radius `cos(π/4)`). Two alternative scenes are commented in `main.go:40` (metal/glass + ground sphere, and an animation loop). + +Constants in `main.go:11`: `Width=2000`, `Height=1000`, `TMin=0.001`, `TMax=1000`, `RaysPerPixel=10`, `MaxDepth=50`. + +## How to run + +Prerequisite: [mise](https://mise.jdx.dev/) (installs Go automatically) or Go 1.27+. + +```sh +# with mise (recommended) +mise install # installs Go 1.27 +mise run run # go run . → writes img.png (~5s, 2000×1000) +mise run build # builds ./goraytracer +./goraytracer # same as above + +# with Go directly +go mod tidy +go run . # → img.png +go build -o goraytracer . && ./goraytracer +``` + +Output is `img.png` (2000×1000 PNG, excluded by `.gitignore`). Check/lint: + +```sh +mise run check # go fmt + go fix -diff + go vet + go build +``` + +## Customizing + +Edit `main.go` to swap the `World` definition (uncomment the 4-sphere or animation blocks), or adjust `Width`/`Height`/`RaysPerPixel` for faster previews (e.g. 400×200). diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d32608b --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module goraytracer + +go 1.27 + +require github.com/schollz/progressbar/v3 v3.19.1 + +require ( + github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect + github.com/rivo/uniseg v0.4.7 // indirect + golang.org/x/sys v0.46.0 // indirect + golang.org/x/term v0.44.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8247f11 --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM= +github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/schollz/progressbar/v3 v3.19.1 h1:iv8BgwOvdML/S3p84uBpy/IMigv4U9594vPZYa2EdrU= +github.com/schollz/progressbar/v3 v3.19.1/go.mod h1:LFL7jqimKxfhero4K1eCkUr/6R39AgQeiPCJtlTWIW8= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= +golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc= +golang.org/x/term v0.44.0/go.mod h1:7ze4MdzUzLXpSAoFP1H0bOI9aXDqveSvatT5vKcFh2Y= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 957fc92..1330e74 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ import ( "math" "math/rand" - "github.com/schollz/progressbar" + "github.com/schollz/progressbar/v3" ) const ( @@ -55,14 +55,13 @@ func main() { // move := Vec3D{2.0, 0.0, 0.0} frame := Frame{} bar := progressbar.New(Height) - bar.RenderBlank() // for k := 0; k < FPS*DURATION; k++ { // origin := AddVec3D(start, ScalarProduct(float64(k)/float64(FPS*DURATION), move)) // camera.origin = origin - for x := 0; x < Height; x++ { - for y := 0; y < Width; y++ { + for x := range Height { + for y := range Width { var colorList [RaysPerPixel]Color - for it := 0; it < RaysPerPixel; it++ { + for it := range RaysPerPixel { u := (float64(y) + rand.Float64()) / float64(Width) v := (float64(x) + rand.Float64()) / float64(Height) ray := camera.GetRay(u, v) diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..bfa3c33 --- /dev/null +++ b/mise.toml @@ -0,0 +1,23 @@ +[tools] +go = "1.27" + +[tasks.build] +description = "Build the ray tracer binary" +run = "go build -o goraytracer ." + +[tasks.run] +description = "Render the default scene to img.png (2000x1000, ~5s)" +run = "go run ." + +[tasks.check] +description = "Format, vet, apply go fix, and build" +run = [ + "go fmt ./...", + "go fix -diff ./...", + "go vet ./...", + "go build ./...", +] + +[tasks.clean] +description = "Remove rendered image and binary" +run = "rm -f img.png goraytracer" diff --git a/utils.go b/utils.go index a0d0e7c..a6c5619 100644 --- a/utils.go +++ b/utils.go @@ -4,7 +4,7 @@ import "math" // FMin return mins of floats func FMin(a float64, b float64) float64 { - if a >= b { + if a < b { return a } return b -- cgit v1.3