vbo.go 10 KB

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