camera.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, error)
  16. Unproject(*math32.Vector3) (*math32.Vector3, error)
  17. SetRaycaster(rc *core.Raycaster, x, y float32) error
  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. // SetPositionX sets this camera world position
  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) SetPositionX(x float32) {
  77. cam.Node.SetPositionX(x)
  78. cam.updateQuaternion()
  79. }
  80. // SetPositionY sets this camera world position
  81. // This method overrides the Node method to update
  82. // the camera quaternion, because changing the camera position
  83. // may change its rotation
  84. func (cam *Camera) SetPositionY(y float32) {
  85. cam.Node.SetPositionY(y)
  86. cam.updateQuaternion()
  87. }
  88. // SetPositionZ sets this camera world position
  89. // This method overrides the Node method to update
  90. // the camera quaternion, because changing the camera position
  91. // may change its rotation
  92. func (cam *Camera) SetPositionZ(z float32) {
  93. cam.Node.SetPositionZ(z)
  94. cam.updateQuaternion()
  95. }
  96. // SetPositionVec sets this node position from the specified vector pointer
  97. // This method overrides the Node method to update
  98. // the camera quaternion, because changing the camera position
  99. // may change its rotation
  100. func (cam *Camera) SetPositionVec(vpos *math32.Vector3) {
  101. cam.Node.SetPositionVec(vpos)
  102. cam.updateQuaternion()
  103. }
  104. // ViewMatrix returns the current view matrix of this camera
  105. func (cam *Camera) ViewMatrix(m *math32.Matrix4) {
  106. var wpos math32.Vector3
  107. cam.WorldPosition(&wpos)
  108. cam.viewMatrix.LookAt(&wpos, &cam.target, &cam.up)
  109. *m = cam.viewMatrix
  110. }
  111. // updateQuaternion must be called when the camera position or target
  112. // is changed to update its quaternion.
  113. // This is important if the camera has children, such as an audio listener
  114. func (cam *Camera) updateQuaternion() {
  115. var wdir math32.Vector3
  116. cam.WorldDirection(&wdir)
  117. var q math32.Quaternion
  118. q.SetFromUnitVectors(&math32.Vector3{0, 0, -1}, &wdir)
  119. cam.SetQuaternionQuat(&q)
  120. }
  121. // Project satisfies the ICamera interface and must
  122. // be implemented for specific camera types.
  123. func (cam *Camera) Project(v *math32.Vector3) (*math32.Vector3, error) {
  124. panic("Not implemented")
  125. }
  126. // Unproject satisfies the ICamera interface and must
  127. // be implemented for specific camera types.
  128. func (cam *Camera) Unproject(v *math32.Vector3) (*math32.Vector3, error) {
  129. panic("Not implemented")
  130. }
  131. // SetRaycaster satisfies the ICamera interface and must
  132. // be implemented for specific camera types.
  133. func (cam *Camera) SetRaycaster(rc *core.Raycaster, x, y float32) error {
  134. panic("Not implemented")
  135. }