problem: got an OpenGL error at the first line of OnCreate event.

Hi!

I made a procedure to trap the OpenGL errors.

procedure TForm1.finderr;
{some code}
private
errorcode: GLenum;
{some code}
procedure TForm1.finderr;
begin
errorcode:= glGetError;
if errorcode<>GL_NO_ERROR then
MessageDlg(‘glerr:’+ gluErrorString(errorcode),
mtInformation, [mbOK], 0);
end;

then I call finderr routine at the first line of OnCreate event of the form. The whole project only have one form.
An error message still pops up when I run the program. Message is “Invalid Operation”.

I don’t know where is the error made, because I think this is the first point the the whole execution path.

Can you gie me some idea?

Thank you!

you cannot call glgeterror before you have an active context… to call it is an invalid operation, so thats why you get that message

Thank you!

I tried to insert “finderr;” at different positions(indicated in the code}, every time only one “finderr;” in the whole program. However, I still got the same error messages each time. What can I do now?

procedure TForm1.FormCreate(Sender: TObject);
var
pfd: TPixelFormatDescriptor;
FormatIndex: integer;
begin
{finderr}
fillchar(pfd, SizeOf(pfd),0);
{finderr}
with pfd do
begin
nSize := sizeOf(pfd);
nVersion := 1;
dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL;
iPixelType := PFD_TYPE_RGBA;
cColorBits := 24;
cDepthBits := 32;
iLayerType := PFD_MAIN_PLANE;
end;
{finderr}
FormatIndex := ChoosePixelFormat(Canvas.Handle,@pfd);
SetPixelFormat(Canvas.Handle,FormatIndex, @pfd);
{finderr}
GLContext := wglCreateContext(Canvas.Handle);
{finderr}
wglMakeCurrent(Canvas.Handle, GLContext);
{finderr}
{some code}
end;

only the last err check is valid…

oh, thank you! I will check that procedure’s documentation.

Hi! This time I didn’t use “finderr” function to test that error, but indivitually test output of each commands, with the following code. It pops up “Error2”, which means the command:
GLContext := wglCreateContext(Form1.Canvas.Handle);
has problem, what shall I do?

Thank you!

PS: I already tested SetPixelFormat and it returns true.

procedure TForm1.FormCreate(Sender: TObject);
var
pfd: TPixelFormatDescriptor;
FormatIndex: integer;
begin
fillchar(pfd, SizeOf(pfd),0);
with pfd do
begin
nSize := sizeOf(pfd);
nVersion := 1;
dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL;
iPixelType := PFD_TYPE_RGBA;
cColorBits := 24;
cDepthBits := 32;
iLayerType := PFD_MAIN_PLANE;
end;{with}
FormatIndex := ChoosePixelFormat(Form1.Canvas.Handle,@pfd);
if (FormatIndex=0) then MessageDlg(‘Error1’,mtInformation,[mbOK], 0);
SetPixelFormat(Form1.Canvas.Handle,FormatIndex, @pfd);
GLContext := wglCreateContext(Form1.Canvas.Handle);
if (GLContext=0) then MessageDlg('Error2’mtInformation,[mbOK],0);

That should work in most opengl implementations, however, opengl actually wants the ‘CS_OWNDC’ flag to be set for thw window that should contain the opengl window.

One easy way to get a window with that flag is to use my Draw control, its just a canvas with owndc set in it, since it doesnt like that the DC handle can be different each time if you do like that…
how ever its unprobable that it should be an error when all is called in the same function.

But if you want to try it :
http://mazy.annat.nu/files/Delphi/Components/draw.pas

Install that and just put it on the window and use Draw1.canvas.handle instead of the forms one and try if it could be that error.