graphic.go 6.6 KB

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