renderer.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 renderer...
  5. package renderer
  6. import (
  7. "github.com/g3n/engine/camera"
  8. "github.com/g3n/engine/core"
  9. "github.com/g3n/engine/gls"
  10. "github.com/g3n/engine/graphic"
  11. "github.com/g3n/engine/light"
  12. )
  13. type Renderer struct {
  14. gs *gls.GLS
  15. shaman Shaman // Internal shader manager
  16. ambLights []*light.Ambient // Array of ambient lights for last scene
  17. dirLights []*light.Directional // Array of directional lights for last scene
  18. pointLights []*light.Point // Array of point
  19. spotLights []*light.Spot // Array of spot lights for the scene
  20. others []core.INode // Other nodes (audio, players, etc)
  21. grmats []*graphic.GraphicMaterial // Array of all graphic materials for scene
  22. rinfo core.RenderInfo // Preallocated Render info
  23. specs ShaderSpecs // Preallocated Shader specs
  24. }
  25. func NewRenderer(gs *gls.GLS) *Renderer {
  26. r := new(Renderer)
  27. r.gs = gs
  28. r.shaman.Init(gs)
  29. r.ambLights = make([]*light.Ambient, 0)
  30. r.dirLights = make([]*light.Directional, 0)
  31. r.pointLights = make([]*light.Point, 0)
  32. r.spotLights = make([]*light.Spot, 0)
  33. r.others = make([]core.INode, 0)
  34. r.grmats = make([]*graphic.GraphicMaterial, 0)
  35. return r
  36. }
  37. func (r *Renderer) AddDefaultShaders() error {
  38. return r.shaman.AddDefaultShaders()
  39. }
  40. func (r *Renderer) AddChunk(name, source string) error {
  41. return r.shaman.AddChunk(name, source)
  42. }
  43. func (r *Renderer) AddShader(name, source string) error {
  44. return r.shaman.AddShader(name, source)
  45. }
  46. func (r *Renderer) AddProgram(name, vertex, frag string) error {
  47. return r.shaman.AddProgram(name, vertex, frag)
  48. }
  49. func (r *Renderer) Render(iscene core.INode, icam camera.ICamera) error {
  50. // Updates world matrices of all scene nodes
  51. iscene.UpdateMatrixWorld()
  52. scene := iscene.GetNode()
  53. // Builds RenderInfo calls RenderSetup for all visible nodes
  54. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  55. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  56. // Clear scene arrays
  57. r.ambLights = r.ambLights[0:0]
  58. r.dirLights = r.dirLights[0:0]
  59. r.pointLights = r.pointLights[0:0]
  60. r.spotLights = r.spotLights[0:0]
  61. r.others = r.others[0:0]
  62. r.grmats = r.grmats[0:0]
  63. // Internal function to classify a node and its children
  64. var classifyNode func(inode core.INode)
  65. classifyNode = func(inode core.INode) {
  66. // If node not visible, ignore
  67. node := inode.GetNode()
  68. if !node.Visible() {
  69. return
  70. }
  71. // Checks if node is a Graphic
  72. igr, ok := inode.(graphic.IGraphic)
  73. if ok {
  74. if igr.Renderable() {
  75. // Appends to list each graphic material for this graphic
  76. gr := igr.GetGraphic()
  77. materials := gr.Materials()
  78. for i := 0; i < len(materials); i++ {
  79. r.grmats = append(r.grmats, &materials[i])
  80. }
  81. }
  82. // Node is not a Graphic
  83. } else {
  84. // Checks if node is a Light
  85. il, ok := inode.(light.ILight)
  86. if ok {
  87. switch l := il.(type) {
  88. case *light.Ambient:
  89. r.ambLights = append(r.ambLights, l)
  90. case *light.Directional:
  91. r.dirLights = append(r.dirLights, l)
  92. case *light.Point:
  93. r.pointLights = append(r.pointLights, l)
  94. case *light.Spot:
  95. r.spotLights = append(r.spotLights, l)
  96. default:
  97. panic("Invalid light type")
  98. }
  99. // Other nodes
  100. } else {
  101. r.others = append(r.others, inode)
  102. }
  103. }
  104. // Classify node children
  105. for _, ichild := range node.Children() {
  106. classifyNode(ichild)
  107. }
  108. }
  109. // Classify all scene nodes
  110. classifyNode(scene)
  111. // Sets lights count in shader specs
  112. r.specs.AmbientLightsMax = len(r.ambLights)
  113. r.specs.DirLightsMax = len(r.dirLights)
  114. r.specs.PointLightsMax = len(r.pointLights)
  115. r.specs.SpotLightsMax = len(r.spotLights)
  116. // Render other nodes (audio players, etc)
  117. for i := 0; i < len(r.others); i++ {
  118. inode := r.others[i]
  119. if !inode.GetNode().Visible() {
  120. continue
  121. }
  122. r.others[i].Render(r.gs)
  123. }
  124. // For each *GraphicMaterial
  125. for _, grmat := range r.grmats {
  126. //log.Debug("grmat:%v", grmat)
  127. mat := grmat.GetMaterial().GetMaterial()
  128. // Sets the shader specs for this material and sets shader program
  129. r.specs.Name = mat.Shader()
  130. r.specs.UseLights = mat.UseLights()
  131. r.specs.MatTexturesMax = mat.TextureCount()
  132. _, err := r.shaman.SetProgram(&r.specs)
  133. if err != nil {
  134. return err
  135. }
  136. // Setup lights (transfer lights uniforms)
  137. for idx, l := range r.ambLights {
  138. l.RenderSetup(r.gs, &r.rinfo, idx)
  139. }
  140. for idx, l := range r.dirLights {
  141. l.RenderSetup(r.gs, &r.rinfo, idx)
  142. }
  143. for idx, l := range r.pointLights {
  144. l.RenderSetup(r.gs, &r.rinfo, idx)
  145. }
  146. for idx, l := range r.spotLights {
  147. l.RenderSetup(r.gs, &r.rinfo, idx)
  148. }
  149. // Render this graphic material
  150. grmat.Render(r.gs, &r.rinfo)
  151. }
  152. return nil
  153. }