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;
}