Support interleaved buffers

This commit is contained in:
bloeys
2022-01-26 08:04:50 +04:00
parent e1e617e4e4
commit 1109caef43
8 changed files with 199 additions and 199 deletions

33
buffers/buf_usage.go Executable file
View File

@ -0,0 +1,33 @@
package buffers
import (
"fmt"
"github.com/bloeys/nmage/asserts"
"github.com/go-gl/gl/v4.1-core/gl"
)
type BufUsage int
const (
//Buffer is set only once and used many times
BufUsageStatic BufUsage = iota
//Buffer is changed a lot and used many times
BufUsageDynamic
//Buffer is set only once and used by the GPU at most a few times
BufUsageStream
)
func (b BufUsage) ToGL() uint32 {
switch b {
case BufUsageStatic:
return gl.STATIC_DRAW
case BufUsageDynamic:
return gl.DYNAMIC_DRAW
case BufUsageStream:
return gl.STREAM_DRAW
}
asserts.T(false, fmt.Sprintf("Unexpected BufUsage value '%v'", b))
return 0
}

View File

@ -5,169 +5,89 @@ import (
"github.com/go-gl/gl/v4.1-core/gl"
)
type BufGLType int
const (
BufGLTypeUnknown BufGLType = 0
//Generic array of data. Should be used for most data like vertex positions, vertex colors etc.
BufGLTypeArray BufGLType = gl.ARRAY_BUFFER
BufGLTypeIndices BufGLType = gl.ELEMENT_ARRAY_BUFFER
)
type BufType int
const (
BufTypeUnknown BufType = iota
BufTypeVertPos
BufTypeColor
BufTypeIndex
BufTypeNormal
)
func (bt BufType) GetBufferGLType() BufGLType {
switch bt {
case BufTypeNormal:
fallthrough
case BufTypeColor:
fallthrough
case BufTypeVertPos:
return BufGLTypeArray
case BufTypeIndex:
return BufGLTypeIndices
default:
logging.WarnLog.Println("Unknown BufferType. BufferType: ", bt)
return BufGLTypeUnknown
}
type BufferLayoutElement struct {
Offset int
DataType
}
type BufUsage int
const (
//Buffer is set only once and used many times
BufUsageStatic BufUsage = gl.STATIC_DRAW
//Buffer is changed a lot and used many times
BufUsageDynamic BufUsage = gl.DYNAMIC_DRAW
//Buffer is set only once and used by the GPU at most a few times
BufUsageStream BufUsage = gl.STREAM_DRAW
)
type BufferLayout struct {
Elements []BufferLayoutElement
}
type Buffer struct {
ID uint32
Type BufType
GLType BufGLType
DataTypeInfo
//DataLen is the number of elements in the uploaded to the buffer
DataLen int32
VAOID uint32
//BufID is the ID of the VBO
BufID uint32
//IndexBufID is the ID of the index/element buffer
IndexBufID uint32
IndexBufCount int32
Layout BufferLayout
Stride int32
}
func (b *Buffer) Activate() {
gl.BindBuffer(uint32(b.GLType), b.ID)
func (b *Buffer) Bind() {
gl.BindVertexArray(b.VAOID)
}
func (b *Buffer) Deactivate() {
gl.BindBuffer(uint32(b.GLType), 0)
}
type BufferObject struct {
VAOID uint32
VertPosBuf *Buffer
NormalBuf *Buffer
ColorBuf *Buffer
IndexBuf *Buffer
}
func (bo *BufferObject) GenBuffer(data []float32, bufUsage BufUsage, bufType BufType, bufDataType DataType) {
gl.BindVertexArray(bo.VAOID)
//Create vertex buffer object
var vboID uint32
gl.GenBuffers(1, &vboID)
if vboID == 0 {
logging.ErrLog.Println("Failed to create openGL buffer")
}
buf := &Buffer{
ID: vboID,
Type: bufType,
GLType: bufType.GetBufferGLType(),
DataTypeInfo: GetDataTypeInfo(bufDataType),
DataLen: int32(len(data)),
}
bo.SetBuffer(buf)
//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) GenBufferUint32(data []uint32, bufUsage BufUsage, bufType BufType, bufDataType DataType) {
gl.BindVertexArray(bo.VAOID)
//Create vertex buffer object
var vboID uint32
gl.GenBuffers(1, &vboID)
if vboID == 0 {
logging.ErrLog.Println("Failed to create openGL buffer")
}
buf := &Buffer{
ID: vboID,
Type: bufType,
GLType: bufType.GetBufferGLType(),
DataTypeInfo: GetDataTypeInfo(bufDataType),
DataLen: int32(len(data)),
}
bo.SetBuffer(buf)
//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 BufTypeNormal:
bo.NormalBuf = buf
case BufTypeColor:
bo.ColorBuf = buf
case BufTypeIndex:
bo.IndexBuf = buf
default:
logging.WarnLog.Println("Unknown buffer type in SetBuffer. Type:", buf.Type)
}
}
func (bo *BufferObject) Bind() {
gl.BindVertexArray(bo.VAOID)
}
func (bo *BufferObject) UnBind() {
func (b *Buffer) UnBind() {
gl.BindVertexArray(0)
}
func NewBufferObject() *BufferObject {
func (b *Buffer) CalcValues() {
var vaoID uint32
gl.GenVertexArrays(1, &vaoID)
if vaoID == 0 {
b.Stride = 0
for i := 0; i < len(b.Layout.Elements); i++ {
info := GetDataTypeInfo(b.Layout.Elements[i].DataType)
b.Layout.Elements[i].Offset = int(b.Stride)
b.Stride += info.GetSize()
}
}
func (b *Buffer) SetData(values []float32) {
gl.BindVertexArray(b.VAOID)
gl.BindBuffer(gl.ARRAY_BUFFER, b.BufID)
gl.BufferData(gl.ARRAY_BUFFER, len(values)*4, gl.Ptr(values), BufUsageStatic.ToGL())
gl.BindVertexArray(0)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
func (b *Buffer) SetIndexBufData(values []uint32) {
b.IndexBufCount = int32(len(values))
gl.BindVertexArray(b.VAOID)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, b.IndexBufID)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(values)*4, gl.Ptr(values), BufUsageStatic.ToGL())
gl.BindVertexArray(0)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0)
}
func NewBuffer(layout BufferLayout) Buffer {
b := Buffer{
Layout: layout,
}
gl.GenVertexArrays(1, &b.VAOID)
if b.VAOID == 0 {
logging.ErrLog.Println("Failed to create openGL vertex array object")
}
return &BufferObject{VAOID: vaoID}
gl.GenBuffers(1, &b.BufID)
if b.BufID == 0 {
logging.ErrLog.Println("Failed to create openGL buffer")
}
gl.GenBuffers(1, &b.IndexBufID)
if b.IndexBufID == 0 {
logging.ErrLog.Println("Failed to create openGL buffer")
}
b.CalcValues()
return b
}

View File

@ -1,7 +1,9 @@
package buffers
import (
"github.com/bloeys/nmage/logging"
"fmt"
"github.com/bloeys/nmage/asserts"
"github.com/go-gl/gl/v4.1-core/gl"
)
@ -26,8 +28,6 @@ type DataTypeInfo struct {
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)
@ -45,7 +45,6 @@ func GetDataTypeInfo(dt DataType) DataTypeInfo {
ElementSize: 4,
ElementCount: 1,
ElementType: gl.INT,
GLType: gl.INT,
}
case DataTypeFloat32:
@ -53,14 +52,12 @@ func GetDataTypeInfo(dt DataType) 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:
@ -68,25 +65,22 @@ func GetDataTypeInfo(dt DataType) 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)
asserts.T(false, fmt.Sprintf("Unknown data type passed. DataType '%v'", dt))
return DataTypeInfo{}
}
}