diff --git a/inc/scene.hpp b/inc/scene.hpp index 05cdac0..6dd4c35 100644 --- a/inc/scene.hpp +++ b/inc/scene.hpp @@ -10,9 +10,9 @@ class Scene { public: usu::vector> renderables; - std::tuple + std::tuple viewport_dimension; // - std::tuple scene_dimension; + std::tuple scene_dimension; VECTOR directional_light; FIXED z_plane; diff --git a/src/main.cpp b/src/main.cpp index 0c91536..c08b584 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,13 +5,6 @@ #include "vector.hpp" #include -class Box : public Renderable { -public: - virtual void render(std::shared_ptr scene) { - scene->draw_line({0, 0}, {2 << FIX_SHIFT, 3 << FIX_SHIFT}, 1); - } -}; - int main() { // interrupt & mode 4 foo irq_init(NULL); @@ -20,14 +13,19 @@ int main() { palette::put_palette((std::uint16_t *)MEM_PAL); auto scene = std::make_shared(); - // auto cube = std::shared_ptr((Renderable *)new Cube()); - // scene->renderables.add(cube); - auto box = std::shared_ptr((Renderable *)new Box()); - scene->renderables.add(box); + auto cube = std::shared_ptr((Mesh *)new Cube); + + ModelInstance modelInstance(cube, int2fx(2), {0x0C7F, 0x0000, 0}, + {int2fx(0), 0, int2fx(0)}); + + auto modelInstancePtr = std::shared_ptr(&modelInstance); + scene->renderables.add(modelInstancePtr); while (1) { + Scene::render(scene); + vid_flip(); VBlankIntrWait(); } diff --git a/src/model_instance.cpp b/src/model_instance.cpp index 65ed6a8..61204ff 100644 --- a/src/model_instance.cpp +++ b/src/model_instance.cpp @@ -9,11 +9,12 @@ ModelInstance::ModelInstance(std::shared_ptr mesh, FIXED scale, VECTOR rotate(VECTOR v, VECTOR rot) { FIXED sin_theta_x, sin_theta_y, sin_theta_z; FIXED cos_theta_x, cos_theta_y, cos_theta_z; + VECTOR res = {v.x, v.y, v.z}; if (rot.x != 0) { - sin_theta_x = lu_sin(rot.x) >> 4; - cos_theta_x = lu_cos(rot.x) >> 4; + sin_theta_x = float2fx(0.707); // lu_sin(rot.x) >> 4; + cos_theta_x = float2fx(0.707); // lu_cos(rot.x) >> 4; res.y = fxmul(res.y, cos_theta_x) - fxmul(res.z, sin_theta_x); res.z = fxmul(res.z, cos_theta_x) + fxmul(res.y, sin_theta_x); } @@ -36,14 +37,14 @@ VECTOR rotate(VECTOR v, VECTOR rot) { } void ModelInstance::render(std::shared_ptr scene_context) { - usu::vector transformed(m_mesh->vertices.size()); - usu::vector projected(transformed.size()); + usu::vector projected(m_mesh->vertices.size()); - for (std::uint32_t i = 0; i < transformed.size(); i++) { - VECTOR rotated = rotate(m_mesh->vertices[i], m_rot); - vec_add(&transformed[i], &rotated, &m_pos); + for (std::uint32_t i = 0; i < projected.size(); i++) { + VECTOR transformed = + m_mesh->vertices[i]; // rotate(m_mesh->vertices[i], m_rot); + // vec_add(&transformed, &transformed, &m_pos); - projected[i] = scene_context->project_2d(m_mesh->vertices[i]); + projected[i] = scene_context->project_2d(transformed); } for (const TRIANGLE triangle : m_mesh->triangles) { diff --git a/src/scene.cpp b/src/scene.cpp index f69398d..44e7028 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -2,7 +2,7 @@ Scene::Scene() { directional_light = {0, 0, -1}; - viewport_dimension = {2, 3}; + viewport_dimension = {3, 2}; scene_dimension = {SCREEN_WIDTH / 2, SCREEN_HEIGHT}; z_plane = int2fx(1); @@ -21,13 +21,11 @@ POINT Scene::project_2d(VECTOR vertex) { } POINT Scene::viewport_to_scene(POINT p) { - FIXED x = fxmul( - p.x, (std::get<0>(scene_dimension) / std::get<0>(viewport_dimension)) - << FIX_SHIFT); - FIXED y = fxmul( - p.y, (std::get<1>(scene_dimension) / std::get<1>(viewport_dimension)) - << FIX_SHIFT); - return {x, y}; + FIXED x = fxmul(p.x, int2fx(std::get<0>(scene_dimension) / + std::get<0>(viewport_dimension))); + FIXED y = fxmul(p.y, int2fx(std::get<1>(scene_dimension) / + std::get<1>(viewport_dimension))); + return {x >> FIX_SHIFT, y >> FIX_SHIFT}; } void Scene::draw_line(POINT p0, POINT p1, std::uint8_t pal_idx) { @@ -36,5 +34,5 @@ void Scene::draw_line(POINT p0, POINT p1, std::uint8_t pal_idx) { std::uint16_t pixels = (pal_idx << 8) | pal_idx; bmp16_line(scene_p0.x, scene_p0.y, scene_p1.x, scene_p1.y, pixels, vid_page, - M4_WIDTH); + SCREEN_WIDTH); }