MG: added synchronisation to gtkglarea_demo

git-svn-id: trunk@374 -
This commit is contained in:
lazarus 2001-10-31 10:50:32 +00:00
parent 82aca78edd
commit 4f24702fca

View File

@ -18,6 +18,8 @@
* * * *
*************************************************************************** ***************************************************************************
written 2001 by Satan
} }
unit ExampleForm; unit ExampleForm;
@ -27,7 +29,7 @@ interface
uses uses
Classes, SysUtils, GTKGlArea, Forms, LResources, Buttons, StdCtrls, Classes, SysUtils, GTKGlArea, Forms, LResources, Buttons, StdCtrls,
gtkglarea_int, gtk, glib, NVGL; gtkglarea_int, gtk, glib, NVGL, linux;
type type
TglTexture = class TglTexture = class
@ -72,7 +74,7 @@ type
TParticleEngine = class TParticleEngine = class
//x, y, z: GLfloat; //x, y, z: GLfloat;
xspawn: GLfloat; xspawn: GLfloat;
Particle: array [1..501] of TParticle; Particle: array [1..2001] of TParticle;
procedure MoveParticles; procedure MoveParticles;
procedure DrawParticles; procedure DrawParticles;
//procedure Init; //procedure Init;
@ -97,12 +99,13 @@ var AnExampleForm: TExampleForm;
ParticleList, CubeList, BackList: GLuint; ParticleList, CubeList, BackList: GLuint;
var direction: boolean; var direction: boolean;
hour, minutes, secs, msecs, usecs, mmsecs: word;
timer: single;
implementation implementation
function LoadFileToMemStream(Filename: string): TMemoryStream; function LoadFileToMemStream(const Filename: string): TMemoryStream;
var FileStream: TFileStream; var FileStream: TFileStream;
begin begin
Result:=TMemoryStream.Create; Result:=TMemoryStream.Create;
@ -120,7 +123,7 @@ begin
end; end;
end; end;
function LoadglTexImage2DFromBitmapFile(Filename:string; function LoadglTexImage2DFromBitmapFile(const Filename:string;
var Image:TglTexture): boolean; var Image:TglTexture): boolean;
type type
TBITMAPFILEHEADER = packed record TBITMAPFILEHEADER = packed record
@ -223,14 +226,13 @@ begin
try try
for i:=0 to PixelCount-1 do begin for i:=0 to PixelCount-1 do begin
MemStream.Read(AnRGBQuad,sizeOf(RGBQuad)); MemStream.Read(AnRGBQuad,sizeOf(RGBQuad));
{$IFOPT R+}{$DEFINE RangeCheckOn}{$ENDIF} {$IFOPT R+}{$DEFINE RangeCheckOn}{$R-}{$ENDIF}
{$R-}
with PRawImage(Image.Data)^ do begin with PRawImage(Image.Data)^ do begin
p[i*3+0]:=AnRGBQuad.rgbRed; p[i*3+0]:=AnRGBQuad.rgbRed;
p[i*3+1]:=AnRGBQuad.rgbGreen; p[i*3+1]:=AnRGBQuad.rgbGreen;
p[i*3+2]:=AnRGBQuad.rgbBlue; p[i*3+2]:=AnRGBQuad.rgbBlue;
end; end;
{$IFDEF RangeCheckOn}{$R+}{$ELSE}{$R-}{$ENDIF} {$IFDEF RangeCheckOn}{$R+}{$ENDIF}
end; end;
except except
writeln('Error converting bitmap'); writeln('Error converting bitmap');
@ -368,7 +370,7 @@ end;
constructor TParticleEngine.Create; constructor TParticleEngine.Create;
var i: integer; var i: integer;
begin begin
for i:=1 to 501 do Particle[i]:=TParticle.Create; for i:=1 to 2001 do Particle[i]:=TParticle.Create;
xspawn:=0; xspawn:=0;
end; end;
@ -377,7 +379,8 @@ var i: integer;
begin begin
//if blended then glEnable(GL_DEPTH_TEST) else glEnable(GL_BLEND); //if blended then glEnable(GL_DEPTH_TEST) else glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, textures[0]); glBindTexture(GL_TEXTURE_2D, textures[0]);
for i:=1 to 501 do begin for i:=1 to 2001 do begin
glPushMatrix;
glTranslatef(Particle[i].x, Particle[i].y, Particle[i].z); glTranslatef(Particle[i].x, Particle[i].y, Particle[i].z);
glCallList(ParticleList); glCallList(ParticleList);
{glBegin(GL_TRIANGLE_STRIP); {glBegin(GL_TRIANGLE_STRIP);
@ -387,21 +390,22 @@ begin
glTexCoord2f( 1.0, 0.0); glVertex3f(Particle[i].x+0.03, Particle[i].y-0.03, Particle[i].z); glTexCoord2f( 1.0, 0.0); glVertex3f(Particle[i].x+0.03, Particle[i].y-0.03, Particle[i].z);
glTexCoord2f( 0.0, 0.0); glVertex3f(Particle[i].x-0.03, Particle[i].y-0.03, Particle[i].z); glTexCoord2f( 0.0, 0.0); glVertex3f(Particle[i].x-0.03, Particle[i].y-0.03, Particle[i].z);
glEnd;} glEnd;}
glTranslatef(-Particle[i].x, -Particle[i].y, -Particle[i].z); glPopMatrix;
//glTranslatef(-Particle[i].x, -Particle[i].y, -Particle[i].z);
end; end;
//if blended then glDisable(GL_DEPTH_TEST) else glDisable(GL_BLEND); //if blended then glDisable(GL_DEPTH_TEST) else glDisable(GL_BLEND);
end; end;
procedure TParticleEngine.RespawnParticle(i: integer); procedure TParticleEngine.RespawnParticle(i: integer);
begin begin
{if (xspawn>2) and (direction=true) then direction:=false; if (xspawn>2) and (direction=true) then direction:=false;
if (xspawn<-2) and (direction=false) then direction:=true; if (xspawn<-2) and (direction=false) then direction:=true;
if direction then xspawn:=xspawn+0.005 else xspawn:=xspawn-0.005;} if direction then xspawn:=xspawn+0.0002*(timer/10) else xspawn:=xspawn-0.0002*(timer/10);
Particle[i].x:=xspawn; Particle[i].x:=xspawn;
Particle[i].y:=-0.5; Particle[i].y:=-0.5;
Particle[i].z:=0; Particle[i].z:=0;
Particle[i].vx:=-0.005+random(2000)/200000; Particle[i].vx:=-0.005+random(2000)/200000;
Particle[i].vy:=0.03+random(750)/100000; Particle[i].vy:=0.035+random(750)/100000;
Particle[i].vz:=-0.005+random(2000)/200000; Particle[i].vz:=-0.005+random(2000)/200000;
Particle[i].life:=random(1250)/1000+1; Particle[i].life:=random(1250)/1000+1;
end; end;
@ -409,15 +413,15 @@ end;
procedure TParticleEngine.MoveParticles; procedure TParticleEngine.MoveParticles;
var i: integer; var i: integer;
begin begin
for i:=1 to 501 do begin for i:=1 to 2001 do begin
if Particle[i].life>0 then begin if Particle[i].life>0 then begin
Particle[i].life:=Particle[i].life-0.01; Particle[i].life:=Particle[i].life-0.01*(timer/10);
Particle[i].x:=Particle[i].x+Particle[i].vx; Particle[i].x:=Particle[i].x+Particle[i].vx*(timer/10);
Particle[i].vy:=Particle[i].vy-0.00035; // gravity Particle[i].vy:=Particle[i].vy-0.00035*(timer/10); // gravity
Particle[i].y:=Particle[i].y+Particle[i].vy; Particle[i].y:=Particle[i].y+Particle[i].vy*(timer/10);
Particle[i].z:=Particle[i].z+Particle[i].vz; Particle[i].z:=Particle[i].z+Particle[i].vz*(timer/10);
end else begin end else begin
RespawnParticle(i); RespawnParticle(i);
end; end;
@ -427,7 +431,7 @@ end;
procedure TParticleEngine.Start; procedure TParticleEngine.Start;
var i: integer; var i: integer;
begin begin
for i:=1 to 501 do begin for i:=1 to 2001 do begin
RespawnParticle(i); RespawnParticle(i);
end; end;
end; end;
@ -763,11 +767,16 @@ begin
AreaInitialized:=true; AreaInitialized:=true;
end; end;
GetTime(hour, minutes, secs, msecs, usecs);
timer:=msecs-mmsecs;
if timer<0 then timer:=1000+timer;
mmsecs:=msecs;
ParticleEngine.MoveParticles; ParticleEngine.MoveParticles;
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glLoadIdentity; { clear the matrix } glLoadIdentity; { clear the matrix }
glTranslatef (0.0, 0.0,-2.5); // -2.5); { viewing transformation } glTranslatef (0.0, 0.0,-3.0); // -2.5); { viewing transformation }
glScalef (1.0, 1.0, 1.0); { modeling transformation } glScalef (1.0, 1.0, 1.0); { modeling transformation }
{rotate} {rotate}
@ -777,9 +786,9 @@ begin
glPushMatrix; glPushMatrix;
if MoveBackground then begin if MoveBackground then begin
rrx:=rrx-0.6; rrx:=rrx-0.6*(timer/10);
rry:=rry-0.5; rry:=rry-0.5*(timer/10);
rrz:=rrz-0.3; rrz:=rrz-0.3*(timer/10);
end; end;
glRotatef(rrx,1.0,0.0,0.0); glRotatef(rrx,1.0,0.0,0.0);
@ -802,9 +811,9 @@ begin
glPushMatrix; glPushMatrix;
if MoveCube then begin if MoveCube then begin
rx:=rx+0.5; rx:=rx+0.5*(timer/10);
ry:=ry+0.25; ry:=ry+0.25*(timer/10);
rz:=rz+0.8; rz:=rz+0.8*(timer/10);
end; end;
glRotatef(rx,1.0,0.0,0.0); glRotatef(rx,1.0,0.0,0.0);