camera.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 contain 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
  16. Unproject(*math32.Vector3) *math32.Vector3
  17. SetRaycaster(rc *core.Raycaster, x, y float32)
  18. }
  19. // Camera is the base camera which is normally embedded in other camera types
  20. type Camera struct {
  21. core.Node // Embedded Node
  22. target math32.Vector3 // camera target in world coordinates
  23. up math32.Vector3 // camera Up vector
  24. viewMatrix math32.Matrix4 // last calculated view matrix
  25. }
  26. // Initialize initializes the base camera.
  27. // It is used by other camera types which embed this base camera
  28. func (cam *Camera) Initialize() {
  29. cam.Node.Init()
  30. cam.target.Set(0, 0, 0)
  31. cam.up.Set(0, 1, 0)
  32. cam.SetDirection(0, 0, -1)
  33. cam.updateQuaternion()
  34. }
  35. // WorldDirection updates the specified vector with the
  36. // current world direction the camera is pointed
  37. func (cam *Camera) WorldDirection(result *math32.Vector3) {
  38. var wpos math32.Vector3
  39. cam.WorldPosition(&wpos)
  40. *result = cam.target
  41. result.Sub(&wpos).Normalize()
  42. }
  43. // LookAt sets the camera target position
  44. func (cam *Camera) LookAt(target *math32.Vector3) {
  45. cam.target = *target
  46. cam.updateQuaternion()
  47. }
  48. // GetCamera satisfies the ICamera interface
  49. func (cam *Camera) GetCamera() *Camera {
  50. return cam
  51. }
  52. // Target get the current target position
  53. func (cam *Camera) Target() math32.Vector3 {
  54. return cam.target
  55. }
  56. // Up get the current up position
  57. func (cam *Camera) Up() math32.Vector3 {
  58. return cam.up
  59. }
  60. // SetUp sets the camera up vector
  61. func (cam *Camera) SetUp(up *math32.Vector3) {
  62. cam.up = *up
  63. }
  64. // SetPosition sets this camera world position
  65. // This method overrides the Node method to update
  66. // the camera quaternion, because changing the camera position
  67. // may change its rotation
  68. func (cam *Camera) SetPosition(x, y, z float32) {
  69. cam.Node.SetPosition(x, y, z)
  70. cam.updateQuaternion()
  71. }
  72. // SetPositionVec sets this node position from the specified vector pointer
  73. // This method overrides the Node method to update
  74. // the camera quaternion, because changing the camera position
  75. // may change its rotation
  76. func (cam *Camera) SetPositionVec(vpos *math32.Vector3) {
  77. cam.Node.SetPositionVec(vpos)
  78. cam.updateQuaternion()
  79. }
  80. // ViewMatrix returns the current view matrix of this camera
  81. func (cam *Camera) ViewMatrix(m *math32.Matrix4) {
  82. var wpos math32.Vector3
  83. cam.WorldPosition(&wpos)
  84. cam.viewMatrix.LookAt(&wpos, &cam.target, &cam.up)
  85. *m = cam.viewMatrix
  86. }
  87. // updateQuaternion must be called when the camera position or target
  88. // is changed to update its quaternion.
  89. // This is important if the camera has children, such as an audio listener
  90. func (cam *Camera) updateQuaternion() {
  91. var wdir math32.Vector3
  92. cam.WorldDirection(&wdir)
  93. var q math32.Quaternion
  94. q.SetFromUnitVectors(&math32.Vector3{0, 0, -1}, &wdir)
  95. cam.SetQuaternionQuat(&q)
  96. }
  97. // Project satisfies the ICamera interface and must
  98. // be implemented for specific camera types.
  99. func (cam *Camera) Project(v *math32.Vector3) *math32.Vector3 {
  100. panic("Not implemented")
  101. }
  102. // Unproject satisfies the ICamera interface and must
  103. // be implemented for specific camera types.
  104. func (cam *Camera) Unproject(v *math32.Vector3) *math32.Vector3 {
  105. panic("Not implemented")
  106. }
  107. // SetRaycaster satisfies the ICamera interface and must
  108. // be implemented for specific camera types.
  109. func (cam *Camera) SetRaycaster(rc *core.Raycaster, x, y float32) {
  110. panic("Not implemented")
  111. }