Add opengl.Context.NewBuffer

This commit is contained in:
Hajime Hoshi 2014-12-31 17:45:23 +09:00
parent 7597268e85
commit f88154f380
5 changed files with 47 additions and 45 deletions

View File

@ -27,7 +27,7 @@ const (
FilterLinear FilterLinear
) )
func glFilter(c *opengl.Context, filter Filter) opengl.Filter { func glFilter(c *opengl.Context, filter Filter) opengl.FilterType {
switch filter { switch filter {
case FilterNearest: case FilterNearest:
return c.Nearest return c.Nearest

View File

@ -41,8 +41,6 @@ type TextureQuads interface {
var initialized = false var initialized = false
const size = 10000
// TODO: Use unsafe.SizeOf? // TODO: Use unsafe.SizeOf?
const uint16Size = 2 const uint16Size = 2
const float32Size = 4 const float32Size = 4
@ -54,31 +52,13 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix [4]
if err := initialize(c); err != nil { if err := initialize(c); err != nil {
return err return err
} }
vertexBuffer := gl.GenBuffer()
vertexBuffer.Bind(gl.ARRAY_BUFFER)
s := float32Size * stride * size
gl.BufferData(gl.ARRAY_BUFFER, s, nil, gl.DYNAMIC_DRAW)
indexBuffer := gl.GenBuffer()
indexBuffer.Bind(gl.ELEMENT_ARRAY_BUFFER)
indices := make([]uint16, 6*size)
for i := uint16(0); i < size; i++ {
indices[6*i+0] = 4*i + 0
indices[6*i+1] = 4*i + 1
indices[6*i+2] = 4*i + 2
indices[6*i+3] = 4*i + 1
indices[6*i+4] = 4*i + 2
indices[6*i+5] = 4*i + 3
}
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, uint16Size*len(indices), indices, gl.STATIC_DRAW)
initialized = true initialized = true
} }
if quads.Len() == 0 { if quads.Len() == 0 {
return nil return nil
} }
// TODO: Check performance // TODO: Check performance
program := useProgramColorMatrix(glMatrix(projectionMatrix), geo, color) program := useProgramColorMatrix(glMatrix(projectionMatrix), geo, color)

View File

@ -21,6 +21,8 @@ import (
var programColorMatrix opengl.Program var programColorMatrix opengl.Program
func initialize(c *opengl.Context) error { func initialize(c *opengl.Context) error {
const size = 10000
var err error var err error
shaders[shaderVertex].native, err = c.NewShader(c.VertexShader, shaders[shaderVertex].source) shaders[shaderVertex].native, err = c.NewShader(c.VertexShader, shaders[shaderVertex].source)
if err != nil { if err != nil {
@ -39,6 +41,22 @@ func initialize(c *opengl.Context) error {
shaders[shaderColorMatrix].native, shaders[shaderColorMatrix].native,
} }
programColorMatrix, err = c.NewProgram(shaders) programColorMatrix, err = c.NewProgram(shaders)
const stride = 4 * 4
s := float32Size * stride * size
c.NewBuffer(c.ArrayBuffer, s, nil, c.DynamicDraw)
indices := make([]uint16, 6*size)
for i := uint16(0); i < size; i++ {
indices[6*i+0] = 4*i + 0
indices[6*i+1] = 4*i + 1
indices[6*i+2] = 4*i + 2
indices[6*i+3] = 4*i + 1
indices[6*i+4] = 4*i + 2
indices[6*i+5] = 4*i + 3
}
c.NewBuffer(c.ElementArrayBuffer, uint16Size*len(indices), indices, c.StaticDraw)
return err return err
} }

View File

@ -54,7 +54,7 @@ func (t *Texture) Size() (width, height int) {
return t.width, t.height return t.width, t.height
} }
func NewTexture(c *opengl.Context, width, height int, filter opengl.Filter) (*Texture, error) { func NewTexture(c *opengl.Context, width, height int, filter opengl.FilterType) (*Texture, error) {
w := internal.NextPowerOf2Int(width) w := internal.NextPowerOf2Int(width)
h := internal.NextPowerOf2Int(height) h := internal.NextPowerOf2Int(height)
if w < 4 { if w < 4 {
@ -70,7 +70,7 @@ func NewTexture(c *opengl.Context, width, height int, filter opengl.Filter) (*Te
return &Texture{native, width, height}, nil return &Texture{native, width, height}, nil
} }
func NewTextureFromImage(c *opengl.Context, img image.Image, filter opengl.Filter) (*Texture, error) { func NewTextureFromImage(c *opengl.Context, img image.Image, filter opengl.FilterType) (*Texture, error) {
origSize := img.Bounds().Size() origSize := img.Bounds().Size()
if origSize.X < 4 { if origSize.X < 4 {
return nil, errors.New("width must be equal or more than 4.") return nil, errors.New("width must be equal or more than 4.")

View File

@ -20,19 +20,10 @@ import (
"github.com/go-gl/gl" "github.com/go-gl/gl"
) )
type Filter int type FilterType int
const (
filterNearest Filter = gl.NEAREST
filterLinear = gl.LINEAR
)
type ShaderType int type ShaderType int
type BufferType int
const ( type BufferUsageType int
shaderTypeVertex ShaderType = gl.VERTEX_SHADER
shaderTypeFragment = gl.FRAGMENT_SHADER
)
type Texture gl.Texture type Texture gl.Texture
@ -127,18 +118,26 @@ func (u UniformLocation) Uniform1i(v int) {
} }
type Context struct { type Context struct {
Nearest Filter Nearest FilterType
Linear Filter Linear FilterType
VertexShader ShaderType VertexShader ShaderType
FragmentShader ShaderType FragmentShader ShaderType
ArrayBuffer BufferType
ElementArrayBuffer BufferType
DynamicDraw BufferUsageType
StaticDraw BufferUsageType
} }
func NewContext() *Context { func NewContext() *Context {
c := &Context{ c := &Context{
Nearest: filterNearest, Nearest: gl.NEAREST,
Linear: filterLinear, Linear: gl.LINEAR,
VertexShader: shaderTypeVertex, VertexShader: gl.VERTEX_SHADER,
FragmentShader: shaderTypeFragment, FragmentShader: gl.FRAGMENT_SHADER,
ArrayBuffer: gl.ARRAY_BUFFER,
ElementArrayBuffer: gl.ELEMENT_ARRAY_BUFFER,
DynamicDraw: gl.DYNAMIC_DRAW,
StaticDraw: gl.STATIC_DRAW,
} }
c.init() c.init()
return c return c
@ -152,7 +151,7 @@ func (c *Context) init() {
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
} }
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) { func (c *Context) NewTexture(width, height int, pixels []uint8, filter FilterType) (Texture, error) {
t := gl.GenTexture() t := gl.GenTexture()
if t < 0 { if t < 0 {
return 0, errors.New("glGenTexture failed") return 0, errors.New("glGenTexture failed")
@ -216,3 +215,8 @@ func (c *Context) NewProgram(shaders []Shader) (Program, error) {
} }
return Program(p), nil return Program(p), nil
} }
func (c *Context) NewBuffer(bufferType BufferType, size int, ptr interface{}, bufferUsageType BufferUsageType) {
gl.GenBuffer().Bind(gl.GLenum(bufferType))
gl.BufferData(gl.GLenum(bufferType), size, ptr, gl.GLenum(bufferUsageType))
}