DanieleC88
16-03-2003, 13:45
Sto cercando di fare un motore 3D con GDI, solo per capire un pò come funziona il 3D.
Ecco il codice che uso:
type
{Point}
Point3D = record
X, //X coordinate
Y, //Y coordinate
Z: LongInt; //Profondity
end;
{Face}
Face3D = record
Points: array[1..4] of Point3D;
Colour: TColor;
Texture: TPicture;
end;
{Box}
Box3D = record
Faces: array[1..6] of Face3D;
end;
{Position}
Position3D = record
W: Point3D; //Looking point
X, //X coordinate
Y, //Y coordinate
Z: LongInt; //Profondity
end;
procedure DrawBox(const C: TCanvas; const B: Box3D; const pP: Position3D); overload;
var
F, //Faces
P, //Points
pMax, //Z-Point Max
pMin: LongInt; //Z-Point Min
zDiff, //Difference
T, //Temp
S: Extended; //Step
pARR: array of TPoint; //Point array
begin
C.FillRect(C.ClipRect);
for F:=1 to 5 do //Low(B.Faces) to High(B.Faces) do
begin
SetLength(pARR, High(B.Faces[F].Points));
pARR[Low(B.Faces[F].Points)].X:=0;
pARR[Low(B.Faces[F].Points)].Y:=0;
for P:=Low(B.Faces[F].Points) to High(B.Faces[F].Points) do
begin
zDiff:=(pP.Z - B.Faces[F].Points[P].Z);
S:=GetLowerThan(zDiff, 1);
//S:=1;
T:=(B.Faces[F].Points[P].X - pP.X);
T:=(T + zDiff);
T:=(T * S);
pARR[P-1].X:=B.Faces[F].Points[P].X;
pARR[P-1].X:=Trunc(T); //mod fWidth);
T:=(B.Faces[F].Points[P].Y - pP.Y);
T:=(T + zDiff);
T:=(T * S);
pARR[P-1].Y:=B.Faces[F].Points[P].Y;
pARR[P-1].Y:=Trunc(T); //mod fHeight);
end;
//SetDCBrushColor(DC, B.Faces[F].Colour);
C.Polygon(pARR);
(* C.Brush.Color:=0;
C.FloodFill(
(pARR[Low(B.Faces[F].Points)].X - 1),
(pARR[Low(B.Faces[F].Points)].Y - 1),
B.Faces[F].Colour,
fsBorder
); *)
{FloodFill(DC,
(pARR[Low(B.Faces[F].Points)].X + 1),
(pARR[Low(B.Faces[F].Points)].Y + 1),
B.Faces[F].Colour
);}
end;
end;
Questo codice non funziona un granchè... Ma non riesco a trovare gli errori: sono nuovo a questo tipo di programmazione grafica e non saprei proprio da dove cominciare. Cercate di aiutarmi, siate buoni !
Ecco il codice che uso:
type
{Point}
Point3D = record
X, //X coordinate
Y, //Y coordinate
Z: LongInt; //Profondity
end;
{Face}
Face3D = record
Points: array[1..4] of Point3D;
Colour: TColor;
Texture: TPicture;
end;
{Box}
Box3D = record
Faces: array[1..6] of Face3D;
end;
{Position}
Position3D = record
W: Point3D; //Looking point
X, //X coordinate
Y, //Y coordinate
Z: LongInt; //Profondity
end;
procedure DrawBox(const C: TCanvas; const B: Box3D; const pP: Position3D); overload;
var
F, //Faces
P, //Points
pMax, //Z-Point Max
pMin: LongInt; //Z-Point Min
zDiff, //Difference
T, //Temp
S: Extended; //Step
pARR: array of TPoint; //Point array
begin
C.FillRect(C.ClipRect);
for F:=1 to 5 do //Low(B.Faces) to High(B.Faces) do
begin
SetLength(pARR, High(B.Faces[F].Points));
pARR[Low(B.Faces[F].Points)].X:=0;
pARR[Low(B.Faces[F].Points)].Y:=0;
for P:=Low(B.Faces[F].Points) to High(B.Faces[F].Points) do
begin
zDiff:=(pP.Z - B.Faces[F].Points[P].Z);
S:=GetLowerThan(zDiff, 1);
//S:=1;
T:=(B.Faces[F].Points[P].X - pP.X);
T:=(T + zDiff);
T:=(T * S);
pARR[P-1].X:=B.Faces[F].Points[P].X;
pARR[P-1].X:=Trunc(T); //mod fWidth);
T:=(B.Faces[F].Points[P].Y - pP.Y);
T:=(T + zDiff);
T:=(T * S);
pARR[P-1].Y:=B.Faces[F].Points[P].Y;
pARR[P-1].Y:=Trunc(T); //mod fHeight);
end;
//SetDCBrushColor(DC, B.Faces[F].Colour);
C.Polygon(pARR);
(* C.Brush.Color:=0;
C.FloodFill(
(pARR[Low(B.Faces[F].Points)].X - 1),
(pARR[Low(B.Faces[F].Points)].Y - 1),
B.Faces[F].Colour,
fsBorder
); *)
{FloodFill(DC,
(pARR[Low(B.Faces[F].Points)].X + 1),
(pARR[Low(B.Faces[F].Points)].Y + 1),
B.Faces[F].Colour
);}
end;
end;
Questo codice non funziona un granchè... Ma non riesco a trovare gli errori: sono nuovo a questo tipo di programmazione grafica e non saprei proprio da dove cominciare. Cercate di aiutarmi, siate buoni !