Skip to content

Commit

Permalink
fix bug with alpha channel not working on some nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmaclarty committed Feb 1, 2016
1 parent 909a7e2 commit d94763b
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 3 deletions.
Binary file modified lua/default_font.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions lua/shaders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ local texturecolor_f = [[
}
]]

local premult_texturecolor_f = [[
precision mediump float;
uniform sampler2D tex;
uniform vec4 color;
varying vec2 v_uv;
void main() {
gl_FragColor = texture2D(tex, v_uv) * vec4(color.rgb * color.a, color.a);
}
]]

local sources = {
color = {color_v, color_f},
color2d = {color2d_v, color_f},
Expand All @@ -114,6 +124,7 @@ local sources = {
texture2d = {texture2d_v, texture_f},
texturecolor = {texture_v, texturecolor_f},
texturecolor2d = {texture2d_v, texturecolor_f},
premult_texturecolor2d = {texture2d_v, premult_texturecolor_f},
}

am.shaders = {}
Expand Down
3 changes: 3 additions & 0 deletions lua/shapes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function am.rect(x1, y1, x2, y2, color)
color = color or vec4(1)
local verts = am.rect_verts_2d(x1, y1, x2, y2)
local node = am.use_program(am.shaders.color2d)
^am.blend"alpha"
^am.bind{
vert = verts,
color = color,
Expand Down Expand Up @@ -112,6 +113,7 @@ function am.circle(center, radius, color, sides)
am.translate(center)
^am.scale(radius)
^am.use_program(am.shaders.color2d)
^am.blend"alpha"
^am.bind{
vert = verts,
color = color,
Expand Down Expand Up @@ -163,6 +165,7 @@ function am.line(point1, point2, thickness, color)
set_verts()
local node =
am.use_program(am.shaders.color2d)
^am.blend"alpha"
^am.bind{
vert = verts,
color = color,
Expand Down
4 changes: 2 additions & 2 deletions lua/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function am.text(font, str, color, halign, valign)
local w, h = set_text_verts(font, str, verts, uvs, halign, valign)
local node =
am.blend(font.is_premult and "premult" or "alpha")
^am.use_program(am.shaders.texturecolor2d)
^am.use_program(font.is_premult and am.shaders.premult_texturecolor2d or am.shaders.texturecolor2d)
^am.bind{
vert = verts,
uv = uvs,
Expand Down Expand Up @@ -384,7 +384,7 @@ function am.sprite(image0, color, halign, valign)
set_sprite_verts(image, verts, uvs, halign, valign)
local node =
am.blend(image.is_premult and "premult" or "alpha")
^am.use_program(am.shaders.texturecolor2d)
^am.use_program(image.is_premult and am.shaders.premult_texturecolor2d or am.shaders.texturecolor2d)
^am.bind{
vert = verts,
uv = uvs,
Expand Down
2 changes: 1 addition & 1 deletion src/am_framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void am_framebuffer::init(lua_State *L, am_texture2d *texture, bool depth_buf, b
if (status != AM_FRAMEBUFFER_STATUS_COMPLETE) {
luaL_error(L, "framebuffer incomplete");
}
clear_color = clear_color;
am_framebuffer::clear_color = clear_color;
user_projection = false;
float w = (float)width;
float h = (float)height;
Expand Down
1 change: 1 addition & 0 deletions tests/test_render.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ok
73 changes: 73 additions & 0 deletions tests/test_render.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
win = am.window{}

ib1 = am.image_buffer(1)
v1 = ib1.buffer:view"ubyte"
tx1 = am.texture2d(ib1)
fb1 = am.framebuffer(tx1)

fb1:clear()
fb1:read_back()
assert(v1[1] == 0)
assert(v1[2] == 0)
assert(v1[3] == 0)
assert(v1[4] == 255)
fb1.clear_color = vec4(128, 64, 32, 16) / 255
fb1:clear()
fb1:read_back()
assert(v1[1] == 128)
assert(v1[2] == 64)
assert(v1[3] == 32)
assert(v1[4] == 16)

fb1.clear_color = vec4(0, 0, 0, 1)

fb1:clear()
fb1:render(am.rect(-2, -2, 2, 2, vec4(0, 1, 0, 1)))
fb1:read_back()
assert(v1[1] == 0)
assert(v1[2] == 255)
assert(v1[3] == 0)
assert(v1[4] == 255)

fb1:clear()
fb1:render(am.rect(-2, -2, 2, 2, vec4(255, 255, 0, 128) / 255))
fb1:read_back()
assert(v1[1] == 128)
assert(v1[2] == 128)
assert(v1[3] == 0)
assert(v1[4] == 64 + 255 - 128)

fb1:clear()
fb1:render(am.circle(vec2(0, 0), 2, vec4(255, 255, 255, 64) / 255))
fb1:read_back()
assert(v1[1] == 64)
assert(v1[2] == 64)
assert(v1[3] == 64)
assert(v1[4] == 16 + 255 - 64)

fb1:clear()
fb1:render(am.line(vec2(-2, -2), vec2(2, 2), 4, vec4(255, 0, 128, 128) / 255))
fb1:read_back()
assert(v1[1] == 128)
assert(v1[2] == 0)
assert(v1[3] == 64)
assert(v1[4] == 64 + 255 - 128)

fb1:clear()
fb1:render(am.scale(4) ^ am.text("I", vec4(255, 0, 0, 128) / 255))
fb1:read_back()
assert(v1[1] == 128)
assert(v1[2] == 0)
assert(v1[3] == 0)
assert(v1[4] == 128 + 255 - 128)

fb1:clear()
fb1:render(am.sprite("CCC\nCCC", vec4(128, 128, 128, 128) / 255))
fb1:read_back()
assert(v1[1] == 0)
assert(v1[2] == 64)
assert(v1[3] == 64)
assert(v1[4] == 64 + 255 - 128)

win:close()
print"ok"

0 comments on commit d94763b

Please sign in to comment.