无法在 java 和 openGL 中使用平面照明
Can't get flat lighting in java and openGL to work
我已经尝试让平面照明在我的 openGL 着色器中工作大约两周了,我已经尝试了很多教程但仍然无法让它工作,这就是为什么我决定 post 的原因这个网站我也搜索过,也许你可以帮助我。
顶点着色器:
#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, [...]
我已经尝试让平面照明在我的 openGL 着色器中工作大约两周了,我已经尝试了很多教程但仍然无法让它工作,这就是为什么我决定 post 的原因这个网站我也搜索过,也许你可以帮助我。
顶点着色器:
#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, [...]