Make RenderTarget's fields unexported

This commit is contained in:
Hajime Hoshi 2014-01-08 16:38:03 +09:00
parent 2ed01a6232
commit b60db9c2f6
2 changed files with 42 additions and 28 deletions

View File

@ -6,7 +6,6 @@ package offscreen
// #include <OpenGL/gl.h>
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
}

View File

@ -5,6 +5,7 @@ package rendertarget
// #include <OpenGL/gl.h>
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)
}