2
\$\begingroup\$

Im trying to render a model with anialiasing using OpenGL and SDL, but when I do I get strange artifacts.

When I render with antialiasing disabled everything looks fine. Aliasing

When antialiasing is enabled I get white dots in edges of the model. AntiAliasing

I'm using a Nvidia Geforce 320M graphics card and enabling the display as well as the multisampling with this code

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
glEnable(GL_MULTISAMPLE);

This is the fragment shader:

#version 330

#define MAX_NUM_LIGHTS 4

in vec3 normal0;
in vec3 pos;

out vec4 FragColor;

struct LightSource
{
    vec4 position;
    vec4 ambientColor;
    vec4 diffuseColor;
    vec4 specularColor;

    // attenuation parameters
    float attConstant;
    float attLinear;
    float attQuadratic;
};

struct Material
{
    vec4 ambientColor;
    vec4 diffuseColor;
    vec4 specularColor;
    float shininess;
};

uniform LightSource light[MAX_NUM_LIGHTS];
uniform Material material;
uniform vec3 viewPos;
uniform int numOfLights;

// function declarations
vec4 calcDirLight(LightSource light, Material material, vec3 normal, vec3 viewDirection);
vec4 calcPointLight(LightSource light, Material material, vec3 normal, vec3 fragPos, vec3 viewDirection);

void main()
{
    vec3 viewDirection = normalize( viewPos - pos );
    vec4 result = vec4(0.0f);

    // Loop over all lights
    for(int i = 0; i < numOfLights; i++)
    {
        // Directional light
        if( light[i].position.w == 0.0f )
        {
            result += calcDirLight( light[i], material, normal0, viewDirection);
        }
        // Point light
        else if ( light[i].position.w == 1.0f )
        {
            result += calcPointLight( light[i], material, normal0, pos, viewDirection);
        }
    }

    FragColor = result;
}

vec4 calcDirLight(LightSource light, Material material, vec3 normal, vec3 viewDirection)
{
    // Properties
    vec3 lightDirection = normalize( vec3(light.position) );
    vec3 reflectDirection = reflect( -lightDirection, normal);

    // Ambient component
    vec4 ambientComponent = light.ambientColor * material.ambientColor;

    // Diffuse component
    float diffuseReflection = clamp(dot(normal,lightDirection), 0.0, 1.0);
    vec4 diffuseComponent = diffuseReflection * light.diffuseColor * material.diffuseColor;

    // Specular component
    float specularReflection = pow( max( dot( viewDirection,reflectDirection ), 0.0 ), material.shininess );
    vec4 specularComponent = specularReflection * light.specularColor * material.specularColor;

    return ( ambientComponent + diffuseComponent + specularComponent);
}

vec4  calcPointLight(LightSource light, Material material, vec3 normal, vec3 fragPos, vec3 viewDirection)
{
    // Properties
    vec3 lightDirection = normalize( vec3(light.position) - fragPos );
    vec3 reflectDirection = reflect( -lightDirection, normal);
    float distance = length( vec3(light.position) - fragPos );
    float attenuation = 1.0f / ( light.attConstant + (light.attLinear * distance) + (light.attQuadratic * (distance * distance)) );

    // Ambient component
    vec4 ambientComponent = light.ambientColor * material.ambientColor * attenuation;

    // Diffuse component
    float diffuseReflection = clamp(dot(normal,lightDirection), 0.0, 1.0);
    vec4 diffuseComponent = diffuseReflection * light.diffuseColor * material.diffuseColor * attenuation;

    // Specular component
    float specularReflection = pow( max( dot( viewDirection,reflectDirection ), 0.0 ), material.shininess );
    vec4 specularComponent = specularReflection * light.specularColor * material.specularColor * attenuation;

    return ( ambientComponent + diffuseComponent + specularComponent);
}

Can anyone point me in the direction of what might be the problem?

\$\endgroup\$
  • 1
    \$\begingroup\$ I'd like to see your fragment shader code. I think it appears because in some samples the specular calculation blows up. \$\endgroup\$ – akaltar Feb 20 '15 at 14:15
  • \$\begingroup\$ @akaltar There, fragment shader present :) (I'm new to SE so I'm unsure if you get a notification of the edit by itself) \$\endgroup\$ – Kaitlabben Feb 20 '15 at 14:45
  • \$\begingroup\$ I see no problem with your shader. Did this occur with other test models? for example a box? If you move closer will they become more apparent, or stay at one-pixel size? Could this be appearing in the non-multisampled version just perhaps at a lower rate? Which type of lights cause this? \$\endgroup\$ – akaltar Feb 20 '15 at 16:02
  • \$\begingroup\$ @akaltar This has occurred with other complex models as well. I'm not able to get to my computer right now but I'll check that out your questions as soon as I can. \$\endgroup\$ – Kaitlabben Feb 20 '15 at 20:36
  • 1
    \$\begingroup\$ @patryk.beza Because normals are interpolated over the triangle component-wise when transferred from vertex to fragment shader, their lengths may change, which messes with the way we use their unit-length properties. Using centroid interpolation(I guess) solves this, because it is a type of interpolation that keeps length while interpolating. \$\endgroup\$ – akaltar Nov 18 '16 at 18:16
2
\$\begingroup\$

As pointed out to me, normalizing normals in the vertex shader and then passing them to the fragment shader doesn't ensure they are normalized after being passed. By only passing the normals in the vertex shader and then normalizing them in the fragment shader the problem was fixed.

Another way to fix the error is centroid attribute interpolation between the vertex and fragment shader. This might only be considered a quick-fix however, since the real issue was not normalizing the normals in the fragment shader.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.