diff --git a/graphics/opengl/shader/draw_texture.go b/graphics/opengl/shader/draw_texture.go new file mode 100644 index 000000000..37e068d5f --- /dev/null +++ b/graphics/opengl/shader/draw_texture.go @@ -0,0 +1,82 @@ +package shader + +// #cgo LDFLAGS: -framework OpenGL +// +// #include +// #include +import "C" +import ( + "github.com/hajimehoshi/go-ebiten/graphics/matrix" + "github.com/hajimehoshi/go-ebiten/graphics/opengl/texture" + gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture" + "unsafe" +) + +func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []gtexture.Quad, + geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { + if !initialized { + initialize() + } + + if len(quads) == 0 { + return + } + shaderProgram := use(projectionMatrix, geometryMatrix, colorMatrix) + // This state affects the other functions, so can't disable here... + C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(native)) + defer C.glBindTexture(C.GL_TEXTURE_2D, 0) + + vertexAttrLocation := getAttributeLocation(shaderProgram, "vertex") + textureAttrLocation := getAttributeLocation(shaderProgram, "texture") + + C.glEnableClientState(C.GL_VERTEX_ARRAY) + C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY) + C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation)) + C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation)) + defer func() { + C.glDisableVertexAttribArray(C.GLuint(textureAttrLocation)) + C.glDisableVertexAttribArray(C.GLuint(vertexAttrLocation)) + C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY) + C.glDisableClientState(C.GL_VERTEX_ARRAY) + }() + + vertices := []float32{} + texCoords := []float32{} + indicies := []uint32{} + // TODO: Check len(parts) and GL_MAX_ELEMENTS_INDICES + for i, quad := range quads { + x1 := quad.VertexX1 + x2 := quad.VertexX2 + y1 := quad.VertexY1 + y2 := quad.VertexY2 + vertices = append(vertices, + x1, y1, + x2, y1, + x1, y2, + x2, y2, + ) + u1 := quad.TextureCoordU1 + u2 := quad.TextureCoordU2 + v1 := quad.TextureCoordV1 + v2 := quad.TextureCoordV2 + texCoords = append(texCoords, + u1, v1, + u2, v1, + u1, v2, + u2, v2, + ) + base := uint32(i * 4) + indicies = append(indicies, + base, base+1, base+2, + base+1, base+2, base+3, + ) + } + C.glVertexAttribPointer(C.GLuint(vertexAttrLocation), 2, + C.GL_FLOAT, C.GL_FALSE, + 0, unsafe.Pointer(&vertices[0])) + C.glVertexAttribPointer(C.GLuint(textureAttrLocation), 2, + C.GL_FLOAT, C.GL_FALSE, + 0, unsafe.Pointer(&texCoords[0])) + C.glDrawElements(C.GL_TRIANGLES, C.GLsizei(len(indicies)), + C.GL_UNSIGNED_INT, unsafe.Pointer(&indicies[0])) +} diff --git a/graphics/opengl/shader/program.go b/graphics/opengl/shader/program.go index 0e6450abc..c6b136d21 100644 --- a/graphics/opengl/shader/program.go +++ b/graphics/opengl/shader/program.go @@ -7,8 +7,6 @@ package shader import "C" import ( "github.com/hajimehoshi/go-ebiten/graphics/matrix" - "github.com/hajimehoshi/go-ebiten/graphics/opengl/texture" - gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture" "unsafe" ) @@ -161,72 +159,3 @@ func use(projectionMatrix [16]float32, geometryMatrix matrix.Geometry, colorMatr return program } - -func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []gtexture.Quad, - geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { - if !initialized { - initialize() - } - - if len(quads) == 0 { - return - } - shaderProgram := use(projectionMatrix, geometryMatrix, colorMatrix) - // This state affects the other functions, so can't disable here... - C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(native)) - defer C.glBindTexture(C.GL_TEXTURE_2D, 0) - - vertexAttrLocation := getAttributeLocation(shaderProgram, "vertex") - textureAttrLocation := getAttributeLocation(shaderProgram, "texture") - - C.glEnableClientState(C.GL_VERTEX_ARRAY) - C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY) - C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation)) - C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation)) - defer func() { - C.glDisableVertexAttribArray(C.GLuint(textureAttrLocation)) - C.glDisableVertexAttribArray(C.GLuint(vertexAttrLocation)) - C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY) - C.glDisableClientState(C.GL_VERTEX_ARRAY) - }() - - vertices := []float32{} - texCoords := []float32{} - indicies := []uint32{} - // TODO: Check len(parts) and GL_MAX_ELEMENTS_INDICES - for i, quad := range quads { - x1 := quad.VertexX1 - x2 := quad.VertexX2 - y1 := quad.VertexY1 - y2 := quad.VertexY2 - vertices = append(vertices, - x1, y1, - x2, y1, - x1, y2, - x2, y2, - ) - u1 := quad.TextureCoordU1 - u2 := quad.TextureCoordU2 - v1 := quad.TextureCoordV1 - v2 := quad.TextureCoordV2 - texCoords = append(texCoords, - u1, v1, - u2, v1, - u1, v2, - u2, v2, - ) - base := uint32(i * 4) - indicies = append(indicies, - base, base+1, base+2, - base+1, base+2, base+3, - ) - } - C.glVertexAttribPointer(C.GLuint(vertexAttrLocation), 2, - C.GL_FLOAT, C.GL_FALSE, - 0, unsafe.Pointer(&vertices[0])) - C.glVertexAttribPointer(C.GLuint(textureAttrLocation), 2, - C.GL_FLOAT, C.GL_FALSE, - 0, unsafe.Pointer(&texCoords[0])) - C.glDrawElements(C.GL_TRIANGLES, C.GLsizei(len(indicies)), - C.GL_UNSIGNED_INT, unsafe.Pointer(&indicies[0])) -}