renderer.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. // Renderer renders a 3D scene and/or a 2D GUI on the current window.
  14. // This renderer supports the following use cases:
  15. // - Only a 3D scene:
  16. // - SetScene(scene)
  17. // - SetGui(nil) (default)
  18. // - SetGuiPanel3D(nil) (default)
  19. // - Only GUI:
  20. // - SetScene(nil) (default)
  21. // - SetGui(gui)
  22. // - SetGuiPanel3D(nil)
  23. // - GUI with one internal panel showing 3D scene:
  24. // - SetGui(gui)
  25. // - SetScene(scene)
  26. // - SetGuiPanel3D(panel3D)
  27. // - 3D scene with GUI overlay
  28. // - SetScene(scene)
  29. // - SetGui(gui)
  30. // - SetPanel3D(nil) (default)
  31. type Renderer struct {
  32. gs *gls.GLS
  33. shaman Shaman // Internal shader manager
  34. scene core.INode // Node containing 3D scene to render
  35. panelGui gui.IPanel // Panel containing GUI to render
  36. panel3D gui.IPanel // Panel which contains the 3D scene
  37. ambLights []*light.Ambient // Array of ambient lights for last scene
  38. dirLights []*light.Directional // Array of directional lights for last scene
  39. pointLights []*light.Point // Array of point
  40. spotLights []*light.Spot // Array of spot lights for the scene
  41. others []core.INode // Other nodes (audio, players, etc)
  42. grmats []*graphic.GraphicMaterial // Array of all graphic materials for scene
  43. rinfo core.RenderInfo // Preallocated Render info
  44. specs ShaderSpecs // Preallocated Shader specs
  45. redrawGui bool
  46. needSwap bool
  47. panList []gui.IPanel
  48. panRendered int
  49. }
  50. // NewRenderer creates and returns a pointer to a new Renderer
  51. func NewRenderer(gs *gls.GLS) *Renderer {
  52. r := new(Renderer)
  53. r.gs = gs
  54. r.shaman.Init(gs)
  55. r.ambLights = make([]*light.Ambient, 0)
  56. r.dirLights = make([]*light.Directional, 0)
  57. r.pointLights = make([]*light.Point, 0)
  58. r.spotLights = make([]*light.Spot, 0)
  59. r.others = make([]core.INode, 0)
  60. r.grmats = make([]*graphic.GraphicMaterial, 0)
  61. r.panList = make([]gui.IPanel, 0)
  62. return r
  63. }
  64. func (r *Renderer) AddDefaultShaders() error {
  65. return r.shaman.AddDefaultShaders()
  66. }
  67. func (r *Renderer) AddChunk(name, source string) {
  68. r.shaman.AddChunk(name, source)
  69. }
  70. func (r *Renderer) AddShader(name, source string) {
  71. r.shaman.AddShader(name, source)
  72. }
  73. func (r *Renderer) AddProgram(name, vertex, frag string, others ...string) {
  74. r.shaman.AddProgram(name, vertex, frag, others...)
  75. }
  76. // SetGui sets the gui panel which contains the Gui to render.
  77. // If set to nil, no Gui will be rendered
  78. func (r *Renderer) SetGui(gui gui.IPanel) {
  79. r.panelGui = gui
  80. }
  81. // SetGuiPanel3D sets the gui panel inside which the 3D scene is shown.
  82. // This informs the renderer that the Gui elements over this panel
  83. // must be redrawn even if they didn't change.
  84. // This panel panel must not be renderable, otherwise it will cover the 3D scene.
  85. func (r *Renderer) SetGuiPanel3D(panel3D gui.IPanel) {
  86. r.panel3D = panel3D
  87. }
  88. // SetScene sets the 3D scene to render
  89. // If set to nil, no 3D scene will be rendered
  90. func (r *Renderer) SetScene(scene core.INode) {
  91. r.scene = scene
  92. }
  93. func (r *Renderer) NeedSwap() bool {
  94. return r.needSwap
  95. }
  96. // Render renders the previously set Scene and Gui using the specified camera
  97. func (r *Renderer) Render(icam camera.ICamera) error {
  98. r.redrawGui = false
  99. r.needSwap = false
  100. // Renders the 3D scene
  101. if r.scene != nil {
  102. err := r.renderScene(r.scene, icam)
  103. if err != nil {
  104. return err
  105. }
  106. }
  107. // Renders the Gui over the 3D scene
  108. if r.panelGui != nil {
  109. err := r.renderGui(icam)
  110. if err != nil {
  111. return err
  112. }
  113. }
  114. return nil
  115. }
  116. // renderScene renders the 3D scene using the specified camera
  117. func (r *Renderer) renderScene(iscene core.INode, icam camera.ICamera) error {
  118. // Updates world matrices of all scene nodes
  119. iscene.UpdateMatrixWorld()
  120. scene := iscene.GetNode()
  121. // Builds RenderInfo calls RenderSetup for all visible nodes
  122. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  123. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  124. // Clear scene arrays
  125. r.ambLights = r.ambLights[0:0]
  126. r.dirLights = r.dirLights[0:0]
  127. r.pointLights = r.pointLights[0:0]
  128. r.spotLights = r.spotLights[0:0]
  129. r.others = r.others[0:0]
  130. r.grmats = r.grmats[0:0]
  131. // Internal function to classify a node and its children
  132. var classifyNode func(inode core.INode)
  133. classifyNode = func(inode core.INode) {
  134. // If node not visible, ignore
  135. node := inode.GetNode()
  136. if !node.Visible() {
  137. return
  138. }
  139. // Checks if node is a Graphic
  140. igr, ok := inode.(graphic.IGraphic)
  141. if ok {
  142. if igr.Renderable() {
  143. // Appends to list each graphic material for this graphic
  144. gr := igr.GetGraphic()
  145. materials := gr.Materials()
  146. for i := 0; i < len(materials); i++ {
  147. r.grmats = append(r.grmats, &materials[i])
  148. }
  149. }
  150. // Node is not a Graphic
  151. } else {
  152. // Checks if node is a Light
  153. il, ok := inode.(light.ILight)
  154. if ok {
  155. switch l := il.(type) {
  156. case *light.Ambient:
  157. r.ambLights = append(r.ambLights, l)
  158. case *light.Directional:
  159. r.dirLights = append(r.dirLights, l)
  160. case *light.Point:
  161. r.pointLights = append(r.pointLights, l)
  162. case *light.Spot:
  163. r.spotLights = append(r.spotLights, l)
  164. default:
  165. panic("Invalid light type")
  166. }
  167. // Other nodes
  168. } else {
  169. r.others = append(r.others, inode)
  170. }
  171. }
  172. // Classify node children
  173. for _, ichild := range node.Children() {
  174. classifyNode(ichild)
  175. }
  176. }
  177. // Classify all scene nodes
  178. classifyNode(scene)
  179. // Sets lights count in shader specs
  180. r.specs.AmbientLightsMax = len(r.ambLights)
  181. r.specs.DirLightsMax = len(r.dirLights)
  182. r.specs.PointLightsMax = len(r.pointLights)
  183. r.specs.SpotLightsMax = len(r.spotLights)
  184. // Render other nodes (audio players, etc)
  185. for i := 0; i < len(r.others); i++ {
  186. inode := r.others[i]
  187. if !inode.GetNode().Visible() {
  188. continue
  189. }
  190. r.others[i].Render(r.gs)
  191. }
  192. // If there is graphic material to render
  193. if len(r.grmats) > 0 {
  194. //log.Error("graphics:%v", len(r.grmats))
  195. // If the 3D scene to draw is to be confined to user specified panel
  196. // sets scissor to avoid erasing gui elements outside of this panel
  197. if r.panel3D != nil {
  198. pos := r.panel3D.GetPanel().Pospix()
  199. width, height := r.panel3D.GetPanel().Size()
  200. _, _, _, viewheight := r.gs.GetViewport()
  201. r.gs.Enable(gls.SCISSOR_TEST)
  202. r.gs.Scissor(int32(pos.X), viewheight-int32(pos.Y)-int32(height), uint32(width), uint32(height))
  203. } else {
  204. r.gs.Disable(gls.SCISSOR_TEST)
  205. r.redrawGui = true
  206. }
  207. // Clears the area inside the current scissor
  208. r.gs.Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
  209. r.needSwap = true
  210. }
  211. // For each *GraphicMaterial
  212. for _, grmat := range r.grmats {
  213. mat := grmat.GetMaterial().GetMaterial()
  214. // Sets the shader specs for this material and sets shader program
  215. r.specs.Name = mat.Shader()
  216. r.specs.ShaderUnique = mat.ShaderUnique()
  217. r.specs.UseLights = mat.UseLights()
  218. r.specs.MatTexturesMax = mat.TextureCount()
  219. _, err := r.shaman.SetProgram(&r.specs)
  220. if err != nil {
  221. return err
  222. }
  223. // Setup lights (transfer lights uniforms)
  224. for idx, l := range r.ambLights {
  225. l.RenderSetup(r.gs, &r.rinfo, idx)
  226. }
  227. for idx, l := range r.dirLights {
  228. l.RenderSetup(r.gs, &r.rinfo, idx)
  229. }
  230. for idx, l := range r.pointLights {
  231. l.RenderSetup(r.gs, &r.rinfo, idx)
  232. }
  233. for idx, l := range r.spotLights {
  234. l.RenderSetup(r.gs, &r.rinfo, idx)
  235. }
  236. // Render this graphic material
  237. grmat.Render(r.gs, &r.rinfo)
  238. }
  239. return nil
  240. }
  241. // renderGui renders the Gui
  242. func (r *Renderer) renderGui(icam camera.ICamera) error {
  243. // Updates panels bounds and relative positions
  244. parent := r.panelGui.GetPanel()
  245. parent.UpdateMatrixWorld()
  246. // Clears list of panels to render
  247. r.panList = r.panList[0:0]
  248. r.panRendered = 0
  249. // Redraw all GUI elements if necessary by appending the GUI panel to the render list
  250. if r.redrawGui || r.checkPanelUnbounded(r.panelGui) {
  251. r.panList = append(r.panList, r.panelGui)
  252. } else {
  253. r.buildPanelList(r.panelGui, true)
  254. }
  255. // If there are panels to render, disable the scissor test
  256. // which could have been set by the 3D scene renderer and
  257. // then clear the depth buffer, so the panels will be rendered
  258. // over the 3D scene.
  259. if len(r.panList) > 0 {
  260. r.gs.Disable(gls.SCISSOR_TEST)
  261. r.gs.Clear(gls.DEPTH_BUFFER_BIT)
  262. r.needSwap = true
  263. }
  264. // Render panels
  265. for i := 0; i < len(r.panList); i++ {
  266. err := r.renderPanel(r.panList[i])
  267. if err != nil {
  268. return err
  269. }
  270. }
  271. if r.panRendered > 0 {
  272. log.Error("panels rendered:%v", r.panRendered)
  273. }
  274. return nil
  275. }
  276. // buildPanelList builds list of panel materials that must be rendered.
  277. func (r *Renderer) buildPanelList(ipan gui.IPanel, check3D bool) {
  278. // If panel is not visible, ignore it and all its children
  279. pan := ipan.GetPanel()
  280. if !pan.Visible() {
  281. return
  282. }
  283. // If this panel is not the main GUI panel checks if its over 3D.
  284. // If it is appends to the render list and we are done.
  285. // Otherwise do not check over 3D for its children because if the
  286. // parent is not over 3D than its children are also not over 3D.
  287. // The exception to this case is when the panel is unbounded,
  288. // which is verified in the next check.
  289. if ipan != r.panelGui {
  290. if check3D && r.checkPanelOver3D(ipan) {
  291. r.panList = append(r.panList, ipan)
  292. log.Error("panel not over 3D")
  293. return
  294. } else {
  295. check3D = false
  296. }
  297. }
  298. // If panel is unbounded and is over 3D, appends to the render list
  299. if !pan.Bounded() && r.checkPanelOver3D(ipan) {
  300. r.panList = append(r.panList, ipan)
  301. log.Error("unbounded over 3D")
  302. return
  303. }
  304. // If any of this panel immediate children changed, appends to the render list
  305. if r.checkPanelChildren(ipan) {
  306. r.panList = append(r.panList, ipan)
  307. return
  308. }
  309. //// If this panel is renderable and changed, appends to the render list
  310. //if pan.Renderable() && pan.Changed() {
  311. // r.panList = append(r.panList, ipan)
  312. // log.Error("panel changed")
  313. // return
  314. //}
  315. // Checks this panel children
  316. for _, ichild := range pan.Children() {
  317. r.buildPanelList(ichild.(gui.IPanel), check3D)
  318. }
  319. }
  320. // renderPanel renders the specified panel and all its children
  321. // and then sets the panel as not changed.
  322. func (r *Renderer) renderPanel(ipan gui.IPanel) error {
  323. // If panel not visible, ignore it and all its children
  324. pan := ipan.GetPanel()
  325. if !pan.Visible() {
  326. return nil
  327. }
  328. // If panel is renderable, renders it
  329. if pan.Renderable() {
  330. // Sets shader program for the panel's material
  331. grmat := pan.GetGraphic().Materials()[0]
  332. mat := grmat.GetMaterial().GetMaterial()
  333. r.specs.Name = mat.Shader()
  334. r.specs.ShaderUnique = mat.ShaderUnique()
  335. _, err := r.shaman.SetProgram(&r.specs)
  336. if err != nil {
  337. return err
  338. }
  339. // Render this panel's graphic material
  340. grmat.Render(r.gs, &r.rinfo)
  341. pan.SetChanged(false)
  342. r.panRendered++
  343. }
  344. // Renders this panel children
  345. for i := 0; i < len(pan.Children()); i++ {
  346. err := r.renderPanel(pan.Children()[i].(gui.IPanel))
  347. if err != nil {
  348. return err
  349. }
  350. }
  351. return nil
  352. }
  353. // checkPanelOver3D checks if the specified panel is over
  354. // the area where the 3D scene will be rendered.
  355. func (r *Renderer) checkPanelOver3D(ipan gui.IPanel) bool {
  356. if r.panel3D == nil || len(r.panel3D.GetPanel().Children()) == 0 {
  357. return false
  358. }
  359. pan := ipan.GetPanel()
  360. if !pan.Visible() {
  361. return false
  362. }
  363. if r.panel3D.GetPanel().Intersects(pan) {
  364. log.Error("panel..: %v %v %v", pan.Pospix(), pan.Width(), pan.Height())
  365. panel3D := r.panel3D.GetPanel()
  366. log.Error("panel3D: %v %v %v", panel3D.Pospix(), panel3D.Width(), panel3D.Height())
  367. return true
  368. }
  369. return false
  370. }
  371. // checkPanelUnbounded checks if the specified panel or any
  372. // of its children has changed and it is unbounded
  373. func (r *Renderer) checkPanelUnbounded(ipan gui.IPanel) bool {
  374. pan := ipan.GetPanel()
  375. if !pan.Visible() {
  376. return false
  377. }
  378. if pan.Changed() && !pan.Bounded() {
  379. log.Error("panel unbounded changed")
  380. return true
  381. }
  382. for _, ichild := range pan.Children() {
  383. if r.checkPanelUnbounded(ichild.(gui.IPanel)) {
  384. return true
  385. }
  386. }
  387. return false
  388. }
  389. // checkPanelChildren checks if any of this panel immediate children has changed
  390. func (r *Renderer) checkPanelChildren(ipan gui.IPanel) bool {
  391. pan := ipan.GetPanel()
  392. for _, ichild := range pan.Children() {
  393. child := ichild.(gui.IPanel).GetPanel()
  394. if !child.Visible() || !child.Renderable() {
  395. continue
  396. }
  397. if child.Changed() {
  398. log.Error("panel children changed")
  399. return true
  400. }
  401. }
  402. return false
  403. }