renderer.go 12 KB

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