i have a c++ opengl app - i import a model using assimp, and i try to render it, and it…
- User
i have a c++ opengl app - i import a model using assimp, and i try to render it, and it renders correctly, but the shader seems to be wrong as it is displayed as black, and it shouldnt. here is my vertex shader: #version 330
uniform mat4 P; uniform mat4 V; uniform mat4 M; uniform sampler2D tex1s; uniform sampler2D tex2s; uniform mat4 modelMatrix;
in vec3 vertex; in vec2 texCoords; in vec3 normal; in vec3 tangent; in vec3 bitangent;
out vec3 fragPos; out vec2 texCoordsOut; out vec3 normalOut; out vec3 tangentOut; out vec3 bitangentOut;
void main(){ fragPos = vec3(M * vec4(vertex, 1.0)); normalOut = vec3(transpose(inverse(M))) * normal; texCoordsOut = texCoords; tangentOut = vec3(transpose(inverse(M))) * tangent; bitangentOut = vec3(transpose(inverse(M))) * bitangent; gl_Position = P * V * M * vec4(fragPos, 1.0); } here is my fragment shader: #version 330
uniform vec3 diffuseColor; uniform float shininess; uniform vec3 emissiveColor; uniform float opacity; uniform sampler2D tex1s; uniform sampler2D tex2s; uniform vec3 lightColor; uniform vec3 lightDirection;
in vec3 normalOut; in vec3 fragPos; in vec2 texCoordsOut; in vec3 tangentOut; in vec3 bitangentOut; out vec4 Color;
void main() { vec3 normaln = normalize(normalOut); vec3 bitangentn = normalize(bitangentOut); vec3 tangentn = normalize(tangentOut);
codevec3 normalMap = texture(tex1s, texCoordsOut).rgb; normalMap = normalize(normalMap * 2.0 - 1.0); mat3 TBN = mat3(tangentn, bitangentn, normaln); vec3 transformedNormal = normalize(TBN * normalMap); // Diffuse Lighting float diffuseFactor = max(dot(transformedNormal, -lightDirection), 0.0); vec4 texColor = texture(tex2s, texCoordsOut); vec3 diffuse = diffuseColor * texColor.rgb * lightColor * diffuseFactor; // Emissive Lighting vec3 emissive = emissiveColor; // Final Color Calculation vec3 finalColor = diffuse + emissive; // shininess is between 0 and 256 float specularFactor = pow(max(dot(transformedNormal, -lightDirection), 0.0), shininess); vec3 specular = vec3(1.0, 1.0, 1.0) * specularFactor; finalColor += specular; // Apply opacity finalColor *= opacity; Color = vec4(finalColor, 1.0); - Assistant
Your code looks mostly fine, but there are a couple of minor issues that might be causing the black rendering.
- glUniformMatrix3fv for diffuseColor and emissiveColor should be glUniform3fv
In your main code, change the following lines:
cppglUniformMatrix3fv(sp->u("diffuseColor"), 1, false, glm::value_ptr(allMaterialData[0].diffuseColors)); glUniformMatrix3fv(sp->u("emissiveColor"), 1, false, glm::value_ptr(allMaterialData[0].emissiveColors));to
cpp - User
how do i ensure a good relation between light orientation and camera position?
- Assistant