PDA

View Full Version : OpenGL cannot be initialized in Delphi5



earth_walker
12-28-2003, 02:57 PM
Hi! I'm using Delphi5 to do OpenGL programming. The following code is from a book, it have been working, but doesn't work now. Can you help to see how to fix it?
Thank you!

*************************************
procedure TForm1.FormCreate(Sender: TObject);
var
pfd: TPixelFormatDescriptor;
FormatIndex: integer;
begin

......

FormatIndex := ChoosePixelFormat(Canvas.Handle,@pfd);
SetPixelFormat(Canvas.Handle,FormatIndex,@pfd);
GLContext := wglCreateContext(Canvas.Handle);
wglMakeCurrent(Canvas.Handle,GLContext);

ShowMessage('version='+glGetString(GL_VERSION)+';'
+#13+'vendor='+glGetString(GL_VENDOR)+';');
{error checking}
errorCode := glGetError;
if errorCode<>GL_NO_ERROR then
raise Exception.Create('Error in Paint'#13+
gluErrorString(errorCode));
end; {FormCreate}
**********************************

The information given by watching variables:
FormatIndex=1
GLContext=0
errorCode=1282

also, the ShowMessage statement tells that both vendor and version returns empty string.

I remember this program worked in summer this year, but it doesn't work during these couple month, is it possible to because of the update of Windows? I'm under Windows 2000

Result:

PanzerSchreck
12-28-2003, 03:04 PM
Don't use the handle of the form's canvas, use the form's handle itself instead. I see a lot of people using the handle of a canvas for creating their contexts, but that's wrong. So for example just do


FormatIndex := ChoosePixelFormat(Handle,@pfd);

earth_walker
12-28-2003, 04:27 PM
Thank you!
I tried to modify it but it doesn't work...
Here is the code after modification,can you check it? Thank you!




FormatIndex := ChoosePixelFormat(HDC(Handle),@pfd);
SetPixelFormat(HDC(Handle),FormatIndex,@pfd);
GLContext := wglCreateContext(HDC(Handle));
wglMakeCurrent(HDC(Handle),GLContext);

ShowMessage('version='+glGetString(GL_VERSION)+';'
+#13+'vendor='+glGetString(GL_VENDOR)+';');
{error checking}
errorCode := glGetError;
if errorCode<>GL_NO_ERROR then
raise Exception.Create('Error in Paint'#13+
gluErrorString(errorCode));


running result:
FormatIndex becomes 0, other variables do not change.

earth_walker
12-28-2003, 04:45 PM
This is another approach, still failed...



FormatIndex := ChoosePixelFormat(GetDC(Handle),@pfd);
SetPixelFormat(GetDC(Handle),FormatIndex,@pfd);
GLContext := wglCreateContext(GetDC(Handle));
wglMakeCurrent(GetDC(Handle),GLContext);

PanzerSchreck
12-29-2003, 12:04 AM
Try this :


DC := GetDC(Handle);
FormatIndex := ChoosePixelFormat(DC,@pfd);
SetPixelFormat(DC,FormatIndex,@pfd);
GLContext := wglCreateContext(DC);
wglMakeCurrent(DC, GLContext);
Using a call to GetDC in every function isn't good, since you'll most likely get different DCs which all take up resource you can't easily free later. And moreover, SetPixelFormat, wglCreateContext and wglMakeCurrent also need the DC instead of just the handle.
If the above approach isn't working, then make sure that your pixelformat is really supported and also valid.

[This message has been edited by PanzerSchreck (edited 12-29-2003).]

earth_walker
01-02-2004, 12:44 PM
Thank you! It works under Win2K now. I will test it in other versions of windows later.

Zengar
01-03-2004, 12:51 AM
It can be also usefull using RaiseOSError(or RaiseLastWin32Error) after every windows API call to catch windows exceptions.

Azazel
01-16-2004, 01:15 AM
I dont see any pfd fields definitions... Dont you knew that at least dwFlags must be set?