ebiten/graphics/opengl/rendertarget/render_target.go
2013-11-17 22:42:12 +09:00

62 lines
1.7 KiB
Go

package rendertarget
// #cgo LDFLAGS: -framework OpenGL
//
// #include <OpenGL/gl.h>
import "C"
import (
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
grendertarget "github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
)
type Framebuffer C.GLuint
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")
}
// 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
}
func New(width, height int, filter texture.Filter) (
*grendertarget.RenderTarget, *gtexture.Texture, error) {
tex, err := texture.New(width, height, filter)
if err != nil {
return nil, nil, err
}
f := func(native interface{}) interface{} {
return createFramebuffer(C.GLuint(native.(texture.Native)))
}
framebuffer := tex.CreateFramebuffer(f)
return grendertarget.NewWithFramebuffer(tex,
Framebuffer(framebuffer.(C.GLuint))), tex, nil
}
func NewWithFramebuffer(width, height int, framebuffer Framebuffer) (
*grendertarget.RenderTarget, error) {
tex, err := texture.NewEmpty(width, height)
if err != nil {
return nil, err
}
return grendertarget.NewWithFramebuffer(tex, framebuffer), nil
}