ebiten/graphics/opengl/context.go

371 lines
11 KiB
Go
Raw Normal View History

2013-07-01 15:25:41 +02:00
// Copyright 2013 Hajime Hoshi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
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-06-20 16:29:51 +02:00
"github.com/hajimehoshi/go.ebiten/graphics"
2013-06-20 18:47:39 +02:00
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
2013-06-19 16:08:24 +02:00
"image"
2013-06-15 10:07:14 +02:00
"image/color"
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-07-12 19:04:19 +02:00
screen *Texture
2013-10-05 18:47:19 +02:00
screenWidth int
screenHeight int
2013-06-22 08:56:25 +02:00
screenScale int
textures map[graphics.TextureID]*Texture
2013-07-12 19:04:19 +02:00
currentOffscreen *Texture
2013-06-22 08:56:25 +02:00
projectionMatrix [16]float32
currentShaderProgram C.GLuint
2013-07-12 18:36:01 +02:00
mainFramebufferTexture *Texture
2013-06-15 10:07:14 +02:00
}
// This method should be called on the UI thread.
2013-06-27 17:33:03 +02:00
func newContext(screenWidth, screenHeight, screenScale int) *Context {
context := &Context{
2013-10-05 18:47:19 +02:00
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
textures: map[graphics.TextureID]*Texture{},
2013-06-15 10:07:14 +02:00
}
2013-10-05 18:47:19 +02:00
return context
}
func (context *Context) Init() {
2013-06-15 10:07:14 +02:00
// main framebuffer should be created sooner than any other framebuffers!
mainFramebuffer := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
2013-06-17 16:10:55 +02:00
2013-07-12 18:36:01 +02:00
context.mainFramebufferTexture = newVirtualTexture(
2013-10-05 18:47:19 +02:00
context.screenWidth*context.screenScale,
context.screenHeight*context.screenScale)
2013-07-12 19:04:19 +02:00
context.mainFramebufferTexture.framebuffer = C.GLuint(mainFramebuffer)
2013-07-12 18:36:01 +02:00
2013-06-17 16:10:55 +02:00
initializeShaders()
2013-10-05 18:47:19 +02:00
context.screen =
context.NewTexture(context.screenWidth, context.screenHeight).(*Texture)
2013-07-04 16:57:53 +02:00
}
func (context *Context) Screen() graphics.Texture {
return context.screen
}
2013-06-27 17:33:03 +02:00
func (context *Context) Clear() {
2013-06-24 15:03:38 +02:00
C.glClearColor(0, 0, 0, 0)
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) Fill(clr color.Color) {
2013-06-19 01:49:54 +02:00
r, g, b, a := clr.RGBA()
2013-06-22 08:56:25 +02:00
max := float64(math.MaxUint16)
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),
C.GLclampf(float64(a)/max))
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) DrawRect(rect graphics.Rect, clr color.Color) {
2013-07-12 19:04:19 +02:00
width := float32(context.currentOffscreen.Width())
height := float32(context.currentOffscreen.Height())
2013-06-22 19:42:13 +02:00
textureWidth := float32(clp2(uint64(width)))
textureHeight := float32(clp2(uint64(height)))
2013-06-22 08:56:25 +02:00
// Normalize the coord between -1.0 and 1.0.
x1 := float32(rect.X)/textureWidth*2.0 - 1.0
x2 := float32(rect.X+rect.Width)/textureHeight*2.0 - 1.0
y1 := float32(rect.Y)/textureHeight*2.0 - 1.0
y2 := float32(rect.Y+rect.Height)/textureHeight*2.0 - 1.0
2013-06-22 08:56:25 +02:00
vertex := [...]float32{
x1, y1,
x2, y1,
x1, y2,
x2, y2,
}
origR, origG, origB, origA := clr.RGBA()
max := float32(math.MaxUint16)
r := float32(origR) / max
g := float32(origG) / max
b := float32(origB) / max
a := float32(origA) / max
color := [...]float32{
r, g, b, a,
r, g, b, a,
r, g, b, a,
r, g, b, a,
}
C.glUseProgram(0)
context.currentShaderProgram = 0
C.glDisable(C.GL_TEXTURE_2D)
C.glEnableClientState(C.GL_VERTEX_ARRAY)
C.glEnableClientState(C.GL_COLOR_ARRAY)
C.glVertexPointer(2, C.GL_FLOAT, C.GL_FALSE, unsafe.Pointer(&vertex[0]))
C.glColorPointer(4, C.GL_FLOAT, C.GL_FALSE, unsafe.Pointer(&color[0]))
C.glDrawArrays(C.GL_TRIANGLE_STRIP, 0, 4)
C.glDisableClientState(C.GL_COLOR_ARRAY)
C.glDisableClientState(C.GL_VERTEX_ARRAY)
C.glEnable(C.GL_TEXTURE_2D)
if glError := C.glGetError(); glError != C.GL_NO_ERROR {
panic("OpenGL error")
}
2013-06-15 10:07:14 +02:00
}
2013-06-27 17:33:03 +02:00
func (context *Context) DrawTexture(
textureID graphics.TextureID,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
texture := context.textures[textureID]
source := graphics.Rect{0, 0, texture.width, texture.height}
2013-06-22 19:16:22 +02:00
locations := []graphics.TexturePart{{0, 0, source}}
context.DrawTextureParts(textureID, locations,
geometryMatrix, colorMatrix)
}
2013-06-27 17:33:03 +02:00
func (context *Context) DrawTextureParts(
2013-07-02 18:27:04 +02:00
textureID graphics.TextureID, parts []graphics.TexturePart,
2013-06-20 18:47:39 +02:00
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
2013-06-19 16:08:24 +02:00
texture := context.textures[textureID]
2013-07-10 15:59:38 +02:00
if texture == nil {
panic("invalid texture ID")
}
2013-06-19 01:49:54 +02:00
2013-06-17 16:10:55 +02:00
context.setShaderProgram(geometryMatrix, colorMatrix)
C.glBindTexture(C.GL_TEXTURE_2D, texture.id)
vertexAttrLocation := getAttributeLocation(context.currentShaderProgram, "vertex")
textureAttrLocation := getAttributeLocation(context.currentShaderProgram, "texture")
C.glEnableClientState(C.GL_VERTEX_ARRAY)
C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY)
C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation))
C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation))
2013-07-09 15:48:56 +02:00
// TODO: Optimization
2013-07-02 18:27:04 +02:00
for _, part := range parts {
x1 := float32(part.LocationX)
x2 := float32(part.LocationX + part.Source.Width)
y1 := float32(part.LocationY)
y2 := float32(part.LocationY + part.Source.Height)
vertex := [...]float32{
x1, y1,
x2, y1,
x1, y2,
x2, y2,
}
2013-07-02 18:27:04 +02:00
src := part.Source
tu1 := float32(src.X) / float32(texture.textureWidth)
tu2 := float32(src.X+src.Width) / float32(texture.textureWidth)
tv1 := float32(src.Y) / float32(texture.textureHeight)
tv2 := float32(src.Y+src.Height) / float32(texture.textureHeight)
texCoord := [...]float32{
tu1, tv1,
tu2, tv1,
tu1, tv2,
tu2, tv2,
}
C.glVertexAttribPointer(C.GLuint(vertexAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE,
0, unsafe.Pointer(&vertex[0]))
C.glVertexAttribPointer(C.GLuint(textureAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE,
0, unsafe.Pointer(&texCoord[0]))
C.glDrawArrays(C.GL_TRIANGLE_STRIP, 0, 4)
}
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-06-18 18:14:55 +02:00
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
2013-06-27 17:33:03 +02:00
func (context *Context) SetOffscreen(textureID graphics.TextureID) {
2013-06-19 16:08:24 +02:00
texture := context.textures[textureID]
2013-07-12 19:04:19 +02:00
if texture.framebuffer == 0 {
texture.framebuffer = createFramebuffer(texture.id)
2013-06-19 16:08:24 +02:00
}
2013-07-12 19:04:19 +02:00
context.setOffscreen(texture)
context.currentOffscreen = texture
2013-06-19 16:08:24 +02:00
}
2013-07-12 19:04:19 +02:00
func (context *Context) setOffscreen(texture *Texture) {
2013-06-19 02:41:17 +02:00
C.glFlush()
2013-07-12 19:04:19 +02:00
C.glBindFramebuffer(C.GL_FRAMEBUFFER, texture.framebuffer)
2013-06-19 16:51:41 +02:00
if err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER); 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
}
C.glEnable(C.GL_BLEND)
C.glBlendFunc(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA)
2013-07-12 19:04:19 +02:00
C.glViewport(0, 0, C.GLsizei(abs(texture.textureWidth)),
C.GLsizei(abs(texture.textureHeight)))
var e11, e22, e41, e42 float32
2013-07-12 19:04:19 +02:00
if texture != context.mainFramebufferTexture {
e11 = float32(2) / float32(texture.textureWidth)
e22 = float32(2) / float32(texture.textureWidth)
e41 = -1
e42 = -1
2013-06-17 16:10:55 +02:00
} else {
2013-07-12 19:04:19 +02:00
height := float32(texture.Height())
e11 = float32(2) / float32(texture.textureWidth)
e22 = -1 * float32(2) / float32(texture.textureHeight)
e41 = -1
2013-07-12 19:04:19 +02:00
e42 = -1 + height/float32(texture.textureHeight)*2
2013-06-17 16:10:55 +02:00
}
2013-06-22 16:16:01 +02:00
2013-06-17 16:10:55 +02:00
context.projectionMatrix = [...]float32{
2013-06-19 16:51:41 +02:00
e11, 0, 0, 0,
0, e22, 0, 0,
0, 0, 1, 0,
2013-06-17 16:10:55 +02:00
e41, e42, 0, 1,
}
2013-06-15 10:07:14 +02:00
}
2013-06-27 17:33:03 +02:00
func (context *Context) resetOffscreen() {
2013-07-12 19:04:19 +02:00
context.setOffscreen(context.mainFramebufferTexture)
context.currentOffscreen = context.mainFramebufferTexture
2013-06-15 10:07:14 +02:00
}
// This method should be called on the UI thread.
2013-06-27 17:33:03 +02:00
func (context *Context) flush() {
2013-06-15 10:07:14 +02:00
C.glFlush()
}
// This method should be called on the UI thread.
2013-06-27 17:33:03 +02:00
func (context *Context) setShaderProgram(
2013-06-20 18:47:39 +02:00
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
2013-06-15 10:07:14 +02:00
program := C.GLuint(0)
if colorMatrix.IsIdentity() {
program = regularShaderProgram
} else {
program = colorMatrixShaderProgram
}
// TODO: cache and skip?
C.glUseProgram(program)
context.currentShaderProgram = program
C.glUniformMatrix4fv(getUniformLocation(program, "projection_matrix"),
1, C.GL_FALSE,
(*C.GLfloat)(&context.projectionMatrix[0]))
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-07-12 19:04:19 +02:00
func createFramebuffer(textureID C.GLuint) C.GLuint {
framebuffer := C.GLuint(0)
C.glGenFramebuffers(1, &framebuffer)
2013-06-15 10:07:14 +02:00
origFramebuffer := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &origFramebuffer)
2013-07-12 19:04:19 +02:00
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
2013-06-15 10:07:14 +02:00
C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0,
2013-06-19 16:08:24 +02:00
C.GL_TEXTURE_2D, textureID, 0)
2013-06-15 10:07:14 +02:00
C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer))
if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) != C.GL_FRAMEBUFFER_COMPLETE {
panic("creating framebuffer failed")
}
2013-07-12 19:04:19 +02:00
return framebuffer
2013-06-19 16:08:24 +02:00
}
2013-06-27 17:33:03 +02:00
func (context *Context) NewTexture(width, height int) graphics.Texture {
2013-06-19 16:08:24 +02:00
texture := newTexture(width, height)
id := graphics.TextureID(texture.id)
context.textures[id] = texture
2013-06-22 19:23:45 +02:00
2013-07-12 18:36:01 +02:00
context.SetOffscreen(texture.ID())
2013-06-22 19:23:45 +02:00
context.Clear()
context.resetOffscreen()
2013-07-12 18:36:01 +02:00
return texture
2013-06-19 16:08:24 +02:00
}
2013-06-27 17:33:03 +02:00
func (context *Context) NewTextureFromImage(img image.Image) (graphics.Texture, error) {
2013-06-23 09:06:32 +02:00
texture, err := newTextureFromImage(img)
if err != nil {
2013-07-12 18:36:01 +02:00
return nil, err
2013-06-23 09:06:32 +02:00
}
2013-06-19 16:08:24 +02:00
id := graphics.TextureID(texture.id)
context.textures[id] = texture
2013-07-12 18:36:01 +02:00
return texture, nil
2013-06-15 10:07:14 +02:00
}