PDA

View Full Version : Geometry shader: points to thick lines



Albert85
12-17-2014, 09:22 AM
Hello everybody,
I would like to render thick lines strarting from a list of points.
This is the geometry shader that takes in input point and creates the thick lines:


////////////////////////////////////////////////////////////////////////////////////////////
#version 330 compatibility
#extension GL_EXT_geometry_shader4 : enable

#define MAX_VERTICES 6
layout(lines_adjacency) in;
layout(triangle_strip, max_vertices = MAX_VERTICES) out;


in vec4 v_position[4]; // Position

void emitTheVertex(vec3 p){
gl_Position = vec4(p, 1.0);
gl_FrontColor = v_color[0];
EmitVertex();
}

void main(void)
{
// Get the 4 vertices and convert them into screen space
vec4 pos0 = gl_ModelViewProjectionMatrix * vec4(v_position[0].xyz, 1.0);
vec4 pos1 = gl_ModelViewProjectionMatrix * vec4(v_position[1].xyz, 1.0);
vec4 pos2 = gl_ModelViewProjectionMatrix * vec4(v_position[2].xyz, 1.0);
vec4 pos3 = gl_ModelViewProjectionMatrix * vec4(v_position[3].xyz, 1.0);


vec3 v0 = pos0.xyz / (pos0.w);
vec3 v1 = pos1.xyz / (pos1.w);
vec3 v2 = pos2.xyz / (pos2.w);
vec3 v3 = pos3.xyz / (pos3.w);

float width = 0.005;

p0 = v1 - n12*width;
p1 = v1 + n12*width;
p2 = v2 - n12*width;
p3 = v2 + n12*width;

emitTheVertex(p0);
emitTheVertex(p1);
emitTheVertex(p2);
emitTheVertex(p3);

EndPrimitive();

}
//////////////////////////////////////////////////////////////////

Sometimes I have a problem when one point is visible and the next is outside the screen. The coordinates of the point outside are different if I move the camera view.

Here there are some images....
1553
1554
1555
.. if I get closer...
1556

the point outside the screen is placed in the wrong place.
I think this problem is related to the following operation: ....pos1.xyz / (pos1.w)....
I divide each point by "w" in order to not keep the perspective (near lines are equal to far lines).

Can anyone help me?

Thanks!

malexander
12-17-2014, 11:16 AM
Your line is probably going behind the camera plane. When this happens, w becomes negative. You can clip the line at z=0, or just punt and divide by abs(w) instead. I'm guessing you've left out the calculation for n12 for brevity, but if that suggestion doesn't work, please post it. (also, please use code tags around code).

Albert85
12-18-2014, 02:15 AM
This is the code (I removed unnecessary lines):




////////////////////////////////////////////////////////////////////////////////////////////
#version 330 compatibility
#extension GL_EXT_geometry_shader4 : enable

#define MAX_VERTICES 6
layout(lines_adjacency) in;
layout(triangle_strip, max_vertices = MAX_VERTICES) out;


in vec4 v_position[4]; // Position

void emitTheVertex(vec3 p){
gl_Position = vec4(p, 1.0);
gl_FrontColor = v_color[0];
EmitVertex();
}

void main(void)
{
// Get the 4 vertices and convert them into screen space
vec4 pos0 = gl_ModelViewProjectionMatrix * vec4(v_position[0].xyz, 1.0);
vec4 pos1 = gl_ModelViewProjectionMatrix * vec4(v_position[1].xyz, 1.0);
vec4 pos2 = gl_ModelViewProjectionMatrix * vec4(v_position[2].xyz, 1.0);
vec4 pos3 = gl_ModelViewProjectionMatrix * vec4(v_position[3].xyz, 1.0);


vec3 v0 = pos0.xyz / (pos0.w);
vec3 v1 = pos1.xyz / (pos1.w);
vec3 v2 = pos2.xyz / (pos2.w);
vec3 v3 = pos3.xyz / (pos3.w);

float width = 0.005;

vec3 n12 = cross(normalize(v2-v1), vec3(0, 0, 1));

p0 = v1 - n12*width;
p1 = v1 + n12*width;
p2 = v2 - n12*width;
p3 = v2 + n12*width;

emitTheVertex(p0);
emitTheVertex(p1);
emitTheVertex(p2);
emitTheVertex(p3);

EndPrimitive();

}




using :




vec3 v0 = pos0.xyz / abs(pos0.w);
vec3 v1 = pos1.xyz / abs(pos1.w);
vec3 v2 = pos2.xyz / abs(pos2.w);
vec3 v3 = pos3.xyz / abs(pos3.w);



The problem is not solved...
If I get near the lines:
1565

1566
after a while it seems like I go back, but is not what I'm doing..
1567

1568

1569

Do you understand my problem?

Albert85
12-23-2014, 01:40 AM
Someone can help me??