PDA

View Full Version : Initializing OpenGL...



HopeDagger
13-02-2004, 05:19 AM
I'm just starting out with OpenGL, and I've got some code here that's giving me some trouble. I've got the proper .dlls for OpenGL and GLUT, so I'm sure that's not the problem. What happens is that I get an exception (access violation, etc) when I try to run it. I'm pretty sure the problem lies in the initialization of the window:



unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OpenGL, Glut;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
w,h: GLint;

implementation

{$R *.dfm}

procedure SetRenderContext;
begin
glClearColor(0.9,0.4,0.2,1.0);
end;

procedure RenderIt; cdecl;
begin
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
w := 640;
h := 480;
glutInitDisplayMode(GLUT_DOUBLE or GLUT_RGBA);
glutInitWindowSize(w,h);
glutInitWindowPosition((Screen.Width-Form1.Width) div 2,(Screen.Height-Form1.Height) div 2);
glutCreateWindow('Generic Window');
glutDisplayFunc(RenderIt);
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
SetRenderContext;
glutMainLoop;
end;

end.


I'm sure not all of that is needed, but I thought that I should post it all for completeness.

Thanks in advance!

noeska
13-02-2004, 06:43 PM
i dont think you can combine the glut commands you use from within a form. Maybe you can take a look a the courses available on http://nehe.gamedev.net. They are also available for delphi. Or take a look at http://www.sulaco.co.za.

TheLion
13-02-2004, 07:25 PM
I think you need to turn off certain errors in Delphi to use Glut... no clue if they improved it now, but a long time ago I tried using Glut and ran into a lot of problems :-x

HopeDagger
13-02-2004, 08:40 PM
Nevermind! I managed to locate the line that I need:



glutInit(MS_LIB);


Thanks anyways. :)

EDIT
Is there a way to set a procedure to be called or something that will allow me to close the Form after the OpenGL window has been closed? It's pretty annoying to have to close both windows each time I run a program. :P

noeska
13-02-2004, 11:17 PM
you should not combine glut procedures together with a form. glut is for use with a console like program. That wont leave you with an open form. But if you want to use it with a form you could call application.terminate directly after your glutmainloop.

tux
13-02-2004, 11:49 PM
you should use the form template from sulaco if u want to draw on forms, i sent in a opengl 1.5 updated version to him and its on the site now. that works really well :)

HopeDagger
13-02-2004, 11:59 PM
you should not combine glut procedures together with a form. glut is for use with a console like program. That wont leave you with an open form. But if you want to use it with a form you could call application.terminate directly after your glutmainloop.

Thanks for the advice. I recreated the program as a console application, but I'm still having the same problem. This time the console window stays active after I close the OpenGL window. Arg! :(

I'll supply the code just in case I'm doing something wrong:



var
w,h: GLint;

procedure Startup;
begin
glClearColor(0.9,0.4,0.2,1.0);
end;

procedure Render; cdecl;
begin
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers;
end;

begin
w := 640;
h := 480;
glutInit(MS_LIB);
glutInitDisplayMode(GLUT_DOUBLE or GLUT_RGBA);
glutInitWindowSize(w,h);
glutInitWindowPosition(100,100);
glutCreateWindow('Generic Window!');
glutDisplayFunc(Render);
Startup;
glutMainLoop;
end.


Thanks in advance!

tux
14-02-2004, 12:57 AM
i dont know anything about glut but shouldnt it look more like...


[code]
begin
{.. setup gl.. }

while not CanClose do
Begin
{.. render ..}
end;

{.. destroy gl.. }
end;//console closes here

HopeDagger
14-02-2004, 01:57 AM
[code]
begin
{.. setup gl.. }

while not CanClose do
Begin
{.. render ..}
end;

{.. destroy gl.. }
end;//console closes here

Nope. glutMainLoop() does the loop, and calls the glutDisplayFunc() specified when it comes time to render. Conveinent, no? :)

Also: It seems that when I include glut32.dll in the same directory as the .exe, the console window closes when the OpenGL window closes. Otherwise, the console window remains active. Weirdness.

noeska
14-02-2004, 09:46 AM
on this site you will find some delphi glut examples: http://hjem.get2net.dk/mithrandir/OpenGL/opengl.html
If you are anoyed with windows staying open you might want to use the sulaco templates. Either the one with form or the one without the form.

tux
14-02-2004, 09:09 PM
Nope. glutMainLoop() does the loop, and calls the glutDisplayFunc() specified when it comes time to render. Conveinent, no? :)


no i would call it anoying :D i would prefer to handle it myself :)