Week8 [16/11] - [19/11]

Added Light Base Class
Added AmbientLight, DirectionalLight and PointLight Classes
Added Constructor to the Camera Class
Added Light CoEff's to the Model Class
Added Color Variables to Polygon3D Class
Added Light Vector to hold the lights in the Rasteriser
Added Camera Vector to hold multiple Cameras and Methods to set which is the current
Added Lighting to the Rasterizer
Added DrawSolidFlat method to the Rastorizer
Added Bounds Check to the SharedTools for Int and Float values
Added Color Enum Class
Added Normalize Method to the Vector3D Class
Renamed the TransformTools to SharedTools since adding more non transform methods to it
This commit is contained in:
IDunnoDev
2021-12-11 17:05:43 +00:00
committed by iDunnoDev
parent 773507b4ab
commit f5fd5b6756
24 changed files with 815 additions and 41 deletions

View File

@ -25,7 +25,7 @@ bool Rasteriser::Initialise()
return false;
}
if (MD2Loader::LoadModel(".\\cheff.MD2", modelC, &Model::AddPolygon, &Model::AddVertex))
if (MD2Loader::LoadModel(".\\w_railgun.MD2", modelC, &Model::AddPolygon, &Model::AddVertex))
{
_sceneModels.push_back(modelC);
}
@ -33,10 +33,33 @@ bool Rasteriser::Initialise()
{
return false;
}
_lights.push_back(new AmbientLight(50, 50, 50));
_lights.push_back(new DirectionalLight(Vector3D(1, 0, 1), 50, 0, 0));
_lights.push_back(new PointLight(Vertex(0, 10, 0), 0, 1, 0, 100, 0, 0));
_cameras.push_back(Camera(0.0f, 0.0f, 0.0f, Vertex(0.0f, 0.0f, -100.0f)));
return true;
}
void Rasteriser::SetCurrentCamera(int index)
{
if (index >= 0 && index < _cameras.size())
{
_currentCamera = index;
}
}
Camera& Rasteriser::GetCamera(int index)
{
return _cameras[index];
}
Camera& Rasteriser::GetCurrentCamera()
{
return GetCamera(_currentCamera);
}
void Rasteriser::Update(const Bitmap& bitmap)
{
int currentRot = (_rotation % 360);
@ -78,32 +101,44 @@ void Rasteriser::Update(const Bitmap& bitmap)
void Rasteriser::Render(const Bitmap& bitmap)
{
ClearViewport(bitmap);
//SelectObject(bitmap.GetDC(), GetStockObject(DC_BRUSH));
SelectObject(bitmap.GetDC(), GetStockObject(DC_BRUSH));
SelectObject(bitmap.GetDC(), GetStockObject(DC_PEN));
Camera mainCamera = Camera(0, 0, 0, Vertex(0, 0, -100));
for (Model &currentModel : _sceneModels)
{
currentModel.SetReflectionCoefficient(0.5f, 0.5f, 0.5f);
Matrix workingMatrix = workingMatrix.IdentityMatrix();
for (Matrix currentTransform : currentModel.GetPendingTransforms())
{
workingMatrix *= currentTransform;
}
currentModel.ApplyTransformToLocalVertices(workingMatrix);
currentModel.CalculateBackfaces(mainCamera);
currentModel.ApplyTransformToTransformedVertices(mainCamera.GetCurrentCameraTransformMatrix());
currentModel.CalculateBackfaces(GetCurrentCamera());
for (Polygon3D &currentPolygon : currentModel.GetPolygons())
{
if (!currentPolygon.GetCulled())
{
COLORREF colorWorking = RGB(0, 0, 0);
for (Light* currentLight : _lights)
{
colorWorking = currentLight->CalculateLight(currentModel, currentPolygon, colorWorking);
}
currentPolygon.SetColor(colorWorking);
}
}
currentModel.ApplyTransformToTransformedVertices(GetCurrentCamera().GetCurrentCameraTransformMatrix());
currentModel.Sort();
currentModel.ApplyTransformToTransformedVertices(_currentPerspectiveMatrix);
currentModel.DehomogenizeAllVertices();
currentModel.ApplyTransformToTransformedVertices(_currentViewMatrix);
DrawWireFrame(bitmap.GetDC(), currentModel);
DrawSolidFlat(bitmap.GetDC(), currentModel);
currentModel.ClearPendingTransforms();
}
}
void Rasteriser::ClearViewport(const Bitmap& bitmap)
{
bitmap.Clear(RGB(255, 255, 255));
bitmap.Clear(RGB(0, 0, 0));
}
void Rasteriser::DrawSquare(HDC hDc, const vector<Vertex> verticies)
@ -162,4 +197,28 @@ void Rasteriser::DrawWireFrame(HDC hDc, Model& model)
}
PolyPolygon(hDc, pointArray.data(), sizeArray.data(), unculledPolyCount);
}
void Rasteriser::DrawSolidFlat(HDC hDc, Model& model)
{
int modelPolygonCount = (int)model.GetPolygonCount();
for (int i = 0; i < modelPolygonCount; i++)
{
if (!model.GetPolygon(i).GetCulled())
{
vector<POINT> pointArray;
int currentPolygonVertexCount = (int)model.GetPolygon(i).GetPolygonVertexCount();
for (int j = 0; j < currentPolygonVertexCount; j++)
{
POINT newPoint;
newPoint.x = (long)model.GetVertex(i, j).GetX();
newPoint.y = (long)model.GetVertex(i, j).GetY();
pointArray.push_back(newPoint);
}
SetDCBrushColor(hDc, model.GetPolygon(i).GetColor());
SetDCPenColor(hDc, model.GetPolygon(i).GetColor());
Polygon(hDc, pointArray.data(), currentPolygonVertexCount);
}
}
}