renderer.go 13 KB

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