2276人阅读 评论(0)

Vertex 顶点本来就是vec4（vec4(x,y,z,w)）。而Direction 方向是vec3。

Perspective(fovy,ratio,znear,zfar)这个函数得到的深度值z/w的增长不是线性的。离znear越近深度值z/w越精确。

CameraPostion为光源的位置，因为我们计算的是进过世界坐标变换后的顶点的位置到光源的位置。depth 就是两者之间的距离。

#version 330
layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Normal;
layout (location = 2) in vec2 TexCoord;

uniform mat4 matModel;
uniform mat4 matView;
uniform mat4 matProjection;
uniform mat4 matMVP;
uniform vec4 CameraPosition;

out float depth;

void main()
{
//基本变换
//MVP * pos = matProjection * matView * matModel * vec4(Position, 1.0);
vec4 WorldPos = matModel * vec4(Position,1.0);
gl_Position = matMVP * vec4(Position, 1.0);
//深度值
depth = distance(CameraPosition.xyz,WorldPos.xyz);
}

#version 330
in vec2 TexCoord0;
in vec3 Normal0;
in vec4 FinalLighting;

in float depth;
out vec4 FragColor;

void main()
{
FragColor = vec4(depth ,depth/255 ,depth/65535 ,1.0);//深度图，自定义深度描述
}

#version 330
layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Normal;
layout (location = 2) in vec2 TexCoord;

struct LightInfo{
vec4 lightPosition;
vec3 lightDirection;
vec3 lightIntensity;
vec3 lightAmbient;
vec3 lightAttenuation;
};

struct MaterialInfo{
vec4 mtlEmssive;
vec4 mtlReflectionAmbient;
vec4 mtlReflectionDiffuse;
vec4 mtlReflectionSpecular;
float mtlSpecularShiness;
vec4 mtlOpacity;
};
uniform LightInfo light0;
uniform MaterialInfo mtl0;
//vec3 is for direciton,vec4 is for position;
//in variable should use vec3 than vec4,shorter first.so data input Postion is using vec3.

uniform mat4 matModel;
uniform mat4 matView;
uniform mat4 matProjection;
uniform mat4 matMVP;
uniform vec4 CameraPosition;

out vec2 TexCoord0;
out vec3 Normal0;
out vec4 FinalLighting;

out vec2 depthTexCoord;
out float lengthToLight0;

void main()
{
//基本变换
//MVP * pos = matProjection * matView * matModel * vec4(Position, 1.0);
vec4 WorldPos = matModel * vec4(Position,1.0);
gl_Position = matMVP * vec4(Position, 1.0);
TexCoord0 = TexCoord;
Normal0 = (matModel * vec4(Normal,0.0)).xyz;
vec3 l0dir = (matModel * vec4(light0.lightDirection,0.0)).xyz;
//要用的变量，避免重复计算
float angle =dot(Normal0, normalize(-l0dir));
vec3 lightReflect = normalize(reflect(l0dir, Normal0));
float lengthToEye = distance(CameraPosition,WorldPos);
//阴影计算
vec4 light0pos = (matShadowMapViewProjection * WorldPos);// x/w,y/w,z/w (-1,1)
depthTexCoord = (light0pos.xy / light0pos.w + vec2(1.0,1.0))*0.5;//世界变换后顶点对应的深度纹理坐标
lengthToLight0 = distance(light0.lightPosition,WorldPos) ;// 世界变换后顶点到光源的位置
//环境光照
vec3 directionToEye = normalize((CameraPosition - WorldPos).xyz);
vec4 la = vec4(light0.lightAmbient,1.0) * mtl0.mtlReflectionAmbient;
//漫射光照
vec4 ld = vec4(0.0,0.0,0.0,0.0);
if(angle > 0)
ld = vec4(light0.lightIntensity,1.0) * mtl0.mtlReflectionDiffuse * angle;
//镜面光照
vec4 ls = vec4(0.0,0.0,0.0,0.0);
float specularFactor = dot(directionToEye,lightReflect);
if(specularFactor > 0 && angle > 0)
ls = vec4(light0.lightIntensity,1.0) * mtl0.mtlReflectionSpecular * pow(specularFactor,mtl0.mtlSpecularShiness);
//最终光照
float attenuationFactor = 1.0 / (light0.lightAttenuation.x + light0.lightAttenuation.y * lengthToEye + pow(light0.lightAttenuation.z * lengthToEye,2));
FinalLighting = la + mtl0.mtlEmssive + attenuationFactor * (ld + ls);
}

#version 330
in vec2 TexCoord0;
in vec3 Normal0;
in vec4 FinalLighting;

in vec2 depthTexCoord;
in float lengthToLight0;

uniform int TextureCount;
uniform sampler2D Sampler0;
uniform sampler2D SamplerDepthMap;

out vec4 FragColor;

void main()
{
float factorShadow = 1.0;
vec4 vd = texture(SamplerDepthMap,depthTexCoord);//纹理采样的范围最终为[0,1] value<0 的一律为0，value>1 的一律为1
float depth = vd.x + vd.y * 255 + vd.z * 65535;//自定义的深度描述
if (lengthToLight0 > depth)//阴影判断
if(TextureCount == 0)
{
FragColor = FinalLighting * factorShadow;
}
else if(TextureCount == 1)
{
FragColor = texture(Sampler0, TexCoord0.xy) * FinalLighting * factorShadow;
}
//gl_FragDepth = Depth;
}

0
1

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：17059次
• 积分：452
• 等级：
• 排名：千里之外
• 原创：19篇
• 转载：1篇
• 译文：0篇
• 评论：3条
阅读排行
评论排行
最新评论