From af1e77eced98cc6493b1995f012be6b9e943d03e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 27 Oct 2013 19:27:59 +0900 Subject: [PATCH] Add graphics/opengl/texture --- graphics/opengl/context.go | 27 ++++++++++++----------- graphics/opengl/shader/program.go | 7 +++--- graphics/opengl/{ => texture}/texture.go | 28 +++++++++++++++--------- 3 files changed, 35 insertions(+), 27 deletions(-) rename graphics/opengl/{ => texture}/texture.go (72%) diff --git a/graphics/opengl/context.go b/graphics/opengl/context.go index 1edb872e1..711c5bac2 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/shader" "github.com/hajimehoshi/go-ebiten/graphics/rendertarget" - "github.com/hajimehoshi/go-ebiten/graphics/texture" + gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture" "image" "math" ) @@ -21,7 +22,7 @@ type Context struct { screenWidth int screenHeight int screenScale int - textures map[graphics.TextureId]*texture.Texture + textures map[graphics.TextureId]*gtexture.Texture renderTargets map[graphics.RenderTargetId]*rendertarget.RenderTarget renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId mainFramebufferTexture *rendertarget.RenderTarget @@ -33,7 +34,7 @@ func newContext(screenWidth, screenHeight, screenScale int) *Context { screenWidth: screenWidth, screenHeight: screenHeight, screenScale: screenScale, - textures: map[graphics.TextureId]*texture.Texture{}, + textures: map[graphics.TextureId]*gtexture.Texture{}, renderTargets: map[graphics.RenderTargetId]*rendertarget.RenderTarget{}, renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{}, } @@ -47,10 +48,10 @@ func (context *Context) Init() { C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer) var err error - context.mainFramebufferTexture, err = newRenderTargetWithFramebuffer( + context.mainFramebufferTexture, err = texture.NewRenderTargetWithFramebuffer( context.screenWidth*context.screenScale, context.screenHeight*context.screenScale, - C.GLuint(mainFramebuffer)) + texture.Framebuffer(mainFramebuffer)) if err != nil { panic("creating main framebuffer failed: " + err.Error()) } @@ -61,7 +62,7 @@ func (context *Context) Init() { panic("initializing the offscreen failed: " + err.Error()) } screen := context.renderTargets[context.screenId] - C.glBindTexture(C.GL_TEXTURE_2D, screen.Texture().Native().(C.GLuint)) + C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(screen.Texture().Native().(texture.Native))) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_NEAREST) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST) C.glBindTexture(C.GL_TEXTURE_2D, 0) @@ -92,8 +93,8 @@ func (context *Context) DrawTexture( if !ok { panic("invalid texture ID") } - tex.Draw(func(native interface{}, quads []texture.Quad) { - shader.DrawTexture(shader.Texture(native.(C.GLuint)), + tex.Draw(func(native interface{}, quads []gtexture.Quad) { + shader.DrawTexture(native.(texture.Native), context.projectionMatrix, quads, geometryMatrix, colorMatrix) }) @@ -106,8 +107,8 @@ func (context *Context) DrawTextureParts( if !ok { panic("invalid texture ID") } - tex.DrawParts(parts, func(native interface{}, quads []texture.Quad) { - shader.DrawTexture(shader.Texture(native.(C.GLuint)), + tex.DrawParts(parts, func(native interface{}, quads []gtexture.Quad) { + shader.DrawTexture(native.(texture.Native), context.projectionMatrix, quads, geometryMatrix, colorMatrix) }) @@ -125,7 +126,7 @@ func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) { func (context *Context) setOffscreen(renderTarget *rendertarget.RenderTarget) { C.glFlush() - framebuffer := renderTarget.Framebuffer().(C.GLuint) + framebuffer := C.GLuint(renderTarget.Framebuffer().(texture.Framebuffer)) C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer) err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) if err != C.GL_FRAMEBUFFER_COMPLETE { @@ -178,7 +179,7 @@ func (context *Context) flush() { func (context *Context) NewRenderTarget(width, height int) ( graphics.RenderTargetId, error) { - renderTarget, err := newRenderTarget(width, height) + renderTarget, err := texture.NewRenderTarget(width, height) if err != nil { return 0, nil } @@ -198,7 +199,7 @@ func (context *Context) NewRenderTarget(width, height int) ( func (context *Context) NewTextureFromImage(img image.Image) ( graphics.TextureId, error) { - texture, err := texture.NewFromImage(img, createFromImage) + texture, err := texture.NewFromImage(img) if err != nil { return 0, err } diff --git a/graphics/opengl/shader/program.go b/graphics/opengl/shader/program.go index 8edb00fad..229c581aa 100644 --- a/graphics/opengl/shader/program.go +++ b/graphics/opengl/shader/program.go @@ -7,7 +7,8 @@ package shader import "C" import ( "github.com/hajimehoshi/go-ebiten/graphics/matrix" - "github.com/hajimehoshi/go-ebiten/graphics/texture" + "github.com/hajimehoshi/go-ebiten/graphics/opengl/texture" + gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture" "unsafe" ) @@ -161,9 +162,7 @@ func use(projectionMatrix [16]float32, geometryMatrix matrix.Geometry, colorMatr return program } -type Texture C.GLuint - -func DrawTexture(native Texture, projectionMatrix [16]float32, quads []texture.Quad, +func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []gtexture.Quad, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { if !initialized { initialize() diff --git a/graphics/opengl/texture.go b/graphics/opengl/texture/texture.go similarity index 72% rename from graphics/opengl/texture.go rename to graphics/opengl/texture/texture.go index 03d26c55d..573e8a610 100644 --- a/graphics/opengl/texture.go +++ b/graphics/opengl/texture/texture.go @@ -1,4 +1,4 @@ -package opengl +package texture // #cgo LDFLAGS: -framework OpenGL // @@ -6,12 +6,14 @@ package opengl import "C" import ( "github.com/hajimehoshi/go-ebiten/graphics/rendertarget" - "github.com/hajimehoshi/go-ebiten/graphics/texture" + gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture" "image" "unsafe" ) -func createNativeTexture(textureWidth, textureHeight int, pixels []uint8) C.GLuint { +type Native C.GLuint + +func createNativeTexture(textureWidth, textureHeight int, pixels []uint8) Native { nativeTexture := C.GLuint(0) C.glGenTextures(1, (*C.GLuint)(&nativeTexture)) @@ -33,7 +35,7 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8) C.GLui C.GLsizei(textureWidth), C.GLsizei(textureHeight), 0, C.GL_RGBA, C.GL_UNSIGNED_BYTE, ptr) - return nativeTexture + return Native(nativeTexture) } func create(textureWidth, textureHeight int) (interface{}, error) { @@ -45,17 +47,19 @@ func createFromImage(img *image.NRGBA) (interface{}, error) { return createNativeTexture(size.X, size.Y, img.Pix), nil } -func newRenderTarget(width, height int) (*rendertarget.RenderTarget, error) { - texture, err := texture.New(width, height, create) +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(texture.Native().(C.GLuint)) - return rendertarget.NewWithFramebuffer(texture, framebuffer), nil + framebuffer := createFramebuffer(C.GLuint(texture.Native().(Native))) + return rendertarget.NewWithFramebuffer(texture, Framebuffer(framebuffer)), nil } -func newRenderTargetWithFramebuffer(width, height int, framebuffer C.GLuint) (*rendertarget.RenderTarget, error) { - texture, err := texture.New(width, height, create) +func NewRenderTargetWithFramebuffer(width, height int, framebuffer Framebuffer) (*rendertarget.RenderTarget, error) { + texture, err := gtexture.New(width, height, create) if err != nil { return nil, err } @@ -79,3 +83,7 @@ func createFramebuffer(nativeTexture C.GLuint) C.GLuint { return framebuffer } + +func NewFromImage(img image.Image) (*gtexture.Texture, error) { + return gtexture.NewFromImage(img, createFromImage) +}