PDA

View Full Version : Simple Geometry Shader



Ottaz
12-08-2010, 12:55 PM
I'm just trying to get a program to work using a simple geometry shader and a vbo.

The output i am getting is as if the program is ignoring the geometry shader.

I'm including all the relevant code where the error might occur.

<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. (Vertex Shader) <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">
attribute vec4 vertex;

void main()
{
gl_Position = gl_ModelViewMatrix * vertex;
}
[/QUOTE]</div>

<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. (Geometry Shader) <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">
#extension GL_EXT_geometry_shader4 : enable

void main()
{
gl_Position = gl_PositionIn[0];
gl_Position.y += 2.0;
gl_Position = gl_ProjectionMatrix * gl_Position;
EmitVertex();
EndPrimitive();
}
[/QUOTE]</div>

<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. (Fragment Shader) <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">
void main()
{
gl_FragColor = vec4(0.0,0.0,1.0,1.0);
}
[/QUOTE]</div>

The shader files could not be simpler.
Here is the relevant code in the main file.

<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. (Relevant main code) <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">
GLuint vert_attr = 0;
...
void renderBuffer(GLuint vbo)
{
if (!pUSE) {
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_POINTS, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
else {
glUseProgram(ptest);
vert_attr = glGetAttribLocation(ptest, "vertex");
glEnableClientState(GL_VERTEX_ARRAY);
glEnableVertexAttribArray(vert_attr);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(vert_attr, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glDrawArrays(GL_POINTS, 0, 3);
glDisableVertexAttribArray(vert_attr);
glUseProgram(0);
}
}

...
GLuint setShader3(char *vert, char *frag, char *geom)
{
char *vs = NULL, *fs = NULL, *gs = NULL;

v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
g = glCreateShader(GL_GEOMETRY_SHADER_EXT);

vs = textFileRead(vert);
fs = textFileRead(frag);
gs = textFileRead(geom);

const char * vv = vs; const char * ff = fs; const char * gg = gs;

glShaderSource(v, 1, &amp;vv,NULL);
glShaderSource(f, 1, &amp;ff,NULL);
glShaderSource(g, 1, &amp;gg,NULL);

free(vs); free(fs); free(gs);

glCompileShader(v); glCompileShader(f); glCompileShader(g);

printShaderInfoLog(v);
printShaderInfoLog(f);
printShaderInfoLog(g);

GLuint p = glCreateProgram();
glAttachShader(p,v);
glAttachShader(p,f);
glAttachShader(p,g);

glProgramParameteriEXT(p,GL_GEOMETRY_INPUT_TYPE_EX T,GL_POINTS);
glProgramParameteriEXT(p,GL_GEOMETRY_OUTPUT_TYPE_E XT,GL_POINTS);
glProgramParameteriEXT(p,GL_GEOMETRY_VERTICES_OUT_ EXT,4);

glLinkProgram(p);
printProgramInfoLog(p);

return p;
}
[/QUOTE]</div>

In the display function, glColor is set to red. So toggling the boolean pUSE will enable/disable using the shader program which re-colors the vertices in blue and (is supposed to) translate each point.

When the shader program is enabled the 3 points are re-colored correctly but no translation is taking place and no matter how much i increase the value in the geometry shader file, the points stay put.

What am I doing wrong here?

NB = I am using an old version for OpenGL (2.x) and GLSL (1.2) because I have to be able to run the program on a campus computer that doesn't have the new lovely versions.

arekkusu
12-09-2010, 11:04 AM
What are your modelview and projection matrices set to?
"2.0" might be unnoticeably tiny in your world-space coordinates.

Ottaz
12-09-2010, 12:52 PM
Yuh know what I like about this forum? They give you time to figure things out on your own since the amount of help ppl need is exponentially larger than the amount of help gurus and others can give.

So I hope that others with a similar problem will view this thread to see the simple addition that needs to be included in the geometry file in order for this to work...

At the top of the Geometry Shader file it is IMPERATIVE that you include the following 3 lines.



#version 120
#extension GL_EXT_gpu_shader4: enable
#extension GL_EXT_geometry_shader4 : enable


I only had the last line which I think is why my geometry shader refused to work.

As I mentioned before, these lines are for ppl running older versions of OpenGL and GLSL.

Happy Coding.