PDA

View Full Version : Geometry shaders



debonair
01-31-2013, 03:17 AM
Hi,
I have just started with geometric shaders and stuck-up at basics only :(
I am trying to draw GL_TRIANGLE_STRIP.

My vertex array:




const GLfloat data[] =
{
-0.9f, -0.2f,
-0.9f, -0.9f,
-0.2f, -0.9f,
-0.2f, -0.2f,

-0.1f,-0.2f,
-0.1f,-0.9f,
0.4f,-0.9f,
0.4f,-0.2f,

0.5, -0.2,
0.5, -0.9,
0.8, -0.9,
0.8, -0.2

};



Vertex shader:


in vec4 v_color;
flat varying vec4 out_color;
in vec2 Position;

void main()
{
gl_Position = vec4(Position,0.0,1.0);
out_color = v_color;
}



geo shader



in vec4 pos[3];

void main()
{
int i;
vec4 vertex;

gl_Position = pos[0];
EmitVertex();

gl_Position = pos[1];
EmitVertex();

gl_Position = pos[2];
EmitVertex();

gl_Position = pos[0] + vec4(0.0,0.5,0.0,0.0);
EmitVertex();

EndPrimitive();
}


Also, i am setting input and output modes for geo shader and making call to glDrawArrays


glProgramParameteriEXT(shader_data.psId, GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES);

glProgramParameteriEXT(shader_data.psId, GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP);

glDrawArrays(GL_TRIANGLE_STRIP,0,4);



Here, the program is behaving like geo shaders are not included. In geo shader, i am assigning my own coords to 4th vertex but it is taking the one from data array. Geo shader is compiling with success but whatever changes done in geo shader are not getting reflected while rendering. What am i missing here?
Also, as i dont want to mess with colors passed to vert shader, i have not included them in geo shader. And it applies colors successfully to geometry.
When i call glDraw with 3 instead of 4 (as shown above) it renders 1 triangle.

kevinz
01-31-2013, 09:39 AM
Should you declare the input and output primitive types in geometry shader?
like
"
layout ( triangles) in;
layout (triangle_strip, max_vertices = 4) out;

"



Hi,
I have just started with geometric shaders and stuck-up at basics only :(
I am trying to draw GL_TRIANGLE_STRIP.

My vertex array:




const GLfloat data[] =
{
-0.9f, -0.2f,
-0.9f, -0.9f,
-0.2f, -0.9f,
-0.2f, -0.2f,

-0.1f,-0.2f,
-0.1f,-0.9f,
0.4f,-0.9f,
0.4f,-0.2f,

0.5, -0.2,
0.5, -0.9,
0.8, -0.9,
0.8, -0.2

};



Vertex shader:


in vec4 v_color;
flat varying vec4 out_color;
in vec2 Position;

void main()
{
gl_Position = vec4(Position,0.0,1.0);
out_color = v_color;
}



geo shader



in vec4 pos[3];

void main()
{
int i;
vec4 vertex;

gl_Position = pos[0];
EmitVertex();

gl_Position = pos[1];
EmitVertex();

gl_Position = pos[2];
EmitVertex();

gl_Position = pos[0] + vec4(0.0,0.5,0.0,0.0);
EmitVertex();

EndPrimitive();
}


Also, i am setting input and output modes for geo shader and making call to glDrawArrays


glProgramParameteriEXT(shader_data.psId, GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES);

glProgramParameteriEXT(shader_data.psId, GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP);

glDrawArrays(GL_TRIANGLE_STRIP,0,4);



Here, the program is behaving like geo shaders are not included. In geo shader, i am assigning my own coords to 4th vertex but it is taking the one from data array. Geo shader is compiling with success but whatever changes done in geo shader are not getting reflected while rendering. What am i missing here?
Also, as i dont want to mess with colors passed to vert shader, i have not included them in geo shader. And it applies colors successfully to geometry.
When i call glDraw with 3 instead of 4 (as shown above) it renders 1 triangle.

kevinz
01-31-2013, 10:06 AM
or add something like this:
"
glProgramParameteriEXT(shader_data.psId, GL_GEOMETRY_VERTICES_OUT_EXT, 4);
"

debonair
01-31-2013, 10:28 AM
or add something like this:
"
glProgramParameteriEXT(shader_data.psId, GL_GEOMETRY_VERTICES_OUT_EXT, 4);
"
Actually, tried with that too.. :(

significant.bit
01-31-2013, 02:21 PM
I'm new at GS too, but a couple of things look odd at first glance.

In VS you write to gl_Position, but in GS you read from pos[] array. Built-in varyings live in the gl_in[] array, so write gl_in[i].gl_Position. If VS had an out variable named pos, I think that would work too, but I've only used the built-in array.

Did you specify "layout (primtype) in;" & "layout (primtype, max_vertices = x);" in the geometry shader? I'm not familiar with glProgramParameteriEXT as used in your example.

Also, is the compiled GS attached to the program (just like the VS) before link time? If not, that would explain why it's acting like GS doesn't exist.

Hope one of these things is helpful!

debonair
01-31-2013, 10:51 PM
Guess what, i forgot to attach geometry shader, lolz :D

BTW, thank you all for your reply..