Files
Graphics-Rasterizer/Vertex.cpp
IDunnoDev 773507b4ab Week7 [09/11] - [11/11]
Added Backface Culling Methods to the Model Class
Added Depth, Normal and Culled Flag Variables to the Polygon3D Class
Added Vector3D Class
Added - operator to the Vertex Class
Cleaned up Code, Adding Void to Params etc
2021-12-11 14:48:46 +00:00

122 lines
2.0 KiB
C++

#include "Vertex.h"
Vertex::Vertex()
{
_x = 0.0f;
_y = 0.0f;
_z = 0.0f;
_w = 0.0f;
}
Vertex::Vertex(const float x, const float y, const float z)
{
_x = x;
_y = y;
_z = z;
_w = 1.0f;
}
Vertex::Vertex(const float x, const float y, const float z, const float w)
{
_x = x;
_y = y;
_z = z;
_w = w;
}
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;
}
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() - GetX(), rhs.GetY() - GetY(), rhs.GetZ() - GetZ());
}
// 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();
}