2013-06-19 01:49:54 +02:00
|
|
|
package opengl
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
// #cgo LDFLAGS: -framework OpenGL
|
|
|
|
//
|
|
|
|
// #include <stdlib.h>
|
2013-06-17 16:10:55 +02:00
|
|
|
// #include <OpenGL/gl.h>
|
2013-06-15 10:07:14 +02:00
|
|
|
import "C"
|
|
|
|
import (
|
2013-06-18 17:48:41 +02:00
|
|
|
"fmt"
|
2013-10-14 04:34:58 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
2013-10-25 02:30:39 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/texture"
|
2013-06-19 16:08:24 +02:00
|
|
|
"image"
|
2013-06-22 08:56:25 +02:00
|
|
|
"math"
|
2013-06-15 10:07:14 +02:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2013-06-27 17:33:03 +02:00
|
|
|
type Context struct {
|
2013-10-21 15:18:10 +02:00
|
|
|
screenId graphics.RenderTargetId
|
2013-10-05 18:47:19 +02:00
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
2013-06-22 08:56:25 +02:00
|
|
|
screenScale int
|
2013-10-25 02:30:39 +02:00
|
|
|
textures map[graphics.TextureId]*texture.Texture
|
2013-10-21 15:18:10 +02:00
|
|
|
renderTargets map[graphics.RenderTargetId]*RenderTarget
|
|
|
|
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
2013-10-19 11:47:34 +02:00
|
|
|
currentOffscreen *RenderTarget
|
|
|
|
mainFramebufferTexture *RenderTarget
|
2013-10-22 18:08:01 +02:00
|
|
|
projectionMatrix [16]float32
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 17:33:03 +02:00
|
|
|
func newContext(screenWidth, screenHeight, screenScale int) *Context {
|
|
|
|
context := &Context{
|
2013-10-19 18:52:18 +02:00
|
|
|
screenWidth: screenWidth,
|
|
|
|
screenHeight: screenHeight,
|
|
|
|
screenScale: screenScale,
|
2013-10-25 02:30:39 +02:00
|
|
|
textures: map[graphics.TextureId]*texture.Texture{},
|
2013-10-21 15:18:10 +02:00
|
|
|
renderTargets: map[graphics.RenderTargetId]*RenderTarget{},
|
|
|
|
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
2013-10-05 18:47:19 +02:00
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (context *Context) Init() {
|
2013-10-11 18:29:19 +02:00
|
|
|
// The main framebuffer should be created sooner than any other
|
|
|
|
// framebuffers!
|
2013-06-15 10:07:14 +02:00
|
|
|
mainFramebuffer := C.GLint(0)
|
|
|
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
|
2013-06-17 16:10:55 +02:00
|
|
|
|
2013-10-25 02:30:39 +02:00
|
|
|
var err error
|
|
|
|
context.mainFramebufferTexture, err = newRenderTargetWithFramebuffer(
|
2013-10-05 18:47:19 +02:00
|
|
|
context.screenWidth*context.screenScale,
|
2013-10-11 18:29:19 +02:00
|
|
|
context.screenHeight*context.screenScale,
|
|
|
|
C.GLuint(mainFramebuffer))
|
2013-10-25 02:30:39 +02:00
|
|
|
if err != nil {
|
|
|
|
panic("creating main framebuffer failed: " + err.Error())
|
|
|
|
}
|
2013-07-12 18:36:01 +02:00
|
|
|
|
2013-06-17 16:10:55 +02:00
|
|
|
initializeShaders()
|
|
|
|
|
2013-10-20 18:13:09 +02:00
|
|
|
context.screenId, err = context.NewRenderTarget(
|
2013-10-16 17:14:32 +02:00
|
|
|
context.screenWidth, context.screenHeight)
|
2013-10-20 18:13:09 +02:00
|
|
|
if err != nil {
|
|
|
|
panic("initializing the offscreen failed: " + err.Error())
|
|
|
|
}
|
2013-10-16 17:14:32 +02:00
|
|
|
}
|
|
|
|
|
2013-10-21 15:18:10 +02:00
|
|
|
func (context *Context) ToTexture(renderTargetId graphics.RenderTargetId) graphics.TextureId {
|
|
|
|
return context.renderTargetToTexture[renderTargetId]
|
2013-07-04 16:57:53 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 17:33:03 +02:00
|
|
|
func (context *Context) Clear() {
|
2013-10-11 20:20:13 +02:00
|
|
|
context.Fill(0, 0, 0)
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-10-11 20:20:13 +02:00
|
|
|
func (context *Context) Fill(r, g, b uint8) {
|
|
|
|
const max = float64(math.MaxUint8)
|
2013-06-17 16:10:55 +02:00
|
|
|
C.glClearColor(
|
2013-06-19 16:51:41 +02:00
|
|
|
C.GLclampf(float64(r)/max),
|
|
|
|
C.GLclampf(float64(g)/max),
|
|
|
|
C.GLclampf(float64(b)/max),
|
2013-10-11 20:20:13 +02:00
|
|
|
1)
|
2013-06-17 16:10:55 +02:00
|
|
|
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 17:33:03 +02:00
|
|
|
func (context *Context) DrawTexture(
|
2013-10-21 15:18:10 +02:00
|
|
|
textureId graphics.TextureId,
|
2013-06-21 17:50:55 +02:00
|
|
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
2013-10-21 15:18:10 +02:00
|
|
|
texture, ok := context.textures[textureId]
|
2013-10-19 11:47:34 +02:00
|
|
|
if !ok {
|
|
|
|
panic("invalid texture ID")
|
|
|
|
}
|
2013-10-25 02:30:39 +02:00
|
|
|
// TODO: fix this
|
|
|
|
source := graphics.Rect{0, 0, texture.Width(), texture.Height()}
|
2013-06-22 19:16:22 +02:00
|
|
|
locations := []graphics.TexturePart{{0, 0, source}}
|
2013-10-21 15:18:10 +02:00
|
|
|
context.DrawTextureParts(textureId, locations,
|
2013-06-21 17:50:55 +02:00
|
|
|
geometryMatrix, colorMatrix)
|
|
|
|
}
|
|
|
|
|
2013-06-27 17:33:03 +02:00
|
|
|
func (context *Context) DrawTextureParts(
|
2013-10-21 15:18:10 +02:00
|
|
|
textureId graphics.TextureId, parts []graphics.TexturePart,
|
2013-06-20 18:47:39 +02:00
|
|
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
2013-10-19 11:47:34 +02:00
|
|
|
|
2013-10-21 15:18:10 +02:00
|
|
|
texture, ok := context.textures[textureId]
|
2013-10-19 11:47:34 +02:00
|
|
|
if !ok {
|
2013-10-22 18:08:01 +02:00
|
|
|
panic("invalid texture ID")
|
2013-07-10 15:59:38 +02:00
|
|
|
}
|
2013-10-25 17:01:12 +02:00
|
|
|
if len(parts) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2013-06-19 01:49:54 +02:00
|
|
|
|
2013-10-09 16:34:11 +02:00
|
|
|
shaderProgram := context.setShaderProgram(geometryMatrix, colorMatrix)
|
2013-10-22 18:08:01 +02:00
|
|
|
C.glBindTexture(C.GL_TEXTURE_2D, texture.Native().(C.GLuint))
|
2013-06-17 16:10:55 +02:00
|
|
|
|
2013-10-09 16:34:11 +02:00
|
|
|
vertexAttrLocation := getAttributeLocation(shaderProgram, "vertex")
|
|
|
|
textureAttrLocation := getAttributeLocation(shaderProgram, "texture")
|
2013-06-17 16:10:55 +02:00
|
|
|
|
|
|
|
C.glEnableClientState(C.GL_VERTEX_ARRAY)
|
|
|
|
C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY)
|
|
|
|
C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation))
|
|
|
|
C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation))
|
2013-10-25 17:01:12 +02:00
|
|
|
vertices := []float32{}
|
|
|
|
texCoords := []float32{}
|
|
|
|
indicies := []uint32{}
|
|
|
|
// TODO: Check len(parts) and GL_MAX_ELEMENTS_INDICES
|
|
|
|
for i, part := range parts {
|
2013-07-02 18:27:04 +02:00
|
|
|
x1 := float32(part.LocationX)
|
|
|
|
x2 := float32(part.LocationX + part.Source.Width)
|
|
|
|
y1 := float32(part.LocationY)
|
|
|
|
y2 := float32(part.LocationY + part.Source.Height)
|
2013-10-25 17:01:12 +02:00
|
|
|
vertices = append(vertices,
|
2013-06-21 17:50:55 +02:00
|
|
|
x1, y1,
|
|
|
|
x2, y1,
|
|
|
|
x1, y2,
|
|
|
|
x2, y2,
|
2013-10-25 17:01:12 +02:00
|
|
|
)
|
2013-07-02 18:27:04 +02:00
|
|
|
src := part.Source
|
2013-10-22 18:08:01 +02:00
|
|
|
tu1 := float32(texture.U(src.X))
|
|
|
|
tu2 := float32(texture.U(src.X + src.Width))
|
|
|
|
tv1 := float32(texture.V(src.Y))
|
|
|
|
tv2 := float32(texture.V(src.Y + src.Height))
|
2013-10-25 17:01:12 +02:00
|
|
|
texCoords = append(texCoords,
|
2013-06-21 17:50:55 +02:00
|
|
|
tu1, tv1,
|
|
|
|
tu2, tv1,
|
|
|
|
tu1, tv2,
|
|
|
|
tu2, tv2,
|
2013-10-25 17:01:12 +02:00
|
|
|
)
|
|
|
|
base := uint32(i*4)
|
|
|
|
indicies = append(indicies,
|
|
|
|
base, base+1, base+2,
|
|
|
|
base+1, base+2, base+3,
|
|
|
|
)
|
2013-06-21 17:50:55 +02:00
|
|
|
}
|
2013-10-25 17:01:12 +02:00
|
|
|
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]))
|
2013-06-17 16:10:55 +02:00
|
|
|
C.glDisableVertexAttribArray(C.GLuint(textureAttrLocation))
|
|
|
|
C.glDisableVertexAttribArray(C.GLuint(vertexAttrLocation))
|
|
|
|
C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY)
|
|
|
|
C.glDisableClientState(C.GL_VERTEX_ARRAY)
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-10-16 16:06:35 +02:00
|
|
|
func (context *Context) ResetOffscreen() {
|
2013-10-20 08:51:26 +02:00
|
|
|
context.SetOffscreen(context.screenId)
|
2013-10-16 16:06:35 +02:00
|
|
|
}
|
|
|
|
|
2013-10-21 15:18:10 +02:00
|
|
|
func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
|
|
|
|
renderTarget := context.renderTargets[renderTargetId]
|
2013-10-11 18:29:19 +02:00
|
|
|
context.setOffscreen(renderTarget)
|
2013-06-19 16:08:24 +02:00
|
|
|
}
|
|
|
|
|
2013-10-19 11:47:34 +02:00
|
|
|
func (context *Context) setOffscreen(renderTarget *RenderTarget) {
|
2013-10-11 18:29:19 +02:00
|
|
|
context.currentOffscreen = renderTarget
|
2013-10-09 16:34:11 +02:00
|
|
|
|
2013-06-19 02:41:17 +02:00
|
|
|
C.glFlush()
|
|
|
|
|
2013-10-11 18:29:19 +02:00
|
|
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, renderTarget.framebuffer)
|
|
|
|
err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER)
|
|
|
|
if err != C.GL_FRAMEBUFFER_COMPLETE {
|
2013-06-18 17:48:41 +02:00
|
|
|
panic(fmt.Sprintf("glBindFramebuffer failed: %d", err))
|
2013-06-17 16:10:55 +02:00
|
|
|
}
|
2013-10-11 04:35:10 +02:00
|
|
|
|
2013-06-17 16:10:55 +02:00
|
|
|
C.glEnable(C.GL_BLEND)
|
2013-10-11 04:35:10 +02:00
|
|
|
C.glBlendFuncSeparate(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA,
|
|
|
|
C.GL_ZERO, C.GL_ONE)
|
2013-06-17 16:10:55 +02:00
|
|
|
|
2013-10-22 18:08:01 +02:00
|
|
|
context.currentOffscreen.SetAsViewport(context)
|
2013-10-09 16:34:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-22 18:08:01 +02:00
|
|
|
func (context *Context) SetViewport(x, y, width, height int) {
|
|
|
|
C.glViewport(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height))
|
2013-10-19 11:33:55 +02:00
|
|
|
|
2013-10-25 02:30:39 +02:00
|
|
|
matrix := graphics.OrthoProjectionMatrix(x, width, y, height)
|
2013-10-19 18:52:18 +02:00
|
|
|
if context.currentOffscreen == context.mainFramebufferTexture {
|
2013-10-25 02:30:39 +02:00
|
|
|
// Flip Y and move to fit with the top of the window.
|
2013-10-22 18:08:01 +02:00
|
|
|
matrix[1][1] *= -1
|
|
|
|
actualHeight := context.screenHeight * context.screenScale
|
|
|
|
matrix[1][3] += float64(actualHeight) / float64(height) * 2
|
2013-06-17 16:10:55 +02:00
|
|
|
}
|
2013-06-22 16:16:01 +02:00
|
|
|
|
2013-10-22 18:08:01 +02:00
|
|
|
for j := 0; j < 4; j++ {
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
context.projectionMatrix[i+j*4] = float32(matrix[i][j])
|
|
|
|
}
|
2013-06-17 16:10:55 +02:00
|
|
|
}
|
2013-10-22 18:08:01 +02:00
|
|
|
|
|
|
|
// TODO: call 'setShaderProgram' here?
|
|
|
|
}
|
|
|
|
|
|
|
|
func (context *Context) setMainFramebufferOffscreen() {
|
|
|
|
context.setOffscreen(context.mainFramebufferTexture)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (context *Context) flush() {
|
|
|
|
C.glFlush()
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 17:33:03 +02:00
|
|
|
func (context *Context) setShaderProgram(
|
2013-10-09 16:34:11 +02:00
|
|
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) (program C.GLuint) {
|
2013-06-15 10:07:14 +02:00
|
|
|
if colorMatrix.IsIdentity() {
|
|
|
|
program = regularShaderProgram
|
|
|
|
} else {
|
|
|
|
program = colorMatrixShaderProgram
|
|
|
|
}
|
|
|
|
// TODO: cache and skip?
|
|
|
|
C.glUseProgram(program)
|
|
|
|
|
|
|
|
C.glUniformMatrix4fv(getUniformLocation(program, "projection_matrix"),
|
|
|
|
1, C.GL_FALSE,
|
2013-10-22 18:08:01 +02:00
|
|
|
(*C.GLfloat)(&context.projectionMatrix[0]))
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2013-06-20 18:47:39 +02:00
|
|
|
a := float32(geometryMatrix.Elements[0][0])
|
|
|
|
b := float32(geometryMatrix.Elements[0][1])
|
|
|
|
c := float32(geometryMatrix.Elements[1][0])
|
|
|
|
d := float32(geometryMatrix.Elements[1][1])
|
|
|
|
tx := float32(geometryMatrix.Elements[0][2])
|
|
|
|
ty := float32(geometryMatrix.Elements[1][2])
|
2013-06-15 10:07:14 +02:00
|
|
|
glModelviewMatrix := [...]float32{
|
2013-06-19 16:51:41 +02:00
|
|
|
a, c, 0, 0,
|
|
|
|
b, d, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
2013-06-15 10:07:14 +02:00
|
|
|
tx, ty, 0, 1,
|
|
|
|
}
|
|
|
|
C.glUniformMatrix4fv(getUniformLocation(program, "modelview_matrix"),
|
|
|
|
1, C.GL_FALSE,
|
|
|
|
(*C.GLfloat)(&glModelviewMatrix[0]))
|
|
|
|
|
|
|
|
C.glUniform1i(getUniformLocation(program, "texture"), 0)
|
|
|
|
|
|
|
|
if program != colorMatrixShaderProgram {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e := [4][5]float32{}
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
for j := 0; j < 5; j++ {
|
2013-06-20 18:47:39 +02:00
|
|
|
e[i][j] = float32(colorMatrix.Elements[i][j])
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
glColorMatrix := [...]float32{
|
2013-06-23 16:43:44 +02:00
|
|
|
e[0][0], e[1][0], e[2][0], e[3][0],
|
|
|
|
e[0][1], e[1][1], e[2][1], e[3][1],
|
|
|
|
e[0][2], e[1][2], e[2][2], e[3][2],
|
|
|
|
e[0][3], e[1][3], e[2][3], e[3][3],
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
C.glUniformMatrix4fv(getUniformLocation(program, "color_matrix"),
|
|
|
|
1, C.GL_FALSE, (*C.GLfloat)(&glColorMatrix[0]))
|
|
|
|
|
|
|
|
glColorMatrixTranslation := [...]float32{
|
|
|
|
e[0][4], e[1][4], e[2][4], e[3][4],
|
|
|
|
}
|
|
|
|
C.glUniform4fv(getUniformLocation(program, "color_matrix_translation"),
|
|
|
|
1, (*C.GLfloat)(&glColorMatrixTranslation[0]))
|
2013-10-09 16:34:11 +02:00
|
|
|
|
|
|
|
return
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 18:13:09 +02:00
|
|
|
func (context *Context) NewRenderTarget(width, height int) (
|
2013-10-21 15:18:10 +02:00
|
|
|
graphics.RenderTargetId, error) {
|
2013-10-20 18:13:09 +02:00
|
|
|
renderTarget, err := newRenderTarget(width, height)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2013-10-21 15:18:10 +02:00
|
|
|
renderTargetId := graphics.RenderTargetId(<-newId)
|
|
|
|
textureId := graphics.TextureId(<-newId)
|
2013-10-19 18:52:18 +02:00
|
|
|
context.renderTargets[renderTargetId] = renderTarget
|
|
|
|
context.textures[textureId] = renderTarget.texture
|
|
|
|
context.renderTargetToTexture[renderTargetId] = textureId
|
2013-06-22 19:23:45 +02:00
|
|
|
|
2013-10-19 11:47:34 +02:00
|
|
|
context.setOffscreen(renderTarget)
|
2013-06-22 19:23:45 +02:00
|
|
|
context.Clear()
|
2013-10-22 18:08:01 +02:00
|
|
|
// TODO: Is it OK to revert he main framebuffer?
|
2013-10-17 17:45:12 +02:00
|
|
|
context.setMainFramebufferOffscreen()
|
2013-06-22 19:23:45 +02:00
|
|
|
|
2013-10-20 18:13:09 +02:00
|
|
|
return renderTargetId, nil
|
2013-06-19 16:08:24 +02:00
|
|
|
}
|
|
|
|
|
2013-10-11 18:29:19 +02:00
|
|
|
func (context *Context) NewTextureFromImage(img image.Image) (
|
2013-10-21 15:18:10 +02:00
|
|
|
graphics.TextureId, error) {
|
2013-10-25 02:30:39 +02:00
|
|
|
texture, err := texture.NewFromImage(img, &NativeTextureCreator{})
|
2013-06-23 09:06:32 +02:00
|
|
|
if err != nil {
|
2013-10-16 17:14:32 +02:00
|
|
|
return 0, err
|
2013-06-23 09:06:32 +02:00
|
|
|
}
|
2013-10-21 15:18:10 +02:00
|
|
|
textureId := graphics.TextureId(<-newId)
|
2013-10-19 18:52:18 +02:00
|
|
|
context.textures[textureId] = texture
|
|
|
|
return textureId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var newId chan int
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
newId = make(chan int)
|
|
|
|
go func() {
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
newId <- i
|
|
|
|
}
|
|
|
|
}()
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|