2013-10-30 14:26:10 +01:00
|
|
|
package shader
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -framework OpenGL
|
|
|
|
//
|
|
|
|
// #include <OpenGL/gl.h>
|
|
|
|
// #include <stdlib.h>
|
|
|
|
import "C"
|
|
|
|
import (
|
2014-01-07 15:33:53 +01:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
2013-10-30 14:26:10 +01:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
2013-12-13 21:34:27 +01:00
|
|
|
"sync"
|
2013-10-30 14:26:10 +01:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2014-01-08 10:03:21 +01:00
|
|
|
type NativeTexture C.GLuint
|
|
|
|
|
2013-12-13 21:34:27 +01:00
|
|
|
var once sync.Once
|
|
|
|
|
2014-01-08 10:03:21 +01:00
|
|
|
func DrawTexture(native NativeTexture, projectionMatrix [16]float32,
|
|
|
|
quads []graphics.TextureQuad, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
2013-12-13 21:34:27 +01:00
|
|
|
once.Do(func() {
|
2013-10-30 14:26:10 +01:00
|
|
|
initialize()
|
2013-12-13 21:34:27 +01:00
|
|
|
})
|
2013-10-30 14:26:10 +01:00
|
|
|
|
|
|
|
if len(quads) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2014-05-03 20:29:45 +02:00
|
|
|
// TODO: Check performance
|
2013-10-30 14:26:10 +01:00
|
|
|
shaderProgram := use(projectionMatrix, geometryMatrix, colorMatrix)
|
2013-11-25 17:08:12 +01:00
|
|
|
|
2013-10-30 14:26:10 +01:00
|
|
|
C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(native))
|
|
|
|
defer C.glBindTexture(C.GL_TEXTURE_2D, 0)
|
|
|
|
|
|
|
|
vertexAttrLocation := getAttributeLocation(shaderProgram, "vertex")
|
2013-12-13 21:34:27 +01:00
|
|
|
texCoordAttrLocation := getAttributeLocation(shaderProgram, "tex_coord")
|
2013-10-30 14:26:10 +01:00
|
|
|
|
|
|
|
C.glEnableClientState(C.GL_VERTEX_ARRAY)
|
|
|
|
C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY)
|
|
|
|
C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation))
|
2013-12-13 21:34:27 +01:00
|
|
|
C.glEnableVertexAttribArray(C.GLuint(texCoordAttrLocation))
|
2013-10-30 14:26:10 +01:00
|
|
|
defer func() {
|
2013-12-13 21:34:27 +01:00
|
|
|
C.glDisableVertexAttribArray(C.GLuint(texCoordAttrLocation))
|
2013-10-30 14:26:10 +01:00
|
|
|
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]))
|
2013-12-13 21:34:27 +01:00
|
|
|
C.glVertexAttribPointer(C.GLuint(texCoordAttrLocation), 2,
|
2013-10-30 14:26:10 +01:00
|
|
|
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]))
|
|
|
|
}
|