renderer.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. if err != nil {
  77. return err
  78. }
  79. }
  80. return nil
  81. }
  82. // renderScene renders the 3D scene using the specified camera
  83. func (r *Renderer) renderScene(iscene core.INode, icam camera.ICamera) error {
  84. // Updates world matrices of all scene nodes
  85. iscene.UpdateMatrixWorld()
  86. scene := iscene.GetNode()
  87. // Builds RenderInfo calls RenderSetup for all visible nodes
  88. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  89. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  90. // Clear scene arrays
  91. r.ambLights = r.ambLights[0:0]
  92. r.dirLights = r.dirLights[0:0]
  93. r.pointLights = r.pointLights[0:0]
  94. r.spotLights = r.spotLights[0:0]
  95. r.others = r.others[0:0]
  96. r.grmats = r.grmats[0:0]
  97. // Internal function to classify a node and its children
  98. var classifyNode func(inode core.INode)
  99. classifyNode = func(inode core.INode) {
  100. // If node not visible, ignore
  101. node := inode.GetNode()
  102. if !node.Visible() {
  103. return
  104. }
  105. // Checks if node is a Graphic
  106. igr, ok := inode.(graphic.IGraphic)
  107. if ok {
  108. if igr.Renderable() {
  109. // Appends to list each graphic material for this graphic
  110. gr := igr.GetGraphic()
  111. materials := gr.Materials()
  112. for i := 0; i < len(materials); i++ {
  113. r.grmats = append(r.grmats, &materials[i])
  114. }
  115. }
  116. // Node is not a Graphic
  117. } else {
  118. // Checks if node is a Light
  119. il, ok := inode.(light.ILight)
  120. if ok {
  121. switch l := il.(type) {
  122. case *light.Ambient:
  123. r.ambLights = append(r.ambLights, l)
  124. case *light.Directional:
  125. r.dirLights = append(r.dirLights, l)
  126. case *light.Point:
  127. r.pointLights = append(r.pointLights, l)
  128. case *light.Spot:
  129. r.spotLights = append(r.spotLights, l)
  130. default:
  131. panic("Invalid light type")
  132. }
  133. // Other nodes
  134. } else {
  135. r.others = append(r.others, inode)
  136. }
  137. }
  138. // Classify node children
  139. for _, ichild := range node.Children() {
  140. classifyNode(ichild)
  141. }
  142. }
  143. // Classify all scene nodes
  144. classifyNode(scene)
  145. // Sets lights count in shader specs
  146. r.specs.AmbientLightsMax = len(r.ambLights)
  147. r.specs.DirLightsMax = len(r.dirLights)
  148. r.specs.PointLightsMax = len(r.pointLights)
  149. r.specs.SpotLightsMax = len(r.spotLights)
  150. // Render other nodes (audio players, etc)
  151. for i := 0; i < len(r.others); i++ {
  152. inode := r.others[i]
  153. if !inode.GetNode().Visible() {
  154. continue
  155. }
  156. r.others[i].Render(r.gs)
  157. }
  158. // For each *GraphicMaterial
  159. for _, grmat := range r.grmats {
  160. //log.Debug("grmat:%v", grmat)
  161. mat := grmat.GetMaterial().GetMaterial()
  162. // Sets the shader specs for this material and sets shader program
  163. r.specs.Name = mat.Shader()
  164. r.specs.ShaderUnique = mat.ShaderUnique()
  165. r.specs.UseLights = mat.UseLights()
  166. r.specs.MatTexturesMax = mat.TextureCount()
  167. _, err := r.shaman.SetProgram(&r.specs)
  168. if err != nil {
  169. return err
  170. }
  171. // Setup lights (transfer lights uniforms)
  172. for idx, l := range r.ambLights {
  173. l.RenderSetup(r.gs, &r.rinfo, idx)
  174. }
  175. for idx, l := range r.dirLights {
  176. l.RenderSetup(r.gs, &r.rinfo, idx)
  177. }
  178. for idx, l := range r.pointLights {
  179. l.RenderSetup(r.gs, &r.rinfo, idx)
  180. }
  181. for idx, l := range r.spotLights {
  182. l.RenderSetup(r.gs, &r.rinfo, idx)
  183. }
  184. // Render this graphic material
  185. grmat.Render(r.gs, &r.rinfo)
  186. }
  187. return nil
  188. }
  189. // renderGui renders the Gui
  190. func (r *Renderer) renderGui(icam camera.ICamera) error {
  191. // Updates panels bounds and relative positions
  192. parent := r.gui.GetPanel()
  193. parent.UpdateMatrixWorld()
  194. // Builds RenderInfo calls RenderSetup for all visible nodes
  195. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  196. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  197. var buildRenderList func(ipan gui.IPanel)
  198. buildRenderList = func(ipan gui.IPanel) {
  199. pan := ipan.GetPanel()
  200. // If panel is not visible, ignore
  201. if !pan.Visible() {
  202. return
  203. }
  204. // Get panel graphic materials
  205. gr := pan.GetGraphic()
  206. materials := gr.Materials()
  207. for i := 0; i < len(materials); i++ {
  208. r.grmats = append(r.grmats, &materials[i])
  209. }
  210. // Get this panel children
  211. for _, ichild := range pan.Children() {
  212. buildRenderList(ichild.(gui.IPanel))
  213. }
  214. }
  215. // Builds list of panel graphic materials to render
  216. r.grmats = r.grmats[0:0]
  217. buildRenderList(parent)
  218. // For each *GraphicMaterial
  219. for _, grmat := range r.grmats {
  220. mat := grmat.GetMaterial().GetMaterial()
  221. // Sets the shader specs for this material and sets shader program
  222. r.specs.Name = mat.Shader()
  223. r.specs.ShaderUnique = mat.ShaderUnique()
  224. _, err := r.shaman.SetProgram(&r.specs)
  225. if err != nil {
  226. return err
  227. }
  228. // Render this graphic material
  229. grmat.Render(r.gs, &r.rinfo)
  230. }
  231. return nil
  232. }