PDA

View Full Version : Crisp rendering of horizontal and vertical lines 2



Qu0ll
05-09-2011, 08:36 AM
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?

Ilian Dinev
05-09-2011, 02:52 PM
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;
}


http://en.wikipedia.org/wiki/Quantization_%28signal_processing%29

dukey
05-09-2011, 03:28 PM
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.

Kelvin
05-09-2011, 09:22 PM
Take a look this (http://glprogramming.com/red/appendixg.html) 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.