aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2023-11-26 08:43:14 +0100
committervimene <vincent.menegaux@gmail.com>2023-11-26 08:43:14 +0100
commit5a1d67d1797a3db874e44500e13237d676843185 (patch)
tree17d0f1c2db6cc32fe40fdde4a081b96e8ad0ae13
parentf63febed2a769d0c55192e192a20b8e39f162932 (diff)
downloadengine-5a1d67d1797a3db874e44500e13237d676843185.tar.gz
added namespaces, made every function in engine.cpp static and added warnings' flags
-rw-r--r--Makefile.am3
-rw-r--r--src/engine.cpp40
-rw-r--r--src/fb/chfb.cpp30
-rw-r--r--src/fb/chfb.h8
-rw-r--r--src/fb/pixfb.cpp30
-rw-r--r--src/fb/pixfb.h8
-rw-r--r--src/math/mat4.cpp4
-rw-r--r--src/math/mat4.h4
-rw-r--r--src/math/math_vector.cpp20
-rw-r--r--src/math/math_vector.h4
-rw-r--r--src/o3d/obj3d.cpp19
-rw-r--r--src/o3d/obj3d.h4
-rw-r--r--src/o3d/tri_vertex.cpp14
-rw-r--r--src/o3d/tri_vertex.h4
-rw-r--r--src/o3d/vertex.cpp6
-rw-r--r--src/o3d/vertex.h12
-rw-r--r--src/o3d/vertex_data.cpp2
-rw-r--r--src/o3d/vertex_data.h4
18 files changed, 135 insertions, 81 deletions
diff --git a/Makefile.am b/Makefile.am
index 2a81b4c..7f0c5bf 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,6 @@
ACLOCAL_AMFLAGS = -Im4 --install
-AM_CPPFLAGS = $(DEPS_CFLAGS)
+AM_CPPFLAGS = $(DEPS_CFLAGS) -Wall -Wextra
bin_PROGRAMS = engine
engine_SOURCES = src/engine.cpp \
@@ -10,6 +10,7 @@ engine_SOURCES = src/engine.cpp \
src/o3d/obj3d.h src/o3d/obj3d.cpp src/o3d/tri_vertex.h \
src/o3d/tri_vertex.cpp src/o3d/vertex.h src/o3d/vertex.cpp \
src/o3d/vertex_data.h src/o3d/vertex_data.cpp
+engine_LDFLAGS = -Wall -Wextra
engine_LDADD = $(DEPS_LIBS)
EXTRA_DIST = m4/NOTES
diff --git a/src/engine.cpp b/src/engine.cpp
index 69b2f43..112d230 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -33,7 +33,7 @@
#define MODE_TERM 1
#define MODE_GRAPHICAL 2
-void print_usage(std::ostream& output_stream) {
+static void print_usage(std::ostream& output_stream) {
output_stream << "Usage: ./engine [-tg] [--term] [--graphical]\n"
<< " -h, --help show usage (this)\n"
<< " -t, --term terminal mode\n"
@@ -41,35 +41,39 @@ void print_usage(std::ostream& output_stream) {
<< std::flush;
}
-[[noreturn]] void usage_error_exit() {
+[[noreturn]]
+static void usage_error_exit() {
print_usage(std::cerr);
std::exit(EXIT_FAILURE);
}
template <class FB>
-void scene_main(FB& fb, std::function<bool()> update_frame) {
- MathVector3 a{0.f, 0.f, 0.f};
+static void scene_main(FB& fb, std::function<bool()> update_frame) {
+ engine::math::MathVector3 a{0.f, 0.f, 0.f};
float dist = 4.f;
float rad = 5.f;
bool cont = true;
- std::array<Object3D, 2> objs{{ Object3D::cube(), Object3D::cube() }};
- auto scale_mat = Mat4::scale(rad);
+ std::array<engine::o3d::Object3D, 2> objs{{ engine::o3d::Object3D::cube(), engine::o3d::Object3D::cube() }};
+ auto scale_mat = engine::math::Mat4::scale(rad);
while (cont) {
a.x += .0050f;
a.y += .0065f;
a.z += .0080f;
fb.clear();
- auto transform_mat = Mat4::translate(MathVector3{0.f, 0.f, -rad * dist}) * Mat4::rot_z(a.z) * Mat4::rot_y(a.y) * Mat4::rot_x(a.x);
- std::array<Mat4, 2> mats{{
- transform_mat * Mat4::translate(MathVector3{-.5f * rad, -.5f * rad, -.5f * rad}) * scale_mat,
- transform_mat * Mat4::translate(MathVector3{+.5f * rad, +.5f * rad, +.5f * rad}) * scale_mat,
+ auto transform_mat =
+ engine::math::Mat4::translate(engine::math::MathVector3{0.f, 0.f, -rad * dist})
+ * engine::math::Mat4::rot_z(a.z)
+ * engine::math::Mat4::rot_y(a.y) * engine::math::Mat4::rot_x(a.x);
+ std::array<engine::math::Mat4, 2> mats{{
+ transform_mat * engine::math::Mat4::translate(engine::math::MathVector3{-.5f * rad, -.5f * rad, -.5f * rad}) * scale_mat,
+ transform_mat * engine::math::Mat4::translate(engine::math::MathVector3{+.5f * rad, +.5f * rad, +.5f * rad}) * scale_mat,
}};
- auto projection_mat = Mat4::projection(static_cast<float>(fb.height()) / static_cast<float>(fb.width()), 2.f, 50.f);
+ auto projection_mat = engine::math::Mat4::projection(static_cast<float>(fb.height()) / static_cast<float>(fb.width()), 2.f, 50.f);
for (int i = 0; i < 2; i++) {
auto final_mat = projection_mat * mats[i];
for (auto triangle : objs[i]) {
- TriangleVertex4 t{triangle};
+ engine::o3d::TriangleVertex4 t{triangle};
t.vertex1.point = final_mat * t.vertex1.point;
t.vertex2.point = final_mat * t.vertex2.point;
@@ -90,7 +94,7 @@ void scene_main(FB& fb, std::function<bool()> update_frame) {
#define MKEY_ESC 27
-int main_term() {
+static int main_term() {
// init
setlocale(LC_ALL, "");
initscr();
@@ -103,7 +107,7 @@ int main_term() {
int w, h;
getmaxyx(stdscr, h, w);
- CharacterFrameBuffer cfb{static_cast<unsigned int>(w), static_cast<unsigned int>(h)};
+ engine::fb::CharacterFrameBuffer cfb{static_cast<unsigned int>(w), static_cast<unsigned int>(h)};
scene_main(cfb, [&]() {
mvaddnstr(0, 0, cfb.chars(), cfb.width() * cfb.height());
@@ -153,7 +157,7 @@ int main_term() {
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
-int main_graphical() {
+static int main_graphical() {
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* texture = NULL;
@@ -170,7 +174,7 @@ int main_graphical() {
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
- PixelFrameBuffer pfb{SCREEN_WIDTH, SCREEN_HEIGHT};
+ engine::fb::PixelFrameBuffer pfb{SCREEN_WIDTH, SCREEN_HEIGHT};
SDL_Event e;
@@ -200,14 +204,14 @@ int main_graphical() {
return EXIT_SUCCESS;
}
-std::vector<std::string_view> convert_args(int argc, char *argv[]) {
+static std::vector<std::string_view> convert_args(int argc, char *argv[]) {
std::vector<std::string_view> args(argc);
for (int i = 0; i < argc; i++)
args[i] = argv[i];
return args;
}
-void parse_args(const std::vector<std::string_view>& args, int& mode) {
+static void parse_args(const std::vector<std::string_view>& args, int& mode) {
for (auto args_iter = std::next(args.begin()); args_iter != args.end(); args_iter++) {
const auto& arg = *args_iter;
if (arg.size() >= 1 && arg[0] == '-') {
diff --git a/src/fb/chfb.cpp b/src/fb/chfb.cpp
index e2355df..82b8f86 100644
--- a/src/fb/chfb.cpp
+++ b/src/fb/chfb.cpp
@@ -7,6 +7,8 @@
#include "o3d/tri_vertex.h"
#include "o3d/vertex_data.h"
+using namespace engine::fb;
+
CharacterFrameBuffer::CharacterFrameBuffer(unsigned int w, unsigned int h) {
resize(w, h);
}
@@ -37,12 +39,12 @@ void CharacterFrameBuffer::clear() {
face_ind = -1;
}
-void CharacterFrameBuffer::_draw_cropped_triangle(TriangleVertex3 triangle) {
- std::array<Vertex3*, 3> sorted_vs = { &triangle.vertex1, &triangle.vertex2, &triangle.vertex3 };
+void CharacterFrameBuffer::_draw_cropped_triangle(engine::o3d::TriangleVertex3 triangle) {
+ std::array<engine::o3d::Vertex3*, 3> sorted_vs = { &triangle.vertex1, &triangle.vertex2, &triangle.vertex3 };
#define SWAP_IF_LT(X,Y) ({\
if (sorted_vs[X]->point.y < sorted_vs[Y]->point.y) {\
- Vertex3* temp = sorted_vs[X];\
+ engine::o3d::Vertex3* temp = sorted_vs[X];\
sorted_vs[X] = sorted_vs[Y];\
sorted_vs[Y] = temp;\
}\
@@ -52,18 +54,18 @@ void CharacterFrameBuffer::_draw_cropped_triangle(TriangleVertex3 triangle) {
SWAP_IF_LT(1, 0);
#undef SWAP_IF_LT
- Vertex3 middle_vl = *sorted_vs[1];
+ engine::o3d::Vertex3 middle_vl = *sorted_vs[1];
float fac = (sorted_vs[1]->point.y - sorted_vs[0]->point.y) / (sorted_vs[2]->point.y - sorted_vs[0]->point.y);
- Vertex3 middle_vr{
+ engine::o3d::Vertex3 middle_vr{
{
sorted_vs[0]->point.x + fac * (sorted_vs[2]->point.x - sorted_vs[0]->point.x),
sorted_vs[1]->point.y,
sorted_vs[0]->point.z + fac * (sorted_vs[2]->point.z - sorted_vs[0]->point.z)
},
- VertexData::lerp(sorted_vs[0]->data, sorted_vs[2]->data, fac)
+ engine::o3d::VertexData::lerp(sorted_vs[0]->data, sorted_vs[2]->data, fac)
};
if (middle_vr.point.x < middle_vl.point.x) {
- Vertex3 temp = middle_vr;
+ engine::o3d::Vertex3 temp = middle_vr;
middle_vr = middle_vl;
middle_vl = temp;
}
@@ -123,7 +125,7 @@ void CharacterFrameBuffer::_draw_cropped_triangle(TriangleVertex3 triangle) {
}
}
-void CharacterFrameBuffer::draw_triangle(TriangleVertex4 triangle) {
+void CharacterFrameBuffer::draw_triangle(engine::o3d::TriangleVertex4 triangle) {
face_ind++;
for (auto t1 : triangle.crop_z_out(-1.f, 1.f)) {
auto t1_2 = t1.div_by_w();
@@ -131,13 +133,13 @@ void CharacterFrameBuffer::draw_triangle(TriangleVertex4 triangle) {
t1_2.vertex2.point.x *= 2.f;
t1_2.vertex3.point.x *= 2.f;
for (auto t2 : t1_2.crop_xy_out(-1.f, 1.f, -1.f, 1.f)) {
- MathVector2 pp1 = t2.vertex1.point.xy(),
- pp2 = t2.vertex2.point.xy(),
- pp3 = t2.vertex3.point.xy();
+ engine::math::MathVector2 pp1 = t2.vertex1.point.xy(),
+ pp2 = t2.vertex2.point.xy(),
+ pp3 = t2.vertex3.point.xy();
if ((pp2 - pp1).det(pp3 - pp1) >= 0.f) continue;
- t2.vertex1.point = (t2.vertex1.point + MathVector3{1.f, 1.f, 0.f}) / 2.f;
- t2.vertex2.point = (t2.vertex2.point + MathVector3{1.f, 1.f, 0.f}) / 2.f;
- t2.vertex3.point = (t2.vertex3.point + MathVector3{1.f, 1.f, 0.f}) / 2.f;
+ t2.vertex1.point = (t2.vertex1.point + engine::math::MathVector3{1.f, 1.f, 0.f}) / 2.f;
+ t2.vertex2.point = (t2.vertex2.point + engine::math::MathVector3{1.f, 1.f, 0.f}) / 2.f;
+ t2.vertex3.point = (t2.vertex3.point + engine::math::MathVector3{1.f, 1.f, 0.f}) / 2.f;
float fw = static_cast<float>(w), fh = static_cast<float>(h);
t2.vertex1.point.x = t2.vertex1.point.x * fw - .5f;
t2.vertex1.point.y = t2.vertex1.point.y * fh - .5f;
diff --git a/src/fb/chfb.h b/src/fb/chfb.h
index a3d2ed5..1bf8b69 100644
--- a/src/fb/chfb.h
+++ b/src/fb/chfb.h
@@ -5,6 +5,8 @@
#include "math/math_vector.h"
#include "o3d/tri_vertex.h"
+namespace engine::fb {
+
class CharacterFrameBuffer {
public:
CharacterFrameBuffer(unsigned int w, unsigned int h);
@@ -13,7 +15,7 @@ class CharacterFrameBuffer {
unsigned int height() const;
const char* chars() const;
void clear();
- void draw_triangle(TriangleVertex4 triangle);
+ void draw_triangle(engine::o3d::TriangleVertex4 triangle);
private:
unsigned int w, h;
@@ -21,8 +23,10 @@ class CharacterFrameBuffer {
std::vector<float> depth_buf;
int face_ind;
- void _draw_cropped_triangle(TriangleVertex3 triangle);
+ void _draw_cropped_triangle(engine::o3d::TriangleVertex3 triangle);
char face_char() const;
};
+}
+
#endif // FB_CHFB_H
diff --git a/src/fb/pixfb.cpp b/src/fb/pixfb.cpp
index d4563ca..c9e9927 100644
--- a/src/fb/pixfb.cpp
+++ b/src/fb/pixfb.cpp
@@ -8,6 +8,8 @@
#include "o3d/tri_vertex.h"
#include "o3d/vertex_data.h"
+using namespace engine::fb;
+
PixelFrameBuffer::PixelFrameBuffer(unsigned int w, unsigned int h) {
resize(w, h);
}
@@ -38,12 +40,12 @@ void PixelFrameBuffer::clear() {
face_ind = -1;
}
-void PixelFrameBuffer::_draw_cropped_triangle(TriangleVertex3 triangle) {
- std::array<Vertex3*, 3> sorted_vs = { &triangle.vertex1, &triangle.vertex2, &triangle.vertex3 };
+void PixelFrameBuffer::_draw_cropped_triangle(engine::o3d::TriangleVertex3 triangle) {
+ std::array<engine::o3d::Vertex3*, 3> sorted_vs = { &triangle.vertex1, &triangle.vertex2, &triangle.vertex3 };
#define SWAP_IF_LT(X,Y) ({\
if (sorted_vs[X]->point.y < sorted_vs[Y]->point.y) {\
- Vertex3* temp = sorted_vs[X];\
+ engine::o3d::Vertex3* temp = sorted_vs[X];\
sorted_vs[X] = sorted_vs[Y];\
sorted_vs[Y] = temp;\
}\
@@ -53,18 +55,18 @@ void PixelFrameBuffer::_draw_cropped_triangle(TriangleVertex3 triangle) {
SWAP_IF_LT(1, 0);
#undef SWAP_IF_LT
- Vertex3 middle_vl = *sorted_vs[1];
+ engine::o3d::Vertex3 middle_vl = *sorted_vs[1];
float fac = (sorted_vs[1]->point.y - sorted_vs[0]->point.y) / (sorted_vs[2]->point.y - sorted_vs[0]->point.y);
- Vertex3 middle_vr{
+ engine::o3d::Vertex3 middle_vr{
{
sorted_vs[0]->point.x + fac * (sorted_vs[2]->point.x - sorted_vs[0]->point.x),
sorted_vs[1]->point.y,
sorted_vs[0]->point.z + fac * (sorted_vs[2]->point.z - sorted_vs[0]->point.z)
},
- VertexData::lerp(sorted_vs[0]->data, sorted_vs[2]->data, fac)
+ engine::o3d::VertexData::lerp(sorted_vs[0]->data, sorted_vs[2]->data, fac)
};
if (middle_vr.point.x < middle_vl.point.x) {
- Vertex3 temp = middle_vr;
+ engine::o3d::Vertex3 temp = middle_vr;
middle_vr = middle_vl;
middle_vl = temp;
}
@@ -124,17 +126,17 @@ void PixelFrameBuffer::_draw_cropped_triangle(TriangleVertex3 triangle) {
}
}
-void PixelFrameBuffer::draw_triangle(TriangleVertex4 triangle) {
+void PixelFrameBuffer::draw_triangle(engine::o3d::TriangleVertex4 triangle) {
face_ind++;
for (auto t1 : triangle.crop_z_out(-1.f, 1.f)) {
for (auto t2 : t1.div_by_w().crop_xy_out(-1.f, 1.f, -1.f, 1.f)) {
- MathVector2 pp1 = t2.vertex1.point.xy(),
- pp2 = t2.vertex2.point.xy(),
- pp3 = t2.vertex3.point.xy();
+ engine::math::MathVector2 pp1 = t2.vertex1.point.xy(),
+ pp2 = t2.vertex2.point.xy(),
+ pp3 = t2.vertex3.point.xy();
if ((pp2 - pp1).det(pp3 - pp1) >= 0.f) continue;
- t2.vertex1.point = (t2.vertex1.point + MathVector3{1.f, 1.f, 0.f}) / 2.f;
- t2.vertex2.point = (t2.vertex2.point + MathVector3{1.f, 1.f, 0.f}) / 2.f;
- t2.vertex3.point = (t2.vertex3.point + MathVector3{1.f, 1.f, 0.f}) / 2.f;
+ t2.vertex1.point = (t2.vertex1.point + engine::math::MathVector3{1.f, 1.f, 0.f}) / 2.f;
+ t2.vertex2.point = (t2.vertex2.point + engine::math::MathVector3{1.f, 1.f, 0.f}) / 2.f;
+ t2.vertex3.point = (t2.vertex3.point + engine::math::MathVector3{1.f, 1.f, 0.f}) / 2.f;
float fw = static_cast<float>(w), fh = static_cast<float>(h);
t2.vertex1.point.x = t2.vertex1.point.x * fw - .5f;
t2.vertex1.point.y = t2.vertex1.point.y * fh - .5f;
diff --git a/src/fb/pixfb.h b/src/fb/pixfb.h
index f5f8bde..bf37d51 100644
--- a/src/fb/pixfb.h
+++ b/src/fb/pixfb.h
@@ -6,6 +6,8 @@
#include "math/math_vector.h"
#include "o3d/tri_vertex.h"
+namespace engine::fb {
+
class PixelFrameBuffer {
public:
PixelFrameBuffer(unsigned int w, unsigned int h);
@@ -14,7 +16,7 @@ class PixelFrameBuffer {
unsigned int height() const;
const uint32_t* pixels() const;
void clear();
- void draw_triangle(TriangleVertex4 triangle);
+ void draw_triangle(engine::o3d::TriangleVertex4 triangle);
private:
unsigned int w, h;
@@ -22,8 +24,10 @@ class PixelFrameBuffer {
std::vector<float> depth_buf;
int face_ind;
- void _draw_cropped_triangle(TriangleVertex3 triangle);
+ void _draw_cropped_triangle(engine::o3d::TriangleVertex3 triangle);
uint32_t face_color() const;
};
+}
+
#endif // FB_PIXFB_H
diff --git a/src/math/mat4.cpp b/src/math/mat4.cpp
index ece42d8..6418e9c 100644
--- a/src/math/mat4.cpp
+++ b/src/math/mat4.cpp
@@ -3,6 +3,8 @@
#include <cmath>
#include "math/math_vector.h"
+using namespace engine::math;
+
Mat4 Mat4::idty() {
return {
1.f, 0.f, 0.f, 0.f,
@@ -127,7 +129,7 @@ std::array<MathVector4, 4> Mat4::to_vecs() {
}};
}
-Mat4 operator*(float fac, Mat4 m) {
+Mat4 engine::math::operator*(float fac, Mat4 m) {
return {
fac * m.values[ 0], fac * m.values[ 1], fac * m.values[ 2], fac * m.values[ 3],
fac * m.values[ 4], fac * m.values[ 5], fac * m.values[ 6], fac * m.values[ 7],
diff --git a/src/math/mat4.h b/src/math/mat4.h
index 6df9d49..d8b66c4 100644
--- a/src/math/mat4.h
+++ b/src/math/mat4.h
@@ -4,6 +4,8 @@
#include <array>
#include "math/math_vector.h"
+namespace engine::math {
+
class Mat4 {
public:
static Mat4 idty();
@@ -27,4 +29,6 @@ class Mat4 {
Mat4 operator*(float fac, Mat4 m);
+}
+
#endif // MATH_MAT4_H
diff --git a/src/math/math_vector.cpp b/src/math/math_vector.cpp
index baa2448..aacff86 100644
--- a/src/math/math_vector.cpp
+++ b/src/math/math_vector.cpp
@@ -1,6 +1,8 @@
#include "math/math_vector.h"
#include <cmath>
+using namespace engine::math;
+
MathVector2::MathVector2() {
}
@@ -39,15 +41,15 @@ MathVector2 MathVector2::round() const {
return { std::round(x), std::round(y) };
}
-MathVector2 operator*(float n, MathVector2 other) {
+MathVector2 engine::math::operator*(float n, MathVector2 other) {
return { n * other.x, n * other.y };
}
-MathVector2 operator*(MathVector2 other, float n) {
+MathVector2 engine::math::operator*(MathVector2 other, float n) {
return n * other;
}
-MathVector2 operator/(MathVector2 other, float n) {
+MathVector2 engine::math::operator/(MathVector2 other, float n) {
return { other.x / n, other.y / n };
}
@@ -97,15 +99,15 @@ MathVector3 MathVector3::cross(MathVector3 other) const {
};
}
-MathVector3 operator*(float n, MathVector3 other) {
+MathVector3 engine::math::operator*(float n, MathVector3 other) {
return { n * other.x, n * other.y, n * other.z };
}
-MathVector3 operator*(MathVector3 other, float n) {
+MathVector3 engine::math::operator*(MathVector3 other, float n) {
return n * other;
}
-MathVector3 operator/(MathVector3 other, float n) {
+MathVector3 engine::math::operator/(MathVector3 other, float n) {
return { other.x / n, other.y / n, other.z / n };
}
@@ -160,14 +162,14 @@ MathVector3 MathVector4::div_by_w() const {
return xyz() / w;
}
-MathVector4 operator*(float n, MathVector4 other) {
+MathVector4 engine::math::operator*(float n, MathVector4 other) {
return { n * other.x, n * other.y, n * other.z, n * other.w };
}
-MathVector4 operator*(MathVector4 other, float n) {
+MathVector4 engine::math::operator*(MathVector4 other, float n) {
return n * other;
}
-MathVector4 operator/(MathVector4 other, float n) {
+MathVector4 engine::math::operator/(MathVector4 other, float n) {
return { other.x / n, other.y / n, other.z / n, other.w / n };
}
diff --git a/src/math/math_vector.h b/src/math/math_vector.h
index 668a3f3..28ae81e 100644
--- a/src/math/math_vector.h
+++ b/src/math/math_vector.h
@@ -1,6 +1,8 @@
#ifndef MATH_MATH_VECTOR_H
#define MATH_MATH_VECTOR_H
+namespace engine::math {
+
class MathVector2 {
public:
float x, y;
@@ -66,4 +68,6 @@ MathVector4 operator*(float n, MathVector4 other);
MathVector4 operator*(MathVector4 other, float n);
MathVector4 operator/(MathVector4 other, float n);
+}
+
#endif // MATH_MATH_VECTOR_H
diff --git a/src/o3d/obj3d.cpp b/src/o3d/obj3d.cpp
index 584e304..9ccb93a 100644
--- a/src/o3d/obj3d.cpp
+++ b/src/o3d/obj3d.cpp
@@ -1,9 +1,12 @@
#include "o3d/obj3d.h"
#include <vector>
#include <array>
+#include "math/math_vector.h"
#include "o3d/vertex.h"
#include "o3d/tri_vertex.h"
+using namespace engine::o3d;
+
Object3D::TriangleVertex3Iterator::TriangleVertex3Iterator(const Object3D* obj, int face_ind) : obj{obj}, face_ind{face_ind} {
}
@@ -37,14 +40,14 @@ Object3D::TriangleVertex3Iterator::reference Object3D::TriangleVertex3Iterator::
Object3D Object3D::cube() {
return {
{
- { MathVector3(-1.f, -1.f, -1.f), {} },
- { MathVector3(+1.f, -1.f, -1.f), {} },
- { MathVector3(-1.f, +1.f, -1.f), {} },
- { MathVector3(+1.f, +1.f, -1.f), {} },
- { MathVector3(-1.f, -1.f, +1.f), {} },
- { MathVector3(+1.f, -1.f, +1.f), {} },
- { MathVector3(-1.f, +1.f, +1.f), {} },
- { MathVector3(+1.f, +1.f, +1.f), {} },
+ { engine::math::MathVector3(-1.f, -1.f, -1.f), {} },
+ { engine::math::MathVector3(+1.f, -1.f, -1.f), {} },
+ { engine::math::MathVector3(-1.f, +1.f, -1.f), {} },
+ { engine::math::MathVector3(+1.f, +1.f, -1.f), {} },
+ { engine::math::MathVector3(-1.f, -1.f, +1.f), {} },
+ { engine::math::MathVector3(+1.f, -1.f, +1.f), {} },
+ { engine::math::MathVector3(-1.f, +1.f, +1.f), {} },
+ { engine::math::MathVector3(+1.f, +1.f, +1.f), {} },
},
{
{ 0, 2, 3 }, { 0, 3, 1 }, // face 1
diff --git a/src/o3d/obj3d.h b/src/o3d/obj3d.h
index c65a06b..795b337 100644
--- a/src/o3d/obj3d.h
+++ b/src/o3d/obj3d.h
@@ -7,6 +7,8 @@
#include "o3d/vertex.h"
#include "o3d/tri_vertex.h"
+namespace engine::o3d {
+
class Object3D {
public:
class TriangleVertex3Iterator {
@@ -41,4 +43,6 @@ class Object3D {
std::vector<std::array<int, 3>> faces;
};
+}
+
#endif // O3D_OBJ3D_H
diff --git a/src/o3d/tri_vertex.cpp b/src/o3d/tri_vertex.cpp
index 7b6b379..07185f6 100644
--- a/src/o3d/tri_vertex.cpp
+++ b/src/o3d/tri_vertex.cpp
@@ -5,6 +5,8 @@
#include "o3d/vertex.h"
#include "o3d/tri_vertex.h"
+using namespace engine::o3d;
+
TriangleVertex3::TriangleVertex3(Vertex3 vertex1, Vertex3 vertex2, Vertex3 vertex3) : vertex1{vertex1}, vertex2{vertex2}, vertex3{vertex3} {
}
@@ -46,7 +48,7 @@ static void _crop_x(std::vector<TriangleVertex3>& tris, TriangleVertex3 t, int n
q3 = &t.vertex1;
break;
}
- MathVector3 dq2 = q1->point - q2->point;
+ engine::math::MathVector3 dq2 = q1->point - q2->point;
float fac2 = (x - q2->point.x) / dq2.x;
Vertex3 r2{
{
@@ -56,7 +58,7 @@ static void _crop_x(std::vector<TriangleVertex3>& tris, TriangleVertex3 t, int n
},
VertexData::lerp(q2->data, q1->data, fac2)
};
- MathVector3 dq3 = q1->point - q3->point;
+ engine::math::MathVector3 dq3 = q1->point - q3->point;
float fac3 = (x - q3->point.x) / dq3.x;
Vertex3 r3{
{
@@ -131,7 +133,7 @@ static void _crop_y(std::vector<TriangleVertex3>& tris, TriangleVertex3 t, int n
q3 = &t.vertex1;
break;
}
- MathVector3 dq2 = q1->point - q2->point;
+ engine::math::MathVector3 dq2 = q1->point - q2->point;
float fac2 = (y - q2->point.y) / dq2.y;
Vertex3 r2{
{
@@ -141,7 +143,7 @@ static void _crop_y(std::vector<TriangleVertex3>& tris, TriangleVertex3 t, int n
},
VertexData::lerp(q2->data, q1->data, fac2)
};
- MathVector3 dq3 = q1->point - q3->point;
+ engine::math::MathVector3 dq3 = q1->point - q3->point;
float fac3 = (y - q3->point.y) / dq3.y;
Vertex3 r3{
{
@@ -245,7 +247,7 @@ static void _crop_z(std::vector<TriangleVertex4>& tris, TriangleVertex4 t, int n
q3 = &t.vertex1;
break;
}
- MathVector4 dq2 = q1->point - q2->point;
+ engine::math::MathVector4 dq2 = q1->point - q2->point;
float fac2 = (z - q2->point.z) / dq2.z;
Vertex4 r2{
{
@@ -256,7 +258,7 @@ static void _crop_z(std::vector<TriangleVertex4>& tris, TriangleVertex4 t, int n
},
VertexData::lerp(q2->data, q1->data, fac2)
};
- MathVector4 dq3 = q1->point - q3->point;
+ engine::math::MathVector4 dq3 = q1->point - q3->point;
float fac3 = (z - q3->point.z) / dq3.z;
Vertex4 r3{
{
diff --git a/src/o3d/tri_vertex.h b/src/o3d/tri_vertex.h
index 91e20f5..368a5e0 100644
--- a/src/o3d/tri_vertex.h
+++ b/src/o3d/tri_vertex.h
@@ -4,6 +4,8 @@
#include "o3d/vertex.h"
#include <vector>
+namespace engine::o3d {
+
class TriangleVertex3 {
public:
Vertex3 vertex1;
@@ -26,4 +28,6 @@ class TriangleVertex4 {
TriangleVertex3 div_by_w() const;
};
+}
+
#endif // O3D_TRI_VERTEX_H
diff --git a/src/o3d/vertex.cpp b/src/o3d/vertex.cpp
index c3bddef..4da6aa4 100644
--- a/src/o3d/vertex.cpp
+++ b/src/o3d/vertex.cpp
@@ -2,10 +2,12 @@
#include "math/math_vector.h"
#include "o3d/vertex_data.h"
-Vertex3::Vertex3(MathVector3 point, VertexData data) : point{point}, data{data} {
+using namespace engine::o3d;
+
+Vertex3::Vertex3(engine::math::MathVector3 point, VertexData data) : point{point}, data{data} {
}
-Vertex4::Vertex4(MathVector4 point, VertexData data) : point{point}, data{data} {
+Vertex4::Vertex4(engine::math::MathVector4 point, VertexData data) : point{point}, data{data} {
}
Vertex4::Vertex4(Vertex3 vertex) : point{vertex.point}, data{vertex.data} {
diff --git a/src/o3d/vertex.h b/src/o3d/vertex.h
index 27ca56a..66d073d 100644
--- a/src/o3d/vertex.h
+++ b/src/o3d/vertex.h
@@ -4,22 +4,26 @@
#include "math/math_vector.h"
#include "o3d/vertex_data.h"
+namespace engine::o3d {
+
class Vertex3 {
public:
- MathVector3 point;
+ engine::math::MathVector3 point;
VertexData data;
- Vertex3(MathVector3 point, VertexData data);
+ Vertex3(engine::math::MathVector3 point, VertexData data);
};
class Vertex4 {
public:
- MathVector4 point;
+ engine::math::MathVector4 point;
VertexData data;
- Vertex4(MathVector4 point, VertexData data);
+ Vertex4(engine::math::MathVector4 point, VertexData data);
Vertex4(Vertex3 vertex);
Vertex3 div_by_w() const;
};
+}
+
#endif // O3D_VERTEX_H
diff --git a/src/o3d/vertex_data.cpp b/src/o3d/vertex_data.cpp
index 8d6f77b..0957742 100644
--- a/src/o3d/vertex_data.cpp
+++ b/src/o3d/vertex_data.cpp
@@ -1,5 +1,7 @@
#include "o3d/vertex_data.h"
+using namespace engine::o3d;
+
VertexData VertexData::lerp(VertexData& vd1, VertexData& vd2, float s) {
return {};
}
diff --git a/src/o3d/vertex_data.h b/src/o3d/vertex_data.h
index 0000dda..dda881b 100644
--- a/src/o3d/vertex_data.h
+++ b/src/o3d/vertex_data.h
@@ -1,6 +1,8 @@
#ifndef O3D_VERTEX_DATA_H
#define O3D_VERTEX_DATA_H
+namespace engine::o3d {
+
class VertexData {
public:
static VertexData lerp(VertexData& vd1, VertexData& vd2, float s);
@@ -9,4 +11,6 @@ class VertexData {
VertexData();
};
+}
+
#endif // O3D_VERTEX_DATA_H