graphic.go 7.3 KB

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