View Full Version : Possible bug on Opengl template ?

25-10-2006, 08:43 PM
I already posted a thread on sulaco website forum without reply ...

If you're using a c++ style template for delphi, there's a little bug (in my opinion) in the windowed mode.. (the one i use although, there is a large number of template on the web)

the viewport is not properly set because the height of the title bar is included.

as an example, if you use glreadpixels you can notice there's a black area in the window titlebar's place.

In the following code the parameters Width and Height are no longer used.(so they can be deleted)
It works for both windowed and fullscreen mode.

procedure glResizeWnd(Width, Height : Integer);
ClientRect : TRect;
ScreenHeight, ScreenWidth : integer
ScreenHeight := ClientRect.bottom - ClientRect.Top;
ScreenWidth := ClientRect.Right - ClientRect.Left;

if (ScreenHeight = 0) then // prevent divide by zero exception
ScreenHeight := 1;

glViewport(0, 0, ScreenWidth, ScreenHeight); // Set the viewport for the OpenGL window
glMatrixMode(GL_PROJECTION); // Change Matrix Mode to Projection
glLoadIdentity(); // Reset View
gluPerspective(45.0, ScreenWidth/ScreenHeight, 1.0, 100.0); // Do the perspective calculations. Last value = max clipping depth
glMatrixMode(GL_MODELVIEW); // Return to the modelview matrix
glLoadIdentity(); // Reset View

what do you think ?

25-10-2006, 09:23 PM
No. existing code for resizing window is proper, since glResizeWnd is called with proper parameters from the message loop. the real problem is, that the viewport is definetly not set right when the window is created(it does not take title bar into account).

25-10-2006, 09:44 PM
So the best way to prevent this problem would be to call another procedure than glresizewnd in the glcreatewnd function ?

26-10-2006, 07:59 PM
So the best way to prevent this problem would be to call another procedure than glresizewnd in the glcreatewnd function ?

no. but your GetClientRect solution is not proper either, since the dimensions will match those retrieved from the message queve and fix something that is not broken.

my solution:

find call to CreateWindowExA and make it use: Width - 1

after calling showwindow, setfocus and setforeground window, etc.. add this:

// fix the window and window projection, this will also call GLResizeWnd
(GetSystemMetrics(SM_CXSCREEN) div 2) - (Width div 2),
(GetSystemMetrics(SM_CYSCREEN) div 2) - (Height div 2),
Width, Height, True);