OpenGL Correctness tips



According to the OpenGL SDKs that I downloaded for use in Visual Studio
2005, the following tip is recommended:

--------------------------------------
To obtain exact two-dimensional rasterization, carefully specify both the
orthographic projection and the vertices of the primitives that are to be
rasterized. Specify the orthographic projection with integer coordinates, as
shown in the following example:

gluOrtho2D(0, width, 0, height);

The parameters width and height are the dimensions of the viewport. Given
this projection matrix, place polygon vertices and pixel image positions at
integer coordinates to rasterize predictably. For example, glRecti(0, 0, 1,
1) reliably fills the lower-left pixel of the viewport, and glRasterPos2i(0,
0) reliably positions an unzoomed image at the lower-left pixel of the
viewport. However, point vertices, line vertices, and bitmap positions
should be placed at half-integer locations. For example, a line drawn from
(x (1) , 0.5) to (x (2) , 0.5) will be reliably rendered along the bottom
row of pixels in the viewport, and a point drawn at (0.5, 0.5) will reliably
fill the same pixel as glRecti(0, 0, 1, 1).

An optimum compromise that allows all primitives to be specified at integer
positions, while still ensuring predictable rasterization, is to translate x
and y by 0.375, as shown in the following code sample. Such a translation
keeps polygon and pixel image edges safely away from the centers of pixels,
while moving line vertices close enough to the pixel centers.

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
glTranslatef(0.375, 0.375, 0.0);
/* render all primitives at integer positions */
----------------------------------

To my OpenGL noob mind this means that the above procedure ensures that
primitive vertex coordinates map accurately to pixels on screen. Is this
correct?

If so, and I use the above method, I guess I'll have to issue a
glTranslatef(0.375, 0.375, 0); every time I use glLoadIdentity(); to reset a
model matrix because this cancels my 0.375 translation.

To avoid this, I suumed I could use the following so that glLoadIdentity();
will atomatically reset the origin of the coordinate system to include this
0.375 offset:

gluOrtho2D(0.375, width, 0.375, height);

but it doesn't seem to work. Should it work? Or will I simply have to do the
offset translation after any glLoadIdentoty().

Or have I missed something about this concept entirely :)


.



Relevant Pages

  • Re: page is too wide even
    ... You have nested tables, so you need to specify each table as 100%, and then ... specify the width percentage for each cell individually. ... search for "width=" and change the pixel value to a %. ... I'm getting ready to sign off, Susan, but if you need additional help, feel ...
    (microsoft.public.frontpage.programming)
  • Region.IsEmpty() without a Graphics object
    ... to specify a Graphics object, i.e. an override that specifies the pixel ... determining whether not the sum of width * height for all rectangles ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Drawing a line
    ... The place where you create new classes is where you specify the base class, ... How do I create a "subclass" of CStatic? ... if you want a "line" that is 256 pixels long where each pixel ...
    (microsoft.public.vc.mfc)
  • Re: create a clone of an existing Bitmap without using the clone method.
    ... > you to specify the Pixel GraphicsUnit otherwise the image will be adjusted ... > Find great Windows Forms articles in Windows Forms Tips and Tricks ... >> Dim TmpBitmap = New Drawing.Bitmap ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Virtual Functions optimization
    ... ie. process whole scanlines at once ... (using a pointer to browse through that scanline's pixels, ... the onde pixel at a time approach. ... There are some ways to improve speed: rasterization, dirty rectangles, ...
    (borland.public.delphi.language.basm)