| 
| Autor | Beitrag |  
| mathias 
          Beiträge: 59
 Erhaltene Danke: 3
 
 
 
 
 | 
Verfasst: Mi 12.06.02 20:05 
 
Ein kleines Beispiel mit einem drehenden Würfel.
 												| 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);                                   nVersion  := 1;                                             dwFlags   := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;      iPixelType:= PFD_TYPE_RGBA;                                 cColorBits:= 24;                                            cDepthBits:= 32;                                            iLayerType:= PFD_MAIN_PLANE;                              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(0, 0, -10);
 
 glRotatef(winkel1, 1, 0, 0);
 glRotatef(winkel2, 0, 1, 0);
 glRotatef(winkel3, 0, 0, 1);
 
 glBegin(GL_POLYGON);
 glColor3f(0, 0, 1);
 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(1, 1, 0);
 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(1, 0, 1);
 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(0, 1, 1);
 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(0, 1, 0);
 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(1, 0, 0);
 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);  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.0, 50.0);
 glViewport(0, 0, Width, Height);
 wglMakeCurrent(0, 0);
 Invalidate;
 end;
 
 procedure TForm1.FormPaint(Sender: TObject);
 begin
 wglMakeCurrent(DC, hrc);
 DrawScene;
 wglMakeCurrent(0, 0);
 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 19:49, insgesamt 2-mal bearbeitet
 |  |  |  
| AvadeX Hält's aus hier
 Beiträge: 10
 
 
 
 
 | 
Verfasst: So 16.06.02 10:59 
 
MIt DirectX sieht das ganze so aus:
 												| 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
 
 Quader: TD3DPoly;
 Koords: TD3DKoords;
 Seite : TD3DRect;
 Achse : Integer;
 public
 
 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
 QEck[1] := GetD3DVector (-1.0, 1.0,-1.0);    QEck[2] := GetD3DVector ( 1.0, 1.0,-1.0);    QEck[3] := GetD3DVector ( 1.0,-1.0,-1.0);    QEck[4] := GetD3DVector (-1.0,-1.0,-1.0);    QEck[5] := GetD3DVector (-1.0, 1.0, 1.0);    QEck[6] := GetD3DVector ( 1.0, 1.0, 1.0);    QEck[7] := GetD3DVector ( 1.0,-1.0, 1.0);    QEck[8] := GetD3DVector (-1.0,-1.0, 1.0);      QSeite[1] := GetD3DVector ( 0.0, 0.0,-1.0);    QSeite[2] := GetD3DVector ( 0.0, 0.0, 1.0);    QSeite[3] := GetD3DVector (-1.0, 0.0, 0.0);    QSeite[4] := GetD3DVector ( 1.0, 0.0, 0.0);    QSeite[5] := GetD3DVector ( 0.0, 1.0, 0.0);    QSeite[6] := GetD3DVector ( 0.0,-1.0, 0.0);  end;
 
 procedure TD3DPoly.SetVertices;
 begin
 Vertex[1]  := GetD3DVertex (QEck[1], QSeite[1], 0.0, 0.0);
 Vertex[2]  := GetD3DVertex (QEck[2], QSeite[1], 0.0, 1.0);
 Vertex[3]  := GetD3DVertex (QEck[3], QSeite[1], 1.0, 0.0);
 Vertex[4]  := GetD3DVertex (QEck[4], QSeite[1], 1.0, 1.0);
 Vertex[5]  := GetD3DVertex (QEck[5], QSeite[2], 0.0, 0.0);
 Vertex[6]  := GetD3DVertex (QEck[6], QSeite[2], 0.0, 1.0);
 Vertex[7]  := GetD3DVertex (QEck[7], QSeite[2], 1.0, 0.0);
 Vertex[8]  := GetD3DVertex (QEck[8], QSeite[2], 1.0, 1.0);
 Vertex[9]  := GetD3DVertex (QEck[1], QSeite[3], 0.0, 0.0);
 Vertex[10] := GetD3DVertex (QEck[4], QSeite[3], 0.0, 1.0);
 Vertex[11] := GetD3DVertex (QEck[8], QSeite[3], 1.0, 0.0);
 Vertex[12] := GetD3DVertex (QEck[5], QSeite[3], 1.0, 1.0);
 Vertex[13] := GetD3DVertex (QEck[2], QSeite[4], 0.0, 0.0);
 Vertex[14] := GetD3DVertex (QEck[3], QSeite[4], 0.0, 1.0);
 Vertex[15] := GetD3DVertex (QEck[7], QSeite[4], 1.0, 0.0);
 Vertex[16] := GetD3DVertex (QEck[6], QSeite[4], 1.0, 1.0);
 Vertex[17] := GetD3DVertex (QEck[5], QSeite[5], 0.0, 0.0);
 Vertex[18] := GetD3DVertex (QEck[1], QSeite[5], 0.0, 1.0);
 Vertex[19] := GetD3DVertex (QEck[2], QSeite[5], 1.0, 0.0);
 Vertex[20] := GetD3DVertex (QEck[6], QSeite[5], 1.0, 1.0);
 Vertex[21] := GetD3DVertex (QEck[8], QSeite[6], 0.0, 0.0);
 Vertex[22] := GetD3DVertex (QEck[4], QSeite[6], 0.0, 1.0);
 Vertex[23] := GetD3DVertex (QEck[3], QSeite[6], 1.0, 0.0);
 Vertex[24] := GetD3DVertex (QEck[7], QSeite[6], 1.0, 1.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], 4, 0);
 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;      ambient.g := random(3)/2;      ambient.b := random(3)/2;    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, $000000, 1, 0)
 else
 DXDraw1.D3DDevice7.Clear
 (1, Seite, D3DCLEAR_TARGET, $000000, 1, 0);
 TurnQuad (Achse);
 DXDraw1.Flip;
 end;
 
 end.
 |  |  |  |  |