OpenGL cannot be initialized in Delphi5

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:

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

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.

This is another approach, still failed…

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

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).]

Thank you! It works under Win2K now. I will test it in other versions of windows later.

It can be also usefull using RaiseOSError(or RaiseLastWin32Error) after every windows API call to catch windows exceptions.

I dont see any pfd fields definitions… Dont you knew that at least dwFlags must be set?