image: Remove drawing lines and rects (#142)

This commit is contained in:
Hajime Hoshi 2016-02-06 17:57:20 +09:00
parent 1e738f6a10
commit 38552f8d9d
4 changed files with 1 additions and 211 deletions

View File

@ -81,82 +81,3 @@ func drawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
c.DrawElements(c.Triangles, 6*num) c.DrawElements(c.Triangles, 6*num)
return nil return nil
} }
func drawLines(c *opengl.Context, projectionMatrix *[4][4]float64, lines Lines) error {
if !shadersInitialized {
if err := initialize(c); err != nil {
return err
}
shadersInitialized = true
}
if lines.Len() == 0 {
return nil
}
f := useProgramForLines(c, glMatrix(projectionMatrix))
defer f.FinishProgram()
vertices := vertices[0:0]
num := 0
for i := 0; i < lines.Len(); i++ {
x0, y0, x1, y1 := lines.Points(i)
if x0 == x1 && y0 == y1 {
continue
}
r, g, b, a := lines.Color(i).RGBA()
vertices = append(vertices,
int16(x0), int16(y0), int16(r), int16(g), int16(b), int16(a),
int16(x1), int16(y1), int16(r), int16(g), int16(b), int16(a),
)
num++
}
if len(vertices) == 0 {
return nil
}
c.BufferSubData(c.ArrayBuffer, vertices)
c.DrawElements(c.Lines, 2*num)
return nil
}
func drawFilledRects(c *opengl.Context, projectionMatrix *[4][4]float64, rects Rects) error {
if !shadersInitialized {
if err := initialize(c); err != nil {
return err
}
shadersInitialized = true
}
if rects.Len() == 0 {
return nil
}
f := useProgramForRects(c, glMatrix(projectionMatrix))
defer f.FinishProgram()
vertices := vertices[0:0]
num := 0
for i := 0; i < rects.Len(); i++ {
x, y, w, h := rects.Rect(i)
if w == 0 || h == 0 {
continue
}
x0, y0, x1, y1 := x, y, x+w, y+h
r, g, b, a := rects.Color(i).RGBA()
vertices = append(vertices,
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++
}
if len(vertices) == 0 {
return nil
}
c.BufferSubData(c.ArrayBuffer, vertices)
c.DrawElements(c.Triangles, 6*num)
return nil
}

View File

@ -136,22 +136,6 @@ func (f *Framebuffer) DrawTexture(c *opengl.Context, t *Texture, quads TextureQu
return drawTexture(c, t.native, p, quads, geo, clr) return drawTexture(c, t.native, p, quads, geo, clr)
} }
func (f *Framebuffer) DrawLines(c *opengl.Context, lines Lines) error {
if err := f.setAsViewport(c); err != nil {
return err
}
p := f.projectionMatrix()
return drawLines(c, p, lines)
}
func (f *Framebuffer) DrawFilledRects(c *opengl.Context, rects Rects) error {
if err := f.setAsViewport(c); err != nil {
return err
}
p := f.projectionMatrix()
return drawFilledRects(c, p, rects)
}
func (f *Framebuffer) Pixels(c *opengl.Context) ([]uint8, error) { func (f *Framebuffer) Pixels(c *opengl.Context) ([]uint8, error) {
w, h := f.Size() w, h := f.Size()
w, h = NextPowerOf2Int(w), NextPowerOf2Int(h) w, h = NextPowerOf2Int(w), NextPowerOf2Int(h)

View File

@ -25,9 +25,7 @@ var (
) )
var ( var (
programTexture opengl.Program programTexture opengl.Program
programSolidRect opengl.Program
programSolidLine opengl.Program
) )
const indicesNum = math.MaxUint16 + 1 const indicesNum = math.MaxUint16 + 1
@ -44,30 +42,12 @@ func initialize(c *opengl.Context) error {
} }
defer c.DeleteShader(shaderVertexModelviewNative) defer c.DeleteShader(shaderVertexModelviewNative)
shaderVertexColorNative, err := c.NewShader(c.VertexShader, shader(c, shaderVertexColor))
if err != nil {
return err
}
defer c.DeleteShader(shaderVertexColorNative)
shaderVertexColorLineNative, err := c.NewShader(c.VertexShader, shader(c, shaderVertexColorLine))
if err != nil {
return err
}
defer c.DeleteShader(shaderVertexColorLineNative)
shaderFragmentTextureNative, err := c.NewShader(c.FragmentShader, shader(c, shaderFragmentTexture)) shaderFragmentTextureNative, err := c.NewShader(c.FragmentShader, shader(c, shaderFragmentTexture))
if err != nil { if err != nil {
return err return err
} }
defer c.DeleteShader(shaderFragmentTextureNative) defer c.DeleteShader(shaderFragmentTextureNative)
shaderFragmentSolidNative, err := c.NewShader(c.FragmentShader, shader(c, shaderFragmentSolid))
if err != nil {
return err
}
defer c.DeleteShader(shaderFragmentSolidNative)
programTexture, err = c.NewProgram([]opengl.Shader{ programTexture, err = c.NewProgram([]opengl.Shader{
shaderVertexModelviewNative, shaderVertexModelviewNative,
shaderFragmentTextureNative, shaderFragmentTextureNative,
@ -76,22 +56,6 @@ func initialize(c *opengl.Context) error {
return err return err
} }
programSolidRect, err = c.NewProgram([]opengl.Shader{
shaderVertexColorNative,
shaderFragmentSolidNative,
})
if err != nil {
return err
}
programSolidLine, err = c.NewProgram([]opengl.Shader{
shaderVertexColorLineNative,
shaderFragmentSolidNative,
})
if err != nil {
return err
}
// 16 [bytse] is an arbitrary number which seems enough to draw anything. Fix this if necessary. // 16 [bytse] is an arbitrary number which seems enough to draw anything. Fix this if necessary.
const stride = 16 const stride = 16
c.NewBuffer(c.ArrayBuffer, 4*stride*quadsMaxNum, c.DynamicDraw) c.NewBuffer(c.ArrayBuffer, 4*stride*quadsMaxNum, c.DynamicDraw)
@ -223,50 +187,3 @@ func useProgramForTexture(c *opengl.Context, projectionMatrix []float32, texture
c.DisableVertexAttribArray(program, "vertex") c.DisableVertexAttribArray(program, "vertex")
} }
} }
func useProgramForLines(c *opengl.Context, projectionMatrix []float32) programFinisher {
if !lastProgram.Equals(programSolidLine) {
c.UseProgram(programSolidLine)
lastProgram = programSolidLine
}
program := programSolidLine
c.BindElementArrayBuffer(indexBufferLines)
c.UniformFloats(program, "projection_matrix", projectionMatrix)
c.EnableVertexAttribArray(program, "vertex")
c.EnableVertexAttribArray(program, "color")
// TODO: Change to floats?
c.VertexAttribPointer(program, "vertex", true, false, int16Size*6, 2, int16Size*0)
c.VertexAttribPointer(program, "color", false, true, int16Size*6, 4, int16Size*2)
return func() {
c.DisableVertexAttribArray(program, "color")
c.DisableVertexAttribArray(program, "vertex")
}
}
func useProgramForRects(c *opengl.Context, projectionMatrix []float32) programFinisher {
if !lastProgram.Equals(programSolidRect) {
c.UseProgram(programSolidRect)
lastProgram = programSolidRect
}
program := programSolidRect
c.BindElementArrayBuffer(indexBufferQuads)
c.UniformFloats(program, "projection_matrix", projectionMatrix)
c.EnableVertexAttribArray(program, "vertex")
c.EnableVertexAttribArray(program, "color")
c.VertexAttribPointer(program, "vertex", true, false, int16Size*6, 2, int16Size*0)
c.VertexAttribPointer(program, "color", false, true, int16Size*6, 4, int16Size*2)
return func() {
c.DisableVertexAttribArray(program, "color")
c.DisableVertexAttribArray(program, "vertex")
}
}

