Frage

I have a simple geometry shader which I am using to calculate per face normals.

VertexShader

#version 150

in vec3 in_Position;

uniform mat4 modelMat;
uniform vec3 scale; 


void main(void) 
{
    // scale the verts
    vec3 scaledPosition = vec3( in_Position.x *  scale.x,
                                in_Position.y *  scale.y,
                                in_Position.z *  scale.z );

    gl_Position = modelMat * vec4(scaledPosition, 1.0f);
}

Geometry Shader

#version 150 core

layout(triangles) in;
layout (triangle_strip) out;
layout (max_vertices=3) out;

uniform mat4 ProjectionMat; 
uniform mat4 ViewMat;

out vec3 ex_Normal;

 void main()
{
    // calculate the normal

    vec3 d1 = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
    vec3 d2 = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
    vec3 normal = normalize(cross(d1,d2));
    for(int i = 0; i < gl_in.length(); i++)
    {
        gl_Position = ProjectionMat * ViewMat * gl_in[i].gl_Position;
        ex_Normal = normal;
        EmitVertex();
    }
    EndPrimitive();
}

Fragment Shader

#version 150

in  vec3 ex_Normal;

out vec4 v_FragColour;

uniform vec3 colour;
uniform vec3 lightDir;


void main(void) 
{
    vec3 diffuse = vec3(0.2, 0.2, 0.2);
    vec3 lightColor = vec3(1.0, 1.0, 1.0);
    const vec3 AMBIENT = vec3(0.1, 0.1, 0.1);


    // normalize the fragment normal and camera direction
    vec3 diffuseC = clamp( diffuse * (dot(lightDir,ex_Normal) + 1.0f)/2.0f  , 0.0, 1.0 ) ;
    v_FragColour =  vec4(clamp(colour.rgb * (diffuseC + AMBIENT), 0.0, 1.0), 1.0f);

}

All appears to pass through fine, and most of the geometry output is exactly as I expected, however I get these random primitives popping in and out stretched across my mesh, they sometimes even join from one mesh to the next, i.e from different draw calls.

I am thinking it must be something to do with outputting a triangle strip from the geometry shader, but I am forcing a single primitive at a time out of the geometry shader by using EndPrimitve(); If anyone has any ideas or clues on why this might be, or in what ways I can go about debugging this please let me know.

The draw is done using glDrawElements();

EDIT PROGRESS:

Right So some Progress has been made. When I don't use the normal data output from the geometry shader in the fragment shader the problem goes away, my fragment shader now looks like:

void main(void) 
{
    vec3 diffuse = vec3(0.2, 0.2, 0.2);
    vec3 lightColor = vec3(1.0, 1.0, 1.0);
    const vec3 AMBIENT = vec3(0.1, 0.1, 0.1);


    // normalize the fragment normal and camera direction
    //vec3 diffuseC = clamp( diffuse * (dot(lightDir,ex_Normal) + 1.0f)/2.0f  , 0.0, 1.0 ) ;
    //v_FragColour =  vec4(clamp(colour.rgb * (diffuseC + AMBIENT), 0.0, 1.0), 1.0f);
    v_FragColour =  vec4(0.0f,0.5f,1.0f,0.5f);

}

However if I uncomment the line:

//vec3 diffuseC = clamp( diffuse * (dot(lightDir,ex_Normal) + 1.0f)/2.0f  , 0.0, 1.0 );

The problem returns even though the output of this calculation is never used.

I'm guessing this has something to do with the way I am passing info from the geometry shader to the fragment shader for each vertex.

Any Ideas ??

War es hilfreich?

Lösung

This is a geometry shader driver bug. It took me quite a while to find that out: I've had the same on Mac OSX 10.8 with an AMD Radeon HD HD6750M. Switching to the internal Intel HD Graphics 3000 (using gfxCardStatus) solves the problem but is of course much slower and doesn't support multi-monitor. Finally I upgraded to Mac OSX 10.9 Developer Preview 4 and the bug seems to be gone for good.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top