我一直在尝试使平面照明在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);
}
如果您知道如何将平面着色应用于我的项目,请告诉我.
提前致谢
解决方法:
If you know how i could apply flat shading to my project please let me know.
将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
A variable qualified as flat will not be interpolated. Instead, it will have the same value for every fragment within a triangle. This value will come from a single provoking vertex, […]