renderer.go 5.1 KB

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