vbo.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. return vbo
  67. }
  68. // Sets the expected usage pattern of the buffer.
  69. // The default value is GL_STATIC_DRAW.
  70. func (vbo *VBO) SetUsage(usage uint32) {
  71. vbo.usage = usage
  72. }
  73. // Buffer returns pointer to the VBO buffer
  74. func (vbo *VBO) Buffer() *math32.ArrayF32 {
  75. return &vbo.buffer
  76. }
  77. // Updates sets the update flag to force the VBO update
  78. func (vbo *VBO) Update() {
  79. vbo.update = true
  80. }
  81. // Stride returns the stride of this VBO which is the number of bytes
  82. // used by one group attributes side by side in the buffer
  83. // Ex: x y z r g b x y z r g b ...x y z r g b
  84. // The stride will be: sizeof(float) * 6 = 24
  85. func (vbo *VBO) Stride() int {
  86. stride := 0
  87. elsize := int(unsafe.Sizeof(float32(0)))
  88. for _, attrib := range vbo.attribs {
  89. stride += elsize * int(attrib.ItemSize)
  90. }
  91. return stride
  92. }
  93. // Transfer is called internally and transfer the data in the VBO buffer to OpenGL if necessary
  94. func (vbo *VBO) Transfer(gs *GLS) {
  95. // If the VBO buffer is empty, ignore
  96. if vbo.buffer.Bytes() == 0 {
  97. return
  98. }
  99. // First time initialization
  100. if vbo.gs == nil {
  101. vbo.handle = gs.GenBuffer()
  102. gs.BindBuffer(ARRAY_BUFFER, vbo.handle)
  103. // Calculates stride
  104. stride := vbo.Stride()
  105. // For each attribute
  106. var items uint32 = 0
  107. var offset uint32 = 0
  108. elsize := int32(unsafe.Sizeof(float32(0)))
  109. for _, attrib := range vbo.attribs {
  110. // Get attribute location in the current program
  111. loc := gs.Prog.GetAttribLocation(attrib.Name)
  112. if loc < 0 {
  113. continue
  114. }
  115. // Enables attribute and sets its stride and offset in the buffer
  116. gs.EnableVertexAttribArray(uint32(loc))
  117. gs.VertexAttribPointer(uint32(loc), attrib.ItemSize, FLOAT, false, int32(stride), offset)
  118. items += uint32(attrib.ItemSize)
  119. offset = uint32(elsize) * items
  120. }
  121. vbo.gs = gs // this indicates that the vbo was initialized
  122. }
  123. if !vbo.update {
  124. return
  125. }
  126. // Transfer the VBO data to OpenGL
  127. gs.BindBuffer(ARRAY_BUFFER, vbo.handle)
  128. gs.BufferData(ARRAY_BUFFER, vbo.buffer.Bytes(), &vbo.buffer[0], vbo.usage)
  129. vbo.update = false
  130. }