Implemented specular reflective lighting for OpenGL 3.x, everything looks shiny (will add APIs to make it less shiny ;-)
Remove roof from kitchens/1.sdf, otherwise shadows and shinyness won't work (light is outside the room, bouncing against roof-top) Make kuka_iiwa/model.urdf more smooth, use .obj for per-vertex normals (using Blender, import STL, export OBJ, enable triangles, normals and Z-UP, Y forward)
This commit is contained in:
@@ -13,9 +13,14 @@ in Vert
|
||||
|
||||
uniform sampler2D Diffuse;
|
||||
uniform sampler2DShadow shadowMap;
|
||||
uniform mat4 ViewMatrixInverse;
|
||||
|
||||
in vec3 lightDir,normal,ambient;
|
||||
in vec3 lightPos,cameraPosition, normal,ambient;
|
||||
in vec4 ShadowCoord;
|
||||
in vec4 vertexPos;
|
||||
in float materialShininess;
|
||||
in vec3 lightSpecularIntensity;
|
||||
in vec3 materialSpecularColor;
|
||||
|
||||
out vec4 color;
|
||||
|
||||
@@ -28,8 +33,11 @@ void main(void)
|
||||
float intensity,at,af;
|
||||
if (fragment.color.w==0)
|
||||
discard;
|
||||
|
||||
intensity = 0.5+0.5*clamp( dot( normalize(normal),lightDir ), -1,1 );
|
||||
vec3 lightDir = normalize(lightPos);
|
||||
|
||||
vec3 normalDir = normalize(normal);
|
||||
|
||||
intensity = 0.5+0.5*clamp( dot( normalDir,lightDir ), -1,1 );
|
||||
|
||||
af = 1.0;
|
||||
|
||||
@@ -38,7 +46,24 @@ void main(void)
|
||||
|
||||
//float bias = 0.005f;
|
||||
|
||||
vec3 specularReflection;
|
||||
|
||||
if (dot(normalDir, lightDir) < 0.0)
|
||||
{
|
||||
specularReflection = vec3(0.0, 0.0, 0.0);
|
||||
}
|
||||
else // light source on the right side
|
||||
{
|
||||
vec3 surfaceToLight = normalize(lightPos - vertexPos.xyz);
|
||||
vec3 surfaceToCamera = normalize(cameraPosition - vertexPos.xyz);
|
||||
|
||||
|
||||
float specularCoefficient = 0.0;
|
||||
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalDir))), materialShininess);
|
||||
specularReflection = specularCoefficient * materialSpecularColor * lightSpecularIntensity;
|
||||
|
||||
}
|
||||
|
||||
|
||||
float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z)/ShadowCoord.w));
|
||||
if (intensity<0.5)
|
||||
@@ -46,6 +71,6 @@ void main(void)
|
||||
|
||||
intensity = 0.7*intensity + 0.3*intensity*visibility;
|
||||
|
||||
cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient;
|
||||
cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient+specularReflection*visibility;
|
||||
color = vec4(ct * cf, fragment.color.w);
|
||||
}
|
||||
|
||||
@@ -12,8 +12,13 @@ static const char* useShadowMapInstancingFragmentShader= \
|
||||
"} vert;\n"
|
||||
"uniform sampler2D Diffuse;\n"
|
||||
"uniform sampler2DShadow shadowMap;\n"
|
||||
"in vec3 lightDir,normal,ambient;\n"
|
||||
"uniform mat4 ViewMatrixInverse;\n"
|
||||
"in vec3 lightPos,cameraPosition, normal,ambient;\n"
|
||||
"in vec4 ShadowCoord;\n"
|
||||
"in vec4 vertexPos;\n"
|
||||
"in float materialShininess;\n"
|
||||
"in vec3 lightSpecularIntensity;\n"
|
||||
"in vec3 materialSpecularColor;\n"
|
||||
"out vec4 color;\n"
|
||||
"void main(void)\n"
|
||||
"{\n"
|
||||
@@ -22,7 +27,11 @@ static const char* useShadowMapInstancingFragmentShader= \
|
||||
" float intensity,at,af;\n"
|
||||
" if (fragment.color.w==0)\n"
|
||||
" discard;\n"
|
||||
" intensity = 0.5+0.5*clamp( dot( normalize(normal),lightDir ), -1,1 );\n"
|
||||
" vec3 lightDir = normalize(lightPos);\n"
|
||||
" \n"
|
||||
" vec3 normalDir = normalize(normal);\n"
|
||||
" \n"
|
||||
" intensity = 0.5+0.5*clamp( dot( normalDir,lightDir ), -1,1 );\n"
|
||||
" \n"
|
||||
" af = 1.0;\n"
|
||||
" \n"
|
||||
@@ -31,13 +40,30 @@ static const char* useShadowMapInstancingFragmentShader= \
|
||||
" \n"
|
||||
" //float bias = 0.005f;\n"
|
||||
" \n"
|
||||
" vec3 specularReflection;\n"
|
||||
" \n"
|
||||
" if (dot(normalDir, lightDir) < 0.0) \n"
|
||||
" {\n"
|
||||
" specularReflection = vec3(0.0, 0.0, 0.0);\n"
|
||||
" }\n"
|
||||
" else // light source on the right side\n"
|
||||
" {\n"
|
||||
" vec3 surfaceToLight = normalize(lightPos - vertexPos.xyz);\n"
|
||||
" vec3 surfaceToCamera = normalize(cameraPosition - vertexPos.xyz);\n"
|
||||
" \n"
|
||||
" \n"
|
||||
" float specularCoefficient = 0.0;\n"
|
||||
" specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalDir))), materialShininess);\n"
|
||||
" specularReflection = specularCoefficient * materialSpecularColor * lightSpecularIntensity;\n"
|
||||
" \n"
|
||||
" }\n"
|
||||
" \n"
|
||||
" float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z)/ShadowCoord.w));\n"
|
||||
" if (intensity<0.5)\n"
|
||||
" visibility = 0;\n"
|
||||
" intensity = 0.7*intensity + 0.3*intensity*visibility;\n"
|
||||
" \n"
|
||||
" cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient;\n"
|
||||
" cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient+specularReflection*visibility;\n"
|
||||
" color = vec4(ct * cf, fragment.color.w);\n"
|
||||
"}\n"
|
||||
;
|
||||
|
||||
@@ -15,7 +15,12 @@ uniform mat4 ModelViewMatrix;
|
||||
uniform mat4 ProjectionMatrix;
|
||||
uniform mat4 DepthBiasModelViewProjectionMatrix;
|
||||
uniform mat4 MVP;
|
||||
uniform vec3 lightDirIn;
|
||||
uniform vec3 lightPosIn;
|
||||
uniform vec3 cameraPositionIn;
|
||||
uniform mat4 ViewMatrixInverse;
|
||||
uniform float materialShininessIn;
|
||||
uniform vec3 lightSpecularIntensityIn;
|
||||
uniform vec3 materialSpecularColorIn;
|
||||
|
||||
out vec4 ShadowCoord;
|
||||
|
||||
@@ -60,7 +65,13 @@ vec4 quatRotate ( in vec4 p, in vec4 q )
|
||||
return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );
|
||||
}
|
||||
|
||||
out vec3 lightDir,normal,ambient;
|
||||
out vec3 lightPos,normal,ambient;
|
||||
out vec4 vertexPos;
|
||||
out vec3 cameraPosition;
|
||||
out float materialShininess;
|
||||
out vec3 lightSpecularIntensity;
|
||||
out vec3 materialSpecularColor;
|
||||
|
||||
|
||||
void main(void)
|
||||
{
|
||||
@@ -69,15 +80,19 @@ void main(void)
|
||||
|
||||
vec4 worldNormal = (quatRotate3( vertexnormal,q));
|
||||
|
||||
normal = normalize(worldNormal).xyz;
|
||||
normal = worldNormal.xyz;
|
||||
|
||||
lightDir = lightDirIn;
|
||||
|
||||
lightPos = lightPosIn;
|
||||
cameraPosition = cameraPositionIn;
|
||||
materialShininess = materialShininessIn;
|
||||
lightSpecularIntensity = lightSpecularIntensityIn;
|
||||
materialSpecularColor = materialSpecularColorIn;
|
||||
|
||||
vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);
|
||||
vec4 vertexPos = MVP* vec4((instance_position+localcoord).xyz,1);
|
||||
|
||||
gl_Position = vertexPos;
|
||||
vertexPos = vec4((instance_position+localcoord).xyz,1);
|
||||
|
||||
vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1);
|
||||
gl_Position = vertexLoc;
|
||||
ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);
|
||||
|
||||
fragment.color = instance_color;
|
||||
|
||||
@@ -13,7 +13,12 @@ static const char* useShadowMapInstancingVertexShader= \
|
||||
"uniform mat4 ProjectionMatrix;\n"
|
||||
"uniform mat4 DepthBiasModelViewProjectionMatrix;\n"
|
||||
"uniform mat4 MVP;\n"
|
||||
"uniform vec3 lightDirIn;\n"
|
||||
"uniform vec3 lightPosIn;\n"
|
||||
"uniform vec3 cameraPositionIn;\n"
|
||||
"uniform mat4 ViewMatrixInverse;\n"
|
||||
"uniform float materialShininessIn;\n"
|
||||
"uniform vec3 lightSpecularIntensityIn;\n"
|
||||
"uniform vec3 materialSpecularColorIn;\n"
|
||||
"out vec4 ShadowCoord;\n"
|
||||
"out Fragment\n"
|
||||
"{\n"
|
||||
@@ -51,7 +56,12 @@ static const char* useShadowMapInstancingVertexShader= \
|
||||
" vec4 temp = quatMul ( q, p );\n"
|
||||
" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n"
|
||||
"}\n"
|
||||
"out vec3 lightDir,normal,ambient;\n"
|
||||
"out vec3 lightPos,normal,ambient;\n"
|
||||
"out vec4 vertexPos;\n"
|
||||
"out vec3 cameraPosition;\n"
|
||||
"out float materialShininess;\n"
|
||||
"out vec3 lightSpecularIntensity;\n"
|
||||
"out vec3 materialSpecularColor;\n"
|
||||
"void main(void)\n"
|
||||
"{\n"
|
||||
" vec4 q = instance_quaternion;\n"
|
||||
@@ -59,13 +69,18 @@ static const char* useShadowMapInstancingVertexShader= \
|
||||
" \n"
|
||||
" vec4 worldNormal = (quatRotate3( vertexnormal,q));\n"
|
||||
" \n"
|
||||
" normal = normalize(worldNormal).xyz;\n"
|
||||
" lightDir = lightDirIn;\n"
|
||||
" \n"
|
||||
" normal = worldNormal.xyz;\n"
|
||||
" lightPos = lightPosIn;\n"
|
||||
" cameraPosition = cameraPositionIn;\n"
|
||||
" materialShininess = materialShininessIn;\n"
|
||||
" lightSpecularIntensity = lightSpecularIntensityIn;\n"
|
||||
" materialSpecularColor = materialSpecularColorIn;\n"
|
||||
" \n"
|
||||
" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n"
|
||||
" vec4 vertexPos = MVP* vec4((instance_position+localcoord).xyz,1);\n"
|
||||
" gl_Position = vertexPos;\n"
|
||||
" vertexPos = vec4((instance_position+localcoord).xyz,1);\n"
|
||||
" \n"
|
||||
" vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1);\n"
|
||||
" gl_Position = vertexLoc;\n"
|
||||
" ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n"
|
||||
" fragment.color = instance_color;\n"
|
||||
" vert.texcoord = uvcoords;\n"
|
||||
|
||||
Reference in New Issue
Block a user