Skip to content

Commit

Permalink
Converted application to only use floating point, enabled compiler fl…
Browse files Browse the repository at this point in the history
…ags to enable fast floating point math, added constxpr for converting degrees to radians
  • Loading branch information
ZaymonFC committed Apr 7, 2018
1 parent 687a7f3 commit a363911
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 79 deletions.
50 changes: 21 additions & 29 deletions LabSoftware VS2010/GraphicsMath.cpp
Original file line number Diff line number Diff line change
@@ -1,67 +1,59 @@
#include "GraphicsMath.h"
#include "Point.h"

Point GraphicsMath::RotatePoint(const Point& point, const int rotationX, const int rotationY, const int rotationZ)
#include <iostream>

/**
* \param point Point to rotate
* \param a Rotation around the x Axis
* \param b Rotation around the y Axis
* \param c Rotation around the z Axis
* \return
*/
Point GraphicsMath::RotatePoint(const Point& point, const float a, const float b, const float c)
{
// Degress to rad conversions
const auto a = rotationX * 0.0174533;
const auto b = rotationY * 0.0174533;
const auto c = rotationZ * 0.0174533;

// const auto x3 = point.x * (cos(b) * cos(c)) +
// point.y * (sin(a)* sin(b)* cos(c) - cos(a) * sin(c)) +
// point.z * (cos(a) * sin(b) * cos(c) + sin(a) * sin(c));
//
// const auto y3 = point.x * (cos(b) * sin(c)) +
// point.y * (sin(a) * sin(b) * sin(c) + cos(a) * cos(c)) +
// point.z * (cos(a) * sin(b) * sin(c) - sin(a) * cos(c));
//
// const auto z3 = point.x * sin(b) + point.y * sin(a) * cos(b) + point.z * cos(a) * cos(b);


const auto x3 = point.x * (cos(b) * cos(c)) + point.z * sin(b) - point.y * (cos(b) * sin(c));
const auto y3 = -point.z * (cos(b) * sin(a)) + point.x * (cos(c) * sin(a) * sin(b) + cos(a) * sin(c)) + point.y * (cos(a) * cos(c) - sin(a) * sin(b) * sin(c));
const auto z3 = point.z * (cos(a) * cos(b)) + point.x * (sin(a) * sin(c) - cos(a) * cos(c) * sin(b)) + point.y * (cos(c) * sin(a) + cos(a) * sin(b) * sin(c));

const float x3 = point.x * (cos(b) * cos(c)) + point.z * sin(b) - point.y * (cos(b) * sin(c));
const float y3 = -point.z * (cos(b) * sin(a)) + point.x * (cos(c) * sin(a) * sin(b) + cos(a) * sin(c)) + point.y * (cos(a) * cos(c) - sin(a) * sin(b) * sin(c));
const float z3 = point.z * (cos(a) * cos(b)) + point.x * (sin(a) * sin(c) - cos(a) * cos(c) * sin(b)) + point.y * (cos(c) * sin(a) + cos(a) * sin(b) * sin(c));

// std::cout << a << b << c;
return {x3, y3, z3, point.colour};
}

double GraphicsMath::Clamp(const double value, const double minimum = 0, const double maximum = 1)
float GraphicsMath::Clamp(const float value, const float minimum = 0, const float maximum = 1)
{
return value > maximum ? 1 :
value < minimum ? 0 : value;
}

double GraphicsMath::LinearLerp(const double start, const double end, const double gradient)
float GraphicsMath::LinearLerp(const float start, const float end, float gradient)
{
return start + (end - start) * Clamp(gradient);
}

double GraphicsMath::CrossProduct(const double x1, const double y1, const double x2, const double y2)
float GraphicsMath::CrossProduct(const float x1, const float y1, const float x2, const float y2)
{
return x1 * y2 - x2 * y1;
}

double GraphicsMath::CrossProduct(const Point v1, const Point v2)
float GraphicsMath::CrossProduct(const Point v1, const Point v2)
{
return (v1.x * v2.y) - (v1.y * v2.x);
}

double GraphicsMath::LineSide2D(const Point p, const Point lineFrom, const Point lineTo)
float GraphicsMath::LineSide2D(const Point p, const Point lineFrom, const Point lineTo)
{
return CrossProduct(p.x - lineFrom.x, p.y - lineFrom.y, lineTo.x - lineFrom.x, lineTo.y - lineFrom.y);
}

