Home > OS >  My geometry shader won't compile (GLSL, openGL, c )
My geometry shader won't compile (GLSL, openGL, c )

Time:02-24

When I compile the shader using just the vertex and fragment, it works, but not when I add the geometry shader. I've tried many variations of the geometry shader but none of them work.

If my code is wrong then how would I make a geometry shader that "does nothing", using the code I have?

If my code is right, then what could I be doing wrong outside of the shader?

My github repo for reference:

My vertex shader:

#version 400

in vec3 vertex;

void main() {
    gl_Position = vec4(vertex, 1);
}

My geometry shader:

#version 400

layout(points) in;
layout(points, max_vertices=1) out;

void main() {
    gl_Position = gl_in[0].gl_Position;
    EmitVertex();
    EndPrimitive();
}

My fragment shader:

#version 400

uniform vec4 color = vec4(1, 0, 0, 1);

void main() {
    gl_FragColor = color;
}

CodePudding user response:

you are missing calls to: glCompileShader, which should be between your calls to glShaderSource & glAttachShader (IIRC, Nvidia allows you to skip that step, but it's not guaranteed to work against the GL spec). You don't seem to be checking for shader compilation and/or linker errors? Chances are that will provide you the info you need.

The big problem though, is that the shaders you've listed above are called "Lines", and from I can see, you are specifying the geometry in the draw calls with GL_LINES.

This somewhat conflicts with your shaders input specifier....

layout(points) in;

This is a problem. If you want to convert the input primitive to points, then you can do:

#version 400

layout(lines) in;
layout(points, max_vertices=2) out;

void main() {
    gl_Position = gl_in[0].gl_Position;
    EmitVertex();
    
    gl_Position = gl_in[1].gl_Position;
    EmitVertex();

    EndPrimitive();
}

CodePudding user response:

Ok, I figured out what was wrong, using robthebloke's code I made this, which works:

#version 400

layout(lines) in;
layout(line_strip, max_vertices=2) out;

void main() {
    gl_Position = gl_in[0].gl_Position;
    EmitVertex();

    gl_Position = gl_in[1].gl_Position;
    EmitVertex();

    EndPrimitive();
}
  • Related