renderer.go 13 KB

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