This commit is contained in:
Zack Buhman 2025-04-07 17:37:59 -05:00
parent 3c9abcc88c
commit 35effc6f86
14 changed files with 51031 additions and 1 deletions

View File

@ -1,4 +1,4 @@
CFLAGS = -Isaturn CFLAGS = -Isaturn -I. -D__saturn__
OPT ?= -O2 OPT ?= -O2
LIB = ./saturn LIB = ./saturn
@ -59,6 +59,8 @@ vdp2/line_color_screen.elf: vdp2/line_color_screen.o $(LIBGCC)
vdp1/polygon.elf: vdp1/polygon.o vdp1/polygon.elf: vdp1/polygon.o
vdp1/cube.elf: vdp1/cube.o $(LIBGCC) vdp1/cube.elf: vdp1/cube.o $(LIBGCC)
vdp1/cube2.elf: vdp1/cube2.o vdp1/cube2.elf: vdp1/cube2.o
vdp1/bear.elf: CFLAGS += -DUSE_SH2_DVSR
vdp1/bear.elf: vdp1/bear.o $(LIBGCC)
vdp1/normal_sprite.elf: vdp1/normal_sprite.o res/mai00.data.o res/mai.data.pal.o vdp1/normal_sprite.elf: vdp1/normal_sprite.o res/mai00.data.o res/mai.data.pal.o
vdp1/normal_sprite_color_bank.elf: vdp1/normal_sprite_color_bank.o res/mai00.data.o res/mai.data.pal.o vdp1/normal_sprite_color_bank.elf: vdp1/normal_sprite_color_bank.o res/mai00.data.o res/mai.data.pal.o

BIN
model/bear/bear.data Normal file

Binary file not shown.

15
model/bear/bear.data.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_model_bear_bear_data_start __asm("_binary_model_bear_bear_data_start");
extern uint32_t _binary_model_bear_bear_data_end __asm("_binary_model_bear_bear_data_end");
extern uint32_t _binary_model_bear_bear_data_size __asm("_binary_model_bear_bear_data_size");
#ifdef __cplusplus
}
#endif

22
model/bear/bear.mtl Normal file
View File

@ -0,0 +1,22 @@
# Blender 4.3.2 MTL File: 'bear.blend'
# www.blender.org
newmtl matBear
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 1
newmtl matOutline
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.000000 0.000000 0.000000
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 1

3407
model/bear/bear0000.obj Normal file

File diff suppressed because it is too large Load Diff

3407
model/bear/bear0005.obj Normal file

File diff suppressed because it is too large Load Diff

3407
model/bear/bear0010.obj Normal file

File diff suppressed because it is too large Load Diff

3407
model/bear/bear0015.obj Normal file

File diff suppressed because it is too large Load Diff

3407
model/bear/bear0020.obj Normal file

File diff suppressed because it is too large Load Diff

13
model/bear/material.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include <stdint.h>
#include "model/material.h"
enum material {
bear_matBear,
bear_matOutline,
};
const struct material_descriptor bear_material[] = {
};

33616
model/bear/model.h Normal file

File diff suppressed because it is too large Load Diff

25
model/material.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <stdint.h>
struct pixel_descriptor {
uint8_t * start;
int32_t size;
int32_t vram_offset; // offset into vram texture address space
int16_t width;
int16_t height;
};
struct palette_descriptor {
uint8_t * start;
int32_t size;
int32_t vram_offset; // offset into vram palette address space
int32_t palette_size;
};
struct material_descriptor {
struct pixel_descriptor pixel;
struct palette_descriptor palette;
};

59
model/model.h Normal file
View File

@ -0,0 +1,59 @@
#pragma once
#include <stdint.h>
#include "math/vec3.hpp"
#include "math/vec2.hpp"
#ifdef __dreamcast__
using vertex_position = vec<3, float>;
using vertex_normal = vec<3, float>;
using vertex_texture = vec<2, float>;
#endif
#ifdef __saturn__
#include "math/fp.hpp"
using vertex_position = vec<3, fp16_16>;
using vertex_normal = vec<3, fp16_16>;
using vertex_texture = vec<2, fp16_16>;
#endif
struct index_ptn {
uint16_t position;
uint16_t texture;
uint16_t normal;
};
union triangle {
struct {
struct index_ptn a;
struct index_ptn b;
struct index_ptn c;
};
struct index_ptn v[3];
};
union quadrilateral {
struct {
struct index_ptn a;
struct index_ptn b;
struct index_ptn c;
struct index_ptn d;
};
struct index_ptn v[4];
};
struct object {
const union triangle * triangle;
const union quadrilateral * quadrilateral;
const int triangle_count;
const int quadrilateral_count;
const int material;
};
struct model {
const vertex_position * position;
const vertex_texture * texture;
const vertex_normal * normal;
const struct object ** object;
const int object_count;
};

