graphic.go 5.6 KB

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