Refactoring: Hide some functions of opengl

This commit is contained in:
Hajime Hoshi 2014-12-30 22:25:38 +09:00
parent 501e51c554
commit 957b4d392f
6 changed files with 66 additions and 81 deletions

View File

@ -72,7 +72,5 @@ func (c *graphicsContext) postUpdate() error {
if err := c.defaultR.drawImage(c.screen, options); err != nil { if err := c.defaultR.drawImage(c.screen, options); err != nil {
return err return err
} }
opengl.Flush()
return nil return nil
} }

View File

@ -17,7 +17,6 @@ package ebiten
import ( import (
"github.com/hajimehoshi/ebiten/internal" "github.com/hajimehoshi/ebiten/internal"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
"github.com/hajimehoshi/ebiten/internal/opengl/internal/shader"
"image" "image"
"image/color" "image/color"
) )
@ -44,12 +43,8 @@ func (i *innerImage) Clear() error {
} }
func (i *innerImage) Fill(clr color.Color) error { func (i *innerImage) Fill(clr color.Color) error {
if err := i.framebuffer.SetAsViewport(); err != nil {
return err
}
r, g, b, a := internal.RGBA(clr) r, g, b, a := internal.RGBA(clr)
opengl.Clear(r, g, b, a) return i.framebuffer.Fill(r, g, b, a)
return nil
} }
func (i *innerImage) drawImage(img *innerImage, options *DrawImageOptions) error { func (i *innerImage) drawImage(img *innerImage, options *DrawImageOptions) error {
@ -68,14 +63,9 @@ func (i *innerImage) drawImage(img *innerImage, options *DrawImageOptions) error
} }
geo := options.GeoM geo := options.GeoM
clr := options.ColorM clr := options.ColorM
w, h := img.size()
if err := i.framebuffer.SetAsViewport(); err != nil { quads := &textureQuads{parts, w, h}
return err return i.framebuffer.DrawTexture(img.texture, quads, geo, clr)
}
w, h := img.texture.Size()
quads := textureQuads(parts, w, h)
projectionMatrix := i.framebuffer.ProjectionMatrix()
return shader.DrawTexture(img.texture.Native(), projectionMatrix, quads, geo, clr)
} }
func u(x float64, width int) float32 { func u(x float64, width int) float32 {
@ -86,22 +76,25 @@ func v(y float64, height int) float32 {
return float32(y) / float32(internal.NextPowerOf2Int(height)) return float32(y) / float32(internal.NextPowerOf2Int(height))
} }
func textureQuads(parts []ImagePart, width, height int) []shader.TextureQuad { type textureQuads struct {
quads := make([]shader.TextureQuad, 0, len(parts)) parts []ImagePart
for _, part := range parts { width int
dst, src := part.Dst, part.Src height int
x1 := float32(dst.Min.X)
x2 := float32(dst.Max.X)
y1 := float32(dst.Min.Y)
y2 := float32(dst.Max.Y)
u1 := u(float64(src.Min.X), width)
u2 := u(float64(src.Max.X), width)
v1 := v(float64(src.Min.Y), height)
v2 := v(float64(src.Max.Y), height)
quad := shader.TextureQuad{x1, x2, y1, y2, u1, u2, v1, v2}
quads = append(quads, quad)
} }
return quads
func (t *textureQuads) Len() int {
return len(t.parts)
}
func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 float32) {
dst := t.parts[i].Dst
return float32(dst.Min.X), float32(dst.Min.Y), float32(dst.Max.X), float32(dst.Max.Y)
}
func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 float32) {
src := t.parts[i].Src
w, h := t.width, t.height
return u(float64(src.Min.X), w), v(float64(src.Min.Y), h), u(float64(src.Max.X), w), v(float64(src.Max.Y), h)
} }
type syncer interface { type syncer interface {

View File

@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"github.com/go-gl/gl" "github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/internal" "github.com/hajimehoshi/ebiten/internal"
"github.com/hajimehoshi/ebiten/internal/opengl/internal/shader"
) )
func orthoProjectionMatrix(left, right, bottom, top int) [4][4]float64 { func orthoProjectionMatrix(left, right, bottom, top int) [4][4]float64 {
@ -84,7 +85,7 @@ func createFramebuffer(nativeTexture gl.Texture) (gl.Framebuffer, error) {
return framebuffer, nil return framebuffer, nil
} }
func (f *Framebuffer) SetAsViewport() error { func (f *Framebuffer) setAsViewport() error {
gl.Flush() gl.Flush()
f.framebuffer.Bind() f.framebuffer.Bind()
err := gl.CheckFramebufferStatus(gl.FRAMEBUFFER) err := gl.CheckFramebufferStatus(gl.FRAMEBUFFER)
@ -101,7 +102,7 @@ func (f *Framebuffer) SetAsViewport() error {
return nil return nil
} }
func (f *Framebuffer) ProjectionMatrix() [4][4]float64 { func (f *Framebuffer) projectionMatrix() [4][4]float64 {
width := internal.NextPowerOf2Int(f.width) width := internal.NextPowerOf2Int(f.width)
height := internal.NextPowerOf2Int(f.height) height := internal.NextPowerOf2Int(f.height)
m := orthoProjectionMatrix(0, width, 0, height) m := orthoProjectionMatrix(0, width, 0, height)
@ -111,3 +112,31 @@ func (f *Framebuffer) ProjectionMatrix() [4][4]float64 {
} }
return m return m
} }
type Matrix interface {
Element(i, j int) float64
}
type TextureQuads interface {
Len() int
Vertex(i int) (x0, y0, x1, y1 float32)
Texture(i int) (u0, v0, u1, v1 float32)
}
func (f *Framebuffer) Fill(r, g, b, a float64) error {
if err := f.setAsViewport(); err != nil {
return err
}
gl.ClearColor(gl.GLclampf(r), gl.GLclampf(g), gl.GLclampf(b), gl.GLclampf(a))
gl.Clear(gl.COLOR_BUFFER_BIT)
return nil
}
func (f *Framebuffer) DrawTexture(t *Texture, quads TextureQuads, geo, clr Matrix) error {
if err := f.setAsViewport(); err != nil {
return err
}
projectionMatrix := f.projectionMatrix()
// TODO: Define texture.Draw()
return shader.DrawTexture(t.native, projectionMatrix, quads, geo, clr)
}

View File

@ -32,12 +32,3 @@ func Init() {
gl.Enable(gl.BLEND) gl.Enable(gl.BLEND)
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
} }
func Clear(r, g, b, a float64) {
gl.ClearColor(gl.GLclampf(r), gl.GLclampf(g), gl.GLclampf(b), gl.GLclampf(a))
gl.Clear(gl.COLOR_BUFFER_BIT)
}
func Flush() {
gl.Flush()
}

