aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/engine.cpp48
-rw-r--r--src/o3d/camera.cpp7
-rw-r--r--src/o3d/camera.h21
-rw-r--r--src/o3d/mesh.cpp33
-rw-r--r--src/o3d/mesh.h23
-rw-r--r--src/o3d/obj3d.cpp69
-rw-r--r--src/o3d/obj3d.h44
-rw-r--r--src/o3d/scene.cpp12
-rw-r--r--src/o3d/scene.h20
-rw-r--r--src/o3d/vertex_data.cpp8
10 files changed, 168 insertions, 117 deletions
diff --git a/src/engine.cpp b/src/engine.cpp
index 112d230..145c1f9 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -18,10 +18,13 @@
#include "fb/chfb.h"
#include "fb/pixfb.h"
+#include "o3d/scene.h"
+#include "o3d/mesh.h"
#include "o3d/obj3d.h"
#include "o3d/vertex.h"
#include "o3d/vertex_data.h"
#include "o3d/tri_vertex.h"
+#include "o3d/camera.h"
#include "math/math_vector.h"
#include "math/mat4.h"
@@ -49,22 +52,36 @@ static void usage_error_exit() {
template <class FB>
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<engine::o3d::Object3D, 2> objs{{ engine::o3d::Object3D::cube(), engine::o3d::Object3D::cube() }};
+ engine::o3d::Scene scene{
+ {{0.f, 0.f, rad * dist}, 0.f, 0.f, 0.f}, // camera
+ { // objects
+ engine::o3d::Object3D{
+ engine::o3d::Mesh::cube(),
+ -rad * engine::math::MathVector3(.5f, .5f, .5f),
+ rad, 0.f, 0.f, 0.f
+ },
+ engine::o3d::Object3D{
+ engine::o3d::Mesh::cube(),
+ +rad * engine::math::MathVector3(.5f, .5f, .5f),
+ rad, 0.f, 0.f, 0.f
+ },
+ }
+ };
auto scale_mat = engine::math::Mat4::scale(rad);
while (cont) {
- a.x += .0050f;
- a.y += .0065f;
- a.z += .0080f;
+ scene.camera.rot_x += .0050f;
+ scene.camera.rot_y += .0065f;
+ scene.camera.rot_z += .0080f;
fb.clear();
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);
+ engine::math::Mat4::translate(-scene.camera.loc)
+ * engine::math::Mat4::rot_x(-scene.camera.rot_x)
+ * engine::math::Mat4::rot_y(-scene.camera.rot_y)
+ * engine::math::Mat4::rot_z(-scene.camera.rot_z);
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,
@@ -72,15 +89,12 @@ static void scene_main(FB& fb, std::function<bool()> update_frame) {
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]) {
- engine::o3d::TriangleVertex4 t{triangle};
-
- t.vertex1.point = final_mat * t.vertex1.point;
- t.vertex2.point = final_mat * t.vertex2.point;
- t.vertex3.point = final_mat * t.vertex3.point;
-
- fb.draw_triangle(t);
- }
+ const auto& mesh = scene.objs[i].mesh;
+ std::vector<engine::o3d::Vertex4> pts;
+ for (const auto& vert : mesh.pts)
+ pts.push_back({ final_mat * engine::math::MathVector4{vert.point}, vert.data });
+ for (auto face : mesh.faces)
+ fb.draw_triangle({pts[face[0]], pts[face[1]], pts[face[2]]});
}
cont = update_frame();
}
diff --git a/src/o3d/camera.cpp b/src/o3d/camera.cpp
new file mode 100644
index 0000000..9ad2ac7
--- /dev/null
+++ b/src/o3d/camera.cpp
@@ -0,0 +1,7 @@
+#include "o3d/camera.h"
+#include "math/math_vector.h"
+
+using namespace engine::o3d;
+
+Camera::Camera(engine::math::MathVector3 loc, float rot_x, float rot_y, float rot_z) : loc{loc}, rot_x{rot_x}, rot_y{rot_y}, rot_z{rot_z} {
+}
diff --git a/src/o3d/camera.h b/src/o3d/camera.h
new file mode 100644
index 0000000..1039f86
--- /dev/null
+++ b/src/o3d/camera.h
@@ -0,0 +1,21 @@
+#ifndef O3D_CAMERA_H
+#define O3D_CAMERA_H
+
+#include "math/math_vector.h"
+
+namespace engine::o3d {
+
+class Scene;
+
+class Camera {
+ public:
+ Scene* scene = nullptr;
+ engine::math::MathVector3 loc;
+ float rot_x, rot_y, rot_z; // TODO: replace by quaternions
+
+ Camera(engine::math::MathVector3 loc, float rot_x, float rot_y, float rot_z);
+};
+
+}
+
+#endif // O3D_CAMERA_H
diff --git a/src/o3d/mesh.cpp b/src/o3d/mesh.cpp
new file mode 100644
index 0000000..7da77aa
--- /dev/null
+++ b/src/o3d/mesh.cpp
@@ -0,0 +1,33 @@
+#include "o3d/mesh.h"
+#include <vector>
+#include <array>
+#include "math/math_vector.h"
+#include "o3d/vertex.h"
+
+using namespace engine::o3d;
+
+Mesh Mesh::cube() {
+ return {
+ {
+ { 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
+ { 0, 4, 6 }, { 0, 6, 2 }, // face 2
+ { 0, 1, 5 }, { 0, 5, 4 }, // face 3
+ { 7, 6, 4 }, { 7, 4, 5 }, // face 4
+ { 7, 3, 2 }, { 7, 2, 6 }, // face 5
+ { 7, 5, 1 }, { 7, 1, 3 }, // face 6
+ }
+ };
+}
+
+Mesh::Mesh(std::vector<Vertex3> pts, std::vector<std::array<int, 3>> faces) : pts{pts}, faces{faces} {
+}
diff --git a/src/o3d/mesh.h b/src/o3d/mesh.h
new file mode 100644
index 0000000..579c8b2
--- /dev/null
+++ b/src/o3d/mesh.h
@@ -0,0 +1,23 @@
+#ifndef O3D_MESH_H
+#define O3D_MESH_H
+
+#include <vector>
+#include <array>
+#include <iterator>
+#include "o3d/vertex.h"
+
+namespace engine::o3d {
+
+class Mesh {
+ public:
+ static Mesh cube(); // this function should not be in this file
+
+ std::vector<Vertex3> pts;
+ std::vector<std::array<int, 3>> faces;
+
+ Mesh(std::vector<Vertex3> pts, std::vector<std::array<int, 3>> faces);
+};
+
+}
+
+#endif // O3D_MESH_H
diff --git a/src/o3d/obj3d.cpp b/src/o3d/obj3d.cpp
index 9ccb93a..047bf3e 100644
--- a/src/o3d/obj3d.cpp
+++ b/src/o3d/obj3d.cpp
@@ -1,72 +1,9 @@
#include "o3d/obj3d.h"
-#include <vector>
-#include <array>
#include "math/math_vector.h"
-#include "o3d/vertex.h"
-#include "o3d/tri_vertex.h"
+#include "o3d/mesh.h"
using namespace engine::o3d;
-Object3D::TriangleVertex3Iterator::TriangleVertex3Iterator(const Object3D* obj, int face_ind) : obj{obj}, face_ind{face_ind} {
-}
-
-Object3D::TriangleVertex3Iterator& Object3D::TriangleVertex3Iterator::operator++() {
- face_ind++;
- return *this;
-}
-
-Object3D::TriangleVertex3Iterator Object3D::TriangleVertex3Iterator::operator++(int) {
- TriangleVertex3Iterator retval = *this;
- ++(*this);
- return retval;
-}
-
-bool Object3D::TriangleVertex3Iterator::operator==(TriangleVertex3Iterator other) const {
- return obj == other.obj && face_ind == other.face_ind;
-}
-
-bool Object3D::TriangleVertex3Iterator::operator!=(TriangleVertex3Iterator other) const {
- return !(*this == other);
-}
-
-Object3D::TriangleVertex3Iterator::reference Object3D::TriangleVertex3Iterator::operator*() const {
- return {
- obj->pts[obj->faces[face_ind][0]],
- obj->pts[obj->faces[face_ind][1]],
- obj->pts[obj->faces[face_ind][2]]
- };
-}
-
-Object3D Object3D::cube() {
- return {
- {
- { 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
- { 0, 4, 6 }, { 0, 6, 2 }, // face 2
- { 0, 1, 5 }, { 0, 5, 4 }, // face 3
- { 7, 6, 4 }, { 7, 4, 5 }, // face 4
- { 7, 3, 2 }, { 7, 2, 6 }, // face 5
- { 7, 5, 1 }, { 7, 1, 3 }, // face 6
- }
- };
-}
-
-Object3D::Object3D(std::vector<Vertex3> pts, std::vector<std::array<int, 3>> faces) : pts{pts}, faces{faces} {
-}
-
-Object3D::TriangleVertex3Iterator Object3D::begin() {
- return Object3D::TriangleVertex3Iterator{this};
-}
-
-Object3D::TriangleVertex3Iterator Object3D::end() {
- return Object3D::TriangleVertex3Iterator{this, static_cast<int>(faces.size())};
+Object3D::Object3D(Mesh mesh, engine::math::MathVector3 loc, float scale, float rot_x, float rot_y, float rot_z)
+ : mesh{mesh}, loc{loc}, scale{scale}, rot_x{rot_x}, rot_y{rot_y}, rot_z{rot_z} {
}
diff --git a/src/o3d/obj3d.h b/src/o3d/obj3d.h
index 795b337..3a931b1 100644
--- a/src/o3d/obj3d.h
+++ b/src/o3d/obj3d.h
@@ -1,46 +1,22 @@
#ifndef O3D_OBJ3D_H
#define O3D_OBJ3D_H
-#include <vector>
-#include <array>
-#include <iterator>
-#include "o3d/vertex.h"
-#include "o3d/tri_vertex.h"
+#include "math/math_vector.h"
+#include "o3d/mesh.h"
namespace engine::o3d {
+class Scene;
+
class Object3D {
public:
- class TriangleVertex3Iterator {
- public:
- using iterator_category = std::input_iterator_tag;
- using value_type = TriangleVertex3;
- using difference_type = TriangleVertex3;
- using pointer = const TriangleVertex3*;
- using reference = TriangleVertex3;
-
- explicit TriangleVertex3Iterator(const Object3D* obj, int face_ind = 0);
- TriangleVertex3Iterator& operator++();
- TriangleVertex3Iterator operator++(int);
- bool operator==(TriangleVertex3Iterator other) const;
- bool operator!=(TriangleVertex3Iterator other) const;
- reference operator*() const;
-
- private:
- const Object3D* obj;
- int face_ind;
-
- };
-
- static Object3D cube(); // this function should not be in this file
-
- Object3D(std::vector<Vertex3> pts, std::vector<std::array<int, 3>> faces);
- TriangleVertex3Iterator begin();
- TriangleVertex3Iterator end();
+ Scene* scene = nullptr;
+ Mesh mesh;
+ engine::math::MathVector3 loc;
+ float scale;
+ float rot_x, rot_y, rot_z;
- private:
- std::vector<Vertex3> pts;
- std::vector<std::array<int, 3>> faces;
+ Object3D(Mesh mesh, engine::math::MathVector3 loc, float scale, float rot_x, float rot_y, float rot_z);
};
}
diff --git a/src/o3d/scene.cpp b/src/o3d/scene.cpp
new file mode 100644
index 0000000..aba25dc
--- /dev/null
+++ b/src/o3d/scene.cpp
@@ -0,0 +1,12 @@
+#include "o3d/scene.h"
+#include <vector>
+#include "o3d/camera.h"
+#include "o3d/obj3d.h"
+
+using namespace engine::o3d;
+
+Scene::Scene(Camera camera, std::vector<Object3D> objs) : camera{camera}, objs{objs} {
+ this.camera.scene = this;
+ for (auto& obj : this.objs)
+ obj.scene = this;
+}
diff --git a/src/o3d/scene.h b/src/o3d/scene.h
new file mode 100644
index 0000000..3759cfb
--- /dev/null
+++ b/src/o3d/scene.h
@@ -0,0 +1,20 @@
+#ifndef O3D_SCENE_H
+#define O3D_SCENE_H
+
+#include <vector>
+#include "o3d/obj3d.h"
+#include "o3d/camera.h"
+
+namespace engine::o3d {
+
+class Scene {
+ public:
+ Camera camera;
+ std::vector<Object3D> objs;
+
+ Scene(Camera camera, std::vector<Object3D> objs);
+};
+
+}
+
+#endif // O3D_SCENE_H
diff --git a/src/o3d/vertex_data.cpp b/src/o3d/vertex_data.cpp
index 0957742..4e0ab04 100644
--- a/src/o3d/vertex_data.cpp
+++ b/src/o3d/vertex_data.cpp
@@ -3,10 +3,18 @@
using namespace engine::o3d;
VertexData VertexData::lerp(VertexData& vd1, VertexData& vd2, float s) {
+ (void) vd1;
+ (void) vd2;
+ (void) s;
return {};
}
VertexData VertexData::bilerp(VertexData& vd1, VertexData& vd2, VertexData& vd3, float s, float t) {
+ (void) vd1;
+ (void) vd2;
+ (void) vd3;
+ (void) s;
+ (void) t;
return {};
}