From b60db9c2f695b183849a01745cae86175b3106dc Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 8 Jan 2014 16:38:03 +0900 Subject: [PATCH] Make RenderTarget's fields unexported --- graphics/opengl/offscreen/offscreen.go | 40 ++++++++----------- graphics/opengl/rendertarget/render_target.go | 30 ++++++++++++-- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/graphics/opengl/offscreen/offscreen.go b/graphics/opengl/offscreen/offscreen.go index 4ac9301b0..0eb44403d 100644 --- a/graphics/opengl/offscreen/offscreen.go +++ b/graphics/opengl/offscreen/offscreen.go @@ -6,7 +6,6 @@ package offscreen // #include import "C" import ( - "fmt" "github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics/matrix" "github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget" @@ -17,8 +16,8 @@ import ( type Offscreen struct { screenHeight int screenScale int + currentRenderTarget *rendertarget.RenderTarget mainFramebufferTexture *rendertarget.RenderTarget - projectionMatrix [16]float32 } func New(screenWidth, screenHeight, screenScale int) *Offscreen { @@ -38,15 +37,15 @@ func New(screenWidth, screenHeight, screenScale int) *Offscreen { if err != nil { panic("creating main framebuffer failed: " + err.Error()) } + offscreen.currentRenderTarget = offscreen.mainFramebufferTexture return offscreen } func (o *Offscreen) Set(rt *rendertarget.RenderTarget) { C.glFlush() - // TODO: Calc x, y, width, heigth at another function - o.doSet(rt.Framebuffer, 0, 0, - graphics.AdjustSizeForTexture(rt.Width), graphics.AdjustSizeForTexture(rt.Height)) + o.currentRenderTarget = rt + rt.SetAsViewport() } func (o *Offscreen) SetMainFramebuffer() { @@ -55,45 +54,38 @@ func (o *Offscreen) SetMainFramebuffer() { func (o *Offscreen) DrawTexture(texture *texture.Texture, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { + projectionMatrix := o.projectionMatrix() quad := graphics.TextureQuadForTexture(texture.Width, texture.Height) shader.DrawTexture(texture.Native, - o.projectionMatrix, []graphics.TextureQuad{quad}, + projectionMatrix, []graphics.TextureQuad{quad}, geometryMatrix, colorMatrix) } func (o *Offscreen) DrawTextureParts(texture *texture.Texture, parts []graphics.TexturePart, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { + projectionMatrix := o.projectionMatrix() quads := graphics.TextureQuadsForTextureParts(parts, texture.Width, texture.Height) shader.DrawTexture(texture.Native, - o.projectionMatrix, quads, + projectionMatrix, quads, geometryMatrix, colorMatrix) } -func (o *Offscreen) doSet(framebuffer rendertarget.Framebuffer, x, y, width, height int) { - C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(framebuffer)) - err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) - if err != C.GL_FRAMEBUFFER_COMPLETE { - panic(fmt.Sprintf("glBindFramebuffer failed: %d", err)) - } - - C.glBlendFuncSeparate(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA, - C.GL_ZERO, C.GL_ONE) - - C.glViewport(C.GLint(x), C.GLint(y), - C.GLsizei(width), C.GLsizei(height)) - - matrix := graphics.OrthoProjectionMatrix(x, width, y, height) - if framebuffer == o.mainFramebufferTexture.Framebuffer { +func (o *Offscreen) projectionMatrix() [16]float32 { + matrix := o.currentRenderTarget.ProjectionMatrix() + if o.currentRenderTarget == o.mainFramebufferTexture { actualScreenHeight := o.screenHeight * o.screenScale // Flip Y and move to fit with the top of the window. matrix[1][1] *= -1 - matrix[1][3] += float64(actualScreenHeight) / float64(height) * 2 + matrix[1][3] += float64(actualScreenHeight) / + float64(graphics.AdjustSizeForTexture(actualScreenHeight)) * 2 } + projectionMatrix := [16]float32{} for j := 0; j < 4; j++ { for i := 0; i < 4; i++ { - o.projectionMatrix[i+j*4] = float32(matrix[i][j]) + projectionMatrix[i+j*4] = float32(matrix[i][j]) } } + return projectionMatrix } diff --git a/graphics/opengl/rendertarget/render_target.go b/graphics/opengl/rendertarget/render_target.go index 8cc67009d..b477a86f5 100644 --- a/graphics/opengl/rendertarget/render_target.go +++ b/graphics/opengl/rendertarget/render_target.go @@ -5,6 +5,7 @@ package rendertarget // #include import "C" import ( + "fmt" "github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics/opengl/texture" ) @@ -12,9 +13,9 @@ import ( type Framebuffer C.GLuint type RenderTarget struct { - Framebuffer - Width int - Height int + framebuffer Framebuffer + width int + height int } func createFramebuffer(nativeTexture C.GLuint) Framebuffer { @@ -57,7 +58,28 @@ func CreateWithFramebuffer(width, height int, framebuffer Framebuffer) ( return &RenderTarget{framebuffer, width, height}, nil } +func (r *RenderTarget) SetAsViewport() { + C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(r.framebuffer)) + err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) + if err != C.GL_FRAMEBUFFER_COMPLETE { + panic(fmt.Sprintf("glBindFramebuffer failed: %d", err)) + } + + C.glBlendFuncSeparate(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA, + C.GL_ZERO, C.GL_ONE) + + width := graphics.AdjustSizeForTexture(r.width) + height := graphics.AdjustSizeForTexture(r.height) + C.glViewport(0, 0, C.GLsizei(width), C.GLsizei(height)) +} + +func (r *RenderTarget) ProjectionMatrix() [4][4]float64 { + width := graphics.AdjustSizeForTexture(r.width) + height := graphics.AdjustSizeForTexture(r.height) + return graphics.OrthoProjectionMatrix(0, width, 0, height) +} + func (r *RenderTarget) Dispose() { - f := C.GLuint(r.Framebuffer) + f := C.GLuint(r.framebuffer) C.glDeleteFramebuffers(1, &f) }