Ep4: Buffers package

This commit is contained in:
bloeys
2021-10-30 23:10:11 +04:00
parent 5e3123b00e
commit 2b341132f8
7 changed files with 271 additions and 23 deletions

View File

@ -2,34 +2,111 @@ package buffers
import (
"github.com/bloeys/go-sdl-engine/logging"
"github.com/bloeys/go-sdl-engine/shaders"
"github.com/go-gl/gl/v4.6-compatibility/gl"
)
func HandleBuffers(sp shaders.ShaderProgram) {
type BufferType int
//Create and fill Vertex buffer object
const (
BufTypeUnknown BufferType = iota
BufTypeVertPos
BufTypeColor
)
type BufferGLType int
const (
//Generic array of data. Should be used for most data like vertex positions, vertex colors etc.
BufGLTypeArrayBuffer BufferGLType = gl.ARRAY_BUFFER
)
type BufferUsage int
const (
//Buffer is set only once and used many times
BufUsageStatic BufferUsage = gl.STATIC_DRAW
//Buffer is changed a lot and used many times
BufUsageDynamic BufferUsage = gl.DYNAMIC_DRAW
//Buffer is set only once and used by the GPU at most a few times
BufUsageStream BufferUsage = gl.STREAM_DRAW
)
type Buffer struct {
ID uint32
Type BufferType
GLType BufferGLType
DataTypeInfo
}
func (b *Buffer) Activate() {
gl.BindBuffer(uint32(b.GLType), b.ID)
}
func (b *Buffer) Deactivate() {
gl.BindBuffer(uint32(b.GLType), 0)
}
type BufferObject struct {
VAOID uint32
VertPosBuf *Buffer
ColorBuf *Buffer
}
func (bo *BufferObject) GenBuffer(data []float32, bufUsage BufferUsage, bufType BufferType, bufDataType DataType) {
gl.BindVertexArray(bo.VAOID)
//Create vertex buffer object
var vboID uint32
gl.CreateBuffers(1, &vboID)
if vboID == 0 {
logging.ErrLog.Println("Failed to create openGL buffer")
}
gl.BindBuffer(gl.ARRAY_BUFFER, vboID)
vertices := []float32{
-0.5, 0.5, 0,
0.5, 0.5, 0,
-0.5, -0.5, 0,
0.5, 0.5, 0,
0.5, -0.5, 0,
-0.5, -0.5, 0,
buf := &Buffer{
ID: vboID,
Type: bufType,
GLType: BufGLTypeArrayBuffer,
DataTypeInfo: GetDataTypeInfo(bufDataType),
}
gl.BufferData(gl.ARRAY_BUFFER, 4*len(vertices), gl.Ptr(vertices), gl.STATIC_DRAW)
bo.SetBuffer(buf)
//Assign the VBO to vertPos attribute
vertPosLoc := sp.GetAttribLoc("vertPos")
gl.VertexAttribPointer(uint32(vertPosLoc), 3, gl.FLOAT, false, 3*4, gl.PtrOffset(0))
gl.EnableVertexAttribArray(uint32(vertPosLoc))
//Fill buffer with data
gl.BindBuffer(uint32(buf.GLType), buf.ID)
gl.BufferData(uint32(buf.GLType), int(buf.DataTypeInfo.ElementSize)*len(data), gl.Ptr(data), uint32(bufUsage))
//Unbind everything
gl.BindVertexArray(0)
gl.BindBuffer(uint32(buf.GLType), 0)
}
func (bo *BufferObject) SetBuffer(buf *Buffer) {
switch buf.Type {
case BufTypeVertPos:
bo.VertPosBuf = buf
case BufTypeColor:
bo.ColorBuf = buf
default:
logging.WarnLog.Println("Unknown buffer type in SetBuffer. Type:", buf.Type)
}
}
func (bo *BufferObject) Activate() {
gl.BindVertexArray(bo.VAOID)
}
func (bo *BufferObject) Deactivate() {
gl.BindVertexArray(0)
}
func NewBufferObject() *BufferObject {
var vaoID uint32
gl.CreateVertexArrays(1, &vaoID)
if vaoID == 0 {
logging.ErrLog.Println("Failed to create openGL vertex array object")
}
return &BufferObject{VAOID: vaoID}
}

89
buffers/dataType.go Executable file
View File

@ -0,0 +1,89 @@
package buffers
import (
"github.com/bloeys/go-sdl-engine/logging"
"github.com/go-gl/gl/v4.6-compatibility/gl"
)
type DataType int
const (
DataTypeUnknown = iota
DataTypeInt32
DataTypeFloat32
DataTypeFloat64
DataTypeVec2
DataTypeVec3
DataTypeVec4
)
type DataTypeInfo struct {
//ElementSize is size in bytes of one element (e.g. for vec3 its 4)
ElementSize int32
//ElementCount is number of elements (e.g. for vec3 its 3)
ElementCount int32
//ElementType is the type of each primitive (e.g. for vec3 its gl.FLOAT)
ElementType uint32
//GLType is the type of the variable represented (e.g. for vec3 its gl.FLOAT_VEC2)
GLType uint32
}
//GetSize returns the total size in bytes (e.g. for vec3 its 4*3)
func (dti *DataTypeInfo) GetSize() int32 {
return dti.ElementSize * dti.ElementCount
}
func GetDataTypeInfo(dt DataType) DataTypeInfo {
switch dt {
case DataTypeInt32:
return DataTypeInfo{
ElementSize: 4,
ElementCount: 1,
ElementType: gl.INT,
GLType: gl.INT,
}
case DataTypeFloat32:
return DataTypeInfo{
ElementSize: 4,
ElementCount: 1,
ElementType: gl.FLOAT,
GLType: gl.FLOAT,
}
case DataTypeFloat64:
return DataTypeInfo{
ElementSize: 8,
ElementCount: 1,
ElementType: gl.DOUBLE,
GLType: gl.DOUBLE,
}
case DataTypeVec2:
return DataTypeInfo{
ElementSize: 4,
ElementCount: 2,
ElementType: gl.FLOAT,
GLType: gl.FLOAT_VEC2,
}
case DataTypeVec3:
return DataTypeInfo{
ElementSize: 4,
ElementCount: 3,
ElementType: gl.FLOAT,
GLType: gl.FLOAT_VEC3,
}
case DataTypeVec4:
return DataTypeInfo{
ElementSize: 4,
ElementCount: 4,
ElementType: gl.FLOAT,
GLType: gl.FLOAT_VEC4,
}
default:
logging.WarnLog.Println("Unknown data type passed. DataType:", dt)
return DataTypeInfo{}
}
}