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) 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. // 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. return r.shaman.SetProgramShader(pname, stype, sname)
  53. }
  54. func (r *Renderer) Render(iscene core.INode, icam camera.ICamera) error {
  55. // Updates world matrices of all scene nodes
  56. iscene.UpdateMatrixWorld()
  57. scene := iscene.GetNode()
  58. // Builds RenderInfo calls RenderSetup for all visible nodes
  59. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  60. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  61. // Clear scene arrays
  62. r.ambLights = r.ambLights[0:0]
  63. r.dirLights = r.dirLights[0:0]
  64. r.pointLights = r.pointLights[0:0]
  65. r.spotLights = r.spotLights[0:0]
  66. r.others = r.others[0:0]
  67. r.grmats = r.grmats[0:0]
  68. // Internal function to classify a node and its children
  69. var classifyNode func(inode core.INode)
  70. classifyNode = func(inode core.INode) {
  71. // If node not visible, ignore
  72. node := inode.GetNode()
  73. if !node.Visible() {
  74. return
  75. }
  76. // Checks if node is a Graphic
  77. igr, ok := inode.(graphic.IGraphic)
  78. if ok {
  79. if igr.Renderable() {
  80. // Appends to list each graphic material for this graphic
  81. gr := igr.GetGraphic()
  82. materials := gr.Materials()
  83. for i := 0; i < len(materials); i++ {
  84. r.grmats = append(r.grmats, &materials[i])
  85. }
  86. }
  87. // Node is not a Graphic
  88. } else {
  89. // Checks if node is a Light
  90. il, ok := inode.(light.ILight)
  91. if ok {
  92. switch l := il.(type) {
  93. case *light.Ambient:
  94. r.ambLights = append(r.ambLights, l)
  95. case *light.Directional:
  96. r.dirLights = append(r.dirLights, l)
  97. case *light.Point:
  98. r.pointLights = append(r.pointLights, l)
  99. case *light.Spot:
  100. r.spotLights = append(r.spotLights, l)
  101. default:
  102. panic("Invalid light type")
  103. }
  104. // Other nodes
  105. } else {
  106. r.others = append(r.others, inode)
  107. }
  108. }
  109. // Classify node children
  110. for _, ichild := range node.Children() {
  111. classifyNode(ichild)
  112. }
  113. }
  114. // Classify all scene nodes
  115. classifyNode(scene)
  116. // Sets lights count in shader specs
  117. r.specs.AmbientLightsMax = len(r.ambLights)
  118. r.specs.DirLightsMax = len(r.dirLights)
  119. r.specs.PointLightsMax = len(r.pointLights)
  120. r.specs.SpotLightsMax = len(r.spotLights)
  121. // Render other nodes (audio players, etc)
  122. for i := 0; i < len(r.others); i++ {
  123. inode := r.others[i]
  124. if !inode.GetNode().Visible() {
  125. continue
  126. }
  127. r.others[i].Render(r.gs)
  128. }
  129. // For each *GraphicMaterial
  130. for _, grmat := range r.grmats {
  131. //log.Debug("grmat:%v", grmat)
  132. mat := grmat.GetMaterial().GetMaterial()
  133. // Sets the shader specs for this material and sets shader program
  134. r.specs.Name = mat.Shader()
  135. r.specs.ShaderUnique = mat.ShaderUnique()
  136. r.specs.UseLights = mat.UseLights()
  137. r.specs.MatTexturesMax = mat.TextureCount()
  138. _, err := r.shaman.SetProgram(&r.specs)
  139. if err != nil {
  140. return err
  141. }
  142. // Setup lights (transfer lights uniforms)
  143. for idx, l := range r.ambLights {
  144. l.RenderSetup(r.gs, &r.rinfo, idx)
  145. }
  146. for idx, l := range r.dirLights {
  147. l.RenderSetup(r.gs, &r.rinfo, idx)
  148. }
  149. for idx, l := range r.pointLights {
  150. l.RenderSetup(r.gs, &r.rinfo, idx)
  151. }
  152. for idx, l := range r.spotLights {
  153. l.RenderSetup(r.gs, &r.rinfo, idx)
  154. }
  155. // Render this graphic material
  156. grmat.Render(r.gs, &r.rinfo)
  157. }
  158. return nil
  159. }