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
This commit is contained in:
162
Rasteriser.cpp
162
Rasteriser.cpp
@ -1,47 +1,102 @@
|
||||
#include "Rasteriser.h"
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
|
||||
Rasteriser app;
|
||||
|
||||
bool Rasteriser::Initialise()
|
||||
{
|
||||
//_initialVertexArray.push_back(Vertex(175, 175, 0, 1));
|
||||
//_initialVertexArray.push_back(Vertex(225, 175, 0, 1));
|
||||
//_initialVertexArray.push_back(Vertex(225, 225, 0, 1));
|
||||
//_initialVertexArray.push_back(Vertex(175, 225, 0, 1));
|
||||
|
||||
_initialVertexArray.push_back(Vertex(150, 200, 1));
|
||||
_initialVertexArray.push_back(Vertex(187.5f, 187.5f, 1));
|
||||
_initialVertexArray.push_back(Vertex(200, 150, 1));
|
||||
_initialVertexArray.push_back(Vertex(212.5f, 187.5f, 1));
|
||||
_initialVertexArray.push_back(Vertex(250, 200, 1));
|
||||
_initialVertexArray.push_back(Vertex(212.5f, 212.5f, 1));
|
||||
_initialVertexArray.push_back(Vertex(200, 250, 1));
|
||||
_initialVertexArray.push_back(Vertex(187.5, 212.5f, 1));
|
||||
Model modelA;
|
||||
Model modelB;
|
||||
Model modelC;
|
||||
if (MD2Loader::LoadModel(".\\marvin.MD2", modelA, &Model::AddPolygon, &Model::AddVertex))
|
||||
{
|
||||
_sceneModels.push_back(modelA);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_currentVertexArray = _initialVertexArray;
|
||||
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);
|
||||
for (int i = 0; i < _initialVertexArray.size(); i++) {
|
||||
Matrix currentTransfomation = GetRotationMatrixFromPoint(Axis::Z, (float) currentRot, _initialVertexArray[0].GetX(), _initialVertexArray[0].GetY(), 0);
|
||||
_currentVertexArray[i] = currentTransfomation * _initialVertexArray[i];
|
||||
Vertex startPos = Vertex(-50, 0, 0);
|
||||
int currentZOff = 0;
|
||||
int currentModelIndex = 0;
|
||||
|
||||
for (Model ¤tModel : _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++;
|
||||
}
|
||||
_rotation += 1;
|
||||
|
||||
_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));
|
||||
//DrawSquare(bitmap.GetDC(), _initialVertexArray);
|
||||
DrawShape(bitmap.GetDC(), _currentVertexArray);
|
||||
//SelectObject(bitmap.GetDC(), GetStockObject(DC_BRUSH));
|
||||
|
||||
Camera mainCamera = Camera(0, 0, 0, Vertex(0, 0, -100));
|
||||
|
||||
for (Model ¤tModel : _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)
|
||||
@ -49,41 +104,6 @@ void Rasteriser::ClearViewport(const Bitmap& bitmap)
|
||||
bitmap.Clear(RGB(255, 255, 255));
|
||||
}
|
||||
|
||||
Matrix Rasteriser::GetTranslateMatrix(const float x, const float y, const float z)
|
||||
{
|
||||
return Matrix({ 1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1 });
|
||||
}
|
||||
|
||||
Matrix Rasteriser::GetScaleMatrix(const float x, const float y, const float z)
|
||||
{
|
||||
return Matrix({ x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 });
|
||||
}
|
||||
|
||||
Matrix Rasteriser::GetRotationMatrix(const Axis rotAxis, const float rotDegrees)
|
||||
{
|
||||
float rotationRadian = DegreesToRadians(rotDegrees);
|
||||
switch (rotAxis)
|
||||
{
|
||||
case (Axis::X):
|
||||
return Matrix({ 1, 0, 0, 0, 0, cos(rotationRadian), -sin(rotationRadian), 0, 0, sin(rotationRadian), cos(rotationRadian), 0, 0, 0, 0, 1 });
|
||||
case (Axis::Y):
|
||||
return Matrix({ cos(rotationRadian), 0, sin(rotationRadian), 0, 0, 1, 0, 0, -sin(rotationRadian), 0, cos(rotationRadian), 0, 0, 0, 0, 1 });
|
||||
default:
|
||||
case (Axis::Z):
|
||||
return Matrix({ cos(rotationRadian), -sin(rotationRadian), 0, 0, sin(rotationRadian), cos(rotationRadian), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 });
|
||||
}
|
||||
}
|
||||
|
||||
Matrix Rasteriser::GetRotationMatrixFromPoint(const Axis rotAxis, const float rotDegrees, const float x, const float y, const float z)
|
||||
{
|
||||
return GetTranslateMatrix(x, y, z) * GetRotationMatrix(rotAxis, rotDegrees) * GetTranslateMatrix(-x, -y, -z);
|
||||
}
|
||||
|
||||
float Rasteriser::DegreesToRadians(const float degrees)
|
||||
{
|
||||
return (degrees * _PI) / 180;
|
||||
}
|
||||
|
||||
void Rasteriser::DrawSquare(HDC hDc, const vector<Vertex> verticies)
|
||||
{
|
||||
POINT pointArray[4];
|
||||
@ -112,4 +132,26 @@ void Rasteriser::DrawShape(HDC hDc, const vector<Vertex> verticies)
|
||||
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);
|
||||
}
|
Reference in New Issue
Block a user