Autor Beitrag
mathias
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 58
Erhaltene Danke: 3



BeitragVerfasst: Mi 12.06.02 21:05 
Ein kleines Beispiel mit einem drehenden Würfel.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
unit unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  OpenGL, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    hrc: HGLRC;
    DC : hdc;
    procedure DrawScene;
  public
  end;

var
  Form1: TForm1;
  winkel1, winkel2, winkel3 : Integer;

implementation

{$R *.DFM}

procedure SetDCPixelFormat(Handle: HDC);
var
  pfd: TPixelFormatDescriptor;
  nPixelFormat: Integer;

begin
  FillChar(pfd, SizeOf(pfd), 0);
  with pfd do begin
    nSize     := sizeof(pfd);                               // Size of this structure
    nVersion  := 1;                                         // Version number
    dwFlags   := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;  // Flags
    iPixelType:= PFD_TYPE_RGBA;                             // RGBA pixel values
    cColorBits:= 24;                                        // 24-bit color
    cDepthBits:= 32;                                        // 32-bit depth buffer
    iLayerType:= PFD_MAIN_PLANE;                            // Layer type
  end;
  nPixelFormat := ChoosePixelFormat(Handle, @pfd);
  SetPixelFormat(Handle, nPixelFormat, @pfd);
end;

procedure TForm1.DrawScene;
begin
  glEnable(GL_DEPTH_TEST);
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity;
  glTranslatef(00, -10);

  glRotatef(winkel1, 100);
  glRotatef(winkel2, 010);
  glRotatef(winkel3, 001);

  glBegin(GL_POLYGON);
    glColor3f(001);
    glVertex3f( 1.0,  1.0,  1.0);
    glVertex3f(-1.0,  1.0,  1.0);
    glVertex3f(-1.0, -1.0,  1.0);
    glVertex3f( 1.0, -1.0,  1.0);
  glEnd;

  glBegin(GL_POLYGON);
    glColor3f(110);
    glVertex3f( 1.0,  1.0, -1.0);
    glVertex3f( 1.0, -1.0, -1.0);
    glVertex3f(-1.0, -1.0, -1.0);
    glVertex3f(-1.0,  1.0, -1.0);
  glEnd;

  glBegin(GL_POLYGON);
    glColor3f(101);
    glVertex3f(-1.0,  1.0,  1.0);
    glVertex3f(-1.0,  1.0, -1.0);
    glVertex3f(-1.0, -1.0, -1.0);
    glVertex3f(-1.0, -1.0,  1.0);
  glEnd;

  glBegin(GL_POLYGON);
    glColor3f(011);
    glVertex3f( 1.0,  1.0,  1.0);
    glVertex3f( 1.0, -1.0,  1.0);
    glVertex3f( 1.0, -1.0, -1.0);
    glVertex3f( 1.0,  1.0, -1.0);
  glEnd;

  glBegin(GL_POLYGON);
    glColor3f(010);
    glVertex3f(-1.0,  1.0, -1.0);
    glVertex3f(-1.0,  1.0,  1.0);
    glVertex3f( 1.0,  1.0,  1.0);
    glVertex3f( 1.0,  1.0, -1.0);
  glEnd;

  glBegin(GL_POLYGON);
    glColor3f(100);
    glVertex3f(-1.0, -1.0, -1.0);
    glVertex3f( 1.0, -1.0, -1.0);
    glVertex3f( 1.0, -1.0,  1.0);
    glVertex3f(-1.0, -1.0,  1.0);
  glEnd;

  SwapBuffers(DC);  // Von Schattenspeicher Flimmerfrei in den Canvas kopieren
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DC := GetDC(Handle);
  SetDCPixelFormat(Canvas.Handle);
  hrc := wglCreateContext(Canvas.Handle);
end;

procedure TForm1.FormResize(Sender: TObject);
var
  gldAspect : GLdouble;

