examples/cube: unrot

This commit is contained in:
Zack Buhman 2025-05-15 20:04:34 -05:00
parent 32a294c209
commit 6eda2bba0a
3 changed files with 82 additions and 57 deletions

View File

@ -1,7 +1,7 @@
#include "io_registers.h" #include "io_registers.h"
#include "bits.h" #include "bits.h"
#include "models/cube.h" #include "model/cube.h"
#include "math/math.h" #include "math/math.h"
static const uint16_t face_colors[6] = { static const uint16_t face_colors[6] = {
@ -159,29 +159,31 @@ void main()
// the following vertices are a quadrilateral // the following vertices are a quadrilateral
io_registers.a.BEGIN_VTXS = BEGIN_VTXS__type__quadrilateral; io_registers.a.BEGIN_VTXS = BEGIN_VTXS__type__quadrilateral;
const union quadrilateral * quadrilateral = cube_Cube_quadrilateral;
// cube faces // cube faces
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
io_registers.a.COLOR = face_colors[i]; io_registers.a.COLOR = face_colors[i];
struct vertex_position * a = &cube_positions[cube_quadrilaterals[i].a.position]; const struct vertex_position * a = &cube_position[quadrilateral[i].a.position];
io_registers.a.VTX_10 = 0 io_registers.a.VTX_10 = 0
| VTX_10__z_coordinate(a->z) | VTX_10__z_coordinate(a->z)
| VTX_10__y_coordinate(a->y) | VTX_10__y_coordinate(a->y)
| VTX_10__x_coordinate(a->x); | VTX_10__x_coordinate(a->x);
struct vertex_position * b = &cube_positions[cube_quadrilaterals[i].b.position]; const struct vertex_position * b = &cube_position[quadrilateral[i].b.position];
io_registers.a.VTX_10 = 0 io_registers.a.VTX_10 = 0
| VTX_10__z_coordinate(b->z) | VTX_10__z_coordinate(b->z)
| VTX_10__y_coordinate(b->y) | VTX_10__y_coordinate(b->y)
| VTX_10__x_coordinate(b->x); | VTX_10__x_coordinate(b->x);
struct vertex_position * c = &cube_positions[cube_quadrilaterals[i].c.position]; const struct vertex_position * c = &cube_position[quadrilateral[i].c.position];
io_registers.a.VTX_10 = 0 io_registers.a.VTX_10 = 0
| VTX_10__z_coordinate(c->z) | VTX_10__z_coordinate(c->z)
| VTX_10__y_coordinate(c->y) | VTX_10__y_coordinate(c->y)
| VTX_10__x_coordinate(c->x); | VTX_10__x_coordinate(c->x);
struct vertex_position * d = &cube_positions[cube_quadrilaterals[i].d.position]; const struct vertex_position * d = &cube_position[quadrilateral[i].d.position];
io_registers.a.VTX_10 = 0 io_registers.a.VTX_10 = 0
| VTX_10__z_coordinate(d->z) | VTX_10__z_coordinate(d->z)
| VTX_10__y_coordinate(d->y) | VTX_10__y_coordinate(d->y)

View File

@ -1,7 +1,11 @@
#pragma once
#include <stddef.h>
#include "model.h" #include "model.h"
// .6 fixed-point // 3.6 fixed-point
struct vertex_position cube_positions[] = { const vertex_position cube_position[] = {
{64, 64, -64}, {64, 64, -64},
{64, -64, -64}, {64, -64, -64},
{64, 64, 64}, {64, 64, 64},
@ -12,16 +16,16 @@ struct vertex_position cube_positions[] = {
{-64, -64, 64}, {-64, -64, 64},
}; };
// .15 fixed-point // 1.14 fixed-point
struct vertex_texture cube_textures[] = { const vertex_texture cube_texture[] = {
{32768, 32768}, {16384, 16384},
{0, 32768}, {0, 16384},
{0, 0}, {0, 0},
{32768, 0}, {16384, 0},
}; };
// .9 fixed-point // 0.9 fixed-point
struct vertex_normal cube_normals[] = { const vertex_normal cube_normal[] = {
{0, 512, 0}, {0, 512, 0},
{0, 0, 512}, {0, 0, 512},
{-512, 0, 0}, {-512, 0, 0},
@ -30,42 +34,61 @@ struct vertex_normal cube_normals[] = {
{0, 0, -512}, {0, 0, -512},
}; };
struct quadrilateral cube_quadrilaterals[] = { const union quadrilateral cube_Cube_quadrilateral[] = {
{ { .v = {
{0, 0, 0}, {0, 0, 0},
{4, 1, 0}, {4, 1, 0},
{6, 2, 0}, {6, 2, 0},
{2, 3, 0}, {2, 3, 0},
}, }},
{ { .v = {
{3, 3, 1}, {3, 3, 1},
{2, 0, 1}, {2, 0, 1},
{6, 1, 1}, {6, 1, 1},
{7, 2, 1}, {7, 2, 1},
}, }},
{ { .v = {
{7, 2, 2}, {7, 2, 2},
{6, 1, 2}, {6, 1, 2},
{4, 0, 2}, {4, 0, 2},
{5, 3, 2}, {5, 3, 2},
}, }},
{ { .v = {
{5, 1, 3}, {5, 1, 3},
{1, 0, 3}, {1, 0, 3},
{3, 3, 3}, {3, 3, 3},
{7, 2, 3}, {7, 2, 3},
}, }},
{ { .v = {
{1, 3, 4}, {1, 3, 4},
{0, 0, 4}, {0, 0, 4},
{2, 1, 4}, {2, 1, 4},
{3, 2, 4}, {3, 2, 4},
}, }},
{ { .v = {
{5, 2, 5}, {5, 2, 5},
{4, 1, 5}, {4, 1, 5},
{0, 0, 5}, {0, 0, 5},
{1, 3, 5}, {1, 3, 5},
}, }},
}; };
const struct object cube_Cube = {
.triangle = NULL,
.quadrilateral = &cube_Cube_quadrilateral[0],
.triangle_count = 0,
.quadrilateral_count = 6,
.material = -1,
};
const struct object * cube_object_list[] = {
&cube_Cube,
};
const struct model cube_model = {
.position = &cube_position[0],
.texture = &cube_texture[0],
.normal = &cube_normal[0],
.object = &cube_object_list[0],
.object_count = 1,
};

View File

@ -3,45 +3,45 @@
#include <stdint.h> #include <stdint.h>
struct index_ptn { struct index_ptn {
uint16_t position; const uint16_t position;
uint16_t texture; const uint16_t texture;
uint16_t normal; const uint16_t normal;
}; };
union triangle { union triangle {
struct { struct {
struct index_ptn a; const struct index_ptn a;
struct index_ptn b; const struct index_ptn b;
struct index_ptn c; const struct index_ptn c;
}; };
struct index_ptn v[3]; const struct index_ptn v[3];
}; };
union quadrilateral { union quadrilateral {
struct { struct {
struct index_ptn a; const struct index_ptn a;
struct index_ptn b; const struct index_ptn b;
struct index_ptn c; const struct index_ptn c;
struct index_ptn d; const struct index_ptn d;
}; };
struct index_ptn v[4]; const struct index_ptn v[4];
}; };
struct vertex_position { // signed 4.6 fixed point struct vertex_position { // signed 4.6 fixed point
int16_t x; const int16_t x;
int16_t y; const int16_t y;
int16_t z; const int16_t z;
}; };
struct vertex_normal { // s.9 fixed point struct vertex_normal { // s.9 fixed point
int16_t x; const int16_t x;
int16_t y; const int16_t y;
int16_t z; const int16_t z;
}; };
struct vertex_texture { // s.15 fixed point struct vertex_texture { // s.15 fixed point
int16_t u; const int16_t u;
int16_t v; const int16_t v;
}; };
typedef struct vertex_position vertex_position; typedef struct vertex_position vertex_position;
@ -49,18 +49,18 @@ typedef struct vertex_normal vertex_normal;
typedef struct vertex_texture vertex_texture; typedef struct vertex_texture vertex_texture;
struct object { struct object {
union triangle * triangle; const union triangle * triangle;
union quadrilateral * quadrilateral; const union quadrilateral * quadrilateral;
int triangle_count; const int triangle_count;
int quadrilateral_count; const int quadrilateral_count;
int material; const int material;
}; };
struct model { struct model {
vertex_position * position; const vertex_position * position;
vertex_texture * texture; const vertex_texture * texture;
vertex_normal * normal; const vertex_normal * normal;
struct object ** object; const struct object ** object;
int object_count; const int object_count;
}; };