From e730e8bdc69ef89dfa28d2606d260cb8b72f4740 Mon Sep 17 00:00:00 2001 From: vimene Date: Sun, 26 Nov 2023 17:58:30 +0100 Subject: added scenes, camera and meshes --- src/engine.cpp | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) (limited to 'src/engine.cpp') 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 static void scene_main(FB& fb, std::function 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 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 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 update_frame) { auto projection_mat = engine::math::Mat4::projection(static_cast(fb.height()) / static_cast(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 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(); } -- cgit v1.2.3