tCube2 = class (tObject)
x0, y0, z0, r : float;
facets : array [1..6] of tPlane;
PointsM : array [1..6] of Point3D;
Normals : array [1..6, 1..2] of Vec3D;
facetID : integer;
alpha, beta, gamma: float;
constructor Create( x0, y0, z0, side, alpha, beta, gamma, Kr0, Kg0, Kb0, Ks0 : float );
function Intersect( ray : tRay; Store : Boolean ) : float; override;
procedure getNormal( var n : Vec3D ); override;
end;
{---------------------------------------------------------------------}
constructor tCube2.Create( x0, y0, z0, side, alpha, beta, gamma, Kr0, Kg0, Kb0, Ks0 : float );
var
v1, v2: Vec3D;
Rx, Ry, Rz, M1: Mat3D;
m: float;
p: Point3D;
begin
self.x0 := x0;
self.y0 := y0;
self.z0 := z0;
self.alpha := alpha;
self.beta := beta;
self.gamma := gamma;
r := side/2;
m := pi/180;
v1.x := 0;
v1.y := r;
v1.z := 0;
MakeRx(alpha*m, Rz);
MakeRy(beta*m, Ry);
MakeRz(gamma*m, Rx);
MakeE(M1);
Mult3D(M1, Rx, M1);
Mult3D(M1, Ry, M1);
Mult3D(M1, Rz, M1);
RotateVector(v1, M1);
p.x := x0 + v1.x;
p.y := y0 + v1.y;
p.z := z0 + v1.z;
PointsM[1] := p;
facets[1] := tPlane.Create(p.x, p.y, p.z, p.x + v1.x, p.y + v1.y, p.z + v1.z, Kr0, Kg0, Kb0, Ks0);
VSub(v2, v1, v1);
p.x := x0 + v1.x;
p.y := y0 + v1.y;
p.z := z0 + v1.z;
PointsM[2] := p;
facets[2] := tPlane.Create(p.x, p.y, p.z, p.x + v1.x, p.y + v1.y, p.z + v1.z, Kr0, Kg0, Kb0, Ks0);
v1.x := r;
v1.y := 0;
v1.z := 0;
RotateVector(v1, M1);
p.x := x0 + v1.x;
p.y := y0 + v1.y;
p.z := z0 + v1.z;
PointsM[3] := p;
facets[3] := tPlane.Create(p.x, p.y, p.z, p.x + v1.x, p.y + v1.y, p.z + v1.z, Kr0, Kg0, Kb0, Ks0);
VSub(v2, v1, v1);
p.x := x0 + v1.x;
p.y := y0 + v1.y;
p.z := z0 + v1.z;
PointsM[4] := p;
facets[4] := tPlane.Create(p.x, p.y, p.z, p.x + v1.x, p.y + v1.y, p.z + v1.z, Kr0, Kg0, Kb0, Ks0);
v1.x := 0;
v1.y := 0;
v1.z := r;
RotateVector(v1, M1);
p.x := x0 + v1.x;
p.y := y0 + v1.y;
p.z := z0 + v1.z;
PointsM[5] := p;
facets[5] := tPlane.Create(p.x, p.y, p.z, p.x + v1.x, p.y + v1.y, p.z + v1.z, Kr0, Kg0, Kb0, Ks0);
VSub(v2, v1, v1);
p.x := x0 + v1.x;
p.y := y0 + v1.y;
p.z := z0 + v1.z;
PointsM[6] := p;
facets[6] := tPlane.Create(p.x, p.y, p.z, p.x + v1.x, p.y + v1.y, p.z + v1.z, Kr0, Kg0, Kb0, Ks0);
Normals[1, 1] := facets[3].normal;
Normals[1, 2] := facets[5].normal;
Normals[2, 1] := facets[3].normal;
Normals[2, 2] := facets[5].normal;
Normals[3, 1] := facets[1].normal;
Normals[3, 2] := facets[5].normal;
Normals[4, 1] := facets[1].normal;
Normals[4, 2] := facets[5].normal;
Normals[5, 1] := facets[1].normal;
Normals[5, 2] := facets[3].normal;
Normals[6, 1] := facets[1].normal;
Normals[6, 2] := facets[3].normal;
Kr := Kr0;
Kg := Kg0;
Kb := Kb0;
Ks := Ks0;
end;
function tCube2.Intersect( ray : tRay; Store : Boolean ) : float;
const
sides = 6;
var
i : integer;
xx, yy, zz : float;
tmin : float;
t : array [1..sides] of float;
p : Point3D;
v : Vec3D;
begin
xx := ray.p0.x - x0;
yy := ray.p0.y - y0;
zz := ray.p0.z - z0;
for i := 1 to sides do
t[i] := facets[i].Intersect(ray, Store);
tmin := Infinity;
for i := 1 to sides do begin
GetPoint(ray, t[i], p);
v.x := p.x - PointsM[i].x;
v.y := p.y - PointsM[i].y;
v.z := p.z - PointsM[i].z;
if (abs(ScalarProduct(v, Normals[i, 1])) <= r + eps) and
(abs(ScalarProduct(v, Normals[i, 2])) <= r + eps)
then begin
if (t[i] < tmin) and (t[i] > eps) then begin
tmin := t[i];
facetID := i;
end;
if Store then GetPoint( ray, tmin, SavePoint );
end;
end;
Intersect := tmin;
end;
procedure tCube2.getNormal( var n : Vec3D );
begin
n := facets[facetID].normal;
end;