vbo.go 10 KB

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