user2970139
user2970139

Reputation: 559

Pass-through geometry shader

I just started with OpenGL in conjunction with Qt. I try to use a geometry shader to change the width of lines, but even a simple pass-through geometry shader results in an empty screen.

Here is (an excerpt of) my code:

line_shader.addShaderFromSourceFile(QGLShader::Vertex, ":/shaders/lines.v.glsl");
line_shader.addShaderFromSourceFile(QGLShader::Fragment, ":/shaders/lines.f.glsl");
line_shader.addShaderFromSourceFile(QGLShader::Geometry, ":/shaders/lines.g.glsl");
line_shader.setGeometryInputType(GL_LINES);
line_shader.setGeometryOutputType(GL_LINES);
line_shader.setGeometryOutputVertexCount(100);
line_shader.link();
line_shader.bind();


const float data[] = {
0, 0, 0, 3,    0.8, 0, 0, 0.5,      1, 0, 0, 3,    0.8, 0, 0, 0.5,
0, 0, 0, 3,    0, 0.8, 0, 0.5,      0, 1, 0, 3,    0, 0.8, 0, 0.5,
0, 0, 0, 3,    0, 0, 0.8, 0.5,      0, 0, 1, 3,    0, 0, 0.8, 0.5,
};
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); 


glUseProgram(line_shader.programId());

glBindBuffer(GL_ARRAY_BUFFER, vbo);

GLint a_vertex = line_shader.attributeLocation("a_vertex");
GLint a_color = line_shader.attributeLocation("a_color");
line_shader.enableAttributeArray(a_vertex);
line_shader.enableAttributeArray(a_color);

glVertexAttribPointer(a_vertex, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
glVertexAttribPointer(a_color, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(4 * sizeof(GLfloat)));

glDrawArrays(GL_LINES, 0, 6);

Vertex shader:

attribute vec4 a_vertex;
attribute vec4 a_color;
uniform mat4 u_pmv_matrix;

void main(void) {
  gl_Position = u_pmv_matrix * vec4(a_vertex.x, a_vertex.y, a_vertex.z, 1);
}

Geometry shader:

void main() {
  for (int i=0; i<gl_VerticesIn; ++i) {
    gl_Position = gl_PositionIn[i];
    EmitVertex();
  }
  EndPrimitive();
}

Fragment shader:

varying vec4 v_color;

void main(void) {
  gl_FragColor = vec4(1, 0, 0, 1);
}

If I remove line_shader.addShaderFromSourceFile(QGLShader::Geometry, ":/shaders/lines.g.glsl"); from the code, everything works as expected.

What am I doing wrong here?

EDIT: I use OS X 10.9, Qt 5.2.1.

Upvotes: 1

Views: 869

Answers (1)

Sez
Sez

Reputation: 1293

Are geometry shaders supported by an extension that is not being loaded/resolved on your platform? I believe they're optional, but Qt tries to load them (source).

Perhaps they're named something different in your implementation. If you have to manually resolve them this KDAB article may help. There's code there to list the extensions Qt knows about:

// Query extensions
QList extensions = m_context->extensions().toList();
std::sort( extensions );
qDebug() << "Supported extensions (" << extensions.count() << ")";
foreach ( const QByteArray &extension, extensions )
   qDebug() << "    " << extension;

It might be that the extension is called something other than what Qt expects.

Also obvious, but I guess you have a shader tool on your platform and have been able to run that shader? I use MacOSX for most of my development and unfortunately the "OpenGL Shader Builder" tool only supports fragment and vertex shaders. I'm not sure what tools do support geometry shaders but I guess you've arrived at that shader and verified it somehow.

Upvotes: 1

Related Questions