diff --git a/renderer2/hrend/math.go b/renderer2/hrend/math.go index 972aaf7..aeeddff 100644 --- a/renderer2/hrend/math.go +++ b/renderer2/hrend/math.go @@ -149,6 +149,13 @@ func (m *Mat44f) SetViewport(tl Vec3f, br Vec3f) { //width, height, depth int) { //m.Set(2, 3, (br.Z+tl.Z)/2) } +// Convert the point to a viewport point +func (v *Vec3f) ViewportSelf(width, height int) { + v.X = (v.X + 1) / 2 * float32(width) + v.Y = (1 - (v.Y+1)/2) * float32(height) + // Don't touch Z +} + func (m *Mat44f) SetViewportSimple(width, height, depth int) { var tl Vec3f // All zero br := Vec3f{ diff --git a/renderer2/main.go b/renderer2/main.go index 8b0b460..8e09d6e 100644 --- a/renderer2/main.go +++ b/renderer2/main.go @@ -210,9 +210,9 @@ func main() { objects[len(objects)-1].Pos.Z -= 2 // These don't really change - var projection, viewport hrend.Mat44f + var projection hrend.Mat44f //, viewport hrend.Mat44f projection.SetProjection(float32(*fov), float32(Width)/float32(Height), NearClip, FarClip) - viewport.SetViewportSimple(int(Width), int(Height), 1) //65535) + //viewport.SetViewportSimple(int(Width), int(Height), 1) //65535) var camera hrend.Mat44f var newcamtrans hrend.Vec3f @@ -234,7 +234,7 @@ func main() { camtrans = *camtrans.Add(&newcamtrans) _ = camera.SetCamera(&camtrans, yaw, pitch, &camup) screenmat := camera.Inverse().Multiply(&projection) - screenmat = screenmat.Multiply(&viewport) + //screenmat = screenmat.Multiply(&viewport) rb.ResetZBuffer() for y := range Height { @@ -253,7 +253,6 @@ func main() { modelmat.SetLookAt(&o.Pos, o.Pos.Add(&o.LookVec), &camup) modelmat.ScaleSelf(o.Scale) matrix3d := modelmat.Multiply(screenmat) - //matrix3d.ScaleSelf(o.Scale) for _, f := range o.Model.Faces { for i := range 3 { sc[i] = f[i] @@ -264,6 +263,10 @@ func main() { log.Print(o.Model.Faces[0][0].Pos, o.Model.Faces[0][1].Pos, o.Model.Faces[0][2].Pos) log.Print(sc[0].Pos, sc[1].Pos, sc[2].Pos) log.Print(matrix3d) + for i := range 3 { + // Perspective divide (?) and screen coord + sc[i].Pos.ViewportSelf(*width, *height) + } //log.Print(sc[0].Pos, sc[1].Pos, sc[2].Pos, matrix3d) if o.Lighting { l1 := f[2].Pos.Sub(&f[0].Pos)