Skip to content

Commit

Permalink
Even more template parameterization
Browse files Browse the repository at this point in the history
  • Loading branch information
willow385 committed Jan 5, 2020
1 parent 3715535 commit 5e73b63
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 126 deletions.
61 changes: 18 additions & 43 deletions demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <djf-3d-2/djf-3d.h>

int main(int argc, char *argv[]) {
Expand Down Expand Up @@ -92,42 +90,24 @@ int main(int argc, char *argv[]) {

/* We move around the model(s) in the scene based
on user input. */
if (keyboard_state.A) {
model.rotate_self(
djf_3d::Axis::Z,
-1.0
);
}
if (keyboard_state.D) {
model.rotate_self(
djf_3d::Axis::Z,
+1.0
);
}
if (keyboard_state.W) {
model.rotate_self(
djf_3d::Axis::X,
-1.0
);
}
if (keyboard_state.S) {
model.rotate_self(
djf_3d::Axis::X,
+1.0
);
}
if (keyboard_state.Q) {
model.rotate_self(
djf_3d::Axis::Y,
-1.0
);
}
if (keyboard_state.E) {
model.rotate_self(
djf_3d::Axis::Y,
+1.0
);
}
if (keyboard_state.A)
model.rotate_self<djf_3d::Axis::Z>(-0.5);

if (keyboard_state.D)
model.rotate_self<djf_3d::Axis::Z>(+0.5);

if (keyboard_state.W)
model.rotate_self<djf_3d::Axis::X>(-0.5);

if (keyboard_state.S)
model.rotate_self<djf_3d::Axis::X>(+0.5);

if (keyboard_state.Q)
model.rotate_self<djf_3d::Axis::Y>(-0.5);

if (keyboard_state.E)
model.rotate_self<djf_3d::Axis::Y>(+0.5);

if (keyboard_state.I) {
model.translate<djf_3d::Axis::Y>(-5.0);
}
Expand All @@ -150,11 +130,6 @@ int main(int argc, char *argv[]) {
// We exit the loop if the user presses X.
if (keyboard_state.X) break;

/* We delay refreshing the frame so as to get a
consistent framerate of 60 fps. */
std::this_thread::sleep_for(
std::chrono::microseconds(16667)
);
}

std::cout << "Program exited normally." << std::endl;
Expand Down
Binary file modified djf-3d-2.tar.gz
Binary file not shown.
53 changes: 30 additions & 23 deletions src/Model3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,50 +213,57 @@ const Polygon& Model3d::nth_face(
return faces[index];
}

template <>
void Model3d::translate<Axis::X>(
template <Axis axis>
void Model3d::translate(
const float distance
) noexcept {
centroid.translate<Axis::X>(distance);
centroid.translate<axis>(distance);
for (auto& vertex: vertices) {
vertex.translate<Axis::X>(distance);
vertex.translate<axis>(distance);
}
}

template <>
void Model3d::translate<Axis::Y>(
template void Model3d::translate<Axis::X>(
const float distance
) noexcept {
centroid.translate<Axis::Y>(distance);
for (auto& vertex: vertices) {
vertex.translate<Axis::Y>(distance);
}
}
) noexcept;

template <>
void Model3d::translate<Axis::Z>(
template void Model3d::translate<Axis::Y>(
const float distance
) noexcept {
centroid.translate<Axis::Z>(distance);
for (auto& vertex: vertices) {
vertex.translate<Axis::Z>(distance);
}
}
) noexcept;

template void Model3d::translate<Axis::Z>(
const float distance
) noexcept;


template <Axis axis>
void Model3d::rotate_self(
const Axis axis,
const float theta_degrees
) noexcept {
for (auto& vertex: vertices) {
vertex.rotate_3d(
axis,
vertex.rotate_3d<axis>(
centroid,
theta_degrees
);
}
}

template
void Model3d::rotate_self<Axis::X>(
const float theta_degrees
) noexcept;

template
void Model3d::rotate_self<Axis::Y>(
const float theta_degrees
) noexcept;

template
void Model3d::rotate_self<Axis::Z>(
const float theta_degrees
) noexcept;


void Model3d::scale(const float amount) noexcept {
for (auto& vertex: vertices) {
float new_x = vertex.get_pos<Axis::X>() * amount;
Expand Down
2 changes: 1 addition & 1 deletion src/Model3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class Model3d {
* @param axis the djf_3d::Axis about which to rotate
* @param theta_degrees number of degrees to rotate
*/
template <Axis axis>
void rotate_self(
const Axis axis,
const float theta_degrees
) noexcept;

Expand Down
131 changes: 79 additions & 52 deletions src/Vec3f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,74 +23,100 @@ void Vec3f::set_position(
const float y,
const float z
) noexcept {
components[0] = x;
components[1] = y;
components[2] = z;
components[(int) Axis::X] = x;
components[(int) Axis::Y] = y;
components[(int) Axis::Z] = z;
}

template <>
float Vec3f::get_pos<Axis::X>(void) const {
return components[0];
template <Axis axis>
float Vec3f::get_pos(void) const noexcept {
return components[(int) axis];
}

template <>
float Vec3f::get_pos<Axis::Y>(void) const {
return components[1];
}
template float Vec3f::get_pos<Axis::X>(void) const noexcept;
template float Vec3f::get_pos<Axis::Y>(void) const noexcept;
template float Vec3f::get_pos<Axis::Z>(void) const noexcept;

template <>
float Vec3f::get_pos<Axis::Z>(void) const {
return components[2];
template <Axis axis>
void Vec3f::translate(const float distance) noexcept {
components[(int) axis] += distance;
}

template <>
void Vec3f::translate<Axis::X>(const float distance) {
components[0] += distance;
}
template void Vec3f::translate<Axis::X>(
const float distance
) noexcept;
template void Vec3f::translate<Axis::Y>(
const float distance
) noexcept;
template void Vec3f::translate<Axis::Z>(
const float distance
) noexcept;

template <>
void Vec3f::translate<Axis::Y>(const float distance) {
components[1] += distance;
void Vec3f::rotate_3d<Axis::X>(
const Vec3f& axis_point,
const float theta_degrees
) noexcept {
float *pos_0 = components + 2;
float *pos_1 = components + 1;
float about_0 = axis_point.get_pos<Axis::Z>();
float about_1 = axis_point.get_pos<Axis::Y>();

const float radians
= theta_degrees * (3.14159 / 180.0);

const float sin_theta = std::sin(radians);
const float cos_theta = std::cos(radians);

*pos_0 -= about_0;
*pos_1 -= about_1;

const float new_0
= *pos_0 * cos_theta - *pos_1 * sin_theta;
const float new_1
= *pos_0 * sin_theta + *pos_1 * cos_theta;

*pos_0 = new_0 + about_0;
*pos_1 = new_1 + about_1;
}

template <>
void Vec3f::translate<Axis::Z>(const float distance) {
components[2] += distance;
void Vec3f::rotate_3d<Axis::Y>(
const Vec3f& axis_point,
const float theta_degrees
) noexcept {
float *pos_0 = components;
float *pos_1 = components + 2;
float about_0 = axis_point.get_pos<Axis::X>();
float about_1 = axis_point.get_pos<Axis::Z>();

const float radians
= theta_degrees * (3.14159 / 180.0);

const float sin_theta = std::sin(radians);
const float cos_theta = std::cos(radians);

*pos_0 -= about_0;
*pos_1 -= about_1;

const float new_0
= *pos_0 * cos_theta - *pos_1 * sin_theta;
const float new_1
= *pos_0 * sin_theta + *pos_1 * cos_theta;

*pos_0 = new_0 + about_0;
*pos_1 = new_1 + about_1;
}

void Vec3f::rotate_3d(
const Axis axis,
template <>
void Vec3f::rotate_3d<Axis::Z>(
const Vec3f& axis_point,
const float theta_degrees
) {
float *pos_0;
float *pos_1;
float about_0;
float about_1;
switch (axis) {
case Axis::X:
pos_0 = components + 2;
pos_1 = components + 1;
about_0 = axis_point.get_pos<Axis::Z>();
about_1 = axis_point.get_pos<Axis::Y>();
break;
case Axis::Y:
pos_0 = components;
pos_1 = components + 2;
about_0 = axis_point.get_pos<Axis::X>();
about_1 = axis_point.get_pos<Axis::Z>();
break;
case Axis::Z:
pos_0 = components;
pos_1 = components + 1;
about_0 = axis_point.get_pos<Axis::X>();
about_1 = axis_point.get_pos<Axis::Y>();
break;
default:
throw std::invalid_argument(
"Invalid axis for Vec3f::rotate_3d()"
);
}
) noexcept {
float *pos_0 = components;
float *pos_1 = components + 1;
float about_0 = axis_point.get_pos<Axis::X>();
float about_1 = axis_point.get_pos<Axis::Y>();

const float radians
= theta_degrees * (3.14159 / 180.0);
Expand All @@ -110,6 +136,7 @@ void Vec3f::rotate_3d(
*pos_1 = new_1 + about_1;
}


float Vec3f::project_2d_x(
const float vanish_x,
const float fov
Expand Down
14 changes: 7 additions & 7 deletions src/Vec3f.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
namespace djf_3d {

enum class Axis {
X, // left/right axis (horizontal, paralell to screen)
Y, // near/far axis (horizontal, perpendicular to screen)
Z // up/down axis (vertical)
X = 0, // left/right axis (horizontal, paralell to screen)
Y = 1, // near/far axis (horizontal, perpendicular to screen)
Z = 2 // up/down axis (vertical)
};

class Vec3f {
Expand Down Expand Up @@ -45,7 +45,7 @@ class Vec3f {
* @return x_pos, y_pos, or z_pos, according to the axis
*/
template <Axis axis>
float get_pos(void) const;
float get_pos(void) const noexcept;

/**
* This translates the Vec3f along one of the axes.
Expand All @@ -54,7 +54,7 @@ class Vec3f {
* @param distance the amount by which to move
*/
template <Axis axis>
void translate(const float distance);
void translate(const float distance) noexcept;

/**
* This rotates the Vec3f about one of the three axes
Expand All @@ -64,11 +64,11 @@ class Vec3f {
* @param axis_point point to rotate around
* @param theta_degrees number of degrees to rotate
*/
template <Axis axis>
void rotate_3d(
const Axis axis,
const Vec3f& axis_point,
const float theta_degrees
);
) noexcept;


/* IMPORTANT: REGARDING THE PROJECTION METHODS.
Expand Down

0 comments on commit 5e73b63

Please sign in to comment.