2013-10-27 11:54:28 +01:00
|
|
|
package rendertarget
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -framework OpenGL
|
|
|
|
//
|
|
|
|
// #include <OpenGL/gl.h>
|
|
|
|
import "C"
|
|
|
|
import (
|
2014-01-08 08:38:03 +01:00
|
|
|
"fmt"
|
2013-12-18 10:05:28 +01:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
2013-10-27 11:54:28 +01:00
|
|
|
)
|
|
|
|
|
2014-01-08 10:47:38 +01:00
|
|
|
type NativeTexture C.GLuint
|
|
|
|
|
2014-01-08 06:37:07 +01:00
|
|
|
type RenderTarget struct {
|
2014-01-08 10:47:38 +01:00
|
|
|
framebuffer C.GLuint
|
2014-01-08 10:03:21 +01:00
|
|
|
width int
|
|
|
|
height int
|
2014-01-10 13:28:50 +01:00
|
|
|
flipY bool
|
2013-10-27 11:54:28 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 10:47:38 +01:00
|
|
|
func NewWithCurrentFramebuffer(width, height int) *RenderTarget {
|
|
|
|
framebuffer := C.GLint(0)
|
|
|
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &framebuffer)
|
2014-01-10 13:28:50 +01:00
|
|
|
rt := &RenderTarget{C.GLuint(framebuffer), width, height, true}
|
|
|
|
rt.setAsViewport()
|
|
|
|
return rt
|
2014-01-08 10:47:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
|
|
|
|
framebuffer := C.GLuint(0)
|
|
|
|
C.glGenFramebuffers(1, &framebuffer)
|
|
|
|
|
|
|
|
origFramebuffer := C.GLint(0)
|
|
|
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &origFramebuffer)
|
|
|
|
|
|
|
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
|
|
|
|
defer C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer))
|
|
|
|
|
|
|
|
C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0,
|
|
|
|
C.GL_TEXTURE_2D, nativeTexture, 0)
|
|
|
|
if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) !=
|
|
|
|
C.GL_FRAMEBUFFER_COMPLETE {
|
|
|
|
panic("creating framebuffer failed")
|
2013-10-27 11:54:28 +01:00
|
|
|
}
|
2014-01-08 10:47:38 +01:00
|
|
|
|
|
|
|
// Set this framebuffer opaque because alpha values on a target might be
|
|
|
|
// confusing.
|
|
|
|
C.glClearColor(0, 0, 0, 1)
|
|
|
|
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
|
|
|
|
|
|
|
return framebuffer
|
2013-10-27 11:54:28 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 10:47:38 +01:00
|
|
|
func CreateFromTexture(native NativeTexture, width, height int) *RenderTarget {
|
|
|
|
framebuffer := createFramebuffer(C.GLuint(native))
|
2014-01-10 13:28:50 +01:00
|
|
|
return &RenderTarget{framebuffer, width, height, false}
|
2014-01-07 13:58:46 +01:00
|
|
|
}
|
|
|
|
|
2014-01-10 13:28:50 +01:00
|
|
|
func (r *RenderTarget) setAsViewport() {
|
2014-01-08 10:47:38 +01:00
|
|
|
C.glFlush()
|
|
|
|
|
2014-01-08 08:38:03 +01:00
|
|
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(r.framebuffer))
|
|
|
|
err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER)
|
|
|
|
if err != C.GL_FRAMEBUFFER_COMPLETE {
|
|
|
|
panic(fmt.Sprintf("glBindFramebuffer failed: %d", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
C.glBlendFuncSeparate(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA,
|
|
|
|
C.GL_ZERO, C.GL_ONE)
|
|
|
|
|
|
|
|
width := graphics.AdjustSizeForTexture(r.width)
|
|
|
|
height := graphics.AdjustSizeForTexture(r.height)
|
|
|
|
C.glViewport(0, 0, C.GLsizei(width), C.GLsizei(height))
|
|
|
|
}
|
|
|
|
|
2014-01-10 13:28:50 +01:00
|
|
|
func (r *RenderTarget) SetAsViewport() {
|
|
|
|
current := C.GLint(0)
|
|
|
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, ¤t)
|
|
|
|
if C.GLuint(current) == r.framebuffer {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.setAsViewport()
|
|
|
|
}
|
|
|
|
|
2014-01-08 08:38:03 +01:00
|
|
|
func (r *RenderTarget) ProjectionMatrix() [4][4]float64 {
|
|
|
|
width := graphics.AdjustSizeForTexture(r.width)
|
|
|
|
height := graphics.AdjustSizeForTexture(r.height)
|
2014-01-10 13:28:50 +01:00
|
|
|
matrix := graphics.OrthoProjectionMatrix(0, width, 0, height)
|
|
|
|
if r.flipY {
|
|
|
|
// Flip Y and move to fit with the top of the window.
|
|
|
|
matrix[1][1] *= -1
|
|
|
|
matrix[1][3] += float64(r.height) /
|
|
|
|
float64(graphics.AdjustSizeForTexture(r.height)) * 2
|
|
|
|
}
|
|
|
|
return matrix
|
2014-01-08 08:38:03 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 06:37:07 +01:00
|
|
|
func (r *RenderTarget) Dispose() {
|
2014-01-08 10:58:15 +01:00
|
|
|
C.glDeleteFramebuffers(1, &r.framebuffer)
|
2014-01-07 13:58:46 +01:00
|
|
|
}
|