renderer.go 12 KB

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