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:
@ -146,16 +146,19 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Bitmap.cpp" />
|
||||
<ClCompile Include="Camera.cpp" />
|
||||
<ClCompile Include="Framework.cpp" />
|
||||
<ClCompile Include="Matrix.cpp" />
|
||||
<ClCompile Include="MD2Loader.cpp" />
|
||||
<ClCompile Include="Model.cpp" />
|
||||
<ClCompile Include="Polygon3D.cpp" />
|
||||
<ClCompile Include="Rasteriser.cpp" />
|
||||
<ClCompile Include="TransformTools.cpp" />
|
||||
<ClCompile Include="Vertex.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Bitmap.h" />
|
||||
<ClInclude Include="Camera.h" />
|
||||
<ClInclude Include="Framework.h" />
|
||||
<ClInclude Include="Matrix.h" />
|
||||
<ClInclude Include="MD2Loader.h" />
|
||||
@ -164,6 +167,7 @@
|
||||
<ClInclude Include="Rasteriser.h" />
|
||||
<ClInclude Include="Resource.h" />
|
||||
<ClInclude Include="targetver.h" />
|
||||
<ClInclude Include="TransformTools.h" />
|
||||
<ClInclude Include="Vertex.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -39,6 +39,12 @@
|
||||
<ClCompile Include="MD2Loader.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Camera.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="TransformTools.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Framework.h">
|
||||
@ -71,6 +77,12 @@
|
||||
<ClInclude Include="MD2Loader.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Camera.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="TransformTools.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Image Include="Rasteriser.ico">
|
||||
|
102
Camera.cpp
Normal file
102
Camera.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
#include "Camera.h"
|
||||
|
||||
Camera::Camera(float xRot, float yRot, float zRot, const Vertex& pos)
|
||||
{
|
||||
_initialRotation[0] = xRot;
|
||||
_currentRotation[0] = xRot;
|
||||
_initialRotation[1] = yRot;
|
||||
_currentRotation[1] = yRot;
|
||||
_initialRotation[2] = zRot;
|
||||
_currentRotation[2] = zRot;
|
||||
_initialPosition = pos;
|
||||
_currentPosition = pos;
|
||||
}
|
||||
|
||||
Camera::Camera(const Camera& other)
|
||||
{
|
||||
Copy(other);
|
||||
}
|
||||
|
||||
Camera::~Camera()
|
||||
{
|
||||
}
|
||||
|
||||
Matrix Camera::UpdateCameraPosition(const Vertex& newPos)
|
||||
{
|
||||
_currentPosition = newPos;
|
||||
return GetCurrentCameraTransformMatrix();
|
||||
}
|
||||
|
||||
Matrix Camera::RotateCamera(float xRot, float yRot, float zRot)
|
||||
{
|
||||
_currentRotation[0] = xRot;
|
||||
_currentRotation[1] = yRot;
|
||||
_currentRotation[2] = zRot;
|
||||
return GetCurrentCameraTransformMatrix();
|
||||
}
|
||||
|
||||
Matrix Camera::GetCurrentCameraTransformMatrix()
|
||||
{
|
||||
return GetCameraRotationMatrix(Axis::X, _currentRotation[0]) * GetCameraRotationMatrix(Axis::Y, _currentRotation[1]) * GetCameraRotationMatrix(Axis::Z, _currentRotation[2]) * GetCameraTranslateMatrix(_currentPosition);
|
||||
}
|
||||
|
||||
float Camera::GetCurrentRotationValue(const Axis axis)
|
||||
{
|
||||
switch (axis)
|
||||
{
|
||||
case (Axis::X):
|
||||
return _currentRotation[0];
|
||||
case (Axis::Y):
|
||||
return _currentRotation[1];
|
||||
case (Axis::Z):
|
||||
return _currentRotation[2];
|
||||
default:
|
||||
throw "No valid axis?";
|
||||
}
|
||||
}
|
||||
|
||||
float Camera::GetInitialRotationValue(const Axis axis)
|
||||
{
|
||||
switch (axis)
|
||||
{
|
||||
case (Axis::X):
|
||||
return _initialRotation[0];
|
||||
case (Axis::Y):
|
||||
return _initialRotation[1];
|
||||
case (Axis::Z):
|
||||
return _initialRotation[2];
|
||||
default:
|
||||
throw "No valid axis?";
|
||||
}
|
||||
}
|
||||
|
||||
Vertex Camera::GetCurrentPosition()
|
||||
{
|
||||
return _currentPosition;
|
||||
}
|
||||
|
||||
Vertex Camera::GetInitialPosition()
|
||||
{
|
||||
return _initialPosition;
|
||||
}
|
||||
|
||||
Camera& Camera::operator=(const Camera& rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
{
|
||||
Copy(rhs);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Camera::Copy(const Camera& other)
|
||||
{
|
||||
_initialRotation[0] = other._initialRotation[0];
|
||||
_currentRotation[0] = other._currentRotation[0];
|
||||
_initialRotation[1] = other._initialRotation[1];
|
||||
_currentRotation[1] = other._currentRotation[1];
|
||||
_initialRotation[2] = other._initialRotation[2];
|
||||
_currentRotation[2] = other._currentRotation[2];
|
||||
_initialPosition = other._initialPosition;
|
||||
_currentPosition = other._currentPosition;
|
||||
}
|
39
Camera.h
Normal file
39
Camera.h
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include "Vertex.h"
|
||||
#include "Matrix.h"
|
||||
#include "TransformTools.h"
|
||||
|
||||
using namespace TransformTools;
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
Camera(float xRot, float yRot, float zRot, const Vertex& pos);
|
||||
Camera(const Camera& other);
|
||||
|
||||
~Camera();
|
||||
|
||||
Matrix UpdateCameraPosition(const Vertex& newPos);
|
||||
Matrix RotateCamera(float xRot, float yRot, float zRot);
|
||||
|
||||
Matrix GetCurrentCameraTransformMatrix();
|
||||
|
||||
float GetCurrentRotationValue(const Axis axis);
|
||||
float GetInitialRotationValue(const Axis axis);
|
||||
Vertex GetCurrentPosition();
|
||||
Vertex GetInitialPosition();
|
||||
|
||||
Camera& operator= (const Camera& rhs);
|
||||
|
||||
private:
|
||||
float _initialRotation[3] = { 0 };
|
||||
Vertex _initialPosition;
|
||||
|
||||
float _currentRotation[3] = { 0 };
|
||||
Vertex _currentPosition;
|
||||
|
||||
Matrix _currentCameraTransform;
|
||||
|
||||
void Copy(const Camera& other);
|
||||
};
|
||||
|
@ -82,6 +82,12 @@ Matrix& Matrix::operator= (const Matrix& rhs)
|
||||
return *this;
|
||||
}
|
||||
|
||||
Matrix& Matrix::operator*= (const Matrix& rhs)
|
||||
{
|
||||
*this = *this * rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool Matrix::operator== (const Matrix& other) const
|
||||
{
|
||||
for (int i = 0; i < ROWS; i++)
|
||||
|
1
Matrix.h
1
Matrix.h
@ -28,6 +28,7 @@ public:
|
||||
Matrix IdentityMatrix();
|
||||
|
||||
Matrix& operator= (const Matrix& rhs);
|
||||
Matrix& operator*= (const Matrix& rhs);
|
||||
|
||||
bool operator==(const Matrix& other) const;
|
||||
const Matrix operator*(const Matrix& other) const;
|
||||
|
60
Model.cpp
60
Model.cpp
@ -18,6 +18,11 @@ const vector<Vertex>& Model::GetVertices()
|
||||
return _vertices;
|
||||
}
|
||||
|
||||
const vector<Matrix>& Model::GetPendingTransforms()
|
||||
{
|
||||
return _pendingTransforms;
|
||||
}
|
||||
|
||||
size_t Model::GetPolygonCount()
|
||||
{
|
||||
return _polygons.size();
|
||||
@ -28,12 +33,67 @@ size_t Model::GetVerticesCount()
|
||||
return _vertices.size();
|
||||
}
|
||||
|
||||
size_t Model::GetPendingTransformCount()
|
||||
{
|
||||
return _pendingTransforms.size();
|
||||
}
|
||||
|
||||
void Model::AddVertex(float x, float y, float z)
|
||||
{
|
||||
_vertices.push_back(Vertex(x, y, z));
|
||||
_transformedVertices.push_back(Vertex(x, y, z));
|
||||
}
|
||||
|
||||
void Model::AddPolygon(int index0, int index1, int index2)
|
||||
{
|
||||
_polygons.push_back(Polygon3D(index0, index1, index2));
|
||||
}
|
||||
|
||||
void Model::EnqueueTransform(Matrix transform)
|
||||
{
|
||||
_pendingTransforms.push_back(transform);
|
||||
}
|
||||
|
||||
void Model::ClearPendingTransforms()
|
||||
{
|
||||
_pendingTransforms.clear();
|
||||
}
|
||||
|
||||
Polygon3D Model::GetPolygon(int index)
|
||||
{
|
||||
return _polygons[index];
|
||||
}
|
||||
|
||||
Vertex Model::GetVertex(int polygonIndex, int vertexPolygonIndex)
|
||||
{
|
||||
return GetVertex(GetPolygon(polygonIndex).GetIndex(vertexPolygonIndex));
|
||||
}
|
||||
|
||||
Vertex Model::GetVertex(int index)
|
||||
{
|
||||
return _transformedVertices[index];
|
||||
}
|
||||
|
||||
void Model::ApplyTransformToLocalVertices(const Matrix& transform)
|
||||
{
|
||||
for (int i = 0; i < GetVerticesCount(); i++)
|
||||
{
|
||||
_transformedVertices[i] = transform * _vertices[i];
|
||||
}
|
||||
}
|
||||
|
||||
void Model::ApplyTransformToTransformedVertices(const Matrix& transform)
|
||||
{
|
||||
for (int i = 0; i < GetVerticesCount(); i++)
|
||||
{
|
||||
_transformedVertices[i] = transform * _transformedVertices[i];
|
||||
}
|
||||
}
|
||||
|
||||
void Model::DehomogenizeAllVertices()
|
||||
{
|
||||
for (Vertex ¤tVertex : _transformedVertices)
|
||||
{
|
||||
currentVertex.Dehomogenize();
|
||||
}
|
||||
}
|
17
Model.h
17
Model.h
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Polygon3D.h"
|
||||
#include "Vertex.h"
|
||||
#include "Matrix.h"
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
@ -15,15 +15,30 @@ public:
|
||||
|
||||
const vector<Polygon3D>& GetPolygons();
|
||||
const vector<Vertex>& GetVertices();
|
||||
const vector<Matrix>& GetPendingTransforms();
|
||||
|
||||
size_t GetPolygonCount();
|
||||
size_t GetVerticesCount();
|
||||
size_t GetPendingTransformCount();
|
||||
|
||||
void AddVertex(float x, float y, float z);
|
||||
void AddPolygon(int index0, int index1, int index2);
|
||||
void EnqueueTransform(Matrix transform);
|
||||
void ClearPendingTransforms();
|
||||
|
||||
Polygon3D GetPolygon(int index);
|
||||
Vertex GetVertex(int polygonIndex, int vertexPolygonIndex);
|
||||
Vertex GetVertex(int index);
|
||||
|
||||
void ApplyTransformToLocalVertices(const Matrix& transform);
|
||||
void ApplyTransformToTransformedVertices(const Matrix& transform);
|
||||
void DehomogenizeAllVertices();
|
||||
|
||||
private:
|
||||
vector<Polygon3D> _polygons;
|
||||
vector<Vertex> _vertices;
|
||||
vector<Vertex> _transformedVertices;
|
||||
vector<Matrix> _pendingTransforms;
|
||||
|
||||
};
|
||||
|
||||
|
@ -20,6 +20,11 @@ Polygon3D::~Polygon3D()
|
||||
{
|
||||
}
|
||||
|
||||
size_t Polygon3D::GetPolygonVertexCount()
|
||||
{
|
||||
return sizeof(_indices) / sizeof(_indices[0]);
|
||||
}
|
||||
|
||||
int Polygon3D::GetIndex(const int index) const
|
||||
{
|
||||
return _indices[index];
|
||||
|
@ -8,8 +8,10 @@ public:
|
||||
|
||||
~Polygon3D();
|
||||
|
||||
int GetIndex(int index) const;
|
||||
size_t GetPolygonVertexCount();
|
||||
|
||||
int GetIndex(int index) const;
|
||||
|
||||
Polygon3D& operator= (const Polygon3D& rhs);
|
||||
|
||||
private:
|
||||
|
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);
|
||||
}
|
24
Rasteriser.h
24
Rasteriser.h
@ -2,9 +2,14 @@
|
||||
#include "Framework.h"
|
||||
#include "Vertex.h"
|
||||
#include "Matrix.h"
|
||||
#include "TransformTools.h"
|
||||
#include "Camera.h"
|
||||
#include "Model.h"
|
||||
#include "MD2Loader.h"
|
||||
#include <vector>
|
||||
|
||||
enum class Axis { X, Y, Z };
|
||||
using namespace std;
|
||||
using namespace TransformTools;
|
||||
|
||||
class Rasteriser : public Framework
|
||||
{
|
||||
@ -17,17 +22,16 @@ public:
|
||||
void DrawSquare(HDC hDc, const vector<Vertex> verticies);
|
||||
void DrawShape(HDC hDc, const vector<Vertex> verticies);
|
||||
|
||||
Matrix GetTranslateMatrix(const float x, const float y, const float z);
|
||||
Matrix GetScaleMatrix(const float x, const float y, const float z);
|
||||
Matrix GetRotationMatrix(const Axis rotAxis, const float rotDegrees);
|
||||
Matrix GetRotationMatrixFromPoint(const Axis rotAxis, const float rotDegrees, const float x, const float y, const float z);
|
||||
|
||||
float DegreesToRadians(const float degrees);
|
||||
void DrawWireFrame(HDC hDc, Model& model);
|
||||
|
||||
private:
|
||||
vector<Vertex> _initialVertexArray;
|
||||
vector<Vertex> _currentVertexArray;
|
||||
const float _PI = (float) acos(-1);
|
||||
vector<Model> _sceneModels;
|
||||
|
||||
Matrix _currentPerspectiveMatrix;
|
||||
Matrix _currentViewMatrix;
|
||||
float _currentAspectRatio;
|
||||
|
||||
int _rotation;
|
||||
|
||||
};
|
||||
|
||||
|
90
TransformTools.cpp
Normal file
90
TransformTools.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
#include "TransformTools.h"
|
||||
|
||||
Matrix TransformTools::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 TransformTools::GetTranslateMatrix(const Vertex& position)
|
||||
{
|
||||
return GetTranslateMatrix(position.GetX(), position.GetY(), position.GetZ());
|
||||
}
|
||||
|
||||
Matrix TransformTools::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 TransformTools::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 });
|
||||
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 });
|
||||
default:
|
||||
throw "Invalid axis selected";
|
||||
}
|
||||
}
|
||||
|
||||
Matrix TransformTools::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 TransformTools::DegreesToRadians(const float degrees)
|
||||
{
|
||||
return (degrees * PI) / 180;
|
||||
}
|
||||
|
||||
Matrix TransformTools::GetOrthegraphicProjectionMatrix(float d)
|
||||
{
|
||||
return Matrix({ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, d, 0, 0, 0, 1 });
|
||||
}
|
||||
|
||||
Matrix TransformTools::GetPerspectiveProjectionMatrix(float d, float aspectRatio)
|
||||
{
|
||||
float distanceAspect = d / aspectRatio;
|
||||
|
||||
return Matrix({ distanceAspect, 0, 0, 0, 0, d, 0, 0, 0, 0, d, 0, 0, 0, 1, 0 });
|
||||
}
|
||||
|
||||
Matrix TransformTools::GetViewMatrix(float d, int width, int height)
|
||||
{
|
||||
float widthCenter = (float) width / 2.0f;
|
||||
float heightCenter = (float) height / 2.0f;
|
||||
float distanceCenter = (float) d / 2.0f;
|
||||
|
||||
return Matrix({ widthCenter, 0, 0, widthCenter, 0, -heightCenter, 0, heightCenter, 0, 0, distanceCenter, distanceCenter, 0, 0, 0, 1 });
|
||||
}
|
||||
|
||||
Matrix TransformTools::GetCameraTranslateMatrix(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 TransformTools::GetCameraTranslateMatrix(const Vertex& position)
|
||||
{
|
||||
return GetCameraTranslateMatrix(position.GetX(), position.GetY(), position.GetZ());
|
||||
}
|
||||
|
||||
|
||||
Matrix TransformTools::GetCameraRotationMatrix(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 });
|
||||
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 });
|
||||
default:
|
||||
throw "Invalid axis selected";
|
||||
}
|
||||
}
|
29
TransformTools.h
Normal file
29
TransformTools.h
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include "Matrix.h"
|
||||
#include <cmath>
|
||||
|
||||
enum class Axis { X, Y, Z };
|
||||
|
||||
namespace TransformTools
|
||||
{
|
||||
const float PI = (float)acos(-1);
|
||||
|
||||
Matrix GetTranslateMatrix(const float x, const float y, const float z);
|
||||
Matrix GetTranslateMatrix(const Vertex& position);
|
||||
|
||||
Matrix GetScaleMatrix(const float x, const float y, const float z);
|
||||
|
||||
Matrix GetRotationMatrix(const Axis rotAxis, const float rotDegrees);
|
||||
Matrix GetRotationMatrixFromPoint(const Axis rotAxis, const float rotDegrees, const float x, const float y, const float z);
|
||||
|
||||
Matrix GetOrthegraphicProjectionMatrix(float d);
|
||||
Matrix GetPerspectiveProjectionMatrix(float d, float aspectRatio);
|
||||
Matrix GetViewMatrix(float d, int width, int height);
|
||||
|
||||
Matrix GetCameraRotationMatrix(const Axis rotAxis, const float rotDegrees);
|
||||
Matrix GetCameraTranslateMatrix(const float x, const float y, const float z);
|
||||
Matrix GetCameraTranslateMatrix(const Vertex& position);
|
||||
|
||||
float DegreesToRadians(const float degrees);
|
||||
};
|
||||
|
@ -69,6 +69,14 @@ void Vertex::SetW(const float w)
|
||||
_w = w;
|
||||
}
|
||||
|
||||
void Vertex::Dehomogenize()
|
||||
{
|
||||
_x = _x / _w;
|
||||
_y = _y / _w;
|
||||
_z = _z / _w;
|
||||
_w = _w / _w;
|
||||
}
|
||||
|
||||
Vertex& Vertex::operator=(const Vertex& rhs)
|
||||
{
|
||||
// Only do the assignment if we are not assigning
|
||||
|
2
Vertex.h
2
Vertex.h
@ -17,6 +17,8 @@ public:
|
||||
float GetW() const;
|
||||
void SetW(const float w);
|
||||
|
||||
void Dehomogenize();
|
||||
|
||||
// Assignment operator
|
||||
Vertex& operator= (const Vertex& rhs);
|
||||
|
||||
|
BIN
marvin.md2
Normal file
BIN
marvin.md2
Normal file
Binary file not shown.
BIN
megaman.MD2
Normal file
BIN
megaman.MD2
Normal file
Binary file not shown.
Reference in New Issue
Block a user