graphicsdriver/opengl: Refactoring: Remove framebuffer.projectionMatrix

This commit is contained in:
Hajime Hoshi 2018-11-17 22:29:04 +09:00
parent 14f5a03a79
commit ef08e218c8
5 changed files with 24 additions and 69 deletions

View File

@ -16,11 +16,10 @@ package opengl
// framebuffer is a wrapper of OpenGL's framebuffer.
type framebuffer struct {
driver *Driver
native framebufferNative
proMatrix []float32
width int
height int
driver *Driver
native framebufferNative
width int
height int
}
// newFramebufferFromTexture creates a framebuffer from the given texture.
@ -45,19 +44,6 @@ func newScreenFramebuffer(context *context, width, height int) *framebuffer {
}
}
// projectionMatrix returns a projection matrix of the framebuffer.
//
// A projection matrix converts the coodinates on the framebuffer
// (0, 0) - (viewport width, viewport height)
// to the normalized device coodinates (-1, -1) - (1, 1) with adjustment.
func (f *framebuffer) projectionMatrix() []float32 {
if f.proMatrix != nil {
return f.proMatrix
}
f.proMatrix = orthoProjectionMatrix(0, f.width, 0, f.height)
return f.proMatrix
}
func (f *framebuffer) delete(context *context) {
if f.native != context.getScreenFramebuffer() {
context.deleteFramebuffer(f.native)

View File

@ -63,13 +63,6 @@ func (i *Image) Pixels() ([]byte, error) {
return p, nil
}
func (i *Image) projectionMatrix() []float32 {
if i.framebuffer == nil {
panic("not reached")
}
return i.framebuffer.projectionMatrix()
}
func (i *Image) ensureFramebuffer() error {
if i.framebuffer != nil {
return nil

View File

@ -1,32 +0,0 @@
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package opengl
// orthoProjectionMatrix returns an orthogonal projection matrix for OpenGL.
//
// The matrix converts the coodinates (left, bottom) - (right, top) to the normalized device coodinates (-1, -1) - (1, 1).
func orthoProjectionMatrix(left, right, bottom, top int) []float32 {
e11 := 2 / float32(right-left)
e22 := 2 / float32(top-bottom)
e14 := -1 * float32(right+left) / float32(right-left)
e24 := -1 * float32(top+bottom) / float32(top-bottom)
return []float32{
e11, 0, 0, 0,
0, e22, 0, 0,
0, 0, 1, 0,
e14, e24, 0, 1,
}
}

View File

@ -124,7 +124,7 @@ type openGLState struct {
programScreen program
lastProgram program
lastProjectionMatrix []float32
lastViewportSize []float32
lastColorMatrix []float32
lastColorMatrixTranslation []float32
lastSourceWidth int
@ -151,7 +151,7 @@ func (s *openGLState) reset(context *context) error {
}
s.lastProgram = zeroProgram
s.lastProjectionMatrix = nil
s.lastViewportSize = nil
s.lastColorMatrix = nil
s.lastColorMatrixTranslation = nil
s.lastSourceWidth = 0
@ -266,7 +266,10 @@ func (d *Driver) useProgram(mode graphics.CompositeMode, colorM *affine.ColorM,
if err := destination.setViewport(); err != nil {
return err
}
proj := destination.projectionMatrix()
viewportSize := []float32{
float32(destination.framebuffer.width),
float32(destination.framebuffer.height),
}
dstW := destination.width
srcW, srcH := source.width, source.height
@ -298,21 +301,19 @@ func (d *Driver) useProgram(mode graphics.CompositeMode, colorM *affine.ColorM,
}
d.state.lastProgram = program
d.state.lastProjectionMatrix = nil
d.state.lastViewportSize = nil
d.state.lastColorMatrix = nil
d.state.lastColorMatrixTranslation = nil
d.state.lastSourceWidth = 0
d.state.lastSourceHeight = 0
}
if !areSameFloat32Array(d.state.lastProjectionMatrix, proj) {
d.context.uniformFloats(program, "projection_matrix", proj)
if d.state.lastProjectionMatrix == nil {
d.state.lastProjectionMatrix = make([]float32, 16)
if !areSameFloat32Array(d.state.lastViewportSize, viewportSize) {
d.context.uniformFloats(program, "viewport_size", viewportSize)
if d.state.lastViewportSize == nil {
d.state.lastViewportSize = make([]float32, 2)
}
// (*framebuffer).projectionMatrix is always same for the same framebuffer.
// It's OK to hold the reference without copying.
d.state.lastProjectionMatrix = proj
d.state.lastViewportSize = viewportSize
}
esBody, esTranslate := colorM.UnsafeElements()

View File

@ -47,7 +47,7 @@ func shaderStr(id shaderID) string {
const (
shaderStrVertex = `
uniform mat4 projection_matrix;
uniform vec2 viewport_size;
attribute vec4 vertex;
attribute vec4 tex_coord;
attribute vec4 color_scale;
@ -66,6 +66,13 @@ void main(void) {
varying_tex_coord_max = vec2(1, 1);
}
varying_color_scale = color_scale;
mat4 projection_matrix = mat4(
vec4(2.0 / viewport_size.x, 0, 0, 0),
vec4(0, 2.0 / viewport_size.y, 0, 0),
vec4(0, 0, 1, 0),
vec4(-1, -1, 0, 1)
);
gl_Position = projection_matrix * vertex;
}
`