243
vdp1/bear.cpp Normal file
View File

@ -0,0 +1,243 @@
#include <stdint.h>
#include "vdp2.h"
#include "vdp1.h"
#include <concepts>
#include "common/vdp2_func.hpp"
#include "math/fp.hpp"
#include "math/vec3.hpp"
#include "math/mat4x4.hpp"
#include "cos.hpp"
#include "model/model.h"
#include "model/bear/material.h"
#include "model/bear/model.h"
// |--
// |
// |
using vec3 = vec<3, fp16_16>;
using mat4x4 = mat<4, 4, fp16_16>;
struct canvas {
fp16_16 width;
fp16_16 height;
};
constexpr struct canvas canvas = { 240, 240 };
template <typename T>
vec<3, T> viewport_to_canvas(T x, T y)
{
return vec<3, T>(x * canvas.width, y * canvas.height, T(1));
}
template <typename T>
inline constexpr vec<3, T> project_vertex(vec<3, T> const& v)
{
// / (v.z - T(5))
// / (v.z - T(5))
return viewport_to_canvas<T>((v.x * T(0.5) + T(2.0/3.0)),
(v.y * T(0.5) + T(0.5)));
}
constexpr inline uint16_t rgb15(int32_t r, int32_t g, int32_t b)
{
return ((b & 31) << 10) | ((g & 31) << 5) | ((r & 31) << 0);
}
constexpr uint16_t colors[] = {
rgb15(31, 0, 0), // red
rgb15( 0, 31, 0), // green
rgb15( 0, 0, 31), // blue
rgb15(31, 0, 31), // magenta
rgb15( 0, 31, 31), // cyan
rgb15(31, 31, 0), // yellow
};
static int32_t tick = 0;
static inline void render_quad(int ix, const vec3 a, const vec3 b, const vec3 c, const vec3 d)
{
vdp1.vram.cmd[ix].CTRL = CTRL__JP__JUMP_NEXT | CTRL__COMM__POLYGON;
vdp1.vram.cmd[ix].LINK = 0;
vdp1.vram.cmd[ix].PMOD = PMOD__ECD | PMOD__SPD;
vdp1.vram.cmd[ix].COLR = COLR__RGB | colors[ix & 3];
vdp1.vram.cmd[ix].A.X = static_cast<int>(a.x);
vdp1.vram.cmd[ix].A.Y = static_cast<int>(a.y);
vdp1.vram.cmd[ix].B.X = static_cast<int>(b.x);
vdp1.vram.cmd[ix].B.Y = static_cast<int>(b.y);
vdp1.vram.cmd[ix].C.X = static_cast<int>(c.x);
vdp1.vram.cmd[ix].C.Y = static_cast<int>(c.y);
vdp1.vram.cmd[ix].D.X = static_cast<int>(d.x);
vdp1.vram.cmd[ix].D.Y = static_cast<int>(d.y);
}
int render_object(int ix,
const mat4x4& screen,
const struct model * model,
const struct object * object0)
{
mat4x4 trans = screen;
for (int i = 0; i < object0->quadrilateral_count; i++) {
const union quadrilateral * quad0 = &object0->quadrilateral[i];
vec3 a0 = model->position[quad0->v[0].position];
vec3 b0 = model->position[quad0->v[1].position];
vec3 c0 = model->position[quad0->v[2].position];
vec3 d0 = model->position[quad0->v[3].position];
vec3 a = trans * a0;
vec3 b = trans * b0;
vec3 c = trans * c0;
vec3 d = trans * d0;
vec3 an0 = trans * model->normal[quad0->v[0].normal];
//vec3 l = {0, 0, 1};
fp16_16 cull = dot(a, an0);
if (cull.value > 0) {
// `origin` above is p==0 below; the `origin` calculation could
// be reused, though it would hurt readability slightly
/*
for (int p = 0; p < 4; p++) {
const vec3& v0 = vertices[face[p]];
// rotation
const vec3 v1 = transform * v0;
// translation
const vec3 v2{v1.x, v1.y + fp16_16(1), v1.z + fp16_16(2)};
const vec3 v3 = project_vertex(v2 / v2.z);
}
*/
render_quad(ix,
project_vertex(a / a.z),
project_vertex(b / b.z),
project_vertex(c / c.z),
project_vertex(d / d.z));
ix++;
}
}
return ix;
}
void render()
{
tick++;
int ix = 2;
const mat4x4 scale {
1, 0, 0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
};
const int rx = tick >> 2;
const mat4x4 rotationX {
1, 0, 0, 0,
0, cos(rx), sin(rx), 0,
0, -sin(rx), cos(rx), 0,
0, 0, 0, 1,
};
const int ry = tick >> 1;
const mat4x4 rotationY {
cos(ry), 0, -sin(ry), 0,
0, 1, 0, 0,
sin(ry), 0, cos(ry), 0,
0, 0, 0, 1,
};
const mat4x4 translation = {
1, 0, 0, 0,
0, 1, 0, 4.5,
0, 0, 1, 6.5,
0, 0, 0, 1,
};
const mat4x4 screen = translation * rotationY * scale;
//const vec3 camera = {0, 0, 0};
const int frame_ix0 = 0;
const struct model * model = &bear_model;
const struct object * object0 = bear_object[frame_ix0 * 2];
//const struct object * object1 = bear_object[frame_ix1 * 2];
ix = render_object(ix, screen, model, object0);
vdp1.vram.cmd[ix].CTRL = CTRL__END;
}
void main()
{
v_blank_in();
// DISP: Please make sure to change this bit from 0 to 1 during V blank.
vdp2.reg.TVMD = ( TVMD__DISP | TVMD__LSMD__NON_INTERLACE
| TVMD__VRESO__240 | TVMD__HRESO__NORMAL_320);
// disable all VDP2 backgrounds (e.g: the Sega bios logo)
vdp2.reg.BGON = 0;
// VDP2 User's Manual:
// "When sprite data is in an RGB format, sprite register 0 is selected"
// "When the value of a priority number is 0h, it is read as transparent"
//
// The power-on value of PRISA is zero. Set the priority for sprite register 0
// to some number greater than zero, so that the color data is not interpreted
// as "transparent".
vdp2.reg.PRISA = PRISA__S0PRIN(1); // Sprite register 0 Priority Number
/* TVM settings must be performed from the second H-blank IN interrupt after the
V-blank IN interrupt to the H-blank IN interrupt immediately after the V-blank
OUT interrupt. */
// "normal" display resolution, 16 bits per pixel, 512x256 framebuffer
vdp1.reg.TVMR = TVMR__TVM__NORMAL;
// swap framebuffers every 1 cycle; non-interlace
vdp1.reg.FBCR = 0;
// during a framebuffer erase cycle, write the color "black" to each pixel
constexpr uint16_t black = 0x0000;
vdp1.reg.EWDR = black;
// the EWLR/EWRR macros use somewhat nontrivial math for the X coordinates
// erase upper-left coordinate
vdp1.reg.EWLR = EWLR__16BPP_X1(0) | EWLR__Y1(0);
// erase lower-right coordinate
vdp1.reg.EWRR = EWRR__16BPP_X3(319) | EWRR__Y3(239);
vdp1.vram.cmd[0].CTRL = CTRL__JP__JUMP_NEXT | CTRL__COMM__SYSTEM_CLIP_COORDINATES;
vdp1.vram.cmd[0].LINK = 0;
vdp1.vram.cmd[0].XC = 319;
vdp1.vram.cmd[0].YC = 239;
vdp1.vram.cmd[1].CTRL = CTRL__JP__JUMP_NEXT | CTRL__COMM__LOCAL_COORDINATE;
vdp1.vram.cmd[1].LINK = 0;
vdp1.vram.cmd[1].XA = 0;
vdp1.vram.cmd[1].YA = 0;
vdp1.vram.cmd[2].CTRL = CTRL__END;
// start drawing (execute the command list) on every frame
vdp1.reg.PTMR = PTMR__PTM__FRAME_CHANGE;
while (true) {
v_blank_in();
render();
}
}