graphic.go 5.2 KB

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