# OpenGL Pixel-perfect OpenGL?

## Recommended Posts

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!

##### Share on other sites
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.

##### Share on other sites
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...

##### Share on other sites
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 :)

##### Share on other sites
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]

##### Share on other sites
 @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]

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978617
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 9
• 13
• 12
• 10
• 12