change vertex format to include names + add vertex format to shader + ID methods
This commit is contained in:
parent
a1913661e7
commit
e009011c37
|
@ -18,7 +18,8 @@ type UniformFormat map[string]Attr
|
||||||
// Shader is an OpenGL shader program.
|
// Shader is an OpenGL shader program.
|
||||||
type Shader struct {
|
type Shader struct {
|
||||||
parent Doer
|
parent Doer
|
||||||
format UniformFormat
|
vertexFormat VertexFormat
|
||||||
|
uniformFormat UniformFormat
|
||||||
program uint32
|
program uint32
|
||||||
uniforms map[Attr]int32
|
uniforms map[Attr]int32
|
||||||
}
|
}
|
||||||
|
@ -26,10 +27,11 @@ type Shader struct {
|
||||||
// NewShader creates a new shader program from the specified vertex shader and fragment shader sources.
|
// NewShader creates a new shader program from the specified vertex shader and fragment shader sources.
|
||||||
//
|
//
|
||||||
// Note that vertexShader and fragmentShader parameters must contain the source code, they're not filenames.
|
// Note that vertexShader and fragmentShader parameters must contain the source code, they're not filenames.
|
||||||
func NewShader(parent Doer, format UniformFormat, vertexShader, fragmentShader string) (*Shader, error) {
|
func NewShader(parent Doer, vertexFormat VertexFormat, uniformFormat UniformFormat, vertexShader, fragmentShader string) (*Shader, error) {
|
||||||
shader := &Shader{
|
shader := &Shader{
|
||||||
parent: parent,
|
parent: parent,
|
||||||
format: format,
|
vertexFormat: vertexFormat,
|
||||||
|
uniformFormat: uniformFormat,
|
||||||
uniforms: make(map[Attr]int32),
|
uniforms: make(map[Attr]int32),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +102,7 @@ func NewShader(parent Doer, format UniformFormat, vertexShader, fragmentShader s
|
||||||
gl.DeleteShader(fshader)
|
gl.DeleteShader(fshader)
|
||||||
|
|
||||||
// uniforms
|
// uniforms
|
||||||
for uname, utype := range format {
|
for uname, utype := range uniformFormat {
|
||||||
ulocation := gl.GetUniformLocation(shader.program, gl.Str(uname+"\x00"))
|
ulocation := gl.GetUniformLocation(shader.program, gl.Str(uname+"\x00"))
|
||||||
if ulocation == -1 {
|
if ulocation == -1 {
|
||||||
return fmt.Errorf("shader does not contain uniform '%s'", uname)
|
return fmt.Errorf("shader does not contain uniform '%s'", uname)
|
||||||
|
@ -133,6 +135,21 @@ func (s *Shader) Delete() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ID returns an OpenGL identifier of a shader program.
|
||||||
|
func (s *Shader) ID() uint32 {
|
||||||
|
return s.program
|
||||||
|
}
|
||||||
|
|
||||||
|
// VertexFormat returns the vertex attribute format of this shader. Do not change it.
|
||||||
|
func (s *Shader) VertexFormat() VertexFormat {
|
||||||
|
return s.vertexFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
// UniformFormat returns the uniform attribute format of this shader. Do not change it.
|
||||||
|
func (s *Shader) UniformFormat() UniformFormat {
|
||||||
|
return s.uniformFormat
|
||||||
|
}
|
||||||
|
|
||||||
// SetUniformInt sets the value of an uniform attribute Attr{Purpose: purpose, Type: Int}.
|
// SetUniformInt sets the value of an uniform attribute Attr{Purpose: purpose, Type: Int}.
|
||||||
//
|
//
|
||||||
// Returns false if the attribute does not exist.
|
// Returns false if the attribute does not exist.
|
||||||
|
|
|
@ -55,6 +55,11 @@ func (t *Texture) Delete() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ID returns an OpenGL identifier of a texture.
|
||||||
|
func (t *Texture) ID() uint32 {
|
||||||
|
return t.tex
|
||||||
|
}
|
||||||
|
|
||||||
// Do bind a texture, executes sub, and unbinds the texture.
|
// Do bind a texture, executes sub, and unbinds the texture.
|
||||||
func (t *Texture) Do(sub func(Context)) {
|
func (t *Texture) Do(sub func(Context)) {
|
||||||
t.parent.Do(func(ctx Context) {
|
t.parent.Do(func(ctx Context) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package pixelgl
|
package pixelgl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/go-gl/gl/v3.3-core/gl"
|
"github.com/go-gl/gl/v3.3-core/gl"
|
||||||
|
@ -12,10 +13,10 @@ import (
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// VertexFormat{{Position, Vec2}, {Color, Vec4}, {TexCoord, Vec2}}
|
// VertexFormat{"position": {Position, Vec2}, "colr": {Color, Vec4}, "texCoord": {TexCoord, Vec2}}
|
||||||
//
|
//
|
||||||
// Note: vertex array currently doesn't support matrices in vertex format.
|
// Note: vertex array currently doesn't support matrices in vertex format.
|
||||||
type VertexFormat []Attr
|
type VertexFormat map[string]Attr
|
||||||
|
|
||||||
// Size calculates the total size of a single vertex in this vertex format (sum of the sizes of all vertex attributes).
|
// Size calculates the total size of a single vertex in this vertex format (sum of the sizes of all vertex attributes).
|
||||||
func (vf VertexFormat) Size() int {
|
func (vf VertexFormat) Size() int {
|
||||||
|
@ -104,9 +105,9 @@ func NewVertexArray(parent Doer, format VertexFormat, mode VertexDrawMode, usage
|
||||||
offset += attr.Type.Size()
|
offset += attr.Type.Size()
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err, glerr error
|
||||||
parent.Do(func(ctx Context) {
|
parent.Do(func(ctx Context) {
|
||||||
err = DoGLErr(func() {
|
err, glerr = DoErrGLErr(func() error {
|
||||||
gl.GenVertexArrays(1, &va.vao)
|
gl.GenVertexArrays(1, &va.vao)
|
||||||
gl.BindVertexArray(va.vao)
|
gl.BindVertexArray(va.vao)
|
||||||
|
|
||||||
|
@ -117,8 +118,11 @@ func NewVertexArray(parent Doer, format VertexFormat, mode VertexDrawMode, usage
|
||||||
gl.BufferData(gl.ARRAY_BUFFER, len(emptyData), gl.Ptr(emptyData), uint32(usage))
|
gl.BufferData(gl.ARRAY_BUFFER, len(emptyData), gl.Ptr(emptyData), uint32(usage))
|
||||||
|
|
||||||
offset := 0
|
offset := 0
|
||||||
for i, attr := range format {
|
for name, attr := range format {
|
||||||
//XXX: ugly but OpenGL is so inconsistent
|
location := gl.GetAttribLocation(ctx.Shader().ID(), gl.Str(name+"\x00"))
|
||||||
|
if location == -1 {
|
||||||
|
return fmt.Errorf("shader does not contain vertex attribute '%s'", name)
|
||||||
|
}
|
||||||
|
|
||||||
var size int32
|
var size int32
|
||||||
switch attr.Type {
|
switch attr.Type {
|
||||||
|
@ -141,23 +145,31 @@ func NewVertexArray(parent Doer, format VertexFormat, mode VertexDrawMode, usage
|
||||||
}
|
}
|
||||||
|
|
||||||
gl.VertexAttribPointer(
|
gl.VertexAttribPointer(
|
||||||
uint32(i),
|
uint32(location),
|
||||||
size,
|
size,
|
||||||
xtype,
|
xtype,
|
||||||
false,
|
false,
|
||||||
int32(va.stride),
|
int32(va.stride),
|
||||||
gl.PtrOffset(offset),
|
gl.PtrOffset(offset),
|
||||||
)
|
)
|
||||||
gl.EnableVertexAttribArray(uint32(i))
|
gl.EnableVertexAttribArray(uint32(location))
|
||||||
offset += attr.Type.Size()
|
offset += attr.Type.Size()
|
||||||
}
|
}
|
||||||
|
|
||||||
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
|
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
|
||||||
gl.BindVertexArray(0)
|
gl.BindVertexArray(0)
|
||||||
|
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
if err != nil && glerr != nil {
|
||||||
|
return nil, errors.Wrap(errors.Wrap(glerr, err.Error()), "failed to create vertex array")
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to create a vertex array")
|
return nil, errors.Wrap(err, "failed to create vertex array")
|
||||||
|
}
|
||||||
|
if glerr != nil {
|
||||||
|
return nil, errors.Wrap(glerr, "failed to create vertex array")
|
||||||
}
|
}
|
||||||
|
|
||||||
return va, nil
|
return va, nil
|
||||||
|
@ -173,6 +185,11 @@ func (va *VertexArray) Delete() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ID returns an OpenGL identifier of a vertex array.
|
||||||
|
func (va *VertexArray) ID() uint32 {
|
||||||
|
return va.vao
|
||||||
|
}
|
||||||
|
|
||||||
// Count returns the number of vertices in a vertex array.
|
// Count returns the number of vertices in a vertex array.
|
||||||
func (va *VertexArray) Count() int {
|
func (va *VertexArray) Count() int {
|
||||||
return va.count
|
return va.count
|
||||||
|
|
Loading…
Reference in New Issue