Joey Dewd
Joey Dewd

Reputation: 1824

OpenGL Diffuse Shader, Object not showing

I have an object from Blender rendered into my scene and the normals are loaded correctly. I´m using Gourrad Diffuse shading passing the ModelViewProjectionMatrix (CameraToClip * ModelToCamera) to the shader and a 3x3 version of the ModelToCameraMatrix to multiply with the normals.

However, When I'm setting the output color to DiffuseColor * Angle my entire object (a cube in my case) just dissapears (not even black lines, nothing shows up on my gray background). When I replace the coloroutput to

theColor = Vec4(1.0f, 1.0f, 1.0f, 1.0f); 

it displays my obj in white without any shading. I am quite curious as to why I'm not seeing anything? Is there something wrong with my shader or with the way I pass my uniforms?

Vertex Shader:

#version 330

layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;

smooth out vec4 theColor;

uniform mat4 modelViewProjectionMatrix;

uniform vec3 lightDir;
uniform mat3 normalModelToCameraMatrix;

void main()
{
    gl_Position = modelViewProjectionMatrix * position;

    vec3 normCamSpace = normalize(normalModelToCameraMatrix * normal);

    vec4 diffuseColor = vec4(0.6f, 0.7f, 0.8f, 1.0f);

    float angle = dot(normCamSpace, lightDir);
    angle = clamp(angle, 0, 1);

    theColor = diffuseColor  * angle;
}

Render method:

stack.Translate(glm::vec3(0.0f, 0.0f, 3.0f));

glm::vec4 lightDirCameraSpace = stack.Top() * glm::vec4(1.5f, 1.0f, 0.0f, 0.0f);
glUniform3fv(lightDirUnif, 1, glm::value_ptr(lightDirCameraSpace));
glm::mat3 normalMatrix(stack.Top());
glUniformMatrix3fv(normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normalMatrix));


// Draw Blender object
glBindVertexArray(this->load.vertexArrayOBject);

glm::mat4 modelViewProjectionMatrix = cameraToClipMatrix * stack.Top();
glUniformMatrix4fv(modelViewProjectionMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelViewProjectionMatrix));


glDrawElements(GL_TRIANGLES, this->load.indicesVec.size(), GL_UNSIGNED_INT, 0);

I can't see what's wrong with the shader nor the rendering part.


Update When using glGetError it seems that I continuously get error code 1285 which seems to be related to "Out of Memory" problems. However, I'm not using textures of any kind so I'm kind of confused as to why this is happening?

Upvotes: 0

Views: 600

Answers (1)

Bahbar
Bahbar

Reputation: 18015

My guess is that you're:

  1. enabling blending and/or alpha test
  2. multiplying your angle to the alpha channel
  3. getting a value that is not actually what you want for angle, ending with 0 for all the important angle cases

So alpha=0, the polygon is all transparent/alpha-tested.

What you should do first is make sure alpha is always 1.f, and only modulate the rgb. You could also turn off blending/alpha-test.

Then, fix the math that actually compute the angle variable as you want it.

Upvotes: 1

Related Questions