PDA

View Full Version : drawing antialiased lines on triangular mesh



derbachii
09-14-2009, 05:37 AM
Hi,

I'm trying to visualize polylines on top of a triangular mesh.
My problems are:
-"stitching" or "bleeding"
-antialiasing

The lines are drawn as quads. To avoid bleeding I'm using a shader to move the lines along the vertex-normals.
Is there a better way of coping with that?

And how to get antialiased quads?
I tried:


glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

without any noticeable improvements.

It would be nice to get a point in the right direction...

Thanks in advance
Daniel

thinks
09-14-2009, 07:03 AM
Check out this (http://csserver.ucd.ie/~thinks) description of Single-pass Wireframe Rendering. It nicely handles exactly what you are trying to do. :)

derbachii
09-14-2009, 08:12 AM
I don't want du draw the wireframe of the mesh.
I want to draw arbitrary polylines on top of the mesh.

With this approach the distances to the lines have to be computed for each fragment. Or am I completely wrong?

thinks
09-14-2009, 09:02 AM
The methods can be modified to draw arbitrary lines, it would be a bit trickier though. The lines you want to draw always lie in the same plane as your triangles/quads?

Yes, distances are computed in viewport space and anti-aliasing based on these distances in done per fragment.

Ilian Dinev
09-14-2009, 10:05 AM
If you want to draw antialiased, enable multisampling or use a multisampling-FBO. If you don't want to spend so much resources on a few edges, extract the relevant code from here:



#define ANTIALIAS_EDGE

//#include "Tex1.c"




#include "system.h"
/*
lightmapped textured. Write albedo to COLOR0 and lightness to COLOR1

*/

attribute1(vec3 inNormal);
attribute2(vec4 inCoord); // contains both vu0 and uv1

varying0(vec3 varNormal);
varying1(vec4 varCoord);
varying2(vec3 varPos);
varying3(vec4 varPosix);

texture0(sampler2D tex0);
texture1(sampler2D tex1);
texture7(sampler2D texNoise1);


#if IS_VERTEX


uniform mat4 mvpx : C0;

void main(){
varNormal= inNormal;
varCoord = inCoord;
varPos = glVertex.xyz;

vec4 thePos = mvpx * glVertex;

gl_Position = thePos;
#ifdef ANTIALIAS_EDGE
gl_Position.z-=0.0001;
varPosix = thePos;
#endif

}

#endif

#if IS_FRAGMENT


void main(){
vec4 ALBEDO = texture2D(tex0,varCoord.xy);
vec4 LIGHTM = texture2D(tex1,varCoord.zw);
vec4 noise1 = texture2D(texNoise1,varCoord.zw*32);

float noise2 = (noise1.x*noise1.y*noise1.z+0.5);
noise2=noise2*0.3+0.7;


vec3 varPos2 = varPos - vec3(0,1,0);
float dist1 = 4.0/dot(varPos2,varPos2);

LIGHTM+=dist1*dist1*0.9;



gl_FragColor = ALBEDO*LIGHTM*noise2 + LIGHTM*0.3;
//gl_FragColor = ALBEDO*LIGHTM*noise2+LIGHTM*0.3;
#ifdef ANTIALIAS_EDGE
vec2 ScreenSizeHalf = vec2(1024/2,576/2); // for 640x480 screen
vec2 posix1= (varPosix.xy/varPosix.w+1)*ScreenSizeHalf;
vec2 fpos = gl_FragCoord.xy;
float dist = distance(fpos,posix1);
gl_FragColor.w = saturate(1-dist);
//gl_FragColor.xyz=vec3(1,0,0);
//gl_FragColor.w = 0.2/(dist*dist);
#endif
}
#endif



Enable alpha-blending mode, set line-width = 2 . Draw the lines as you usually would, but with the above shader.

With the above shader you can also antialias a quad's edges in a second pass: draw the geometry in wireframe mode: glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

The line-antialiasing has a lot of requirements to be enabled, and sometimes is locked only to be used with Quadro/FireGL, afaik. It had always been of worse quality than the proposed shader or CSAA anyway.

derbachii
09-14-2009, 11:05 AM
Hi,

thanks for your replies. I activated Multisampling and it works quite good on my quadro fx.

Thinking about implementing a "single-pass wireframe"-like approach in the near future...

thx
Daniel.