mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 11:12:44 +01:00
graphics: Refactoring
This commit is contained in:
parent
8fbe423125
commit
37d8bd312a
@ -246,14 +246,7 @@ func (c *drawImageCommand) Exec(indexOffsetInBytes int) error {
|
|||||||
}
|
}
|
||||||
_, h := c.dst.Size()
|
_, h := c.dst.Size()
|
||||||
proj := f.projectionMatrix(h)
|
proj := f.projectionMatrix(h)
|
||||||
p := &programContext{
|
if err := theOpenGLState.useProgram(proj, c.src.texture.native, c.color); err != nil {
|
||||||
state: &theOpenGLState,
|
|
||||||
program: theOpenGLState.programTexture,
|
|
||||||
projectionMatrix: proj,
|
|
||||||
texture: c.src.texture.native,
|
|
||||||
colorM: c.color,
|
|
||||||
}
|
|
||||||
if err := p.begin(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// TODO: We should call glBindBuffer here?
|
// TODO: We should call glBindBuffer here?
|
||||||
|
@ -205,6 +205,7 @@ func (s *openGLState) reset() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// areSameFloat32Array returns a boolean indicating if a and b are deeply equal.
|
||||||
func areSameFloat32Array(a, b []float32) bool {
|
func areSameFloat32Array(a, b []float32) bool {
|
||||||
if len(a) != len(b) {
|
if len(a) != len(b) {
|
||||||
return false
|
return false
|
||||||
@ -217,41 +218,36 @@ func areSameFloat32Array(a, b []float32) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
type programContext struct {
|
// useProgram uses the program (programTexture).
|
||||||
state *openGLState
|
func (s *openGLState) useProgram(proj []float32, texture opengl.Texture, colorM affine.ColorM) error {
|
||||||
program opengl.Program
|
|
||||||
projectionMatrix []float32
|
|
||||||
texture opengl.Texture
|
|
||||||
colorM affine.ColorM
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *programContext) begin() error {
|
|
||||||
c := opengl.GetContext()
|
c := opengl.GetContext()
|
||||||
if p.state.lastProgram != p.program {
|
program := s.programTexture
|
||||||
c.UseProgram(p.program)
|
|
||||||
if p.state.lastProgram != zeroProgram {
|
|
||||||
theArrayBufferLayout.disable(p.state.lastProgram)
|
|
||||||
}
|
|
||||||
theArrayBufferLayout.enable(p.program)
|
|
||||||
|
|
||||||
p.state.lastProgram = p.state.programTexture
|
if s.lastProgram != program {
|
||||||
p.state.lastProjectionMatrix = nil
|
c.UseProgram(program)
|
||||||
p.state.lastColorMatrix = nil
|
if s.lastProgram != zeroProgram {
|
||||||
p.state.lastColorMatrixTranslation = nil
|
theArrayBufferLayout.disable(s.lastProgram)
|
||||||
c.BindElementArrayBuffer(p.state.elementArrayBuffer)
|
}
|
||||||
c.UniformInt(p.program, "texture", 0)
|
theArrayBufferLayout.enable(program)
|
||||||
|
|
||||||
|
s.lastProgram = s.programTexture
|
||||||
|
s.lastProjectionMatrix = nil
|
||||||
|
s.lastColorMatrix = nil
|
||||||
|
s.lastColorMatrixTranslation = nil
|
||||||
|
c.BindElementArrayBuffer(s.elementArrayBuffer)
|
||||||
|
c.UniformInt(program, "texture", 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !areSameFloat32Array(p.state.lastProjectionMatrix, p.projectionMatrix) {
|
if !areSameFloat32Array(s.lastProjectionMatrix, proj) {
|
||||||
c.UniformFloats(p.program, "projection_matrix", p.projectionMatrix)
|
c.UniformFloats(program, "projection_matrix", proj)
|
||||||
if p.state.lastProjectionMatrix == nil {
|
if s.lastProjectionMatrix == nil {
|
||||||
p.state.lastProjectionMatrix = make([]float32, 16)
|
s.lastProjectionMatrix = make([]float32, 16)
|
||||||
}
|
}
|
||||||
copy(p.state.lastProjectionMatrix, p.projectionMatrix)
|
copy(s.lastProjectionMatrix, proj)
|
||||||
}
|
}
|
||||||
|
|
||||||
e := [4][5]float32{}
|
e := [4][5]float32{}
|
||||||
es := p.colorM.UnsafeElements()
|
es := colorM.UnsafeElements()
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
for j := 0; j < 5; j++ {
|
for j := 0; j < 5; j++ {
|
||||||
e[i][j] = float32(es[i*affine.ColorMDim+j])
|
e[i][j] = float32(es[i*affine.ColorMDim+j])
|
||||||
@ -264,27 +260,27 @@ func (p *programContext) begin() error {
|
|||||||
e[0][2], e[1][2], e[2][2], e[3][2],
|
e[0][2], e[1][2], e[2][2], e[3][2],
|
||||||
e[0][3], e[1][3], e[2][3], e[3][3],
|
e[0][3], e[1][3], e[2][3], e[3][3],
|
||||||
}
|
}
|
||||||
if !areSameFloat32Array(p.state.lastColorMatrix, colorMatrix) {
|
if !areSameFloat32Array(s.lastColorMatrix, colorMatrix) {
|
||||||
c.UniformFloats(p.program, "color_matrix", colorMatrix)
|
c.UniformFloats(program, "color_matrix", colorMatrix)
|
||||||
if p.state.lastColorMatrix == nil {
|
if s.lastColorMatrix == nil {
|
||||||
p.state.lastColorMatrix = make([]float32, 16)
|
s.lastColorMatrix = make([]float32, 16)
|
||||||
}
|
}
|
||||||
copy(p.state.lastColorMatrix, colorMatrix)
|
copy(s.lastColorMatrix, colorMatrix)
|
||||||
}
|
}
|
||||||
colorMatrixTranslation := []float32{
|
colorMatrixTranslation := []float32{
|
||||||
e[0][4], e[1][4], e[2][4], e[3][4],
|
e[0][4], e[1][4], e[2][4], e[3][4],
|
||||||
}
|
}
|
||||||
if !areSameFloat32Array(p.state.lastColorMatrixTranslation, colorMatrixTranslation) {
|
if !areSameFloat32Array(s.lastColorMatrixTranslation, colorMatrixTranslation) {
|
||||||
c.UniformFloats(p.program, "color_matrix_translation", colorMatrixTranslation)
|
c.UniformFloats(program, "color_matrix_translation", colorMatrixTranslation)
|
||||||
if p.state.lastColorMatrixTranslation == nil {
|
if s.lastColorMatrixTranslation == nil {
|
||||||
p.state.lastColorMatrixTranslation = make([]float32, 4)
|
s.lastColorMatrixTranslation = make([]float32, 4)
|
||||||
}
|
}
|
||||||
copy(p.state.lastColorMatrixTranslation, colorMatrixTranslation)
|
copy(s.lastColorMatrixTranslation, colorMatrixTranslation)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture
|
// We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture
|
||||||
// See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml
|
// See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml
|
||||||
if err := c.BindTexture(p.texture); err != nil {
|
if err := c.BindTexture(texture); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user