diff options
Diffstat (limited to 'src/engine.cpp')
-rw-r--r-- | src/engine.cpp | 48 |
1 files changed, 31 insertions, 17 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(); } |