Refactoring

This commit is contained in:
Hajime Hoshi 2013-10-17 11:21:57 +09:00
parent c8011ffe88
commit cb25b6a9e4
3 changed files with 23 additions and 25 deletions

View File

@ -150,10 +150,6 @@ func abs(x int) int {
func (context *Context) SetOffscreen(renderTargetID graphics.RenderTargetID) { func (context *Context) SetOffscreen(renderTargetID graphics.RenderTargetID) {
renderTarget := context.textures[C.GLuint(renderTargetID)] renderTarget := context.textures[C.GLuint(renderTargetID)]
// TODO: This is a kind of side-effect.
if renderTarget.framebuffer == 0 {
renderTarget.framebuffer = createFramebuffer(renderTarget.id)
}
context.setOffscreen(renderTarget) context.setOffscreen(renderTarget)
} }
@ -271,30 +267,11 @@ func (context *Context) setShaderProgram(
return return
} }
func createFramebuffer(textureID 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, textureID, 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 (context *Context) NewRenderTarget(width, height int) graphics.RenderTargetID { func (context *Context) NewRenderTarget(width, height int) graphics.RenderTargetID {
renderTarget := newRenderTarget(width, height) renderTarget := newRenderTarget(width, height)
context.textures[renderTarget.id] = (*Texture)(renderTarget) context.textures[renderTarget.id] = (*Texture)(renderTarget)
//context.setOffscreen((*Texture)(renderTarget)) context.setOffscreen((*Texture)(renderTarget))
context.SetOffscreen(graphics.RenderTargetID(renderTarget.id))
context.Clear() context.Clear()
context.resetOffscreen() context.resetOffscreen()

View File

@ -43,6 +43,7 @@ func (device *Device) Update(draw func(graphics.Context)) {
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_LINEAR) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_LINEAR)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_LINEAR) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_LINEAR)
// TODO: The name is not good.
context.resetOffscreen() context.resetOffscreen()
context.Clear() context.Clear()

View File

@ -104,7 +104,9 @@ func (err textureError) Error() string {
} }
func newRenderTarget(width, height int) *Texture { func newRenderTarget(width, height int) *Texture {
return createTexture(width, height, nil) texture := createTexture(width, height, nil)
texture.framebuffer = createFramebuffer(texture.id)
return texture
} }
func newTextureFromImage(img image.Image) (*Texture, error) { func newTextureFromImage(img image.Image) (*Texture, error) {
@ -132,3 +134,21 @@ func newRenderTargetWithFramebuffer(width, height int,
framebuffer: framebuffer, framebuffer: framebuffer,
} }
} }
func createFramebuffer(textureID 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, textureID, 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
}