Remove all dependencies on go-gl/gl from internal/graphics

This commit is contained in:
Hajime Hoshi 2014-12-31 17:53:04 +09:00
parent f88154f380
commit cdcc46543b
3 changed files with 27 additions and 8 deletions

View File

@ -15,7 +15,6 @@
package shader package shader
import ( import (
"github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
) )
@ -42,7 +41,6 @@ type TextureQuads interface {
var initialized = false var initialized = false
// TODO: Use unsafe.SizeOf? // TODO: Use unsafe.SizeOf?
const uint16Size = 2
const float32Size = 4 const float32Size = 4
func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix [4][4]float64, quads TextureQuads, geo Matrix, color Matrix) error { func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix [4][4]float64, quads TextureQuads, geo Matrix, color Matrix) error {
@ -62,8 +60,8 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix [4]
// TODO: Check performance // TODO: Check performance
program := useProgramColorMatrix(glMatrix(projectionMatrix), geo, color) program := useProgramColorMatrix(glMatrix(projectionMatrix), geo, color)
gl.ActiveTexture(gl.TEXTURE0) // TODO: Do we have to call gl.ActiveTexture(gl.TEXTURE0)?
gl.Texture(texture).Bind(gl.TEXTURE_2D) texture.Bind()
vertexAttrLocation := program.GetAttributeLocation("vertex") vertexAttrLocation := program.GetAttributeLocation("vertex")
texCoordAttrLocation := program.GetAttributeLocation("tex_coord") texCoordAttrLocation := program.GetAttributeLocation("tex_coord")
@ -89,9 +87,9 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix [4]
x1, y1, u1, v1, x1, y1, u1, v1,
) )
} }
gl.BufferSubData(gl.ARRAY_BUFFER, 0, float32Size*len(vertices), vertices) c.BufferSubData(c.ArrayBuffer, vertices)
gl.DrawElements(gl.TRIANGLES, 6*quads.Len(), gl.UNSIGNED_SHORT, uintptr(0)) c.DrawElements(6 * quads.Len())
gl.Flush() c.Flush()
return nil return nil
} }

View File

@ -22,6 +22,7 @@ var programColorMatrix opengl.Program
func initialize(c *opengl.Context) error { func initialize(c *opengl.Context) error {
const size = 10000 const size = 10000
const uint16Size = 2
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)
@ -41,6 +42,9 @@ func initialize(c *opengl.Context) error {
shaders[shaderColorMatrix].native, shaders[shaderColorMatrix].native,
} }
programColorMatrix, err = c.NewProgram(shaders) programColorMatrix, err = c.NewProgram(shaders)
if err != nil {
return err
}
const stride = 4 * 4 const stride = 4 * 4
s := float32Size * stride * size s := float32Size * stride * size
@ -57,7 +61,7 @@ func initialize(c *opengl.Context) error {
} }
c.NewBuffer(c.ElementArrayBuffer, uint16Size*len(indices), indices, c.StaticDraw) c.NewBuffer(c.ElementArrayBuffer, uint16Size*len(indices), indices, c.StaticDraw)
return err return nil
} }
var lastProgram opengl.Program = 0 var lastProgram opengl.Program = 0

View File

@ -39,6 +39,10 @@ func (t Texture) Pixels(width, height int) ([]uint8, error) {
return pixels, nil return pixels, nil
} }
func (t Texture) Bind() {
gl.Texture(t).Bind(gl.TEXTURE_2D)
}
func (t Texture) Delete() { func (t Texture) Delete() {
gl.Texture(t).Delete() gl.Texture(t).Delete()
} }
@ -220,3 +224,16 @@ func (c *Context) NewBuffer(bufferType BufferType, size int, ptr interface{}, bu
gl.GenBuffer().Bind(gl.GLenum(bufferType)) gl.GenBuffer().Bind(gl.GLenum(bufferType))
gl.BufferData(gl.GLenum(bufferType), size, ptr, gl.GLenum(bufferUsageType)) gl.BufferData(gl.GLenum(bufferType), size, ptr, gl.GLenum(bufferUsageType))
} }
func (c *Context) BufferSubData(bufferType BufferType, data []float32) {
const float32Size = 4
gl.BufferSubData(gl.GLenum(bufferType), 0, float32Size*len(data), data)
}
func (c *Context) DrawElements(len int) {
gl.DrawElements(gl.TRIANGLES, len, gl.UNSIGNED_SHORT, uintptr(0))
}
func (c *Context) Flush() {
gl.Flush()
}