vbo.go 8.9 KB

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