skybox.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 graphic
  5. import (
  6. "github.com/g3n/engine/core"
  7. "github.com/g3n/engine/geometry"
  8. "github.com/g3n/engine/gls"
  9. "github.com/g3n/engine/material"
  10. "github.com/g3n/engine/math32"
  11. "github.com/g3n/engine/texture"
  12. )
  13. type SkyboxData struct {
  14. DirAndPrefix string
  15. Extension string
  16. Suffixes [6]string
  17. }
  18. type Skybox struct {
  19. Graphic // embedded graphic object
  20. mvm gls.UniformMatrix4f // model view matrix uniform
  21. mvpm gls.UniformMatrix4f // model view projection matrix uniform
  22. nm gls.UniformMatrix3f // normal matrix uniform
  23. }
  24. // NewSkybox creates and returns a pointer to a skybox with the specified textures
  25. func NewSkybox(data SkyboxData) (*Skybox, error) {
  26. skybox := new(Skybox)
  27. geom := geometry.NewBox(50, 50, 50, 1, 1, 1)
  28. skybox.Graphic.Init(geom, gls.TRIANGLES)
  29. for i := 0; i < 6; i++ {
  30. tex, err := texture.NewTexture2DFromImage(data.DirAndPrefix + data.Suffixes[i] + "." + data.Extension)
  31. if err != nil {
  32. return nil, err
  33. }
  34. matFace := material.NewStandard(math32.NewColor(1, 1, 1))
  35. matFace.AddTexture(tex)
  36. matFace.SetSide(material.SideBack)
  37. matFace.SetUseLights(material.UseLightAmbient)
  38. skybox.AddGroupMaterial(skybox, matFace, i)
  39. }
  40. // Creates uniforms
  41. skybox.mvm.Init("ModelViewMatrix")
  42. skybox.mvpm.Init("MVP")
  43. skybox.nm.Init("NormalMatrix")
  44. return skybox, nil
  45. }
  46. // RenderSetup is called by the engine before drawing the skybox geometry
  47. // It is responsible to updating the current shader uniforms with
  48. // the model matrices.
  49. func (skybox *Skybox) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  50. // TODO
  51. // Disable writes to the depth buffer (call glDepthMask(GL_FALSE)).
  52. // This will cause every other object to draw over the skybox, making it always appear "behind" everything else.
  53. // Since writes to the depth buffer are off, it doesn't matter how small the skybox is as long as it's larger than the camera's near clip plane.
  54. var mvm math32.Matrix4
  55. mvm.Copy(&rinfo.ViewMatrix)
  56. // Clear translation
  57. mvm[12] = 0
  58. mvm[13] = 0
  59. mvm[14] = 0
  60. // mvm.ExtractRotation(&rinfo.ViewMatrix) // TODO <- ExtractRotation does not work as expected?
  61. skybox.mvm.SetMatrix4(&mvm)
  62. skybox.mvm.Transfer(gs)
  63. // Calculates model view projection matrix and updates uniform
  64. var mvpm math32.Matrix4
  65. mvpm.MultiplyMatrices(&rinfo.ProjMatrix, &mvm)
  66. skybox.mvpm.SetMatrix4(&mvpm)
  67. skybox.mvpm.Transfer(gs)
  68. // Calculates normal matrix and updates uniform
  69. var nm math32.Matrix3
  70. nm.GetNormalMatrix(&mvm)
  71. skybox.nm.SetMatrix3(&nm)
  72. skybox.nm.Transfer(gs)
  73. }