diff --git a/example/q3bsp.cpp b/example/q3bsp.cpp index 026257d..72efbf2 100644 --- a/example/q3bsp.cpp +++ b/example/q3bsp.cpp @@ -63,6 +63,9 @@ #include "pk/models/mapobjects/gratelamp/gratetorch2.data.h" #include "pk/models/mapobjects/gratelamp/gratetorch2b.data.h" +#include "model/model.h" +#include "model/icosphere/model.h" + #include "font/font_bitmap.hpp" #include "font/verite_8x16/verite_8x16.data.h" #include "palette.hpp" @@ -81,6 +84,8 @@ using mat4x4 = mat<4, 4, float>; #define _fsrra(n) (1.0f / (__builtin_sqrtf(n))) +static vec3 sphere_position = {890, 480, 400}; + static ft0::data_transfer::data_format data[4]; uint8_t send_buf[1024] __attribute__((aligned(32))); @@ -176,7 +181,12 @@ void global_polygon_type_0(ta_parameter_writer& writer) void global_polygon_type_1(ta_parameter_writer& writer, uint32_t obj_control_texture, uint32_t texture_u_v_size, - uint32_t texture_control_word) + uint32_t texture_control_word, + const float a = 1.0f, + const float r = 1.0f, + const float g = 1.0f, + const float b = 1.0f + ) { const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume | para_control::list_type::opaque @@ -197,11 +207,6 @@ void global_polygon_type_1(ta_parameter_writer& writer, | texture_u_v_size ; - const float a = 1.0f; - const float r = 1.0f; - const float g = 1.0f; - const float b = 1.0f; - writer.append() = ta_global_parameter::polygon_type_1(parameter_control_word, isp_tsp_instruction_word, @@ -360,7 +365,7 @@ void transfer_faces(uint8_t * buf, q3bsp_header_t * header, ta_parameter_writer& if (has_texture) { global_texture(writer, face[i].texture); } else { - global_polygon_type_1(writer, 0, 0, 0); + //global_polygon_type_1(writer, 0, 0, 0); } } @@ -408,6 +413,7 @@ void transfer_faces(uint8_t * buf, q3bsp_header_t * header, ta_parameter_writer& li, li); } else { + /* render_tri_type_2(writer, ap, bp, @@ -415,11 +421,63 @@ void transfer_faces(uint8_t * buf, q3bsp_header_t * header, ta_parameter_writer& li, li, li); + */ } } } } +void transfer_icosphere(ta_parameter_writer& writer, mat4x4& screen_trans) +{ + const struct model * model = &icosphere_model; + const struct object * object = model->object[0]; + const vertex_position * position = model->position; + const vertex_normal * normal = model->normal; + + float s = 50; + mat4x4 scale = { + s, 0, 0, 0, + 0, -s, 0, 0, + 0, 0, s, 0, + 0, 0, 0, 1 + }; + + mat4x4 translate = { + 1, 0, 0, sphere_position.x, + 0, 1, 0, sphere_position.y, + 0, 0, 1, sphere_position.z, + 0, 0, 0, 1 + }; + + mat4x4 trans = screen_trans * translate * scale; + + float a = 1.0f; + float r = 0.9f; + float g = 0.5f; + float b = 0.0f; + global_polygon_type_1(writer, 0, 0, 0, a, r, g, b); + + for (int i = 0; i < object->triangle_count; i++) { + const union triangle * tri = &object->triangle[i]; + vec3 ap = trans * position[tri->v[0].position]; + vec3 bp = trans * position[tri->v[1].position]; + vec3 cp = trans * position[tri->v[2].position]; + + if (ap.z < 0 || bp.z < 0 || cp.z < 0) return; + + vec3 n = normal_transform(trans, normal[tri->v[0].normal]); + float li = light_intensity(light_vec, n); + + render_tri_type_2(writer, + screen_transform(ap), + screen_transform(bp), + screen_transform(cp), + li, + li, + li); + } +} + static inline void render_quad(ta_parameter_writer& writer, vec3 ap, vec3 bp, @@ -548,6 +606,27 @@ void render_matrix(ta_parameter_writer& writer, const mat4x4& trans) } } +void render_sphere_position(ta_parameter_writer& writer) +{ + char __attribute__((aligned(4))) s[64] = "pos: "; + for (uint32_t i = 2; i < ((sizeof (s)) - 8) / 4; i++) + reinterpret_cast(s)[i] = 0x20202020; + + int offset = 8; + int row = 5; + offset += format_float(&s[offset], sphere_position[0], 7); + offset += format_float(&s[offset], sphere_position[1], 7); + offset += format_float(&s[offset], sphere_position[2], 7); + + font_bitmap::transform_string(writer, + 8, 16, // texture + 8, 16, // glyph + 16 + 2 * 8, // position x + 16 + row * 16, // position y + s, offset, + para_control::list_type::opaque); +} + static int root_ix = 0; void render_ix(ta_parameter_writer& writer, int row, char * s, int ix) @@ -668,14 +747,24 @@ void transfer_scene(ta_parameter_writer& writer, const mat4x4& screen_trans) transform_vertices(&buf[ve->offset], ve->length, trans); transfer_faces(buf, header, writer); + transfer_icosphere(writer, trans); render_matrix(writer, trans); render_leaf_ix(writer); + render_sphere_position(writer); writer.append() = ta_global_parameter::end_of_list(para_control::para_type::end_of_list); - render_bounding_boxes(writer, trans); + global_polygon_type_0(writer); + render_quad(writer, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + 0); + + //render_bounding_boxes(writer, trans); writer.append() = ta_global_parameter::end_of_list(para_control::para_type::end_of_list); @@ -772,6 +861,8 @@ mat4x4 update_analog(mat4x4& screen) int da = ft0::data_transfer::digital_button::da(data[0].digital_button) == 0; int ua = ft0::data_transfer::digital_button::ua(data[0].digital_button) == 0; + int db_a = ft0::data_transfer::digital_button::a(data[0].digital_button) == 0; + int db_b = ft0::data_transfer::digital_button::b(data[0].digital_button) == 0; int db_x = ft0::data_transfer::digital_button::x(data[0].digital_button) == 0; int db_y = ft0::data_transfer::digital_button::y(data[0].digital_button) == 0; @@ -814,25 +905,40 @@ mat4x4 update_analog(mat4x4& screen) q3bsp_direntry * ne = &header->direntries[LUMP_NODES]; q3bsp_node_t * nodes = reinterpret_cast(&buf[ne->offset]); - if (db_x && !db_y && !push) { - push = true; - //leaf_ix -= 1; - //if (leaf_ix < 0) leaf_ix = num_leaves - 1; + if (0) { + if (db_x && !db_y && !push) { + push = true; + //leaf_ix -= 1; + //if (leaf_ix < 0) leaf_ix = num_leaves - 1; - int ix = nodes[root_ix].children[0]; - if (ix >= 0) - root_ix = ix; - } - if (db_y && !db_x && !push) { - push = true; - //leaf_ix += 1; - //if (leaf_ix > num_leaves) leaf_ix = 0; - int ix = nodes[root_ix].children[1]; - if (ix >= 0) - root_ix = ix; - } - if (!db_x && !db_y) { - push = false; + int ix = nodes[root_ix].children[0]; + if (ix >= 0) + root_ix = ix; + } + if (db_y && !db_x && !push) { + push = true; + //leaf_ix += 1; + //if (leaf_ix > num_leaves) leaf_ix = 0; + int ix = nodes[root_ix].children[1]; + if (ix >= 0) + root_ix = ix; + } + if (!db_x && !db_y) { + push = false; + } + } else { + if (db_x && !db_b) { + sphere_position.x -= 10; + } + if (db_b && !db_x) { + sphere_position.x += 10; + } + if (db_y && !db_a) { + sphere_position.y += 10; + } + if (db_a && !db_y) { + sphere_position.y -= 10; + } } return rx * ry * t * screen; @@ -910,9 +1016,9 @@ int main() int core = 0; mat4x4 trans = { - 1.0, 0.0, 0.000, -1400.0, - 0.0, -0.574, -0.818, 981.0, - 0.0, 0.818, -0.574, 711.0, + 1.0, 0.0, 0.000, -1123.0, + 0.0, -0.888, -0.458, 859.0, + 0.0, 0.458, -0.888, 791.0, 0.0, 0.000, 0.000, 1.0, }; diff --git a/model/icosphere/model.h b/model/icosphere/model.h new file mode 100644 index 0000000..180ea3b --- /dev/null +++ b/model/icosphere/model.h @@ -0,0 +1,197 @@ +#pragma once +#include "../model.h" + +const vertex_position icosphere_position[] = { + {0.000000f, -1.000000f, 0.000000f}, + {0.723600f, -0.447215f, 0.525720f}, + {-0.276385f, -0.447215f, 0.850640f}, + {-0.894425f, -0.447215f, 0.000000f}, + {-0.276385f, -0.447215f, -0.850640f}, + {0.723600f, -0.447215f, -0.525720f}, + {0.276385f, 0.447215f, 0.850640f}, + {-0.723600f, 0.447215f, 0.525720f}, + {-0.723600f, 0.447215f, -0.525720f}, + {0.276385f, 0.447215f, -0.850640f}, + {0.894425f, 0.447215f, 0.000000f}, + {0.000000f, 1.000000f, 0.000000f}, +}; + +const vertex_texture icosphere_texture[] = { + {0.181819f, 0.000000f}, + {0.272728f, 0.157461f}, + {0.090910f, 0.157461f}, + {0.363637f, 0.000000f}, + {0.454546f, 0.157461f}, + {0.909091f, 0.000000f}, + {1.000000f, 0.157461f}, + {0.818182f, 0.157461f}, + {0.727273f, 0.000000f}, + {0.636364f, 0.157461f}, + {0.545455f, 0.000000f}, + {0.363637f, 0.314921f}, + {0.181819f, 0.314921f}, + {0.909091f, 0.314921f}, + {0.727273f, 0.314921f}, + {0.545455f, 0.314921f}, + {0.000000f, 0.314921f}, + {0.272728f, 0.472382f}, + {0.090910f, 0.472382f}, + {0.818182f, 0.472382f}, + {0.636364f, 0.472382f}, + {0.454546f, 0.472382f}, +}; + +const vertex_normal icosphere_normal[] = { + {0.1876f, -0.7947f, 0.5774f}, + {0.6071f, -0.7947f, -0.0000f}, + {-0.4911f, -0.7947f, 0.3568f}, + {-0.4911f, -0.7947f, -0.3568f}, + {0.1876f, -0.7947f, -0.5774f}, + {0.9822f, -0.1876f, -0.0000f}, + {0.3035f, -0.1876f, 0.9342f}, + {-0.7946f, -0.1876f, 0.5774f}, + {-0.7946f, -0.1876f, -0.5774f}, + {0.3035f, -0.1876f, -0.9342f}, + {0.7946f, 0.1876f, 0.5774f}, + {-0.3035f, 0.1876f, 0.9342f}, + {-0.9822f, 0.1876f, -0.0000f}, + {-0.3035f, 0.1876f, -0.9342f}, + {0.7946f, 0.1876f, -0.5774f}, + {0.4911f, 0.7947f, 0.3568f}, + {-0.1876f, 0.7947f, 0.5774f}, + {-0.6071f, 0.7947f, -0.0000f}, + {-0.1876f, 0.7947f, -0.5774f}, + {0.4911f, 0.7947f, -0.3568f}, +}; + +const union triangle icosphere_Icosphere_triangle[] = { + { .v = { + {0, 0, 0}, + {1, 1, 0}, + {2, 2, 0}, + }}, + { .v = { + {1, 1, 1}, + {0, 3, 1}, + {5, 4, 1}, + }}, + { .v = { + {0, 5, 2}, + {2, 6, 2}, + {3, 7, 2}, + }}, + { .v = { + {0, 8, 3}, + {3, 7, 3}, + {4, 9, 3}, + }}, + { .v = { + {0, 10, 4}, + {4, 9, 4}, + {5, 4, 4}, + }}, + { .v = { + {1, 1, 5}, + {5, 4, 5}, + {10, 11, 5}, + }}, + { .v = { + {2, 2, 6}, + {1, 1, 6}, + {6, 12, 6}, + }}, + { .v = { + {3, 7, 7}, + {2, 6, 7}, + {7, 13, 7}, + }}, + { .v = { + {4, 9, 8}, + {3, 7, 8}, + {8, 14, 8}, + }}, + { .v = { + {5, 4, 9}, + {4, 9, 9}, + {9, 15, 9}, + }}, + { .v = { + {1, 1, 10}, + {10, 11, 10}, + {6, 12, 10}, + }}, + { .v = { + {2, 2, 11}, + {6, 12, 11}, + {7, 16, 11}, + }}, + { .v = { + {3, 7, 12}, + {7, 13, 12}, + {8, 14, 12}, + }}, + { .v = { + {4, 9, 13}, + {8, 14, 13}, + {9, 15, 13}, + }}, + { .v = { + {5, 4, 14}, + {9, 15, 14}, + {10, 11, 14}, + }}, + { .v = { + {6, 12, 15}, + {10, 11, 15}, + {11, 17, 15}, + }}, + { .v = { + {7, 16, 16}, + {6, 12, 16}, + {11, 18, 16}, + }}, + { .v = { + {8, 14, 17}, + {7, 13, 17}, + {11, 19, 17}, + }}, + { .v = { + {9, 15, 18}, + {8, 14, 18}, + {11, 20, 18}, + }}, + { .v = { + {10, 11, 19}, + {9, 15, 19}, + {11, 21, 19}, + }}, +}; + +const union quadrilateral icosphere_Icosphere_quadrilateral[] = { +}; + +const union line icosphere_Icosphere_line[] = { +}; + +const struct object icosphere_Icosphere = { + .triangle = &icosphere_Icosphere_triangle[0], + .quadrilateral = &icosphere_Icosphere_quadrilateral[0], + .line = &icosphere_Icosphere_line[0], + .triangle_count = 20, + .quadrilateral_count = 0, + .line_count = 0, + .material = 0, +}; + +const struct object * icosphere_object[] = { + &icosphere_Icosphere, +}; + +const struct model icosphere_model = { + .position = icosphere_position, + .texture = icosphere_texture, + .normal = icosphere_normal, + .object = icosphere_object, + .object_count = 1 +}; +