Entschuldigung für meine späte Antwort...ich war die letzten Tage damit beschäftigz OpenGl 3.3 zum laufen zu bringen!!! Mein erstes Dreieck..
so.. hier mal meine komplette Kameraklasse...
Code: Alles auswählen
unit uOpemGLKamera;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
ExtCtrls,OpenGLContext, dglOpenGL, math, Windows,video,keyboard;
type
{ tKamera }
{ tOpenGlKamera }
tOpenGlKamera = class
private
fOpenGLControl : TOpenGLControl;
Matrix : Array[0..15] of glFloat;
fKey_vor : char;
fKey_zurueck : char;
fKey_strafe_links : char;
fKey_strafe_rechts : char;
fKey_hoch : char;
fKey_runter : char;
fKey_drehen_links : char;
fKey_drehen_rechts : char;
fKey_drehen_hoch : char;
fKey_drehen_runter : char;
fmovespeed : glFloat;
fRot_Speed : glFloat;
pi : glFloat;
PosX_Aktuell : glFloat;
PosY_Aktuell : glFloat;
PosZ_Aktuell : glFloat;
RotY, RotZ : glFloat;
CursorPos : TSmallPoint;
public
constructor create(OGL: TOpenGLControl);
destructor free;
procedure Update; // kommt dorthin wo gerendert wird
procedure KeyPress(Key : char); // kommt ins OnKeyPress Event des Forms, oder OpenglContext ?
procedure MouseMove(x,y : integer); //kommt ins On MouseMove Event von OpenGLContext
procedure SetKey_Bewegung(vor,zurueck,strafe_rechts,strafe_links,hoch,runter : char); //Wird im Create vorbelegt
procedure SetKey_Drehung(links, rechts, oben, unten : char); //Wird im Create vorbelegt
procedure SetMoveSpeed(value : glFloat); //Wird im Create vorbelegt
procedure SetRotSpeed(value : glFloat); //Wird im Create vorbelegt
procedure SetKameraPos(PosX, PosY, PosZ : GLFloat); //Wird im Create vorbelegt
end;
implementation
{ tOpenGlKamera }
constructor tOpenGlKamera.create(OGL: TOpenGLControl);
begin
fOpenGLControl:=OGL;
fKey_vor :='w';
fKey_zurueck :='s';
fKey_strafe_links :='a';
fKey_strafe_rechts :='d';
fKey_hoch :='q';
fKey_runter :='e';
fKey_drehen_links :='f';
fKey_drehen_rechts :='h';
fKey_drehen_hoch :='t';
fKey_drehen_runter :='g';
fmovespeed:=0.1;
fRot_Speed :=1.0;
PosX_Aktuell :=0;
PosY_Aktuell :=0;
PosZ_Aktuell :=-15;
pi := arccos(-1);
CursorPos.X:=fOpenGLControl.Width;
CursorPos.Y:=fOpenGLControl.Height;
end;
destructor tOpenGlKamera.free;
begin
end;
procedure tOpenGlKamera.SetKameraPos(PosX, PosY, PosZ : GLFloat);
begin
PosX_Aktuell :=PosX;
PosY_Aktuell :=PosY;
PosZ_Aktuell :=PosZ;
end;
procedure tOpenGlKamera.Update;
begin
matrix[0]:=cos(RotZ*pi/180);
matrix[1]:=sin(RotY*PI/180)*-sin(RotZ*PI/180);
matrix[2]:=cos(RotY*PI/180)*-sin(RotZ*PI/180);
matrix[3]:=0;
matrix[4]:=0;
matrix[5]:=cos(RotY*PI/180);
matrix[6]:=-sin(RotY*PI/180);
matrix[7]:=0;
matrix[8]:=sin(RotZ*PI/180);
matrix[9]:=sin(RotY*PI/180)*cos(RotZ*PI/180);
matrix[10]:=cos(RotY*PI/180)*cos(RotZ*PI/180);
matrix[11]:=0;
matrix[12]:=PosX_Aktuell*cos(RotZ*PI/180)+PosZ_Aktuell*sin(RotZ*PI/180);
matrix[13]:=-PosX_Aktuell*sin(RotY*PI/180)*sin(RotZ*PI/180)+PosZ_Aktuell*sin(RotY*PI/180)*cos(RotZ*PI/180)+PosY_Aktuell*cos(RotY*PI/180);
matrix[14]:=-PosX_Aktuell*cos(RotY*PI/180)*sin(RotZ*PI/180)+PosZ_Aktuell*cos(RotY*PI/180)*cos(RotZ*PI/180)-PosY_Aktuell*sin(RotY*PI/180);
matrix[15]:=1;
glLoadMatrixf(matrix);
end;
procedure tOpenGlKamera.KeyPress(Key: char);
begin
if Key = fKey_vor then //Vorwärtsbewegung
begin
PosX_Aktuell+=fmovespeed*sin(-RotZ*PI/180);
PosZ_Aktuell+=fmovespeed*cos(-RotZ*PI/180);
end;
if Key = fKey_zurueck then //Rückwärtsbewegung
begin
PosX_Aktuell-=fmovespeed*sin(-RotZ*PI/180);
PosZ_Aktuell-=fmovespeed*cos(-RotZ*PI/180);
end;
if Key = fKey_strafe_links then //Strafe Links
begin
PosX_Aktuell+=fmovespeed*sin((-RotZ+90)*PI/180);
PosZ_Aktuell+=fmovespeed*cos((-RotZ+90)*PI/180);
end;
if Key = fKey_strafe_rechts then //Strafe Rechts
begin
PosX_Aktuell-=fmovespeed*sin((-RotZ+90)*PI/180);
PosZ_Aktuell-=fmovespeed*cos((-RotZ+90)*PI/180);
end;
if Key = fKey_hoch then //Hochschweben
begin
PosY_Aktuell-=fmovespeed;
end;
if Key = fKey_runter then //Runterschweben
begin
PosY_Aktuell+=fmovespeed;
end;
if Key = fKey_drehen_hoch then //Hochschauen
begin
RotY:=RotY+fRot_speed;
If RotY>45 then RotY:=45;
end;
if Key = fKey_drehen_runter then //Runterschauen
begin
RotY:=RotY-fRot_speed;
If RotY<-45 then RotY:=-45;
end;
if Key = fKey_drehen_links then //Linkschauen
begin
RotZ:=RotZ-fRot_speed;
If RotZ<-360 then RotZ:=-360;
end;
if Key = fKey_drehen_rechts then //Rechtsschauen
begin
RotZ:=RotZ+fRot_speed;
If RotZ>360 then RotZ:=360;
end;
end;
procedure tOpenGlKamera.MouseMove(x, y: integer);
var NewCursorPos: TSmallPoint;
begin
NewCursorPos.x:=x;
NewCursorPos.y:=y;
if CursorPos.y-NewCursorPos.y>0 then //hochschauen
begin
RotY:=RotY+fRot_speed;
If RotY>45 then RotY:=45; //Kopf-Nick-Begrenzung
end;
if CursorPos.y-NewCursorPos.y<0 then //Runterschauen
begin
RotY:=RotY-fRot_speed;
If RotY<-45 then RotY:=-45; //Kopf-Nick-Begrenzung
end;
if CursorPos.x-NewCursorPos.x>0 then //Rechtsschauen
begin
RotZ:=RotZ+fRot_speed;
If RotZ>360 then RotZ:=360;
end;
if CursorPos.x-NewCursorPos.x<0 then //Linkschauen
begin
RotZ:=RotZ-fRot_speed;
If RotZ<-360 then RotZ:=-360;
end;
SetCursorPos(fOpenGLControl.Width div 2,fOpenGLControl.Height div 2);
CursorPos.X:=fOpenGLControl.Width div 2;
CursorPos.Y:=fOpenGLControl.Height div 2;
end;
procedure tOpenGlKamera.SetKey_Bewegung(vor, zurueck, strafe_rechts, strafe_links, hoch,
runter: char);
begin
fKey_vor :=vor;
fKey_zurueck :=zurueck;
fKey_strafe_links :=strafe_links;
fKey_strafe_rechts :=strafe_rechts;
fKey_hoch :=hoch;
fKey_runter :=runter;
end;
procedure tOpenGlKamera.SetKey_Drehung(links, rechts, oben, unten: char);
begin
fKey_drehen_links :=links;
fKey_drehen_rechts :=rechts;
fKey_drehen_hoch :=oben;
fKey_drehen_runter :=unten;
end;
procedure tOpenGlKamera.SetMoveSpeed(value: glFloat);
begin
fmovespeed:=value;
end;
procedure tOpenGlKamera.SetRotSpeed(value: glFloat);
begin
fRot_Speed:=value;
end;
end.