example/castle: render textures

This commit is contained in:
Zack Buhman 2025-02-18 23:24:37 -06:00
parent 39f496b52c
commit 94c12d690d
20 changed files with 604 additions and 203 deletions

View File

@ -29,6 +29,12 @@
#include "math/vec4.hpp" #include "math/vec4.hpp"
#include "math/mat4x4.hpp" #include "math/mat4x4.hpp"
#include "model/castle/castlest.data.h"
#include "model/castle/gothic3.data.h"
#include "model/castle/oldbric.data.h"
#include "model/castle/shingle.data.h"
#include "model/castle/stone2.data.h"
using vec2 = vec<2, float>; using vec2 = vec<2, float>;
using vec3 = vec<3, float>; using vec3 = vec<3, float>;
using vec4 = vec<4, float>; using vec4 = vec<4, float>;
@ -177,21 +183,22 @@ void interrupt_init()
: "r" (sr)); : "r" (sr));
} }
void global_polygon_type_0(ta_parameter_writer& writer, uint32_t texture_address, uint32_t list, uint32_t cull) void global_polygon_type_0(ta_parameter_writer& writer, uint32_t texture_address, int width, int height)
{ {
const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume
| list | para_control::list_type::opaque
| obj_control::col_type::packed_color | obj_control::col_type::packed_color
| obj_control::texture
; ;
const uint32_t isp_tsp_instruction_word = isp_tsp_instruction_word::depth_compare_mode::greater const uint32_t isp_tsp_instruction_word = isp_tsp_instruction_word::depth_compare_mode::greater
| cull; | isp_tsp_instruction_word::culling_mode::no_culling;
const uint32_t tsp_instruction_word = tsp_instruction_word::fog_control::no_fog const uint32_t tsp_instruction_word = tsp_instruction_word::fog_control::no_fog
| tsp_instruction_word::src_alpha_instr::one | tsp_instruction_word::src_alpha_instr::one
| tsp_instruction_word::dst_alpha_instr::zero | tsp_instruction_word::dst_alpha_instr::zero
| tsp_instruction_word::texture_u_size::from_int(256) | tsp_instruction_word::texture_u_size::from_int(width)
| tsp_instruction_word::texture_v_size::from_int(256) | tsp_instruction_word::texture_v_size::from_int(height)
; ;
const uint32_t texture_control_word = texture_control_word::pixel_format::_565 const uint32_t texture_control_word = texture_control_word::pixel_format::_565
@ -239,28 +246,34 @@ static inline void render_tri(ta_parameter_writer& writer,
uint32_t base_color, uint32_t base_color,
vec3 ap, vec3 ap,
vec3 bp, vec3 bp,
vec3 cp) vec3 cp,
vec2 at,
vec2 bt,
vec2 ct)
{ {
if (ap.z < 0 || bp.z < 0 || cp.z < 0) if (ap.z < 0 || bp.z < 0 || cp.z < 0)
return; return;
writer.append<ta_vertex_parameter::polygon_type_0>() = writer.append<ta_vertex_parameter::polygon_type_3>() =
ta_vertex_parameter::polygon_type_0(polygon_vertex_parameter_control_word(false), ta_vertex_parameter::polygon_type_3(polygon_vertex_parameter_control_word(false),
ap.x, ap.y, ap.z, ap.x, ap.y, ap.z,
base_color at.x, at.y,
); // offset_color base_color,
0); // offset_color
writer.append<ta_vertex_parameter::polygon_type_0>() = writer.append<ta_vertex_parameter::polygon_type_3>() =
ta_vertex_parameter::polygon_type_0(polygon_vertex_parameter_control_word(false), ta_vertex_parameter::polygon_type_3(polygon_vertex_parameter_control_word(false),
bp.x, bp.y, bp.z, bp.x, bp.y, bp.z,
base_color bt.x, bt.y,
); // offset_color base_color,
0); // offset_color
writer.append<ta_vertex_parameter::polygon_type_0>() = writer.append<ta_vertex_parameter::polygon_type_3>() =
ta_vertex_parameter::polygon_type_0(polygon_vertex_parameter_control_word(true), ta_vertex_parameter::polygon_type_3(polygon_vertex_parameter_control_word(true),
cp.x, cp.y, cp.z, cp.x, cp.y, cp.z,
base_color ct.x, ct.y,
); // offset_color base_color,
0); // offset_color
} }
constexpr inline mat4x4 screen_rotation(float theta) constexpr inline mat4x4 screen_rotation(float theta)
@ -307,34 +320,25 @@ uint32_t xorshift32()
return xorshift_state = x; return xorshift_state = x;
} }
const frame * frames[] = {
&frame_x3ds_Box08,
&frame_x3ds_Box09,
&frame_x3ds_Box02,
&frame_x3ds_Cone02,
&frame_x3ds_Cylinder02,
&frame_x3ds_Box21,
&frame_x3ds_Box22,
&frame_x3ds_Box23,
&frame_x3ds_Box24,
&frame_x3ds_Box25,
&frame_x3ds_Box26,
&frame_x3ds_Box27,
&frame_x3ds_Cylinder03,
&frame_x3ds_Cone03,
&frame_x3ds_Cylinder01,
&frame_x3ds_Box01,
&frame_x3ds_Box28,
&frame_x3ds_Box03,
&frame_x3ds_roof,
&frame_x3ds_walkway,
};
static inline void render_mesh(ta_parameter_writer& writer, static inline void render_mesh(ta_parameter_writer& writer,
const mat4x4& screen, const mat4x4& screen,
const mat4x4& model, const mat4x4& model,
const mesh * mesh) const mesh * mesh)
{ {
const material * m = &mesh->material_list->objects[0]->material;
assert(m->tag == tag::material);
const texture_filename * t = &m->objects[0]->texture_filename;
assert(t->tag == tag::texture_filename);
global_polygon_type_0(writer,
texture_memory_alloc.texture.start + t->texture_memory_offset,
t->width,
t->height);
assert(mesh->texture_coords->tag == tag::mesh_texture_coords);
const vec2 * texture_coords = mesh->texture_coords->texture_coords;
assert(mesh->texture_coords->n_texture_coords == mesh->n_vertices);
//mesh->vertices; //mesh->vertices;
for (int i = 0; i < mesh->n_faces; i++) { for (int i = 0; i < mesh->n_faces; i++) {
const auto& indices = mesh->faces[i].face_vertex_indices; const auto& indices = mesh->faces[i].face_vertex_indices;
@ -343,7 +347,10 @@ static inline void render_mesh(ta_parameter_writer& writer,
base_color, base_color,
screen_transform(screen, model * mesh->vertices[indices[0]]), screen_transform(screen, model * mesh->vertices[indices[0]]),
screen_transform(screen, model * mesh->vertices[indices[1]]), screen_transform(screen, model * mesh->vertices[indices[1]]),
screen_transform(screen, model * mesh->vertices[indices[2]])); screen_transform(screen, model * mesh->vertices[indices[2]]),
texture_coords[indices[0]],
texture_coords[indices[1]],
texture_coords[indices[2]]);
} }
} }
@ -380,8 +387,11 @@ static inline void render_frame(ta_parameter_writer& writer,
void render_castle(ta_parameter_writer& writer, const mat4x4& screen) void render_castle(ta_parameter_writer& writer, const mat4x4& screen)
{ {
for (uint32_t i = 0; i < (sizeof (frames)) / (sizeof (frames[0])); i++) { for (uint32_t i = 0; i < (sizeof (castle_objects)) / (sizeof (castle_objects[0])); i++) {
render_frame(writer, screen, frames[i]); if (castle_objects[i]->tag != tag::frame)
continue;
render_frame(writer, screen, &castle_objects[i]->frame);
} }
} }
@ -390,10 +400,6 @@ void transfer_scene(ta_parameter_writer& writer)
{ {
const mat4x4 screen = screen_rotation(theta); const mat4x4 screen = screen_rotation(theta);
global_polygon_type_0(writer, texture_memory_alloc.texture.start,
para_control::list_type::opaque,
isp_tsp_instruction_word::culling_mode::no_culling);
render_castle(writer, screen); render_castle(writer, screen);
// end of opaque list // end of opaque list
@ -403,14 +409,14 @@ void transfer_scene(ta_parameter_writer& writer)
theta += deg * 0.1; theta += deg * 0.1;
} }
void transfer_ta_fifo_texture_memory_32byte(void * dst, void * src, int length) void transfer_ta_fifo_texture_memory_32byte(void * dst, const void * src, int length)
{ {
uint32_t out_addr = (uint32_t)dst; uint32_t out_addr = (uint32_t)dst;
sh7091.CCN.QACR0 = ((reinterpret_cast<uint32_t>(out_addr) >> 24) & 0b11100); sh7091.CCN.QACR0 = ((reinterpret_cast<uint32_t>(out_addr) >> 24) & 0b11100);
sh7091.CCN.QACR1 = ((reinterpret_cast<uint32_t>(out_addr) >> 24) & 0b11100); sh7091.CCN.QACR1 = ((reinterpret_cast<uint32_t>(out_addr) >> 24) & 0b11100);
volatile uint32_t * base = &store_queue[(out_addr & 0x03ffffc0) / 4]; volatile uint32_t * base = &store_queue[(out_addr & 0x03ffffc0) / 4];
uint32_t * src32 = reinterpret_cast<uint32_t *>(src); const uint32_t * src32 = reinterpret_cast<const uint32_t *>(src);
length = (length + 31) & ~31; // round up to nearest multiple of 32 length = (length + 31) & ~31; // round up to nearest multiple of 32
while (length > 0) { while (length > 0) {
@ -432,6 +438,39 @@ void transfer_ta_fifo_texture_memory_32byte(void * dst, void * src, int length)
} }
} }
void transfer_texture(const material * material)
{
int ix = 0;
while (true) {
if (material->objects[ix] == nullptr)
return;
if (material->objects[ix]->tag == tag::texture_filename)
break;
ix += 1;
}
const texture_filename * t = &material->objects[ix]->texture_filename;
assert(t->tag == tag::texture_filename);
uint32_t offset = texture_memory_alloc.texture.start + t->texture_memory_offset;
void * dst = (void *)(&texture_memory64[offset / 4]);
transfer_ta_fifo_texture_memory_32byte(dst, t->start, t->size);
}
void transfer_textures()
{
system.LMMODE0 = 0; // 64-bit address space
system.LMMODE1 = 0; // 64-bit address space
for (uint32_t i = 0; i < (sizeof (castle_objects)) / (sizeof (castle_objects[0])); i++) {
if (castle_objects[i]->tag != tag::material)
continue;
transfer_texture(&castle_objects[i]->material);
}
}
uint8_t __attribute__((aligned(32))) ta_parameter_buf[1024 * 1024]; uint8_t __attribute__((aligned(32))) ta_parameter_buf[1024 * 1024];
void main() void main()
@ -488,6 +527,7 @@ void main()
ta_parameter_writer writer = ta_parameter_writer(ta_parameter_buf); ta_parameter_writer writer = ta_parameter_writer(ta_parameter_buf);
transfer_textures();
video_output::set_mode_vga(); video_output::set_mode_vga();
while (1) { while (1) {
ta_polygon_converter_init2(texture_memory_alloc.isp_tsp_parameters[ta].start, ta_polygon_converter_init2(texture_memory_alloc.isp_tsp_parameters[ta].start,

View File

@ -898,6 +898,11 @@ CASTLE_OBJ = \
holly/ta_fifo_polygon_converter.o \ holly/ta_fifo_polygon_converter.o \
holly/video_output.o \ holly/video_output.o \
sh7091/serial.o \ sh7091/serial.o \
model/castle/castlest.data.o \
model/castle/gothic3.data.o \
model/castle/oldbric.data.o \
model/castle/shingle.data.o \
model/castle/stone2.data.o \
$(LIBGCC) $(LIBGCC)
example/castle.elf: LDSCRIPT = $(LIB)/main.lds example/castle.elf: LDSCRIPT = $(LIB)/main.lds

BIN
model/castle/castlest.data Normal file

Binary file not shown.

View File

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

BIN
model/castle/castlest.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

43
model/castle/gothic3.data Normal file

File diff suppressed because one or more lines are too long

View File

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

BIN
model/castle/gothic3.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

View File

@ -9789,7 +9789,11 @@ const material material_x3ds_mat_castlestone = {
const texture_filename texture_filename_0 = { const texture_filename texture_filename_0 = {
.tag = tag::texture_filename, .tag = tag::texture_filename,
.start = reinterpret_cast<const void *>(&_binary_model_castle_castlest_data_start),
.size = reinterpret_cast<int>(&_binary_model_castle_castlest_data_size),
.texture_memory_offset = 0,
.width = 256,
.height = 256,
}; };
const material material_x3ds_mat_Material__3 = { const material material_x3ds_mat_Material__3 = {
@ -9807,7 +9811,11 @@ const material material_x3ds_mat_Material__3 = {
const texture_filename texture_filename_1 = { const texture_filename texture_filename_1 = {
.tag = tag::texture_filename, .tag = tag::texture_filename,
.start = reinterpret_cast<const void *>(&_binary_model_castle_oldbric_data_start),
.size = reinterpret_cast<int>(&_binary_model_castle_oldbric_data_size),
.texture_memory_offset = 131072,
.width = 256,
.height = 256,
}; };
const material material_x3ds_mat_shingle = { const material material_x3ds_mat_shingle = {
@ -9825,7 +9833,11 @@ const material material_x3ds_mat_shingle = {
const texture_filename texture_filename_2 = { const texture_filename texture_filename_2 = {
.tag = tag::texture_filename, .tag = tag::texture_filename,
.start = reinterpret_cast<const void *>(&_binary_model_castle_shingle_data_start),
.size = reinterpret_cast<int>(&_binary_model_castle_shingle_data_size),
.texture_memory_offset = 262144,
.width = 256,
.height = 128,
}; };
const material material_x3ds_mat_Material__8 = { const material material_x3ds_mat_Material__8 = {
@ -9843,7 +9855,11 @@ const material material_x3ds_mat_Material__8 = {
const texture_filename texture_filename_3 = { const texture_filename texture_filename_3 = {
.tag = tag::texture_filename, .tag = tag::texture_filename,
.start = reinterpret_cast<const void *>(&_binary_model_castle_gothic3_data_start),
.size = reinterpret_cast<int>(&_binary_model_castle_gothic3_data_size),
.texture_memory_offset = 327680,
.width = 256,
.height = 256,
}; };
const frame frame_x3ds_Box08 = { const frame frame_x3ds_Box08 = {
@ -9872,13 +9888,9 @@ const mesh mesh_Box08 = {
.vertices = mesh_Box08_vertices, .vertices = mesh_Box08_vertices,
.n_faces = 44, .n_faces = 44,
.faces = mesh_Box08_faces, .faces = mesh_Box08_faces,
.material_list = &mesh_material_list_0,
.objects = { .normals = &mesh_normals_0,
reinterpret_cast<const data_object *>(&mesh_material_list_0), .texture_coords = &mesh_texture_coords_0,
reinterpret_cast<const data_object *>(&mesh_normals_0),
reinterpret_cast<const data_object *>(&mesh_texture_coords_0),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_0 = { const mesh_material_list mesh_material_list_0 = {
@ -10033,13 +10045,9 @@ const mesh mesh_Box09 = {
.vertices = mesh_Box09_vertices, .vertices = mesh_Box09_vertices,
.n_faces = 136, .n_faces = 136,
.faces = mesh_Box09_faces, .faces = mesh_Box09_faces,
.material_list = &mesh_material_list_1,
.objects = { .normals = &mesh_normals_1,
reinterpret_cast<const data_object *>(&mesh_material_list_1), .texture_coords = &mesh_texture_coords_1,
reinterpret_cast<const data_object *>(&mesh_normals_1),
reinterpret_cast<const data_object *>(&mesh_texture_coords_1),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_1 = { const mesh_material_list mesh_material_list_1 = {
@ -10422,13 +10430,9 @@ const mesh mesh_Box02 = {
.vertices = mesh_Box02_vertices, .vertices = mesh_Box02_vertices,
.n_faces = 8, .n_faces = 8,
.faces = mesh_Box02_faces, .faces = mesh_Box02_faces,
.material_list = &mesh_material_list_2,
.objects = { .normals = &mesh_normals_2,
reinterpret_cast<const data_object *>(&mesh_material_list_2), .texture_coords = &mesh_texture_coords_2,
reinterpret_cast<const data_object *>(&mesh_normals_2),
reinterpret_cast<const data_object *>(&mesh_texture_coords_2),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_2 = { const mesh_material_list mesh_material_list_2 = {
@ -10499,13 +10503,9 @@ const mesh mesh_Cone02 = {
.vertices = mesh_Cone02_vertices, .vertices = mesh_Cone02_vertices,
.n_faces = 32, .n_faces = 32,
.faces = mesh_Cone02_faces, .faces = mesh_Cone02_faces,
.material_list = &mesh_material_list_3,
.objects = { .normals = &mesh_normals_3,
reinterpret_cast<const data_object *>(&mesh_material_list_3), .texture_coords = &mesh_texture_coords_3,
reinterpret_cast<const data_object *>(&mesh_normals_3),
reinterpret_cast<const data_object *>(&mesh_texture_coords_3),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_3 = { const mesh_material_list mesh_material_list_3 = {
@ -10602,13 +10602,9 @@ const mesh mesh_Cylinder02 = {
.vertices = mesh_Cylinder02_vertices, .vertices = mesh_Cylinder02_vertices,
.n_faces = 16, .n_faces = 16,
.faces = mesh_Cylinder02_faces, .faces = mesh_Cylinder02_faces,
.material_list = &mesh_material_list_4,
.objects = { .normals = &mesh_normals_4,
reinterpret_cast<const data_object *>(&mesh_material_list_4), .texture_coords = &mesh_texture_coords_4,
reinterpret_cast<const data_object *>(&mesh_normals_4),
reinterpret_cast<const data_object *>(&mesh_texture_coords_4),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_4 = { const mesh_material_list mesh_material_list_4 = {
@ -10682,13 +10678,9 @@ const mesh mesh_Box21 = {
.vertices = mesh_Box21_vertices, .vertices = mesh_Box21_vertices,
.n_faces = 176, .n_faces = 176,
.faces = mesh_Box21_faces, .faces = mesh_Box21_faces,
.material_list = &mesh_material_list_5,
.objects = { .normals = &mesh_normals_5,
reinterpret_cast<const data_object *>(&mesh_material_list_5), .texture_coords = &mesh_texture_coords_5,
reinterpret_cast<const data_object *>(&mesh_normals_5),
reinterpret_cast<const data_object *>(&mesh_texture_coords_5),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_5 = { const mesh_material_list mesh_material_list_5 = {
@ -11121,13 +11113,9 @@ const mesh mesh_Box22 = {
.vertices = mesh_Box22_vertices, .vertices = mesh_Box22_vertices,
.n_faces = 136, .n_faces = 136,
.faces = mesh_Box22_faces, .faces = mesh_Box22_faces,
.material_list = &mesh_material_list_6,
.objects = { .normals = &mesh_normals_6,
reinterpret_cast<const data_object *>(&mesh_material_list_6), .texture_coords = &mesh_texture_coords_6,
reinterpret_cast<const data_object *>(&mesh_normals_6),
reinterpret_cast<const data_object *>(&mesh_texture_coords_6),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_6 = { const mesh_material_list mesh_material_list_6 = {
@ -11510,13 +11498,9 @@ const mesh mesh_Box23 = {
.vertices = mesh_Box23_vertices, .vertices = mesh_Box23_vertices,
.n_faces = 176, .n_faces = 176,
.faces = mesh_Box23_faces, .faces = mesh_Box23_faces,
.material_list = &mesh_material_list_7,
.objects = { .normals = &mesh_normals_7,
reinterpret_cast<const data_object *>(&mesh_material_list_7), .texture_coords = &mesh_texture_coords_7,
reinterpret_cast<const data_object *>(&mesh_normals_7),
reinterpret_cast<const data_object *>(&mesh_texture_coords_7),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_7 = { const mesh_material_list mesh_material_list_7 = {
@ -11949,13 +11933,9 @@ const mesh mesh_Box24 = {
.vertices = mesh_Box24_vertices, .vertices = mesh_Box24_vertices,
.n_faces = 44, .n_faces = 44,
.faces = mesh_Box24_faces, .faces = mesh_Box24_faces,
.material_list = &mesh_material_list_8,
.objects = { .normals = &mesh_normals_8,
reinterpret_cast<const data_object *>(&mesh_material_list_8), .texture_coords = &mesh_texture_coords_8,
reinterpret_cast<const data_object *>(&mesh_normals_8),
reinterpret_cast<const data_object *>(&mesh_texture_coords_8),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_8 = { const mesh_material_list mesh_material_list_8 = {
@ -12110,13 +12090,9 @@ const mesh mesh_Box25 = {
.vertices = mesh_Box25_vertices, .vertices = mesh_Box25_vertices,
.n_faces = 136, .n_faces = 136,
.faces = mesh_Box25_faces, .faces = mesh_Box25_faces,
.material_list = &mesh_material_list_9,
.objects = { .normals = &mesh_normals_9,
reinterpret_cast<const data_object *>(&mesh_material_list_9), .texture_coords = &mesh_texture_coords_9,
reinterpret_cast<const data_object *>(&mesh_normals_9),
reinterpret_cast<const data_object *>(&mesh_texture_coords_9),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_9 = { const mesh_material_list mesh_material_list_9 = {
@ -12499,13 +12475,9 @@ const mesh mesh_Box26 = {
.vertices = mesh_Box26_vertices, .vertices = mesh_Box26_vertices,
.n_faces = 176, .n_faces = 176,
.faces = mesh_Box26_faces, .faces = mesh_Box26_faces,
.material_list = &mesh_material_list_10,
.objects = { .normals = &mesh_normals_10,
reinterpret_cast<const data_object *>(&mesh_material_list_10), .texture_coords = &mesh_texture_coords_10,
reinterpret_cast<const data_object *>(&mesh_normals_10),
reinterpret_cast<const data_object *>(&mesh_texture_coords_10),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_10 = { const mesh_material_list mesh_material_list_10 = {
@ -12938,13 +12910,9 @@ const mesh mesh_Box27 = {
.vertices = mesh_Box27_vertices, .vertices = mesh_Box27_vertices,
.n_faces = 136, .n_faces = 136,
.faces = mesh_Box27_faces, .faces = mesh_Box27_faces,
.material_list = &mesh_material_list_11,
.objects = { .normals = &mesh_normals_11,
reinterpret_cast<const data_object *>(&mesh_material_list_11), .texture_coords = &mesh_texture_coords_11,
reinterpret_cast<const data_object *>(&mesh_normals_11),
reinterpret_cast<const data_object *>(&mesh_texture_coords_11),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_11 = { const mesh_material_list mesh_material_list_11 = {
@ -13327,13 +13295,9 @@ const mesh mesh_Cylinder03 = {
.vertices = mesh_Cylinder03_vertices, .vertices = mesh_Cylinder03_vertices,
.n_faces = 16, .n_faces = 16,
.faces = mesh_Cylinder03_faces, .faces = mesh_Cylinder03_faces,
.material_list = &mesh_material_list_12,
.objects = { .normals = &mesh_normals_12,
reinterpret_cast<const data_object *>(&mesh_material_list_12), .texture_coords = &mesh_texture_coords_12,
reinterpret_cast<const data_object *>(&mesh_normals_12),
reinterpret_cast<const data_object *>(&mesh_texture_coords_12),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_12 = { const mesh_material_list mesh_material_list_12 = {
@ -13407,13 +13371,9 @@ const mesh mesh_Cone03 = {
.vertices = mesh_Cone03_vertices, .vertices = mesh_Cone03_vertices,
.n_faces = 32, .n_faces = 32,
.faces = mesh_Cone03_faces, .faces = mesh_Cone03_faces,
.material_list = &mesh_material_list_13,
.objects = { .normals = &mesh_normals_13,
reinterpret_cast<const data_object *>(&mesh_material_list_13), .texture_coords = &mesh_texture_coords_13,
reinterpret_cast<const data_object *>(&mesh_normals_13),
reinterpret_cast<const data_object *>(&mesh_texture_coords_13),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_13 = { const mesh_material_list mesh_material_list_13 = {
@ -13510,13 +13470,9 @@ const mesh mesh_Cylinder01 = {
.vertices = mesh_Cylinder01_vertices, .vertices = mesh_Cylinder01_vertices,
.n_faces = 174, .n_faces = 174,
.faces = mesh_Cylinder01_faces, .faces = mesh_Cylinder01_faces,
.material_list = &mesh_material_list_14,
.objects = { .normals = &mesh_normals_14,
reinterpret_cast<const data_object *>(&mesh_material_list_14), .texture_coords = &mesh_texture_coords_14,
reinterpret_cast<const data_object *>(&mesh_normals_14),
reinterpret_cast<const data_object *>(&mesh_texture_coords_14),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_14 = { const mesh_material_list mesh_material_list_14 = {
@ -13976,13 +13932,9 @@ const mesh mesh_Box01 = {
.vertices = mesh_Box01_vertices, .vertices = mesh_Box01_vertices,
.n_faces = 8, .n_faces = 8,
.faces = mesh_Box01_faces, .faces = mesh_Box01_faces,
.material_list = &mesh_material_list_15,
.objects = { .normals = &mesh_normals_15,
reinterpret_cast<const data_object *>(&mesh_material_list_15), .texture_coords = &mesh_texture_coords_15,
reinterpret_cast<const data_object *>(&mesh_normals_15),
reinterpret_cast<const data_object *>(&mesh_texture_coords_15),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_15 = { const mesh_material_list mesh_material_list_15 = {
@ -14054,13 +14006,9 @@ const mesh mesh_Box28 = {
.vertices = mesh_Box28_vertices, .vertices = mesh_Box28_vertices,
.n_faces = 8, .n_faces = 8,
.faces = mesh_Box28_faces, .faces = mesh_Box28_faces,
.material_list = &mesh_material_list_16,
.objects = { .normals = &mesh_normals_16,
reinterpret_cast<const data_object *>(&mesh_material_list_16), .texture_coords = &mesh_texture_coords_16,
reinterpret_cast<const data_object *>(&mesh_normals_16),
reinterpret_cast<const data_object *>(&mesh_texture_coords_16),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_16 = { const mesh_material_list mesh_material_list_16 = {
@ -14132,13 +14080,9 @@ const mesh mesh_Box03 = {
.vertices = mesh_Box03_vertices, .vertices = mesh_Box03_vertices,
.n_faces = 10, .n_faces = 10,
.faces = mesh_Box03_faces, .faces = mesh_Box03_faces,
.material_list = &mesh_material_list_17,
.objects = { .normals = &mesh_normals_17,
reinterpret_cast<const data_object *>(&mesh_material_list_17), .texture_coords = &mesh_texture_coords_17,
reinterpret_cast<const data_object *>(&mesh_normals_17),
reinterpret_cast<const data_object *>(&mesh_texture_coords_17),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_17 = { const mesh_material_list mesh_material_list_17 = {
@ -14201,13 +14145,9 @@ const mesh mesh_roof = {
.vertices = mesh_roof_vertices, .vertices = mesh_roof_vertices,
.n_faces = 4, .n_faces = 4,
.faces = mesh_roof_faces, .faces = mesh_roof_faces,
.material_list = &mesh_material_list_18,
.objects = { .normals = &mesh_normals_18,
reinterpret_cast<const data_object *>(&mesh_material_list_18), .texture_coords = &mesh_texture_coords_18,
reinterpret_cast<const data_object *>(&mesh_normals_18),
reinterpret_cast<const data_object *>(&mesh_texture_coords_18),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_18 = { const mesh_material_list mesh_material_list_18 = {
@ -14268,13 +14208,9 @@ const mesh mesh_walkway = {
.vertices = mesh_walkway_vertices, .vertices = mesh_walkway_vertices,
.n_faces = 38, .n_faces = 38,
.faces = mesh_walkway_faces, .faces = mesh_walkway_faces,
.material_list = &mesh_material_list_19,
.objects = { .normals = &mesh_normals_19,
reinterpret_cast<const data_object *>(&mesh_material_list_19), .texture_coords = &mesh_texture_coords_19,
reinterpret_cast<const data_object *>(&mesh_normals_19),
reinterpret_cast<const data_object *>(&mesh_texture_coords_19),
nullptr,
}
}; };
const mesh_material_list mesh_material_list_19 = { const mesh_material_list mesh_material_list_19 = {
@ -14969,4 +14905,33 @@ const animation_key animation_key_39 = {
}, },
}; };
const data_object * castle_objects[] = {
reinterpret_cast<const data_object *>(&header_0),
reinterpret_cast<const data_object *>(&material_x3ds_mat_castlestone),
reinterpret_cast<const data_object *>(&material_x3ds_mat_Material__3),
reinterpret_cast<const data_object *>(&material_x3ds_mat_shingle),
reinterpret_cast<const data_object *>(&material_x3ds_mat_Material__8),
reinterpret_cast<const data_object *>(&frame_x3ds_Box08),
reinterpret_cast<const data_object *>(&frame_x3ds_Box09),
reinterpret_cast<const data_object *>(&frame_x3ds_Box02),
reinterpret_cast<const data_object *>(&frame_x3ds_Cone02),
reinterpret_cast<const data_object *>(&frame_x3ds_Cylinder02),
reinterpret_cast<const data_object *>(&frame_x3ds_Box21),
reinterpret_cast<const data_object *>(&frame_x3ds_Box22),
reinterpret_cast<const data_object *>(&frame_x3ds_Box23),
reinterpret_cast<const data_object *>(&frame_x3ds_Box24),
reinterpret_cast<const data_object *>(&frame_x3ds_Box25),
reinterpret_cast<const data_object *>(&frame_x3ds_Box26),
reinterpret_cast<const data_object *>(&frame_x3ds_Box27),
reinterpret_cast<const data_object *>(&frame_x3ds_Cylinder03),
reinterpret_cast<const data_object *>(&frame_x3ds_Cone03),
reinterpret_cast<const data_object *>(&frame_x3ds_Cylinder01),
reinterpret_cast<const data_object *>(&frame_x3ds_Box01),
reinterpret_cast<const data_object *>(&frame_x3ds_Box28),
reinterpret_cast<const data_object *>(&frame_x3ds_Box03),
reinterpret_cast<const data_object *>(&frame_x3ds_roof),
reinterpret_cast<const data_object *>(&frame_x3ds_walkway),
reinterpret_cast<const data_object *>(&animation_set_x3ds_animset_0),
};

206
model/castle/oldbric.data Normal file

File diff suppressed because one or more lines are too long

View File

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

BIN
model/castle/oldbric.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
model/castle/shingle.data Normal file

Binary file not shown.

View File

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

BIN
model/castle/shingle.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

BIN
model/castle/stone2.data Normal file

Binary file not shown.

View File

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

BIN
model/castle/stone2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

View File

@ -3,9 +3,13 @@ from parse import parse_all, TokenReader
import templates import templates
import dataclasses import dataclasses
from collections import Counter from collections import Counter
from os import path
from pprint import pprint from pprint import pprint
import sys import sys
from PIL import Image
x_filename = None
def obj_value(obj): def obj_value(obj):
if type(obj) is tuple: if type(obj) is tuple:
@ -32,6 +36,9 @@ def visit_objects(func, obj):
for o in obj_value(obj).objects: for o in obj_value(obj).objects:
yield from visit(func, o) yield from visit(func, o)
def visit_objects_silent(func, obj):
yield from visit_objects(func, obj)
def visit_self(func, obj): def visit_self(func, obj):
yield from func(obj) yield from func(obj)
@ -67,7 +74,7 @@ visitors = {
templates.TextureFilename : visit_self, templates.TextureFilename : visit_self,
templates.Frame : visit_objects, templates.Frame : visit_objects,
templates.FrameTransformMatrix : visit_self, templates.FrameTransformMatrix : visit_self,
templates.Mesh : visit_objects, templates.Mesh : visit_objects_silent,
templates.MeshMaterialList : visit_objects, templates.MeshMaterialList : visit_objects,
templates.MeshNormals : visit_self, templates.MeshNormals : visit_self,
templates.MeshTextureCoords : visit_self, templates.MeshTextureCoords : visit_self,
@ -147,6 +154,18 @@ def type_declaration(obj):
string_name = get_obj_name(obj) string_name = get_obj_name(obj)
return f"const {type_name} {string_name}" return f"const {type_name} {string_name}"
def find_object_by_type(objects, type):
return [
obj for obj in objects if obj_type(obj) is type
]
def one_object_name_or_nullptr(objects):
assert len(objects) in {0, 1}, objects
if len(objects) == 1:
return get_obj_name(objects[0])
else:
return "nullptr"
def generate_vec3(name, name2, vertices): def generate_vec3(name, name2, vertices):
yield f"vec3 {name}_{name2}[] = {{" yield f"vec3 {name}_{name2}[] = {{"
for v in vertices: for v in vertices:
@ -194,8 +213,26 @@ def generate_material(obj):
yield f".specular_color = {{{specular_color}}}," yield f".specular_color = {{{specular_color}}},"
yield f".emissive_color = {{{emissive_color}}}," yield f".emissive_color = {{{emissive_color}}},"
texture_memory_offset = 0
def generate_texture_filename(obj): def generate_texture_filename(obj):
yield "" global x_filename
global texture_memory_offset
directory, _ = path.split(x_filename)
image_path = path.join(directory, obj.filename.lower())
with Image.open(image_path) as im:
width, height = im.size
noext, _ = path.splitext(image_path)
data = noext.replace("/", "_")
data_name = f"_binary_{data}_data"
yield f".start = reinterpret_cast<const void *>(&{data_name}_start),"
yield f".size = reinterpret_cast<int>(&{data_name}_size),"
yield f".texture_memory_offset = {texture_memory_offset},"
yield f".width = {width},"
yield f".height = {height},"
texture_memory_offset += width * height * 2
def generate_frame(obj): def generate_frame(obj):
return return
@ -218,6 +255,16 @@ def generate_mesh(obj):
yield f".n_faces = {obj.nFaces}," yield f".n_faces = {obj.nFaces},"
yield f".faces = {name}_faces," yield f".faces = {name}_faces,"
material_list = one_object_name_or_nullptr(
find_object_by_type(obj.objects, templates.MeshMaterialList))
normals = one_object_name_or_nullptr(
find_object_by_type(obj.objects, templates.MeshNormals))
texture_coords = one_object_name_or_nullptr(
find_object_by_type(obj.objects, templates.MeshTextureCoords))
yield f".material_list = &{material_list},"
yield f".normals = &{normals},"
yield f".texture_coords = &{texture_coords},"
def generate_mesh_material_list(obj): def generate_mesh_material_list(obj):
name = get_obj_name(obj) name = get_obj_name(obj)
yield f".n_materials = {obj.nMaterials}," yield f".n_materials = {obj.nMaterials},"
@ -313,16 +360,22 @@ def visit_all(func, objects):
for obj in objects: for obj in objects:
yield from visit(func, obj) yield from visit(func, obj)
def gen(objects): def gen(prefix, objects):
yield from visit_all(generate_predeclaration, objects) yield from visit_all(generate_predeclaration, objects)
yield from visit_all(generate_definition, objects) yield from visit_all(generate_definition, objects)
yield f"const data_object * {prefix}_objects[] = {{"
for obj in objects:
yield f"reinterpret_cast<const data_object *>(&{get_obj_name(obj)}),"
yield "};"
with open(sys.argv[1], "rb") as f: prefix = sys.argv[2]
x_filename = sys.argv[1]
with open(x_filename, "rb") as f:
buf = f.read() buf = f.read()
objects = list(parse_all(TokenReader(buf))) objects = list(parse_all(TokenReader(buf)))
_ = list(visit_all(add_name_map, objects)) _ = list(visit_all(add_name_map, objects))
render, out = generate.renderer() render, out = generate.renderer()
render(gen(objects)) render(gen(prefix, objects))
print(out.getvalue()) print(out.getvalue())

22
x/x.hpp
View File

@ -51,7 +51,12 @@ struct material {
struct texture_filename { struct texture_filename {
enum tag tag; enum tag tag;
const char * filename; //const char * filename;
const void * start;
int size;
int texture_memory_offset;
int16_t width;
int16_t height;
}; };
struct frame { struct frame {
@ -64,13 +69,22 @@ struct frame_transform_matrix {
mat4x4 frame_matrix; mat4x4 frame_matrix;
}; };
struct mesh_material_list;
struct mesh_normals;
struct mesh_texture_coords;
struct mesh { struct mesh {
enum tag tag; enum tag tag;
int n_vertices; int n_vertices;
vec3 * vertices; vec3 * vertices;
int n_faces; int n_faces;
struct mesh_face * faces; mesh_face * faces;
const data_object * objects[]; //
const mesh_material_list * material_list;
const mesh_normals * normals;
const mesh_texture_coords * texture_coords;
//const data_object * objects[];
}; };
struct mesh_material_list { struct mesh_material_list {
@ -86,7 +100,7 @@ struct mesh_normals {
int n_normals; int n_normals;
vec3 * normals; vec3 * normals;
int n_face_normals; int n_face_normals;
struct mesh_face * face_normals; mesh_face * face_normals;
}; };
struct mesh_texture_coords { struct mesh_texture_coords {