Why is 2D view slightly off

Stupid newbie problem. I have a 2D view (a map) which I set up with want positioned on the screen at bottomLeftX,bottomLeftY and Width,Height in size so I use

glViewport(blx,bly,wdt,hgt)

I set up my projection matrix with


 GLdouble originX = wdt/2 + blx;
 GLdouble originY = hgt/2 + bly;
 wdt *= zoom;
 hgt *= zoom;
 blx = originX-(wdt/2);
 bly = originY-(hgt/2);
 glOrtho(blx,wdt, bly,hgt, -1,1);

With my simple settings of blx=0,bly=0, wdt=640,hgt=480 camera at -320,0,0 the object at 0,0,0 the object isn’t centered in my 640x480 window (it very nearly is) and it moves slightly left as I zoom out.

Everything works with 3D view (using gluPerspective), including zoom. What I don’t get is why in 2D I need my camera at -320. Any pointers on where to look would be great.

Thanks.

This is what I use for pixel perfect alignment in FBOs, when drawing to them in 2D for HUDs and procedural texture atlases.

	glViewport(0, 0, m_nTextureSize, m_nTextureSize);
		glMatrixMode(GL_PROJECTION);
		glPushMatrix();
		glLoadIdentity();
		glOrthof(0, m_nTextureSize, 0, m_nTextureSize, 0, 1);
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		glLoadIdentity();

		glTranslatef(0.5, 0.5, 0);	

You may or may not need the translate at the end, but that is to center the pixels on their respective coordinate.

You will need to adjust the parameters in glViewport and glOrtho to reflect your offset into the screen, and the width / height of your square.

Once you’ve done that you can draw your map using a QUAD with 2D coordinates and x,y coordinates which reflect it’s width and height one to one.

The code you listed is the same as mine for the simple case when there is no zoom, but with zoom my blx,bly are wrong since I obviously don’t understand glOrtho :slight_smile: For the simple case (full screen, zoom in center) I need


 GLdouble  wdt = screenWdt*zoom;
 Gldouble  hgt = screenHgt*zoom;
 blx = screenWdt - wdt;
 bly = screenHgt - hgt;
 glOrtho(blx,wdt, bly,hgt, -1,1);

I’m now trying get it to work with a “sub-window” i.e. a non-full screen area at an arbitrary position…