orthographic.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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
  5. import (
  6. "github.com/g3n/engine/core"
  7. "github.com/g3n/engine/math32"
  8. )
  9. // Orthographic is an orthographic camera.
  10. type Orthographic struct {
  11. Camera // Embedded camera
  12. left float32 // left plane x coordinate
  13. right float32 // right plane x coordinate
  14. top float32 // top plane y coordinate
  15. bottom float32 // bottom plane y coordinate
  16. near float32 // near plane z coordinate
  17. far float32 // far plane z coordinate
  18. zoom float32
  19. projChanged bool // camera projection parameters changed (needs to recalculates projection matrix)
  20. projMatrix math32.Matrix4 // last calculated projection matrix
  21. }
  22. // NewOrthographic creates and returns a pointer to a new orthographic camera with the specified parameters.
  23. func NewOrthographic(left, right, top, bottom, near, far float32) *Orthographic {
  24. cam := new(Orthographic)
  25. cam.Camera.Initialize()
  26. cam.left = left
  27. cam.right = right
  28. cam.top = top
  29. cam.bottom = bottom
  30. cam.near = near
  31. cam.far = far
  32. cam.zoom = 1.0
  33. cam.projChanged = true
  34. return cam
  35. }
  36. // SetAspect sets the camera aspect ratio (width/height).
  37. func (cam *Orthographic) SetAspect(aspect float32) {
  38. height := cam.top - cam.bottom
  39. halfwidth := height * aspect * 0.5
  40. center := (cam.left + cam.right) * 0.5
  41. cam.left = center - halfwidth
  42. cam.right = center + halfwidth
  43. cam.projChanged = true
  44. }
  45. // SetZoom sets the zoom factor of the camera.
  46. func (cam *Orthographic) SetZoom(zoom float32) {
  47. cam.zoom = math32.Abs(zoom)
  48. cam.projChanged = true
  49. }
  50. // Zoom returns the zoom factor of the camera.
  51. func (cam *Orthographic) Zoom() float32 {
  52. return cam.zoom
  53. }
  54. // Planes returns the coordinates of the camera planes.
  55. func (cam *Orthographic) Planes() (left, right, top, bottom, near, far float32) {
  56. return cam.left, cam.right, cam.top, cam.bottom, cam.near, cam.far
  57. }
  58. // ProjMatrix satisfies the ICamera interface.
  59. func (cam *Orthographic) ProjMatrix(m *math32.Matrix4) {
  60. if cam.projChanged {
  61. cam.projMatrix.MakeOrthographic(cam.left/cam.zoom, cam.right/cam.zoom, cam.top/cam.zoom, cam.bottom/cam.zoom, cam.near, cam.far)
  62. cam.projChanged = false
  63. }
  64. *m = cam.projMatrix
  65. }
  66. // Project satisfies the ICamera interface and must
  67. // be implemented for specific camera types.
  68. func (cam *Camera) Project(v *math32.Vector3) (*math32.Vector3, error) {
  69. panic("Not implemented")
  70. }
  71. // Unproject satisfies the ICamera interface and must
  72. // be implemented for specific camera types.
  73. func (cam *Camera) Unproject(v *math32.Vector3) (*math32.Vector3, error) {
  74. panic("Not implemented")
  75. }
  76. // SetRaycaster satisfies the ICamera interface and must
  77. // be implemented for specific camera types.
  78. func (cam *Camera) SetRaycaster(rc *core.Raycaster, x, y float32) error {
  79. panic("Not implemented")
  80. }