// ScreenTransf returns a new matrix that transforms vectors after projection // to screen coordinates. The upper left corner of the near rectangle will be // (0,0) and the bottom right will be (w,h). If the aspect ratio does not match // the camera the image will be distorted. func ScreenTransf(f *Frustum, w, h int) *geom.Mat4 { wf := float64(w) hf := float64(h) t := TranslTransf(&geom.Vec3{f.Nwidth / 2, -f.Nheight / 2, 0}) m := geom.Mat4{ wf / f.Nwidth, 0, 0, 0, 0, -hf / f.Nheight, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, } m.Mul(t) return &m }
// Transf transforms a 3D triangle with the given transformation matrix and // returns a 2D triangle. func (t *Tri4) Transf(m *geom.Mat4) *Tri2 { p1 := m.Transf(&t[0]) x1 := tmath.Round(p1[0] / p1[3]) y1 := tmath.Round(p1[1] / p1[3]) p2 := m.Transf(&t[1]) x2 := tmath.Round(p2[0] / p2[3]) y2 := tmath.Round(p2[1] / p2[3]) p3 := m.Transf(&t[2]) x3 := tmath.Round(p3[0] / p3[3]) y3 := tmath.Round(p3[1] / p3[3]) return NewTri2(x1, y1, x2, y2, x3, y3) }