renderer.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/gui"
  11. "github.com/g3n/engine/light"
  12. )
  13. type Renderer struct {
  14. gs *gls.GLS
  15. shaman Shaman // Internal shader manager
  16. scene core.INode // Node containing 3D scene to render
  17. gui gui.IPanel // Panel containing GUI to render
  18. ambLights []*light.Ambient // Array of ambient lights for last scene
  19. dirLights []*light.Directional // Array of directional lights for last scene
  20. pointLights []*light.Point // Array of point
  21. spotLights []*light.Spot // Array of spot lights for the scene
  22. others []core.INode // Other nodes (audio, players, etc)
  23. grmats []*graphic.GraphicMaterial // Array of all graphic materials for scene
  24. rinfo core.RenderInfo // Preallocated Render info
  25. specs ShaderSpecs // Preallocated Shader specs
  26. }
  27. func NewRenderer(gs *gls.GLS) *Renderer {
  28. r := new(Renderer)
  29. r.gs = gs
  30. r.shaman.Init(gs)
  31. r.ambLights = make([]*light.Ambient, 0)
  32. r.dirLights = make([]*light.Directional, 0)
  33. r.pointLights = make([]*light.Point, 0)
  34. r.spotLights = make([]*light.Spot, 0)
  35. r.others = make([]core.INode, 0)
  36. r.grmats = make([]*graphic.GraphicMaterial, 0)
  37. return r
  38. }
  39. func (r *Renderer) AddDefaultShaders() error {
  40. return r.shaman.AddDefaultShaders()
  41. }
  42. func (r *Renderer) AddChunk(name, source string) {
  43. r.shaman.AddChunk(name, source)
  44. }
  45. func (r *Renderer) AddShader(name, source string) {
  46. r.shaman.AddShader(name, source)
  47. }
  48. func (r *Renderer) AddProgram(name, vertex, frag string, others ...string) {
  49. r.shaman.AddProgram(name, vertex, frag, others...)
  50. }
  51. // SetGui sets the gui panel which contains the Gui to render
  52. // over the optional 3D scene.
  53. // If set to nil, no Gui will be rendered
  54. func (r *Renderer) SetGui(gui gui.IPanel) {
  55. r.gui = gui
  56. }
  57. // SetScene sets the Node which contains the scene to render
  58. // If set to nil, no scene will be rendered
  59. func (r *Renderer) SetScene(scene core.INode) {
  60. r.scene = scene
  61. }
  62. // Render renders the previously set Scene and Gui using the specified camera
  63. func (r *Renderer) Render(icam camera.ICamera) error {
  64. // Renders the 3D scene
  65. if r.scene != nil {
  66. r.gs.Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
  67. err := r.renderScene(r.scene, icam)
  68. if err != nil {
  69. return err
  70. }
  71. }
  72. // Renders the Gui over the 3D scene
  73. if r.gui != nil {
  74. r.gs.Clear(gls.DEPTH_BUFFER_BIT)
  75. err := r.renderGui(icam)
  76. //err := r.renderScene(r.gui, icam)
  77. if err != nil {
  78. return err
  79. }
  80. }
  81. return nil
  82. }
  83. // renderScene renders the 3D scene using the specified camera
  84. func (r *Renderer) renderScene(iscene core.INode, icam camera.ICamera) error {
  85. // Updates world matrices of all scene nodes
  86. iscene.UpdateMatrixWorld()
  87. scene := iscene.GetNode()
  88. // Builds RenderInfo calls RenderSetup for all visible nodes
  89. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  90. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  91. // Clear scene arrays
  92. r.ambLights = r.ambLights[0:0]
  93. r.dirLights = r.dirLights[0:0]
  94. r.pointLights = r.pointLights[0:0]
  95. r.spotLights = r.spotLights[0:0]
  96. r.others = r.others[0:0]
  97. r.grmats = r.grmats[0:0]
  98. // Internal function to classify a node and its children
  99. var classifyNode func(inode core.INode)
  100. classifyNode = func(inode core.INode) {
  101. // If node not visible, ignore
  102. node := inode.GetNode()
  103. if !node.Visible() {
  104. return
  105. }
  106. // Checks if node is a Graphic
  107. igr, ok := inode.(graphic.IGraphic)
  108. if ok {
  109. if igr.Renderable() {
  110. // Appends to list each graphic material for this graphic
  111. gr := igr.GetGraphic()
  112. materials := gr.Materials()
  113. for i := 0; i < len(materials); i++ {
  114. r.grmats = append(r.grmats, &materials[i])
  115. }
  116. }
  117. // Node is not a Graphic
  118. } else {
  119. // Checks if node is a Light
  120. il, ok := inode.(light.ILight)
  121. if ok {
  122. switch l := il.(type) {
  123. case *light.Ambient:
  124. r.ambLights = append(r.ambLights, l)
  125. case *light.Directional:
  126. r.dirLights = append(r.dirLights, l)
  127. case *light.Point:
  128. r.pointLights = append(r.pointLights, l)
  129. case *light.Spot:
  130. r.spotLights = append(r.spotLights, l)
  131. default:
  132. panic("Invalid light type")
  133. }
  134. // Other nodes
  135. } else {
  136. r.others = append(r.others, inode)
  137. }
  138. }
  139. // Classify node children
  140. for _, ichild := range node.Children() {
  141. classifyNode(ichild)
  142. }
  143. }
  144. // Classify all scene nodes
  145. classifyNode(scene)
  146. // Sets lights count in shader specs
  147. r.specs.AmbientLightsMax = len(r.ambLights)
  148. r.specs.DirLightsMax = len(r.dirLights)
  149. r.specs.PointLightsMax = len(r.pointLights)
  150. r.specs.SpotLightsMax = len(r.spotLights)
  151. // Render other nodes (audio players, etc)
  152. for i := 0; i < len(r.others); i++ {
  153. inode := r.others[i]
  154. if !inode.GetNode().Visible() {
  155. continue
  156. }
  157. r.others[i].Render(r.gs)
  158. }
  159. // For each *GraphicMaterial
  160. for _, grmat := range r.grmats {
  161. //log.Debug("grmat:%v", grmat)
  162. mat := grmat.GetMaterial().GetMaterial()
  163. // Sets the shader specs for this material and sets shader program
  164. r.specs.Name = mat.Shader()
  165. r.specs.ShaderUnique = mat.ShaderUnique()
  166. r.specs.UseLights = mat.UseLights()
  167. r.specs.MatTexturesMax = mat.TextureCount()
  168. _, err := r.shaman.SetProgram(&r.specs)
  169. if err != nil {
  170. return err
  171. }
  172. // Setup lights (transfer lights uniforms)
  173. for idx, l := range r.ambLights {
  174. l.RenderSetup(r.gs, &r.rinfo, idx)
  175. }
  176. for idx, l := range r.dirLights {
  177. l.RenderSetup(r.gs, &r.rinfo, idx)
  178. }
  179. for idx, l := range r.pointLights {
  180. l.RenderSetup(r.gs, &r.rinfo, idx)
  181. }
  182. for idx, l := range r.spotLights {
  183. l.RenderSetup(r.gs, &r.rinfo, idx)
  184. }
  185. // Render this graphic material
  186. grmat.Render(r.gs, &r.rinfo)
  187. }
  188. return nil
  189. }
  190. // renderGui renders the Gui
  191. func (r *Renderer) renderGui(icam camera.ICamera) error {
  192. // Updates panels bounds and relative positions
  193. parent := r.gui.GetPanel()
  194. parent.UpdateMatrixWorld()
  195. // Builds RenderInfo calls RenderSetup for all visible nodes
  196. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  197. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  198. var buildRenderList func(ipan gui.IPanel)
  199. buildRenderList = func(ipan gui.IPanel) {
  200. pan := ipan.GetPanel()
  201. if !pan.Visible() {
  202. return
  203. }
  204. gr := pan.GetGraphic()
  205. materials := gr.Materials()
  206. for i := 0; i < len(materials); i++ {
  207. r.grmats = append(r.grmats, &materials[i])
  208. }
  209. for _, ichild := range pan.Children() {
  210. buildRenderList(ichild.(gui.IPanel))
  211. }
  212. }
  213. r.grmats = r.grmats[0:0]
  214. buildRenderList(parent)
  215. // For each *GraphicMaterial
  216. for _, grmat := range r.grmats {
  217. mat := grmat.GetMaterial().GetMaterial()
  218. // Sets the shader specs for this material and sets shader program
  219. r.specs.Name = mat.Shader()
  220. r.specs.ShaderUnique = mat.ShaderUnique()
  221. _, err := r.shaman.SetProgram(&r.specs)
  222. if err != nil {
  223. return err
  224. }
  225. // Render this graphic material
  226. grmat.Render(r.gs, &r.rinfo)
  227. }
  228. return nil
  229. }