2013-10-27 11:54:28 +01:00
|
|
|
package rendertarget
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -framework OpenGL
|
|
|
|
//
|
|
|
|
// #include <OpenGL/gl.h>
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
2013-10-27 14:58:56 +01:00
|
|
|
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
|
2013-10-27 11:54:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Framebuffer C.GLuint
|
|
|
|
|
2013-11-28 01:57:30 +01:00
|
|
|
func createFramebuffer(nativeTexture C.GLuint) Framebuffer {
|
2013-10-27 11:54:28 +01:00
|
|
|
framebuffer := C.GLuint(0)
|
|
|
|
C.glGenFramebuffers(1, &framebuffer)
|
|
|
|
|
|
|
|
origFramebuffer := C.GLint(0)
|
|
|
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &origFramebuffer)
|
2013-10-27 13:27:16 +01:00
|
|
|
|
2013-10-27 11:54:28 +01:00
|
|
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
|
2013-10-27 13:27:16 +01:00
|
|
|
defer C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer))
|
|
|
|
|
2013-10-27 11:54:28 +01:00
|
|
|
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 14:58:56 +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)
|
|
|
|
|
2013-11-28 01:57:30 +01:00
|
|
|
return Framebuffer(framebuffer)
|
2013-10-27 11:54:28 +01:00
|
|
|
}
|
|
|
|
|
2013-11-28 01:36:31 +01:00
|
|
|
// TODO: Rename them
|
2013-10-27 13:27:16 +01:00
|
|
|
func New(width, height int, filter texture.Filter) (
|
2013-11-28 01:36:31 +01:00
|
|
|
*gtexture.RenderTarget, *gtexture.Texture, error) {
|
2013-10-27 13:27:16 +01:00
|
|
|
tex, err := texture.New(width, height, filter)
|
2013-10-27 11:54:28 +01:00
|
|
|
if err != nil {
|
2013-10-27 14:09:19 +01:00
|
|
|
return nil, nil, err
|
2013-10-27 11:54:28 +01:00
|
|
|
}
|
2013-10-27 14:58:56 +01:00
|
|
|
f := func(native interface{}) interface{} {
|
2013-10-27 13:29:39 +01:00
|
|
|
return createFramebuffer(C.GLuint(native.(texture.Native)))
|
|
|
|
}
|
2013-11-28 01:57:30 +01:00
|
|
|
return gtexture.NewRenderTarget(tex, f), tex, nil
|
2013-10-27 11:54:28 +01:00
|
|
|
}
|
|
|
|
|
2013-10-27 14:12:28 +01:00
|
|
|
func NewWithFramebuffer(width, height int, framebuffer Framebuffer) (
|
2013-11-28 01:36:31 +01:00
|
|
|
*gtexture.RenderTarget, error) {
|
2013-10-27 14:12:28 +01:00
|
|
|
tex, err := texture.NewEmpty(width, height)
|
2013-10-27 11:54:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-28 01:57:30 +01:00
|
|
|
f := func(native interface{}) interface{} {
|
|
|
|
return framebuffer
|
|
|
|
}
|
|
|
|
return gtexture.NewRenderTarget(tex, f), nil
|
2013-10-27 11:54:28 +01:00
|
|
|
}
|