From 0d12bc4156f4065ce10dc1a3ad56c010d32e4794 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Wed, 24 Jul 2024 19:18:42 -0400 Subject: [PATCH] Animations! --- tinyrender1_perspective/animation.sh | 24 ++++++++++++++++++++++++ tinyrender1_perspective/main.go | 17 +++++++++++------ 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100755 tinyrender1_perspective/animation.sh diff --git a/tinyrender1_perspective/animation.sh b/tinyrender1_perspective/animation.sh new file mode 100755 index 0000000..77b7c4f --- /dev/null +++ b/tinyrender1_perspective/animation.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +if [ $# -ne 1 ]; then + echo "You must pass the basename for the animation folder" + exit 1 +fi + +echo "Building" +go build +mkdir -p $1 + +echo "Running" + +frame=0 +for x in $(seq -3 0.1 3); do + ff=$(printf "%03d" $frame) + ./tinyrender1 "-xofs=$x" "-zofs=-1.8" "-fov=70" >"$1/$ff.ppm" + frame=$((frame + 1)) +done + +echo "Converting animation" + +cd $1 +convert -delay 5 -loop 0 *.ppm -resize 256x256 anim.gif diff --git a/tinyrender1_perspective/main.go b/tinyrender1_perspective/main.go index 2d41872..b9d50db 100644 --- a/tinyrender1_perspective/main.go +++ b/tinyrender1_perspective/main.go @@ -11,11 +11,11 @@ import ( const ( Width = 512 Height = 512 - FOV = 110 + FOV = 70 NearClip = 0.1 - FarClip = 2 // Because the head is so small and close + FarClip = 5 // Because the head is so small and close ObjectFile = "head.obj" - Repeat = 1000 + Repeat = 1 // Because I want to see an animation ) func must(err error) { @@ -26,6 +26,9 @@ func must(err error) { // However flag works... idk var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") +var xofs = flag.Float64("xofs", 0, "Offset image by x") +var zofs = flag.Float64("zofs", -1.5, "Offset image by z (should be negative)") +var fov = flag.Float64("fov", 90, "Field of view") func main() { log.Printf("Program start") @@ -59,9 +62,10 @@ func main() { var projection Mat44f var worldToCamera Mat44f - projection.SetProjection(FOV, NearClip, FarClip) + projection.SetProjection(float32(*fov), NearClip, FarClip) worldToCamera.SetIdentity() - worldToCamera.Set(2, 3, -1) // Get farther away from the face + worldToCamera.Set(0, 3, float32(*xofs)) // Let user decide how to offset x + worldToCamera.Set(2, 3, float32(*zofs)) // Get farther away from the face (user) var x [3]int var y [3]int @@ -78,7 +82,8 @@ func main() { fp = projection.MultiplyPoint3(fp) x[i] = int((fp.X + 1) * halfwidth) y[i] = hi - int((fp.Y+1)*halfheight) - c[i] = Col2Uint(byte(0xFF*fp.Z), byte(0xFF*fp.Z), byte(0xFF*fp.Z)) + c[i] = 0xFFFFFF + //c[i] = Col2Uint(byte(0xFF*fp.Z), byte(0xFF*fp.Z), byte(0xFF*fp.Z)) } Bresenham2(&fb, c[0], x[0], y[0], x[1], y[1]) Bresenham2(&fb, c[1], x[1], y[1], x[2], y[2])