listener.go 1.7 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 audio
  5. import (
  6. "github.com/g3n/engine/audio/al"
  7. "github.com/g3n/engine/core"
  8. "github.com/g3n/engine/gls"
  9. "github.com/g3n/engine/math32"
  10. )
  11. // Listener embeds a core.Node and
  12. type Listener struct {
  13. core.Node
  14. }
  15. func NewListener() *Listener {
  16. l := new(Listener)
  17. l.Node.Init()
  18. return l
  19. }
  20. func (l *Listener) SetVelocity(vx, vy, vz float32) {
  21. al.Listener3f(al.Velocity, vx, vy, vz)
  22. }
  23. func (l *Listener) SetVelocityVec(v *math32.Vector3) {
  24. al.Listener3f(al.Velocity, v.X, v.Y, v.Z)
  25. }
  26. func (l *Listener) Velocity() (float32, float32, float32) {
  27. return al.GetListener3f(al.Velocity)
  28. }
  29. func (l *Listener) VelocityVec() math32.Vector3 {
  30. vx, vy, vz := al.GetListener3f(al.Velocity)
  31. return math32.Vector3{vx, vy, vz}
  32. }
  33. func (l *Listener) SetGain(gain float32) {
  34. al.Listenerf(al.Gain, gain)
  35. }
  36. func (l *Listener) Gain() float32 {
  37. return al.GetListenerf(al.Gain)
  38. }
  39. // Render is called by the renderer at each frame
  40. // Updates the OpenAL position and orientation of this listener
  41. func (l *Listener) Render(gl *gls.GLS) {
  42. // Sets the listener source world position
  43. var wpos math32.Vector3
  44. l.WorldPosition(&wpos)
  45. al.Listener3f(al.Position, wpos.X, wpos.Y, wpos.Z)
  46. // Get listener current world direction
  47. var vdir math32.Vector3
  48. l.WorldDirection(&vdir)
  49. // Assumes initial UP vector and recalculates current up vector
  50. vup := math32.Vector3{0, 1, 0}
  51. var vright math32.Vector3
  52. vright.CrossVectors(&vdir, &vup)
  53. vup.CrossVectors(&vright, &vdir)
  54. // Sets the listener orientation
  55. orientation := []float32{vdir.X, vdir.Y, vdir.Z, vup.X, vup.Y, vup.Z}
  56. al.Listenerfv(al.Orientation, orientation)
  57. }