renderer.go 13 KB

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