|
@@ -36,24 +36,25 @@ func (cam *Camera) Initialize() {
|
|
|
cam.target.Set(0, 0, 0)
|
|
cam.target.Set(0, 0, 0)
|
|
|
cam.up.Set(0, 1, 0)
|
|
cam.up.Set(0, 1, 0)
|
|
|
cam.SetDirection(0, 0, -1)
|
|
cam.SetDirection(0, 0, -1)
|
|
|
- cam.updateQuaternion()
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// WorldDirection updates the specified vector with the
|
|
|
|
|
-// current world direction the camera is pointed
|
|
|
|
|
-func (cam *Camera) WorldDirection(result *math32.Vector3) {
|
|
|
|
|
-
|
|
|
|
|
- var wpos math32.Vector3
|
|
|
|
|
- cam.WorldPosition(&wpos)
|
|
|
|
|
- *result = cam.target
|
|
|
|
|
- result.Sub(&wpos).Normalize()
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// LookAt sets the camera target position
|
|
// LookAt sets the camera target position
|
|
|
|
|
+// TODO - maybe move to Node, or create similar in Node
|
|
|
func (cam *Camera) LookAt(target *math32.Vector3) {
|
|
func (cam *Camera) LookAt(target *math32.Vector3) {
|
|
|
|
|
|
|
|
|
|
+ // This method does not support objects with rotated and/or translated parent(s)
|
|
|
|
|
+
|
|
|
cam.target = *target
|
|
cam.target = *target
|
|
|
- cam.updateQuaternion()
|
|
|
|
|
|
|
+
|
|
|
|
|
+ var rotMat math32.Matrix4
|
|
|
|
|
+ pos := cam.Position()
|
|
|
|
|
+ rotMat.LookAt(&pos, &cam.target, &cam.up)
|
|
|
|
|
+ log.Error("pos = %v, target = %v", pos, cam.target)
|
|
|
|
|
+
|
|
|
|
|
+ var q math32.Quaternion
|
|
|
|
|
+ q.SetFromRotationMatrix(&rotMat)
|
|
|
|
|
+ cam.SetQuaternionQuat(&q)
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// GetCamera satisfies the ICamera interface
|
|
// GetCamera satisfies the ICamera interface
|
|
@@ -78,74 +79,17 @@ func (cam *Camera) Up() math32.Vector3 {
|
|
|
func (cam *Camera) SetUp(up *math32.Vector3) {
|
|
func (cam *Camera) SetUp(up *math32.Vector3) {
|
|
|
|
|
|
|
|
cam.up = *up
|
|
cam.up = *up
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// SetPosition sets this camera world position
|
|
|
|
|
-// This method overrides the Node method to update
|
|
|
|
|
-// the camera quaternion, because changing the camera position
|
|
|
|
|
-// may change its rotation
|
|
|
|
|
-func (cam *Camera) SetPosition(x, y, z float32) {
|
|
|
|
|
-
|
|
|
|
|
- cam.Node.SetPosition(x, y, z)
|
|
|
|
|
- cam.updateQuaternion()
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// SetPositionX sets this camera world position
|
|
|
|
|
-// This method overrides the Node method to update
|
|
|
|
|
-// the camera quaternion, because changing the camera position
|
|
|
|
|
-// may change its rotation
|
|
|
|
|
-func (cam *Camera) SetPositionX(x float32) {
|
|
|
|
|
- cam.Node.SetPositionX(x)
|
|
|
|
|
- cam.updateQuaternion()
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// SetPositionY sets this camera world position
|
|
|
|
|
-// This method overrides the Node method to update
|
|
|
|
|
-// the camera quaternion, because changing the camera position
|
|
|
|
|
-// may change its rotation
|
|
|
|
|
-func (cam *Camera) SetPositionY(y float32) {
|
|
|
|
|
- cam.Node.SetPositionY(y)
|
|
|
|
|
- cam.updateQuaternion()
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// SetPositionZ sets this camera world position
|
|
|
|
|
-// This method overrides the Node method to update
|
|
|
|
|
-// the camera quaternion, because changing the camera position
|
|
|
|
|
-// may change its rotation
|
|
|
|
|
-func (cam *Camera) SetPositionZ(z float32) {
|
|
|
|
|
- cam.Node.SetPositionZ(z)
|
|
|
|
|
- cam.updateQuaternion()
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// SetPositionVec sets this node position from the specified vector pointer
|
|
|
|
|
-// This method overrides the Node method to update
|
|
|
|
|
-// the camera quaternion, because changing the camera position
|
|
|
|
|
-// may change its rotation
|
|
|
|
|
-func (cam *Camera) SetPositionVec(vpos *math32.Vector3) {
|
|
|
|
|
-
|
|
|
|
|
- cam.Node.SetPositionVec(vpos)
|
|
|
|
|
- cam.updateQuaternion()
|
|
|
|
|
|
|
+ cam.LookAt(&cam.target)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ViewMatrix returns the current view matrix of this camera
|
|
// ViewMatrix returns the current view matrix of this camera
|
|
|
func (cam *Camera) ViewMatrix(m *math32.Matrix4) {
|
|
func (cam *Camera) ViewMatrix(m *math32.Matrix4) {
|
|
|
|
|
|
|
|
- var wpos math32.Vector3
|
|
|
|
|
- cam.WorldPosition(&wpos)
|
|
|
|
|
- cam.viewMatrix.LookAt(&wpos, &cam.target, &cam.up)
|
|
|
|
|
- *m = cam.viewMatrix
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// updateQuaternion must be called when the camera position or target
|
|
|
|
|
-// is changed to update its quaternion.
|
|
|
|
|
-// This is important if the camera has children, such as an audio listener
|
|
|
|
|
-func (cam *Camera) updateQuaternion() {
|
|
|
|
|
-
|
|
|
|
|
- var wdir math32.Vector3
|
|
|
|
|
- cam.WorldDirection(&wdir)
|
|
|
|
|
- var q math32.Quaternion
|
|
|
|
|
- q.SetFromUnitVectors(&math32.Vector3{0, 0, -1}, &wdir)
|
|
|
|
|
- cam.SetQuaternionQuat(&q)
|
|
|
|
|
|
|
+ matrixWorld := cam.MatrixWorld()
|
|
|
|
|
+ err := m.GetInverse(&matrixWorld)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ panic("Camera.ViewMatrix: Couldn't invert matrix")
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Project satisfies the ICamera interface and must
|
|
// Project satisfies the ICamera interface and must
|