diff --git a/.gitignore b/.gitignore index b33ad04..2c0bb40 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ cdi4dc tools/ttf_outline *.blend1 *.scramble +*.FCStd1 \ No newline at end of file diff --git a/Makefile b/Makefile index 108c2ed..c777550 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ build-fonts: ./tools/ttf_outline 20 7f 20 1 little /usr/share/fonts/terminus/ter-u20n.otb ter_u20n.data include example/example.mk +include chess/chess.mk include text_editor/text_editor.mk include snake/snake.mk include pokemon/pokemon.mk diff --git a/chess/bishop.blend b/chess/bishop.blend new file mode 100644 index 0000000..b52298c Binary files /dev/null and b/chess/bishop.blend differ diff --git a/chess/bishop.hpp b/chess/bishop.hpp new file mode 100644 index 0000000..139be85 --- /dev/null +++ b/chess/bishop.hpp @@ -0,0 +1,186 @@ +#pragma once + +#include "geometry/geometry.hpp" + +namespace bishop { + constexpr vec3 vertices[] = { + { 0.584650f, -0.818375f, -0.500000f }, + { 0.529532f, -0.513422f, -0.500000f }, + { 0.563451f, -0.571720f, -0.500000f }, + { 0.583590f, -0.676656f, -0.500000f }, + { 0.443675f, -0.462544f, -0.500000f }, + { 0.334500f, -0.438165f, -0.500000f }, + { 0.407043f, -0.320535f, -0.500000f }, + { 0.463823f, -0.189392f, -0.500000f }, + { 0.479209f, -0.023082f, -0.500000f }, + { 0.452467f, 0.148357f, -0.500000f }, + { 0.382133f, 0.302579f, -0.500000f }, + { 0.280853f, 0.429180f, -0.500000f }, + { 0.178835f, 0.531712f, -0.500000f }, + { 0.132904f, 0.457515f, -0.500000f }, + { 0.092272f, 0.362119f, -0.500000f }, + { 0.059590f, 0.230507f, -0.500000f }, + { 0.039274f, 0.091829f, -0.500000f }, + { 0.035741f, -0.005334f, -0.500000f }, + { -0.054356f, -0.005334f, -0.500000f }, + { -0.049939f, 0.075929f, -0.500000f }, + { -0.036690f, 0.184575f, -0.500000f }, + { -0.014607f, 0.311770f, -0.500000f }, + { 0.024258f, 0.435432f, -0.500000f }, + { 0.061357f, 0.532595f, -0.500000f }, + { 0.117888f, 0.611209f, -0.500000f }, + { 0.156753f, 0.654491f, -0.500000f }, + { 0.143503f, 0.726038f, -0.500000f }, + { 0.102872f, 0.784336f, -0.500000f }, + { 0.026025f, 0.832034f, -0.500000f }, + { -0.054356f, 0.837334f, -0.500000f }, + { -0.129436f, 0.807302f, -0.500000f }, + { -0.187734f, 0.742821f, -0.500000f }, + { -0.207167f, 0.660674f, -0.500000f }, + { -0.174484f, 0.571460f, -0.500000f }, + { -0.290197f, 0.458398f, -0.500000f }, + { -0.398335f, 0.315972f, -0.500000f }, + { -0.482736f, 0.141896f, -0.500000f }, + { -0.511748f, -0.037456f, -0.500000f }, + { -0.491674f, -0.191457f, -0.500000f }, + { -0.437898f, -0.324945f, -0.500000f }, + { -0.365704f, -0.436764f, -0.500000f }, + { -0.455874f, -0.460073f, -0.500000f }, + { -0.517828f, -0.487063f, -0.500000f }, + { -0.577941f, -0.546563f, -0.500000f }, + { -0.600052f, -0.670555f, -0.500000f }, + { -0.604886f, -0.818375f, -0.500000f }, + { 0.411411f, -0.543957f, -1.500000f }, + { 0.180380f, -0.452089f, -1.500000f }, + { 0.466089f, -0.576359f, -1.500000f }, + { 0.481290f, -0.602485f, -1.500000f }, + { 0.497165f, -0.685202f, -1.500000f }, + { 0.501751f, -0.748000f, -1.500000f }, + { 0.325265f, -0.291323f, -1.500000f }, + { 0.377679f, -0.177063f, -1.500000f }, + { 0.392465f, -0.029934f, -1.500000f }, + { 0.369663f, 0.120901f, -1.500000f }, + { 0.308541f, 0.256122f, -1.500000f }, + { 0.209282f, 0.388418f, -1.500000f }, + { 0.174547f, 0.334577f, -1.500000f }, + { 0.144549f, 0.213777f, -1.500000f }, + { 0.125532f, 0.083966f, -1.500000f }, + { 0.119140f, -0.091821f, -1.500000f }, + { -0.145671f, -0.091821f, -1.500000f }, + { -0.136141f, 0.083520f, -1.500000f }, + { -0.122276f, 0.197215f, -1.500000f }, + { -0.098837f, 0.332223f, -1.500000f }, + { -0.057469f, 0.463849f, -1.500000f }, + { -0.015428f, 0.573955f, -1.500000f }, + { 0.058744f, 0.679341f, -1.500000f }, + { 0.042113f, 0.720256f, -1.500000f }, + { -0.001227f, 0.747156f, -1.500000f }, + { -0.040460f, 0.749743f, -1.500000f }, + { -0.078716f, 0.734440f, -1.500000f }, + { -0.108683f, 0.701295f, -1.500000f }, + { -0.117024f, 0.666037f, -1.500000f }, + { -0.074035f, 0.548691f, -1.500000f }, + { -0.225181f, 0.401007f, -1.500000f }, + { -0.324259f, 0.270512f, -1.500000f }, + { -0.399383f, 0.115571f, -1.500000f }, + { -0.424164f, -0.037621f, -1.500000f }, + { -0.407556f, -0.169668f, -1.500000f }, + { -0.359987f, -0.286169f, -1.500000f }, + { -0.228055f, -0.450233f, -1.500000f }, + { -0.427604f, -0.542096f, -1.500000f }, + { -0.468471f, -0.559899f, -1.500000f }, + { -0.503422f, -0.594493f, -1.500000f }, + { -0.513465f, -0.685691f, -1.500000f }, + { -0.514719f, -0.748000f, -1.500000f }, + }; + + constexpr face_v faces[] = { + {{21}, {22}, {40}}, + {{67}, {70}, {75}}, + {{ 1}, { 4}, { 5}}, + {{ 5}, { 6}, { 7}}, + {{ 7}, { 8}, { 9}}, + {{ 9}, {10}, {11}}, + {{11}, {12}, {13}}, + {{13}, {14}, {15}}, + {{15}, {16}, {17}}, + {{13}, {15}, {17}}, + {{ 9}, {11}, {13}}, + {{ 5}, { 7}, { 9}}, + {{ 2}, { 1}, { 5}}, + {{ 0}, { 3}, { 2}}, + {{44}, {45}, { 0}}, + {{42}, {43}, {44}}, + {{40}, {41}, {42}}, + {{38}, {39}, {40}}, + {{36}, {37}, {38}}, + {{34}, {35}, {36}}, + {{31}, {32}, {33}}, + {{33}, {34}, {36}}, + {{36}, {38}, {40}}, + {{40}, {42}, {44}}, + {{44}, { 0}, { 2}}, + {{ 5}, { 9}, {13}}, + {{44}, { 2}, { 5}}, + {{33}, {36}, {40}}, + {{40}, {44}, { 5}}, + {{ 5}, {13}, {17}}, + {{ 5}, {17}, {18}}, + {{30}, {31}, {33}}, + {{28}, {29}, {30}}, + {{26}, {27}, {28}}, + {{24}, {25}, {26}}, + {{23}, {24}, {26}}, + {{26}, {28}, {30}}, + {{30}, {33}, {40}}, + {{40}, { 5}, {18}}, + {{40}, {18}, {19}}, + {{26}, {30}, {40}}, + {{23}, {26}, {40}}, + {{40}, {19}, {20}}, + {{40}, {20}, {21}}, + {{22}, {23}, {40}}, + {{48}, {46}, {47}}, + {{47}, {52}, {53}}, + {{53}, {54}, {55}}, + {{55}, {56}, {57}}, + {{57}, {58}, {59}}, + {{59}, {60}, {61}}, + {{57}, {59}, {61}}, + {{53}, {55}, {57}}, + {{49}, {48}, {47}}, + {{47}, {53}, {57}}, + {{47}, {57}, {61}}, + {{50}, {49}, {47}}, + {{87}, {51}, {50}}, + {{85}, {86}, {87}}, + {{83}, {84}, {85}}, + {{80}, {81}, {82}}, + {{82}, {83}, {85}}, + {{85}, {87}, {50}}, + {{47}, {61}, {62}}, + {{85}, {50}, {47}}, + {{79}, {80}, {82}}, + {{82}, {85}, {47}}, + {{82}, {47}, {62}}, + {{78}, {79}, {82}}, + {{76}, {77}, {78}}, + {{73}, {74}, {75}}, + {{75}, {76}, {78}}, + {{78}, {82}, {62}}, + {{78}, {62}, {63}}, + {{72}, {73}, {75}}, + {{78}, {63}, {64}}, + {{78}, {64}, {65}}, + {{71}, {72}, {75}}, + {{75}, {78}, {65}}, + {{75}, {65}, {66}}, + {{70}, {71}, {75}}, + {{68}, {69}, {70}}, + {{75}, {66}, {67}}, + {{67}, {68}, {70}}, + }; + + constexpr uint32_t num_faces = (sizeof (faces)) / (sizeof (face_v)); + +} diff --git a/chess/bishop.mtl b/chess/bishop.mtl new file mode 100644 index 0000000..61df9cf --- /dev/null +++ b/chess/bishop.mtl @@ -0,0 +1,2 @@ +# Blender 3.3.6 MTL File: 'bishop.blend' +# www.blender.org diff --git a/chess/bishop.obj b/chess/bishop.obj new file mode 100644 index 0000000..f4b0ed9 --- /dev/null +++ b/chess/bishop.obj @@ -0,0 +1,176 @@ +# Blender 3.3.6 +# www.blender.org +o Bishop +v 0.584650 -0.818375 -0.500000 +v 0.529532 -0.513422 -0.500000 +v 0.563451 -0.571720 -0.500000 +v 0.583590 -0.676656 -0.500000 +v 0.443675 -0.462544 -0.500000 +v 0.334500 -0.438165 -0.500000 +v 0.407043 -0.320535 -0.500000 +v 0.463823 -0.189392 -0.500000 +v 0.479209 -0.023082 -0.500000 +v 0.452467 0.148357 -0.500000 +v 0.382133 0.302579 -0.500000 +v 0.280853 0.429180 -0.500000 +v 0.178835 0.531712 -0.500000 +v 0.132904 0.457515 -0.500000 +v 0.092272 0.362119 -0.500000 +v 0.059590 0.230507 -0.500000 +v 0.039274 0.091829 -0.500000 +v 0.035741 -0.005334 -0.500000 +v -0.054356 -0.005334 -0.500000 +v -0.049939 0.075929 -0.500000 +v -0.036690 0.184575 -0.500000 +v -0.014607 0.311770 -0.500000 +v 0.024258 0.435432 -0.500000 +v 0.061357 0.532595 -0.500000 +v 0.117888 0.611209 -0.500000 +v 0.156753 0.654491 -0.500000 +v 0.143503 0.726038 -0.500000 +v 0.102872 0.784336 -0.500000 +v 0.026025 0.832034 -0.500000 +v -0.054356 0.837334 -0.500000 +v -0.129436 0.807302 -0.500000 +v -0.187734 0.742821 -0.500000 +v -0.207167 0.660674 -0.500000 +v -0.174484 0.571460 -0.500000 +v -0.290197 0.458398 -0.500000 +v -0.398335 0.315972 -0.500000 +v -0.482736 0.141896 -0.500000 +v -0.511748 -0.037456 -0.500000 +v -0.491674 -0.191457 -0.500000 +v -0.437898 -0.324945 -0.500000 +v -0.365704 -0.436764 -0.500000 +v -0.455874 -0.460073 -0.500000 +v -0.517828 -0.487063 -0.500000 +v -0.577941 -0.546563 -0.500000 +v -0.600052 -0.670555 -0.500000 +v -0.604886 -0.818375 -0.500000 +v 0.411411 -0.543957 -1.500000 +v 0.180380 -0.452089 -1.500000 +v 0.466089 -0.576359 -1.500000 +v 0.481290 -0.602485 -1.500000 +v 0.497165 -0.685202 -1.500000 +v 0.501751 -0.748000 -1.500000 +v 0.325265 -0.291323 -1.500000 +v 0.377679 -0.177063 -1.500000 +v 0.392465 -0.029934 -1.500000 +v 0.369663 0.120901 -1.500000 +v 0.308541 0.256122 -1.500000 +v 0.209282 0.388418 -1.500000 +v 0.174547 0.334577 -1.500000 +v 0.144549 0.213777 -1.500000 +v 0.125532 0.083966 -1.500000 +v 0.119140 -0.091821 -1.500000 +v -0.145671 -0.091821 -1.500000 +v -0.136141 0.083520 -1.500000 +v -0.122276 0.197215 -1.500000 +v -0.098837 0.332223 -1.500000 +v -0.057469 0.463849 -1.500000 +v -0.015428 0.573955 -1.500000 +v 0.058744 0.679341 -1.500000 +v 0.042113 0.720256 -1.500000 +v -0.001227 0.747156 -1.500000 +v -0.040460 0.749743 -1.500000 +v -0.078716 0.734440 -1.500000 +v -0.108683 0.701295 -1.500000 +v -0.117024 0.666037 -1.500000 +v -0.074035 0.548691 -1.500000 +v -0.225181 0.401007 -1.500000 +v -0.324259 0.270512 -1.500000 +v -0.399383 0.115571 -1.500000 +v -0.424164 -0.037621 -1.500000 +v -0.407556 -0.169668 -1.500000 +v -0.359987 -0.286169 -1.500000 +v -0.228055 -0.450233 -1.500000 +v -0.427604 -0.542096 -1.500000 +v -0.468471 -0.559899 -1.500000 +v -0.503422 -0.594493 -1.500000 +v -0.513465 -0.685691 -1.500000 +v -0.514719 -0.748000 -1.500000 +s 0 +f 22 23 41 +f 68 71 76 +f 2 5 6 +f 6 7 8 +f 8 9 10 +f 10 11 12 +f 12 13 14 +f 14 15 16 +f 16 17 18 +f 14 16 18 +f 10 12 14 +f 6 8 10 +f 3 2 6 +f 1 4 3 +f 45 46 1 +f 43 44 45 +f 41 42 43 +f 39 40 41 +f 37 38 39 +f 35 36 37 +f 32 33 34 +f 34 35 37 +f 37 39 41 +f 41 43 45 +f 45 1 3 +f 6 10 14 +f 45 3 6 +f 34 37 41 +f 41 45 6 +f 6 14 18 +f 6 18 19 +f 31 32 34 +f 29 30 31 +f 27 28 29 +f 25 26 27 +f 24 25 27 +f 27 29 31 +f 31 34 41 +f 41 6 19 +f 41 19 20 +f 27 31 41 +f 24 27 41 +f 41 20 21 +f 41 21 22 +f 23 24 41 +f 49 47 48 +f 48 53 54 +f 54 55 56 +f 56 57 58 +f 58 59 60 +f 60 61 62 +f 58 60 62 +f 54 56 58 +f 50 49 48 +f 48 54 58 +f 48 58 62 +f 51 50 48 +f 88 52 51 +f 86 87 88 +f 84 85 86 +f 81 82 83 +f 83 84 86 +f 86 88 51 +f 48 62 63 +f 86 51 48 +f 80 81 83 +f 83 86 48 +f 83 48 63 +f 79 80 83 +f 77 78 79 +f 74 75 76 +f 76 77 79 +f 79 83 63 +f 79 63 64 +f 73 74 76 +f 79 64 65 +f 79 65 66 +f 72 73 76 +f 76 79 66 +f 76 66 67 +f 71 72 76 +f 69 70 71 +f 76 67 68 +f 68 69 71 diff --git a/chess/board.FCStd b/chess/board.FCStd new file mode 100644 index 0000000..be21262 Binary files /dev/null and b/chess/board.FCStd differ diff --git a/chess/chess.cpp b/chess/chess.cpp new file mode 100644 index 0000000..2386b7f --- /dev/null +++ b/chess/chess.cpp @@ -0,0 +1,446 @@ +#include + +#include "chess.hpp" +#include "sh7091/serial.hpp" +namespace chess { + +xy position_to_xy(int8_t position) +{ + int8_t y = (position >> 4) & 0xf; + int8_t x = (position >> 0) & 0xf; + return {x, y}; +} + +int8_t xy_to_position(int8_t x, int8_t y) +{ + return ((y & 0xf) << 4) + | ((x & 0xf) << 0); +} + +bool xy_inside_board(int8_t x, int8_t y) +{ + int8_t position = xy_to_position(x, y); + return (position & 0x88) == 0; +} + +int8_t piece_list_to_position(const chess::board_state& board_state, + chess::piece * piece) +{ + int8_t position = piece - &board_state.board[0]; + return position; +} + +constexpr inline bool turn_same_side(int8_t side, int8_t type) +{ + return (type != 0) && ((side > 0) == (type > 0)); +} + +bool speculative_check(game_state& game_state, int8_t from_position, int8_t to_position, int8_t side); + +template +static move_t * moves_line(game_state& game_state, int8_t origin, move_t * moves, int dx, int dy) +{ + board_state& board = game_state.board; + piece& self = board.board[origin]; + auto [ox, oy] = position_to_xy(origin); + int8_t x = ox, y = oy; + for (int limit = 0; limit < max_limit; limit++) { + x += dx; + y += dy; + if (xy_inside_board(x, y)) { + piece& other = board.board[xy_to_position(x, y)]; + bool different_sides = ((self.type ^ other.type) < 0); + if (other.type == 0 || different_sides) { + int8_t to_position = xy_to_position(x, y); + if (!speculative_check(game_state, origin, to_position, self.type)) + *moves++ = {move_type::normal, to_position}; + } + if (other.type != 0) + break; + } else { + break; + } + } + return moves; +} + +move_t * moves_pawn(game_state& game_state, int8_t origin, move_t * moves) +{ + board_state& board = game_state.board; + piece& self = board.board[origin]; + int8_t direction = (self.type > 0) ? 1 : -1; + + auto [ox, oy] = position_to_xy(origin); + int advance = 1; + auto type = move_type::normal; + do { + int8_t x = ox; + int8_t y = oy + direction * advance; + if (xy_inside_board(x, y)) { + piece& other = board.board[xy_to_position(x, y)]; + if (other.type == 0) { + int8_t to_position = xy_to_position(x, y); + if (!speculative_check(game_state, origin, to_position, self.type)) { + *moves++ = {type, to_position}; + } + } else { + break; + } + } + advance += 1; + type = move_type::pawn_double_advance; + } while (self.movement == movement_state::not_moved && advance <= 2); + + int8_t diagonal = 1; + for (int i = 0; i < 2; i++) { + int8_t x = ox + diagonal; + int8_t y = oy + direction; + + if (xy_inside_board(x, y)) { + piece& other = board.board[xy_to_position(x, y)]; + bool different_sides = ((self.type ^ other.type) < 0); + if (other.type != 0 && different_sides) { + int8_t to_position = xy_to_position(x, y); + if (!speculative_check(game_state, origin, to_position, self.type)) + *moves++ = {move_type::normal, to_position}; + } + if (game_state.en_passant_target != -1 && xy_to_position(x, oy) == game_state.en_passant_target) { + // en_passant_target is always the opposite side + int8_t to_position = xy_to_position(x, y); + if (!speculative_check(game_state, origin, to_position, self.type)) + *moves++ = {move_type::pawn_en_passant_capture, to_position}; + } + } + diagonal = -diagonal; + } + + return moves; +} + +move_t * moves_knight(game_state& game_state, int8_t origin, move_t * moves) +{ + board_state& board = game_state.board; + piece& self = board.board[origin]; + xy delta[8] = { + { 2, -1}, // up, left + { 2, 1}, // up, right + {-2, -1}, // down, left + {-2, 1}, // down, right + { 1, -2}, // left, up + { 1, 2}, // right, up + {-1, -2}, // left, down + {-1, 2}, // right, down + }; + auto [ox, oy] = position_to_xy(origin); + for (int i = 0; i < 8; i++) { + uint8_t x = ox + delta[i].x; + uint8_t y = oy + delta[i].y; + if (xy_inside_board(x, y)) { + piece& other = board.board[xy_to_position(x, y)]; + bool different_sides = ((self.type ^ other.type) < 0); + if (other.type == 0 || different_sides) { + int8_t to_position = xy_to_position(x, y); + if (!speculative_check(game_state, origin, to_position, self.type)) + *moves++ = {move_type::normal, to_position}; + } + } + } + return moves; +} + +move_t * moves_bishop(game_state& game_state, int8_t origin, move_t * moves) +{ + // up-left + moves = moves_line<8>(game_state, origin, moves, 1, 1); + // up-right + moves = moves_line<8>(game_state, origin, moves, -1, 1); + // down-left + moves = moves_line<8>(game_state, origin, moves, 1, -1); + // down-right + moves = moves_line<8>(game_state, origin, moves, -1, -1); + + return moves; +} + +move_t * moves_rook(game_state& game_state, int8_t origin, move_t * moves) +{ + // up + moves = moves_line<8>(game_state, origin, moves, 0, 1); + // down + moves = moves_line<8>(game_state, origin, moves, 0, -1); + // left + moves = moves_line<8>(game_state, origin, moves, 1, 0); + // right + moves = moves_line<8>(game_state, origin, moves, -1, 0); + + return moves; +} + +move_t * moves_queen(game_state& game_state, int8_t origin, move_t * moves) +{ + moves = moves_bishop(game_state, origin, moves); + moves = moves_rook(game_state, origin, moves); + + return moves; +} + +move_t * moves_king(game_state& game_state, int8_t origin, move_t * moves) +{ + // up-left + moves = moves_line<1>(game_state, origin, moves, 1, 1); + // up-right + moves = moves_line<1>(game_state, origin, moves, -1, 1); + // down-left + moves = moves_line<1>(game_state, origin, moves, 1, -1); + // down-right + moves = moves_line<1>(game_state, origin, moves, -1, -1); + // up + moves = moves_line<1>(game_state, origin, moves, 0, 1); + // down + moves = moves_line<1>(game_state, origin, moves, 0, -1); + // left + moves = moves_line<1>(game_state, origin, moves, 1, 0); + // right + moves = moves_line<1>(game_state, origin, moves, -1, 0); + + // castle + board_state& board = game_state.board; + piece& self = board.board[origin]; + if (self.movement == movement_state::not_moved && !speculative_check(game_state, origin, origin, self.type)) { + int direction = 1; + int distance = 2; + move_type type = move_type::castle_short; + auto [ox, oy] = position_to_xy(origin); + do { + int8_t rook_position = xy_to_position(direction == 1 ? 7 : 0, oy); + piece& rook = board.board[rook_position]; + if (__builtin_abs(rook.type) == piece_type::rook && rook.movement == movement_state::not_moved) { + move_t * castle_moves = moves_line<8>(game_state, origin, moves, direction, 0); + if (castle_moves - moves == distance) { + int8_t to_position = xy_to_position(ox + (direction * 2), oy); + *moves++ = {type, to_position}; + } + } + distance += 1; + direction = -direction; + type = move_type::castle_long; + } while (direction == -1); + } + + return moves; +} + +int moves_position(game_state& game_state, int8_t origin, move_t * moves) +{ + board_state& board = game_state.board; + piece& self = board.board[origin]; + switch (__builtin_abs(self.type)) { + case piece_type::pawn: return moves_pawn(game_state, origin, moves) - moves; + case piece_type::knight: return moves_knight(game_state, origin, moves) - moves; + case piece_type::bishop: return moves_bishop(game_state, origin, moves) - moves; + case piece_type::rook: return moves_rook(game_state, origin, moves) - moves; + case piece_type::queen: return moves_queen(game_state, origin, moves) - moves; + case piece_type::king: return moves_king(game_state, origin, moves) - moves; + default: return 0; + } +} + +static void board_init(game_state& game_state) +{ + using namespace piece_type; + + constexpr int8_t types[8][8] = { + { rook , knight, bishop, queen , king , bishop, knight, rook }, // white + { pawn , pawn , pawn , pawn , pawn , pawn , pawn , pawn }, // white + { empty , empty , empty , empty , empty , empty , empty , empty }, + { empty , empty , empty , empty , empty , empty , empty , empty }, + { empty , empty , empty , empty , empty , empty , empty , empty }, + { empty , empty , empty , empty , empty , empty , empty , empty }, + {-pawn , -pawn , -pawn , -pawn , -pawn , -pawn , -pawn , -pawn }, // black + {-rook , -knight, -bishop, -queen , -king , -bishop, -knight, -rook }, // black + }; + + game_state.piece_list.length = 0; + for (int y = 0; y < 8; y++) { + for (int x = 0; x < 8; x++) { + int8_t type = types[y][x]; + int8_t position = xy_to_position(x, y); + piece& p = game_state.board.board[position]; + p.type = type; + p.movement = movement_state::not_moved; + p.piece_list_offset = game_state.piece_list.length; + switch (__builtin_abs(type)) { + case empty: break; + default: + game_state.piece_list.piece[game_state.piece_list.length++] = &p; + break; + } + } + } +} + +void game_init(game_state& game_state) +{ + game_state.turn = -1; // white + game_state.en_passant_target = -1; + game_state.halfmove_number = 0; + game_state.fullmove_number = 0; + game_state.interaction.selected_position = -1; + board_init(game_state); +} + +int position_in_moves(moves_list& moves, int8_t position) +{ + for (int i = 0; i < moves.length; i++) { + int8_t move_position = moves.moves[i].to_position; + if (move_position == position) { + return i; + } + } + return -1; +} + +void piece_list_delete(piece_list& list, int8_t offset) +{ + for (int i = offset + 1; i < list.length; i++) { + list.piece[i]->piece_list_offset = i - 1; + list.piece[i - 1] = list.piece[i]; + } + list.length -= 1; +} + +static int _speculative_check_depth = 0; + +bool speculative_check(game_state& game_state, int8_t from_position, int8_t to_position, int8_t side) +{ + if (_speculative_check_depth > 0) + return false; + _speculative_check_depth++; + + piece& origin = game_state.board.board[from_position]; + piece& destination = game_state.board.board[to_position]; + const int8_t origin_type = origin.type; + const int8_t destination_type = destination.type; + + origin.type = piece_type::empty; + destination.type = origin_type; + + bool check = false; + + for (int i = 0; i < game_state.piece_list.length; i++) { + if (i == origin.piece_list_offset) { + continue; + } + piece * test_piece = game_state.piece_list.piece[i]; + if (turn_same_side(side, test_piece->type)) { + continue; + } + int8_t position = piece_list_to_position(game_state.board, test_piece); + moves_list ml; + ml.length = moves_position(game_state, position, ml.moves); + for (int j = 0; j < ml.length; j++) { + int8_t move_position = ml.moves[j].to_position; + piece& move_piece = game_state.board.board[move_position]; + if (__builtin_abs(move_piece.type) == piece_type::king) { + serial::string("check\n"); + check = true; + goto exit; + } + } + } + + exit: + origin.type = origin_type; + destination.type = destination_type; + + _speculative_check_depth--; + + return check; +} + +void do_move(game_state& game_state, int8_t from_position, move_t& move) +{ + piece& origin = game_state.board.board[from_position]; + piece& destination = game_state.board.board[move.to_position]; + + if (destination.type != 0) { // capture + piece_list_delete(game_state.piece_list, destination.piece_list_offset); + } + + int8_t en_passant_target = game_state.en_passant_target; + game_state.en_passant_target = -1; + + switch (move.type) { + case move_type::pawn_double_advance: + serial::string("pawn double advance\n"); + game_state.en_passant_target = move.to_position; + break; + case move_type::castle_short: + serial::string("move castle short\n"); + { + auto [ox, oy] = position_to_xy(move.to_position); + int8_t rook_from = xy_to_position(7, oy); + move_t rook_move = {move_type::normal, xy_to_position(5, oy)}; + do_move(game_state, rook_from, rook_move); + } + break; + case move_type::castle_long: + serial::string("move castle long\n"); + { + auto [ox, oy] = position_to_xy(move.to_position); + int8_t rook_from = xy_to_position(0, oy); + move_t rook_move = {move_type::normal, xy_to_position(3, oy)}; + do_move(game_state, rook_from, rook_move); + } + break; + case move_type::pawn_en_passant_capture: + { + serial::string("move en passant capture\n"); + piece& en_passant_piece = game_state.board.board[en_passant_target]; + serial::integer(en_passant_target); + serial::integer(en_passant_piece.type); + en_passant_piece.type = piece_type::empty; + piece_list_delete(game_state.piece_list, en_passant_piece.piece_list_offset); + } + break; + default: break; + } + + destination.type = origin.type; + destination.movement = true; + destination.piece_list_offset = origin.piece_list_offset; + origin.type = piece_type::empty; + game_state.piece_list.piece[origin.piece_list_offset] = &destination; + + game_state.turn = -game_state.turn; +} + +void select_position(game_state& game_state, int8_t x, int8_t y) +{ + int8_t position = xy_to_position(x, y); + + if (turn_same_side(game_state.turn, game_state.board.board[position].type)) { + if (game_state.interaction.selected_position != position) { + game_state.interaction.selected_position = position; + game_state.interaction.moves.length = moves_position(game_state, + position, + game_state.interaction.moves.moves); + return; + } + } else { // not same side or empty + if (game_state.interaction.selected_position != -1) { + int moves_ix = position_in_moves(game_state.interaction.moves, position); + if (moves_ix >= 0) { + do_move(game_state, + game_state.interaction.selected_position, // from + game_state.interaction.moves.moves[moves_ix]); // to + // fall through to deselect + } + } + } + + game_state.interaction.selected_position = -1; + game_state.interaction.moves.length = 0; +} + +} diff --git a/chess/chess.hpp b/chess/chess.hpp new file mode 100644 index 0000000..eecc96e --- /dev/null +++ b/chess/chess.hpp @@ -0,0 +1,82 @@ +#pragma once + +#include + +namespace chess { + +namespace piece_type { +constexpr int8_t empty = 0; +constexpr int8_t pawn = 1; +constexpr int8_t knight = 2; +constexpr int8_t bishop = 3; +constexpr int8_t rook = 4; +constexpr int8_t queen = 5; +constexpr int8_t king = 6; +}; + +namespace movement_state { +constexpr int8_t not_moved = 0; +constexpr int8_t moved = 1; +} + +struct xy { + int8_t x; + int8_t y; +}; + +struct piece { + int8_t type; // piece_type + int8_t movement; // movement_state + int8_t piece_list_offset; +}; + +struct piece_list { + struct piece * piece[8 * 8]; + int32_t length; +}; + +struct board_state { + struct piece board[8 * 16]; +}; + +enum struct move_type : int8_t { + normal, + castle_short, + castle_long, + pawn_double_advance, + pawn_en_passant_capture, +}; + +struct move_t { + enum move_type type; + int8_t to_position; +}; + +struct moves_list { + move_t moves[27]; + int8_t length; +}; + +struct interaction_state { + int8_t selected_position; + struct moves_list moves; +}; + +struct game_state { + struct board_state board; + struct piece_list piece_list; + struct interaction_state interaction; + int8_t turn; + int8_t en_passant_target; // index into board_state.board + uint16_t halfmove_number; + uint16_t fullmove_number; +}; + +xy position_to_xy(int8_t position); +int8_t xy_to_position(int8_t x, int8_t y); +void game_init(game_state& game_state); +void select_position(game_state& game_state, int8_t x, int8_t y); +int8_t piece_list_to_position(const chess::board_state& board_state, + chess::piece * piece); + +} diff --git a/chess/chess.mk b/chess/chess.mk new file mode 100644 index 0000000..052f496 --- /dev/null +++ b/chess/chess.mk @@ -0,0 +1,33 @@ +CHESS_OBJ = \ + chess/main.o \ + chess/render.o \ + chess/chess.o \ + chess/input.o \ + holly/video_output.o \ + holly/core.o \ + holly/region_array.o \ + holly/background.o \ + holly/ta_fifo_polygon_converter.o \ + maple/maple.o \ + sh7091/serial.o \ + $(LIBGCC) + +CHESS_GEOMETRY = \ + chess/bishop.hpp \ + chess/king.hpp \ + chess/knight.hpp \ + chess/pawn.hpp \ + chess/queen.hpp \ + chess/rook.hpp \ + chess/square.hpp \ + chess/circle.hpp \ + chess/cursor.hpp + +chess/render.o: chess/render.cpp $(CHESS_GEOMETRY) + +chess/chess.elf: LDSCRIPT = $(LIB)/main.lds +chess/chess.elf: $(START_OBJ) $(CHESS_OBJ) + +chess/%.hpp: chess/%.obj tools/obj_to_cpp.py + PYTHONPATH=regs/gen python tools/obj_to_cpp.py $< > $@.tmp + mv $@.tmp $@ diff --git a/chess/circle.blend b/chess/circle.blend new file mode 100644 index 0000000..852985b Binary files /dev/null and b/chess/circle.blend differ diff --git a/chess/circle.hpp b/chess/circle.hpp new file mode 100644 index 0000000..09e159f --- /dev/null +++ b/chess/circle.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include "geometry/geometry.hpp" + +namespace circle { + constexpr vec3 vertices[] = { + { 0.000000f, 0.000000f, 0.000000f }, + { 0.000000f, -1.000000f, 0.000000f }, + { -0.361242f, -0.932472f, 0.000000f }, + { -0.673696f, -0.739009f, 0.000000f }, + { -0.895163f, -0.445738f, 0.000000f }, + { -0.995734f, -0.092268f, 0.000000f }, + { -0.961826f, 0.273663f, -0.000000f }, + { -0.798017f, 0.602635f, -0.000000f }, + { -0.526432f, 0.850217f, -0.000000f }, + { -0.183750f, 0.982973f, -0.000000f }, + { 0.183750f, 0.982973f, -0.000000f }, + { 0.526432f, 0.850217f, -0.000000f }, + { 0.798017f, 0.602635f, -0.000000f }, + { 0.961826f, 0.273663f, -0.000000f }, + { 0.995734f, -0.092268f, 0.000000f }, + { 0.895163f, -0.445738f, 0.000000f }, + { 0.673696f, -0.739009f, 0.000000f }, + { 0.361242f, -0.932472f, 0.000000f }, + }; + + constexpr face_v faces[] = { + {{ 0}, { 1}, { 2}}, + {{ 0}, { 2}, { 3}}, + {{ 0}, { 3}, { 4}}, + {{ 0}, { 4}, { 5}}, + {{ 0}, { 5}, { 6}}, + {{ 0}, { 6}, { 7}}, + {{ 0}, { 7}, { 8}}, + {{ 0}, { 8}, { 9}}, + {{ 0}, { 9}, {10}}, + {{ 0}, {10}, {11}}, + {{ 0}, {11}, {12}}, + {{ 0}, {12}, {13}}, + {{ 0}, {13}, {14}}, + {{ 0}, {14}, {15}}, + {{ 0}, {15}, {16}}, + {{ 0}, {16}, {17}}, + {{ 0}, {17}, { 1}}, + }; + + constexpr uint32_t num_faces = (sizeof (faces)) / (sizeof (face_v)); + +} diff --git a/chess/circle.obj b/chess/circle.obj new file mode 100644 index 0000000..2372ee8 --- /dev/null +++ b/chess/circle.obj @@ -0,0 +1,39 @@ +# Blender 3.3.6 +# www.blender.org +o Circle +v 0.000000 0.000000 0.000000 +v 0.000000 -1.000000 0.000000 +v -0.361242 -0.932472 0.000000 +v -0.673696 -0.739009 0.000000 +v -0.895163 -0.445738 0.000000 +v -0.995734 -0.092268 0.000000 +v -0.961826 0.273663 -0.000000 +v -0.798017 0.602635 -0.000000 +v -0.526432 0.850217 -0.000000 +v -0.183750 0.982973 -0.000000 +v 0.183750 0.982973 -0.000000 +v 0.526432 0.850217 -0.000000 +v 0.798017 0.602635 -0.000000 +v 0.961826 0.273663 -0.000000 +v 0.995734 -0.092268 0.000000 +v 0.895163 -0.445738 0.000000 +v 0.673696 -0.739009 0.000000 +v 0.361242 -0.932472 0.000000 +s 0 +f 1 2 3 +f 1 3 4 +f 1 4 5 +f 1 5 6 +f 1 6 7 +f 1 7 8 +f 1 8 9 +f 1 9 10 +f 1 10 11 +f 1 11 12 +f 1 12 13 +f 1 13 14 +f 1 14 15 +f 1 15 16 +f 1 16 17 +f 1 17 18 +f 1 18 2 diff --git a/chess/cursor.blend b/chess/cursor.blend new file mode 100644 index 0000000..ceeace3 Binary files /dev/null and b/chess/cursor.blend differ diff --git a/chess/cursor.hpp b/chess/cursor.hpp new file mode 100644 index 0000000..78311ac --- /dev/null +++ b/chess/cursor.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "geometry/geometry.hpp" + +namespace cursor { + constexpr vec3 vertices[] = { + { 0.407995f, -0.805648f, 2.000000f }, + { 0.000000f, 0.000000f, 2.000000f }, + { 0.550770f, -0.527772f, 2.000000f }, + { 0.302290f, -0.556623f, 2.000000f }, + { 0.297535f, -0.852536f, 2.000000f }, + { 0.000000f, -0.762211f, 2.000000f }, + { 0.191836f, -0.603526f, 2.000000f }, + }; + + constexpr face_v faces[] = { + {{1}, {6}, {0}}, + {{3}, {2}, {1}}, + {{1}, {5}, {6}}, + {{6}, {4}, {0}}, + {{0}, {3}, {1}}, + }; + + constexpr uint32_t num_faces = (sizeof (faces)) / (sizeof (face_v)); + +} diff --git a/chess/cursor.obj b/chess/cursor.obj new file mode 100644 index 0000000..870e10a --- /dev/null +++ b/chess/cursor.obj @@ -0,0 +1,16 @@ +# Blender 3.3.6 +# www.blender.org +o Cursor +v 0.407995 -0.805648 2.000000 +v 0.000000 0.000000 2.000000 +v 0.550770 -0.527772 2.000000 +v 0.302290 -0.556623 2.000000 +v 0.297535 -0.852536 2.000000 +v 0.000000 -0.762211 2.000000 +v 0.191836 -0.603526 2.000000 +s 0 +f 2 7 1 +f 4 3 2 +f 2 6 7 +f 7 5 1 +f 1 4 2 diff --git a/chess/input.cpp b/chess/input.cpp new file mode 100644 index 0000000..ca4bc22 --- /dev/null +++ b/chess/input.cpp @@ -0,0 +1,108 @@ +#include + +#include "maple/maple.hpp" +#include "maple/maple_port.hpp" +#include "maple/maple_bus_bits.hpp" +#include "maple/maple_bus_commands.hpp" +#include "maple/maple_bus_ft0.hpp" +#include "maple/maple_bus_ft9.hpp" +#include "maple/maple_host_command_writer.hpp" + +#include "sh7091/serial.hpp" + +#include "input.hpp" + +namespace input { + +struct input_state state; + +void do_get_condition_controller(maple::host_command_writer& writer, + const uint32_t port) +{ + using command_type = maple::get_condition; + using response_type = maple::data_transfer; + + uint32_t host_port_select = host_instruction_port_select(port); + uint32_t destination_ap = ap_port_select(port) | ap::de::device; + + auto [host_command, host_response] + = writer.append_command(host_port_select, + destination_ap, + false); // end_flag + + host_command->bus_data.data_fields.function_type = std::byteswap(function_type::controller); + + state.port[port].host_response_data_transfer_ft0 = host_response; +} + +void do_get_condition_pointing(maple::host_command_writer& writer, + const uint32_t port) +{ + using command_type = maple::get_condition; + using response_type = maple::data_transfer; + + uint32_t host_port_select = host_instruction_port_select(port); + uint32_t destination_ap = ap_port_select(port) | ap::de::device; + + auto [host_command, host_response] + = writer.append_command(host_port_select, + destination_ap, + false); // end_flag + + host_command->bus_data.data_fields.function_type = std::byteswap(function_type::pointing); + + state.port[port].host_response_data_transfer_ft9 = host_response; +} + +maple::host_response * do_device_request(maple::host_command_writer& writer) +{ + using command_type = maple::device_request; + using response_type = maple::device_status; + + auto [host_command, host_response] + = writer.append_command_all_ports(); + + return host_response; +} + +void state_update(uint32_t * send_buf, uint32_t * recv_buf) +{ + maple::host_command_writer writer(send_buf, recv_buf); + + for (uint8_t port = 0; port < 4; port++) { + state.port[port].function_type = state.port[port].next_function_type; + + if (state.port[port].function_type & function_type::controller) { + do_get_condition_controller(writer, port); + } + + if (state.port[port].function_type & function_type::pointing) { + do_get_condition_pointing(writer, port); + } + } + auto host_response_device_status = do_device_request(writer); + + maple::dma_start(writer.send_buf, writer.send_offset, + writer.recv_buf, writer.recv_offset); + + for (uint8_t port = 0; port < 4; port++) { + auto& bus_data = host_response_device_status[port].bus_data; + auto& data_fields = bus_data.data_fields; + if (bus_data.command_code != maple::device_status::command_code) { + state.port[port].function_type = 0; + state.port[port].next_function_type = 0; + continue; + } else { + state.port[port].next_function_type = std::byteswap(data_fields.device_id.ft); + } + } +} + +void state_init() +{ + for (uint8_t port = 0; port < 4; port++) { + state.port[port].function_type = 0; + } +} + +} diff --git a/chess/input.hpp b/chess/input.hpp new file mode 100644 index 0000000..19bf60d --- /dev/null +++ b/chess/input.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "maple/maple.hpp" +#include "maple/maple_bus_commands.hpp" +#include "maple/maple_bus_ft0.hpp" +#include "maple/maple_bus_ft9.hpp" + +namespace input { + +struct port_state { + uint32_t function_type; + uint32_t next_function_type; + maple::host_response::data_fields> * host_response_data_transfer_ft0; + maple::host_response::data_fields> * host_response_data_transfer_ft9; +}; + +struct input_state { + port_state port[4]; +}; + +extern input_state state; + +void state_update(uint32_t * send_buf, uint32_t * recv_buf); +void state_init(); + +} diff --git a/chess/king.blend b/chess/king.blend new file mode 100644 index 0000000..175901c Binary files /dev/null and b/chess/king.blend differ diff --git a/chess/king.hpp b/chess/king.hpp new file mode 100644 index 0000000..3058dab --- /dev/null +++ b/chess/king.hpp @@ -0,0 +1,250 @@ +#pragma once + +#include "geometry/geometry.hpp" + +namespace king { + constexpr vec3 vertices[] = { + { 0.587722f, -0.812159f, -0.500000f }, + { 0.508186f, -0.497465f, -0.500000f }, + { 0.569728f, -0.561184f, -0.500000f }, + { 0.590376f, -0.644625f, -0.500000f }, + { 0.451429f, -0.468419f, -0.500000f }, + { 0.713971f, -0.169361f, -0.500000f }, + { 0.769329f, -0.073675f, -0.500000f }, + { 0.787068f, 0.014243f, -0.500000f }, + { 0.781478f, 0.126337f, -0.500000f }, + { 0.743187f, 0.238086f, -0.500000f }, + { 0.681645f, 0.323805f, -0.500000f }, + { 0.604056f, 0.381043f, -0.500000f }, + { 0.502300f, 0.423017f, -0.500000f }, + { 0.406904f, 0.435736f, -0.500000f }, + { 0.274621f, 0.424289f, -0.500000f }, + { 0.163962f, 0.386130f, -0.500000f }, + { 0.143610f, 0.464991f, -0.500000f }, + { 0.263174f, 0.464991f, -0.500000f }, + { 0.261902f, 0.705390f, -0.500000f }, + { 0.134707f, 0.706662f, -0.500000f }, + { 0.133435f, 0.837673f, -0.500000f }, + { -0.587722f, -0.812159f, -0.500000f }, + { -0.508186f, -0.497465f, -0.500000f }, + { -0.569728f, -0.561184f, -0.500000f }, + { -0.590376f, -0.644625f, -0.500000f }, + { -0.451429f, -0.468419f, -0.500000f }, + { -0.713971f, -0.169361f, -0.500000f }, + { -0.769329f, -0.073675f, -0.500000f }, + { -0.787068f, 0.014243f, -0.500000f }, + { -0.781478f, 0.126337f, -0.500000f }, + { -0.743187f, 0.238086f, -0.500000f }, + { -0.681645f, 0.323805f, -0.500000f }, + { -0.604056f, 0.381043f, -0.500000f }, + { -0.502300f, 0.423017f, -0.500000f }, + { -0.406904f, 0.435736f, -0.500000f }, + { -0.274621f, 0.424289f, -0.500000f }, + { -0.163962f, 0.386130f, -0.500000f }, + { -0.143610f, 0.464991f, -0.500000f }, + { -0.263174f, 0.464991f, -0.500000f }, + { -0.261902f, 0.705390f, -0.500000f }, + { -0.134707f, 0.706662f, -0.500000f }, + { -0.133435f, 0.837673f, -0.500000f }, + { -0.232004f, -0.196898f, -0.500000f }, + { -0.186214f, -0.196898f, -0.500000f }, + { 0.186214f, -0.196898f, -0.500000f }, + { 0.232004f, -0.196898f, -0.500000f }, + { 0.187740f, 0.064106f, -0.500000f }, + { 0.361743f, 0.103791f, -0.500000f }, + { 0.286952f, 0.109897f, -0.500000f }, + { 0.416691f, 0.025948f, -0.500000f }, + { 0.398375f, -0.019843f, -0.500000f }, + { 0.392270f, 0.074791f, -0.500000f }, + { -0.416691f, 0.025948f, -0.500000f }, + { -0.398375f, -0.019843f, -0.500000f }, + { -0.392270f, 0.074791f, -0.500000f }, + { -0.286952f, 0.109897f, -0.500000f }, + { -0.361743f, 0.103791f, -0.500000f }, + { -0.187740f, 0.064106f, -0.500000f }, + { -0.625504f, 0.268384f, -1.500000f }, + { -0.565495f, 0.312652f, -1.500000f }, + { 0.461141f, -0.560928f, -1.500000f }, + { 0.499116f, -0.600247f, -1.500000f }, + { 0.511019f, -0.734231f, -1.500000f }, + { 0.512298f, -0.653515f, -1.500000f }, + { 0.326852f, -0.492204f, -1.500000f }, + { 0.650355f, -0.123707f, -1.500000f }, + { 0.695471f, -0.045723f, -1.500000f }, + { 0.708752f, 0.020099f, -1.500000f }, + { 0.704195f, 0.111476f, -1.500000f }, + { 0.673189f, 0.201964f, -1.500000f }, + { 0.625504f, 0.268384f, -1.500000f }, + { 0.565495f, 0.312652f, -1.500000f }, + { 0.481954f, 0.347113f, -1.500000f }, + { 0.405092f, 0.357361f, -1.500000f }, + { 0.290927f, 0.347481f, -1.500000f }, + { 0.109592f, 0.284952f, -1.500000f }, + { 0.043020f, 0.542919f, -1.500000f }, + { 0.184833f, 0.542919f, -1.500000f }, + { 0.184381f, 0.628234f, -1.500000f }, + { 0.057525f, 0.629502f, -1.500000f }, + { 0.056260f, 0.759745f, -1.500000f }, + { -0.511019f, -0.734231f, -1.500000f }, + { -0.461141f, -0.560928f, -1.500000f }, + { -0.499116f, -0.600247f, -1.500000f }, + { -0.512298f, -0.653515f, -1.500000f }, + { -0.326852f, -0.492204f, -1.500000f }, + { -0.650355f, -0.123707f, -1.500000f }, + { -0.695471f, -0.045723f, -1.500000f }, + { -0.708752f, 0.020099f, -1.500000f }, + { -0.704195f, 0.111476f, -1.500000f }, + { -0.673189f, 0.201964f, -1.500000f }, + { -0.481954f, 0.347113f, -1.500000f }, + { -0.405092f, 0.357361f, -1.500000f }, + { -0.290927f, 0.347481f, -1.500000f }, + { -0.109592f, 0.284952f, -1.500000f }, + { -0.043020f, 0.542919f, -1.500000f }, + { -0.184833f, 0.542919f, -1.500000f }, + { -0.184381f, 0.628234f, -1.500000f }, + { -0.057525f, 0.629502f, -1.500000f }, + { -0.056260f, 0.759745f, -1.500000f }, + { -0.265712f, -0.274826f, -1.500000f }, + { -0.107829f, -0.274826f, -1.500000f }, + { 0.107829f, -0.274826f, -1.500000f }, + { 0.265712f, -0.274826f, -1.500000f }, + { 0.110104f, 0.114101f, -1.500000f }, + { 0.395484f, 0.179223f, -1.500000f }, + { 0.272886f, 0.189231f, -1.500000f }, + { 0.502042f, 0.029498f, -1.500000f }, + { 0.465256f, -0.062466f, -1.500000f }, + { 0.455855f, 0.121871f, -1.500000f }, + { -0.465256f, -0.062466f, -1.500000f }, + { -0.502042f, 0.029498f, -1.500000f }, + { -0.455855f, 0.121871f, -1.500000f }, + { -0.272886f, 0.189231f, -1.500000f }, + { -0.395484f, 0.179223f, -1.500000f }, + { -0.110104f, 0.114101f, -1.500000f }, + }; + + constexpr face_v faces[] = { + {{ 71}, { 75}, {105}}, + {{ 92}, {114}, { 94}}, + {{ 76}, { 80}, { 95}}, + {{ 4}, { 45}, { 42}}, + {{ 33}, { 55}, { 36}}, + {{ 13}, { 15}, { 48}}, + {{ 16}, { 40}, { 37}}, + {{109}, {107}, { 67}}, + {{ 67}, { 68}, { 69}}, + {{ 69}, { 70}, { 71}}, + {{ 71}, { 72}, { 73}}, + {{ 73}, { 74}, { 75}}, + {{ 75}, {106}, {105}}, + {{105}, {109}, { 67}}, + {{ 67}, { 69}, { 71}}, + {{ 71}, { 73}, { 75}}, + {{105}, { 67}, { 71}}, + {{ 94}, { 93}, { 92}}, + {{ 92}, { 91}, {114}}, + {{114}, {113}, { 94}}, + {{ 85}, { 82}, { 83}}, + {{ 83}, { 84}, { 81}}, + {{ 81}, { 62}, { 63}}, + {{ 63}, { 61}, { 60}}, + {{ 64}, { 65}, { 66}}, + {{ 63}, { 60}, { 64}}, + {{ 83}, { 81}, { 63}}, + {{ 87}, { 86}, { 85}}, + {{ 85}, { 83}, { 63}}, + {{ 64}, { 66}, { 67}}, + {{ 85}, { 63}, { 64}}, + {{ 88}, { 87}, { 85}}, + {{ 90}, { 89}, { 88}}, + {{ 59}, { 58}, { 90}}, + {{114}, { 91}, { 59}}, + {{112}, {114}, { 59}}, + {{ 59}, { 90}, { 88}}, + {{ 64}, { 67}, {107}}, + {{ 64}, {107}, {108}}, + {{112}, { 59}, { 88}}, + {{111}, {112}, { 88}}, + {{ 64}, {108}, {103}}, + {{ 64}, {103}, {102}}, + {{111}, { 88}, { 85}}, + {{110}, {111}, { 85}}, + {{ 85}, { 64}, {102}}, + {{104}, {106}, { 75}}, + {{100}, {110}, { 85}}, + {{101}, {100}, { 85}}, + {{102}, {104}, { 75}}, + {{ 76}, { 77}, { 78}}, + {{102}, { 75}, { 76}}, + {{101}, { 85}, {102}}, + {{ 94}, {113}, {115}}, + {{115}, {101}, {102}}, + {{ 76}, { 78}, { 79}}, + {{115}, {102}, { 76}}, + {{ 95}, { 94}, {115}}, + {{ 97}, { 96}, { 95}}, + {{ 80}, { 99}, { 98}}, + {{ 98}, { 97}, { 95}}, + {{ 95}, {115}, { 76}}, + {{ 76}, { 79}, { 80}}, + {{ 80}, { 98}, { 95}}, + {{ 32}, { 31}, { 30}}, + {{ 30}, { 29}, { 28}}, + {{ 28}, { 27}, { 26}}, + {{ 25}, { 22}, { 23}}, + {{ 28}, { 26}, { 25}}, + {{ 32}, { 30}, { 28}}, + {{ 56}, { 33}, { 32}}, + {{ 54}, { 56}, { 32}}, + {{ 32}, { 28}, { 25}}, + {{ 25}, { 23}, { 24}}, + {{ 24}, { 21}, { 0}}, + {{ 0}, { 3}, { 2}}, + {{ 2}, { 1}, { 4}}, + {{ 4}, { 5}, { 6}}, + {{ 6}, { 7}, { 8}}, + {{ 8}, { 9}, { 10}}, + {{ 10}, { 11}, { 12}}, + {{ 12}, { 47}, { 51}}, + {{ 12}, { 51}, { 49}}, + {{ 8}, { 10}, { 12}}, + {{ 4}, { 6}, { 8}}, + {{ 0}, { 2}, { 4}}, + {{ 25}, { 24}, { 0}}, + {{ 52}, { 54}, { 32}}, + {{ 53}, { 52}, { 32}}, + {{ 25}, { 0}, { 4}}, + {{ 4}, { 8}, { 12}}, + {{ 4}, { 12}, { 49}}, + {{ 53}, { 32}, { 25}}, + {{ 44}, { 43}, { 42}}, + {{ 42}, { 53}, { 25}}, + {{ 4}, { 49}, { 50}}, + {{ 45}, { 44}, { 42}}, + {{ 4}, { 50}, { 45}}, + {{ 42}, { 25}, { 4}}, + {{ 35}, { 34}, { 33}}, + {{ 33}, { 56}, { 55}}, + {{ 55}, { 57}, { 36}}, + {{ 36}, { 35}, { 33}}, + {{ 47}, { 12}, { 13}}, + {{ 13}, { 14}, { 15}}, + {{ 15}, { 46}, { 48}}, + {{ 48}, { 47}, { 13}}, + {{ 46}, { 15}, { 16}}, + {{ 16}, { 17}, { 18}}, + {{ 19}, { 20}, { 41}}, + {{ 16}, { 18}, { 19}}, + {{ 44}, { 46}, { 16}}, + {{ 57}, { 43}, { 44}}, + {{ 37}, { 36}, { 57}}, + {{ 39}, { 38}, { 37}}, + {{ 19}, { 41}, { 40}}, + {{ 40}, { 39}, { 37}}, + {{ 37}, { 57}, { 44}}, + {{ 16}, { 19}, { 40}}, + {{ 37}, { 44}, { 16}}, + }; + + constexpr uint32_t num_faces = (sizeof (faces)) / (sizeof (face_v)); + +} diff --git a/chess/king.obj b/chess/king.obj new file mode 100644 index 0000000..e8b9e92 --- /dev/null +++ b/chess/king.obj @@ -0,0 +1,240 @@ +# Blender 3.3.6 +# www.blender.org +o King +v 0.587722 -0.812159 -0.500000 +v 0.508186 -0.497465 -0.500000 +v 0.569728 -0.561184 -0.500000 +v 0.590376 -0.644625 -0.500000 +v 0.451429 -0.468419 -0.500000 +v 0.713971 -0.169361 -0.500000 +v 0.769329 -0.073675 -0.500000 +v 0.787068 0.014243 -0.500000 +v 0.781478 0.126337 -0.500000 +v 0.743187 0.238086 -0.500000 +v 0.681645 0.323805 -0.500000 +v 0.604056 0.381043 -0.500000 +v 0.502300 0.423017 -0.500000 +v 0.406904 0.435736 -0.500000 +v 0.274621 0.424289 -0.500000 +v 0.163962 0.386130 -0.500000 +v 0.143610 0.464991 -0.500000 +v 0.263174 0.464991 -0.500000 +v 0.261902 0.705390 -0.500000 +v 0.134707 0.706662 -0.500000 +v 0.133435 0.837673 -0.500000 +v -0.587722 -0.812159 -0.500000 +v -0.508186 -0.497465 -0.500000 +v -0.569728 -0.561184 -0.500000 +v -0.590376 -0.644625 -0.500000 +v -0.451429 -0.468419 -0.500000 +v -0.713971 -0.169361 -0.500000 +v -0.769329 -0.073675 -0.500000 +v -0.787068 0.014243 -0.500000 +v -0.781478 0.126337 -0.500000 +v -0.743187 0.238086 -0.500000 +v -0.681645 0.323805 -0.500000 +v -0.604056 0.381043 -0.500000 +v -0.502300 0.423017 -0.500000 +v -0.406904 0.435736 -0.500000 +v -0.274621 0.424289 -0.500000 +v -0.163962 0.386130 -0.500000 +v -0.143610 0.464991 -0.500000 +v -0.263174 0.464991 -0.500000 +v -0.261902 0.705390 -0.500000 +v -0.134707 0.706662 -0.500000 +v -0.133435 0.837673 -0.500000 +v -0.232004 -0.196898 -0.500000 +v -0.186214 -0.196898 -0.500000 +v 0.186214 -0.196898 -0.500000 +v 0.232004 -0.196898 -0.500000 +v 0.187740 0.064106 -0.500000 +v 0.361743 0.103791 -0.500000 +v 0.286952 0.109897 -0.500000 +v 0.416691 0.025948 -0.500000 +v 0.398375 -0.019843 -0.500000 +v 0.392270 0.074791 -0.500000 +v -0.416691 0.025948 -0.500000 +v -0.398375 -0.019843 -0.500000 +v -0.392270 0.074791 -0.500000 +v -0.286952 0.109897 -0.500000 +v -0.361743 0.103791 -0.500000 +v -0.187740 0.064106 -0.500000 +v -0.625504 0.268384 -1.500000 +v -0.565495 0.312652 -1.500000 +v 0.461141 -0.560928 -1.500000 +v 0.499116 -0.600247 -1.500000 +v 0.511019 -0.734231 -1.500000 +v 0.512298 -0.653515 -1.500000 +v 0.326852 -0.492204 -1.500000 +v 0.650355 -0.123707 -1.500000 +v 0.695471 -0.045723 -1.500000 +v 0.708752 0.020099 -1.500000 +v 0.704195 0.111476 -1.500000 +v 0.673189 0.201964 -1.500000 +v 0.625504 0.268384 -1.500000 +v 0.565495 0.312652 -1.500000 +v 0.481954 0.347113 -1.500000 +v 0.405092 0.357361 -1.500000 +v 0.290927 0.347481 -1.500000 +v 0.109592 0.284952 -1.500000 +v 0.043020 0.542919 -1.500000 +v 0.184833 0.542919 -1.500000 +v 0.184381 0.628234 -1.500000 +v 0.057525 0.629502 -1.500000 +v 0.056260 0.759745 -1.500000 +v -0.511019 -0.734231 -1.500000 +v -0.461141 -0.560928 -1.500000 +v -0.499116 -0.600247 -1.500000 +v -0.512298 -0.653515 -1.500000 +v -0.326852 -0.492204 -1.500000 +v -0.650355 -0.123707 -1.500000 +v -0.695471 -0.045723 -1.500000 +v -0.708752 0.020099 -1.500000 +v -0.704195 0.111476 -1.500000 +v -0.673189 0.201964 -1.500000 +v -0.481954 0.347113 -1.500000 +v -0.405092 0.357361 -1.500000 +v -0.290927 0.347481 -1.500000 +v -0.109592 0.284952 -1.500000 +v -0.043020 0.542919 -1.500000 +v -0.184833 0.542919 -1.500000 +v -0.184381 0.628234 -1.500000 +v -0.057525 0.629502 -1.500000 +v -0.056260 0.759745 -1.500000 +v -0.265712 -0.274826 -1.500000 +v -0.107829 -0.274826 -1.500000 +v 0.107829 -0.274826 -1.500000 +v 0.265712 -0.274826 -1.500000 +v 0.110104 0.114101 -1.500000 +v 0.395484 0.179223 -1.500000 +v 0.272886 0.189231 -1.500000 +v 0.502042 0.029498 -1.500000 +v 0.465256 -0.062466 -1.500000 +v 0.455855 0.121871 -1.500000 +v -0.465256 -0.062466 -1.500000 +v -0.502042 0.029498 -1.500000 +v -0.455855 0.121871 -1.500000 +v -0.272886 0.189231 -1.500000 +v -0.395484 0.179223 -1.500000 +v -0.110104 0.114101 -1.500000 +s 0 +f 72 76 106 +f 93 115 95 +f 77 81 96 +f 5 46 43 +f 34 56 37 +f 14 16 49 +f 17 41 38 +f 110 108 68 +f 68 69 70 +f 70 71 72 +f 72 73 74 +f 74 75 76 +f 76 107 106 +f 106 110 68 +f 68 70 72 +f 72 74 76 +f 106 68 72 +f 95 94 93 +f 93 92 115 +f 115 114 95 +f 86 83 84 +f 84 85 82 +f 82 63 64 +f 64 62 61 +f 65 66 67 +f 64 61 65 +f 84 82 64 +f 88 87 86 +f 86 84 64 +f 65 67 68 +f 86 64 65 +f 89 88 86 +f 91 90 89 +f 60 59 91 +f 115 92 60 +f 113 115 60 +f 60 91 89 +f 65 68 108 +f 65 108 109 +f 113 60 89 +f 112 113 89 +f 65 109 104 +f 65 104 103 +f 112 89 86 +f 111 112 86 +f 86 65 103 +f 105 107 76 +f 101 111 86 +f 102 101 86 +f 103 105 76 +f 77 78 79 +f 103 76 77 +f 102 86 103 +f 95 114 116 +f 116 102 103 +f 77 79 80 +f 116 103 77 +f 96 95 116 +f 98 97 96 +f 81 100 99 +f 99 98 96 +f 96 116 77 +f 77 80 81 +f 81 99 96 +f 33 32 31 +f 31 30 29 +f 29 28 27 +f 26 23 24 +f 29 27 26 +f 33 31 29 +f 57 34 33 +f 55 57 33 +f 33 29 26 +f 26 24 25 +f 25 22 1 +f 1 4 3 +f 3 2 5 +f 5 6 7 +f 7 8 9 +f 9 10 11 +f 11 12 13 +f 13 48 52 +f 13 52 50 +f 9 11 13 +f 5 7 9 +f 1 3 5 +f 26 25 1 +f 53 55 33 +f 54 53 33 +f 26 1 5 +f 5 9 13 +f 5 13 50 +f 54 33 26 +f 45 44 43 +f 43 54 26 +f 5 50 51 +f 46 45 43 +f 5 51 46 +f 43 26 5 +f 36 35 34 +f 34 57 56 +f 56 58 37 +f 37 36 34 +f 48 13 14 +f 14 15 16 +f 16 47 49 +f 49 48 14 +f 47 16 17 +f 17 18 19 +f 20 21 42 +f 17 19 20 +f 45 47 17 +f 58 44 45 +f 38 37 58 +f 40 39 38 +f 20 42 41 +f 41 40 38 +f 38 58 45 +f 17 20 41 +f 38 45 17 diff --git a/chess/knight.blend b/chess/knight.blend new file mode 100644 index 0000000..5b34d27 Binary files /dev/null and b/chess/knight.blend differ diff --git a/chess/knight.hpp b/chess/knight.hpp new file mode 100644 index 0000000..2973a7c --- /dev/null +++ b/chess/knight.hpp @@ -0,0 +1,162 @@ +#pragma once + +#include "geometry/geometry.hpp" + +namespace knight { + constexpr vec3 vertices[] = { + { 0.659379f, -0.816994f, -0.500000f }, + { 0.562711f, -0.484379f, -0.500000f }, + { 0.619948f, -0.544161f, -0.500000f }, + { 0.660651f, -0.653548f, -0.500000f }, + { 0.514377f, -0.460212f, -0.500000f }, + { 0.590694f, -0.167663f, -0.500000f }, + { 0.590694f, 0.056200f, -0.500000f }, + { 0.542359f, 0.227913f, -0.500000f }, + { 0.452051f, 0.393266f, -0.500000f }, + { 0.356655f, 0.496294f, -0.500000f }, + { 0.214196f, 0.587875f, -0.500000f }, + { 0.062834f, 0.643840f, -0.500000f }, + { -0.031290f, 0.656560f, -0.500000f }, + { -0.172476f, 0.799018f, -0.500000f }, + { -0.250065f, 0.795203f, -0.500000f }, + { -0.297128f, 0.739237f, -0.500000f }, + { -0.317479f, 0.631121f, -0.500000f }, + { -0.419235f, 0.531909f, -0.500000f }, + { -0.477744f, 0.408530f, -0.500000f }, + { -0.509543f, 0.313134f, -0.500000f }, + { -0.706695f, 0.014225f, -0.500000f }, + { -0.707967f, -0.079899f, -0.500000f }, + { -0.659633f, -0.147312f, -0.500000f }, + { -0.506999f, -0.207094f, -0.500000f }, + { -0.396340f, -0.207094f, -0.500000f }, + { -0.307303f, -0.084987f, -0.500000f }, + { -0.204275f, -0.082443f, -0.500000f }, + { -0.124142f, -0.039196f, -0.500000f }, + { -0.032562f, 0.048568f, -0.500000f }, + { -0.035106f, -0.015029f, -0.500000f }, + { -0.077080f, -0.078627f, -0.500000f }, + { -0.206819f, -0.181655f, -0.500000f }, + { -0.295856f, -0.277051f, -0.500000f }, + { -0.363269f, -0.378807f, -0.500000f }, + { -0.386164f, -0.457668f, -0.500000f }, + { -0.462481f, -0.495826f, -0.500000f }, + { -0.510815f, -0.551792f, -0.500000f }, + { -0.534982f, -0.626837f, -0.500000f }, + { -0.534982f, -0.816994f, -0.500000f }, + { 0.500944f, -0.156150f, -1.500000f }, + { 0.409169f, -0.507952f, -1.500000f }, + { 0.508650f, -0.557692f, -1.500000f }, + { 0.542299f, -0.592837f, -1.500000f }, + { 0.570775f, -0.669367f, -1.500000f }, + { 0.570325f, -0.744827f, -1.500000f }, + { 0.500944f, 0.043809f, -1.500000f }, + { 0.458720f, 0.193813f, -1.500000f }, + { 0.378625f, 0.340467f, -1.500000f }, + { 0.298539f, 0.426959f, -1.500000f }, + { 0.173894f, 0.507088f, -1.500000f }, + { 0.040999f, 0.556225f, -1.500000f }, + { -0.073553f, 0.571706f, -1.500000f }, + { -0.209298f, 0.704796f, -1.500000f }, + { -0.234523f, 0.586654f, -1.500000f }, + { -0.345045f, 0.478895f, -1.500000f }, + { -0.394320f, 0.374988f, -1.500000f }, + { -0.428129f, 0.273563f, -1.500000f }, + { -0.617309f, -0.013258f, -1.500000f }, + { -0.617827f, -0.051594f, -1.500000f }, + { -0.602171f, -0.073430f, -1.500000f }, + { -0.490050f, -0.117344f, -1.500000f }, + { -0.441973f, -0.117344f, -1.500000f }, + { -0.353752f, 0.003644f, -1.500000f }, + { -0.227982f, 0.006749f, -1.500000f }, + { -0.177465f, 0.034012f, -1.500000f }, + { 0.029378f, 0.195605f, -1.500000f }, + { 0.053573f, -0.043600f, -1.500000f }, + { -0.010058f, -0.140010f, -1.500000f }, + { -0.145769f, -0.247781f, -1.500000f }, + { -0.225155f, -0.332837f, -1.500000f }, + { -0.280893f, -0.416969f, -1.500000f }, + { -0.310917f, -0.520388f, -1.500000f }, + { -0.406289f, -0.568074f, -1.500000f }, + { -0.430974f, -0.596657f, -1.500000f }, + { -0.445232f, -0.640932f, -1.500000f }, + { -0.445232f, -0.744827f, -1.500000f }, + }; + + constexpr face_v faces[] = { + {{ 4}, {28}, {29}}, + {{40}, {67}, {71}}, + {{ 4}, { 5}, { 6}}, + {{ 6}, { 7}, { 8}}, + {{ 8}, { 9}, {10}}, + {{10}, {11}, {12}}, + {{12}, {13}, {14}}, + {{14}, {15}, {16}}, + {{16}, {17}, {18}}, + {{19}, {20}, {21}}, + {{16}, {18}, {19}}, + {{12}, {14}, {16}}, + {{ 8}, {10}, {12}}, + {{ 4}, { 6}, { 8}}, + {{ 2}, { 1}, { 4}}, + {{ 0}, { 3}, { 2}}, + {{37}, {38}, { 0}}, + {{35}, {36}, {37}}, + {{32}, {33}, {34}}, + {{34}, {35}, {37}}, + {{37}, { 0}, { 2}}, + {{ 4}, { 8}, {12}}, + {{37}, { 2}, { 4}}, + {{31}, {32}, {34}}, + {{34}, {37}, { 4}}, + {{ 4}, {12}, {16}}, + {{16}, {19}, {21}}, + {{21}, {22}, {23}}, + {{23}, {24}, {25}}, + {{25}, {26}, {27}}, + {{25}, {27}, {28}}, + {{21}, {23}, {25}}, + {{31}, {34}, { 4}}, + {{30}, {31}, { 4}}, + {{16}, {21}, {25}}, + {{16}, {25}, {28}}, + {{29}, {30}, { 4}}, + {{ 4}, {16}, {28}}, + {{40}, {39}, {45}}, + {{45}, {46}, {47}}, + {{47}, {48}, {49}}, + {{49}, {50}, {51}}, + {{51}, {52}, {53}}, + {{53}, {54}, {55}}, + {{56}, {57}, {58}}, + {{53}, {55}, {56}}, + {{49}, {51}, {53}}, + {{45}, {47}, {49}}, + {{42}, {41}, {40}}, + {{40}, {45}, {49}}, + {{49}, {53}, {56}}, + {{56}, {58}, {59}}, + {{59}, {60}, {61}}, + {{62}, {63}, {64}}, + {{59}, {61}, {62}}, + {{49}, {56}, {59}}, + {{43}, {42}, {40}}, + {{49}, {59}, {62}}, + {{62}, {64}, {65}}, + {{49}, {62}, {65}}, + {{44}, {43}, {40}}, + {{40}, {49}, {65}}, + {{40}, {65}, {66}}, + {{75}, {44}, {40}}, + {{73}, {74}, {75}}, + {{71}, {72}, {73}}, + {{69}, {70}, {71}}, + {{67}, {68}, {69}}, + {{40}, {66}, {67}}, + {{73}, {75}, {40}}, + {{67}, {69}, {71}}, + {{71}, {73}, {40}}, + }; + + constexpr uint32_t num_faces = (sizeof (faces)) / (sizeof (face_v)); + +} diff --git a/chess/knight.obj b/chess/knight.obj new file mode 100644 index 0000000..c7ce41a --- /dev/null +++ b/chess/knight.obj @@ -0,0 +1,152 @@ +# Blender 3.3.6 +# www.blender.org +o Knight +v 0.659379 -0.816994 -0.500000 +v 0.562711 -0.484379 -0.500000 +v 0.619948 -0.544161 -0.500000 +v 0.660651 -0.653548 -0.500000 +v 0.514377 -0.460212 -0.500000 +v 0.590694 -0.167663 -0.500000 +v 0.590694 0.056200 -0.500000 +v 0.542359 0.227913 -0.500000 +v 0.452051 0.393266 -0.500000 +v 0.356655 0.496294 -0.500000 +v 0.214196 0.587875 -0.500000 +v 0.062834 0.643840 -0.500000 +v -0.031290 0.656560 -0.500000 +v -0.172476 0.799018 -0.500000 +v -0.250065 0.795203 -0.500000 +v -0.297128 0.739237 -0.500000 +v -0.317479 0.631121 -0.500000 +v -0.419235 0.531909 -0.500000 +v -0.477744 0.408530 -0.500000 +v -0.509543 0.313134 -0.500000 +v -0.706695 0.014225 -0.500000 +v -0.707967 -0.079899 -0.500000 +v -0.659633 -0.147312 -0.500000 +v -0.506999 -0.207094 -0.500000 +v -0.396340 -0.207094 -0.500000 +v -0.307303 -0.084987 -0.500000 +v -0.204275 -0.082443 -0.500000 +v -0.124142 -0.039196 -0.500000 +v -0.032562 0.048568 -0.500000 +v -0.035106 -0.015029 -0.500000 +v -0.077080 -0.078627 -0.500000 +v -0.206819 -0.181655 -0.500000 +v -0.295856 -0.277051 -0.500000 +v -0.363269 -0.378807 -0.500000 +v -0.386164 -0.457668 -0.500000 +v -0.462481 -0.495826 -0.500000 +v -0.510815 -0.551792 -0.500000 +v -0.534982 -0.626837 -0.500000 +v -0.534982 -0.816994 -0.500000 +v 0.500944 -0.156150 -1.500000 +v 0.409169 -0.507952 -1.500000 +v 0.508650 -0.557692 -1.500000 +v 0.542299 -0.592837 -1.500000 +v 0.570775 -0.669367 -1.500000 +v 0.570325 -0.744827 -1.500000 +v 0.500944 0.043809 -1.500000 +v 0.458720 0.193813 -1.500000 +v 0.378625 0.340467 -1.500000 +v 0.298539 0.426959 -1.500000 +v 0.173894 0.507088 -1.500000 +v 0.040999 0.556225 -1.500000 +v -0.073553 0.571706 -1.500000 +v -0.209298 0.704796 -1.500000 +v -0.234523 0.586654 -1.500000 +v -0.345045 0.478895 -1.500000 +v -0.394320 0.374988 -1.500000 +v -0.428129 0.273563 -1.500000 +v -0.617309 -0.013258 -1.500000 +v -0.617827 -0.051594 -1.500000 +v -0.602171 -0.073430 -1.500000 +v -0.490050 -0.117344 -1.500000 +v -0.441973 -0.117344 -1.500000 +v -0.353752 0.003644 -1.500000 +v -0.227982 0.006749 -1.500000 +v -0.177465 0.034012 -1.500000 +v 0.029378 0.195605 -1.500000 +v 0.053573 -0.043600 -1.500000 +v -0.010058 -0.140010 -1.500000 +v -0.145769 -0.247781 -1.500000 +v -0.225155 -0.332837 -1.500000 +v -0.280893 -0.416969 -1.500000 +v -0.310917 -0.520388 -1.500000 +v -0.406289 -0.568074 -1.500000 +v -0.430974 -0.596657 -1.500000 +v -0.445232 -0.640932 -1.500000 +v -0.445232 -0.744827 -1.500000 +s 0 +f 5 29 30 +f 41 68 72 +f 5 6 7 +f 7 8 9 +f 9 10 11 +f 11 12 13 +f 13 14 15 +f 15 16 17 +f 17 18 19 +f 20 21 22 +f 17 19 20 +f 13 15 17 +f 9 11 13 +f 5 7 9 +f 3 2 5 +f 1 4 3 +f 38 39 1 +f 36 37 38 +f 33 34 35 +f 35 36 38 +f 38 1 3 +f 5 9 13 +f 38 3 5 +f 32 33 35 +f 35 38 5 +f 5 13 17 +f 17 20 22 +f 22 23 24 +f 24 25 26 +f 26 27 28 +f 26 28 29 +f 22 24 26 +f 32 35 5 +f 31 32 5 +f 17 22 26 +f 17 26 29 +f 30 31 5 +f 5 17 29 +f 41 40 46 +f 46 47 48 +f 48 49 50 +f 50 51 52 +f 52 53 54 +f 54 55 56 +f 57 58 59 +f 54 56 57 +f 50 52 54 +f 46 48 50 +f 43 42 41 +f 41 46 50 +f 50 54 57 +f 57 59 60 +f 60 61 62 +f 63 64 65 +f 60 62 63 +f 50 57 60 +f 44 43 41 +f 50 60 63 +f 63 65 66 +f 50 63 66 +f 45 44 41 +f 41 50 66 +f 41 66 67 +f 76 45 41 +f 74 75 76 +f 72 73 74 +f 70 71 72 +f 68 69 70 +f 41 67 68 +f 74 76 41 +f 68 70 72 +f 72 74 41 diff --git a/chess/main.cpp b/chess/main.cpp new file mode 100644 index 0000000..57a1f68 --- /dev/null +++ b/chess/main.cpp @@ -0,0 +1,140 @@ +#include + +#include "memorymap.hpp" +#include "twiddle.hpp" + +#include "holly/texture_memory_alloc.hpp" +#include "holly/holly.hpp" +#include "holly/core.hpp" +#include "holly/core_bits.hpp" +#include "holly/ta_fifo_polygon_converter.hpp" +#include "holly/ta_parameter.hpp" +#include "holly/ta_global_parameter.hpp" +#include "holly/ta_vertex_parameter.hpp" +#include "holly/ta_bits.hpp" +#include "holly/isp_tsp.hpp" +#include "holly/region_array.hpp" +#include "holly/background.hpp" +#include "holly/video_output.hpp" + +#include "maple/maple_bus_bits.hpp" + +#include "sh7091/store_queue.hpp" +#include "sh7091/serial.hpp" + +#include "render.hpp" +#include "input.hpp" + +struct button { + bool a; + bool b; +}; + +struct cursor { + float x; + float y; + struct button button[2]; +}; + +void cursor_update(chess::game_state& game_state, struct cursor& cursor, uint32_t frame_ix) +{ + for (int port_ix = 0; port_ix < 4; port_ix++) { + auto& port = input::state.port[port_ix]; + if (port.function_type & function_type::controller) { + + } + if (port.function_type & function_type::pointing) { + auto& bus_data = port.host_response_data_transfer_ft9->bus_data; + auto& data_fields = bus_data.data_fields; + if (std::byteswap(data_fields.function_type) & function_type::pointing) { + auto& data = data_fields.data; + cursor.x += static_cast(data.analog_coordinate_axis[0] - 0x200) * 0.0035; + cursor.y += static_cast(data.analog_coordinate_axis[1] - 0x200) * -0.0035; + cursor.button[frame_ix].a = ft9::data_transfer::button::a(data.button) == 0; + cursor.button[frame_ix].b = ft9::data_transfer::button::b(data.button) == 0; + } + } + } + + uint32_t last_frame = (frame_ix + 1) & 1; + if (cursor.button[last_frame].a != cursor.button[frame_ix].a && cursor.button[frame_ix].a) { + int8_t x = cursor.x + 0.5f; + int8_t y = cursor.y + 0.5f; + chess::select_position(game_state, x, y); + } + if (cursor.button[last_frame].b != cursor.button[frame_ix].b && cursor.button[frame_ix].b) { + serial::string("b\n"); + } +} + +void main() +{ + video_output::set_mode_vga(); + + constexpr uint32_t ta_alloc = ta_alloc_ctrl::pt_opb::no_list + | ta_alloc_ctrl::tm_opb::no_list + | ta_alloc_ctrl::t_opb::no_list + | ta_alloc_ctrl::om_opb::no_list + | ta_alloc_ctrl::o_opb::_16x4byte; + + constexpr struct opb_size opb_size = { .opaque = 16 * 4 + , .opaque_modifier = 0 + , .translucent = 0 + , .translucent_modifier = 0 + , .punch_through = 0 + }; + + holly.SOFTRESET = softreset::pipeline_soft_reset + | softreset::ta_soft_reset; + holly.SOFTRESET = 0; + + core_init(); + region_array2(640 / 32, 480 / 32, opb_size); + background_parameter(0xff0000ff); + + chess::game_state game_state; + chess::game_init(game_state); + + uint32_t send_buf[1024] __attribute__((aligned(32))); + uint32_t recv_buf[1024] __attribute__((aligned(32))); + + input::state_init(); + + uint32_t frame_ix = 0; + + struct cursor cursor; + cursor.x = 4.f; + cursor.y = 4.f; + + while (true) { + input::state_update(send_buf, recv_buf); + cursor_update(game_state, cursor, frame_ix); + + ta_polygon_converter_init(opb_size.total(), + ta_alloc, + 640 / 32, + 480 / 32); + + render::draw_board(); + if (game_state.interaction.selected_position != -1) + render::draw_illumination(game_state.interaction.selected_position); + render::draw_pieces(game_state); + render::draw_moves(game_state.interaction.moves); + render::draw_cursor(cursor.x, cursor.y); + + *reinterpret_cast(store_queue) = + ta_global_parameter::end_of_list(para_control::para_type::end_of_list); + sq_transfer_32byte(ta_fifo_polygon_converter); + + ta_wait_opaque_list(); + + core_start_render(frame_ix); + core_wait_end_of_render_video(); + + while (!spg_status::vsync(holly.SPG_STATUS)); + core_flip(frame_ix); + while (spg_status::vsync(holly.SPG_STATUS)); + + frame_ix = (frame_ix + 1) & 1; + } +} diff --git a/chess/pawn.blend b/chess/pawn.blend new file mode 100644 index 0000000..87c339e Binary files /dev/null and b/chess/pawn.blend differ diff --git a/chess/pawn.hpp b/chess/pawn.hpp new file mode 100644 index 0000000..4bc94a1 --- /dev/null +++ b/chess/pawn.hpp @@ -0,0 +1,190 @@ +#pragma once + +#include "geometry/geometry.hpp" + +namespace pawn { + constexpr vec3 vertices[] = { + { 0.501714f, -0.615807f, -0.500000f }, + { 0.524680f, -0.696187f, -0.500000f }, + { 0.525563f, -0.814549f, -0.500000f }, + { 0.469032f, -0.542493f, -0.500000f }, + { 0.399251f, -0.467413f, -0.500000f }, + { 0.300322f, -0.402932f, -0.500000f }, + { 0.234074f, -0.326085f, -0.500000f }, + { 0.182843f, -0.218322f, -0.500000f }, + { 0.167827f, -0.153841f, -0.500000f }, + { 0.280889f, -0.154725f, -0.500000f }, + { 0.302972f, -0.111443f, -0.500000f }, + { 0.320638f, -0.068161f, -0.500000f }, + { 0.317104f, -0.007214f, -0.500000f }, + { 0.196976f, 0.073167f, -0.500000f }, + { 0.228775f, 0.121748f, -0.500000f }, + { 0.261457f, 0.188879f, -0.500000f }, + { 0.274706f, 0.253360f, -0.500000f }, + { 0.273823f, 0.315191f, -0.500000f }, + { 0.257923f, 0.381438f, -0.500000f }, + { 0.219942f, 0.445036f, -0.500000f }, + { 0.173127f, 0.490084f, -0.500000f }, + { 0.123662f, 0.520999f, -0.500000f }, + { 0.053881f, 0.544848f, -0.500000f }, + { 0.000000f, 0.552798f, -0.500000f }, + { -0.501714f, -0.615807f, -0.500000f }, + { -0.524680f, -0.696187f, -0.500000f }, + { -0.525563f, -0.814549f, -0.500000f }, + { -0.469032f, -0.542493f, -0.500000f }, + { -0.399251f, -0.467413f, -0.500000f }, + { -0.300322f, -0.402932f, -0.500000f }, + { -0.234074f, -0.326085f, -0.500000f }, + { -0.182843f, -0.218322f, -0.500000f }, + { -0.167827f, -0.153841f, -0.500000f }, + { -0.280889f, -0.154725f, -0.500000f }, + { -0.302972f, -0.111443f, -0.500000f }, + { -0.320638f, -0.068161f, -0.500000f }, + { -0.317104f, -0.007214f, -0.500000f }, + { -0.196976f, 0.073167f, -0.500000f }, + { -0.228775f, 0.121748f, -0.500000f }, + { -0.261457f, 0.188879f, -0.500000f }, + { -0.274706f, 0.253360f, -0.500000f }, + { -0.273823f, 0.315191f, -0.500000f }, + { -0.257923f, 0.381438f, -0.500000f }, + { -0.219942f, 0.445036f, -0.500000f }, + { -0.173127f, 0.490084f, -0.500000f }, + { -0.123662f, 0.520999f, -0.500000f }, + { -0.053881f, 0.544848f, -0.500000f }, + { 0.388761f, -0.595554f, -1.500000f }, + { 0.411286f, -0.648695f, -1.500000f }, + { 0.429437f, -0.709889f, -1.500000f }, + { 0.431445f, -0.746821f, -1.500000f }, + { -0.431445f, -0.746821f, -1.500000f }, + { -0.429437f, -0.709889f, -1.500000f }, + { -0.411286f, -0.648695f, -1.500000f }, + { -0.388761f, -0.595554f, -1.500000f }, + { -0.337277f, -0.541021f, -1.500000f }, + { -0.236983f, -0.475369f, -1.500000f }, + { -0.153462f, -0.378626f, -1.500000f }, + { -0.092010f, -0.250075f, -1.500000f }, + { -0.092699f, -0.093719f, -1.500000f }, + { -0.230956f, -0.072472f, -1.500000f }, + { -0.233576f, -0.054984f, -1.500000f }, + { -0.102685f, 0.053978f, -1.500000f }, + { -0.145094f, 0.169251f, -1.500000f }, + { -0.170359f, 0.219865f, -1.500000f }, + { -0.178909f, 0.262409f, -1.500000f }, + { -0.178349f, 0.303202f, -1.500000f }, + { -0.168815f, 0.345126f, -1.500000f }, + { -0.144609f, 0.385169f, -1.500000f }, + { -0.113949f, 0.414209f, -1.500000f }, + { -0.082332f, 0.434104f, -1.500000f }, + { -0.031205f, 0.451335f, -1.500000f }, + { 0.000000f, 0.456575f, -1.500000f }, + { 0.031205f, 0.451335f, -1.500000f }, + { 0.082332f, 0.434104f, -1.500000f }, + { 0.113949f, 0.414209f, -1.500000f }, + { 0.144609f, 0.385169f, -1.500000f }, + { 0.168815f, 0.345126f, -1.500000f }, + { 0.178349f, 0.303202f, -1.500000f }, + { 0.178909f, 0.262409f, -1.500000f }, + { 0.170359f, 0.219865f, -1.500000f }, + { 0.145094f, 0.169251f, -1.500000f }, + { 0.102685f, 0.053978f, -1.500000f }, + { 0.233576f, -0.054984f, -1.500000f }, + { 0.230956f, -0.072472f, -1.500000f }, + { 0.092699f, -0.093719f, -1.500000f }, + { 0.092010f, -0.250075f, -1.500000f }, + { 0.153462f, -0.378626f, -1.500000f }, + { 0.236983f, -0.475369f, -1.500000f }, + { 0.337277f, -0.541021f, -1.500000f }, + }; + + constexpr face_v faces[] = { + {{41}, {21}, {13}}, + {{67}, {75}, {82}}, + {{ 4}, { 3}, { 0}}, + {{ 0}, { 1}, { 2}}, + {{ 2}, {26}, {25}}, + {{25}, {24}, {27}}, + {{27}, {28}, {29}}, + {{25}, {27}, {29}}, + {{ 0}, { 2}, {25}}, + {{ 5}, { 4}, { 0}}, + {{ 5}, { 0}, {25}}, + {{25}, {29}, {30}}, + {{ 5}, {25}, {30}}, + {{ 6}, { 5}, {30}}, + {{ 6}, {30}, {31}}, + {{ 7}, { 6}, {31}}, + {{32}, {33}, {34}}, + {{ 7}, {31}, {32}}, + {{10}, { 9}, { 8}}, + {{ 8}, { 7}, {32}}, + {{32}, {34}, {35}}, + {{35}, {36}, {37}}, + {{37}, {38}, {39}}, + {{39}, {40}, {41}}, + {{41}, {42}, {43}}, + {{43}, {44}, {45}}, + {{45}, {46}, {23}}, + {{23}, {22}, {21}}, + {{21}, {20}, {19}}, + {{19}, {18}, {17}}, + {{17}, {16}, {15}}, + {{15}, {14}, {13}}, + {{13}, {12}, {11}}, + {{11}, {10}, { 8}}, + {{ 8}, {32}, {35}}, + {{37}, {39}, {41}}, + {{ 8}, {35}, {37}}, + {{13}, {11}, { 8}}, + {{17}, {15}, {13}}, + {{21}, {19}, {17}}, + {{45}, {23}, {21}}, + {{41}, {43}, {45}}, + {{ 8}, {37}, {41}}, + {{21}, {17}, {13}}, + {{13}, { 8}, {41}}, + {{41}, {45}, {21}}, + {{89}, {47}, {48}}, + {{48}, {49}, {50}}, + {{50}, {51}, {52}}, + {{52}, {53}, {54}}, + {{54}, {55}, {56}}, + {{52}, {54}, {56}}, + {{48}, {50}, {52}}, + {{88}, {89}, {48}}, + {{88}, {48}, {52}}, + {{52}, {56}, {57}}, + {{88}, {52}, {57}}, + {{87}, {88}, {57}}, + {{87}, {57}, {58}}, + {{86}, {87}, {58}}, + {{59}, {60}, {61}}, + {{86}, {58}, {59}}, + {{83}, {84}, {85}}, + {{85}, {86}, {59}}, + {{59}, {61}, {62}}, + {{63}, {64}, {65}}, + {{85}, {59}, {62}}, + {{62}, {63}, {65}}, + {{65}, {66}, {67}}, + {{67}, {68}, {69}}, + {{69}, {70}, {71}}, + {{71}, {72}, {73}}, + {{73}, {74}, {75}}, + {{75}, {76}, {77}}, + {{77}, {78}, {79}}, + {{79}, {80}, {81}}, + {{82}, {83}, {85}}, + {{79}, {81}, {82}}, + {{75}, {77}, {79}}, + {{71}, {73}, {75}}, + {{67}, {69}, {71}}, + {{62}, {65}, {67}}, + {{82}, {85}, {62}}, + {{75}, {79}, {82}}, + {{67}, {71}, {75}}, + {{82}, {62}, {67}}, + }; + + constexpr uint32_t num_faces = (sizeof (faces)) / (sizeof (face_v)); + +} diff --git a/chess/pawn.mtl b/chess/pawn.mtl new file mode 100644 index 0000000..e465572 --- /dev/null +++ b/chess/pawn.mtl @@ -0,0 +1,12 @@ +# Blender 3.3.6 MTL File: 'pawn.blend' +# www.blender.org + +newmtl Material +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 diff --git a/chess/pawn.obj b/chess/pawn.obj new file mode 100644 index 0000000..208ad9a --- /dev/null +++ b/chess/pawn.obj @@ -0,0 +1,180 @@ +# Blender 3.3.6 +# www.blender.org +o Pawn +v 0.501714 -0.615807 -0.500000 +v 0.524680 -0.696187 -0.500000 +v 0.525563 -0.814549 -0.500000 +v 0.469032 -0.542493 -0.500000 +v 0.399251 -0.467413 -0.500000 +v 0.300322 -0.402932 -0.500000 +v 0.234074 -0.326085 -0.500000 +v 0.182843 -0.218322 -0.500000 +v 0.167827 -0.153841 -0.500000 +v 0.280889 -0.154725 -0.500000 +v 0.302972 -0.111443 -0.500000 +v 0.320638 -0.068161 -0.500000 +v 0.317104 -0.007214 -0.500000 +v 0.196976 0.073167 -0.500000 +v 0.228775 0.121748 -0.500000 +v 0.261457 0.188879 -0.500000 +v 0.274706 0.253360 -0.500000 +v 0.273823 0.315191 -0.500000 +v 0.257923 0.381438 -0.500000 +v 0.219942 0.445036 -0.500000 +v 0.173127 0.490084 -0.500000 +v 0.123662 0.520999 -0.500000 +v 0.053881 0.544848 -0.500000 +v 0.000000 0.552798 -0.500000 +v -0.501714 -0.615807 -0.500000 +v -0.524680 -0.696187 -0.500000 +v -0.525563 -0.814549 -0.500000 +v -0.469032 -0.542493 -0.500000 +v -0.399251 -0.467413 -0.500000 +v -0.300322 -0.402932 -0.500000 +v -0.234074 -0.326085 -0.500000 +v -0.182843 -0.218322 -0.500000 +v -0.167827 -0.153841 -0.500000 +v -0.280889 -0.154725 -0.500000 +v -0.302972 -0.111443 -0.500000 +v -0.320638 -0.068161 -0.500000 +v -0.317104 -0.007214 -0.500000 +v -0.196976 0.073167 -0.500000 +v -0.228775 0.121748 -0.500000 +v -0.261457 0.188879 -0.500000 +v -0.274706 0.253360 -0.500000 +v -0.273823 0.315191 -0.500000 +v -0.257923 0.381438 -0.500000 +v -0.219942 0.445036 -0.500000 +v -0.173127 0.490084 -0.500000 +v -0.123662 0.520999 -0.500000 +v -0.053881 0.544848 -0.500000 +v 0.388761 -0.595554 -1.500000 +v 0.411286 -0.648695 -1.500000 +v 0.429437 -0.709889 -1.500000 +v 0.431445 -0.746821 -1.500000 +v -0.431445 -0.746821 -1.500000 +v -0.429437 -0.709889 -1.500000 +v -0.411286 -0.648695 -1.500000 +v -0.388761 -0.595554 -1.500000 +v -0.337277 -0.541021 -1.500000 +v -0.236983 -0.475369 -1.500000 +v -0.153462 -0.378626 -1.500000 +v -0.092010 -0.250075 -1.500000 +v -0.092699 -0.093719 -1.500000 +v -0.230956 -0.072472 -1.500000 +v -0.233576 -0.054984 -1.500000 +v -0.102685 0.053978 -1.500000 +v -0.145094 0.169251 -1.500000 +v -0.170359 0.219865 -1.500000 +v -0.178909 0.262409 -1.500000 +v -0.178349 0.303202 -1.500000 +v -0.168815 0.345126 -1.500000 +v -0.144609 0.385169 -1.500000 +v -0.113949 0.414209 -1.500000 +v -0.082332 0.434104 -1.500000 +v -0.031205 0.451335 -1.500000 +v 0.000000 0.456575 -1.500000 +v 0.031205 0.451335 -1.500000 +v 0.082332 0.434104 -1.500000 +v 0.113949 0.414209 -1.500000 +v 0.144609 0.385169 -1.500000 +v 0.168815 0.345126 -1.500000 +v 0.178349 0.303202 -1.500000 +v 0.178909 0.262409 -1.500000 +v 0.170359 0.219865 -1.500000 +v 0.145094 0.169251 -1.500000 +v 0.102685 0.053978 -1.500000 +v 0.233576 -0.054984 -1.500000 +v 0.230956 -0.072472 -1.500000 +v 0.092699 -0.093719 -1.500000 +v 0.092010 -0.250075 -1.500000 +v 0.153462 -0.378626 -1.500000 +v 0.236983 -0.475369 -1.500000 +v 0.337277 -0.541021 -1.500000 +s 0 +f 42 22 14 +f 68 76 83 +f 5 4 1 +f 1 2 3 +f 3 27 26 +f 26 25 28 +f 28 29 30 +f 26 28 30 +f 1 3 26 +f 6 5 1 +f 6 1 26 +f 26 30 31 +f 6 26 31 +f 7 6 31 +f 7 31 32 +f 8 7 32 +f 33 34 35 +f 8 32 33 +f 11 10 9 +f 9 8 33 +f 33 35 36 +f 36 37 38 +f 38 39 40 +f 40 41 42 +f 42 43 44 +f 44 45 46 +f 46 47 24 +f 24 23 22 +f 22 21 20 +f 20 19 18 +f 18 17 16 +f 16 15 14 +f 14 13 12 +f 12 11 9 +f 9 33 36 +f 38 40 42 +f 9 36 38 +f 14 12 9 +f 18 16 14 +f 22 20 18 +f 46 24 22 +f 42 44 46 +f 9 38 42 +f 22 18 14 +f 14 9 42 +f 42 46 22 +f 90 48 49 +f 49 50 51 +f 51 52 53 +f 53 54 55 +f 55 56 57 +f 53 55 57 +f 49 51 53 +f 89 90 49 +f 89 49 53 +f 53 57 58 +f 89 53 58 +f 88 89 58 +f 88 58 59 +f 87 88 59 +f 60 61 62 +f 87 59 60 +f 84 85 86 +f 86 87 60 +f 60 62 63 +f 64 65 66 +f 86 60 63 +f 63 64 66 +f 66 67 68 +f 68 69 70 +f 70 71 72 +f 72 73 74 +f 74 75 76 +f 76 77 78 +f 78 79 80 +f 80 81 82 +f 83 84 86 +f 80 82 83 +f 76 78 80 +f 72 74 76 +f 68 70 72 +f 63 66 68 +f 83 86 63 +f 76 80 83 +f 68 72 76 +f 83 63 68 diff --git a/chess/queen.blend b/chess/queen.blend new file mode 100644 index 0000000..b5d71a2 Binary files /dev/null and b/chess/queen.blend differ diff --git a/chess/queen.hpp b/chess/queen.hpp new file mode 100644 index 0000000..31ddd14 --- /dev/null +++ b/chess/queen.hpp @@ -0,0 +1,298 @@ +#pragma once + +#include "geometry/geometry.hpp" + +namespace queen { + constexpr vec3 vertices[] = { + { 0.000000f, -0.817631f, -0.500000f }, + { 0.570827f, -0.535257f, -0.500000f }, + { 0.604407f, -0.622259f, -0.500000f }, + { 0.601354f, -0.817631f, -0.500000f }, + { 0.500616f, -0.480309f, -0.500000f }, + { 0.436509f, -0.457414f, -0.500000f }, + { 0.671566f, 0.113438f, -0.500000f }, + { 0.729567f, 0.137859f, -0.500000f }, + { 0.769252f, 0.185176f, -0.500000f }, + { 0.801050f, 0.252306f, -0.500000f }, + { 0.800167f, 0.322970f, -0.500000f }, + { 0.775435f, 0.383035f, -0.500000f }, + { 0.725970f, 0.435149f, -0.500000f }, + { 0.657073f, 0.463415f, -0.500000f }, + { 0.579342f, 0.465181f, -0.500000f }, + { 0.516628f, 0.435149f, -0.500000f }, + { 0.468930f, 0.382151f, -0.500000f }, + { 0.445081f, 0.321204f, -0.500000f }, + { 0.444198f, 0.268206f, -0.500000f }, + { 0.462747f, 0.220507f, -0.500000f }, + { 0.337318f, 0.085363f, -0.500000f }, + { 0.323186f, 0.436916f, -0.500000f }, + { 0.365584f, 0.470481f, -0.500000f }, + { 0.403566f, 0.520829f, -0.500000f }, + { 0.424765f, 0.591493f, -0.500000f }, + { 0.413282f, 0.672757f, -0.500000f }, + { 0.368234f, 0.741654f, -0.500000f }, + { 0.296687f, 0.776103f, -0.500000f }, + { 0.252522f, 0.786702f, -0.500000f }, + { 0.186274f, 0.777869f, -0.500000f }, + { 0.115610f, 0.745187f, -0.500000f }, + { 0.067912f, 0.690423f, -0.500000f }, + { 0.052013f, 0.629475f, -0.500000f }, + { 0.051129f, 0.560578f, -0.500000f }, + { 0.072329f, 0.511113f, -0.500000f }, + { 0.105894f, 0.477548f, -0.500000f }, + { 0.000000f, 0.197542f, -0.500000f }, + { -0.570827f, -0.535257f, -0.500000f }, + { -0.604407f, -0.622259f, -0.500000f }, + { -0.601354f, -0.817631f, -0.500000f }, + { -0.500616f, -0.480309f, -0.500000f }, + { -0.436509f, -0.457414f, -0.500000f }, + { -0.671566f, 0.113438f, -0.500000f }, + { -0.729567f, 0.137859f, -0.500000f }, + { -0.769252f, 0.185176f, -0.500000f }, + { -0.801050f, 0.252306f, -0.500000f }, + { -0.800167f, 0.322970f, -0.500000f }, + { -0.775435f, 0.383035f, -0.500000f }, + { -0.725970f, 0.435149f, -0.500000f }, + { -0.657073f, 0.463415f, -0.500000f }, + { -0.579342f, 0.465181f, -0.500000f }, + { -0.516628f, 0.435149f, -0.500000f }, + { -0.468930f, 0.382151f, -0.500000f }, + { -0.445081f, 0.321204f, -0.500000f }, + { -0.444198f, 0.268206f, -0.500000f }, + { -0.462747f, 0.220507f, -0.500000f }, + { -0.337318f, 0.085363f, -0.500000f }, + { -0.323186f, 0.436916f, -0.500000f }, + { -0.365584f, 0.470481f, -0.500000f }, + { -0.403566f, 0.520829f, -0.500000f }, + { -0.424765f, 0.591493f, -0.500000f }, + { -0.413282f, 0.672757f, -0.500000f }, + { -0.368234f, 0.741654f, -0.500000f }, + { -0.296687f, 0.776103f, -0.500000f }, + { -0.252522f, 0.786702f, -0.500000f }, + { -0.186274f, 0.777869f, -0.500000f }, + { -0.115610f, 0.745187f, -0.500000f }, + { -0.067912f, 0.690423f, -0.500000f }, + { -0.052013f, 0.629475f, -0.500000f }, + { -0.051129f, 0.560578f, -0.500000f }, + { -0.072329f, 0.511113f, -0.500000f }, + { -0.105894f, 0.477548f, -0.500000f }, + { 0.462390f, -0.549696f, -1.500000f }, + { 0.332972f, -0.503475f, -1.500000f }, + { 0.505640f, -0.583544f, -1.500000f }, + { 0.525978f, -0.636236f, -1.500000f }, + { 0.524365f, -0.739429f, -1.500000f }, + { 0.000000f, -0.739429f, -1.500000f }, + { 0.611523f, 0.173007f, -1.500000f }, + { 0.681798f, 0.202596f, -1.500000f }, + { 0.702879f, 0.227731f, -1.500000f }, + { 0.722629f, 0.269426f, -1.500000f }, + { 0.722159f, 0.307031f, -1.500000f }, + { 0.708676f, 0.339775f, -1.500000f }, + { 0.680786f, 0.369160f, -1.500000f }, + { 0.640803f, 0.385563f, -1.500000f }, + { 0.596256f, 0.386576f, -1.500000f }, + { 0.564385f, 0.371313f, -1.500000f }, + { 0.536571f, 0.340409f, -1.500000f }, + { 0.523037f, 0.305821f, -1.500000f }, + { 0.522644f, 0.282247f, -1.500000f }, + { 0.553382f, 0.203206f, -1.500000f }, + { 0.266733f, -0.105648f, -1.500000f }, + { 0.243449f, 0.473532f, -1.500000f }, + { 0.309188f, 0.525576f, -1.500000f }, + { 0.332692f, 0.556731f, -1.500000f }, + { 0.344933f, 0.597535f, -1.500000f }, + { 0.338289f, 0.644553f, -1.500000f }, + { 0.314695f, 0.680638f, -1.500000f }, + { 0.270300f, 0.702013f, -1.500000f }, + { 0.248427f, 0.707263f, -1.500000f }, + { 0.208351f, 0.701920f, -1.500000f }, + { 0.163605f, 0.681225f, -1.500000f }, + { 0.138945f, 0.652911f, -1.500000f }, + { 0.130085f, 0.618950f, -1.500000f }, + { 0.129537f, 0.576148f, -1.500000f }, + { 0.138274f, 0.555761f, -1.500000f }, + { 0.196906f, 0.497129f, -1.500000f }, + { 0.000000f, -0.023532f, -1.500000f }, + { -0.462390f, -0.549696f, -1.500000f }, + { -0.332971f, -0.503475f, -1.500000f }, + { -0.505640f, -0.583544f, -1.500000f }, + { -0.525978f, -0.636236f, -1.500000f }, + { -0.524365f, -0.739429f, -1.500000f }, + { -0.611523f, 0.173007f, -1.500000f }, + { -0.681798f, 0.202596f, -1.500000f }, + { -0.702879f, 0.227731f, -1.500000f }, + { -0.722629f, 0.269426f, -1.500000f }, + { -0.722159f, 0.307031f, -1.500000f }, + { -0.708676f, 0.339775f, -1.500000f }, + { -0.680786f, 0.369160f, -1.500000f }, + { -0.640803f, 0.385563f, -1.500000f }, + { -0.596256f, 0.386576f, -1.500000f }, + { -0.564385f, 0.371313f, -1.500000f }, + { -0.536571f, 0.340409f, -1.500000f }, + { -0.523037f, 0.305821f, -1.500000f }, + { -0.522644f, 0.282247f, -1.500000f }, + { -0.553382f, 0.203206f, -1.500000f }, + { -0.266733f, -0.105648f, -1.500000f }, + { -0.243449f, 0.473532f, -1.500000f }, + { -0.309188f, 0.525576f, -1.500000f }, + { -0.332692f, 0.556731f, -1.500000f }, + { -0.344933f, 0.597535f, -1.500000f }, + { -0.338289f, 0.644553f, -1.500000f }, + { -0.314695f, 0.680638f, -1.500000f }, + { -0.270300f, 0.702013f, -1.500000f }, + { -0.248427f, 0.707263f, -1.500000f }, + { -0.208351f, 0.701920f, -1.500000f }, + { -0.163605f, 0.681225f, -1.500000f }, + { -0.138945f, 0.652911f, -1.500000f }, + { -0.130085f, 0.618950f, -1.500000f }, + { -0.129537f, 0.576148f, -1.500000f }, + { -0.138274f, 0.555761f, -1.500000f }, + { -0.196906f, 0.497129f, -1.500000f }, + }; + + constexpr face_v faces[] = { + {{139}, {135}, {129}}, + {{ 24}, { 36}, { 41}}, + {{ 74}, { 72}, { 73}}, + {{ 78}, { 79}, { 80}}, + {{ 75}, { 74}, { 73}}, + {{ 78}, { 80}, { 81}}, + {{ 76}, { 75}, { 73}}, + {{ 78}, { 81}, { 82}}, + {{ 77}, { 76}, { 73}}, + {{ 78}, { 82}, { 83}}, + {{ 77}, { 73}, { 78}}, + {{112}, {113}, { 77}}, + {{109}, {111}, {112}}, + {{110}, {109}, {112}}, + {{ 77}, { 78}, { 83}}, + {{ 83}, { 84}, { 85}}, + {{ 85}, { 86}, { 87}}, + {{ 87}, { 88}, { 89}}, + {{ 89}, { 90}, { 91}}, + {{ 87}, { 89}, { 91}}, + {{ 83}, { 85}, { 87}}, + {{110}, {112}, { 77}}, + {{116}, {115}, {114}}, + {{114}, {110}, { 77}}, + {{ 83}, { 87}, { 91}}, + {{ 83}, { 91}, { 92}}, + {{117}, {116}, {114}}, + {{119}, {118}, {117}}, + {{121}, {120}, {119}}, + {{123}, {122}, {121}}, + {{125}, {124}, {123}}, + {{127}, {126}, {125}}, + {{127}, {125}, {123}}, + {{123}, {121}, {119}}, + {{119}, {117}, {114}}, + {{ 77}, { 83}, { 92}}, + {{ 93}, { 94}, { 95}}, + {{ 77}, { 92}, { 93}}, + {{119}, {114}, { 77}}, + {{127}, {123}, {119}}, + {{128}, {127}, {119}}, + {{ 93}, { 95}, { 96}}, + {{ 96}, { 97}, { 98}}, + {{ 98}, { 99}, {100}}, + {{100}, {101}, {102}}, + {{102}, {103}, {104}}, + {{104}, {105}, {106}}, + {{104}, {106}, {107}}, + {{100}, {102}, {104}}, + {{ 96}, { 98}, {100}}, + {{ 93}, { 96}, {100}}, + {{100}, {104}, {107}}, + {{ 93}, {100}, {107}}, + {{128}, {119}, { 77}}, + {{131}, {130}, {129}}, + {{129}, {128}, { 77}}, + {{ 77}, { 93}, {107}}, + {{ 77}, {107}, {108}}, + {{132}, {131}, {129}}, + {{129}, { 77}, {108}}, + {{143}, {142}, {141}}, + {{129}, {108}, {143}}, + {{133}, {132}, {129}}, + {{135}, {134}, {133}}, + {{137}, {136}, {135}}, + {{139}, {138}, {137}}, + {{141}, {140}, {139}}, + {{129}, {143}, {141}}, + {{135}, {133}, {129}}, + {{139}, {137}, {135}}, + {{129}, {141}, {139}}, + {{ 1}, { 4}, { 5}}, + {{ 6}, { 7}, { 8}}, + {{ 2}, { 1}, { 5}}, + {{ 6}, { 8}, { 9}}, + {{ 3}, { 2}, { 5}}, + {{ 6}, { 9}, { 10}}, + {{ 0}, { 3}, { 5}}, + {{ 6}, { 10}, { 11}}, + {{ 0}, { 5}, { 6}}, + {{ 38}, { 39}, { 0}}, + {{ 40}, { 37}, { 38}}, + {{ 41}, { 40}, { 38}}, + {{ 38}, { 0}, { 6}}, + {{ 6}, { 11}, { 12}}, + {{ 12}, { 13}, { 14}}, + {{ 14}, { 15}, { 16}}, + {{ 16}, { 17}, { 18}}, + {{ 16}, { 18}, { 19}}, + {{ 12}, { 14}, { 16}}, + {{ 41}, { 38}, { 6}}, + {{ 44}, { 43}, { 42}}, + {{ 6}, { 12}, { 16}}, + {{ 6}, { 16}, { 19}}, + {{ 45}, { 44}, { 42}}, + {{ 47}, { 46}, { 45}}, + {{ 49}, { 48}, { 47}}, + {{ 51}, { 50}, { 49}}, + {{ 53}, { 52}, { 51}}, + {{ 55}, { 54}, { 53}}, + {{ 55}, { 53}, { 51}}, + {{ 51}, { 49}, { 47}}, + {{ 47}, { 45}, { 42}}, + {{ 6}, { 19}, { 20}}, + {{ 21}, { 22}, { 23}}, + {{ 41}, { 6}, { 20}}, + {{ 21}, { 23}, { 24}}, + {{ 41}, { 20}, { 21}}, + {{ 47}, { 42}, { 41}}, + {{ 55}, { 51}, { 47}}, + {{ 56}, { 55}, { 47}}, + {{ 41}, { 21}, { 24}}, + {{ 24}, { 25}, { 26}}, + {{ 26}, { 27}, { 28}}, + {{ 28}, { 29}, { 30}}, + {{ 30}, { 31}, { 32}}, + {{ 32}, { 33}, { 34}}, + {{ 32}, { 34}, { 35}}, + {{ 28}, { 30}, { 32}}, + {{ 24}, { 26}, { 28}}, + {{ 56}, { 47}, { 41}}, + {{ 59}, { 58}, { 57}}, + {{ 60}, { 59}, { 57}}, + {{ 62}, { 61}, { 60}}, + {{ 64}, { 63}, { 62}}, + {{ 66}, { 65}, { 64}}, + {{ 68}, { 67}, { 66}}, + {{ 70}, { 69}, { 68}}, + {{ 71}, { 70}, { 68}}, + {{ 68}, { 66}, { 64}}, + {{ 64}, { 62}, { 60}}, + {{ 64}, { 60}, { 57}}, + {{ 71}, { 68}, { 64}}, + {{ 36}, { 71}, { 64}}, + {{ 64}, { 57}, { 56}}, + {{ 24}, { 28}, { 32}}, + {{ 24}, { 32}, { 35}}, + {{ 36}, { 64}, { 56}}, + {{ 24}, { 35}, { 36}}, + {{ 36}, { 56}, { 41}}, + }; + + constexpr uint32_t num_faces = (sizeof (faces)) / (sizeof (face_v)); + +} diff --git a/chess/queen.obj b/chess/queen.obj new file mode 100644 index 0000000..3eae5e0 --- /dev/null +++ b/chess/queen.obj @@ -0,0 +1,288 @@ +# Blender 3.3.6 +# www.blender.org +o Queen +v 0.000000 -0.817631 -0.500000 +v 0.570827 -0.535257 -0.500000 +v 0.604407 -0.622259 -0.500000 +v 0.601354 -0.817631 -0.500000 +v 0.500616 -0.480309 -0.500000 +v 0.436509 -0.457414 -0.500000 +v 0.671566 0.113438 -0.500000 +v 0.729567 0.137859 -0.500000 +v 0.769252 0.185176 -0.500000 +v 0.801050 0.252306 -0.500000 +v 0.800167 0.322970 -0.500000 +v 0.775435 0.383035 -0.500000 +v 0.725970 0.435149 -0.500000 +v 0.657073 0.463415 -0.500000 +v 0.579342 0.465181 -0.500000 +v 0.516628 0.435149 -0.500000 +v 0.468930 0.382151 -0.500000 +v 0.445081 0.321204 -0.500000 +v 0.444198 0.268206 -0.500000 +v 0.462747 0.220507 -0.500000 +v 0.337318 0.085363 -0.500000 +v 0.323186 0.436916 -0.500000 +v 0.365584 0.470481 -0.500000 +v 0.403566 0.520829 -0.500000 +v 0.424765 0.591493 -0.500000 +v 0.413282 0.672757 -0.500000 +v 0.368234 0.741654 -0.500000 +v 0.296687 0.776103 -0.500000 +v 0.252522 0.786702 -0.500000 +v 0.186274 0.777869 -0.500000 +v 0.115610 0.745187 -0.500000 +v 0.067912 0.690423 -0.500000 +v 0.052013 0.629475 -0.500000 +v 0.051129 0.560578 -0.500000 +v 0.072329 0.511113 -0.500000 +v 0.105894 0.477548 -0.500000 +v 0.000000 0.197542 -0.500000 +v -0.570827 -0.535257 -0.500000 +v -0.604407 -0.622259 -0.500000 +v -0.601354 -0.817631 -0.500000 +v -0.500616 -0.480309 -0.500000 +v -0.436509 -0.457414 -0.500000 +v -0.671566 0.113438 -0.500000 +v -0.729567 0.137859 -0.500000 +v -0.769252 0.185176 -0.500000 +v -0.801050 0.252306 -0.500000 +v -0.800167 0.322970 -0.500000 +v -0.775435 0.383035 -0.500000 +v -0.725970 0.435149 -0.500000 +v -0.657073 0.463415 -0.500000 +v -0.579342 0.465181 -0.500000 +v -0.516628 0.435149 -0.500000 +v -0.468930 0.382151 -0.500000 +v -0.445081 0.321204 -0.500000 +v -0.444198 0.268206 -0.500000 +v -0.462747 0.220507 -0.500000 +v -0.337318 0.085363 -0.500000 +v -0.323186 0.436916 -0.500000 +v -0.365584 0.470481 -0.500000 +v -0.403566 0.520829 -0.500000 +v -0.424765 0.591493 -0.500000 +v -0.413282 0.672757 -0.500000 +v -0.368234 0.741654 -0.500000 +v -0.296687 0.776103 -0.500000 +v -0.252522 0.786702 -0.500000 +v -0.186274 0.777869 -0.500000 +v -0.115610 0.745187 -0.500000 +v -0.067912 0.690423 -0.500000 +v -0.052013 0.629475 -0.500000 +v -0.051129 0.560578 -0.500000 +v -0.072329 0.511113 -0.500000 +v -0.105894 0.477548 -0.500000 +v 0.462390 -0.549696 -1.500000 +v 0.332972 -0.503475 -1.500000 +v 0.505640 -0.583544 -1.500000 +v 0.525978 -0.636236 -1.500000 +v 0.524365 -0.739429 -1.500000 +v 0.000000 -0.739429 -1.500000 +v 0.611523 0.173007 -1.500000 +v 0.681798 0.202596 -1.500000 +v 0.702879 0.227731 -1.500000 +v 0.722629 0.269426 -1.500000 +v 0.722159 0.307031 -1.500000 +v 0.708676 0.339775 -1.500000 +v 0.680786 0.369160 -1.500000 +v 0.640803 0.385563 -1.500000 +v 0.596256 0.386576 -1.500000 +v 0.564385 0.371313 -1.500000 +v 0.536571 0.340409 -1.500000 +v 0.523037 0.305821 -1.500000 +v 0.522644 0.282247 -1.500000 +v 0.553382 0.203206 -1.500000 +v 0.266733 -0.105648 -1.500000 +v 0.243449 0.473532 -1.500000 +v 0.309188 0.525576 -1.500000 +v 0.332692 0.556731 -1.500000 +v 0.344933 0.597535 -1.500000 +v 0.338289 0.644553 -1.500000 +v 0.314695 0.680638 -1.500000 +v 0.270300 0.702013 -1.500000 +v 0.248427 0.707263 -1.500000 +v 0.208351 0.701920 -1.500000 +v 0.163605 0.681225 -1.500000 +v 0.138945 0.652911 -1.500000 +v 0.130085 0.618950 -1.500000 +v 0.129537 0.576148 -1.500000 +v 0.138274 0.555761 -1.500000 +v 0.196906 0.497129 -1.500000 +v 0.000000 -0.023532 -1.500000 +v -0.462390 -0.549696 -1.500000 +v -0.332971 -0.503475 -1.500000 +v -0.505640 -0.583544 -1.500000 +v -0.525978 -0.636236 -1.500000 +v -0.524365 -0.739429 -1.500000 +v -0.611523 0.173007 -1.500000 +v -0.681798 0.202596 -1.500000 +v -0.702879 0.227731 -1.500000 +v -0.722629 0.269426 -1.500000 +v -0.722159 0.307031 -1.500000 +v -0.708676 0.339775 -1.500000 +v -0.680786 0.369160 -1.500000 +v -0.640803 0.385563 -1.500000 +v -0.596256 0.386576 -1.500000 +v -0.564385 0.371313 -1.500000 +v -0.536571 0.340409 -1.500000 +v -0.523037 0.305821 -1.500000 +v -0.522644 0.282247 -1.500000 +v -0.553382 0.203206 -1.500000 +v -0.266733 -0.105648 -1.500000 +v -0.243449 0.473532 -1.500000 +v -0.309188 0.525576 -1.500000 +v -0.332692 0.556731 -1.500000 +v -0.344933 0.597535 -1.500000 +v -0.338289 0.644553 -1.500000 +v -0.314695 0.680638 -1.500000 +v -0.270300 0.702013 -1.500000 +v -0.248427 0.707263 -1.500000 +v -0.208351 0.701920 -1.500000 +v -0.163605 0.681225 -1.500000 +v -0.138945 0.652911 -1.500000 +v -0.130085 0.618950 -1.500000 +v -0.129537 0.576148 -1.500000 +v -0.138274 0.555761 -1.500000 +v -0.196906 0.497129 -1.500000 +s 0 +f 140 136 130 +f 25 37 42 +f 75 73 74 +f 79 80 81 +f 76 75 74 +f 79 81 82 +f 77 76 74 +f 79 82 83 +f 78 77 74 +f 79 83 84 +f 78 74 79 +f 113 114 78 +f 110 112 113 +f 111 110 113 +f 78 79 84 +f 84 85 86 +f 86 87 88 +f 88 89 90 +f 90 91 92 +f 88 90 92 +f 84 86 88 +f 111 113 78 +f 117 116 115 +f 115 111 78 +f 84 88 92 +f 84 92 93 +f 118 117 115 +f 120 119 118 +f 122 121 120 +f 124 123 122 +f 126 125 124 +f 128 127 126 +f 128 126 124 +f 124 122 120 +f 120 118 115 +f 78 84 93 +f 94 95 96 +f 78 93 94 +f 120 115 78 +f 128 124 120 +f 129 128 120 +f 94 96 97 +f 97 98 99 +f 99 100 101 +f 101 102 103 +f 103 104 105 +f 105 106 107 +f 105 107 108 +f 101 103 105 +f 97 99 101 +f 94 97 101 +f 101 105 108 +f 94 101 108 +f 129 120 78 +f 132 131 130 +f 130 129 78 +f 78 94 108 +f 78 108 109 +f 133 132 130 +f 130 78 109 +f 144 143 142 +f 130 109 144 +f 134 133 130 +f 136 135 134 +f 138 137 136 +f 140 139 138 +f 142 141 140 +f 130 144 142 +f 136 134 130 +f 140 138 136 +f 130 142 140 +f 2 5 6 +f 7 8 9 +f 3 2 6 +f 7 9 10 +f 4 3 6 +f 7 10 11 +f 1 4 6 +f 7 11 12 +f 1 6 7 +f 39 40 1 +f 41 38 39 +f 42 41 39 +f 39 1 7 +f 7 12 13 +f 13 14 15 +f 15 16 17 +f 17 18 19 +f 17 19 20 +f 13 15 17 +f 42 39 7 +f 45 44 43 +f 7 13 17 +f 7 17 20 +f 46 45 43 +f 48 47 46 +f 50 49 48 +f 52 51 50 +f 54 53 52 +f 56 55 54 +f 56 54 52 +f 52 50 48 +f 48 46 43 +f 7 20 21 +f 22 23 24 +f 42 7 21 +f 22 24 25 +f 42 21 22 +f 48 43 42 +f 56 52 48 +f 57 56 48 +f 42 22 25 +f 25 26 27 +f 27 28 29 +f 29 30 31 +f 31 32 33 +f 33 34 35 +f 33 35 36 +f 29 31 33 +f 25 27 29 +f 57 48 42 +f 60 59 58 +f 61 60 58 +f 63 62 61 +f 65 64 63 +f 67 66 65 +f 69 68 67 +f 71 70 69 +f 72 71 69 +f 69 67 65 +f 65 63 61 +f 65 61 58 +f 72 69 65 +f 37 72 65 +f 65 58 57 +f 25 29 33 +f 25 33 36 +f 37 65 57 +f 25 36 37 +f 37 57 42 diff --git a/chess/render.cpp b/chess/render.cpp new file mode 100644 index 0000000..fd123b4 --- /dev/null +++ b/chess/render.cpp @@ -0,0 +1,219 @@ +#include "chess/bishop.hpp" +#include "chess/king.hpp" +#include "chess/knight.hpp" +#include "chess/pawn.hpp" +#include "chess/queen.hpp" +#include "chess/rook.hpp" +#include "chess/square.hpp" +#include "chess/circle.hpp" +#include "chess/cursor.hpp" + +#include "sh7091/store_queue.hpp" +#include "sh7091/serial.hpp" + +#include "holly/ta_parameter.hpp" +#include "holly/isp_tsp.hpp" +#include "holly/ta_global_parameter.hpp" +#include "holly/ta_vertex_parameter.hpp" + +#include "render.hpp" + +namespace render { + +static inline vec3 transform(const vec3 v, float cell_x, float cell_y, float scale) +{ + float x = v.x * 28.f * scale; + float y = -v.y * 28.f * scale; + x += 124.f + cell_x * 56.f; + y += 44.f + (7 - cell_y) * 56.f; + + return {x, y, 1.f/(v.z + 10.0f)}; +} + +static void draw_model(vec3 const * const vertices, + face_v const * const faces, + uint32_t num_faces, + uint32_t base_color, + float cell_x, + float cell_y, + float scale, + bool always) +{ + const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume + | para_control::list_type::opaque + | obj_control::col_type::packed_color; + + const uint32_t isp_tsp_instruction_word = (always ? isp_tsp_instruction_word::depth_compare_mode::always + : isp_tsp_instruction_word::depth_compare_mode::greater) + | isp_tsp_instruction_word::culling_mode::no_culling; + + const uint32_t tsp_instruction_word = tsp_instruction_word::src_alpha_instr::one + | tsp_instruction_word::dst_alpha_instr::zero + | tsp_instruction_word::fog_control::no_fog; + + + for (uint32_t face_ix = 0; face_ix < num_faces; face_ix++) { + + *reinterpret_cast(store_queue) = + ta_global_parameter::polygon_type_0(parameter_control_word, + isp_tsp_instruction_word, + tsp_instruction_word, + 0, // texture_control_word + 0, // data_size_for_sort_dma + 0 // next_address_for_sort_dma + ); + sq_transfer_32byte(ta_fifo_polygon_converter); + + auto& face = faces[face_ix]; + + constexpr uint32_t strip_length = 3; + for (uint32_t i = 0; i < strip_length; i++) { + uint32_t vertex_ix = face[i].vertex; + const auto vertex = transform(vertices[vertex_ix], cell_x, cell_y, scale); + + bool end_of_strip = i == strip_length - 1; + float z = __builtin_fabsf(vertices[vertex_ix].z); + uint32_t color = (z < 0.9f && z > 0.1f) ? 0xff000000 : base_color; + *reinterpret_cast(store_queue) = + ta_vertex_parameter::polygon_type_0(polygon_vertex_parameter_control_word(end_of_strip), + vertex.x, vertex.y, vertex.z, + color); + sq_transfer_32byte(ta_fifo_polygon_converter); + } + } +} + +void draw_board() +{ + for (int i = 0; i < 8 * 8; i++) { + int x = i % 8; + int y = i / 8; + bool white = (x % 2 == 0) ^ (y % 2 == 0); + + constexpr uint32_t cream = 0xffebecd0; + constexpr uint32_t green = 0xff739552; + uint32_t color = white ? cream : green; + + draw_model(square::vertices, + square::faces, + square::num_faces, + color, + x, y, + 1.0f, // scale + true); // always + + } +} + +void draw_illumination(int8_t position) +{ + auto [x, y] = chess::position_to_xy(position); + + constexpr uint32_t cream = 0xfff5f681; + draw_model(square::vertices, + square::faces, + square::num_faces, + cream, + x, y, + 1.0f, // scale + true); // always +} + +void draw_moves(chess::moves_list& moves) +{ + for (int i = 0; i < moves.length; i++) { + chess::xy move_xy = chess::position_to_xy(moves.moves[i].to_position); + + uint32_t color = 0xff777777; + draw_model(circle::vertices, + circle::faces, + circle::num_faces, + color, + move_xy.x, move_xy.y, + 0.5f, // scale + true); // always + } +} + +void draw_piece(int8_t type, int8_t x, int8_t y) +{ + bool white = type > 0; + uint32_t color = white ? 0xffdddddd : 0xff444444; + switch (__builtin_abs(type)) { + case chess::piece_type::pawn: + draw_model(pawn::vertices, + pawn::faces, + pawn::num_faces, + color, x, y, + 1.0f, // scale + false); // always + break; + case chess::piece_type::knight: + draw_model(knight::vertices, + knight::faces, + knight::num_faces, + color, x, y, + 1.0f, // scale + false); // always + break; + case chess::piece_type::bishop: + draw_model(bishop::vertices, + bishop::faces, + bishop::num_faces, + color, x, y, + 1.0f, // scale + false); // always + break; + case chess::piece_type::rook: + draw_model(rook::vertices, + rook::faces, + rook::num_faces, + color, x, y, + 1.0f, // scale + false); // always + break; + case chess::piece_type::queen: + draw_model(queen::vertices, + queen::faces, + queen::num_faces, + color, x, y, + 1.0f, // scale + false); // always + break; + case chess::piece_type::king: + draw_model(king::vertices, + king::faces, + king::num_faces, + color, x, y, + 1.0f, // scale + false); // always + break; + default: + break; + } +} + +void draw_pieces(const chess::game_state& game_state) +{ + const auto& piece_list = game_state.piece_list; + for (int i = 0; i < piece_list.length; i++) { + chess::piece * piece = piece_list.piece[i]; + int8_t position = piece_list_to_position(game_state.board, piece); + auto [x, y] = chess::position_to_xy(position); + draw_piece(piece->type, x, y); + } +} + +void draw_cursor(float x, float y) +{ + uint32_t color = 0xffff00ff; + draw_model(cursor::vertices, + cursor::faces, + cursor::num_faces, + color, + x, y, + 1.0f, // scale + true); // always +} + +} // namespace render diff --git a/chess/render.hpp b/chess/render.hpp new file mode 100644 index 0000000..01c663c --- /dev/null +++ b/chess/render.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include "chess.hpp" + +namespace render { + +void draw_board(); +void draw_illumination(int8_t position); +void draw_moves(chess::moves_list& moves); +void draw_pieces(const chess::game_state& game_state); +void draw_cursor(float x, float y); + +} diff --git a/chess/rook.blend b/chess/rook.blend new file mode 100644 index 0000000..9f641de Binary files /dev/null and b/chess/rook.blend differ diff --git a/chess/rook.hpp b/chess/rook.hpp new file mode 100644 index 0000000..0f0ac01 --- /dev/null +++ b/chess/rook.hpp @@ -0,0 +1,150 @@ +#pragma once + +#include "geometry/geometry.hpp" + +namespace rook { + constexpr vec3 vertices[] = { + { 0.586848f, -0.813234f, -0.500000f }, + { 0.510530f, -0.501607f, -0.500000f }, + { 0.560137f, -0.549941f, -0.500000f }, + { 0.585576f, -0.632617f, -0.500000f }, + { 0.430398f, -0.463448f, -0.500000f }, + { 0.350265f, 0.154720f, -0.500000f }, + { 0.411318f, 0.186519f, -0.500000f }, + { 0.453293f, 0.236125f, -0.500000f }, + { 0.477460f, 0.295906f, -0.500000f }, + { 0.502899f, 0.578279f, -0.500000f }, + { 0.438029f, 0.613894f, -0.500000f }, + { 0.354081f, 0.636789f, -0.500000f }, + { 0.271404f, 0.639333f, -0.500000f }, + { 0.226885f, 0.458716f, -0.500000f }, + { 0.169648f, 0.461260f, -0.500000f }, + { 0.144209f, 0.662228f, -0.500000f }, + { 0.071708f, 0.676220f, -0.500000f }, + { -0.586848f, -0.813234f, -0.500000f }, + { -0.510530f, -0.501607f, -0.500000f }, + { -0.560137f, -0.549941f, -0.500000f }, + { -0.585576f, -0.632617f, -0.500000f }, + { -0.430398f, -0.463448f, -0.500000f }, + { -0.350265f, 0.154720f, -0.500000f }, + { -0.411318f, 0.186519f, -0.500000f }, + { -0.453293f, 0.236125f, -0.500000f }, + { -0.477460f, 0.295906f, -0.500000f }, + { -0.502899f, 0.578279f, -0.500000f }, + { -0.438029f, 0.613894f, -0.500000f }, + { -0.354081f, 0.636789f, -0.500000f }, + { -0.271404f, 0.639333f, -0.500000f }, + { -0.226885f, 0.458716f, -0.500000f }, + { -0.169648f, 0.461260f, -0.500000f }, + { -0.144209f, 0.662228f, -0.500000f }, + { -0.071708f, 0.676220f, -0.500000f }, + { 0.000000f, 0.680318f, -0.500000f }, + { 0.351645f, -0.520584f, -1.500000f }, + { 0.257857f, 0.202929f, -1.500000f }, + { 0.461029f, -0.572671f, -1.500000f }, + { 0.484886f, -0.595916f, -1.500000f }, + { 0.500222f, -0.645759f, -1.500000f }, + { 0.500800f, -0.740978f, -1.500000f }, + { 0.356946f, 0.254538f, -1.500000f }, + { 0.379213f, 0.280854f, -1.500000f }, + { 0.393497f, 0.316187f, -1.500000f }, + { 0.412785f, 0.530280f, -1.500000f }, + { 0.405782f, 0.534124f, -1.500000f }, + { 0.341350f, 0.551697f, -1.500000f }, + { 0.337831f, 0.551805f, -1.500000f }, + { 0.293080f, 0.370246f, -1.500000f }, + { 0.093922f, 0.379098f, -1.500000f }, + { 0.067218f, 0.590066f, -1.500000f }, + { 0.061127f, 0.591241f, -1.500000f }, + { -0.351645f, -0.520584f, -1.500000f }, + { -0.257857f, 0.202929f, -1.500000f }, + { -0.461029f, -0.572671f, -1.500000f }, + { -0.484886f, -0.595916f, -1.500000f }, + { -0.500222f, -0.645759f, -1.500000f }, + { -0.500800f, -0.740978f, -1.500000f }, + { -0.356946f, 0.254538f, -1.500000f }, + { -0.379213f, 0.280854f, -1.500000f }, + { -0.393497f, 0.316187f, -1.500000f }, + { -0.412785f, 0.530280f, -1.500000f }, + { -0.405782f, 0.534124f, -1.500000f }, + { -0.341350f, 0.551697f, -1.500000f }, + { -0.337831f, 0.551805f, -1.500000f }, + { -0.293080f, 0.370246f, -1.500000f }, + { -0.093922f, 0.379098f, -1.500000f }, + { -0.067218f, 0.590066f, -1.500000f }, + { -0.061127f, 0.591241f, -1.500000f }, + { 0.000000f, 0.594735f, -1.500000f }, + }; + + constexpr face_v faces[] = { + {{65}, {61}, {53}}, + {{28}, {24}, {22}}, + {{36}, {41}, {42}}, + {{42}, {43}, {44}}, + {{44}, {45}, {46}}, + {{46}, {47}, {48}}, + {{49}, {50}, {51}}, + {{44}, {46}, {48}}, + {{49}, {51}, {69}}, + {{42}, {44}, {48}}, + {{49}, {69}, {68}}, + {{42}, {48}, {49}}, + {{36}, {42}, {49}}, + {{49}, {68}, {67}}, + {{49}, {67}, {66}}, + {{35}, {36}, {49}}, + {{38}, {37}, {35}}, + {{40}, {39}, {38}}, + {{56}, {57}, {40}}, + {{54}, {55}, {56}}, + {{52}, {54}, {56}}, + {{56}, {40}, {38}}, + {{35}, {49}, {66}}, + {{56}, {38}, {35}}, + {{52}, {56}, {35}}, + {{35}, {66}, {65}}, + {{65}, {64}, {63}}, + {{63}, {62}, {61}}, + {{61}, {60}, {59}}, + {{59}, {58}, {53}}, + {{53}, {52}, {35}}, + {{65}, {63}, {61}}, + {{53}, {35}, {65}}, + {{61}, {59}, {53}}, + {{ 5}, { 6}, { 7}}, + {{ 7}, { 8}, { 9}}, + {{ 9}, {10}, {11}}, + {{11}, {12}, {13}}, + {{14}, {15}, {16}}, + {{ 9}, {11}, {13}}, + {{14}, {16}, {34}}, + {{ 7}, { 9}, {13}}, + {{14}, {34}, {33}}, + {{ 7}, {13}, {14}}, + {{ 5}, { 7}, {14}}, + {{14}, {33}, {32}}, + {{14}, {32}, {31}}, + {{ 4}, { 5}, {14}}, + {{ 2}, { 1}, { 4}}, + {{ 0}, { 3}, { 2}}, + {{20}, {17}, { 0}}, + {{18}, {19}, {20}}, + {{21}, {18}, {20}}, + {{20}, { 0}, { 2}}, + {{ 4}, {14}, {31}}, + {{20}, { 2}, { 4}}, + {{21}, {20}, { 4}}, + {{ 4}, {31}, {30}}, + {{30}, {29}, {28}}, + {{28}, {27}, {26}}, + {{26}, {25}, {24}}, + {{24}, {23}, {22}}, + {{22}, {21}, { 4}}, + {{ 4}, {30}, {28}}, + {{28}, {26}, {24}}, + {{22}, { 4}, {28}}, + }; + + constexpr uint32_t num_faces = (sizeof (faces)) / (sizeof (face_v)); + +} diff --git a/chess/rook.obj b/chess/rook.obj new file mode 100644 index 0000000..f98a44e --- /dev/null +++ b/chess/rook.obj @@ -0,0 +1,140 @@ +# Blender 3.3.6 +# www.blender.org +o Rook +v 0.586848 -0.813234 -0.500000 +v 0.510530 -0.501607 -0.500000 +v 0.560137 -0.549941 -0.500000 +v 0.585576 -0.632617 -0.500000 +v 0.430398 -0.463448 -0.500000 +v 0.350265 0.154720 -0.500000 +v 0.411318 0.186519 -0.500000 +v 0.453293 0.236125 -0.500000 +v 0.477460 0.295906 -0.500000 +v 0.502899 0.578279 -0.500000 +v 0.438029 0.613894 -0.500000 +v 0.354081 0.636789 -0.500000 +v 0.271404 0.639333 -0.500000 +v 0.226885 0.458716 -0.500000 +v 0.169648 0.461260 -0.500000 +v 0.144209 0.662228 -0.500000 +v 0.071708 0.676220 -0.500000 +v -0.586848 -0.813234 -0.500000 +v -0.510530 -0.501607 -0.500000 +v -0.560137 -0.549941 -0.500000 +v -0.585576 -0.632617 -0.500000 +v -0.430398 -0.463448 -0.500000 +v -0.350265 0.154720 -0.500000 +v -0.411318 0.186519 -0.500000 +v -0.453293 0.236125 -0.500000 +v -0.477460 0.295906 -0.500000 +v -0.502899 0.578279 -0.500000 +v -0.438029 0.613894 -0.500000 +v -0.354081 0.636789 -0.500000 +v -0.271404 0.639333 -0.500000 +v -0.226885 0.458716 -0.500000 +v -0.169648 0.461260 -0.500000 +v -0.144209 0.662228 -0.500000 +v -0.071708 0.676220 -0.500000 +v 0.000000 0.680318 -0.500000 +v 0.351645 -0.520584 -1.500000 +v 0.257857 0.202929 -1.500000 +v 0.461029 -0.572671 -1.500000 +v 0.484886 -0.595916 -1.500000 +v 0.500222 -0.645759 -1.500000 +v 0.500800 -0.740978 -1.500000 +v 0.356946 0.254538 -1.500000 +v 0.379213 0.280854 -1.500000 +v 0.393497 0.316187 -1.500000 +v 0.412785 0.530280 -1.500000 +v 0.405782 0.534124 -1.500000 +v 0.341350 0.551697 -1.500000 +v 0.337831 0.551805 -1.500000 +v 0.293080 0.370246 -1.500000 +v 0.093922 0.379098 -1.500000 +v 0.067218 0.590066 -1.500000 +v 0.061127 0.591241 -1.500000 +v -0.351645 -0.520584 -1.500000 +v -0.257857 0.202929 -1.500000 +v -0.461029 -0.572671 -1.500000 +v -0.484886 -0.595916 -1.500000 +v -0.500222 -0.645759 -1.500000 +v -0.500800 -0.740978 -1.500000 +v -0.356946 0.254538 -1.500000 +v -0.379213 0.280854 -1.500000 +v -0.393497 0.316187 -1.500000 +v -0.412785 0.530280 -1.500000 +v -0.405782 0.534124 -1.500000 +v -0.341350 0.551697 -1.500000 +v -0.337831 0.551805 -1.500000 +v -0.293080 0.370246 -1.500000 +v -0.093922 0.379098 -1.500000 +v -0.067218 0.590066 -1.500000 +v -0.061127 0.591241 -1.500000 +v 0.000000 0.594735 -1.500000 +s 0 +f 66 62 54 +f 29 25 23 +f 37 42 43 +f 43 44 45 +f 45 46 47 +f 47 48 49 +f 50 51 52 +f 45 47 49 +f 50 52 70 +f 43 45 49 +f 50 70 69 +f 43 49 50 +f 37 43 50 +f 50 69 68 +f 50 68 67 +f 36 37 50 +f 39 38 36 +f 41 40 39 +f 57 58 41 +f 55 56 57 +f 53 55 57 +f 57 41 39 +f 36 50 67 +f 57 39 36 +f 53 57 36 +f 36 67 66 +f 66 65 64 +f 64 63 62 +f 62 61 60 +f 60 59 54 +f 54 53 36 +f 66 64 62 +f 54 36 66 +f 62 60 54 +f 6 7 8 +f 8 9 10 +f 10 11 12 +f 12 13 14 +f 15 16 17 +f 10 12 14 +f 15 17 35 +f 8 10 14 +f 15 35 34 +f 8 14 15 +f 6 8 15 +f 15 34 33 +f 15 33 32 +f 5 6 15 +f 3 2 5 +f 1 4 3 +f 21 18 1 +f 19 20 21 +f 22 19 21 +f 21 1 3 +f 5 15 32 +f 21 3 5 +f 22 21 5 +f 5 32 31 +f 31 30 29 +f 29 28 27 +f 27 26 25 +f 25 24 23 +f 23 22 5 +f 5 31 29 +f 29 27 25 +f 23 5 29 diff --git a/chess/square.blend b/chess/square.blend new file mode 100644 index 0000000..5b165e7 Binary files /dev/null and b/chess/square.blend differ diff --git a/chess/square.hpp b/chess/square.hpp new file mode 100644 index 0000000..4a97c01 --- /dev/null +++ b/chess/square.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "geometry/geometry.hpp" + +namespace square { + constexpr vec3 vertices[] = { + { -1.000000f, 1.000000f, -0.000000f }, + { 1.000000f, 1.000000f, -0.000000f }, + { -1.000000f, -1.000000f, 0.000000f }, + { 1.000000f, -1.000000f, 0.000000f }, + }; + + constexpr face_v faces[] = { + {{1}, {2}, {0}}, + {{1}, {3}, {2}}, + }; + + constexpr uint32_t num_faces = (sizeof (faces)) / (sizeof (face_v)); + +} diff --git a/chess/square.obj b/chess/square.obj new file mode 100644 index 0000000..e0d83a7 --- /dev/null +++ b/chess/square.obj @@ -0,0 +1,10 @@ +# Blender 3.3.6 +# www.blender.org +o Square +v -1.000000 1.000000 -0.000000 +v 1.000000 1.000000 -0.000000 +v -1.000000 -1.000000 0.000000 +v 1.000000 -1.000000 0.000000 +s 0 +f 2 3 1 +f 2 4 3 diff --git a/geometry/geometry.hpp b/geometry/geometry.hpp index c511b09..2de40a6 100644 --- a/geometry/geometry.hpp +++ b/geometry/geometry.hpp @@ -24,6 +24,10 @@ struct vertex__normal { uint16_t normal; }; +struct vertex { + uint16_t vertex; +}; + struct position__color { vec3 position; vec3 color; @@ -32,3 +36,5 @@ struct position__color { using face_vtn = vertex__texture__normal[3]; using face_vn = vertex__normal[3]; + +using face_v = vertex[3]; diff --git a/tools/obj_to_cpp.py b/tools/obj_to_cpp.py index 12e6418..7167468 100644 --- a/tools/obj_to_cpp.py +++ b/tools/obj_to_cpp.py @@ -25,6 +25,10 @@ class TextureCoordinate: u: float v: float +@dataclass +class VertexIx: + vertex: int + @dataclass class VertexNormal: vertex: int @@ -37,7 +41,8 @@ class VertexTextureNormal: normal: int Face = Union[tuple[VertexTextureNormal, VertexTextureNormal, VertexTextureNormal], - tuple[VertexNormal, VertexNormal, VertexNormal]] + tuple[VertexNormal, VertexNormal, VertexNormal], + tuple[VertexIx, VertexIx, VertexIx]] name = None @@ -76,18 +81,21 @@ def parse_face(line): assert h == 'f' assert len(tri) == 3 def parse_ixs(ixs): - ix = ixs.split('/') - assert len(ix) == 3 - vertex_ix, uv_ix, normal_ix = [ - maybe_int(iix, offset=-1) - for iix in ix - ] - assert vertex_ix is not None - assert normal_ix is not None - if uv_ix is None: - return VertexNormal(vertex_ix, normal_ix) + if '/' in ixs: + ix = ixs.split('/') + assert len(ix) == 3 + vertex_ix, uv_ix, normal_ix = [ + maybe_int(iix, offset=-1) + for iix in ix + ] + assert vertex_ix is not None + assert normal_ix is not None + if uv_ix is None: + return VertexNormal(vertex_ix, normal_ix) + else: + return VertexTextureNormal(vertex_ix, uv_ix, normal_ix) else: - return VertexTextureNormal(vertex_ix, uv_ix, normal_ix) + return VertexIx(int(ixs)) return tuple(map(parse_ixs, tri)) @@ -130,7 +138,9 @@ def generate_texture_coordinates(texture_coordinates): yield "" def face_type_str(face_type): - if face_type is VertexNormal: + if face_type is VertexIx: + return "face_v" + elif face_type is VertexNormal: return "face_vn" elif face_type is VertexTextureNormal: return "face_vtn" @@ -139,10 +149,12 @@ def face_type_str(face_type): def generate_faces(faces, face_type): def face_coords(vtn): - if face_type is VertexNormal: - return [vtn.vertex, vtn.normal] + if face_type is VertexIx: + return [vtn.vertex - 1] + elif face_type is VertexNormal: + return [vtn.vertex - 1, vtn.normal - 1] elif face_type is VertexTextureNormal: - return [vtn.vertex, vtn.texture, vtn.normal] + return [vtn.vertex - 1, vtn.texture - 1, vtn.normal - 1] else: assert False, face_type max_ix = max( @@ -169,14 +181,15 @@ def generate_namespace(vertices, texture_coordinates, normals, faces, face_type) assert name is not None yield "#pragma once" yield "" - yield '#include "geometry.hpp"' + yield '#include "geometry/geometry.hpp"' yield "" yield f"namespace {name} {{" yield from generate_vertices(vertices) if texture_coordinates != []: yield from generate_texture_coordinates(texture_coordinates) - yield from generate_normals(normals) + if normals != []: + yield from generate_normals(normals) yield from generate_faces(faces, face_type) yield "}"