Pixel-perfect OpenGL?

Started by
4 comments, last by fyhuang 18 years, 8 months ago
Hello, I've just recently switched my game from using SDL's 2D rendering to OpenGL (for performance and easier scaling of sprites). The first big problem I've run into is how to do 'pixel-perfect' OpenGL. For example, my ship positions are stored as double-precision floats. I used to be able to draw the ships' health bars using code such as:
boxColor( TX( ship->m_Position.getPosX() ) - 31,
   TY( ship->m_Position.getPosY() ) - 39,
   TX( ship->m_Position.getPosX() ) - 31 + ( 62 * healthPercent ),
   TY( ship->m_Position.getPosY() ) - 36, 0x00FF00C0 );
(messy, I know). But now, I've replaced the SDL_gfx functions with OpenGL equivalents, such as:
void glLine( double x1, double y1, double x2, double y2, Uint32 color )
{
	setColor( color );
	
	glBegin( GL_LINES );
		glVertex2f( x1, y1 );
		glVertex2f( x2, y2 );
	glEnd();
}

void hlineColor( Uint32 x1, Uint32 x2, Uint32 y, Uint32 color )
{
	// Wrapper for glLine()
	setAA( false );
	glLine( (int)x1, (int)y, (int)x2, (int)y, color );
}

And it has stopped working. I've tweaked the settings of the above boxColor() call, so that when the ships' positions are integral (i.e. 100.0, 100.0), it will work (note in this screenshot, I haven't gotten any image-rendering functions converted yet): But, when the ships have fractional positions, it becomes off: I have already applied a glTransformf( -0.5, -0.5, 0.0 );, which has prevented the lines from moving position every time the ships move... but the bars apparently still move around when the ships move (and not in a good way). I would rather not tweak a thousand more variables in order to get my rendering to work right... if anyone has any ideas on how to fix this (or has programmed a 2D game in OpenGL before, or something), they would be greatly appreciated! Thanks in advance!
- fyhuang [ site ]
Advertisement
I honostly think the "best" solution will to use integers instead of floats to store your positions. It might be a pain in the butt to do, but if you don't, you'll be fighting these kinds of problems for a long time to come and the work-arounds will get ugly.
I find using ints for actual rendering (eg. glVertex2i and such) gives pixel perfect rendering (although I'm still using floats internally for everything, just casting before drawing). Nice and simple way to stop the 'shimmering' as you attempt to draw lines 'between' pixels.

The only other thing to watch for is texture filtering method. NEAREST works best if you're not scaling or rotating, but I don't think thats the issue here.

If that doesn't fix things I think you're going to have to post a more complete explaination of what doesn't work...
Hi,

When I did 2D stuff in GL, I used a glOrtho projection with width/heght set to screen width height. Then I could just pass in pixel positions to OpenGL directly.

// SetupglViewport (0, 0, GetCVars().ResX, GetCVars().ResY);glMatrixMode (GL_MODELVIEW);  glLoadIdentity();glMatrixMode (GL_PROJECTION); glLoadIdentity();gluOrtho2D (0, (GLdouble)GetCVars().ResX, (GLdouble)GetCVars().ResY, 0);// Draw some random circle (everything is in screen coords).glBegin(GL_LINE_LOOP);for (float t = 0; t <= 2*PI; t+= PI/15)    glVertex2f (pos.x + sinf(t)*radius, pos.y + cosf(t)*radius);glEnd();


Storing your positions as floats won't hurt either (that is what I do).

Hope that helps :)

- Thomas Cowellwebsite | journal | engine video

you know, you'd think the Forum FAQ would have a question about 2D OpenGL, which points to a thread which on the 2nd page explains how to do pixel-perfect OpenGL... I mean, wouldnt that be handy? [smile]
[edit] @Phantom, I'm sorry to admit that I didn't look in the OpenGL FAQ [sad], but now that I did it might not have helped anyhow, as my problem wasn't with projection but rather the method which I was drawing (glVertex2f instead of glVertex2i).

[edit2] @Phantom again, didn't see the part of your post saying "second page"...

It works! Thank you OrangyTang, after converting all my functions to accept Uint32s as position inputs, the whole thing works now. Unfortunately, I now lose the (possible) benefit of being able to start/end lines at fractional positions, but whatever. I'll just re-implement ones that accept floats if I need the functionality [smile].

But, unrelated to the previous problem are the other problems I have with my method - I can't get antialiasing to work (correctly). This is my code:

void setAA( bool aa ){	if ( aa )	{		glHint( GL_POINT_SMOOTH_HINT, GL_NICEST );		glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );		glHint( GL_POLYGON_SMOOTH_HINT, GL_NICEST );				glEnable( GL_POINT_SMOOTH );		glEnable( GL_LINE_SMOOTH );		glEnable( GL_POLYGON_SMOOTH );				glLineWidth( 0.5 );	}	else	{		glHint( GL_POINT_SMOOTH_HINT, GL_FASTEST );		glHint( GL_LINE_SMOOTH_HINT, GL_FASTEST );		glHint( GL_POLYGON_SMOOTH_HINT, GL_FASTEST );				glDisable( GL_POINT_SMOOTH );		glDisable( GL_LINE_SMOOTH );		glDisable( GL_POLYGON_SMOOTH );	}}


On some lines (like the grid lines, which are now not thick and ugly anymore), antialising (the glLineWidth() statement actually has an effect) works. However, the lines under the ships are not AA-ed. Also, alpha blending doesn't seem to want to work either... here is the relevant code:

void setColor( Uint32 color ){	glColor4d( color >> 24, color << 8 >> 24, color << 16 >> 24, color << 24 >> 24 );		//printf( "Color passed: 0x%X\t", color );	//printf( "r: %u, g: %u, b: %u, a: %u\n", color >> 24, color << 8 >> 24, color << 16 >> 24, color << 24 >> 24 );		if ( ( color << 24 >> 24  ) < 255 )	{		glEnable( GL_BLEND );				glBlendFunc( GL_SRC_ALPHA, GL_ONE );	}	else	{		glDisable( GL_BLEND );	}}// In main() function:	glBlendFunc( GL_SRC_ALPHA, GL_ONE );	glEnable( GL_BLEND );	glDisable( GL_TEXTURE_2D );	glDisable( GL_LIGHTING );


Thanks in advance for being so patient and helping me through my OpenGL problems [smile]. Cheers!

[Edited by - fyhuang on August 30, 2005 8:39:38 PM]
- fyhuang [ site ]

This topic is closed to new replies.

Advertisement