سؤال

I just started learning OpenGL 3.x and I'm trying to implement a basic ADS/Phong shader in OpenGL 4.4.

Stanford Bunny 1 Stanford Bunny 2

Unfortunately I get these weird dark spots underneath this low-poly version of the Stanford Bunny. After using some other models I've come to the conclusion that the culprit cannot be the bunny, so it is probably my shader.

Vertex Shader

#version 330

layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertex_normal;

out vec3 lightIntensity;

uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelMatrix;

// Diffuse
// K REFLECTIVITY, L SOURCE INTENSITY
// a AMBIENT, d DIFFUSE, s SPECULAR
struct Light{
    vec3 position;
    vec3 La;
    vec3 Ld;
    vec3 Ls;
};
uniform Light light;

struct Material{
    float shininess;
    vec3 Ka;
    vec3 Kd;
    vec3 Ks;
};
uniform Material material;

void main(){    
    vec4 vertex = vec4(vertexPosition_modelspace, 1.0f);
    vec4 eyeCoords = modelMatrix * vertex;

    vec3 n = normalize(vertex_normal); // Normal
    vec3 s = normalize(light.position - eyeCoords.xyz); // D tw light
    vec3 v = normalize(eyeCoords.xyz);
    vec3 r = reflect(-s, n);
    float sDotN = max(dot(s, n), 0.0);

    vec3 ambient = light.La * material.Ka;
    vec3 diffuse = light.Ld * material.Kd * sDotN;
    vec3 specular = vec3(0.0f);

    if(sDotN > 0.0f){
        specular = light.Ls * material.Ks * pow(max(dot(r, v), 0.0), material.shininess);
    }

    lightIntensity = ambient + diffuse + specular;

    gl_Position = modelViewProjectionMatrix * vertex;
}

What is causing this and how do I fix it?

هل كانت مفيدة؟

المحلول

Turns out I was passing the vertices instead of the normals - I had completely broken my model loader. All is well now and the above code works if the correct uniforms are passed in!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top