macintosh_cookbook/units/Graf3DPrimitives.pas
2020-05-12 13:26:00 +09:00

225 lines
7.9 KiB
ObjectPascal

unit Graf3DPrimitives;
interface
uses
FixMath, Graf3D;
type
Point3DPtr = ^Point3D;
Link3D = record
a: Point3DPtr;
b: Point3DPtr;
end;
Face3D = record
a: Point3DPtr;
b: Point3DPtr;
c: Point3DPtr;
end;
Shape3D = record
origin: Point3D;
length, verticesLength, facesLength: Integer;
vertices: array[1..9] of Point3D;
edges: array[1..13] of Link3D;
faces: array[1..6] of Face3D;
end;
Shape3DPtr = ^Shape3D;
Scene3D = record
length: Integer;
shapes: array[1..20] of Shape3DPtr;
end;
Scene3DPtr = ^Scene3D;
procedure AddShape3D (var scene: scene3D; shape: Shape3DPtr);
procedure MoveShape3D (shape: Shape3DPtr; x, y, z: Fixed);
procedure TurnShape3D (shape: Shape3DPtr; x, y, z: Fixed);
procedure SetScene3D (var scene: Scene3D);
procedure SetLk3D (var lk3D: Link3D; a, b: Point3DPtr);
procedure SetFc3D (var fc3D: Face3D; a, b, c: Point3DPtr);
procedure SetBox3D (var shape: Shape3D; x, y, z, w, h, d: Fixed);
procedure SetWedge3D (var shape: Shape3D; x, y, z, w, h, d, a: Fixed);
procedure SetPyramid3D (var shape: Shape3D; x, y, z, w, h, d: Fixed);
procedure DrawLink3D (link: Link3D);
procedure DrawShape3D (shape: Shape3DPtr);
procedure DrawScene3D (scene: Scene3D);
implementation
procedure AddShape3D (var scene: scene3D; shape: Shape3DPtr);
begin
scene.length := scene.length + 1;
scene.shapes[scene.length] := shape;
end;
procedure MoveShape3D (shape: Shape3DPtr; x, y, z: Fixed);
var
i: Integer;
begin
for i := 1 to shape^.verticesLength do
SetPt3D(shape^.vertices[i], shape^.vertices[i].x + Long2Fix(x), shape^.vertices[i].y + Long2Fix(y), shape^.vertices[i].z + Long2Fix(z));
end;
procedure TurnShape3D (shape: Shape3DPtr; x, y, z: Fixed);
var
i: Integer;
begin
for i := 1 to shape^.verticesLength do
begin
{ DO IT }
end;
end;
procedure SetLk3D (var lk3D: Link3D; a, b: Point3DPtr);
begin
lk3D.a := a;
lk3D.b := b;
end;
procedure SetFc3D (var fc3D: Face3D; a, b, c: Point3DPtr);
begin
fc3D.a := a;
fc3D.b := b;
fc3D.c := c;
end;
procedure SetScene3D (var scene: Scene3D);
begin
scene.length := 0;
end;
procedure SetBox3D (var shape: Shape3D; x, y, z, w, h, d: Fixed);
begin
SetPt3D(shape.origin, x,y,z);
SetPt3D(shape.vertices[1], Long2Fix(x + w div 2), Long2Fix(y + h div 2), Long2Fix(z + d div 2));
SetPt3D(shape.vertices[2], Long2Fix(x + w div 2), Long2Fix(y + h div 2), Long2Fix(z - d div 2));
SetPt3D(shape.vertices[3], Long2Fix(x - w div 2), Long2Fix(y + h div 2), Long2Fix(z - d div 2));
SetPt3D(shape.vertices[4], Long2Fix(x - w div 2), Long2Fix(y + h div 2), Long2Fix(z + d div 2));
SetPt3D(shape.vertices[5], Long2Fix(x + w div 2), Long2Fix(y - h div 2), Long2Fix(z + d div 2));
SetPt3D(shape.vertices[6], Long2Fix(x + w div 2), Long2Fix(y - h div 2), Long2Fix(z - d div 2));
SetPt3D(shape.vertices[7], Long2Fix(x - w div 2), Long2Fix(y - h div 2), Long2Fix(z - d div 2));
SetPt3D(shape.vertices[8], Long2Fix(x - w div 2), Long2Fix(y - h div 2), Long2Fix(z + d div 2));
SetLk3D(shape.edges[1], @shape.vertices[1], @shape.vertices[2]);
SetLk3D(shape.edges[2], @shape.vertices[2], @shape.vertices[3]);
SetLk3D(shape.edges[3], @shape.vertices[3], @shape.vertices[4]);
SetLk3D(shape.edges[4], @shape.vertices[4], @shape.vertices[1]);
SetLk3D(shape.edges[5], @shape.vertices[5], @shape.vertices[6]);
SetLk3D(shape.edges[6], @shape.vertices[6], @shape.vertices[7]);
SetLk3D(shape.edges[7], @shape.vertices[7], @shape.vertices[8]);
SetLk3D(shape.edges[8], @shape.vertices[8], @shape.vertices[5]);
SetLk3D(shape.edges[9], @shape.vertices[1], @shape.vertices[5]);
SetLk3D(shape.edges[10], @shape.vertices[2], @shape.vertices[6]);
SetLk3D(shape.edges[11], @shape.vertices[3], @shape.vertices[7]);
SetLk3D(shape.edges[12], @shape.vertices[4], @shape.vertices[8]);
SetFc3D(shape.faces[1], @shape.vertices[1], @shape.vertices[2], @shape.vertices[5]);
SetFc3D(shape.faces[2], @shape.vertices[2], @shape.vertices[5], @shape.vertices[6]);
shape.verticesLength := 8;
shape.facesLength := 2;
shape.length := 12;
end;
procedure SetWedge3D (var shape: Shape3D; x, y, z, w, h, d, a: Fixed);
begin
SetPt3D(shape.origin, x,y,z);
SetPt3D(shape.vertices[1], Long2Fix(x + a), Long2Fix(y + h div 2), Long2Fix(z - d div 2));
SetPt3D(shape.vertices[2], Long2Fix(x + a), Long2Fix(y + h div 2), Long2Fix(z + d div 2));
SetPt3D(shape.vertices[3], Long2Fix(x + w div 2), Long2Fix(y - h div 2), Long2Fix(z + d div 2));
SetPt3D(shape.vertices[4], Long2Fix(x + w div 2), Long2Fix(y - h div 2), Long2Fix(z - d div 2));
SetPt3D(shape.vertices[5], Long2Fix(x - w div 2), Long2Fix(y - h div 2), Long2Fix(z - d div 2));
SetPt3D(shape.vertices[6], Long2Fix(x - w div 2), Long2Fix(y - h div 2), Long2Fix(z + d div 2));
SetLk3D(shape.edges[1], @shape.vertices[1], @shape.vertices[2]);
SetLk3D(shape.edges[2], @shape.vertices[1], @shape.vertices[4]);
SetLk3D(shape.edges[3], @shape.vertices[1], @shape.vertices[5]);
SetLk3D(shape.edges[4], @shape.vertices[2], @shape.vertices[3]);
SetLk3D(shape.edges[5], @shape.vertices[2], @shape.vertices[6]);
SetLk3D(shape.edges[6], @shape.vertices[3], @shape.vertices[4]);
SetLk3D(shape.edges[7], @shape.vertices[4], @shape.vertices[5]);
SetLk3D(shape.edges[8], @shape.vertices[5], @shape.vertices[6]);
SetLk3D(shape.edges[9], @shape.vertices[6], @shape.vertices[3]);
SetFc3D(shape.faces[1], @shape.vertices[1], @shape.vertices[2], @shape.vertices[3]);
SetFc3D(shape.faces[2], @shape.vertices[1], @shape.vertices[3], @shape.vertices[4]);
shape.verticesLength := 6;
shape.facesLength := 2;
shape.length := 9;
end;
procedure SetPyramid3D (var shape: Shape3D; x, y, z, w, h, d: Fixed);
begin
SetPt3D(shape.origin, x,y,z);
SetPt3D(shape.vertices[1], Long2Fix(x), Long2Fix(y + h div 2), Long2Fix(z));
SetPt3D(shape.vertices[2], Long2Fix(x + w div 2), Long2Fix(y - h div 2), Long2Fix(z + d div 2));
SetPt3D(shape.vertices[3], Long2Fix(x + w div 2), Long2Fix(y - h div 2), Long2Fix(z - d div 2));
SetPt3D(shape.vertices[4], Long2Fix(x - w div 2), Long2Fix(y - h div 2), Long2Fix(z - d div 2));
SetPt3D(shape.vertices[5], Long2Fix(x - w div 2), Long2Fix(y - h div 2), Long2Fix(z + d div 2));
SetLk3D(shape.edges[1], @shape.vertices[1], @shape.vertices[2]);
SetLk3D(shape.edges[2], @shape.vertices[1], @shape.vertices[3]);
SetLk3D(shape.edges[3], @shape.vertices[1], @shape.vertices[4]);
SetLk3D(shape.edges[4], @shape.vertices[1], @shape.vertices[5]);
SetLk3D(shape.edges[5], @shape.vertices[2], @shape.vertices[3]);
SetLk3D(shape.edges[6], @shape.vertices[3], @shape.vertices[4]);
SetLk3D(shape.edges[7], @shape.vertices[4], @shape.vertices[5]);
SetLk3D(shape.edges[8], @shape.vertices[5], @shape.vertices[2]);
SetFc3D(shape.faces[1], @shape.vertices[1], @shape.vertices[2], @shape.vertices[3]);
shape.verticesLength := 5;
shape.facesLength := 1;
shape.length := 8;
end;
procedure DrawWidget (size: Integer);
begin
PenPat(black);
MoveTo3D(Long2Fix(size), 0, 0);
WriteDraw('x');
LineTo3D(0, 0, 0);
LineTo3D(0, Long2Fix(size), 0);
WriteDraw('y');
MoveTo3D(0, 0, 0);
LineTo3D(0, 0, Long2Fix(size));
WriteDraw('z');
end;
procedure DrawLink3D (link: Link3D);
begin
MoveTo3D(link.a^.x, link.a^.y, link.a^.z);
LineTo3D(link.b^.x, link.b^.y, link.b^.z);
end;
procedure DrawFace3D (face: Face3D);
var
tempRgn: RgnHandle;
begin
tempRgn := NewRgn;
OpenRgn;
MoveTo3D(face.a^.X, face.a^.Y, face.a^.Z);
LineTo3D(face.b^.X, face.b^.Y, face.b^.Z);
LineTo3D(face.c^.X, face.c^.Y, face.c^.Z);
LineTo3D(face.a^.X, face.a^.Y, face.a^.Z);
CloseRgn(tempRgn);
FillRgn(tempRgn, ltgray);
DisposeRgn(tempRgn);
end;
procedure DrawShape3D (shape: Shape3DPtr);
var
i: Integer;
begin
for i := 1 to shape^.facesLength do
DrawFace3D(shape^.faces[i]);
for i := 1 to shape^.length do
DrawLink3D(shape^.edges[i]);
end;
procedure DrawScene3D (scene: Scene3D);
var
i: Integer;
begin
for i := 1 to scene.length do
DrawShape3D(scene.shapes[i]);
{ DrawWidget(50); }
end;
end.