Use (un)signed short vertices (#90)

This commit is contained in:
Hajime Hoshi 2015-01-16 10:37:26 +09:00
parent 155be5a88d
commit 8564ba8541
7 changed files with 50 additions and 45 deletions

View File

@ -48,6 +48,7 @@ func (i *Image) Fill(clr color.Color) (err error) {
i.pixels = nil
r, g, b, a := internal.RGBA(clr)
ui.Use(func(c *opengl.Context) {
// TODO: Change to pass color.Color
err = i.framebuffer.Fill(c, r, g, b, a)
})
return
@ -109,9 +110,8 @@ func (l rectVertexQuads) Len() int {
return l.Rects.Len()
}
func (l rectVertexQuads) Vertex(i int) (x0, y0, x1, y1 float64) {
ix0, iy0, ix1, iy1 := l.Rects.Points(i)
return float64(ix0), float64(iy0), float64(ix1), float64(iy1)
func (l rectVertexQuads) Vertex(i int) (x0, y0, x1, y1 int) {
return l.Rects.Points(i)
}
func (l rectVertexQuads) Color(i int) color.Color {

View File

@ -17,6 +17,7 @@ package ebiten
import (
"github.com/hajimehoshi/ebiten/internal"
"image"
"math"
)
// Deprecated (as of 1.1.0-alpha): Use ImageParts instead.
@ -66,12 +67,12 @@ func (w *wholeImage) Src(i int) (x0, y0, x1, y1 int) {
return 0, 0, w.width, w.height
}
func u(x float64, width int) float64 {
return x / float64(internal.NextPowerOf2Int(width))
func u(x int, width int) int {
return math.MaxInt16 * x / internal.NextPowerOf2Int(width)
}
func v(y float64, height int) float64 {
return y / float64(internal.NextPowerOf2Int(height))
func v(y int, height int) int {
return math.MaxInt16 * y / internal.NextPowerOf2Int(height)
}
type textureQuads struct {
@ -84,13 +85,12 @@ func (t *textureQuads) Len() int {
return t.parts.Len()
}
func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 float64) {
ix0, iy0, ix1, iy1 := t.parts.Dst(i)
return float64(ix0), float64(iy0), float64(ix1), float64(iy1)
func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 int) {
return t.parts.Dst(i)
}
func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 float64) {
func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 int) {
x0, y0, x1, y1 := t.parts.Src(i)
w, h := t.width, t.height
return u(float64(x0), w), v(float64(y0), h), u(float64(x1), w), v(float64(y1), h)
return u(x0, w), v(y0, h), u(x1, w), v(y1, h)
}

View File

@ -95,8 +95,8 @@ type Matrix interface {
type TextureQuads interface {
Len() int
Vertex(i int) (x0, y0, x1, y1 float64)
Texture(i int) (u0, v0, u1, v1 float64)
Vertex(i int) (x0, y0, x1, y1 int)
Texture(i int) (u0, v0, u1, v1 int)
}
func (f *Framebuffer) Fill(c *opengl.Context, r, g, b, a float64) error {
@ -116,7 +116,7 @@ func (f *Framebuffer) DrawTexture(c *opengl.Context, t *Texture, quads TextureQu
type VertexQuads interface {
Len() int
Vertex(i int) (x0, y0, x1, y1 float64)
Vertex(i int) (x0, y0, x1, y1 int)
Color(i int) color.Color
}

View File

@ -17,7 +17,6 @@ package shader
import (
"errors"
"fmt"
"github.com/hajimehoshi/ebiten/internal"
"github.com/hajimehoshi/ebiten/internal/opengl"
"image/color"
)
@ -37,11 +36,11 @@ type Matrix interface {
type TextureQuads interface {
Len() int
Vertex(i int) (x0, y0, x1, y1 float64)
Texture(i int) (u0, v0, u1, v1 float64)
Vertex(i int) (x0, y0, x1, y1 int)
Texture(i int) (u0, v0, u1, v1 int)
}
var vertices = make([]float32, 0, 4*8*quadsMaxNum)
var vertices = make([]int16, 0, 4*8*quadsMaxNum)
var initialized = false
@ -75,10 +74,10 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
continue
}
vertices = append(vertices,
float32(x0), float32(y0), float32(u0), float32(v0),
float32(x1), float32(y0), float32(u1), float32(v0),
float32(x0), float32(y1), float32(u0), float32(v1),
float32(x1), float32(y1), float32(u1), float32(v1),
int16(x0), int16(y0), int16(u0), int16(v0),
int16(x1), int16(y0), int16(u1), int16(v0),
int16(x0), int16(y1), int16(u0), int16(v1),
int16(x1), int16(y1), int16(u1), int16(v1),
)
num++
}
@ -92,7 +91,7 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
type VertexQuads interface {
Len() int
Vertex(i int) (x0, y0, x1, y1 float64)
Vertex(i int) (x0, y0, x1, y1 int)
Color(i int) color.Color
}
@ -125,12 +124,12 @@ func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, quads VertexQ
if x0 == x1 || y0 == y1 {
continue
}
r, g, b, a := internal.RGBA(quads.Color(i))
r, g, b, a := quads.Color(i).RGBA()
vertices = append(vertices,
float32(x0), float32(y0), float32(r), float32(g), float32(b), float32(a),
float32(x1), float32(y0), float32(r), float32(g), float32(b), float32(a),
float32(x0), float32(y1), float32(r), float32(g), float32(b), float32(a),
float32(x1), float32(y1), float32(r), float32(g), float32(b), float32(a),
int16(x0), int16(y0), int16(r), int16(g), int16(b), int16(a),
int16(x1), int16(y0), int16(r), int16(g), int16(b), int16(a),
int16(x0), int16(y1), int16(r), int16(g), int16(b), int16(a),
int16(x1), int16(y1), int16(r), int16(g), int16(b), int16(a),
)
num++
}

View File

@ -26,11 +26,9 @@ var (
const quadsMaxNum = 65536 / 6
// unsafe.SizeOf can't be used because unsafe doesn't work with GopherJS.
const float32Size = 4
const int16Size = 2
func initialize(c *opengl.Context) error {
const uint16Size = 2
shaderVertexNative, err := c.NewShader(c.VertexShader, shader(c, shaderVertex))
if err != nil {
return err
@ -71,7 +69,7 @@ func initialize(c *opengl.Context) error {
return err
}
const stride = float32Size * 8 // 8 = (2 for vertex) + (2 for texture) + (4 for color)
const stride = int16Size * 8 // 8 = (2 for vertex) + (2 for texture) + (4 for color)
c.NewBuffer(c.ArrayBuffer, 4*stride*quadsMaxNum, c.DynamicDraw)
indices := make([]uint16, 6*quadsMaxNum)
@ -146,8 +144,8 @@ func useProgramTexture(c *opengl.Context, projectionMatrix []float32, texture op
c.EnableVertexAttribArray(program, "vertex")
c.EnableVertexAttribArray(program, "tex_coord")
c.VertexAttribPointer(program, "vertex", float32Size*4, 2, uintptr(float32Size*0))
c.VertexAttribPointer(program, "tex_coord", float32Size*4, 2, uintptr(float32Size*2))
c.VertexAttribPointer(program, "vertex", true, false, int16Size*4, 2, uintptr(int16Size*0))
c.VertexAttribPointer(program, "tex_coord", true, true, int16Size*4, 2, uintptr(int16Size*2))
return func() {
c.DisableVertexAttribArray(program, "tex_coord")
@ -167,8 +165,8 @@ func useProgramRect(c *opengl.Context, projectionMatrix []float32) programFinish
c.EnableVertexAttribArray(program, "vertex")
c.EnableVertexAttribArray(program, "color")
c.VertexAttribPointer(program, "vertex", float32Size*6, 2, uintptr(float32Size*0))
c.VertexAttribPointer(program, "color", float32Size*6, 4, uintptr(float32Size*2))
c.VertexAttribPointer(program, "vertex", true, false, int16Size*6, 2, uintptr(int16Size*0))
c.VertexAttribPointer(program, "color", false, true, int16Size*6, 4, uintptr(int16Size*2))
return func() {
c.DisableVertexAttribArray(program, "color")

View File

@ -206,9 +206,13 @@ func (c *Context) GetAttribLocation(p Program, location string) AttribLocation {
return AttribLocation(gl.Program(p).GetAttribLocation(location))
}
func (c *Context) VertexAttribPointer(p Program, location string, stride int, size int, v uintptr) {
func (c *Context) VertexAttribPointer(p Program, location string, signed bool, normalize bool, stride int, size int, v uintptr) {
l := gl.AttribLocation(GetAttribLocation(c, p, location))
l.AttribPointer(uint(size), gl.FLOAT, false, stride, v)
t := gl.GLenum(gl.SHORT)
if !signed {
t = gl.UNSIGNED_SHORT
}
l.AttribPointer(uint(size), t, normalize, stride, v)
}
func (c *Context) EnableVertexAttribArray(p Program, location string) {
@ -239,9 +243,9 @@ func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsageTyp
gl.BufferData(gl.GLenum(bufferType), size, ptr, gl.GLenum(bufferUsageType))
}
func (c *Context) BufferSubData(bufferType BufferType, data []float32) {
const float32Size = 4
gl.BufferSubData(gl.GLenum(bufferType), 0, float32Size*len(data), data)
func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
const int16Size = 2
gl.BufferSubData(gl.GLenum(bufferType), 0, int16Size*len(data), data)
}
func (c *Context) DrawElements(len int) {

View File

@ -256,10 +256,14 @@ func (c *Context) GetAttribLocation(p Program, location string) AttribLocation {
return AttribLocation(gl.GetAttribLocation(p.Object, location))
}
func (c *Context) VertexAttribPointer(p Program, location string, stride int, size int, v uintptr) {
func (c *Context) VertexAttribPointer(p Program, location string, signed bool, normalize bool, stride int, size int, v uintptr) {
gl := c.gl
l := GetAttribLocation(c, p, location)
gl.VertexAttribPointer(int(l), size, gl.FLOAT, false, stride, int(v))
t := gl.SHORT
if !signed {
t = gl.UNSIGNED_SHORT
}
gl.VertexAttribPointer(int(l), size, t, normalize, stride, int(v))
}
func (c *Context) EnableVertexAttribArray(p Program, location string) {
@ -281,7 +285,7 @@ func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsageTyp
gl.BufferData(int(bufferType), v, int(bufferUsageType))
}
func (c *Context) BufferSubData(bufferType BufferType, data []float32) {
func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
gl := c.gl
gl.BufferSubData(int(bufferType), 0, data)
}