Setting up 2D

How do i setup 2d in opengl?

About this way:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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

Originally posted by BlackJack:
[b]About this way:

[quote]

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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[/b][/QUOTE]

Correction:-

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

Anyway, use this instead:-

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

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.

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);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
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).]

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, &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);