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. 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) error {
  40. return r.shaman.AddChunk(name, source)
  41. }
  42. func (r *Renderer) AddShader(name, source string) error {
  43. return r.shaman.AddShader(name, source)
  44. }
  45. func (r *Renderer) AddProgram(name, vertex, frag string) error {
  46. return r.shaman.AddProgram(name, vertex, frag)
  47. }
  48. func (r *Renderer) Render(iscene core.INode, icam camera.ICamera) error {
  49. // Updates world matrices of all scene nodes
  50. iscene.UpdateMatrixWorld()
  51. scene := iscene.GetNode()
  52. // Builds RenderInfo calls RenderSetup for all visible nodes
  53. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  54. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  55. // Clear scene arrays
  56. r.ambLights = r.ambLights[0:0]
  57. r.dirLights = r.dirLights[0:0]
  58. r.pointLights = r.pointLights[0:0]
  59. r.spotLights = r.spotLights[0:0]
  60. r.others = r.others[0:0]
  61. r.grmats = r.grmats[0:0]
  62. // Internal function to classify a node and its children
  63. var classifyNode func(inode core.INode)
  64. classifyNode = func(inode core.INode) {
  65. // If node not visible, ignore
  66. node := inode.GetNode()
  67. if !node.Visible() {
  68. return
  69. }
  70. // Checks if node is a Graphic
  71. igr, ok := inode.(graphic.IGraphic)
  72. if ok {
  73. if igr.Renderable() {
  74. // Appends to list each graphic material for this graphic
  75. gr := igr.GetGraphic()
  76. materials := gr.Materials()
  77. for i := 0; i < len(materials); i++ {
  78. r.grmats = append(r.grmats, &materials[i])
  79. }
  80. }
  81. // Node is not a Graphic
  82. } else {
  83. // Checks if node is a Light
  84. il, ok := inode.(light.ILight)
  85. if ok {
  86. switch l := il.(type) {
  87. case *light.Ambient:
  88. r.ambLights = append(r.ambLights, l)
  89. case *light.Directional:
  90. r.dirLights = append(r.dirLights, l)
  91. case *light.Point:
  92. r.pointLights = append(r.pointLights, l)
  93. case *light.Spot:
  94. r.spotLights = append(r.spotLights, l)
  95. default:
  96. panic("Invalid light type")
  97. }
  98. // Other nodes
  99. } else {
  100. r.others = append(r.others, inode)
  101. }
  102. }
  103. // Classify node children
  104. for _, ichild := range node.Children() {
  105. classifyNode(ichild)
  106. }
  107. }
  108. // Classify all scene nodes
  109. classifyNode(scene)
  110. // Sets lights count in shader specs
  111. r.specs.AmbientLightsMax = len(r.ambLights)
  112. r.specs.DirLightsMax = len(r.dirLights)
  113. r.specs.PointLightsMax = len(r.pointLights)
  114. r.specs.SpotLightsMax = len(r.spotLights)
  115. // Render other nodes (audio players, etc)
  116. for i := 0; i < len(r.others); i++ {
  117. inode := r.others[i]
  118. if !inode.GetNode().Visible() {
  119. continue
  120. }
  121. r.others[i].Render(r.gs)
  122. }
  123. // For each *GraphicMaterial
  124. for _, grmat := range r.grmats {
  125. //log.Debug("grmat:%v", grmat)
  126. mat := grmat.GetMaterial().GetMaterial()
  127. // Sets the shader specs for this material and sets shader program
  128. r.specs.Name = mat.Shader()
  129. r.specs.ShaderUnique = mat.ShaderUnique()
  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. }