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

View File

@ -17,6 +17,7 @@ package ebiten
import ( import (
"github.com/hajimehoshi/ebiten/internal" "github.com/hajimehoshi/ebiten/internal"
"image" "image"
"math"
) )
// Deprecated (as of 1.1.0-alpha): Use ImageParts instead. // 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 return 0, 0, w.width, w.height
} }
func u(x float64, width int) float64 { func u(x int, width int) int {
return x / float64(internal.NextPowerOf2Int(width)) return math.MaxInt16 * x / internal.NextPowerOf2Int(width)
} }
func v(y float64, height int) float64 { func v(y int, height int) int {
return y / float64(internal.NextPowerOf2Int(height)) return math.MaxInt16 * y / internal.NextPowerOf2Int(height)
} }
type textureQuads struct { type textureQuads struct {
@ -84,13 +85,12 @@ func (t *textureQuads) Len() int {
return t.parts.Len() return t.parts.Len()
} }
func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 float64) { func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 int) {
ix0, iy0, ix1, iy1 := t.parts.Dst(i) return t.parts.Dst(i)
return float64(ix0), float64(iy0), float64(ix1), float64(iy1)
} }
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) x0, y0, x1, y1 := t.parts.Src(i)
w, h := t.width, t.height 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 { type TextureQuads interface {
Len() int Len() int
Vertex(i int) (x0, y0, x1, y1 float64) Vertex(i int) (x0, y0, x1, y1 int)
Texture(i int) (u0, v0, u1, v1 float64) Texture(i int) (u0, v0, u1, v1 int)
} }
func (f *Framebuffer) Fill(c *opengl.Context, r, g, b, a float64) error { 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 { type VertexQuads interface {
Len() int Len() int
Vertex(i int) (x0, y0, x1, y1 float64) Vertex(i int) (x0, y0, x1, y1 int)
Color(i int) color.Color Color(i int) color.Color
} }

View File

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

View File

@ -26,11 +26,9 @@ var (
const quadsMaxNum = 65536 / 6 const quadsMaxNum = 65536 / 6
// unsafe.SizeOf can't be used because unsafe doesn't work with GopherJS. // 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 { func initialize(c *opengl.Context) error {
const uint16Size = 2
shaderVertexNative, err := c.NewShader(c.VertexShader, shader(c, shaderVertex)) shaderVertexNative, err := c.NewShader(c.VertexShader, shader(c, shaderVertex))
if err != nil { if err != nil {
return err return err
@ -71,7 +69,7 @@ func initialize(c *opengl.Context) error {
return err 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) c.NewBuffer(c.ArrayBuffer, 4*stride*quadsMaxNum, c.DynamicDraw)
indices := make([]uint16, 6*quadsMaxNum) 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, "vertex")
c.EnableVertexAttribArray(program, "tex_coord") c.EnableVertexAttribArray(program, "tex_coord")
c.VertexAttribPointer(program, "vertex", float32Size*4, 2, uintptr(float32Size*0)) c.VertexAttribPointer(program, "vertex", true, false, int16Size*4, 2, uintptr(int16Size*0))
c.VertexAttribPointer(program, "tex_coord", float32Size*4, 2, uintptr(float32Size*2)) c.VertexAttribPointer(program, "tex_coord", true, true, int16Size*4, 2, uintptr(int16Size*2))
return func() { return func() {
c.DisableVertexAttribArray(program, "tex_coord") c.DisableVertexAttribArray(program, "tex_coord")
@ -167,8 +165,8 @@ func useProgramRect(c *opengl.Context, projectionMatrix []float32) programFinish
c.EnableVertexAttribArray(program, "vertex") c.EnableVertexAttribArray(program, "vertex")
c.EnableVertexAttribArray(program, "color") c.EnableVertexAttribArray(program, "color")
c.VertexAttribPointer(program, "vertex", float32Size*6, 2, uintptr(float32Size*0)) c.VertexAttribPointer(program, "vertex", true, false, int16Size*6, 2, uintptr(int16Size*0))
c.VertexAttribPointer(program, "color", float32Size*6, 4, uintptr(float32Size*2)) c.VertexAttribPointer(program, "color", false, true, int16Size*6, 4, uintptr(int16Size*2))
return func() { return func() {
c.DisableVertexAttribArray(program, "color") 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)) 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 := 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) { 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)) gl.BufferData(gl.GLenum(bufferType), size, ptr, gl.GLenum(bufferUsageType))
} }
func (c *Context) BufferSubData(bufferType BufferType, data []float32) { func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
const float32Size = 4 const int16Size = 2
gl.BufferSubData(gl.GLenum(bufferType), 0, float32Size*len(data), data) gl.BufferSubData(gl.GLenum(bufferType), 0, int16Size*len(data), data)
} }
func (c *Context) DrawElements(len int) { 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)) 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 gl := c.gl
l := GetAttribLocation(c, p, location) 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) { 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)) 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 := c.gl
gl.BufferSubData(int(bufferType), 0, data) gl.BufferSubData(int(bufferType), 0, data)
} }