Week7 [09/11] - [11/11]
Added Backface Culling Methods to the Model Class Added Depth, Normal and Culled Flag Variables to the Polygon3D Class Added Vector3D Class Added - operator to the Vertex Class Cleaned up Code, Adding Void to Params etc
This commit is contained in:
47
Model.cpp
47
Model.cpp
@ -59,17 +59,17 @@ void Model::ClearPendingTransforms()
|
||||
_pendingTransforms.clear();
|
||||
}
|
||||
|
||||
Polygon3D Model::GetPolygon(int index)
|
||||
const Polygon3D& Model::GetPolygon(int index)
|
||||
{
|
||||
return _polygons[index];
|
||||
}
|
||||
|
||||
Vertex Model::GetVertex(int polygonIndex, int vertexPolygonIndex)
|
||||
const Vertex& Model::GetVertex(int polygonIndex, int vertexPolygonIndex)
|
||||
{
|
||||
return GetVertex(GetPolygon(polygonIndex).GetIndex(vertexPolygonIndex));
|
||||
}
|
||||
|
||||
Vertex Model::GetVertex(int index)
|
||||
const Vertex& Model::GetVertex(int index)
|
||||
{
|
||||
return _transformedVertices[index];
|
||||
}
|
||||
@ -96,4 +96,45 @@ void Model::DehomogenizeAllVertices()
|
||||
{
|
||||
currentVertex.Dehomogenize();
|
||||
}
|
||||
}
|
||||
|
||||
void Model::CalculateBackfaces(Camera& currentCamera)
|
||||
{
|
||||
for (Polygon3D ¤tPolygon : _polygons)
|
||||
{
|
||||
Vector3D vectorA = _transformedVertices[currentPolygon.GetIndex(1)] - _transformedVertices[currentPolygon.GetIndex(0)];
|
||||
Vector3D vectorB = _transformedVertices[currentPolygon.GetIndex(2)] - _transformedVertices[currentPolygon.GetIndex(0)];
|
||||
currentPolygon.SetNormal(Vector3D::CrossProduct(vectorA, vectorB));
|
||||
Vector3D eyeVector = _transformedVertices[currentPolygon.GetIndex(0)] - currentCamera.GetCurrentPosition();
|
||||
|
||||
float dotProduct = Vector3D::DotProduct(currentPolygon.GetNormal(), eyeVector);
|
||||
if (dotProduct < 0)
|
||||
{
|
||||
currentPolygon.SetCulled(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
currentPolygon.SetCulled(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool DepthCompare(Polygon3D& a, Polygon3D& b)
|
||||
{
|
||||
return a.GetDepth() > b.GetDepth();
|
||||
}
|
||||
|
||||
void Model::Sort()
|
||||
{
|
||||
for (Polygon3D ¤tPolygon : _polygons)
|
||||
{
|
||||
float zTotal = 0.0f;
|
||||
for (int i = 0; i < currentPolygon.GetPolygonVertexCount(); i++)
|
||||
{
|
||||
zTotal += _transformedVertices[currentPolygon.GetIndex(i)].GetZ();
|
||||
}
|
||||
currentPolygon.SetDepth(zTotal / currentPolygon.GetPolygonVertexCount());
|
||||
}
|
||||
|
||||
sort(_polygons.begin(), _polygons.end(), DepthCompare);
|
||||
}
|
Reference in New Issue
Block a user