double GraphicsMath::Convex2D(const Point origin, const Point pNext, const Point pPrev)
float GraphicsMath::Convex2D(const Point origin, const Point pNext, const Point pPrev)
{
const auto vectorA = Point(pNext.x - origin.x, pNext.y - origin.y);
const auto vectorB = Point(pPrev.x - origin.x, pPrev.y - origin.y);

return CrossProduct(vectorA, vectorB);
}

int GraphicsMath::ClipTest(double p, double q, double * u1, double * u2)
int GraphicsMath::ClipTest(float p, float q, float * u1, float * u2)
{
const auto r = q / p;

Expand Down
16 changes: 8 additions & 8 deletions LabSoftware VS2010/GraphicsMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ class Point;
class GraphicsMath
{
public:
static Point RotatePoint(const Point& point, int rotationX, int rotationY, int rotationZ);
static Point RotatePoint(const Point& point, float a, float b, float c);

static double Clamp(double value, double minimum, double maximum);
static double LinearLerp( double start, double end, double gradient);
static double CrossProduct( double x1, double y1, double x2, double y2);
static int ClipTest(double p, double q, double* u1, double* u2);
static float Clamp(float value, float minimum, float maximum);
static float LinearLerp(float start, float end, float gradient);
static float CrossProduct( float x1, float y1, float x2, float y2);
static int ClipTest(float p, float q, float* u1, float* u2);
static bool PointInTriangle(Point p, Point a, Point b, Point c);
static double CrossProduct( Point v1, Point v2);
static double LineSide2D( Point p, Point lineFrom, Point lineTo);
static double Convex2D( Point origin, Point pNext, Point pPrev);
static float CrossProduct( Point v1, Point v2);
static float LineSide2D( Point p, Point lineFrom, Point lineTo);
static float Convex2D( Point origin, Point pNext, Point pPrev);
static int SameSide(Point a, Point b, Point l1, Point l2);
};

4 changes: 3 additions & 1 deletion LabSoftware VS2010/LabSoftware VS2010.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<Optimization>Full</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<FloatingPointModel>Fast</FloatingPointModel>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down
11 changes: 10 additions & 1 deletion LabSoftware VS2010/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Mesh.h"
#include "GraphicsMath.h"
#include <tuple>
#include <iostream>
#include <string>


Mesh::Mesh(int vertexCount, int polygonCount, std::vector<Point> points, std::vector<std::vector<int>> polygons) :
Expand Down Expand Up @@ -30,7 +32,7 @@ void Mesh::Translate(const int xAmount, const int yAmount, const int zAmount)
z += zAmount;
}

auto Mesh::Rotate(const int xAmount, const int yAmount, const int zAmount) -> void
auto Mesh::Rotate(const float xAmount, const float yAmount, const float zAmount) -> void
{
rotationX += xAmount;
rotationY += yAmount;
Expand All @@ -42,6 +44,13 @@ auto Mesh::Scale(const double scaleFactor) -> void
Mesh::scaleFactor += scaleFactor;
}

auto Mesh::PrintStatus() const -> void
{
std::cout << "Position:" << " x: " << x << " y: " << y << " z: " << z << std::endl;
// std::cout << "Scale: " << scaleFactor << std::endl;
std::cout << "Position:" << " Rotation:" << " x: " << rotationX << " y: " << rotationY << " z: " << rotationZ << std::endl;
}

auto Mesh::TransformVertices() const -> std::vector<Point>
{
auto transformedVertices = std::vector<Point>();
Expand Down
15 changes: 7 additions & 8 deletions LabSoftware VS2010/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ class Mesh
int x;
int y;
int z;
public:
int rotationX;
int rotationY;
int rotationZ;
float rotationX;
float rotationY;
float rotationZ;
double scaleFactor;

public:
int vertexCount;
int polygonCount;
std::vector<Point> vertices;
Expand All @@ -24,10 +23,10 @@ class Mesh
Mesh(int vertexCount, int polygonCount, std::vector<Point> points, std::vector<std::vector<int>> polygons);

void Translate(int xAmount, int yAmount, int zAmount);
auto TransformVertices() const -> std::vector<Point>;

auto Rotate(int xAmount, int yAmount, int zAmount) -> void;
auto Rotate(float xAmount, float yAmount, float zAmount) -> void;
auto Scale(double scaleFactor) -> void;
auto TransformVertices() const -> std::vector<Point>;
auto PrintStatus() const -> void;

};

2 changes: 1 addition & 1 deletion LabSoftware VS2010/MeshLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Mesh MeshLoader::LoadMesh(std::string fileName)

