renderer.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 over the 3D scene
  14. // on the current window.
  15. type Renderer struct {
  16. gs *gls.GLS
  17. shaman Shaman // Internal shader manager
  18. scene core.INode // Node containing 3D scene to render
  19. gui 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. screenCleared bool
  30. needSwap bool
  31. }
  32. // NewRenderer creates and returns a pointer to a new Renderer
  33. func NewRenderer(gs *gls.GLS) *Renderer {
  34. r := new(Renderer)
  35. r.gs = gs
  36. r.shaman.Init(gs)
  37. r.ambLights = make([]*light.Ambient, 0)
  38. r.dirLights = make([]*light.Directional, 0)
  39. r.pointLights = make([]*light.Point, 0)
  40. r.spotLights = make([]*light.Spot, 0)
  41. r.others = make([]core.INode, 0)
  42. r.grmats = make([]*graphic.GraphicMaterial, 0)
  43. return r
  44. }
  45. func (r *Renderer) AddDefaultShaders() error {
  46. return r.shaman.AddDefaultShaders()
  47. }
  48. func (r *Renderer) AddChunk(name, source string) {
  49. r.shaman.AddChunk(name, source)
  50. }
  51. func (r *Renderer) AddShader(name, source string) {
  52. r.shaman.AddShader(name, source)
  53. }
  54. func (r *Renderer) AddProgram(name, vertex, frag string, others ...string) {
  55. r.shaman.AddProgram(name, vertex, frag, others...)
  56. }
  57. // SetGui sets the gui panel which contains the Gui to render
  58. // over the optional 3D scene.
  59. // If set to nil, no Gui will be rendered
  60. func (r *Renderer) SetGui(gui gui.IPanel) {
  61. r.gui = gui
  62. }
  63. // SetGuiPanel3D sets the gui panel inside which the 3D scene is shown.
  64. func (r *Renderer) SetGuiPanel3D(panel3D gui.IPanel) {
  65. r.panel3D = panel3D
  66. }
  67. // SetScene sets the Node which contains the scene to render
  68. // If set to nil, no scene will be rendered
  69. func (r *Renderer) SetScene(scene core.INode) {
  70. r.scene = scene
  71. }
  72. func (r *Renderer) NeedSwap() bool {
  73. return r.needSwap
  74. }
  75. // Render renders the previously set Scene and Gui using the specified camera
  76. func (r *Renderer) Render(icam camera.ICamera) error {
  77. r.screenCleared = false
  78. r.needSwap = false
  79. // Renders the 3D scene
  80. if r.scene != nil {
  81. err := r.renderScene(r.scene, icam)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. // Renders the Gui over the 3D scene
  87. if r.gui != nil {
  88. err := r.renderGui(icam)
  89. if err != nil {
  90. return err
  91. }
  92. }
  93. return nil
  94. }
  95. // renderScene renders the 3D scene using the specified camera
  96. func (r *Renderer) renderScene(iscene core.INode, icam camera.ICamera) error {
  97. // Updates world matrices of all scene nodes
  98. iscene.UpdateMatrixWorld()
  99. scene := iscene.GetNode()
  100. // Builds RenderInfo calls RenderSetup for all visible nodes
  101. icam.ViewMatrix(&r.rinfo.ViewMatrix)
  102. icam.ProjMatrix(&r.rinfo.ProjMatrix)
  103. // Clear scene arrays
  104. r.ambLights = r.ambLights[0:0]
  105. r.dirLights = r.dirLights[0:0]
  106. r.pointLights = r.pointLights[0:0]
  107. r.spotLights = r.spotLights[0:0]
  108. r.others = r.others[0:0]
  109. r.grmats = r.grmats[0:0]
  110. // Internal function to classify a node and its children
  111. var classifyNode func(inode core.INode)
  112. classifyNode = func(inode core.INode) {
  113. // If node not visible, ignore
  114. node := inode.GetNode()
  115. if !node.Visible() {
  116. return
  117. }
  118. // Checks if node is a Graphic
  119. igr, ok := inode.(graphic.IGraphic)
  120. if ok {
  121. if igr.Renderable() {
  122. // Appends to list each graphic material for this graphic
  123. gr := igr.GetGraphic()
  124. materials := gr.Materials()
  125. for i := 0; i < len(materials); i++ {
  126. r.grmats = append(r.grmats, &materials[i])
  127. }
  128. }
  129. // Node is not a Graphic
  130. } else {
  131. // Checks if node is a Light
  132. il, ok := inode.(light.ILight)
  133. if ok {
  134. switch l := il.(type) {
  135. case *light.Ambient:
  136. r.ambLights = append(r.ambLights, l)
  137. case *light.Directional:
  138. r.dirLights = append(r.dirLights, l)
  139. case *light.Point:
  140. r.pointLights = append(r.pointLights, l)
  141. case *light.Spot:
  142. r.spotLights = append(r.spotLights, l)
  143. default:
  144. panic("Invalid light type")
  145. }
  146. // Other nodes
  147. } else {
  148. r.others = append(r.others, inode)
  149. }
  150. }
  151. // Classify node children
  152. for _, ichild := range node.Children() {
  153. classifyNode(ichild)
  154. }
  155. }
  156. // Classify all scene nodes
  157. classifyNode(scene)
  158. // Sets lights count in shader specs
  159. r.specs.AmbientLightsMax = len(r.ambLights)
  160. r.specs.DirLightsMax = len(r.dirLights)
  161. r.specs.PointLightsMax = len(r.pointLights)
  162. r.specs.SpotLightsMax = len(r.spotLights)
  163. // Render other nodes (audio players, etc)
  164. for i := 0; i < len(r.others); i++ {
  165. inode := r.others[i]
  166. if !inode.GetNode().Visible() {
  167. continue
  168. }
  169. r.others[i].Render(r.gs)
  170. }
  171. // If there is graphic material to render
  172. if len(r.grmats) > 0 {
  173. if r.panel3D != nil {
  174. pos := r.panel3D.GetPanel().Pospix()
  175. width, height := r.panel3D.GetPanel().Size()
  176. _, _, _, viewheight := r.gs.GetViewport()
  177. r.gs.Enable(gls.SCISSOR_TEST)
  178. r.gs.Scissor(int32(pos.X), viewheight-int32(pos.Y)-int32(height), uint32(width), uint32(height))
  179. } else {
  180. r.gs.Disable(gls.SCISSOR_TEST)
  181. r.screenCleared = true
  182. }
  183. // Clears the area inside the current scissor
  184. r.gs.Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
  185. r.needSwap = true
  186. }
  187. // For each *GraphicMaterial
  188. for _, grmat := range r.grmats {
  189. //log.Debug("grmat:%v", grmat)
  190. mat := grmat.GetMaterial().GetMaterial()
  191. // Sets the shader specs for this material and sets shader program
  192. r.specs.Name = mat.Shader()
  193. r.specs.ShaderUnique = mat.ShaderUnique()
  194. r.specs.UseLights = mat.UseLights()
  195. r.specs.MatTexturesMax = mat.TextureCount()
  196. _, err := r.shaman.SetProgram(&r.specs)
  197. if err != nil {
  198. return err
  199. }
  200. // Setup lights (transfer lights uniforms)
  201. for idx, l := range r.ambLights {
  202. l.RenderSetup(r.gs, &r.rinfo, idx)
  203. }
  204. for idx, l := range r.dirLights {
  205. l.RenderSetup(r.gs, &r.rinfo, idx)
  206. }
  207. for idx, l := range r.pointLights {
  208. l.RenderSetup(r.gs, &r.rinfo, idx)
  209. }
  210. for idx, l := range r.spotLights {
  211. l.RenderSetup(r.gs, &r.rinfo, idx)
  212. }
  213. // Render this graphic material
  214. grmat.Render(r.gs, &r.rinfo)
  215. }
  216. return nil
  217. }
  218. // renderGui renders the Gui
  219. func (r *Renderer) renderGui(icam camera.ICamera) error {
  220. // Updates panels bounds and relative positions
  221. parent := r.gui.GetPanel()
  222. parent.UpdateMatrixWorld()
  223. // Builds list of panel's graphic materials to render
  224. // If all the screen has been cleared by the 3D scene,
  225. // no need to check panels. All must be redrawn.
  226. r.grmats = r.grmats[0:0]
  227. checkChanged := !r.screenCleared
  228. r.buildPanelList(parent, checkChanged)
  229. // If there are panels to render, disable the scissor test
  230. // which could have been set by the 3D scene renderer and
  231. // then clear the depth buffer, so the panels will be rendered
  232. // over the 3D scene.
  233. if len(r.grmats) > 0 {
  234. log.Error("render list:%v", len(r.grmats))
  235. r.gs.Disable(gls.SCISSOR_TEST)
  236. r.gs.Clear(gls.DEPTH_BUFFER_BIT)
  237. r.needSwap = true
  238. }
  239. // For each *GraphicMaterial
  240. for _, grmat := range r.grmats {
  241. // Sets shader program for this material
  242. mat := grmat.GetMaterial().GetMaterial()
  243. r.specs.Name = mat.Shader()
  244. r.specs.ShaderUnique = mat.ShaderUnique()
  245. _, err := r.shaman.SetProgram(&r.specs)
  246. if err != nil {
  247. return err
  248. }
  249. // Render this graphic material
  250. grmat.Render(r.gs, &r.rinfo)
  251. }
  252. return nil
  253. }
  254. // buildPanelList builds list of panel materials that must be rendered.
  255. // If checkChanged is false the specified panel and all its children
  256. // recursively will be appended to the list inconditionally.
  257. // Otherwise the following criteria is used:
  258. // - If the panel is changed, itself and all its children recursively
  259. // are appended to list
  260. // - If any the panel immediate children is changed the panel itself
  261. // and the immediate children are appended to the list
  262. func (r *Renderer) buildPanelList(ipan gui.IPanel, checkChanged bool) {
  263. pan := ipan.GetPanel()
  264. // If panel is not visible, ignore
  265. if !pan.Visible() {
  266. return
  267. }
  268. // If no check or if any of this panel immediate children changed,
  269. // inserts this panel if renderable and all its visible/renderable children
  270. if !checkChanged || r.checkPanelOver3D(ipan) || r.checkPanelChildren(ipan) || r.checkPanelUnbounded(ipan) {
  271. if pan.Renderable() {
  272. materials := pan.GetGraphic().Materials()
  273. r.grmats = append(r.grmats, &materials[0])
  274. }
  275. for _, ichild := range pan.Children() {
  276. r.buildPanelList(ichild.(gui.IPanel), false)
  277. }
  278. pan.SetChanged(false)
  279. return
  280. }
  281. // If this panel is renderable and changed, inserts this panel
  282. if pan.Renderable() && pan.Changed() {
  283. materials := pan.GetGraphic().Materials()
  284. r.grmats = append(r.grmats, &materials[0])
  285. }
  286. // Checks this panel children
  287. for _, ichild := range pan.Children() {
  288. r.buildPanelList(ichild.(gui.IPanel), true)
  289. }
  290. pan.SetChanged(false)
  291. }
  292. // checkPanelOver3D checks if the specified panel is over
  293. // the area where the 3D scene will be rendered.
  294. func (r *Renderer) checkPanelOver3D(ipan gui.IPanel) bool {
  295. if r.panel3D == nil {
  296. return false
  297. }
  298. pan := r.gui.GetPanel()
  299. if !pan.Visible() || !pan.Renderable() {
  300. return false
  301. }
  302. res := pan.Intersects(r.panel3D.GetPanel())
  303. if res {
  304. log.Error("panel over 3D:%v %v %v / %v %v %v", pan.Pospix(), pan.Width(), pan.Height(),
  305. r.panel3D.GetPanel().Pospix(), r.panel3D.GetPanel().Width(), r.panel3D.GetPanel().Height())
  306. }
  307. return res
  308. }
  309. // checkPanelUnbounded checks if any of the specified panel children has
  310. // changed and it is not bounded
  311. func (r *Renderer) checkPanelUnbounded(ipan gui.IPanel) bool {
  312. pan := ipan.GetPanel()
  313. if !pan.Visible() {
  314. return false
  315. }
  316. if pan.Changed() && !pan.Bounded() {
  317. return true
  318. }
  319. for _, ichild := range pan.Children() {
  320. if r.checkPanelUnbounded(ichild.(gui.IPanel)) {
  321. return true
  322. }
  323. }
  324. return false
  325. }
  326. // checkPanelChildren checks if any of this panel immediate children has changed
  327. func (r *Renderer) checkPanelChildren(ipan gui.IPanel) bool {
  328. pan := ipan.GetPanel()
  329. for _, ichild := range pan.Children() {
  330. child := ichild.(gui.IPanel).GetPanel()
  331. if !child.Visible() || !child.Renderable() {
  332. continue
  333. }
  334. if child.Changed() {
  335. return true
  336. }
  337. }
  338. return false
  339. }