ebiten/graphics/opengl/rendertarget/render_target.go

56 lines
1.5 KiB
Go
Raw Normal View History

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"
"github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
)
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)
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")
}
return framebuffer
}
2013-10-27 13:27:16 +01:00
func New(width, height int, filter texture.Filter) (
*rendertarget.RenderTarget, error) {
tex, err := texture.New(width, height, filter)
2013-10-27 11:54:28 +01:00
if err != nil {
return nil, err
}
2013-10-27 13:27:16 +01:00
framebuffer := C.GLuint(0)
tex.CreateFramebuffer(func(native interface{}) {
framebuffer =
createFramebuffer(C.GLuint(native.(texture.Native)))
})
2013-10-27 11:54:28 +01:00
return rendertarget.NewWithFramebuffer(tex, Framebuffer(framebuffer)), nil
}
2013-10-27 13:27:16 +01:00
func NewWithFramebuffer(width, height int, framebuffer Framebuffer,
filter texture.Filter) (*rendertarget.RenderTarget, error) {
tex, err := texture.New(width, height, filter)
2013-10-27 11:54:28 +01:00
if err != nil {
return nil, err
}
return rendertarget.NewWithFramebuffer(tex, framebuffer), nil
}