const auto vertexCount = jsonMesh["VertexCount"].get<int>();
const auto polygonCount = jsonMesh["PolygonCount"].get<int>();
auto vertexData = jsonMesh["Vertices"].get<std::vector<std::vector<int>>>();
auto vertexData = jsonMesh["Vertices"].get<std::vector<std::vector<float>>>();
auto polygonData = jsonMesh["Polygons"].get<std::vector<std::vector<int>>>();

auto vertices = std::vector<Point>();
Expand Down
8 changes: 4 additions & 4 deletions LabSoftware VS2010/Point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ Point::Point()
colour = Colour(255, 255, 255);
}

Point::Point(const double x, const double y) : x{x}, y{y}
Point::Point(const float x, const float y) : x{x}, y{y}
{
z = 0;
colour = Colour(255, 255, 255);
}

Point::Point(const double x, const double y, const double z) : x {x}, y{y}, z{z}
Point::Point(const float x, const float y, const float z) : x {x}, y{y}, z{z}
{
}

Point::Point(const double x, const double y, const Colour colour) : x{x}, y{y}, colour{colour}
Point::Point(const float x, const float y, const Colour colour) : x{x}, y{y}, colour{colour}
{
z = 0;
}

Point::Point(const double x, const double y, const double z, const Colour colour) : x{x}, y{y}, z{z}, colour{colour}
Point::Point(const float x, const float y, const float z, const Colour colour) : x{x}, y{y}, z{z}, colour{colour}
{
}

Expand Down
10 changes: 5 additions & 5 deletions LabSoftware VS2010/Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
class Point
{
public:
double x, y, z;
float x, y, z;
Colour colour;

Point();
Point(double x, double y);
Point(double x, double y, double z);
Point(double x, double y, Colour colour);
Point(double x, double y, double z, Colour colour);
Point(float x, float y);
Point(float x, float y, float z);
Point(float x, float y, Colour colour);
Point(float x, float y, float z, Colour colour);
bool operator==(const Point & otherPoint) const;
};
18 changes: 9 additions & 9 deletions LabSoftware VS2010/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ void Render::DrawLine_Dda(const Point p0, const Point p1)

