Files
Graphics-Rasterizer/Rasteriser.cpp
IDunnoDev 19639d70d1 Week6 [02/11] - [04/11]
Added Camera Class
Added *= operator to the matrix class
Added Transformation Queue vector to the Model Class
Added Get Vertex, Polygon and Polygon Vertex methods to the Model Class
Added DehomogenizeAllVerticies method to the Model Class
Added GetPolygonVertexCount method to Polygon Class
Added Ability to have multiple models in a "scene" in the Rasterizer
Added DrawWireFrame method to the Rasterizer Class
Added Aspect Ratio and View Matrix to the Rasterizer Class
Added TransformTools namespace to hold shared transformation functions
Added Dehomogenize method to the Vector Class
Moved Transformation methods to a new shared name space
2021-12-11 14:15:02 +00:00

157 lines
3.8 KiB
C++

#include "Rasteriser.h"
Rasteriser app;
bool Rasteriser::Initialise()
{
Model modelA;
Model modelB;
Model modelC;
if (MD2Loader::LoadModel(".\\marvin.MD2", modelA, &Model::AddPolygon, &Model::AddVertex))
{
_sceneModels.push_back(modelA);
}
else
{
return false;
}
if (MD2Loader::LoadModel(".\\megaman.MD2", modelB, &Model::AddPolygon, &Model::AddVertex))
{
_sceneModels.push_back(modelB);
}
else
{
return false;
}
if (MD2Loader::LoadModel(".\\cheff.MD2", modelC, &Model::AddPolygon, &Model::AddVertex))
{
_sceneModels.push_back(modelC);
}
else
{
return false;
}
return true;
}
void Rasteriser::Update(const Bitmap& bitmap)
{
int currentRot = (_rotation % 360);
Vertex startPos = Vertex(-50, 0, 0);
int currentZOff = 0;
int currentModelIndex = 0;
for (Model &currentModel : _sceneModels)
{
currentModel.EnqueueTransform(GetTranslateMatrix(startPos));
currentModel.EnqueueTransform(GetRotationMatrix(Axis::Y, (float) currentRot));
startPos.SetX(startPos.GetX() + 50);
if ((currentModelIndex % 2) == 1)
{
currentZOff = 0;
}
else
{
currentZOff = 100;
}
startPos.SetZ((float)currentZOff);
currentModelIndex++;
}
_currentAspectRatio = (float)(bitmap.GetWidth() / bitmap.GetHeight());
_currentPerspectiveMatrix = GetPerspectiveProjectionMatrix(1, _currentAspectRatio);
_currentViewMatrix = GetViewMatrix(1, bitmap.GetWidth(), bitmap.GetHeight());
if (_rotation == 360)
{
_rotation = 0;
}
else
{
_rotation += 1;
}
}
void Rasteriser::Render(const Bitmap& bitmap)
{
ClearViewport(bitmap);
//SelectObject(bitmap.GetDC(), GetStockObject(DC_BRUSH));
Camera mainCamera = Camera(0, 0, 0, Vertex(0, 0, -100));
for (Model &currentModel : _sceneModels)
{
Matrix workingMatrix = workingMatrix.IdentityMatrix();
for (Matrix currentTransform : currentModel.GetPendingTransforms())
{
workingMatrix *= currentTransform;
}
currentModel.ApplyTransformToLocalVertices(workingMatrix);
currentModel.ApplyTransformToTransformedVertices(mainCamera.GetCurrentCameraTransformMatrix());
currentModel.ApplyTransformToTransformedVertices(_currentPerspectiveMatrix);
currentModel.DehomogenizeAllVertices();
currentModel.ApplyTransformToTransformedVertices(_currentViewMatrix);
DrawWireFrame(bitmap.GetDC(), currentModel);
currentModel.ClearPendingTransforms();
}
}
void Rasteriser::ClearViewport(const Bitmap& bitmap)
{
bitmap.Clear(RGB(255, 255, 255));
}
void Rasteriser::DrawSquare(HDC hDc, const vector<Vertex> verticies)
{
POINT pointArray[4];
for (int i = 0; i < 4; i++)
{
pointArray[i].x = (long) verticies[i].GetX();
pointArray[i].y = (long) verticies[i].GetY();
}
SetDCBrushColor(hDc, RGB(255, 0, 255));
SetDCPenColor(hDc, RGB(0, 0, 255));
Polygon(hDc, pointArray, 4);
}
void Rasteriser::DrawShape(HDC hDc, const vector<Vertex> verticies)
{
vector<POINT> pointArray;
for (int i = 0; i < verticies.size(); i++)
{
POINT newPoint;
newPoint.x = (long)verticies[i].GetX();
newPoint.y = (long)verticies[i].GetY();
pointArray.push_back(newPoint);
}
SetDCBrushColor(hDc, RGB(rand() % 256, rand() % 256, rand() % 256));
SetDCPenColor(hDc, RGB(rand() % 256, rand() % 256, rand() % 256));
Polygon(hDc, pointArray.data(), (int) verticies.size());
}
void Rasteriser::DrawWireFrame(HDC hDc, Model& model)
{
vector<POINT> pointArray;
vector<int> sizeArray;
int modelPolygonCount = (int) model.GetPolygonCount();
for (int i = 0; i < modelPolygonCount; i++)
{
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);
}
sizeArray.push_back(currentPolygonVertexCount);
}
PolyPolygon(hDc, pointArray.data(), sizeArray.data(), modelPolygonCount);
}