render the front face of a cube

This commit is contained in:
Elizabeth Hunt 2023-11-28 13:58:18 -07:00
parent dbccd86622
commit 1b96b0211e
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
4 changed files with 27 additions and 30 deletions

View File

@ -10,9 +10,9 @@
class Scene { class Scene {
public: public:
usu::vector<std::shared_ptr<Renderable>> renderables; usu::vector<std::shared_ptr<Renderable>> renderables;
std::tuple<std::uint16_t, std::uint16_t> std::tuple<std::uint32_t, std::uint32_t>
viewport_dimension; // <width, height> viewport_dimension; // <width, height>
std::tuple<std::uint16_t, std::uint16_t> scene_dimension; std::tuple<std::uint32_t, std::uint32_t> scene_dimension;
VECTOR directional_light; VECTOR directional_light;
FIXED z_plane; FIXED z_plane;

View File

@ -5,13 +5,6 @@
#include "vector.hpp" #include "vector.hpp"
#include <tonc.h> #include <tonc.h>
class Box : public Renderable {
public:
virtual void render(std::shared_ptr<Scene> scene) {
scene->draw_line({0, 0}, {2 << FIX_SHIFT, 3 << FIX_SHIFT}, 1);
}
};
int main() { int main() {
// interrupt & mode 4 foo // interrupt & mode 4 foo
irq_init(NULL); irq_init(NULL);
@ -20,14 +13,19 @@ int main() {
palette::put_palette((std::uint16_t *)MEM_PAL); palette::put_palette((std::uint16_t *)MEM_PAL);
auto scene = std::make_shared<Scene>(); auto scene = std::make_shared<Scene>();
// auto cube = std::shared_ptr<Renderable>((Renderable *)new Cube());
// scene->renderables.add(cube);
auto box = std::shared_ptr<Renderable>((Renderable *)new Box());
scene->renderables.add(box); auto cube = std::shared_ptr<Mesh>((Mesh *)new Cube);
ModelInstance modelInstance(cube, int2fx(2), {0x0C7F, 0x0000, 0},
{int2fx(0), 0, int2fx(0)});
auto modelInstancePtr = std::shared_ptr<Renderable>(&modelInstance);
scene->renderables.add(modelInstancePtr);
while (1) { while (1) {
Scene::render(scene); Scene::render(scene);
vid_flip();
VBlankIntrWait(); VBlankIntrWait();
} }

View File

@ -9,11 +9,12 @@ ModelInstance::ModelInstance(std::shared_ptr<Mesh> mesh, FIXED scale,
VECTOR rotate(VECTOR v, VECTOR rot) { VECTOR rotate(VECTOR v, VECTOR rot) {
FIXED sin_theta_x, sin_theta_y, sin_theta_z; FIXED sin_theta_x, sin_theta_y, sin_theta_z;
FIXED cos_theta_x, cos_theta_y, cos_theta_z; FIXED cos_theta_x, cos_theta_y, cos_theta_z;
VECTOR res = {v.x, v.y, v.z}; VECTOR res = {v.x, v.y, v.z};
if (rot.x != 0) { if (rot.x != 0) {
sin_theta_x = lu_sin(rot.x) >> 4; sin_theta_x = float2fx(0.707); // lu_sin(rot.x) >> 4;
cos_theta_x = lu_cos(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.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); 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> scene_context) { void ModelInstance::render(std::shared_ptr<Scene> scene_context) {
usu::vector<VECTOR> transformed(m_mesh->vertices.size()); usu::vector<POINT> projected(m_mesh->vertices.size());
usu::vector<POINT> projected(transformed.size());
for (std::uint32_t i = 0; i < transformed.size(); i++) { for (std::uint32_t i = 0; i < projected.size(); i++) {
VECTOR rotated = rotate(m_mesh->vertices[i], m_rot); VECTOR transformed =
vec_add(&transformed[i], &rotated, &m_pos); 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) { for (const TRIANGLE triangle : m_mesh->triangles) {

View File

@ -2,7 +2,7 @@
Scene::Scene() { Scene::Scene() {
directional_light = {0, 0, -1}; directional_light = {0, 0, -1};
viewport_dimension = {2, 3}; viewport_dimension = {3, 2};
scene_dimension = {SCREEN_WIDTH / 2, SCREEN_HEIGHT}; scene_dimension = {SCREEN_WIDTH / 2, SCREEN_HEIGHT};
z_plane = int2fx(1); z_plane = int2fx(1);
@ -21,13 +21,11 @@ POINT Scene::project_2d(VECTOR vertex) {
} }
POINT Scene::viewport_to_scene(POINT p) { POINT Scene::viewport_to_scene(POINT p) {
FIXED x = fxmul( FIXED x = fxmul(p.x, int2fx(std::get<0>(scene_dimension) /
p.x, (std::get<0>(scene_dimension) / std::get<0>(viewport_dimension)) std::get<0>(viewport_dimension)));
<< FIX_SHIFT); FIXED y = fxmul(p.y, int2fx(std::get<1>(scene_dimension) /
FIXED y = fxmul( std::get<1>(viewport_dimension)));
p.y, (std::get<1>(scene_dimension) / std::get<1>(viewport_dimension)) return {x >> FIX_SHIFT, y >> FIX_SHIFT};
<< FIX_SHIFT);
return {x, y};
} }
void Scene::draw_line(POINT p0, POINT p1, std::uint8_t pal_idx) { 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; 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, bmp16_line(scene_p0.x, scene_p0.y, scene_p1.x, scene_p1.y, pixels, vid_page,
M4_WIDTH); SCREEN_WIDTH);
} }