PDA

View Full Version : Setting up 2D

smiley
07-28-2002, 02:27 AM
How do i setup 2d in opengl?

BlackJack
07-28-2002, 06:32 AM

glMatrixMode(GL_MODELVIEW);
glOrtho(0, miWidth,miHeight, 0, -miDepth, miDepth);
glViewport(miOffsetX,mInterface->mdwClientHeight-miHeight-miOffsetY,miWidth,miHeight);

If you draw polygons then the upper left is 0,0 and the lower right miWidth and miHeight.

BUT: wrong forum, ask such things on beginners forum next time plz.

BlackJack

knackered
07-28-2002, 07:55 AM
Originally posted by BlackJack:

glMatrixMode(GL_MODELVIEW);
glOrtho(0, miWidth,miHeight, 0, -miDepth, miDepth);
glViewport(miOffsetX,mInterface->mdwClientHeight-miHeight-miOffsetY,miWidth,miHeight);

If you draw polygons then the upper left is 0,0 and the lower right miWidth and miHeight.

BUT: wrong forum, ask such things on beginners forum next time plz.

BlackJack

Correction:-

glMatrixMode(GL_PROJECTION);
glOrtho(0, miWidth,miHeight, 0, -miDepth, miDepth);
glViewport(miOffsetX,mInterface->mdwClientHeight-miHeight-miOffsetY,miWidth,miHeight);

glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);

Now, glVertex3f(-1.0f, -1.0f, 0.0f) will give you a point in the lower left of your viewport, while glVertex3f(1.0f, 1.0f, 0.0f) will give you a point in the upper right of your viewport.

BlackJack
07-28-2002, 10:03 AM
Am using modelview since years and works perfectly, but yes, you are right, it's more clean your way... ok, still missing LoadIdentity of model view..., so...

glMatrixMode(GL_MODEL_VIEW);
glMatrixMode(GL_PROJECTION);
glOrtho(0, miWidth,miHeight, 0, -miDepth, miDepth); glViewport(miOffsetX,mInterface->mdwClientHeight-miHeight-miOffsetY,miWidth,miHeight);

Because of your second version: Hm........ I know that this would be the result of the identity matrix, but... isn't it a pain in the ass to manage 2D this way? After all the input data you are working with is in general in pixel sizes and coordinates... hm... well, everybody as he means to do...

BlackJack

[This message has been edited by BlackJack (edited 07-28-2002).]

knackered
07-28-2002, 01:49 PM
I prefer to work in ratios, that way I don't need to know the actual size of anything. For instance, I implement these functions to size my windows at initialisation, so it doesn't matter what resolution someones running at:-

void Window::UpdateDesktopDimensions()
{
RECT rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &amp;rect, 0);

sm_iDesktopWidth = rect.right - rect.left;
sm_iDesktopHeight = rect.bottom - rect.top;
}

int Window::GetDeskX(float fRatio)
{
UpdateDesktopDimensions();

return (int)(fRatio*(float)sm_iDesktopWidth);
}

int Window::GetDeskY(float fRatio)
{
UpdateDesktopDimensions();

return (int)(fRatio*(float)sm_iDesktopHeight);
}

So, for example, if I want a window to take up the bottom right quarter of the desktop, I'd do something like this:-

topleftx = Window::GetDeskX(0.5);
toplefty = Window::GetDeskY(0.5);
width = Window::GetDeskX(0.5);
height = Window::GetDeskY(0.5);

window.Create(topleftx,toplefty,width,height);