orthographic.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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
  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 recalculates 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. // SetZoom sets the zoom factor of the camera
  36. func (cam *Orthographic) SetZoom(zoom float32) {
  37. cam.zoom = math32.Abs(zoom)
  38. cam.projChanged = true
  39. }
  40. // Zoom returns the zoom factor of the camera
  41. func (cam *Orthographic) Zoom() float32 {
  42. return cam.zoom
  43. }
  44. // Planes returns the coordinates of the camera planes
  45. func (cam *Orthographic) Planes() (left, right, top, bottom, near, far float32) {
  46. return cam.left, cam.right, cam.top, cam.bottom, cam.near, cam.far
  47. }
  48. // ProjMatrix satisfies the ICamera interface
  49. func (cam *Orthographic) ProjMatrix(m *math32.Matrix4) {
  50. if cam.projChanged {
  51. cam.projMatrix.MakeOrthographic(cam.left/cam.zoom, cam.right/cam.zoom, cam.top/cam.zoom, cam.bottom/cam.zoom, cam.near, cam.far)
  52. cam.projChanged = false
  53. }
  54. *m = cam.projMatrix
  55. }