PDA

View Full Version : glOrtho



Bobalandi
02-15-2008, 03:59 PM
I am using glOrtho simple 2d app, and I'm just trying to draw a rectangle, but it's not working, and I think it has to do with my glOrtho, what I put was:

glOrtho(0.0f,800.0f,0.0f,600.0f,1.0f,-1.0f); any one have ideas?

if you want, here is the rest of my code.


#include <windows.h>
#include <gl/gl.h>

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);

int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;

wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "WinClass";
RegisterClass (&amp;wc);

hWnd = CreateWindow (
"WinClass", "Rectangle",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
100, 75, 800, 600,
NULL, NULL, hInstance, NULL);

EnableOpenGL (hWnd, &amp;hDC, &amp;hRC);

while (!bQuit)
{
/* check for messages */
if (PeekMessage (&amp;msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&amp;msg);
DispatchMessage (&amp;msg);
}
}
else
{
/* OpenGL animation code goes here */
glOrtho(0.0f,800.0f,0.0f,600.0f,1.0f,-1.0f);

glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);

glPushMatrix ();
glBegin (GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex2f(10.0f,10.0f);
glVertex2f(50.0f,10.0f);
glVertex2f(50.0f,5.0f);
glVertex2f(10.0f,5.0f);
glEnd ();
glPopMatrix ();

SwapBuffers (hDC);
Sleep (1);
}
}

DisableOpenGL (hWnd, hDC, hRC);
DestroyWindow (hWnd);
return msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{

switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;

case WM_DESTROY:
return 0;

case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;

default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
*hDC = GetDC (hWnd);

ZeroMemory (&amp;pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &amp;pfd);
SetPixelFormat (*hDC, iFormat, &amp;pfd);

*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}

Jan
02-15-2008, 04:53 PM
I only had a quick look, but try:
glOrtho(0.0f,800.0f,0.0f,600.0f,-1.0f,1.0f);

Otherwise your near-plane is actually farther away than your far-plane.

Hope that helps,
Jan.

Bobalandi
02-15-2008, 05:37 PM
Ok, thanks, I just tried that and it didn't work, like it compiles, but it doesn't show the rectangle, so I tried using glVertex3f to draw my rectangle, but it didn't work either.

-NiCo-
02-15-2008, 05:45 PM
Make sure you call glMatrixMode(GL_PROJECTION) and glLoadIdentity() before you call glOrtho. The glOrtho call performs a postmultiplication with the current value of the projection matrix.

N.

Bobalandi
02-15-2008, 06:12 PM
aha! That's exactly what I needed, thanks a lot. I appreciate it.