begin
  wglMakeCurrent(DC, hrc);
  gldAspect := Width / Height;  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity;
  gluPerspective(30.0, gldAspect, 1.050.0);
  glViewport(00, Width, Height);
  wglMakeCurrent(00);
  Invalidate;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  wglMakeCurrent(DC, hrc);
  DrawScene;
  wglMakeCurrent(00);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  wglDeleteContext(hrc);
  ReleaseDC(Handle, DC);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Inc(winkel1); if Winkel1 >= 360 then Dec(winkel1, 360);
  Inc(winkel2); if Winkel2 >= 360 then Dec(winkel2, 360);
  Inc(winkel3); if Winkel3 >= 360 then Dec(winkel3, 360);
  FormPaint(sender);
end;

end.


Zuletzt bearbeitet von mathias am Fr 21.06.02 20:49, insgesamt 2-mal bearbeitet
AvadeX
Hält's aus hier
Beiträge: 10



BeitragVerfasst: So 16.06.02 11:59 
MIt DirectX sieht das ganze so aus:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
unit DX5;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls,
  DXClass, DXDraws, DirectX;


const EckZahl = 8; SeitenZahl = 6; VertexZahl = 24;
      xAchse = 1; yAchse = 2; zAchse = 3;


function GetD3DVector (x, y, z: TD3DValue): TD3DVector;
function GetD3DVertex
         (Ecke, Seite: TD3DVector; texU, texV: TD3DValue): TD3DVertex;

type
  TD3DPoly = class
    QEck  : Array[1..EckZahl]    of TD3DVector;
    QSeite: Array[1..SeitenZahl] of TD3DVector;
    Vertex: Array[1..VertexZahl] of TD3DVertex;
    constructor Create;
    procedure SetVectors;
    procedure SetVertices;
  end;

  TD3DKoords = class
    WorldMatrix,
    ViewMatrix,
    ProjectionMatrix: TD3DMatrix;
    constructor Create;
    procedure SetMatrices;
    procedure SetRotation (Achse: Integer);
  end;

  TForm1 = class(TDXForm)
    DXDraw1: TDXDraw;
    DXTimer1: TDXTimer;
    procedure FormCreate(Sender: TObject);
    procedure DXDraw1InitializeSurface(Sender: TObject);
    procedure DXTimer1Timer(Sender: TObject; LagCount: Integer);
    procedure TurnQuad (Richtung: Integer);
  private
    { Private-Deklarationen }
    Quader: TD3DPoly;
    Koords: TD3DKoords;
    Seite : TD3DRect;
    Achse : Integer;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function GetD3DVector(x, y, z: TD3DValue): TD3DVector;
begin
  Result.x := x;
  Result.y := y;
  Result.z := z;
end;

function GetD3DVertex
  (Ecke, Seite: TD3DVector; texU, texV: TD3DValue): TD3DVertex;
begin
  with Result do
  begin
    x := Ecke.x; nx := Seite.x;
    y := Ecke.y; ny := Seite.y;
    z := Ecke.z; nz := Seite.z;
    tu := texU;  tv := texV;
  end;
end;

constructor TD3DPoly.Create;
begin
  SetVectors;
  SetVertices;
end;

