vbo.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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
  12. handle uint32 // OpenGL handle for this VBO
  13. usage uint32 // Expected usage patter 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. Name string // Name of of the attribute
  21. ItemSize int32 // Number of elements for each item
  22. }
  23. // NewVBO creates and returns a pointer to a new OpenGL Vertex Buffer Object
  24. func NewVBO() *VBO {
  25. vbo := new(VBO)
  26. vbo.init()
  27. return vbo
  28. }
  29. // init initializes this VBO
  30. func (vbo *VBO) init() {
  31. vbo.gs = nil
  32. vbo.handle = 0
  33. vbo.usage = STATIC_DRAW
  34. vbo.update = true
  35. vbo.attribs = make([]VBOattrib, 0)
  36. }
  37. // AddAttrib adds a new attribute to this VBO
  38. func (vbo *VBO) AddAttrib(name string, itemSize int32) *VBO {
  39. vbo.attribs = append(vbo.attribs, VBOattrib{
  40. Name: name,
  41. ItemSize: itemSize,
  42. })
  43. return vbo
  44. }
  45. // Attrib finds and returns pointer the attribute with the specified name
  46. // or nil if not found
  47. func (vbo *VBO) Attrib(name string) *VBOattrib {
  48. for _, attr := range vbo.attribs {
  49. if attr.Name == name {
  50. return &attr
  51. }
  52. }
  53. return nil
  54. }
  55. // AttribAt returns pointer to the VBO attribute at the specified index
  56. func (vbo *VBO) AttribAt(idx int) *VBOattrib {
  57. return &vbo.attribs[idx]
  58. }
  59. // AttribCount returns the current number of attributes for this VBO
  60. func (vbo *VBO) AttribCount() int {
  61. return len(vbo.attribs)
  62. }
  63. // Sets the VBO buffer
  64. func (vbo *VBO) SetBuffer(buffer math32.ArrayF32) *VBO {
  65. vbo.buffer = buffer
  66. vbo.update = true
  67. return vbo
  68. }
  69. // Sets the expected usage pattern of the buffer.
  70. // The default value is GL_STATIC_DRAW.
  71. func (vbo *VBO) SetUsage(usage uint32) {
  72. vbo.usage = usage
  73. }
  74. // Buffer returns pointer to the VBO buffer
  75. func (vbo *VBO) Buffer() *math32.ArrayF32 {
  76. return &vbo.buffer
  77. }
  78. // Updates sets the update flag to force the VBO update
  79. func (vbo *VBO) Update() {
  80. vbo.update = true
  81. }
  82. // Stride returns the stride of this VBO which is the number of bytes
  83. // used by one group attributes side by side in the buffer
  84. // Ex: x y z r g b x y z r g b ...x y z r g b
  85. // The stride will be: sizeof(float) * 6 = 24
  86. func (vbo *VBO) Stride() int {
  87. stride := 0
  88. elsize := int(unsafe.Sizeof(float32(0)))
  89. for _, attrib := range vbo.attribs {
  90. stride += elsize * int(attrib.ItemSize)
  91. }
  92. return stride
  93. }
  94. // Transfer is called internally and transfer the data in the VBO buffer to OpenGL if necessary
  95. func (vbo *VBO) Transfer(gs *GLS) {
  96. // If the VBO buffer is empty, ignore
  97. if vbo.buffer.Bytes() == 0 {
  98. return
  99. }
  100. // First time initialization
  101. if vbo.gs == nil {
  102. vbo.handle = gs.GenBuffer()
  103. gs.BindBuffer(ARRAY_BUFFER, vbo.handle)
  104. // Calculates stride
  105. stride := vbo.Stride()
  106. // For each attribute
  107. var items uint32 = 0
  108. var offset uint32 = 0
  109. elsize := int32(unsafe.Sizeof(float32(0)))
  110. for _, attrib := range vbo.attribs {
  111. // Get attribute location in the current program
  112. loc := gs.Prog.GetAttribLocation(attrib.Name)
  113. if loc < 0 {
  114. continue
  115. }
  116. // Enables attribute and sets its stride and offset in the buffer
  117. gs.EnableVertexAttribArray(uint32(loc))
  118. gs.VertexAttribPointer(uint32(loc), attrib.ItemSize, FLOAT, false, int32(stride), offset)
  119. items += uint32(attrib.ItemSize)
  120. offset = uint32(elsize) * items
  121. }
  122. vbo.gs = gs // this indicates that the vbo was initialized
  123. }
  124. if !vbo.update {
  125. return
  126. }
  127. // Transfer the VBO data to OpenGL
  128. gs.BindBuffer(ARRAY_BUFFER, vbo.handle)
  129. gs.BufferData(ARRAY_BUFFER, vbo.buffer.Bytes(), &vbo.buffer[0], vbo.usage)
  130. vbo.update = false
  131. }