diff --git a/graphics/opengl/context.go b/graphics/opengl/context.go index 711c5bac2..021ca54b7 100644 --- a/graphics/opengl/context.go +++ b/graphics/opengl/context.go @@ -9,9 +9,10 @@ import ( "fmt" "github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics/matrix" - "github.com/hajimehoshi/go-ebiten/graphics/opengl/texture" + "github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget" "github.com/hajimehoshi/go-ebiten/graphics/opengl/shader" - "github.com/hajimehoshi/go-ebiten/graphics/rendertarget" + "github.com/hajimehoshi/go-ebiten/graphics/opengl/texture" + grendertarget "github.com/hajimehoshi/go-ebiten/graphics/rendertarget" gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture" "image" "math" @@ -23,9 +24,9 @@ type Context struct { screenHeight int screenScale int textures map[graphics.TextureId]*gtexture.Texture - renderTargets map[graphics.RenderTargetId]*rendertarget.RenderTarget + renderTargets map[graphics.RenderTargetId]*grendertarget.RenderTarget renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId - mainFramebufferTexture *rendertarget.RenderTarget + mainFramebufferTexture *grendertarget.RenderTarget projectionMatrix [16]float32 } @@ -35,7 +36,7 @@ func newContext(screenWidth, screenHeight, screenScale int) *Context { screenHeight: screenHeight, screenScale: screenScale, textures: map[graphics.TextureId]*gtexture.Texture{}, - renderTargets: map[graphics.RenderTargetId]*rendertarget.RenderTarget{}, + renderTargets: map[graphics.RenderTargetId]*grendertarget.RenderTarget{}, renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{}, } return context @@ -48,10 +49,10 @@ func (context *Context) Init() { C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer) var err error - context.mainFramebufferTexture, err = texture.NewRenderTargetWithFramebuffer( + context.mainFramebufferTexture, err = rendertarget.NewRenderTargetWithFramebuffer( context.screenWidth*context.screenScale, context.screenHeight*context.screenScale, - texture.Framebuffer(mainFramebuffer)) + rendertarget.Framebuffer(mainFramebuffer)) if err != nil { panic("creating main framebuffer failed: " + err.Error()) } @@ -123,10 +124,10 @@ func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) { context.setOffscreen(renderTarget) } -func (context *Context) setOffscreen(renderTarget *rendertarget.RenderTarget) { +func (context *Context) setOffscreen(renderTarget *grendertarget.RenderTarget) { C.glFlush() - framebuffer := C.GLuint(renderTarget.Framebuffer().(texture.Framebuffer)) + framebuffer := C.GLuint(renderTarget.Framebuffer().(rendertarget.Framebuffer)) C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer) err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) if err != C.GL_FRAMEBUFFER_COMPLETE { @@ -179,7 +180,7 @@ func (context *Context) flush() { func (context *Context) NewRenderTarget(width, height int) ( graphics.RenderTargetId, error) { - renderTarget, err := texture.NewRenderTarget(width, height) + renderTarget, err := rendertarget.NewRenderTarget(width, height) if err != nil { return 0, nil } diff --git a/graphics/opengl/rendertarget/render_target.go b/graphics/opengl/rendertarget/render_target.go new file mode 100644 index 000000000..3b129f17e --- /dev/null +++ b/graphics/opengl/rendertarget/render_target.go @@ -0,0 +1,47 @@ +package rendertarget + +// #cgo LDFLAGS: -framework OpenGL +// +// #include +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) + C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer) + C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0, + C.GL_TEXTURE_2D, nativeTexture, 0) + C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer)) + if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) != + C.GL_FRAMEBUFFER_COMPLETE { + panic("creating framebuffer failed") + } + + return framebuffer +} + +func NewRenderTarget(width, height int) (*rendertarget.RenderTarget, error) { + tex, err := texture.New(width, height) + if err != nil { + return nil, err + } + framebuffer := createFramebuffer(C.GLuint(tex.Native().(texture.Native))) + return rendertarget.NewWithFramebuffer(tex, Framebuffer(framebuffer)), nil +} + +func NewRenderTargetWithFramebuffer(width, height int, framebuffer Framebuffer) (*rendertarget.RenderTarget, error) { + tex, err := texture.New(width, height) + if err != nil { + return nil, err + } + return rendertarget.NewWithFramebuffer(tex, framebuffer), nil +} diff --git a/graphics/opengl/shader/program.go b/graphics/opengl/shader/program.go index 229c581aa..0e6450abc 100644 --- a/graphics/opengl/shader/program.go +++ b/graphics/opengl/shader/program.go @@ -80,7 +80,7 @@ func getLocation(program C.GLuint, name string, qualifierVariableType int) C.GLi defer C.free(unsafe.Pointer(locationName)) location := C.GLint(-1) - + switch qualifierVariableType { case qualifierVariableTypeAttribute: location = C.glGetAttribLocation(program, (*C.GLchar)(locationName)) @@ -111,7 +111,7 @@ func use(projectionMatrix [16]float32, geometryMatrix matrix.Geometry, colorMatr program = programColorMatrix } C.glUseProgram(program) - + C.glUniformMatrix4fv(C.GLint(getUniformLocation(program, "projection_matrix")), 1, C.GL_FALSE, (*C.GLfloat)(&projectionMatrix[0])) diff --git a/graphics/opengl/texture/texture.go b/graphics/opengl/texture/texture.go index 573e8a610..6e5f67324 100644 --- a/graphics/opengl/texture/texture.go +++ b/graphics/opengl/texture/texture.go @@ -5,7 +5,6 @@ package texture // #include import "C" import ( - "github.com/hajimehoshi/go-ebiten/graphics/rendertarget" gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture" "image" "unsafe" @@ -47,41 +46,8 @@ func createFromImage(img *image.NRGBA) (interface{}, error) { return createNativeTexture(size.X, size.Y, img.Pix), nil } -type Framebuffer C.GLuint - -func NewRenderTarget(width, height int) (*rendertarget.RenderTarget, error) { - texture, err := gtexture.New(width, height, create) - if err != nil { - return nil, err - } - framebuffer := createFramebuffer(C.GLuint(texture.Native().(Native))) - return rendertarget.NewWithFramebuffer(texture, Framebuffer(framebuffer)), nil -} - -func NewRenderTargetWithFramebuffer(width, height int, framebuffer Framebuffer) (*rendertarget.RenderTarget, error) { - texture, err := gtexture.New(width, height, create) - if err != nil { - return nil, err - } - return rendertarget.NewWithFramebuffer(texture, framebuffer), nil -} - -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) - C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0, - C.GL_TEXTURE_2D, nativeTexture, 0) - C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer)) - if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) != - C.GL_FRAMEBUFFER_COMPLETE { - panic("creating framebuffer failed") - } - - return framebuffer +func New(width, height int) (*gtexture.Texture, error) { + return gtexture.New(width, height, create) } func NewFromImage(img image.Image) (*gtexture.Texture, error) {