vbo.go 11 KB

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