From eed954203dd35b339a92400883299265b787506b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Tue, 1 Dec 2015 20:27:03 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/libs@55 a8b55f48-bf90-11e4-a774-851b48703e85 --- qglview/globject.cpp | 4 +- qglview/glshaders.cpp | 2 + qglview/renderer_deferred_shading.cpp | 10 ++-- qglview/shaders/dsl_pass_0.frag | 13 ++--- qglview/shaders/dsl_pass_0.geom | 14 ------ qglview/shaders/dsl_pass_0.vert | 3 +- qglview/shaders/dsl_pass_1.frag | 71 +++++++++++++++++++-------- qglview/shaders/shadow.frag | 2 + qglview/shaders/shadow.vert | 5 +- 9 files changed, 74 insertions(+), 50 deletions(-) delete mode 100644 qglview/shaders/dsl_pass_0.geom diff --git a/qglview/globject.cpp b/qglview/globject.cpp index 5128aa7..abf622d 100644 --- a/qglview/globject.cpp +++ b/qglview/globject.cpp @@ -210,7 +210,7 @@ void GLObjectBase::render(int * id, QMap * ids, int sh_id_ -Light::Light(): GLObjectBase(), shadow_map(0, true, GL_R16F) { +Light::Light(): GLObjectBase(), shadow_map(1, true, GL_R16F) { type_ = GLObjectBase::Light; light_type = Omni; intensity = 1.; @@ -221,7 +221,7 @@ Light::Light(): GLObjectBase(), shadow_map(0, true, GL_R16F) { } -Light::Light(const QVector3D & p, const QColor & c, GLdouble i): GLObjectBase(), shadow_map(0, true, GL_R16F) { +Light::Light(const QVector3D & p, const QColor & c, GLdouble i): GLObjectBase(), shadow_map(1, true, GL_R16F) { type_ = GLObjectBase::Light; light_type = Omni; pos_ = p; diff --git a/qglview/glshaders.cpp b/qglview/glshaders.cpp index a548fa5..d6d025c 100644 --- a/qglview/glshaders.cpp +++ b/qglview/glshaders.cpp @@ -66,6 +66,7 @@ const char qgl_structs[] = " float linearAttenuation;\n" " float quadraticAttenuation;\n" " sampler2DShadow shadow;\n" + " sampler2D shadowColor;\n" " mat4 shadowMatrix;\n" "};\n" "struct QGLMap {\n" @@ -233,5 +234,6 @@ void setUniformLight(QGLShaderProgram * prog, Light * light, QString ulightn, co prog->setUniformValue((ulightn + ".linearAttenuation").toLatin1().constData(), GLfloat(light->decay_linear)); prog->setUniformValue((ulightn + ".quadraticAttenuation").toLatin1().constData(), GLfloat(light->decay_quadratic)); prog->setUniformValue((ulightn + ".shadow").toLatin1().constData(), shadow); + prog->setUniformValue((ulightn + ".shadowColor").toLatin1().constData(), shadow); prog->setUniformValue((ulightn + ".shadowMatrix").toLatin1().constData(), light->shadow_matrix); } diff --git a/qglview/renderer_deferred_shading.cpp b/qglview/renderer_deferred_shading.cpp index d8cf1ed..51afa0b 100644 --- a/qglview/renderer_deferred_shading.cpp +++ b/qglview/renderer_deferred_shading.cpp @@ -127,9 +127,9 @@ void RendererDeferredShading::renderScene() { if (l->light_type == Light::Omni) continue; l->shadow_map.resize(sms, sms); l->shadow_map.bind(); - //l->shadow_map.setWriteBuffer(0); - //glClearFramebuffer(); - glClear(GL_DEPTH_BUFFER_BIT); + l->shadow_map.setWriteBuffer(0); + glClearFramebuffer(); + //glClear(GL_DEPTH_BUFFER_BIT); renderShadow(l, shader_shadow, mviewi); l->shadow_map.release(); } @@ -364,10 +364,10 @@ void RendererDeferredShading::setupDSLights(int pass, const QMatrix4x4 & view_ma for (int i = light_start; i < light_end; ++i) { lv << view.lights()[i]; glActiveTextureChannel(shadow_start + i - light_start); - glBindTexture(GL_TEXTURE_2D, lv.back()->shadow_map.depthTexture()); + glBindTexture(GL_TEXTURE_2D, lv.back()->shadow_map.colorTexture()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); } amb_light.setName("null"); for (int i = light_end; i < lmax; ++i) diff --git a/qglview/shaders/dsl_pass_0.frag b/qglview/shaders/dsl_pass_0.frag index e3945ad..f3acccd 100644 --- a/qglview/shaders/dsl_pass_0.frag +++ b/qglview/shaders/dsl_pass_0.frag @@ -1,6 +1,6 @@ #version 150 -in vec3 src_normal, normal;//, et; +in vec3 src_normal, normal, binormal;//, et; in vec4 pos, ppos; in float fogCoord; in mat3 TBN; @@ -18,14 +18,15 @@ void main(void) { float hei = dot(texture(qgl_Material.map_relief.map, tc).rgb, luma) * qgl_Material.map_relief.amount + qgl_Material.map_relief.offset; //if (acc_fog) dc.xyz = mix(dc.rgb, gl_Fog.color.rgb, fogCoord); - vec3 n, dn; + vec3 n, dn, bn; dn = (texture(qgl_Material.map_normal.map, tc).rgb - vec3(0.5, 0.5, 1.)) * qgl_Material.map_normal.amount + qgl_Material.map_normal.offset; //float tx = dn.x; dn.y = -dn.y; //dn.y = tx; dn = TBN * dn; //dn = dn * mat3(qgl_ModelViewMatrix)*10;//*(mat3(qgl_NormalMatrix)); - n = normalize(qgl_NormalMatrix*(normal+dn));//normalize(qgl_NormalMatrix * (normal - dn)); + n = normalize(qgl_NormalMatrix * (normal + dn));//normalize(qgl_NormalMatrix * (normal - dn)); + bn = normalize(qgl_NormalMatrix * binormal);//normalize(qgl_NormalMatrix * (normal - dn)); //n = dn; /*vec2 dpm = normalize(gl_FragCoord.xy * dt * 2. - vec2(1., 1.)), ntc; @@ -45,11 +46,11 @@ void main(void) { vec3 speed = pos.xyz/pos.w - ppos.xyz/ppos.w; //speed /= abs(pos.z); - qgl_FragData[0] = vec4(dc.rgb, z); + qgl_FragData[0] = vec4(dc.rgb, pos.w); qgl_FragData[1] = vec4(n.xyz / 2. + vec3(0.5), specularity); qgl_FragData[2] = vec4(spec.rgb, hei); - qgl_FragData[3] = vec4(self.rgb, pos.w); - qgl_FragData[4] = vec4(speed.xy, 0, 0); + qgl_FragData[3] = vec4(self.rgb, bn.x); + qgl_FragData[4] = vec4(speed.xy, bn.yz); //gl_FragData[0] = vec4(et.xyz, pos.w); //gl_FragDepth = gl_FragCoord.z - clamp(hei / pos.z / pos.z / (abs(n.z) + 1), -0.01, 0.01); diff --git a/qglview/shaders/dsl_pass_0.geom b/qglview/shaders/dsl_pass_0.geom deleted file mode 100644 index bf08c73..0000000 --- a/qglview/shaders/dsl_pass_0.geom +++ /dev/null @@ -1,14 +0,0 @@ -#version 130 - -layout(triangles, invocations = 1) in; - -in vec3 normal[]; -out vec3 tangent[]; - -void main() { - for (int i = 0; i < gl_in.length(); i++) { - gl_Position = gl_in[i].gl_Position; - EmitVertex(); - } - EndPrimitive(); -} diff --git a/qglview/shaders/dsl_pass_0.vert b/qglview/shaders/dsl_pass_0.vert index c5ce4fb..f3ad6d0 100644 --- a/qglview/shaders/dsl_pass_0.vert +++ b/qglview/shaders/dsl_pass_0.vert @@ -1,6 +1,6 @@ #version 150 -out vec3 src_normal, normal;//, et; +out vec3 src_normal, normal, binormal;//, et; out vec4 pos, ppos; out float fogCoord, fs_gid; out mat3 TBN; @@ -12,6 +12,7 @@ uniform mat4 prev_ModelViewProjectioMatrix; void main(void) { normal = qgl_Normal;//(qgl_NormalMatrix * qgl_Normal); + binormal = qgl_Tangent;//(qgl_NormalMatrix * qgl_Normal); pos = qgl_ftransform(); TBN = mat3(qgl_Tangent, qgl_Bitangent, qgl_Normal); /*if (acc_fog) { diff --git a/qglview/shaders/dsl_pass_1.frag b/qglview/shaders/dsl_pass_1.frag index aa40dc1..29805ac 100644 --- a/qglview/shaders/dsl_pass_1.frag +++ b/qglview/shaders/dsl_pass_1.frag @@ -17,8 +17,11 @@ float light_diffuse(int model, vec3 l, vec3 n) {return max(0., dot(l, n));} float light_specular(int model, vec3 l, vec3 n, vec3 h, vec3 v, float shininess) {return max(0., pow(dot(n, h), shininess));} vec4 pos, lpos, shp; -vec3 li, si, ldir, halfV; -float sh_pow, sh_mul, dist, NdotL, NdotH, spot, ldist, diff; +vec3 li, si, ldir, halfV, bn, bn2; +vec2 vds, vds2; +float sh_pow, sh_mul, dist, NdotL, NdotH, spot, ldist, diff, sdist, shadow; + + void calcLight(in int index, in vec3 n, in vec3 v, in vec4 v2) { lpos = qgl_Light[index].position; @@ -37,23 +40,47 @@ void calcLight(in int index, in vec3 n, in vec3 v, in vec4 v2) { spot *= smoothstep(qgl_Light[index].endAngleCos, qgl_Light[index].startAngleCos, scos); //spot = pow(spot, (qgl_Light[index].spotExponent + 0.001)); shp = qgl_Light[index].shadowMatrix*vec4(pos.xyz,1); - shp.z -= 0.1; - ///spot *= textureProj(qgl_Light[index].shadow, shp); - float ds = shp.z / 100, dds = ds + ds; - float shadow = textureProj(qgl_Light[index].shadow, vec4(shp.xy, shp.zw))/* + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( 0, ds), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( 0, -ds), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( ds, 0), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( -ds, 0), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( ds, ds), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( ds, -ds), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( -ds, -ds), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( -ds, ds), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( dds, 0), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2(-dds, 0), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( 0, dds), shp.zw)) + - textureProj(qgl_Light[index].shadow, vec4(shp.xy + vec2( 0, -dds), shp.zw))*/; - spot *= shadow / 1.; + //shp.z -= 0.1; + shp.xyz /= shp.w; + float shr = texture(qgl_Light[index].shadowColor, shp.xy).r; + sdist = shp.w - shr - 1.; + //if (sdist > 0.) { + float ds = 0.01;//max(abs(sdist) / 5000, 0.02); + //spot *= clamp(1. - sdist, 0, 1); + vds = vec2(ds,0);//bn.xy * ds; + vds2 = vec2(0,ds);//bn2.xy * ds; + shadow = //shr + //texture(qgl_Light[index].shadowColor, shp.xy, shp.zw)) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy + vds ).r - 1, 0, 1) + /*clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy + vds - vds2 ).r - 1, 0, 1) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy + vds + vds2 ).r - 1, 0, 1) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy - vds ).r - 1, 0, 1) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy - vds - vds2 ).r - 1, 0, 1) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy - vds + vds2 ).r - 1, 0, 1) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy - vds2 ).r - 1, 0, 1) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy + vds2 ).r - 1, 0, 1) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy + vds + vds ).r - 1, 0, 1) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy - vds - vds ).r - 1, 0, 1) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy - vds2 - vds2).r - 1, 0, 1) + + clamp(shp.w - texture(qgl_Light[index].shadowColor, shp.xy + vds2 + vds2).r - 1, 0, 1)*/; + /*texture(qgl_Light[index].shadowColor, shp.xy - vds ).r + + texture(qgl_Light[index].shadowColor, shp.xy - vds + vds2 ).r + + texture(qgl_Light[index].shadowColor, shp.xy + vds2 ).r + + texture(qgl_Light[index].shadowColor, shp.xy ).r + + texture(qgl_Light[index].shadowColor, shp.xy - vds ).r + + texture(qgl_Light[index].shadowColor, shp.xy - vds2 ).r + + texture(qgl_Light[index].shadowColor, shp.xy - vds - vds2 ).r + + texture(qgl_Light[index].shadowColor, shp.xy ).r + + texture(qgl_Light[index].shadowColor, shp.xy + vds + vds + vds2 + vds2).r + + texture(qgl_Light[index].shadowColor, shp.xy + vds + vds + vds2 + vds2).r + + texture(qgl_Light[index].shadowColor, shp.xy + vds + vds + vds2 + vds2).r + + texture(qgl_Light[index].shadowColor, shp.xy + vds + vds + vds2 + vds2).r + + texture(qgl_Light[index].shadowColor, shp.xy + vds + vds + vds2 + vds2).r + + texture(qgl_Light[index].shadowColor, shp.xy + vds + vds + vds2 + vds2).r + + texture(qgl_Light[index].shadowColor, shp.xy + vds + vds + vds2 + vds2).r + + texture(qgl_Light[index].shadowColor, shp.xy + vds + vds + vds2 + vds2).r*/; + shadow = shadow / 1;// - 0.5; + spot *= ds*100;//clamp(1. - shadow, 0, 1); + //} ///spot *= textureProj(qgl_Light[index].shadow, shp); } spot /= (qgl_Light[index].constantAttenuation + ldist * (qgl_Light[index].linearAttenuation + ldist * qgl_Light[index].quadraticAttenuation)); @@ -85,13 +112,15 @@ void main(void) { v4 = texelFetch(t4, tc, 0); vec2 sp = gl_FragCoord.xy * dt * 2 - vec2(1, 1); vec3 dc = v0.rgb, n = v1.xyz * 2. - vec3(1.); + bn = vec3(v3.w, v4.zw); + bn2 = normalize(cross(n, bn)); float height = v2.w; li = qgl_AmbientLight.color.rgb * qgl_AmbientLight.intensity; si = vec3(0.); float posz = z_near * z_far / (texelFetch(td, tc, 0).r * (z_far - z_near) - z_far); pos = vec4(sp, 0., 1) * mat_proji; - pos.xy *= v3.w; + pos.xy *= v0.w; pos.z = posz; //pos.xyz += n * height; //pos.xyz = v3.xyz; @@ -115,6 +144,6 @@ void main(void) { //shd = shd - shp.w; //qgl_FragData[0].rgb = vec3(abs(shd)/10000); //qgl_FragData[0].rgb = vec3((shp.z)/1000); - //qgl_FragData[0].rgb = vec3(abs(pos.xyz)/1000); + qgl_FragData[0].rgb = vec3((shp.w-1 - texture(qgl_Light[4].shadowColor, shp.xy-vec2(0.01,0)).r)); //qgl_FragData[0].a = 0.; } diff --git a/qglview/shaders/shadow.frag b/qglview/shaders/shadow.frag index c99df5b..25628db 100644 --- a/qglview/shaders/shadow.frag +++ b/qglview/shaders/shadow.frag @@ -1,5 +1,7 @@ #version 150 +in float w; void main(void) { + qgl_FragData[0].r = w; } diff --git a/qglview/shaders/shadow.vert b/qglview/shaders/shadow.vert index a22256f..3b1faca 100644 --- a/qglview/shaders/shadow.vert +++ b/qglview/shaders/shadow.vert @@ -1,7 +1,10 @@ #version 150 +out float w; void main(void) { qgl_FragTexture = qgl_Texture; - gl_Position = qgl_ftransform(); + vec4 pos = qgl_ftransform(); + w = pos.w; + gl_Position = pos; }