Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Fr Jul 18, 2025 19:34

Foren-Übersicht » Programmierung » OpenGL
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 1 Beitrag ] 
Autor Nachricht
 Betreff des Beitrags: Kamera mit Anleitung (2)
BeitragVerfasst: Fr Okt 14, 2005 11:20 
Offline
DGL Member
Benutzeravatar

Registriert: Do Jun 09, 2005 13:48
Beiträge: 117
Wohnort: Sankt Augustin
Hier das Kameramodul:
Code:
  1.  
  2. unit Camera;
  3.  
  4. interface
  5.  
  6.   Uses DglOpenGL, OpenGLUtil, Windows, Classes;
  7.  
  8.   type TCameraMatrix=Class
  9.     Matrix: TArrMatrix;
  10.     InverseMatrix: TArrMatrix;
  11.     procedure Identity;
  12.     procedure Load(M: TArrMatrix);
  13.     constructor Create;
  14.   end;
  15.  
  16.   TCamera=Class
  17.     CameraMatrix: TCameraMatrix;
  18.     Enabled  : boolean;
  19.     Initiated: boolean;
  20.     constructor Create;
  21.     destructor Destroy;
  22.     function Position: TGLvector;
  23.     function UpVector: TGLvector;
  24.     function ViewDirection: TGLvector;
  25.     procedure RestorePosition(pos: integer);
  26.     procedure SavePosition(pos: integer);
  27.     procedure RotateCamera(ix, iy, iz: TGLdouble);
  28.     procedure TranslateCamera(ix, iy, iz: TGLdouble);
  29.     procedure CameraHome;
  30.     procedure PositionCamera(PositionVec: TGLvector;
  31.                  ViewVec: TGLvector;
  32.                  upVec: TGLvector);
  33.     procedure Adjust;
  34.     procedure Apply;
  35.   private
  36.     HomeMatrix: TCameraMatrix;
  37.     PosArray: array [0..9] of TCameraMatrix;
  38.     FPointOfRotation: TGLvector;
  39.     procedure Identity;
  40.     procedure RotateMatrix(ix, iy, iz: TGLdouble);
  41.     procedure Offset(x, y, z: TGLfloat);
  42.     procedure RotateRoundAxis(rx, ry, rz: TGLfloat);
  43.   published
  44.     property PointOfRotation: TGLvector read FPointOfRotation;
  45.   end;
  46.   TPCamera=^TCamera;
  47.  
  48. implementation
  49.  
  50. constructor TCameraMatrix.Create;
  51. begin
  52.   Identity;
  53. end;
  54.  
  55. procedure TCameraMatrix.Identity;
  56. var
  57.   i: integer;
  58. begin
  59.   Matrix := GetArrIdentity;
  60.   InverseMatrix := GetArrIdentity;
  61. end;
  62.  
  63. procedure TCameraMatrix.Load(M: TArrMatrix);
  64. var
  65.   i: integer;
  66. begin
  67.   for i:=0 to 15 do
  68.     Matrix[i]:=M[i];
  69.   InvertMatrix (M, InverseMatrix);
  70. end;
  71.  
  72.  
  73. procedure TCamera.RotateRoundAxis(rx, ry, rz: TGLfloat);
  74. var
  75.   newMatrix: TArrMatrix;
  76. begin
  77.   glMatrixMode (GL_MODELVIEW);
  78.   glPushMatrix();
  79.   glLoadMatrixf(@CameraMatrix.Matrix);
  80.  
  81.   if(rx <> 0) then
  82.     glRotatef(rx,1,0,0);
  83.  
  84.   if(ry <> 0) then
  85.     glRotatef(ry,0,1,0);
  86.  
  87.   if(rz <> 0) then
  88.     glRotatef(rz,0,0,1);
  89.  
  90.   glGetFloatv(GL_MODELVIEW_MATRIX, @newMatrix);
  91.   CameraMatrix.Load(newMatrix);
  92.   glPopMatrix();
  93. end;
  94.  
  95. constructor TCamera.Create;
  96. var
  97.   i: integer;
  98. begin
  99.   Initiated := false;
  100.   CameraMatrix := TCameraMatrix.Create;
  101.   HomeMatrix := TCameraMatrix.Create;
  102.   for i := 0 to 9 do
  103.     PosArray[i] := TCameraMatrix.Create;
  104. end;
  105.  
  106. procedure TCamera.Identity;
  107. begin
  108.   CameraMatrix.Identity;
  109.   HomeMatrix.Identity;
  110.  
  111.   Enabled := true;
  112. end;
  113.  
  114. destructor TCamera.Destroy;
  115. var
  116.   i: integer;
  117. begin
  118.   HomeMatrix.Free;
  119.   CameraMatrix.Free;
  120.   for i := 0 to 9 do
  121.     PosArray[i].Free;
  122. end;
  123.  
  124. procedure TCamera.Offset(x, y, z: TGLfloat);
  125. var
  126.   newMatrix: TArrMatrix;
  127. begin
  128.   glMatrixMode (GL_MODELVIEW);
  129.   glPushMatrix();
  130.   glLoadIdentity;
  131.   glTranslatef(x,y,z);
  132.   glGetFloatv(GL_MODELVIEW_MATRIX, @newMatrix);
  133.   // wenn ich mit der funktion glMultMatrixf arbeite, wird die zeichnung
  134.   // immer entlang der richtigen achsen verschoben. wenn ich matrixmultiply
  135.   // nehme, wird sie auf den bildschirmachsen verschoben. das ist angenehmer
  136.   // zum arbeiten
  137.   MatrixMultiply (newMatrix, CameraMatrix.Matrix, newMatrix);
  138.   CameraMatrix.Load(newMatrix);
  139.   glPopMatrix();
  140. end;
  141.  
  142.  
  143. procedure TCamera.PositionCamera(PositionVec: TGLvector;
  144.                                        ViewVec: TGLvector;
  145.                                        upVec: TGLvector);
  146. var
  147.   newMatrix: TArrMatrix;
  148.   i: integer;
  149. begin
  150.   Identity;
  151.   FPointOfRotation := ViewVec;
  152.  
  153.   glMatrixMode (GL_MODELVIEW);
  154.   glPushMatrix;
  155.   glLoadIdentity;
  156.   glTranslatef (PositionVec.X, PositionVec.Y, PositionVec.Z);
  157.   gluLookAt (PositionVec.X, PositionVec.Y, PositionVec.Z,
  158.              ViewVec.X, ViewVec.Y, ViewVec.Z,
  159.              upVec.X, upVec.Y, upVec.Z);
  160.   glGetFloatv(GL_MODELVIEW_MATRIX, @newMatrix);
  161.   CameraMatrix.Load(newMatrix);
  162.   HomeMatrix.Load(newMatrix);
  163.   glPopMatrix;
  164.  
  165.   if not Initiated then
  166.   begin
  167.     for i := 0 to 9 do
  168.       SavePosition (i);
  169.     Initiated := true;
  170.   end;
  171. end;
  172.  
  173. // Move the camera to the home position
  174. procedure TCamera.CameraHome;
  175. begin
  176.   CameraMatrix.Load(HomeMatrix.Matrix);
  177. end;
  178.  
  179. procedure TCamera.SavePosition (pos: integer);
  180. begin
  181.   if (pos < 0) or (pos > 9) then
  182.     exit;
  183.  
  184.   PosArray[pos].Load(CameraMatrix.Matrix);
  185. end;
  186.  
  187. procedure TCamera.RestorePosition (pos: integer);
  188. begin
  189.   if (pos < 0) or (pos > 9) then
  190.     exit;
  191.  
  192.   CameraMatrix.Load(PosArray[pos].Matrix);
  193. end;
  194.  
  195. procedure TCamera.TranslateCamera(ix, iy, iz: TGLdouble);
  196. begin
  197.   Offset (ix, iy, iz);
  198. end;
  199.  
  200. procedure TCamera.RotateCamera(ix, iy, iz: TGLdouble);
  201. begin
  202.   RotateMatrix (ix, iy, iz);
  203. end;
  204.  
  205. procedure TCamera.Apply;
  206. begin
  207.   if not Enabled then
  208.     exit;
  209.  
  210.   glMatrixMode (GL_MODELVIEW);
  211.   glLoadMatrixf(@CameraMatrix.Matrix);
  212.   glTranslatef (-PointOfRotation.X,
  213.                 -PointOfRotation.y,
  214.                 -PointOfRotation.Z);
  215. end;
  216.  
  217. procedure TCamera.RotateMatrix (ix, iy, iz: TGLdouble);
  218. begin
  219.   RotateRoundAxis (iy, ix, iz);
  220. end;
  221.  
  222. function TCamera.Position: TGLvector;
  223. var
  224.   return: TGLvector;
  225. begin
  226.   // position: letzte Spalte der Matrix
  227.   InitVector (return,
  228.               CameraMatrix.Matrix[12],
  229.               CameraMatrix.Matrix[13],
  230.               CameraMatrix.Matrix[14]);
  231.   result := return;
  232. end;
  233.  
  234. function TCamera.ViewDirection: TGLvector;
  235. var
  236.   return: TGLvector;
  237. begin
  238.   // view direction: dritte Spalte der Matrix (Z-Achse)
  239.   InitVector (return,
  240.               CameraMatrix.Matrix[08],
  241.               CameraMatrix.Matrix[09],
  242.               CameraMatrix.Matrix[10]);
  243.   result := return;
  244. end;
  245.  
  246. function TCamera.UpVector: TGLvector;
  247. var
  248.   return: TGLvector;
  249. begin
  250.   // upVector: zweite Spalte der Matrix (Y-Achse)
  251.   InitVector (return,
  252.               CameraMatrix.Matrix[04],
  253.               CameraMatrix.Matrix[05],
  254.               CameraMatrix.Matrix[06]);
  255.  result := return;
  256. end;
  257.  
  258. procedure TCamera.Adjust;
  259. var
  260.   temp: TArrMatrix;
  261. begin
  262.   // kamera senkrecht zur Y-Achse ausrichten
  263.   // position beibehalten
  264.   temp[00] := 1;
  265.   temp[01] := 0;
  266.   temp[02] := 0;
  267.   temp[03] := 0;
  268.   temp[04] := 0;
  269.   temp[05] := 1;
  270.   temp[06] := 0;
  271.   temp[07] := 0;
  272.   temp[08] := 0;
  273.   temp[09] := 0;
  274.   temp[10] := 1;
  275.   temp[11] := 0;
  276.   temp[12] := CameraMatrix.Matrix[12];
  277.   temp[13] := CameraMatrix.Matrix[13];
  278.   temp[14] := CameraMatrix.Matrix[14];
  279.   temp[15] := CameraMatrix.Matrix[15];
  280.   CameraMatrix.Load(temp);
  281. end;
  282.  
  283. end.
  284.  


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 1 Beitrag ] 
Foren-Übersicht » Programmierung » OpenGL


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 4 Gäste


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.008s | 14 Queries | GZIP : On ]