Create a new project called lab10_physics, it should be a clone of lab10_lighting project. You can use this one here if you didnt complete it:
struct Geometry : public Renderable
{
//...
VertexGroup *vertexGroup;
std::vector<Vector3> vertices;
//...
};
struct Geometry : public Renderable
{
//...
VertexGroup *vertexGroup;
std::vector<Vector3> vertices;
//...
void extractVertices();
};
Also introduce a method to extract the vertices from the group and populate the 'vertices' object (shown above).
This is the implementation of extractVertices here:
void Geometry::extractVertices()
{
int newIndex=0;
foreach (Face &face, faces)
{
vector<int> groupIndices = face.vertexIndices;
face.vertexIndices.clear();
foreach (int index, groupIndices)
{
Vector3 vertex(vertexGroup->vertices[index-1].X, vertexGroup->vertices[index-1].Y, vertexGroup->vertices[index-1].Z);
vertices.push_back(vertex);
face.vertexIndices.push_back(newIndex);
newIndex++;
}
}
Have a close look at this code - essentially it is just replicating that part of the vertexGroup the is used by this Geometry object, and holding it as part of this object.
We can call this then from the Geometry constructor:
Geometry::Geometry(string groupName, istream& is, VertexGroup*group)
: name(groupName), vertexGroup(group)
{
//...
extractVertices();
}
void Geometry::render()
{
foreach (Face &face, faces)
{
face.render(vertices);
}
}
void Face::render(std::vector <Vector3>&vertexTable)
{
vertexIndices.size() == 3?
glBegin(GL_TRIANGLES)
:glBegin(GL_QUADS);
foreach (int index, vertexIndices)
{
glVertex3f( vertexTable[index-1].X,
vertexTable[index-1].Y,
vertexTable[index-1].Z );
}
glEnd();
}
Note that we have to subtract 1 form the index values, as the Waverfront indices are 1 based.
We change remove this substraction:
void Face::render(std::vector <Vector3>&vertexTable)
{
vertexIndices.size() == 3?
glBegin(GL_TRIANGLES)
:glBegin(GL_QUADS);
foreach (int index, vertexIndices)
{
glVertex3f( vertexTable[index].X,
vertexTable[index].Y,
vertexTable[index].Z );
}
glEnd();
}
To complete this phase of the physics support - we need to replace a number of classes in the utils folder with these versions:
and also introduce these new classes:
Build and test
Replace these classes with the following:
And introduce these:
This is a revised version of the scene implementation:
Build and test this - and load the following model:
Which is created from this may file here:
This is a complete archive of the project in case you have any difficulties