vbo.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 gls
  5. import (
  6. "github.com/g3n/engine/math32"
  7. "unsafe"
  8. )
  9. // VBO abstracts an OpenGL Vertex Buffer Object.
  10. type VBO struct {
  11. gs *GLS // Reference to OpenGL state
  12. handle uint32 // OpenGL handle for this VBO
  13. usage uint32 // Expected usage pattern of the buffer
  14. update bool // Update flag
  15. buffer math32.ArrayF32 // Data buffer
  16. attribs []VBOattrib // List of attributes
  17. }
  18. // VBOattrib describes one attribute of an OpenGL Vertex Buffer Object.
  19. type VBOattrib struct {
  20. Name string // Name of of the attribute
  21. ItemSize int32 // Number of elements
  22. }
  23. // NewVBO creates and returns a pointer to a new OpenGL Vertex Buffer Object.
  24. func NewVBO(buffer math32.ArrayF32) *VBO {
  25. vbo := new(VBO)
  26. vbo.init()
  27. vbo.SetBuffer(buffer)
  28. return vbo
  29. }
  30. // init initializes the VBO.
  31. func (vbo *VBO) init() {
  32. vbo.gs = nil
  33. vbo.handle = 0
  34. vbo.usage = STATIC_DRAW
  35. vbo.update = true
  36. vbo.attribs = make([]VBOattrib, 0)
  37. }
  38. // AddAttrib adds a new attribute to the VBO.
  39. func (vbo *VBO) AddAttrib(name string, itemSize int32) *VBO {
  40. vbo.attribs = append(vbo.attribs, VBOattrib{
  41. Name: name,
  42. ItemSize: itemSize,
  43. })
  44. return vbo
  45. }
  46. // Attrib finds and returns a pointer to the VBO attribute with the specified name.
  47. // Returns nil if not found.
  48. func (vbo *VBO) Attrib(name string) *VBOattrib {
  49. for _, attr := range vbo.attribs {
  50. if attr.Name == name {
  51. return &attr
  52. }
  53. }
  54. return nil
  55. }
  56. // AttribAt returns a pointer to the VBO attribute at the specified index.
  57. func (vbo *VBO) AttribAt(idx int) *VBOattrib {
  58. return &vbo.attribs[idx]
  59. }
  60. // AttribCount returns the current number of attributes for this VBO.
  61. func (vbo *VBO) AttribCount() int {
  62. return len(vbo.attribs)
  63. }
  64. // Attributes returns the attributes for this VBO.
  65. func (vbo *VBO) Attributes() []VBOattrib {
  66. return vbo.attribs
  67. }
  68. // Dispose disposes of the OpenGL resources used by the VBO.
  69. // As currently the VBO is used only by Geometry objects
  70. // it is not referenced counted.
  71. func (vbo *VBO) Dispose() {
  72. if vbo.gs != nil {
  73. vbo.gs.DeleteBuffers(vbo.handle)
  74. }
  75. vbo.gs = nil
  76. }
  77. // SetBuffer sets the VBO buffer.
  78. func (vbo *VBO) SetBuffer(buffer math32.ArrayF32) *VBO {
  79. vbo.buffer = buffer
  80. vbo.update = true
  81. return vbo
  82. }
  83. // SetUsage sets the expected usage pattern of the buffer.
  84. // The default value is GL_STATIC_DRAW.
  85. func (vbo *VBO) SetUsage(usage uint32) {
  86. vbo.usage = usage
  87. }
  88. // Buffer returns a pointer to the VBO buffer.
  89. func (vbo *VBO) Buffer() *math32.ArrayF32 {
  90. return &vbo.buffer
  91. }
  92. // Update sets the update flag to force the VBO update.
  93. func (vbo *VBO) Update() {
  94. vbo.update = true
  95. }
  96. // AttribOffset returns the total number of elements from
  97. // all attributes preceding the specified attribute.
  98. func (vbo *VBO) AttribOffset(name string) int {
  99. elementCount := 0
  100. for _, attr := range vbo.attribs {
  101. if attr.Name == name {
  102. return elementCount
  103. }
  104. elementCount += int(attr.ItemSize)
  105. }
  106. return elementCount
  107. }
  108. // Stride returns the stride of the VBO, which is the number of elements in
  109. // one complete set of group attributes. E.g. for an interleaved VBO with two attributes:
  110. // "VertexPosition" (3 elements) and "VertexTexcoord" (2 elements), the stride would be 5:
  111. // [X, Y, Z, U, V], X, Y, Z, U, V, X, Y, Z, U, V... X, Y, Z, U, V.
  112. func (vbo *VBO) Stride() int {
  113. stride := 0
  114. for _, attrib := range vbo.attribs {
  115. stride += int(attrib.ItemSize)
  116. }
  117. return stride
  118. }
  119. // StrideSize returns the number of bytes used by one complete set of group attributes.
  120. // E.g. for an interleaved VBO with two attributes: "VertexPosition" (3 elements)
  121. // and "VertexTexcoord" (2 elements), the stride would be 5:
  122. // [X, Y, Z, U, V], X, Y, Z, U, V, X, Y, Z, U, V... X, Y, Z, U, V
  123. // and the stride size would be: sizeof(float)*stride = 4*5 = 20
  124. func (vbo *VBO) StrideSize() int {
  125. stride := vbo.Stride()
  126. elsize := int(unsafe.Sizeof(float32(0)))
  127. return stride * elsize
  128. }
  129. // Transfer (called internally) transfers the data from the VBO buffer to OpenGL if necessary.
  130. func (vbo *VBO) Transfer(gs *GLS) {
  131. // If the VBO buffer is empty, ignore
  132. if vbo.buffer.Bytes() == 0 {
  133. return
  134. }
  135. // First time initialization
  136. if vbo.gs == nil {
  137. vbo.handle = gs.GenBuffer()
  138. gs.BindBuffer(ARRAY_BUFFER, vbo.handle)
  139. // Calculates stride size
  140. strideSize := vbo.StrideSize()
  141. // For each attribute
  142. var items uint32
  143. var offset uint32
  144. elsize := int32(unsafe.Sizeof(float32(0)))
  145. for _, attrib := range vbo.attribs {
  146. // Get attribute location in the current program
  147. loc := gs.prog.GetAttribLocation(attrib.Name)
  148. if loc < 0 {
  149. continue
  150. }
  151. // Enables attribute and sets its stride and offset in the buffer
  152. gs.EnableVertexAttribArray(uint32(loc))
  153. gs.VertexAttribPointer(uint32(loc), attrib.ItemSize, FLOAT, false, int32(strideSize), offset)
  154. items += uint32(attrib.ItemSize)
  155. offset = uint32(elsize) * items
  156. }
  157. vbo.gs = gs // this indicates that the vbo was initialized
  158. }
  159. // If nothing has changed, no need to transfer data to OpenGL
  160. if !vbo.update {
  161. return
  162. }
  163. // Transfer the VBO data to OpenGL
  164. gs.BindBuffer(ARRAY_BUFFER, vbo.handle)
  165. gs.BufferData(ARRAY_BUFFER, vbo.buffer.Bytes(), &vbo.buffer[0], vbo.usage)
  166. vbo.update = false
  167. }
  168. // OperateOnVectors3 iterates over all 3-float32 items for the specified attribute
  169. // and calls the specified callback function with a pointer to each item as a Vector3.
  170. // The vector pointers can be modified inside the callback and the modifications will be applied to the buffer at each iteration.
  171. // The callback function returns false to continue or true to break.
  172. func (vbo *VBO) OperateOnVectors3(vboAttrib string, cb func(vec *math32.Vector3) bool) {
  173. stride := vbo.Stride()
  174. offset := vbo.AttribOffset(vboAttrib)
  175. buffer := vbo.Buffer()
  176. // Call callback for each vector3, updating the buffer afterward
  177. var vec math32.Vector3
  178. for i := offset; i < vbo.buffer.Size(); i += stride {
  179. buffer.GetVector3(i, &vec)
  180. brk := cb(&vec)
  181. buffer.SetVector3(i, &vec)
  182. if brk {
  183. break
  184. }
  185. }
  186. vbo.Update()
  187. }
  188. // ReadVectors3 iterates over all 3-float32 items for the specified attribute
  189. // and calls the specified callback function with the value of each item as a Vector3.
  190. // The callback function returns false to continue or true to break.
  191. func (vbo *VBO) ReadVectors3(vboAttrib string, cb func(vec math32.Vector3) bool) {
  192. stride := vbo.Stride()
  193. offset := vbo.AttribOffset(vboAttrib)
  194. positions := vbo.Buffer()
  195. // Call callback for each vector3
  196. var vec math32.Vector3
  197. for i := offset; i < positions.Size(); i += stride {
  198. positions.GetVector3(i, &vec)
  199. brk := cb(vec)
  200. if brk {
  201. break
  202. }
  203. }
  204. }
  205. // Read3Vectors3 iterates over all 3-float32 items (3 items at a time) for the specified attribute
  206. // and calls the specified callback function with the value of each of the 3 items as Vector3.
  207. // The callback function returns false to continue or true to break.
  208. func (vbo *VBO) ReadTripleVectors3(vboAttrib string, cb func(vec1, vec2, vec3 math32.Vector3) bool) {
  209. stride := vbo.Stride()
  210. offset := vbo.AttribOffset(vboAttrib)
  211. positions := vbo.Buffer()
  212. doubleStride := 2*stride
  213. loopStride := 3*stride
  214. // Call callback for each vector3 triple
  215. var vec1, vec2, vec3 math32.Vector3
  216. for i := offset; i < positions.Size(); i += loopStride {
  217. positions.GetVector3(i, &vec1)
  218. positions.GetVector3(i + stride, &vec2)
  219. positions.GetVector3(i + doubleStride, &vec3)
  220. brk := cb(vec1, vec2, vec3)
  221. if brk {
  222. break
  223. }
  224. }
  225. }