0

This glsl shader compiles fine, but when I try to activate it with glUseProgram(); opengl gives me an invalid value error:

@vert
#version 150
uniform mat4 projectionmodelview_matrix_;
in vec3 global_position_;

void main(void) {
    gl_Position = projectionmodelview_matrix_ * vec4(global_position_, 1.0);
    EmitVertex();

    gl_Position = projectionmodelview_matrix_ * vec4(global_position_, 1.0);
    EmitVertex();

    gl_Position = projectionmodelview_matrix_ * vec4(global_position_, 1.0);
    EmitVertex();
    EndPrimitive();
}

@frag
#version 150
out vec4 out_color_;
void main(void) {
    out_color_ = vec4(1.0, 0.0, 0.0, 1.0);
}

However, if I remove the parts which emit vertices, it works. What am I doing wrong?

Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90

1 Answers1

2

Easy answer, a vertex shader cannot use EmitVertex and EndPrimitive. A vertex shader is invoked once for each vertex passed through the pipeline and puts out the varying values for that single vertex. It cannot create or discard any vertices or primitives (it doesn't even have a reasonable notion of primitives at all). What you need for this is a geometry shader (though your code doesn't make so much sense at all, putting out a triangle that has the same vertex for each corner, so there wouldn't be anything rasterized at all).

So given that this vertex shader is plain illegal, I actually doubt the truth of the statement "This glsl shader compiles fine", and the GL_INVALID_VALUE error seems reasonable. Though GL_INVALID_VALUE would actually be thrown for a program that wasn't generated by the GL (but that may also be due to some wrapper framework just deleting a not successfully compiled and linked program, strange though).

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
  • Aha, a geometry shader differs from a vertex shader? I always though a geometry shader was a vertex shader which could also duplicate\emit vertices :) – Viktor Sehr Jul 11 '13 at 15:15
  • 2
    @ViktorSehr No, the [geometry shader](http://www.opengl.org/wiki/Geometry_Shader) comes *after* the vertex shader and is invoked once for each primitive (after this primitive's vertices have run through the vertex shader), and can generate new primtives (or none at all) from that primitive. – Christian Rau Jul 11 '13 at 15:16
  • How could I have missed something like this :) – Viktor Sehr Jul 11 '13 at 15:18