问题
First of all I'm new with computer graphics, openGL and have a basic knowledge of c++ coding. I have struggled with the openGL project like a month and have come to a point where I have to implement shading with Blinn-Phong model. I have implemented calculations in vertex and fragment shaders. There is/are propably some minor error(s) in code because without shading everything works perfectly but after shading parts are added to shaders anything does not happen. I calculate surface normals in fragment shader and also use textures instead of just colors. I also use for every objects (in total 7 objects) own shader.
I would be very happy if someone see right away where I'm doing wrong. I don't get errors anymore so propably the mistake is in implementation.
Here are the code for one objects vertex and fragment shaders.
Vertex shader:
#version 330 core
layout (location=0) in vec3 in_Position;
layout (location=3) in vec2 in_TexCoord0;
// mvpmatrix is the result of multiplying the model, view, and projection matrices
uniform mat4 mvpmatrix;
// Texture coordinate for the fragment shader
out vec2 f_TexCoord0;
out vec3 out_Position;
void main(void)
{
gl_Position = mvpmatrix * vec4(in_Position, 1.0);
out_Position = in_Position;
f_TexCoord0 = in_TexCoord0;
}
Fragment shader:
#version 330 core
uniform sampler2D texture1;
in vec2 f_TexCoord0;
in vec3 out_Position;
layout (location=0) out vec4 fragColor;
uniform vec4 ambientMaterial2, diffuseMaterial2, specularMaterial2;
uniform vec4 ambientLight, diffuseLight, specularLight;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform vec4 lightPosition;
uniform float shininess;
void main(void)
{
vec4 ambientProduct = ambientLight * ambientMaterial2;
vec4 diffuseProduct = diffuseLight * diffuseMaterial2;
vec4 specularProduct = specularLight * specularMaterial2;
vec3 pos = out_Position.xyz;
vec3 nv = cross(dFdx(pos),dFdy(pos));
nv = nv * sign(nv.z);
vec3 L = normalize(lightPosition.xyz - nv);
vec3 E = normalize(-nv);
vec3 H = normalize(L + E);
vec3 N = nv;
float Kd = max(dot(L, N), 0.0);
vec4 diffuse = Kd * diffuseProduct;
vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
if (Kd > 0.0f)
{
float Ks = dot(H, N);
if (Ks > 0.0f)
{
specular = pow(Ks, shininess) * specularProduct;
}
}
vec4 fragColor_lightning = (ambientProduct + diffuse + specular);
vec4 fragColor_texture = texture2D(texture1, vec2(f_TexCoord0.x, f_TexCoord0.y));
fragColor = fragColor_lightning + fragColor_texture;
}
Edit:
Changed shaders:
Vertex shader:
#version 330 core
layout (location=0) in vec3 in_Position;
layout (location=3) in vec2 in_TexCoord0;
// mvpmatrix is the result of multiplying the model, view, and projection matrices
uniform mat4 mvpmatrix;
uniform mat4 modelMat;
// Texture coordinate for the fragment shader
out vec2 f_TexCoord0;
out vec3 viewPos;
void main(void)
{
gl_Position = mvpmatrix * vec4(in_Position, 1.0);
viewPos = (modelMat * vec4(in_Position, 1.0)).xyz;
f_TexCoord0 = in_TexCoord0;
}
Fragment shader:
#version 330 core
uniform sampler2D texture1;
in vec2 f_TexCoord0;
in vec3 viewPos;
layout (location=0) out vec4 fragColor;
uniform vec4 ambientMaterial2, diffuseMaterial2, specularMaterial2;
uniform vec4 ambientLight, diffuseLight, specularLight;
uniform mat4 viewMat;
uniform vec4 lightPosition;
uniform float shininess;
void main(void)
{
vec4 ambientProduct = ambientLight * ambientMaterial2;
vec4 diffuseProduct = diffuseLight * diffuseMaterial2;
vec4 specularProduct = specularLight * specularMaterial2;
vec3 pos = viewPos;
vec3 nv = cross(dFdx(pos),dFdy(pos));
nv = nv * sign(nv.z);
vec3 L = normalize((viewMat*lightPosition).xyz - pos);
vec3 E = normalize(-pos);
vec3 H = normalize(L + E);
vec3 N = normalize(nv);
float Kd = max(dot(L, N), 0.0);
vec4 diffuse = Kd * diffuseProduct;
vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
if (Kd > 0.0f)
{
float Ks = pow(max(dot(H, N),1.0),shininess);
specular = Ks * specularProduct;
}
vec4 fragColor_lightning = (ambientProduct + diffuse + specular);
vec4 fragColor_texture = texture2D(texture1, vec2(f_TexCoord0.x, f_TexCoord0.y));
fragColor = fragColor_lightning + fragColor_texture;
}
回答1:
You have to split the model view projection matrix to a model view matrix and a projection matrix. With the model view matrix the view position can be calculated and passed to the fragment shader. With the projection matrix the clip space position can be calculated.
#version 330 core
layout (location=0) in vec3 in_Position;
layout (location=3) in vec2 in_TexCoord0;
uniform mat4 modelview;
uniform mat4 projection;
out vec2 f_TexCoord0;
out vec3 viewPos;
void main(void)
{
vec4 pos = modelview * vec4(in_Position, 1.0);;
f_TexCoord0 = in_TexCoord0;
viewPos = pos.xyz;
gl_Position = projection * pos;
}
For calculating a blinn phong light model see also GLSL fixed function fragment program replacement
Change your code somehow like this:
in vec3 viewPos;
uniform vec4 lightPosition; // this has to be a view space position
uniform float shininess;
void main()
{
....
vec3 N = cross(dFdx(viewPos.xyz),dFdy(viewPos.xyz));
N = normalize(N * sign(N.z));
vec3 L = normalize(lightPosition.xyz - viewPos.xyz);
vec3 E = normalize(-viewPos.xyz);
vec3 H = normalize(L + E);
float Kd = max(dot(L, N), 0.0);
vec4 diffuse = Kd * diffuseProduct;
vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
if (Kd > 0.0f)
{
float Ks = max(0.0, dot(H, N));
specular = pow(Ks, shininess) * specularProduct;
}
.....
}
Edit:
When you do viewPos = (modelMat * vec4(in_Position, 1.0)).xyz;
then viewPos
is not a view position, but a model position. To calculate a view position you have to transform by the model matrix and the view matrix: viewPos = (viewMat * modelMat * vec4(in_Position, 1.0)).xyz;
.
#version 330 core
layout (location=0) in vec3 in_Position;
layout (location=3) in vec2 in_TexCoord0;
// mvpmatrix is the result of multiplying the model, view, and projection matrices
uniform mat4 mvpmatrix;
uniform mat4 viewMat;
uniform mat4 modelMat;
// Texture coordinate for the fragment shader
out vec2 f_TexCoord0;
out vec3 viewPos;
void main(void)
{
gl_Position = mvpmatrix * vec4(in_Position, 1.0);
viewPos = (viewMat * modelMat * vec4(in_Position, 1.0)).xyz;
f_TexCoord0 = in_TexCoord0;
}
Further I recommend to multiply the color of the texture by the light color:
fragColor = vec4(fragColor_lightning.rgb * fragColor_texture.rgb, fragColor_texture.a);
来源:https://stackoverflow.com/questions/50875639/opengl-blinn-phong-model-implemented-in-shaders-give-wrong-result