From 362ed0802cb311bb16c5ffb7a05775b9ba4dc3ad Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Fri, 2 Aug 2024 00:56:29 -0400 Subject: [PATCH] Really dumb functions --- renderer1/hrend/math.go | 16 ++++++++++++++++ renderer1/main.go | 42 +++++++---------------------------------- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/renderer1/hrend/math.go b/renderer1/hrend/math.go index 661899b..aaa433d 100644 --- a/renderer1/hrend/math.go +++ b/renderer1/hrend/math.go @@ -158,6 +158,22 @@ func (m *Mat44f) SetRotationZ(radang float32) { m[2] = -m[4] } +// TODO: need roll too +func (m *Mat44f) SetModel(loc *Vec3f, yaw float32, pitch float32, scale float32, up *Vec3f) Vec3f { + // Use sphere equation to compute lookat vector through the two + // player-controled angles (pitch and yaw) + lookvec := Vec3f{ + Z: float32(-math.Sin(float64(pitch)) * math.Cos(float64(yaw))), + X: float32(math.Sin(float64(pitch)) * math.Sin(float64(yaw))), + Y: float32(math.Cos(float64(pitch))), + } + m.SetLookAt(loc, loc.Add(&lookvec), up) + m.Set(0, 0, m.Get(0, 0)*scale) + m.Set(1, 1, m.Get(1, 1)*scale) + m.Set(2, 2, m.Get(2, 2)*scale) + return lookvec +} + // Note: use {0,1,0} for up for normal use func (m *Mat44f) SetLookAt(from *Vec3f, to *Vec3f, up *Vec3f) { forward := from.Sub(to).Normalize() diff --git a/renderer1/main.go b/renderer1/main.go index f7f5d2c..369b7df 100644 --- a/renderer1/main.go +++ b/renderer1/main.go @@ -94,12 +94,6 @@ func main() { rb := hrend.NewRenderbuffer(fb, Width, Height) o, texture := loadDefault() - // light := hrend.Vec3f{ - // X: 0, - // Y: 0, - // Z: -1, - // } - var thing hrend.Vec2i log.Print(thing) @@ -118,19 +112,11 @@ func main() { camtrans := hrend.Vec3f{X: 0, Y: 0, Z: ZOffset} camup := hrend.Vec3f{X: 0, Y: 1, Z: 0} - lookvec := hrend.Vec3f{X: 0, Y: 0, Z: -1} // In our system, 0 degree yaw is facing -Z, into the scene yaw := float32(0) pitch := float32(math.Pi / 2) // Start looking flat - // in all three directions... I think?? - //yrot := float32(0.0) - - //var camrotation, camtrans hrend.Mat44f - //camrotation.SetIdentity() - //var camrotx, camroty, camrotz hrend.Mat44f - //var camrotx, camroty hrend.Mat44f - var camera hrend.Mat44f + var camera, moverot hrend.Mat44f for !rl.WindowShouldClose() { @@ -156,7 +142,7 @@ func main() { newcamtrans.X -= Movement / Fps } // Moving forward moves in the negative z direction, since we FACE - // the -z axis + // the -z axis (the camera does anyway) if rl.IsKeyDown(rl.KeyW) { newcamtrans.Z -= Movement / Fps } @@ -170,23 +156,14 @@ func main() { newcamtrans.Y -= Movement / Fps } - lookvec.Z = float32(-math.Sin(float64(pitch)) * math.Cos(float64(yaw))) - lookvec.X = float32(math.Sin(float64(pitch)) * math.Sin(float64(yaw))) - lookvec.Y = float32(math.Cos(float64(pitch))) - //camrotx.SetRotationX(rotvec.X) - //camroty.SetRotationY(rotvec.Y) - - //lookvec := camroty.Multiply(&camrotx).MultiplyPoint3(baselook) + // translate the new camera movement based on the yaw + moverot.SetRotationY(-yaw) + newcamtrans = moverot.MultiplyPoint3(newcamtrans) camtrans = *camtrans.Add(&newcamtrans) - //camrotz.SetRotationZ(zrot) - //camrotation = *camrotation.Multiply( - // This might (thought not currently) - //camtrans.SetTranslation(xofs, yofs, zofs) + // The camera is really just a model + lookvec := camera.SetModel(&camtrans, yaw, pitch, 1.0, &camup) - camera.SetLookAt(&camtrans, camtrans.Add(&lookvec), &camup) - - //screenmat := camrotx.Multiply(&camroty).Multiply(&camrotz).Multiply(&camtrans).Inverse().Multiply(&projection) screenmat := camera.Inverse().Multiply(&projection) screenmat = screenmat.Multiply(&viewport) @@ -195,19 +172,15 @@ func main() { fb.Data[i].G = 0 fb.Data[i].B = 0 } - //rl.ImageDrawRectangle(fb.Image, 0, 0, Width, Height, rl.Black) - //rl.ImageClearBackground(fb.Image, rl.Black) var sc [3]hrend.Vertex rb.ResetZBuffer() for _, f := range o.Faces { // Precompute perspective for vertices to save time. Notice Z // is not considered: is this orthographic projection? Yeah probably... - //var fpt [3]hrend.Vec3f for i := range 3 { // Triangles, bro sc[i] = f[i] sc[i].Pos = screenmat.MultiplyPoint3(f[i].Pos) - //fpt[i] = worldToCamera.MultiplyPoint3(f[i].Pos) } l1 := f[2].Pos.Sub(&f[0].Pos) @@ -217,7 +190,6 @@ func main() { if intensity < 0 { intensity = 0 } - //intensity := float32(1.0) hrend.TriangleTextured(&rb, texture, intensity, sc[0], sc[1], sc[2]) //hrend.TriangleFlat(&rb, hrend.Col2Uint(byte(255*intensity), byte(255*intensity), byte(255*intensity)), sc[0].Pos, sc[1].Pos, sc[2].Pos) }