Crisp rendering of horizontal and vertical lines 2

I posted in this forum some time ago about my desire to render crisp lines when they are perfectly horizontal or vertical i.e. if I specify a line width of 1px, I want only 1 pixel to be used to render the line, not 2 or more that would occur if multisampling or line smoothing were used.

At the time there were two main suggestions namely to disable multisampling when I am drawing such lines and to somehow align the rendering with the pixel grid.

I have tried the first suggestion and it works to a point i.e. it does draw crisp horizontal and vertical lines when multisampling is otherwise applied but if the user goes to their graphics card driver settings and manually overrides the antialiasing by selecting SS (super sampling) then all bets are off and the thick lines return.

So I’d like to try the second suggestion of aligning the rendering to the pixel grid. I have to say I am not entirely sure what this means so could someone let me in on the technique to achieve this?

Before:


void main(){
	vec4 cpos = someMatrix * gl_Vertex;
	gl_Position = cpos;
}

After:


uniform vec2 viewportSize = vec2(1280,720);

void main(){
	vec4 cpos = someMatrix * gl_Vertex;
	vec2 p = floor(cpos.xy * viewportSize * (0.5 /cpos.w));
	// p+= 0.5; // may be necessary, do check
	cpos.xy = (p * cpow.w) * (2.0/viewportSize);
	
	gl_Position = cpos;
}

only way i can think of is render the desired lines to an FBO where you can explicitly control the anti aliasing, then blend with frame buffer. Bit extreme but that would work.

Take a look this excerpt of an old Red Book, starting at “If exact two-dimensional rasterization is desired…”

This provides more information about the technique Ilian Dinev mentioned. It’s from a fixed-function point of view, but the main point is that if you are careful to cover pixels exactly, you shouldn’t be affected by MSAA or SSAA (i.e. you will cover all samples, or no samples). The exception might be a type MSAA mode that has some samples that our outside of the pixel cell (“Wide tent” AA might do that).

Drawing with exact pixel position also means that outer edges of lines/polygons look the same with or without line/polygon AA, because there are no “partially covered” pixels.