2D openGL drawing lines that don't exactly fit pixel raster
I'm drawing something similar to a LED VU meter in OpenGL. Result should be like on vertical bar of this:
Unfortunately, my drawing space is restricted, and I have to fit exactly 18 bars on it. That yields a spacing between bars of 3.6 pixels.
When drawn, this causes visible differences in the gaps between lines, because the gaps are either 2 or 1 pixel wide. I'm looking for a solution to use subpixel rendering and than "fake" the lines by some kind of anti-aliasing, so that all gaps appear of the same width.
This is my code so far
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH, GL_NICEST);
for (int i = 0; i < 18; ++i) {
float bBottom = barBottom + i*3.6f;
glBegin(GL_LINES);
开发者_如何学CglLineWidth(2);
glVertex2f(bRight,bBottom);
glVertex2f(bLeft,bBottom);
glEnd();
}
glDisable(GL_LINE_SMOOTH);
Unfortunately, turning on line smoothing showed no visible effect. Any suggestions?
Two ideas:
Draw all the bars to a large off screen texture such that the gaps and widths are all equal and then use bilinear filtering to blit the result to the required screen position and size.
Get an artist to draw all the fully lit bars so that the look good, then draw black rectangles with an alpha channel from the top of each bar downwards to darken the required parts of the image.
Use multisampling (Multisampling specs).
Additionally, I would use quads (and not lines) for rendering those blocks.
If you can't enable antialiasing for your OpenGl context, you can emulate it - it's easy with such simple graphics. When you need to draw rectangle 10.6 pixels high, draw it 10 pixels with its color and then single pixel line of intermediate color (0.6 of rectangle color and 0.4 of background color).
精彩评论