void Render::DrawClipLine(Point p1, Point p2)
{
auto u1 = 0.0;
auto u2 = 1.0;
const double dx = p2.x - p1.x;
auto u1 = 0.0f;
auto u2 = 1.0f;
const float dx = p2.x - p1.x;

if (GraphicsMath::ClipTest(-dx, p1.x - 0, &u1, &u2))
{
if (GraphicsMath::ClipTest(dx, static_cast<double>(frame_wide_ - p1.x), &u1, &u2))
{
const double dy = p2.y - p1.y;
const float dy = p2.y - p1.y;
if (GraphicsMath::ClipTest(-dy, p1.y - 0, &u1, &u2))
{
if (GraphicsMath::ClipTest(dy, static_cast<double>(frame_high_ - p1.y), &u1, &u2))
if (GraphicsMath::ClipTest(dy, static_cast<float>(frame_high_ - p1.y), &u1, &u2))
{
if (u2 < 1.0)
{
Expand Down Expand Up @@ -155,7 +155,7 @@ void Render::DrawTriangle(Point p1, Point p2, Point p3)
// Draw Triangle - P2 on the right
if (GraphicsMath::LineSide2D(p2, p1, p3) > 0)
{
for (auto y = static_cast<int>(p1.y); y <= p3.y; y++)
for (auto y = static_cast<int>(floor(p1.y)); y <= p3.y; y++)
{
// Calculate the colour on the left edge p1-p3 (Common to top and bottom)
const auto stepsP1P3 = abs(p1.y - p3.y);
Expand Down Expand Up @@ -184,7 +184,7 @@ void Render::DrawTriangle(Point p1, Point p2, Point p3)
// Draw Triangle - P2 on the left
else
{
for (auto y = static_cast<int>(p1.y); y < p3.y; y++)
for (auto y = static_cast<int>(floor(p1.y)); y < p3.y; y++)
{
// Calculate the colour on the right edge p1-p3 (Common edge)
const auto stepsP1P3 = abs(p1.y - p3.y);
Expand Down Expand Up @@ -229,8 +229,8 @@ void Render::DrawTriangle(Face face)

Point Render::ProjectionTransformPoint(std::vector<Point>::const_reference point, const int d) const
{
const auto x = (((point.x - (frame_wide_ / 2.0)) * d) / (point.z + d)) + (frame_wide_ / 2.0);
const auto y = (((point.y - (frame_high_ / 2.0)) * d) / (point.z + d)) + (frame_high_ / 2.0);
const auto x = (((point.x - (frame_wide_ / 2.0f)) * d) / (point.z + d)) + (frame_wide_ / 2.0f);
const auto y = (((point.y - (frame_high_ / 2.0f)) * d) / (point.z + d)) + (frame_high_ / 2.0f);
return {x, y, point.z, point.colour};
}

Expand Down
36 changes: 23 additions & 13 deletions LabSoftware VS2010/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <cstdlib>
#include <cstring>
#include <cmath>

#ifdef _WIN32
#include "libs/glut.h"
Expand Down Expand Up @@ -28,6 +29,12 @@ using BYTE = unsigned char;
const auto frame_wide = 1280;
const auto frame_high = 720;


constexpr float Degrees(const float f)
{
return f * (3.141592653589793238462643383279502884f / 180.0f);
}

//
// ─── GLOBAL VARIABLES ───────────────────────────────────────────────────────────
//
Expand Down Expand Up @@ -62,6 +69,7 @@ void SpecialInput(int key, int x, int y);
// ─── Meshes ───────────────────────────────────────────────────────
//
auto mesh = MeshLoader::LoadMesh("Objects/cube.json");
int frameCount = 0;

//
// ───────────────────────────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -150,15 +158,16 @@ void OnKeypress(const unsigned char key, int x, int y)
// case 's': stereo ^= 1, eyes = 10;break;
case ']': eyes++; break;
case '[': eyes--; break;
case 'w': mesh.Rotate(-10, 0, 0); break;
case 's': mesh.Rotate(10, 0, 0); break;
case 'a': mesh.Rotate(0, 0, 10); break;
case 'd': mesh.Rotate(0, 0, -10); break;
case 'x': mesh.Rotate(0, -10, 0); break;
case 'z': mesh.Rotate(0, 10, 0); break;
case 'w': mesh.Rotate(Degrees(-10), Degrees(0), Degrees(0)); break;
case 's': mesh.Rotate(Degrees(10), Degrees(0), Degrees(0)); break;
case 'a': mesh.Rotate(Degrees(0), Degrees(0), Degrees(10)); break;
case 'd': mesh.Rotate(Degrees(0), Degrees(0), Degrees(-10)); break;
case 'x': mesh.Rotate(Degrees(0), Degrees(-10), Degrees(0)); break;
case 'z': mesh.Rotate(Degrees(0), Degrees(10), Degrees(0)); break;
case 27 : exit(0);
default: ;
}
mesh.PrintStatus();
}

void SpecialInput(int key, int x, int y)
Expand All @@ -169,7 +178,7 @@ void SpecialInput(int key, int x, int y)
if (key == GLUT_KEY_DOWN) mesh.Translate(0, 40, 0);
if (key == GLUT_KEY_PAGE_UP) mesh.Scale(0.1);
if (key == GLUT_KEY_PAGE_DOWN) mesh.Scale(-0.1);
// std::cout << "Roation: x: " << mesh.rotationX << " y:" << mesh.rotationY << " z:" << mesh.rotationZ << std::endl;
mesh.PrintStatus();
}

//
Expand Down Expand Up @@ -231,20 +240,20 @@ void BuildFrame(BYTE *pFrame, int view)
// Point(400, 400),
// Point(0, 400, blue)
// };

//
// auto points = std::vector<Point>();
//
// const auto pointCount = 9;
// double a = 0, x = 0, y = 0;
// const auto angle = (360.0 / pointCount);
// const auto pointCount = 100;
// float a = 0, x = 0, y = 0;
// const auto angle = (360.0f / pointCount);
//
// for (auto i = 0; i < pointCount; i++) {
// const auto r = rand() % 250;
//
// a -= angle * (3.14159262 / 180);
//
// x = r * cos(a) + static_cast<double>(frame_wide) / 2;
// y = r * sin(a) + static_cast<double>(frame_high) / 2;
// x = r * cos(a) + static_cast<float>(frame_wide) / 2;
// y = r * sin(a) + static_cast<float>(frame_high) / 2;
//
// points.emplace_back(x, y, 100, Colour(rand() % 255, rand() % 255, rand() % 255));
// }
Expand All @@ -253,6 +262,7 @@ void BuildFrame(BYTE *pFrame, int view)

_render.DrawMesh(mesh);

// std::cout << "Frame: " << frameCount++ << "\n";
// _render.ClearZBuffer();
}

Expand Down

0 comments on commit a363911

Please sign in to comment.