vbo.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // Transfer is called internally and transfer the data in the VBO buffer to OpenGL if necessary
  82. func (vbo *VBO) Transfer(gs *GLS) {
  83. // If the VBO buffer is empty, ignore
  84. if vbo.buffer.Bytes() == 0 {
  85. return
  86. }
  87. // First time initialization
  88. if vbo.gs == nil {
  89. vbo.handle = gs.GenBuffer()
  90. gs.BindBuffer(ARRAY_BUFFER, vbo.handle)
  91. // Calculates stride
  92. elsize := int32(unsafe.Sizeof(float32(0)))
  93. var stride int32 = 0
  94. for _, attrib := range vbo.attribs {
  95. stride += elsize * attrib.ItemSize
  96. }
  97. // For each attribute
  98. var items uint32 = 0
  99. var offset uint32 = 0
  100. for _, attrib := range vbo.attribs {
  101. // Get attribute location in the current program
  102. loc := gs.Prog.GetAttribLocation(attrib.Name)
  103. if loc < 0 {
  104. continue
  105. }
  106. // Enables attribute and sets its stride and offset in the buffer
  107. gs.EnableVertexAttribArray(uint32(loc))
  108. gs.VertexAttribPointer(uint32(loc), attrib.ItemSize, FLOAT, false, stride, offset)
  109. items += uint32(attrib.ItemSize)
  110. offset = uint32(elsize) * items
  111. }
  112. vbo.gs = gs // this indicates that the vbo was initialized
  113. }
  114. if !vbo.update {
  115. return
  116. }
  117. // Transfer the VBO data to OpenGL
  118. gs.BindBuffer(ARRAY_BUFFER, vbo.handle)
  119. gs.BufferData(ARRAY_BUFFER, vbo.buffer.Bytes(), &vbo.buffer[0], vbo.usage)
  120. vbo.update = false
  121. }