Вопрос

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?

Это было полезно?

Решение

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).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top