PDA

View Full Version : 1px line using triangles



phdr_hroch
09-21-2012, 01:57 AM
Hi,
I have geometry (or fallback vertex only) shader, that generates lines with custom width and pass float uniform into shader.
For lines that are more than 1px thick, it works great. But if I want to draw nonantialliased line with 1px width, it looks ugly, especialy when line is not perpendicular to screen.
Any options ?
Problem is that I have only one definition of some geometry and wants to draw it with different line widths many times
Thank you

thokra
09-21-2012, 02:48 AM
it looks ugly

Define ugly. Screen shot please.

phdr_hroch
09-21-2012, 03:07 AM
876
in 45 degrees rotation the line (created by two triangles) is 2px wide instead of 1px

thokra
09-21-2012, 03:12 AM
Hmm, shaders please.

phdr_hroch
09-21-2012, 03:55 AM
geometry shader line draw function:
// make thick line
// from : transformed point (by modelviewprojectionmatrix)
// to : trasformed point
// colorfrom : first point color
// colorto : second point color
void makeLine(in vec4 from, in vec4 to, in vec4 colorfrom, in vec4 colorto)
{
// triangle positions
vec4 position1 = from;
vec4 position2 = from;
vec4 position3 = to;
vec4 position4 = to;

// direction of line
vec2 line2d = position3.xy - position1.xy;

// normalized direction
vec2 direction = normalize(line2d);

// shift vector
vec2 vector;

// make vector perpendicular to line
vector.x = direction.y / screen.x;
vector.y = -direction.x / screen.y;


// normalizedwidthdiv2 : global variable width normalized from px width to normalized coords
// vec2 normalizedwidthdiv2 = width / screen;
// uniform vec2 screen; // screen size in px
// uniform vec2 width; // width of line in px

vec2 shift = normalizedwidthdiv2 * vector;

// shift positions
position1.xy -= shift;
position2.xy += shift;
position3.xy -= shift;
position4.xy += shift;

// emit triangles
gl_Position = position1;
gl_FrontColor = colorfrom;
EmitVertex();

gl_Position = position3;
gl_FrontColor = colorto;
EmitVertex();

gl_Position = position4;
gl_FrontColor = colorto;
EmitVertex();

EndPrimitive();

gl_Position = position1;
gl_FrontColor = colorfrom;
EmitVertex();

gl_Position = position2;
gl_FrontColor = colorfrom;
EmitVertex();

gl_Position = position4;
gl_FrontColor = colorto;
EmitVertex();

EndPrimitive();
}

Dark Photon
09-21-2012, 07:05 PM
Please use [ code ] ... [ /code ] blocks. Preserves the indentation. I inserted them for you.

michagl
09-23-2012, 07:55 PM
Is it guaranteed that a consumer card can do this with pixel perfect precision?


Please use [ code ] ... [ /code ] blocks. Preserves the indentation. I inserted them for you.

I guess that makes Dark Photon a mod-and-or-admin then.

So many interesting threads.