orthographic.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/math32"
  7. )
  8. // Orthographic is an orthographic camera.
  9. type Orthographic struct {
  10. Camera // Embedded camera
  11. left float32 // left plane x coordinate
  12. right float32 // right plane x coordinate
  13. top float32 // top plane y coordinate
  14. bottom float32 // bottom plane y coordinate
  15. near float32 // near plane z coordinate
  16. far float32 // far plane z coordinate
  17. zoom float32
  18. projChanged bool // camera projection parameters changed (needs to recalculate projection matrix)
  19. projMatrix math32.Matrix4 // last calculated projection matrix
  20. }
  21. // NewOrthographic creates and returns a pointer to a new orthographic camera with the specified parameters.
  22. func NewOrthographic(left, right, top, bottom, near, far float32) *Orthographic {
  23. cam := new(Orthographic)
  24. cam.Camera.Initialize()
  25. cam.left = left
  26. cam.right = right
  27. cam.top = top
  28. cam.bottom = bottom
  29. cam.near = near
  30. cam.far = far
  31. cam.zoom = 1.0
  32. cam.projChanged = true
  33. return cam
  34. }
  35. // SetAspect sets the camera aspect ratio (width/height).
  36. func (cam *Orthographic) SetAspect(aspect float32) {
  37. height := cam.top - cam.bottom
  38. halfwidth := height * aspect * 0.5
  39. center := (cam.left + cam.right) * 0.5
  40. cam.left = center - halfwidth
  41. cam.right = center + halfwidth
  42. cam.projChanged = true
  43. }
  44. // SetZoom sets the zoom factor of the camera.
  45. func (cam *Orthographic) SetZoom(zoom float32) {
  46. cam.zoom = math32.Abs(zoom)
  47. cam.projChanged = true
  48. }
  49. // Zoom returns the zoom factor of the camera.
  50. func (cam *Orthographic) Zoom() float32 {
  51. return cam.zoom
  52. }
  53. // Planes returns the coordinates of the camera planes.
  54. func (cam *Orthographic) Planes() (left, right, top, bottom, near, far float32) {
  55. return cam.left, cam.right, cam.top, cam.bottom, cam.near, cam.far
  56. }
  57. // ProjMatrix satisfies the ICamera interface.
  58. func (cam *Orthographic) ProjMatrix(m *math32.Matrix4) {
  59. if cam.projChanged {
  60. cam.projMatrix.MakeOrthographic(cam.left/cam.zoom, cam.right/cam.zoom, cam.top/cam.zoom, cam.bottom/cam.zoom, cam.near, cam.far)
  61. cam.projChanged = false
  62. }
  63. *m = cam.projMatrix
  64. }