View File

@ -23,10 +23,7 @@ type shaderId int
const ( const (
shaderVertexModelview shaderId = iota shaderVertexModelview shaderId = iota
shaderVertexColor
shaderVertexColorLine
shaderFragmentTexture shaderFragmentTexture
shaderFragmentSolid
) )
func shader(c *opengl.Context, id shaderId) string { func shader(c *opengl.Context, id shaderId) string {
@ -50,28 +47,6 @@ void main(void) {
vertex_out_tex_coord = tex_coord; vertex_out_tex_coord = tex_coord;
gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 0, 1); gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 0, 1);
} }
`,
shaderVertexColor: `
uniform highp mat4 projection_matrix;
attribute highp vec2 vertex;
attribute lowp vec4 color;
varying lowp vec4 vertex_out_color;
void main(void) {
vertex_out_color = color;
gl_Position = projection_matrix * vec4(vertex, 0, 1);
}
`,
shaderVertexColorLine: `
uniform highp mat4 projection_matrix;
attribute highp vec2 vertex;
attribute lowp vec4 color;
varying lowp vec4 vertex_out_color;
void main(void) {
vertex_out_color = color;
gl_Position = projection_matrix * vec4(vertex + vec2(0.5, 0.5), 0, 1);
}
`, `,
shaderFragmentTexture: ` shaderFragmentTexture: `
uniform lowp sampler2D texture; uniform lowp sampler2D texture;
@ -94,12 +69,5 @@ void main(void) {
gl_FragColor = color; gl_FragColor = color;
} }
`,
shaderFragmentSolid: `
varying lowp vec4 vertex_out_color;
void main(void) {
gl_FragColor = vertex_out_color;
}
`, `,
} }