Geometry shader: points to thick lines

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…
[ATTACH=CONFIG]838[/ATTACH]
[ATTACH=CONFIG]839[/ATTACH]
[ATTACH=CONFIG]840[/ATTACH]
… if I get closer…
[ATTACH=CONFIG]841[/ATTACH]

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!

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

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:
[ATTACH=CONFIG]845[/ATTACH]

[ATTACH=CONFIG]846[/ATTACH]
after a while it seems like I go back, but is not what I’m doing…
[ATTACH=CONFIG]847[/ATTACH]

[ATTACH=CONFIG]848[/ATTACH]

[ATTACH=CONFIG]849[/ATTACH]

Do you understand my problem?

Someone can help me??

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.