Graphics Reference
In-Depth Information
10
11
12
13
14
15
16
Vector3(1.0f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.0f),
material specifications
);
// And a back plane...
addSquare(4.0, Point3(0.0f, 2.0f, -4.00f),
Vector3(1.0f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 1.0f),
material specifications
);
...
}
A sphere is specified by its center point and radius; a square by its edge length,
center point, a vector aligned with one axis of the square, and the normal vector to
the square. These two vectors, as specified, must be perpendicular, or the code will
fail. The code for adding a sphere or square to the scene is given in Listings 12.2
and 12.3.
Listing 12.2: The shape-adding methods.
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
void
World::addTransparentSphere(
const
Point3& center,
float
radius,
material parameters
){
ArticulatedModel::Ref sphere =
ArticulatedModel::fromFile(System::findDataFile(
"sphere.ifs"
), radius);
lots of material specification omitted
insert(sphere, CFrame::fromXYZYPRDegrees(center.x, center.y, center.z, 0));
}
void
World::addSquare(
float
edgeLength,
const
Point3& center,
const
Vector3&
axisTangent,
const
Vector3& normal,
const
Material::Specification& material){
ArticulatedModel::Ref square = ArticulatedModel::fromFile(
System::findDataFile(
"squarex8.ifs"
), edgeLength);
material specification code omitted
Vector3 uNormal = normal / normal.length();
Vector3 firstTangent(axisTangent / axisTangent.length());
Vector3 secondTangent(uNormal.cross(firstTangent));
Matrix3 rotmat(
firstTangent.x, secondTangent.x, uNormal.x,
firstTangent.y, secondTangent.y, uNormal.y,
firstTangent.z, secondTangent.z, uNormal.z);
CoordinateFrame cFrame(rotmat, center);
insert(square, cFrame);
}
Listing 12.3: The methods for inserting a shape into the scene.
1
2
3
4
5
6
7
8
9
void
World::insert(
const
ArticulatedModel::Ref& model,
const
CFrame& frame) {
Array<Surface::Ref> posed;
model->pose(posed, frame);
for
(
int
i = 0; i < posed.size(); ++i) {
insert(posed[i]);
m_surfaceArray.append(posed[i]);
Tri::getTris(posed[i], m_triArray, CFrame());
}
}
As you can see, the sphere-adding code reads the model from a file and accepts
a scaling parameter that says how much to scale the model up or down. The