graphic.go 4.8 KB

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