View File

@ -32,6 +32,12 @@ type Matrix interface {
Element(i, j int) float64 Element(i, j int) float64
} }
type TextureQuads interface {
Len() int
Vertex(i int) (x0, y0, x1, y1 float32)
Texture(i int) (u0, v0, u1, v1 float32)
}
var initialized = false var initialized = false
const size = 10000 const size = 10000
@ -40,7 +46,7 @@ const size = 10000
const uint16Size = 2 const uint16Size = 2
const short32Size = 4 const short32Size = 4
func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []TextureQuad, geo Matrix, color Matrix) error { func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads TextureQuads, geo Matrix, color Matrix) error {
// TODO: Check len(quads) and gl.MAX_ELEMENTS_INDICES? // TODO: Check len(quads) and gl.MAX_ELEMENTS_INDICES?
const stride = 4 * 4 const stride = 4 * 4
if !initialized { if !initialized {
@ -69,7 +75,7 @@ func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []Text
initialized = true initialized = true
} }
if len(quads) == 0 { if quads.Len() == 0 {
return nil return nil
} }
// TODO: Check performance // TODO: Check performance
@ -92,15 +98,9 @@ func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []Text
texCoordAttrLocation.AttribPointer(2, gl.FLOAT, false, stride, uintptr(short32Size*2)) texCoordAttrLocation.AttribPointer(2, gl.FLOAT, false, stride, uintptr(short32Size*2))
vertices := []float32{} vertices := []float32{}
for _, quad := range quads { for i := 0; i < quads.Len(); i++ {
x0 := quad.VertexX0 x0, y0, x1, y1 := quads.Vertex(i)
x1 := quad.VertexX1 u0, v0, u1, v1 := quads.Texture(i)
y0 := quad.VertexY0
y1 := quad.VertexY1
u0 := quad.TextureCoordU0
u1 := quad.TextureCoordU1
v0 := quad.TextureCoordV0
v1 := quad.TextureCoordV1
vertices = append(vertices, vertices = append(vertices,
x0, y0, u0, v0, x0, y0, u0, v0,
x1, y0, u1, v0, x1, y0, u1, v0,
@ -109,7 +109,7 @@ func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []Text
) )
} }
gl.BufferSubData(gl.ARRAY_BUFFER, 0, short32Size*len(vertices), vertices) gl.BufferSubData(gl.ARRAY_BUFFER, 0, short32Size*len(vertices), vertices)
gl.DrawElements(gl.TRIANGLES, 6*len(quads), gl.UNSIGNED_SHORT, uintptr(0)) gl.DrawElements(gl.TRIANGLES, 6*quads.Len(), gl.UNSIGNED_SHORT, uintptr(0))
gl.Flush() gl.Flush()
return nil return nil

View File

@ -1,26 +0,0 @@
// Copyright 2014 Hajime Hoshi
//
// 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 shader
type TextureQuad struct {
VertexX0 float32
VertexX1 float32
VertexY0 float32
VertexY1 float32
TextureCoordU0 float32
TextureCoordU1 float32
TextureCoordV0 float32
TextureCoordV1 float32
}