fixed and improved shadows

This commit is contained in:
Thomas Friedel 2020-02-12 17:38:33 +01:00
parent 7213b3edcd
commit 0d915f7178
4 changed files with 38 additions and 43 deletions

View file

@ -245,6 +245,10 @@ func (b *GoGLBackend) drawBlurred(size float64, min, max vec) {
min[1] -= fsize * 3
max[0] += fsize * 3
max[1] += fsize * 3
min[0] = math.Max(0.0, math.Min(b.fw, min[0]))
min[1] = math.Max(0.0, math.Min(b.fh, min[1]))
max[0] = math.Max(0.0, math.Min(b.fw, max[0]))
max[1] = math.Max(0.0, math.Min(b.fh, max[1]))
gl.BindBuffer(gl.ARRAY_BUFFER, b.shadowBuf)
data := [16]float32{
@ -276,27 +280,27 @@ func (b *GoGLBackend) drawBlurred(size float64, min, max vec) {
gl.ClearColor(0, 0, 0, 0)
gl.BindTexture(gl.TEXTURE_2D, b.offscr1.tex)
b.enableTextureRenderTarget(&b.offscr2)
gl.Clear(gl.COLOR_BUFFER_BIT)
b.box3(sizea, false)
gl.BindTexture(gl.TEXTURE_2D, b.offscr2.tex)
gl.BindTexture(gl.TEXTURE_2D, b.offscr1.tex)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
b.box3(sizea, 0, false)
b.enableTextureRenderTarget(&b.offscr1)
b.box3(sizeb, false)
gl.BindTexture(gl.TEXTURE_2D, b.offscr1.tex)
b.enableTextureRenderTarget(&b.offscr2)
b.box3(sizec, false)
gl.BindTexture(gl.TEXTURE_2D, b.offscr2.tex)
b.enableTextureRenderTarget(&b.offscr1)
b.box3(sizea, true)
gl.BindTexture(gl.TEXTURE_2D, b.offscr1.tex)
b.box3(sizeb, -0.5, false)
b.enableTextureRenderTarget(&b.offscr2)
b.box3(sizeb, true)
gl.BindTexture(gl.TEXTURE_2D, b.offscr1.tex)
b.box3(sizec, 0, false)
b.enableTextureRenderTarget(&b.offscr1)
gl.BindTexture(gl.TEXTURE_2D, b.offscr2.tex)
b.box3(sizea, 0, true)
b.enableTextureRenderTarget(&b.offscr2)
gl.BindTexture(gl.TEXTURE_2D, b.offscr1.tex)
b.box3(sizeb, -0.5, true)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
gl.BindTexture(gl.TEXTURE_2D, b.offscr2.tex)
b.disableTextureRenderTarget()
b.box3(sizec, true)
gl.BindTexture(gl.TEXTURE_2D, b.offscr2.tex)
b.box3(sizec, 0, true)
gl.DisableVertexAttribArray(b.shd.Vertex)
gl.DisableVertexAttribArray(b.shd.TexCoord)
@ -304,7 +308,7 @@ func (b *GoGLBackend) drawBlurred(size float64, min, max vec) {
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
}
func (b *GoGLBackend) box3(size int, vertical bool) {
func (b *GoGLBackend) box3(size int, offset float32, vertical bool) {
gl.Uniform1i(b.shd.BoxSize, int32(size))
if vertical {
gl.Uniform1i(b.shd.BoxVertical, 1)
@ -313,5 +317,6 @@ func (b *GoGLBackend) box3(size int, vertical bool) {
gl.Uniform1i(b.shd.BoxVertical, 0)
gl.Uniform1f(b.shd.BoxScale, 1/float32(b.fw))
}
gl.Uniform1f(b.shd.BoxOffset, offset)
gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4)
}

View file

@ -139,6 +139,7 @@ func New(x, y, w, h int, ctx *GLContext) (*GoGLBackend, error) {
}
b.disableTextureRenderTarget = func() {
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
gl.Viewport(int32(b.x), int32(b.y), int32(b.w), int32(b.h))
}
return b, nil
@ -288,28 +289,29 @@ func colorGoToGL(c color.RGBA) glColor {
}
func (b *GoGLBackend) useShader(style *backendbase.FillStyle, useAlpha bool, alphaTexSlot int32) (vertexLoc, alphaTexCoordLoc uint32) {
var alphaVal int32
gl.UseProgram(b.shd.ID)
gl.Uniform2f(b.shd.CanvasSize, float32(b.fw), float32(b.fh))
if useAlpha {
alphaVal = 1
gl.Uniform1i(b.shd.UseAlphaTex, 1)
gl.Uniform1i(b.shd.AlphaTex, alphaTexSlot)
} else {
gl.Uniform1i(b.shd.UseAlphaTex, 0)
}
gl.Uniform1f(b.shd.GlobalAlpha, float32(style.Color.A)/255)
if lg := style.LinearGradient; lg != nil {
lg := lg.(*LinearGradient)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, lg.tex)
gl.UseProgram(b.shd.ID)
from := vec{style.Gradient.X0, style.Gradient.Y0}
to := vec{style.Gradient.X1, style.Gradient.Y1}
dir := to.sub(from)
length := dir.len()
dir = dir.scale(1 / length)
gl.Uniform2f(b.shd.CanvasSize, float32(b.fw), float32(b.fh))
gl.Uniform2f(b.shd.From, float32(from[0]), float32(from[1]))
gl.Uniform2f(b.shd.Dir, float32(dir[0]), float32(dir[1]))
gl.Uniform1f(b.shd.Len, float32(length))
gl.Uniform1i(b.shd.Gradient, 0)
gl.Uniform1f(b.shd.GlobalAlpha, float32(style.Color.A)/255)
gl.Uniform1i(b.shd.AlphaTex, alphaTexSlot)
gl.Uniform1i(b.shd.UseAlphaTex, alphaVal)
gl.Uniform1i(b.shd.Func, shdFuncLinearGradient)
return b.shd.Vertex, b.shd.TexCoord
}
@ -317,28 +319,21 @@ func (b *GoGLBackend) useShader(style *backendbase.FillStyle, useAlpha bool, alp
rg := rg.(*RadialGradient)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, rg.tex)
gl.UseProgram(b.shd.ID)
from := vec{style.Gradient.X0, style.Gradient.Y0}
to := vec{style.Gradient.X1, style.Gradient.Y1}
gl.Uniform2f(b.shd.CanvasSize, float32(b.fw), float32(b.fh))
gl.Uniform2f(b.shd.From, float32(from[0]), float32(from[1]))
gl.Uniform2f(b.shd.To, float32(to[0]), float32(to[1]))
gl.Uniform1f(b.shd.RadFrom, float32(style.Gradient.RadFrom))
gl.Uniform1f(b.shd.RadTo, float32(style.Gradient.RadTo))
gl.Uniform1i(b.shd.Gradient, 0)
gl.Uniform1f(b.shd.GlobalAlpha, float32(style.Color.A)/255)
gl.Uniform1i(b.shd.AlphaTex, alphaTexSlot)
gl.Uniform1i(b.shd.UseAlphaTex, alphaVal)
gl.Uniform1i(b.shd.Func, shdFuncRadialGradient)
return b.shd.Vertex, b.shd.TexCoord
}
if ip := style.ImagePattern; ip != nil {
ipd := ip.(*ImagePattern).data
img := ipd.Image.(*Image)
gl.UseProgram(b.shd.ID)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, img.tex)
gl.Uniform2f(b.shd.CanvasSize, float32(b.fw), float32(b.fh))
gl.Uniform2f(b.shd.ImageSize, float32(img.w), float32(img.h))
gl.Uniform1i(b.shd.Image, 0)
var f32mat [9]float32
@ -356,20 +351,13 @@ func (b *GoGLBackend) useShader(style *backendbase.FillStyle, useAlpha bool, alp
case backendbase.NoRepeat:
gl.Uniform2f(b.shd.Repeat, 0, 0)
}
gl.Uniform1f(b.shd.GlobalAlpha, float32(style.Color.A)/255)
gl.Uniform1i(b.shd.AlphaTex, alphaTexSlot)
gl.Uniform1i(b.shd.UseAlphaTex, alphaVal)
gl.Uniform1i(b.shd.Func, shdFuncImagePattern)
return b.shd.Vertex, b.shd.TexCoord
}
gl.UseProgram(b.shd.ID)
gl.Uniform2f(b.shd.CanvasSize, float32(b.fw), float32(b.fh))
c := colorGoToGL(style.Color)
gl.Uniform4f(b.shd.Color, float32(c.r), float32(c.g), float32(c.b), float32(c.a))
gl.Uniform1f(b.shd.GlobalAlpha, 1)
gl.Uniform1i(b.shd.AlphaTex, alphaTexSlot)
gl.Uniform1i(b.shd.UseAlphaTex, alphaVal)
gl.Uniform1i(b.shd.Func, shdFuncSolid)
return b.shd.Vertex, b.shd.TexCoord
}

View file

@ -41,6 +41,7 @@ uniform sampler2D alphaTex;
uniform int boxSize;
uniform bool boxVertical;
uniform float boxScale;
uniform float boxOffset;
bool isNaN(float v) {
return v < 0.0 || 0.0 < v || v == 0.0 ? false : true;
@ -52,17 +53,17 @@ void main() {
if (func == 5) {
vec4 sum = vec4(0.0);
if (boxVertical) {
vec2 start = v_tc - vec2(0.0, (float)(boxSize) * boxScale);
for (int i=0; i<boxSize*2; i++) {
sum += texture2D(image, start + vec2(0.0, (float)(i) * boxScale));
vec2 start = v_tc - vec2(0.0, (float(boxSize) * 0.5 + boxOffset) * boxScale);
for (int i=0; i <= boxSize; i++) {
sum += texture2D(image, start + vec2(0.0, float(i) * boxScale));
}
} else {
vec2 start = v_tc - vec2((float)(boxSize) * boxScale, 0.0);
for (int i=0; i<boxSize*2; i++) {
sum += texture2D(image, start + vec2((float)(i) * boxScale, 0.0));
vec2 start = v_tc - vec2((float(boxSize) * 0.5 + boxOffset) * boxScale, 0.0);
for (int i=0; i <= boxSize; i++) {
sum += texture2D(image, start + vec2(float(i) * boxScale, 0.0));
}
}
gl_FragColor = sum / float(boxSize * 2);
gl_FragColor = sum / float(boxSize+1);
return;
}
@ -149,4 +150,5 @@ type unifiedShader struct {
BoxSize int32
BoxVertical int32
BoxScale int32
BoxOffset int32
}

BIN
testdata/Shadow.png vendored

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB