graphic.go 7.9 KB

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