renderer.go 12 KB

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