# How does GL_LINES rasterizing work, exactly?

This topic is 4423 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In my latest rework of my GUI system, I've decided, for now, to add a Windows classic-like border around a number of the GUI controls to give them a little definition. The GUI Window control, for instance, is a textured pair of triangles (serving as the background) drawn in ortho mode, and I add some GL_LINES rendering to create the border. I'm having some issues in specifying pixel-exact coordinates on where my lines draw to and from, though. I've tested several methods of calculating what the glVertex2f values should be to render the lines I desire. So far, I've had inconsistent results and I'm wondering if my understanding of how GL_LINES are rasterized is off. As for some relevant information, my ortho mode is setup in the "screen space" direction, where X increases from left to right, and Y increases from top to bottom. The actual call I make is as follows:
glOrtho(0.0, width, height, 0.0, zNear, zFar);

Now consider an example objective of drawing from pixel (0,0) to (3,0), as in the very top left row of 4 pixels from the left edge going right. For my calls to glVertex2f, looking something like:
glBegin(GL_LINES);
glVertex2f(x0, y0);
glVertex2f(x1, y1);
glEnd();

... what, exactly, should I specify as coordinates? The goal is to map a given pair of screen pixel coordinates (pixels have area) to a pair of cartesian coordinates (infinitely small points). I whipped up 3 images to illustrate the 3 ways I tested: specifying vertices at the center of the pixel: specifying the left/right bounds of the line on the X and the Y coordinate is centered on the pixel: specifying the left/right bounds of the line on the X and the Y coordinate is set to 0.0: None of these 3 ways have actually rendered correctly in all test cases when I specify different line coordinates. Help? :(

##### Share on other sites
Just tested it, and (0.0, 0.0)-(4.0, 0.0) worked exactly as you seem to want it to. The line covered the first four pixels, as it should.

Is your orthographic projection an exact 1:1 mapping to screen coords? Are the width and height you use the width and height of the window's client area?

##### Share on other sites
The Red Book has hints on this.

Look for the correctness tips part of appendix H.

dave j

##### Share on other sites
Quote:
 Original post by chawkNone of these 3 ways have actually rendered correctly the way I expect in all test cases when I specify different line coordinates.

There, fixed the quote. Seriously, the OpenGL specification have strict rules on how to rasterize lines (as well as any other primitive), and I can guarantee that the lines ARE rendered correct. Correct according to the specification. Not correct according to what you expect.

If you want exact pixel perfect rendering, I strongly suggest you take a look in the API specification and learn how lines are required to behave in OpenGL.

In short, if I'm not mistake, the following will render a line from the pixel at (x1, y1) to, but not including, the pixel at (x2, y2) (assuming integer coordinates).
glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-0.5, windowwidth-0.5, -0.5, windowheight-0.5, -1, 1);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glBegin(GL_LINES);    glVertex2i(x1, y1);    glVertex2i(x2, y2);glEnd();

Or change the order of third and fourth parameter if you want the origin in the top left corner instead.

If you need the pixel at (x2, y2) to be rasterized aswell, you need to extend the line by the length of one pixel in the direction from the start to the end, or draw a GL_POINT at (x2, y2).

##### Share on other sites
Quote:
 There, fixed the quote. Seriously, the OpenGL specification have strict rules on how to rasterize lines (as well as any other primitive), and I can guarantee that the lines ARE rendered correct. Correct according to the specification. Not correct according to what you expect.

Like I said, the way I expect ^_^ Clearly I'm doing something wrong. Also, I guess it'd behoove me to read the opengl spec once in a while. I've come pretty accustomed to learning by example code from others that I forget about the spec documentation :X

The responses look great and I'll definitely try that out when I get home.

Thanks a lot!

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631768
• Total Posts
3002242
×