procedure TD3DPoly.SetVectors;
begin
  // Normalwerte für Ecken
  QEck[1] := GetD3DVector (-1.01.0,-1.0);  // vorn   links  oben
  QEck[2] := GetD3DVector ( 1.01.0,-1.0);  // vorn   rechts oben
  QEck[3] := GetD3DVector ( 1.0,-1.0,-1.0);  // vorn   rechts unten
  QEck[4] := GetD3DVector (-1.0,-1.0,-1.0);  // vorn   links  unten
  QEck[5] := GetD3DVector (-1.01.01.0);  // hinten links  oben
  QEck[6] := GetD3DVector ( 1.01.01.0);  // hinten rechts oben
  QEck[7] := GetD3DVector ( 1.0,-1.01.0);  // hinten rechts unten
  QEck[8] := GetD3DVector (-1.0,-1.01.0);  // hinten links  unten
  // Normalwerte für Seiten (Faces)
  QSeite[1] := GetD3DVector ( 0.00.0,-1.0);  // vorn   (front)
  QSeite[2] := GetD3DVector ( 0.00.01.0);  // hinten (back)
  QSeite[3] := GetD3DVector (-1.00.00.0);  // links  (left)
  QSeite[4] := GetD3DVector ( 1.00.00.0);  // rechts (right)
  QSeite[5] := GetD3DVector ( 0.01.00.0);  // oben   (top)
  QSeite[6] := GetD3DVector ( 0.0,-1.00.0);  // unten  (bottom)
end;

procedure TD3DPoly.SetVertices;
begin
  // Vorderseite
  Vertex[1]  := GetD3DVertex (QEck[1], QSeite[1], 0.00.0);
  Vertex[2]  := GetD3DVertex (QEck[2], QSeite[1], 0.01.0);
  Vertex[3]  := GetD3DVertex (QEck[3], QSeite[1], 1.00.0);
  Vertex[4]  := GetD3DVertex (QEck[4], QSeite[1], 1.01.0);
  // Rückseite
  Vertex[5]  := GetD3DVertex (QEck[5], QSeite[2], 0.00.0);
  Vertex[6]  := GetD3DVertex (QEck[6], QSeite[2], 0.01.0);
  Vertex[7]  := GetD3DVertex (QEck[7], QSeite[2], 1.00.0);
  Vertex[8]  := GetD3DVertex (QEck[8], QSeite[2], 1.01.0);
  // Linke Seite
  Vertex[9]  := GetD3DVertex (QEck[1], QSeite[3], 0.00.0);
  Vertex[10] := GetD3DVertex (QEck[4], QSeite[3], 0.01.0);
  Vertex[11] := GetD3DVertex (QEck[8], QSeite[3], 1.00.0);
  Vertex[12] := GetD3DVertex (QEck[5], QSeite[3], 1.01.0);
  // Rechte Seite
  Vertex[13] := GetD3DVertex (QEck[2], QSeite[4], 0.00.0);
  Vertex[14] := GetD3DVertex (QEck[3], QSeite[4], 0.01.0);
  Vertex[15] := GetD3DVertex (QEck[7], QSeite[4], 1.00.0);
  Vertex[16] := GetD3DVertex (QEck[6], QSeite[4], 1.01.0);
  // Oberseite
  Vertex[17] := GetD3DVertex (QEck[5], QSeite[5], 0.00.0);
  Vertex[18] := GetD3DVertex (QEck[1], QSeite[5], 0.01.0);
  Vertex[19] := GetD3DVertex (QEck[2], QSeite[5], 1.00.0);
  Vertex[20] := GetD3DVertex (QEck[6], QSeite[5], 1.01.0);
  // Unterseite
  Vertex[21] := GetD3DVertex (QEck[8], QSeite[6], 0.00.0);
  Vertex[22] := GetD3DVertex (QEck[4], QSeite[6], 0.01.0);
  Vertex[23] := GetD3DVertex (QEck[3], QSeite[6], 1.00.0);
  Vertex[24] := GetD3DVertex (QEck[7], QSeite[6], 1.01.0);
end;

constructor TD3DKoords.Create;
begin
  SetMatrices;
end;

