vbo.go 11 KB

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