camera.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }
  34. // LookAt rotates the camera to look at the specified target position.
  35. // This method does not support objects with rotated and/or translated parent(s).
  36. // TODO: maybe move method to Node, or create similar in Node.
  37. func (cam *Camera) LookAt(target *math32.Vector3) {
  38. cam.target = *target
  39. var rotMat math32.Matrix4
  40. pos := cam.Position()
  41. rotMat.LookAt(&pos, &cam.target, &cam.up)
  42. log.Error("pos = %v, target = %v", pos, cam.target)
  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 up position
  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. matrixWorld := cam.MatrixWorld()
  67. err := m.GetInverse(&matrixWorld)
  68. if err != nil {
  69. panic("Camera.ViewMatrix: Couldn't invert matrix")
  70. }
  71. }
  72. // Project satisfies the ICamera interface and must
  73. // be implemented for specific camera types.
  74. func (cam *Camera) Project(v *math32.Vector3) (*math32.Vector3, error) {
  75. panic("Not implemented")
  76. }
  77. // Unproject satisfies the ICamera interface and must
  78. // be implemented for specific camera types.
  79. func (cam *Camera) Unproject(v *math32.Vector3) (*math32.Vector3, error) {
  80. panic("Not implemented")
  81. }
  82. // SetRaycaster satisfies the ICamera interface and must
  83. // be implemented for specific camera types.
  84. func (cam *Camera) SetRaycaster(rc *core.Raycaster, x, y float32) error {
  85. panic("Not implemented")
  86. }