camera.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package camera contains common camera types used for rendering 3D scenes.
  5. package camera
  6. import (
  7. "github.com/g3n/engine/core"
  8. "github.com/g3n/engine/math32"
  9. )
  10. // ICamera is interface for all camera types.
  11. type ICamera interface {
  12. GetCamera() *Camera
  13. ViewMatrix(*math32.Matrix4)
  14. ProjMatrix(*math32.Matrix4)
  15. Project(*math32.Vector3) (*math32.Vector3, error)
  16. SetAspect(float32)
  17. Unproject(*math32.Vector3) (*math32.Vector3, error)
  18. SetRaycaster(rc *core.Raycaster, x, y float32) error
  19. }
  20. // Camera is the base camera which is normally embedded in other camera types.
  21. type Camera struct {
  22. core.Node // Embedded Node
  23. target math32.Vector3 // Camera target in world coordinates
  24. up math32.Vector3 // Camera Up vector
  25. viewMatrix math32.Matrix4 // Last calculated view matrix
  26. }
  27. // Initialize initializes the base camera.
  28. // Normally used by other camera types which embed this base camera.
  29. func (cam *Camera) Initialize() {
  30. cam.Node.Init()
  31. cam.target.Set(0, 0, 0)
  32. cam.up.Set(0, 1, 0)
  33. cam.SetDirection(0, 0, -1)
  34. }
  35. // LookAt rotates the camera to look at the specified target position.
  36. // This method does not support objects with rotated and/or translated parent(s).
  37. // TODO: maybe move method to Node, or create similar in Node.
  38. func (cam *Camera) LookAt(target *math32.Vector3) {
  39. cam.target = *target
  40. var rotMat math32.Matrix4
  41. pos := cam.Position()
  42. rotMat.LookAt(&pos, &cam.target, &cam.up)
  43. var q math32.Quaternion
  44. q.SetFromRotationMatrix(&rotMat)
  45. cam.SetQuaternionQuat(&q)
  46. }
  47. // GetCamera satisfies the ICamera interface.
  48. func (cam *Camera) GetCamera() *Camera {
  49. return cam
  50. }
  51. // Target get the current target position.
  52. func (cam *Camera) Target() math32.Vector3 {
  53. return cam.target
  54. }
  55. // Up get the current camera up vector.
  56. func (cam *Camera) Up() math32.Vector3 {
  57. return cam.up
  58. }
  59. // SetUp sets the camera up vector.
  60. func (cam *Camera) SetUp(up *math32.Vector3) {
  61. cam.up = *up
  62. cam.LookAt(&cam.target) // TODO Maybe remove and let user call LookAt explicitly
  63. }
  64. // ViewMatrix returns the current view matrix of this camera.
  65. func (cam *Camera) ViewMatrix(m *math32.Matrix4) {
  66. cam.UpdateMatrixWorld()
  67. matrixWorld := cam.MatrixWorld()
  68. err := m.GetInverse(&matrixWorld)
  69. if err != nil {
  70. panic("Camera.ViewMatrix: Couldn't invert matrix")
  71. }
  72. }