graphic.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 graphic
  5. import (
  6. "github.com/g3n/engine/core"
  7. "github.com/g3n/engine/geometry"
  8. "github.com/g3n/engine/gls"
  9. "github.com/g3n/engine/material"
  10. "github.com/g3n/engine/math32"
  11. )
  12. // Graphic is a Node which has a visible representation in the scene.
  13. // It has an associated geometry and one or more materials.
  14. // It is the base type used by other graphics such as lines, line_strip,
  15. // points and meshes.
  16. type Graphic struct {
  17. core.Node // Embedded Node
  18. igeom geometry.IGeometry // Associated IGeometry
  19. materials []GraphicMaterial // Materials
  20. mode uint32 // OpenGL primitive
  21. renderable bool // Renderable flag
  22. cullable bool // Cullable flag
  23. renderOrder int // Render order
  24. ShaderDefines gls.ShaderDefines // Graphic-specific shader defines
  25. mm math32.Matrix4 // Cached Model matrix
  26. mvm math32.Matrix4 // Cached ModelView matrix
  27. mvpm math32.Matrix4 // Cached ModelViewProjection matrix
  28. }
  29. // GraphicMaterial specifies the material to be used for
  30. // a subset of vertices from the Graphic geometry
  31. // A Graphic object has at least one GraphicMaterial.
  32. type GraphicMaterial struct {
  33. imat material.IMaterial // Associated material
  34. start int // Index of first element in the geometry
  35. count int // Number of elements
  36. igraphic IGraphic // Graphic which contains this GraphicMaterial
  37. }
  38. // IGraphic is the interface for all Graphic objects.
  39. type IGraphic interface {
  40. core.INode
  41. GetGraphic() *Graphic
  42. GetGeometry() *geometry.Geometry
  43. IGeometry() geometry.IGeometry
  44. SetRenderable(bool)
  45. Renderable() bool
  46. SetCullable(bool)
  47. Cullable() bool
  48. RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo)
  49. }
  50. // NewGraphic creates and returns a pointer to a new graphic object with
  51. // the specified geometry and OpenGL primitive.
  52. // The created graphic object, though, has not materials.
  53. func NewGraphic(igeom geometry.IGeometry, mode uint32) *Graphic {
  54. gr := new(Graphic)
  55. return gr.Init(igeom, mode)
  56. }
  57. // Init initializes a Graphic type embedded in another type
  58. // with the specified geometry and OpenGL mode.
  59. func (gr *Graphic) Init(igeom geometry.IGeometry, mode uint32) *Graphic {
  60. gr.Node.Init()
  61. gr.igeom = igeom
  62. gr.mode = mode
  63. gr.materials = make([]GraphicMaterial, 0)
  64. gr.renderable = true
  65. gr.cullable = true
  66. gr.ShaderDefines = *gls.NewShaderDefines()
  67. return gr
  68. }
  69. // GetGraphic satisfies the IGraphic interface and
  70. // returns pointer to the base Graphic.
  71. func (gr *Graphic) GetGraphic() *Graphic {
  72. return gr
  73. }
  74. // GetGeometry satisfies the IGraphic interface and returns
  75. // a pointer to the geometry associated with this graphic.
  76. func (gr *Graphic) GetGeometry() *geometry.Geometry {
  77. return gr.igeom.GetGeometry()
  78. }
  79. // IGeometry satisfies the IGraphic interface and returns
  80. // a pointer to the IGeometry associated with this graphic.
  81. func (gr *Graphic) IGeometry() geometry.IGeometry {
  82. return gr.igeom
  83. }
  84. // Dispose overrides the embedded Node Dispose method.
  85. func (gr *Graphic) Dispose() {
  86. gr.igeom.Dispose()
  87. for i := 0; i < len(gr.materials); i++ {
  88. gr.materials[i].imat.Dispose()
  89. }
  90. }
  91. // Clone clones the graphic and satisfies the INode interface.
  92. // It should be called by Clone() implementations of IGraphic.
  93. // Note that the topmost implementation calling this method needs
  94. // to call clone.SetIGraphic(igraphic) after calling this method.
  95. func (gr *Graphic) Clone() core.INode {
  96. clone := new(Graphic)
  97. clone.Node = *gr.Node.Clone().(*core.Node)
  98. clone.igeom = gr.igeom
  99. clone.mode = gr.mode
  100. clone.renderable = gr.renderable
  101. clone.cullable = gr.cullable
  102. clone.renderOrder = gr.renderOrder
  103. clone.ShaderDefines = gr.ShaderDefines
  104. clone.materials = make([]GraphicMaterial, len(gr.materials))
  105. for i, grmat := range gr.materials {
  106. clone.materials[i] = grmat
  107. }
  108. return clone
  109. }
  110. // SetRenderable satisfies the IGraphic interface and
  111. // sets the renderable state of this Graphic (default = true).
  112. func (gr *Graphic) SetRenderable(state bool) {
  113. gr.renderable = state
  114. }
  115. // Renderable satisfies the IGraphic interface and
  116. // returns the renderable state of this graphic.
  117. func (gr *Graphic) Renderable() bool {
  118. return gr.renderable
  119. }
  120. // SetCullable satisfies the IGraphic interface and
  121. // sets the cullable state of this Graphic (default = true).
  122. func (gr *Graphic) SetCullable(state bool) {
  123. gr.cullable = state
  124. }
  125. // Cullable satisfies the IGraphic interface and
  126. // returns the cullable state of this graphic.
  127. func (gr *Graphic) Cullable() bool {
  128. return gr.cullable
  129. }
  130. // SetRenderOrder sets the render order of the object.
  131. // All objects have renderOrder of 0 by default.
  132. // To render before renderOrder 0 set a lower renderOrder e.g. -1.
  133. // To render after renderOrder 0 set a higher renderOrder e.g. 1
  134. func (gr *Graphic) SetRenderOrder(order int) {
  135. gr.renderOrder = order
  136. }
  137. // RenderOrder returns the render order of the object.
  138. func (gr *Graphic) RenderOrder() int {
  139. return gr.renderOrder
  140. }
  141. // AddMaterial adds a material for the specified subset of vertices.
  142. // If the material applies to all vertices, start and count must be 0.
  143. func (gr *Graphic) AddMaterial(igr IGraphic, imat material.IMaterial, start, count int) {
  144. gmat := GraphicMaterial{
  145. imat: imat,
  146. start: start,
  147. count: count,
  148. igraphic: igr,
  149. }
  150. gr.materials = append(gr.materials, gmat)
  151. }
  152. // AddGroupMaterial adds a material for the specified geometry group.
  153. func (gr *Graphic) AddGroupMaterial(igr IGraphic, imat material.IMaterial, gindex int) {
  154. geom := gr.igeom.GetGeometry()
  155. if gindex < 0 || gindex >= geom.GroupCount() {
  156. panic("Invalid group index")
  157. }
  158. group := geom.GroupAt(gindex)
  159. gr.AddMaterial(igr, imat, group.Start, group.Count)
  160. }
  161. // Materials returns slice with this graphic materials.
  162. func (gr *Graphic) Materials() []GraphicMaterial {
  163. return gr.materials
  164. }
  165. // GetMaterial returns the material associated with the specified vertex position.
  166. func (gr *Graphic) GetMaterial(vpos int) material.IMaterial {
  167. for _, gmat := range gr.materials {
  168. // Case for unimaterial
  169. if gmat.count == 0 {
  170. return gmat.imat
  171. }
  172. if gmat.start >= vpos && gmat.start+gmat.count <= vpos {
  173. return gmat.imat
  174. }
  175. }
  176. return nil
  177. }
  178. // ClearMaterials removes all the materials from this Graphic.
  179. func (gr *Graphic) ClearMaterials() {
  180. gr.materials = gr.materials[0:0]
  181. }
  182. // SetIGraphic sets the IGraphic on all this Graphic's GraphicMaterials.
  183. func (gr *Graphic) SetIGraphic(igr IGraphic) {
  184. for i := range gr.materials {
  185. gr.materials[i].igraphic = igr
  186. }
  187. }
  188. // BoundingBox recursively calculates and returns the bounding box
  189. // containing this node and all its children.
  190. func (gr *Graphic) BoundingBox() math32.Box3 {
  191. geom := gr.igeom.GetGeometry()
  192. bbox := geom.BoundingBox()
  193. for _, inode := range gr.Children() {
  194. childGraphic, ok := inode.(*Graphic)
  195. if ok {
  196. childBbox := childGraphic.BoundingBox()
  197. bbox.Union(&childBbox)
  198. }
  199. }
  200. return bbox
  201. }
  202. // CalculateMatrices calculates the model view and model view projection matrices.
  203. func (gr *Graphic) CalculateMatrices(gs *gls.GLS, rinfo *core.RenderInfo) {
  204. gr.mm = gr.MatrixWorld()
  205. gr.mvm.MultiplyMatrices(&rinfo.ViewMatrix, &gr.mm)
  206. gr.mvpm.MultiplyMatrices(&rinfo.ProjMatrix, &gr.mvm)
  207. }
  208. // ModelViewMatrix returns the last cached model view matrix for this graphic.
  209. func (gr *Graphic) ModelMatrix() *math32.Matrix4 {
  210. return &gr.mm
  211. }
  212. // ModelViewMatrix returns the last cached model view matrix for this graphic.
  213. func (gr *Graphic) ModelViewMatrix() *math32.Matrix4 {
  214. return &gr.mvm
  215. }
  216. // ModelViewProjectionMatrix returns the last cached model view projection matrix for this graphic.
  217. func (gr *Graphic) ModelViewProjectionMatrix() *math32.Matrix4 {
  218. return &gr.mvpm
  219. }
  220. // IMaterial returns the material associated with the GraphicMaterial.
  221. func (grmat *GraphicMaterial) IMaterial() material.IMaterial {
  222. return grmat.imat
  223. }
  224. // IGraphic returns the graphic associated with the GraphicMaterial.
  225. func (grmat *GraphicMaterial) IGraphic() IGraphic {
  226. return grmat.igraphic
  227. }
  228. // Render is called by the renderer to render this graphic material.
  229. func (grmat *GraphicMaterial) Render(gs *gls.GLS, rinfo *core.RenderInfo) {
  230. // Setup the associated material (set states and transfer material uniforms and textures)
  231. grmat.imat.RenderSetup(gs)
  232. // Setup the associated geometry (set VAO and transfer VBOS)
  233. gr := grmat.igraphic.GetGraphic()
  234. gr.igeom.RenderSetup(gs)
  235. // Setup current graphic (transfer matrices)
  236. grmat.igraphic.RenderSetup(gs, rinfo)
  237. // Get the number of vertices for the current material
  238. count := grmat.count
  239. geom := gr.igeom.GetGeometry()
  240. indices := geom.Indices()
  241. // Indexed geometry
  242. if indices.Size() > 0 {
  243. if count == 0 {
  244. count = indices.Size()
  245. }
  246. gs.DrawElements(gr.mode, int32(count), gls.UNSIGNED_INT, 4*uint32(grmat.start))
  247. // Non indexed geometry
  248. } else {
  249. if count == 0 {
  250. count = geom.Items()
  251. }
  252. gs.DrawArrays(gr.mode, int32(grmat.start), int32(count))
  253. }
  254. }