Files
Graphics-Rasterizer/Vertex.cpp
IDunnoDev df3c9fac81 Week9 [23/11] - [29/11]
Added Lighting Changes for Vertex Normal Calculation
Added Method to Get a Polygons Vertices as a Vector array
Added Methods to Add the Color to a Polygon and Vertex Object
Added Normalization to the Polygon normal vector
Added Vector Normal Calculation to the Model Class
Added Light Calculation method to the Rasterizer Class
Added Flag to Rasterizer to stop processing when the screen is minimised
Added Flat and Gouraud Drawing Methods to the Resterizer
Added Standard and INT triangle drawing Methods
Added Function to mimic sgn function from Java
Added Length to Vector3D class
Added / operator to Vector3D class
Added Get(x,y,z,w)Int methods to Vertex class
Changed Color Variables on the Vertex/Polygon classes to use r, g and b values rather than COLORREF
Changed Camera translation functions to use normal Translation Functions
Cleaned up Code
Removed Unused code from the Camera Class
2021-12-11 19:26:56 +00:00

268 lines
4.0 KiB
C++

#include "Vertex.h"
Vertex::Vertex()
{
_x = 0.0f;
_y = 0.0f;
_z = 0.0f;
_w = 0.0f;
_contributeCount = 0;
_normal = Vector3D(0, 0, 0);
_r = 0;
_g = 0;
_b = 0;
}
Vertex::Vertex(const float x, const float y, const float z)
{
_x = x;
_y = y;
_z = z;
_w = 1.0f;
_contributeCount = 0;
_normal = Vector3D(0, 0, 0);
_r = 0;
_g = 0;
_b = 0;
}
Vertex::Vertex(const float x, const float y, const float z, const float w)
{
_x = x;
_y = y;
_z = z;
_w = w;
_contributeCount = 0;
_normal = Vector3D(0, 0, 0);
_r = 0;
_g = 0;
_b = 0;
}
Vertex::Vertex(const Vertex & other)
{
Copy(other);
}
float Vertex::GetX() const
{
return _x;
}
void Vertex::SetX(const float x)
{
_x = x;
}
float Vertex::GetY() const
{
return _y;
}
void Vertex::SetY(const float y)
{
_y = y;
}
float Vertex::GetZ() const
{
return _z;
}
void Vertex::SetZ(const float z)
{
_z = z;
}
float Vertex::GetW() const
{
return _w;
}
void Vertex::SetW(const float w)
{
_w = w;
}
const int Vertex::GetContributeCount() const
{
return _contributeCount;
}
void Vertex::IncrementContributeCount()
{
_contributeCount++;
}
void Vertex::ResetContributeCount()
{
_contributeCount = 0;
}
const Vector3D& Vertex::GetNormal() const
{
return _normal;
}
void Vertex::SetNormal(float x, float y, float z)
{
_normal.SetX(x);
_normal.SetY(y);
_normal.SetZ(z);
}
void Vertex::SetNormal(const Vector3D& normal)
{
SetNormal(normal.GetX(), normal.GetY(), normal.GetZ());
}
void Vertex::NormalizeNormal()
{
_normal.Normalize();
}
void Vertex::ResetNormal(bool resetCount)
{
SetNormal(0, 0, 0);
if (resetCount)
{
ResetContributeCount();
}
}
const COLORREF Vertex::GetColor() const
{
return RGB(_r, _g, _b);
}
int Vertex::GetR() const
{
return _r;
}
int Vertex::GetG() const
{
return _g;
}
int Vertex::GetB() const
{
return _b;
}
void Vertex::SetColor(int r, int g, int b)
{
_r = r;
_g = g;
_b = b;
}
void Vertex::SetColor(const COLORREF colorIn)
{
SetColor((int)GetRValue(colorIn), (int)GetGValue(colorIn), (int)GetBValue(colorIn));
}
int Vertex::GetXInt(bool forceRoundUp) const
{
if (forceRoundUp)
{
return (int)ceil(GetX());
}
else
{
return (int)floor(GetX());
}
;}
int Vertex::GetYInt(bool forceRoundUp) const
{
if (forceRoundUp)
{
return (int)ceil(GetY());
}
else
{
return (int)floor(GetY());
}
}
int Vertex::GetZInt(bool forceRoundUp) const
{
if (forceRoundUp)
{
return (int)ceil(GetZ());
}
else
{
return (int)floor(GetZ());
}
}
int Vertex::GetWInt(bool forceRoundUp) const
{
if (forceRoundUp)
{
return (int)ceil(GetW());
}
else
{
return (int)floor(GetW());
}
}
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
// to ourselves
if (this != &rhs)
{
Copy(rhs);
}
return *this;
}
const Vector3D Vertex::operator-(const Vertex& rhs) const
{
return Vector3D(rhs.GetX() - _x, rhs.GetY() - _y, rhs.GetZ() - _z);
}
// The const at the end of the declaraion for '==" indicates that this operation does not change
// any of the member variables in this class.
bool Vertex::operator==(const Vertex& rhs) const
{
return (_x == rhs.GetX() && _y == rhs.GetY() && _z == rhs.GetZ() && _w == rhs.GetW());
}
// You can see three different uses of 'const' here:
//
// The first const indicates that the method changes the return value, but it is not moved in memory
// The second const indicates that the parameter is passed by reference, but it is not modified
// The third const indicates that the operator does not change any of the memory variables in the class.
const Vertex Vertex::operator+(const Vertex& rhs) const
{
return Vertex(_x + rhs.GetX(), _y + rhs.GetY(), _z + rhs.GetZ(), _w + rhs.GetW());
}
void Vertex::Copy(const Vertex& other)
{
_x = other.GetX();
_y = other.GetY();
_z = other.GetZ();
_w = other.GetW();
_contributeCount = other.GetContributeCount();
SetNormal(other.GetNormal());
SetColor(other.GetR(), other.GetG(), other.GetB());
}