renderer.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. }
  225. for idx, l := range r.dirLights {
  226. l.RenderSetup(r.gs, &r.rinfo, idx)
  227. }
  228. for idx, l := range r.pointLights {
  229. l.RenderSetup(r.gs, &r.rinfo, idx)
  230. }
  231. for idx, l := range r.spotLights {
  232. l.RenderSetup(r.gs, &r.rinfo, idx)
  233. }
  234. r.stats.Lights += len(r.ambLights) + len(r.dirLights) + len(r.pointLights) + len(r.spotLights)
  235. // Render this graphic material
  236. grmat.Render(r.gs, &r.rinfo)
  237. r.stats.Graphics++
  238. }
  239. return nil
  240. }
  241. // renderGui renders the Gui
  242. func (r *Renderer) renderGui() error {
  243. // If panel3D was defined and 3D scene is empty
  244. // Sets it renderable as the GUI background
  245. if r.panel3D != nil {
  246. if len(r.grmats) == 0 {
  247. r.panel3D.SetRenderable(true)
  248. } else {
  249. r.panel3D.SetRenderable(false)
  250. }
  251. }
  252. // Clears list of panels to render
  253. r.panList = r.panList[0:0]
  254. // Redraw all GUI elements if necessary by appending the GUI panel to the render list
  255. if r.redrawGui || r.checkChanged(r.panelGui) {
  256. r.appendPanel(r.panelGui)
  257. } else {
  258. r.buildPanelList()
  259. }
  260. // If there are panels to render
  261. if len(r.panList) > 0 {
  262. // Updates panels bounds and relative positions
  263. r.panelGui.GetPanel().UpdateMatrixWorld()
  264. // Disable the scissor test which could have been set by the 3D scene renderer
  265. // and then clear the depth buffer, so the panels will be rendered over the 3D scene.
  266. r.gs.Disable(gls.SCISSOR_TEST)
  267. r.gs.Clear(gls.DEPTH_BUFFER_BIT)
  268. r.rendered = true
  269. }
  270. // Render panels
  271. for i := 0; i < len(r.panList); i++ {
  272. err := r.renderPanel(r.panList[i])
  273. if err != nil {
  274. return err
  275. }
  276. }
  277. return nil
  278. }
  279. // buildPanelList builds list of panels over 3D to be rendered
  280. func (r *Renderer) buildPanelList() {
  281. // If panel3D not set or renderable, nothing to do
  282. if r.panel3D == nil || r.panel3D.Renderable() {
  283. return
  284. }
  285. // Internal recursive function to check if any child of the
  286. // specified panel is unbounded and over 3D.
  287. // If it is, it is inserted in the list of panels to render.
  288. var checkUnbounded func(pan *gui.Panel)
  289. checkUnbounded = func(pan *gui.Panel) {
  290. for i := 0; i < len(pan.Children()); i++ {
  291. child := pan.Children()[i].(gui.IPanel).GetPanel()
  292. if !child.Bounded() && r.checkPanelOver3D(child) {
  293. r.appendPanel(child)
  294. continue
  295. }
  296. checkUnbounded(child)
  297. }
  298. }
  299. // For all children of the Gui, checks if it is over the 3D panel
  300. children := r.panelGui.GetPanel().Children()
  301. for i := 0; i < len(children); i++ {
  302. pan := children[i].(gui.IPanel).GetPanel()
  303. if !pan.Visible() {
  304. continue
  305. }
  306. if r.checkPanelOver3D(pan) {
  307. r.appendPanel(pan)
  308. continue
  309. }
  310. // Current child is not over 3D but can have an unbounded child which is
  311. checkUnbounded(pan)
  312. }
  313. }
  314. // renderPanel renders the specified panel and all its children
  315. // and then sets the panel as not changed.
  316. func (r *Renderer) renderPanel(ipan gui.IPanel) error {
  317. // If panel not visible, ignore it and all its children
  318. pan := ipan.GetPanel()
  319. if !pan.Visible() {
  320. pan.SetChanged(false)
  321. return nil
  322. }
  323. // If panel is renderable, renders it
  324. if pan.Renderable() {
  325. // Sets shader program for the panel's material
  326. grmat := pan.GetGraphic().Materials()[0]
  327. mat := grmat.GetMaterial().GetMaterial()
  328. r.specs.Name = mat.Shader()
  329. r.specs.ShaderUnique = mat.ShaderUnique()
  330. _, err := r.shaman.SetProgram(&r.specs)
  331. if err != nil {
  332. return err
  333. }
  334. // Render this panel's graphic material
  335. grmat.Render(r.gs, &r.rinfo)
  336. r.stats.Panels++
  337. }
  338. pan.SetChanged(false)
  339. // Renders this panel children
  340. for i := 0; i < len(pan.Children()); i++ {
  341. err := r.renderPanel(pan.Children()[i].(gui.IPanel))
  342. if err != nil {
  343. return err
  344. }
  345. }
  346. return nil
  347. }
  348. // appendPanel appends the specified panel to the list of panels to render.
  349. // Currently there is no need to check for duplicates.
  350. func (r *Renderer) appendPanel(ipan gui.IPanel) {
  351. r.panList = append(r.panList, ipan)
  352. }
  353. // checkChanged checks if the specified panel or any of its children is changed
  354. func (r *Renderer) checkChanged(ipan gui.IPanel) bool {
  355. // Unbounded panels are checked even if not visible
  356. pan := ipan.GetPanel()
  357. if !pan.Bounded() && pan.Changed() {
  358. pan.SetChanged(false)
  359. return true
  360. }
  361. // Ignore invisible panel and its children
  362. if !pan.Visible() {
  363. return false
  364. }
  365. if pan.Changed() && pan.Renderable() {
  366. return true
  367. }
  368. for i := 0; i < len(pan.Children()); i++ {
  369. res := r.checkChanged(pan.Children()[i].(gui.IPanel))
  370. if res {
  371. return res
  372. }
  373. }
  374. return false
  375. }
  376. // checkPanelOver3D checks if the specified panel is over
  377. // the area where the 3D scene will be rendered.
  378. func (r *Renderer) checkPanelOver3D(ipan gui.IPanel) bool {
  379. pan := ipan.GetPanel()
  380. if !pan.Visible() {
  381. return false
  382. }
  383. if r.panel3D.GetPanel().Intersects(pan) {
  384. return true
  385. }
  386. return false
  387. }