我一直在尝试使平面照明在openGL着色器中工作约两周,我已经尝试了很多教程,但是仍然无法正常工作,这就是为什么我决定在该网站上发布东西的原因,我也进行了搜索继续,也许您可以帮助我。
顶点着色器:
#version 330 core
// vertex shader input \\
in vec3 position;
in vec2 textureCoords;
in vec3 normal;
// vertex shader output \\
out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toCameraVector;
out float visibility;
// uniform variables \\
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;
// fog settings \\
const float density = 0.0012;
const float gradient = 5.0;
void main(void){
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
vec4 positionRelativeToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelativeToCam;
pass_textureCoords = textureCoords * 40.0;
surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
toCameraVector = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz - worldPosition.xyz;
// fog stuff \\
float distance = length(positionRelativeToCam.xyz);
visibility = exp(-pow((distance * density), gradient));
visibility = clamp(visibility, 0.0, 1.0);
}
片段着色器:
#version 330 core
// fragment shader input \\
in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCameraVector;
in float visibility;
// colour output \\
out vec4 out_Colour;
// uniform variables \\
uniform sampler2D textureSampler;
uniform vec3 lightColour;
uniform float shineDamper;
uniform float reflectivity;
uniform vec3 skyColour;
void main(void){
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);
float nDot1 = dot(unitNormal, unitLightVector);
float brightness = max(nDot1, 0.25) + 0.05;
vec3 diffuse = brightness * lightColour;
vec3 unitVectorToCamera = normalize(toCameraVector);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
specularFactor = max(specularFactor, 0.0);
float dampedFactor = pow(specularFactor, shineDamper);
vec3 finalSpecular = dampedFactor * lightColour;
out_Colour = vec4(diffuse, 1.0) * texture(textureSampler, pass_textureCoords) + vec4(finalSpecular, 1.0);
out_Colour = mix(vec4(skyColour,1.0), out_Colour, visibility);
}
如果您知道如何在项目中应用平面着色,请告诉我。
预先感谢
答案 0 :(得分:2)
如果您知道如何在项目中应用平面着色,请告诉我。
对顶点着色器输出使用flat
插值限定符,这些输出用于光线的封闭:
顶点着色器:
flat out vec3 surfaceNormal;
flat out vec3 toLightVector;
flat out vec3 toCameraVector;
片段着色器:
flat in vec3 surfaceNormal;
flat in vec3 toLightVector;
flat in vec3 toCameraVector;
请参见OpenGL Shading Language 3.30 Specification; 4.3.9 Interpolation; page 41
不会被插补为平面的变量。取而代之的是,它将对三角形中的每个片段具有相同的值。此值将来自单个激发顶点,[...]