graphic.go 4.9 KB

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