Kaynağa Gözat

camera improvements

Daniel Salvadori 7 yıl önce
ebeveyn
işleme
91dd3559c3
2 değiştirilmiş dosya ile 11 ekleme ve 7 silme
  1. 4 6
      camera/camera.go
  2. 7 1
      camera/perspective.go

+ 4 - 6
camera/camera.go

@@ -38,12 +38,11 @@ func (cam *Camera) Initialize() {
 	cam.SetDirection(0, 0, -1)
 }
 
-// LookAt sets the camera target position
-// TODO - maybe move to Node, or create similar in Node
+// LookAt rotates the camera to look at the specified target position.
+// This method does not support objects with rotated and/or translated parent(s).
+// TODO: maybe move method to Node, or create similar in Node.
 func (cam *Camera) LookAt(target *math32.Vector3) {
 
-	// This method does not support objects with rotated and/or translated parent(s)
-
 	cam.target = *target
 
 	var rotMat math32.Matrix4
@@ -54,7 +53,6 @@ func (cam *Camera) LookAt(target *math32.Vector3) {
 	var q math32.Quaternion
 	q.SetFromRotationMatrix(&rotMat)
 	cam.SetQuaternionQuat(&q)
-
 }
 
 // GetCamera satisfies the ICamera interface
@@ -79,7 +77,7 @@ func (cam *Camera) Up() math32.Vector3 {
 func (cam *Camera) SetUp(up *math32.Vector3) {
 
 	cam.up = *up
-	cam.LookAt(&cam.target)
+	cam.LookAt(&cam.target) // TODO Maybe remove and let user call LookAt explicitly
 }
 
 // ViewMatrix returns the current view matrix of this camera

+ 7 - 1
camera/perspective.go

@@ -82,13 +82,18 @@ func (cam *Perspective) ProjMatrix(m *math32.Matrix4) {
 // Project transforms the specified position from world coordinates to this camera projected coordinates.
 func (cam *Perspective) Project(v *math32.Vector3) (*math32.Vector3, error) {
 
-	cam.updateProjMatrix()
+	// Get camera view matrix
 	var matrix math32.Matrix4
 	matrixWorld := cam.MatrixWorld()
 	err := matrix.GetInverse(&matrixWorld)
 	if err != nil {
 		return nil, err
 	}
+
+	// Update camera projection matrix
+	cam.updateProjMatrix()
+
+	// Multiply viewMatrix by projMatrix and apply the resulting projection matrix to the provided vector
 	matrix.MultiplyMatrices(&cam.projMatrix, &matrix)
 	v.ApplyProjection(&matrix)
 	return v, nil
@@ -97,6 +102,7 @@ func (cam *Perspective) Project(v *math32.Vector3) (*math32.Vector3, error) {
 // Unproject transforms the specified position from camera projected coordinates to world coordinates.
 func (cam *Perspective) Unproject(v *math32.Vector3) (*math32.Vector3, error) {
 
+	// Get inverted camera view matrix
 	invertedViewMatrix := cam.MatrixWorld()
 
 	// Get inverted camera projection matrix