fixed many memory leaks
added working shadows, using basic shadow mapping
This commit is contained in:
@@ -13,8 +13,7 @@ static const char* createShadowMapInstancingVertexShader= \
|
||||
"layout (location = 6) in vec3 instance_scale;\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"uniform mat4 ModelViewMatrix;\n"
|
||||
"uniform mat4 ProjectionMatrix;\n"
|
||||
"uniform mat4 depthMVP;\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"vec4 quatMul ( in vec4 q1, in vec4 q2 )\n"
|
||||
@@ -52,7 +51,7 @@ static const char* createShadowMapInstancingVertexShader= \
|
||||
"{\n"
|
||||
" vec4 q = instance_quaternion;\n"
|
||||
" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n"
|
||||
" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4( (instance_position+localcoord).xyz,1);\n"
|
||||
" vec4 vertexPos = depthMVP * vec4( (instance_position+localcoord).xyz,1);\n"
|
||||
" gl_Position = vertexPos;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
|
||||
@@ -11,8 +11,7 @@ layout (location = 5) in vec4 instance_color;
|
||||
layout (location = 6) in vec3 instance_scale;
|
||||
|
||||
|
||||
uniform mat4 ModelViewMatrix;
|
||||
uniform mat4 ProjectionMatrix;
|
||||
uniform mat4 depthMVP;
|
||||
|
||||
|
||||
vec4 quatMul ( in vec4 q1, in vec4 q2 )
|
||||
@@ -50,7 +49,7 @@ void main(void)
|
||||
{
|
||||
vec4 q = instance_quaternion;
|
||||
vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);
|
||||
vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4( (instance_position+localcoord).xyz,1);
|
||||
vec4 vertexPos = depthMVP * vec4( (instance_position+localcoord).xyz,1);
|
||||
gl_Position = vertexPos;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,15 +26,31 @@ void main(void)
|
||||
vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color;
|
||||
vec3 ct,cf;
|
||||
float intensity,at,af;
|
||||
intensity = max(dot(lightDir,normalize(normal)),0);
|
||||
cf = intensity*vec3(1.0,1.0,1.0)+ambient;
|
||||
|
||||
intensity = clamp( dot( normalize(normal),lightDir ), 0,1 );
|
||||
|
||||
cf = ambient;
|
||||
af = 1.0;
|
||||
|
||||
ct = texel.rgb;
|
||||
at = texel.a;
|
||||
|
||||
float bias = 0.005f;
|
||||
//float bias = 0.005f;
|
||||
|
||||
float bias = 0.005*tan(acos(intensity));
|
||||
bias = clamp(bias, 0,0.01);
|
||||
|
||||
|
||||
float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z-bias)/ShadowCoord.w));
|
||||
|
||||
color = vec4(ct * cf * visibility, at * af);
|
||||
intensity*=2;
|
||||
if (intensity>1)
|
||||
intensity=1.f;
|
||||
|
||||
visibility *= intensity;
|
||||
|
||||
if (visibility<0.6)
|
||||
visibility=0.6f;
|
||||
|
||||
color = vec4(ct * visibility, 1.f);//at * af);
|
||||
}
|
||||
|
||||
@@ -28,17 +28,33 @@ static const char* useShadowMapInstancingFragmentShader= \
|
||||
" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color;\n"
|
||||
" vec3 ct,cf;\n"
|
||||
" float intensity,at,af;\n"
|
||||
" intensity = max(dot(lightDir,normalize(normal)),0);\n"
|
||||
" cf = intensity*vec3(1.0,1.0,1.0)+ambient;\n"
|
||||
" \n"
|
||||
" intensity = clamp( dot( normalize(normal),lightDir ), 0,1 );\n"
|
||||
" \n"
|
||||
" cf = ambient;\n"
|
||||
" af = 1.0;\n"
|
||||
" \n"
|
||||
" ct = texel.rgb;\n"
|
||||
" at = texel.a;\n"
|
||||
" \n"
|
||||
" float bias = 0.005f;\n"
|
||||
" //float bias = 0.005f;\n"
|
||||
" \n"
|
||||
" float bias = 0.005*tan(acos(intensity));\n"
|
||||
" bias = clamp(bias, 0,0.01);\n"
|
||||
"\n"
|
||||
"\n"
|
||||
" float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z-bias)/ShadowCoord.w));\n"
|
||||
" \n"
|
||||
" color = vec4(ct * cf * visibility, at * af); \n"
|
||||
" intensity*=2;\n"
|
||||
" if (intensity>1)\n"
|
||||
" intensity=1.f;\n"
|
||||
" \n"
|
||||
" visibility *= intensity;\n"
|
||||
" \n"
|
||||
" if (visibility<0.6)\n"
|
||||
" visibility=0.6f;\n"
|
||||
" \n"
|
||||
" color = vec4(ct * visibility, 1.f);//at * af); \n"
|
||||
"}\n"
|
||||
"\n"
|
||||
;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#version 330 core
|
||||
//precision highp float;
|
||||
#version 330
|
||||
precision highp float;
|
||||
|
||||
|
||||
layout (location = 0) in vec4 position;
|
||||
@@ -14,6 +14,7 @@ layout (location = 6) in vec3 instance_scale;
|
||||
uniform mat4 ModelViewMatrix;
|
||||
uniform mat4 ProjectionMatrix;
|
||||
uniform mat4 DepthBiasModelViewProjectionMatrix;
|
||||
uniform mat4 MVP;
|
||||
|
||||
out vec4 ShadowCoord;
|
||||
|
||||
@@ -66,16 +67,15 @@ void main(void)
|
||||
ambient = vec3(0.3,.3,0.3);
|
||||
|
||||
|
||||
vec4 local_normal = (quatRotate3( vertexnormal,q));
|
||||
vec3 light_pos = vec3(-0.3,0.1,0.1);
|
||||
normal = local_normal.xyz;//normalize(ModelViewMatrix * local_normal).xyz;
|
||||
vec4 worldNormal = (quatRotate3( vertexnormal,q));
|
||||
vec3 light_pos = vec3(-5.f,100,-40);
|
||||
normal = normalize(worldNormal).xyz;
|
||||
|
||||
lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz));
|
||||
// lightDir = normalize(vec3(gl_LightSource[0].position));
|
||||
|
||||
vec4 axis = vec4(1,1,1,0);
|
||||
vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);
|
||||
vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4((instance_position+localcoord).xyz,1);
|
||||
vec4 vertexPos = MVP* vec4((instance_position+localcoord).xyz,1);
|
||||
|
||||
gl_Position = vertexPos;
|
||||
ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
|
||||
static const char* useShadowMapInstancingVertexShader= \
|
||||
"#version 330 core\n"
|
||||
"//precision highp float;\n"
|
||||
"#version 330 \n"
|
||||
"precision highp float;\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"layout (location = 0) in vec4 position;\n"
|
||||
@@ -16,6 +16,7 @@ static const char* useShadowMapInstancingVertexShader= \
|
||||
"uniform mat4 ModelViewMatrix;\n"
|
||||
"uniform mat4 ProjectionMatrix;\n"
|
||||
"uniform mat4 DepthBiasModelViewProjectionMatrix;\n"
|
||||
"uniform mat4 MVP;\n"
|
||||
"\n"
|
||||
"out vec4 ShadowCoord;\n"
|
||||
"\n"
|
||||
@@ -68,16 +69,15 @@ static const char* useShadowMapInstancingVertexShader= \
|
||||
" ambient = vec3(0.3,.3,0.3);\n"
|
||||
" \n"
|
||||
" \n"
|
||||
" vec4 local_normal = (quatRotate3( vertexnormal,q));\n"
|
||||
" vec3 light_pos = vec3(-0.3,0.1,0.1);\n"
|
||||
" normal = local_normal.xyz;//normalize(ModelViewMatrix * local_normal).xyz;\n"
|
||||
" vec4 worldNormal = (quatRotate3( vertexnormal,q));\n"
|
||||
" vec3 light_pos = vec3(-5.f,100,-40);\n"
|
||||
" normal = normalize(worldNormal).xyz;\n"
|
||||
"\n"
|
||||
" lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz));\n"
|
||||
"// lightDir = normalize(vec3(gl_LightSource[0].position));\n"
|
||||
" \n"
|
||||
" vec4 axis = vec4(1,1,1,0);\n"
|
||||
" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n"
|
||||
" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4((instance_position+localcoord).xyz,1);\n"
|
||||
" vec4 vertexPos = MVP* vec4((instance_position+localcoord).xyz,1);\n"
|
||||
"\n"
|
||||
" gl_Position = vertexPos;\n"
|
||||
" ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n"
|
||||
|
||||
Reference in New Issue
Block a user