procedure TD3DKoords.SetMatrices;
begin

  FillChar (WorldMatrix, SizeOf(WorldMatrix), 0);
  with WorldMatrix do
  begin
    _11 := 1.0; _22 := 1.0;
    _33 := 1.0; _44 := 1.0;
  end;

  FillChar (ViewMatrix, SizeOf(ViewMatrix), 0);
  with ViewMatrix do
  begin
    _11 := 1.0;
    _22 := 1.0; _23 := -0.5;
    _32 := 0.5; _33 :=  1.0;
    _43 := 5.0; _44 :=  1.0;
  end;

  FillChar (ProjectionMatrix, SizeOf(ProjectionMatrix), 0);
  with ProjectionMatrix do
  begin
    _11 :=  2.0;
    _22 :=  2.0;
    _33 :=  1.0; _34 := 1.0;
    _43 := -1.0;
  end;
end;

procedure TD3DKoords.SetRotation (Achse: Integer);
var Zeit: Extended;
begin
  Zeit := GetTickCount/1000;

  if Achse < 0 then
  begin Achse := -Achse; Zeit := -Zeit; end;
  with WorldMatrix do
  begin
    case Achse of
      xAchse:
      begin
        _22 :=  cos(Zeit); _23 := sin(Zeit);
        _32 := -sin(Zeit); _33 := cos(Zeit);
      end;
      yAchse:
      begin
        _11 :=  cos(Zeit); _13 := -sin(Zeit);
        _31 :=  sin(Zeit); _33 :=  cos(Zeit);
      end;
      zAchse:
      begin
        _11 :=  cos(Zeit); _12 :=  sin(Zeit);
        _21 := -sin(Zeit); _22 :=  cos(Zeit);
      end
      else
      begin
        FillChar (WorldMatrix, SizeOf(WorldMatrix), 0);
        _11 := 1.0; _22 := 1.0;
        _33 := 1.0; _44 := 1.0;
      end;
    end;
  end;
end;

procedure TForm1.TurnQuad (Richtung: Integer);
var i: Integer;
begin
  Koords.SetRotation (Richtung);
  DXDraw1.D3DDevice7.SetTransform
    (D3DTRANSFORMSTATE_WORLD, Koords.WorldMatrix);
  DXDraw1.D3DDevice7.BeginScene;
  for i := 0 to 5 do
    DXDraw1.D3DDevice7.DrawPrimitive
      (D3DPT_LINESTRIP, D3DFVF_VERTEX, Quader.Vertex[4*i+1], 40);
  DXDraw1.D3DDevice7.EndScene;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;
  Quader := TD3DPoly.Create;
  Koords := TD3DKoords.Create;
  Seite.x1 := 0; Seite.x2 := DXDraw1.SurfaceWidth;
  Seite.y1 := 0; Seite.y2 := DXDraw1.SurfaceHeight;
  Achse := random(7) - 3;
end;

procedure TForm1.DXDraw1InitializeSurface(Sender: TObject);
var Material: TD3DMaterial7;
begin
  DXDraw1.D3DDevice7.SetTransform
    (D3DTRANSFORMSTATE_VIEW, Koords.ViewMatrix);
  DXDraw1.D3DDevice7.SetTransform
    (D3DTRANSFORMSTATE_PROJECTION, Koords.ProjectionMatrix);
  FillChar (Material, SizeOf(Material), 0);
  with Material do
  begin
    ambient.r := random(3)/2;  // Rot-Anteil
    ambient.g := random(3)/2;  // Grün-Anteil
    ambient.b := random(3)/2;  // Blau-Anteil
  end;
  DXDraw1.D3DDevice7.SetMaterial (Material);
  DXDraw1.D3DDevice7.SetRenderState (D3DRENDERSTATE_AMBIENT, $ffffffff);
end;

procedure TForm1.DXTimer1Timer(Sender: TObject; LagCount: Integer);
begin
  if not DXDraw1.CanDraw then exit;
  if DXDraw1.ZBuffer <> nil then
    DXDraw1.D3DDevice7.Clear
      (1, Seite, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER, $00000010)
  else
    DXDraw1.D3DDevice7.Clear
      (1, Seite, D3DCLEAR_TARGET, $00000010);
  TurnQuad (Achse);
  DXDraw1.Flip;
end;

end.