renderer.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. clearScreen bool // Clear screen flag
  27. needSwap bool
  28. }
  29. func NewRenderer(gs *gls.GLS) *Renderer {
  30. r := new(Renderer)
  31. r.gs = gs
  32. r.shaman.Init(gs)
  33. r.ambLights = make([]*light.Ambient, 0)
  34. r.dirLights = make([]*light.Directional, 0)
  35. r.pointLights = make([]*light.Point, 0)
  36. r.spotLights = make([]*light.Spot, 0)
  37. r.others = make([]core.INode, 0)
  38. r.grmats = make([]*graphic.GraphicMaterial, 0)
  39. return r
  40. }
  41. func (r *Renderer) AddDefaultShaders() error {
  42. return r.shaman.AddDefaultShaders()
  43. }
  44. func (r *Renderer) AddChunk(name, source string) {
  45. r.shaman.AddChunk(name, source)
  46. }
  47. func (r *Renderer) AddShader(name, source string) {
  48. r.shaman.AddShader(name, source)
  49. }
  50. func (r *Renderer) AddProgram(name, vertex, frag string, others ...string) {
  51. r.shaman.AddProgram(name, vertex, frag, others...)
  52. }
  53. // SetGui sets the gui panel which contains the Gui to render
  54. // over the optional 3D scene.
  55. // If set to nil, no Gui will be rendered
  56. func (r *Renderer) SetGui(gui gui.IPanel) {
  57. r.gui = gui
  58. }
  59. // SetScene sets the Node which contains the scene to render
  60. // If set to nil, no scene will be rendered
  61. func (r *Renderer) SetScene(scene core.INode) {
  62. r.scene = scene
  63. }
  64. func (r *Renderer) NeedSwap() bool {
  65. return r.needSwap
  66. }
  67. // Render renders the previously set Scene and Gui using the specified camera
  68. func (r *Renderer) Render(icam camera.ICamera) error {
  69. r.needSwap = false
  70. // Renders the 3D scene
  71. if r.scene != nil {
  72. //r.gs.Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
  73. err := r.renderScene(r.scene, icam)
  74. if err != nil {
  75. return err
  76. }
  77. }
  78. // Renders the Gui over the 3D scene
  79. if r.gui != nil {
  80. r.gs.Clear(gls.DEPTH_BUFFER_BIT)
  81. err := r.renderGui(icam)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return nil
  87. }
  88. // renderScene renders the 3D scene using the specified camera
  89. func (r *Renderer) renderScene(iscene core.INode, icam camera.ICamera) error {
  90. // Updates world matrices of all scene nodes
  91. iscene.UpdateMatrixWorld()
  92. scene := iscene.GetNode()
  93. // Builds RenderInfo calls RenderSetup for all visible nodes
  94. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  95. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  96. // Clear scene arrays
  97. r.ambLights = r.ambLights[0:0]
  98. r.dirLights = r.dirLights[0:0]
  99. r.pointLights = r.pointLights[0:0]
  100. r.spotLights = r.spotLights[0:0]
  101. r.others = r.others[0:0]
  102. r.grmats = r.grmats[0:0]
  103. // Internal function to classify a node and its children
  104. var classifyNode func(inode core.INode)
  105. classifyNode = func(inode core.INode) {
  106. // If node not visible, ignore
  107. node := inode.GetNode()
  108. if !node.Visible() {
  109. return
  110. }
  111. // Checks if node is a Graphic
  112. igr, ok := inode.(graphic.IGraphic)
  113. if ok {
  114. if igr.Renderable() {
  115. // Appends to list each graphic material for this graphic
  116. gr := igr.GetGraphic()
  117. materials := gr.Materials()
  118. for i := 0; i < len(materials); i++ {
  119. r.grmats = append(r.grmats, &materials[i])
  120. }
  121. }
  122. // Node is not a Graphic
  123. } else {
  124. // Checks if node is a Light
  125. il, ok := inode.(light.ILight)
  126. if ok {
  127. switch l := il.(type) {
  128. case *light.Ambient:
  129. r.ambLights = append(r.ambLights, l)
  130. case *light.Directional:
  131. r.dirLights = append(r.dirLights, l)
  132. case *light.Point:
  133. r.pointLights = append(r.pointLights, l)
  134. case *light.Spot:
  135. r.spotLights = append(r.spotLights, l)
  136. default:
  137. panic("Invalid light type")
  138. }
  139. // Other nodes
  140. } else {
  141. r.others = append(r.others, inode)
  142. }
  143. }
  144. // Classify node children
  145. for _, ichild := range node.Children() {
  146. classifyNode(ichild)
  147. }
  148. }
  149. // Classify all scene nodes
  150. classifyNode(scene)
  151. // Sets lights count in shader specs
  152. r.specs.AmbientLightsMax = len(r.ambLights)
  153. r.specs.DirLightsMax = len(r.dirLights)
  154. r.specs.PointLightsMax = len(r.pointLights)
  155. r.specs.SpotLightsMax = len(r.spotLights)
  156. // Render other nodes (audio players, etc)
  157. for i := 0; i < len(r.others); i++ {
  158. inode := r.others[i]
  159. if !inode.GetNode().Visible() {
  160. continue
  161. }
  162. r.others[i].Render(r.gs)
  163. }
  164. if len(r.grmats) > 0 {
  165. r.gs.Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
  166. r.needSwap = true
  167. log.Error("Clear screen")
  168. }
  169. // For each *GraphicMaterial
  170. for _, grmat := range r.grmats {
  171. //log.Debug("grmat:%v", grmat)
  172. mat := grmat.GetMaterial().GetMaterial()
  173. // Sets the shader specs for this material and sets shader program
  174. r.specs.Name = mat.Shader()
  175. r.specs.ShaderUnique = mat.ShaderUnique()
  176. r.specs.UseLights = mat.UseLights()
  177. r.specs.MatTexturesMax = mat.TextureCount()
  178. _, err := r.shaman.SetProgram(&r.specs)
  179. if err != nil {
  180. return err
  181. }
  182. // Setup lights (transfer lights uniforms)
  183. for idx, l := range r.ambLights {
  184. l.RenderSetup(r.gs, &r.rinfo, idx)
  185. }
  186. for idx, l := range r.dirLights {
  187. l.RenderSetup(r.gs, &r.rinfo, idx)
  188. }
  189. for idx, l := range r.pointLights {
  190. l.RenderSetup(r.gs, &r.rinfo, idx)
  191. }
  192. for idx, l := range r.spotLights {
  193. l.RenderSetup(r.gs, &r.rinfo, idx)
  194. }
  195. // Render this graphic material
  196. grmat.Render(r.gs, &r.rinfo)
  197. }
  198. return nil
  199. }
  200. // renderGui renders the Gui
  201. func (r *Renderer) renderGui(icam camera.ICamera) error {
  202. // Updates panels bounds and relative positions
  203. parent := r.gui.GetPanel()
  204. parent.UpdateMatrixWorld()
  205. // Builds RenderInfo calls RenderSetup for all visible nodes
  206. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  207. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  208. // checkBounded checks if any of this panel children has
  209. // changed and it is not bounded
  210. var checkBounded func(ipan gui.IPanel) bool
  211. checkBounded = func(ipan gui.IPanel) bool {
  212. pan := ipan.GetPanel()
  213. if !pan.Visible() {
  214. return false
  215. }
  216. if pan.Changed() && !pan.Bounded() {
  217. return true
  218. }
  219. for _, ichild := range pan.Children() {
  220. if checkBounded(ichild.(gui.IPanel)) {
  221. return true
  222. }
  223. }
  224. return false
  225. }
  226. // checkChildren checks if any of this panel immediate children has changed
  227. checkChildren := func(ipan gui.IPanel) bool {
  228. pan := ipan.GetPanel()
  229. for _, ichild := range pan.Children() {
  230. child := ichild.(gui.IPanel).GetPanel()
  231. if !child.Visible() || !child.Renderable() {
  232. continue
  233. }
  234. if child.Changed() {
  235. return true
  236. }
  237. }
  238. return false
  239. }
  240. var buildRenderList func(ipan gui.IPanel, checkChanged bool)
  241. buildRenderList = func(ipan gui.IPanel, checkChanged bool) {
  242. pan := ipan.GetPanel()
  243. // If panel is not visible, ignore
  244. if !pan.Visible() {
  245. return
  246. }
  247. // If no check or if any of this panel immediate children changed,
  248. // inserts this panel if renderable and all its visible/renderable children
  249. if !checkChanged || checkChildren(ipan) || checkBounded(ipan) {
  250. if pan.Renderable() {
  251. materials := pan.GetGraphic().Materials()
  252. r.grmats = append(r.grmats, &materials[0])
  253. }
  254. for _, ichild := range pan.Children() {
  255. buildRenderList(ichild.(gui.IPanel), false)
  256. }
  257. pan.SetChanged(false)
  258. return
  259. }
  260. // If this panel is renderable and changed, inserts this panel
  261. if pan.Renderable() && pan.Changed() {
  262. materials := pan.GetGraphic().Materials()
  263. r.grmats = append(r.grmats, &materials[0])
  264. }
  265. // Checks this panel children
  266. for _, ichild := range pan.Children() {
  267. buildRenderList(ichild.(gui.IPanel), true)
  268. }
  269. pan.SetChanged(false)
  270. }
  271. // Builds list of panel graphic materials to render
  272. r.grmats = r.grmats[0:0]
  273. buildRenderList(parent, true)
  274. if len(r.grmats) > 0 {
  275. log.Error("render list:%v", len(r.grmats))
  276. }
  277. // For each *GraphicMaterial
  278. for _, grmat := range r.grmats {
  279. mat := grmat.GetMaterial().GetMaterial()
  280. // Sets the shader specs for this material and sets shader program
  281. r.specs.Name = mat.Shader()
  282. r.specs.ShaderUnique = mat.ShaderUnique()
  283. _, err := r.shaman.SetProgram(&r.specs)
  284. if err != nil {
  285. return err
  286. }
  287. // Render this graphic material
  288. grmat.Render(r.gs, &r.rinfo)
  289. r.needSwap = true
  290. }
  291. return nil
  292. }