renderer.go 9.6 KB

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