Files
Graphics-Rasterizer/Rasteriser.cpp
2021-12-11 13:18:04 +00:00

97 lines
2.8 KiB
C++

#include "Rasteriser.h"
#include <cmath>
using namespace std;
Rasteriser app;
bool Rasteriser::Initialise()
{
//_vertexArray.push_back(Vertex(175, 175, 1));
//_vertexArray.push_back(Vertex(225, 175, 1));
//_vertexArray.push_back(Vertex(225, 225, 1));
//_vertexArray.push_back(Vertex(175, 225, 1));
_vertexArray.push_back(Vertex(150, 200, 1));
_vertexArray.push_back(Vertex(187.5f, 187.5f, 1));
_vertexArray.push_back(Vertex(200, 150, 1));
_vertexArray.push_back(Vertex(212.5f, 187.5f, 1));
_vertexArray.push_back(Vertex(250, 200, 1));
_vertexArray.push_back(Vertex(212.5f, 212.5f, 1));
_vertexArray.push_back(Vertex(200, 250, 1));
_vertexArray.push_back(Vertex(187.5, 212.5f, 1));
return true;
}
void Rasteriser::Update(const Bitmap& bitmap)
{
for (int i = 0; i < _vertexArray.size(); i++) {
_vertexArray[i] = Translate(_vertexArray[i], 2, 0);
_vertexArray[i] = Rotate(_vertexArray[i], 1);
_vertexArray[i] = Scale(_vertexArray[i], 1.001f, 1.001f);
}
}
void Rasteriser::Render(const Bitmap& bitmap)
{
bitmap.Clear(RGB(255, 255, 255));
SelectObject(bitmap.GetDC(), GetStockObject(DC_BRUSH));
//DrawSquare(bitmap.GetDC(), _vertexArray);
DrawShape(bitmap.GetDC(), _vertexArray);
}
Vertex Rasteriser::Translate(const Vertex vertexIn, const float moveXBy, const float moveYBy)
{
Matrix translationMatrix = Matrix({ 1, 0, moveXBy, 0, 1, moveYBy, 0, 0, 1 });
return translationMatrix * vertexIn;
}
Vertex Rasteriser::Scale(const Vertex vertexIn, const float scaleXBy, const float scaleYBy)
{
Matrix scaleFactorMatrix = Matrix({ scaleXBy, 0, 0, 0, scaleYBy, 0, 0, 0, 1 });
return scaleFactorMatrix * vertexIn;
}
Vertex Rasteriser::Rotate(const Vertex vertexIn, const float rotationDegrees)
{
float rotationRadian = DegreesToRadians(rotationDegrees);
Matrix rotationFactorMatrix = Matrix({ cos(rotationRadian), -sin(rotationRadian), 0, sin(rotationRadian), cos(rotationRadian), 0, 0, 0, 1});
return rotationFactorMatrix * vertexIn;
}
float Rasteriser::DegreesToRadians(const float degrees)
{
return (degrees * _PI) / 180;
}
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());
}