From 4e3e592b7f75d8892a5ef90c7a77dcc198151456 Mon Sep 17 00:00:00 2001 From: violette Date: Thu, 21 Aug 2025 22:24:02 -0400 Subject: [PATCH] idk wip --- source/main.c | 141 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 84 insertions(+), 57 deletions(-) diff --git a/source/main.c b/source/main.c index 96760e8..5d58d7a 100644 --- a/source/main.c +++ b/source/main.c @@ -88,7 +88,7 @@ volatile FIXED_POINT K2 = (2 * CUBE_WIDTH) + 20; #define SQ(n) (n * n) #define SQ_FP(n) (MULT_FP(n, n)) -#define COORD2INDEX(x, y) (x * VWIDTH + y) +#define COORD2INDEX(x, y) (y * VWIDTH + x) #define COUPLE2INDEX(x) (COORD2INDEX(x[0], x[1])) #define GET_ROTATE_X_Q(a) ({ float _a = (FIXED2FLOAT(a)) ; \ @@ -117,7 +117,7 @@ struct Quaternions { FIXED_POINT x; FIXED_POINT y; FIXED_POINT z; -} Target, Current, Last; +} Target, Current; FIXED_POINT interpolationStep = 0; FIXED_POINT zBuffer[VHEIGHT * VWIDTH]; @@ -235,38 +235,62 @@ chooseMainFace() return frontFacingFace; } -void -fill_quads(char current_face, char top_left[2], char top_right[2], - char bot_left[2], char bot_right[2]) +char +isInQuad(char curr[2], char top[2], char left[2], + char right[2], char bot[2]) { - // calc slope foreach side - float slope_top = 0; - float slope_bot = 0; - float slope_left = INFINITY; - float slope_right = -INFINITY; + char *points[4] = {top, left, bot, right}; - if (top_left[0] != top_right[0]) - slope_top = (float)(top_right[1] - top_left[1]) / - (float)(top_right[0] - top_left[0]); - if (bot_left[0] == bot_right[0]) - slope_bot = (float)(bot_right[1] - bot_left[1]) / - (float)(bot_right[0] - bot_left[0]); - if (top_left[0] == bot_left[0]) - slope_left = (float)(bot_left[1] - top_left[1]) / - (float)(bot_left[0] - top_left[0]); - if (top_right[0] == bot_right[0]) - slope_right = (float)(bot_right[1] - top_right[1]) / - (float)(bot_right[0] - top_right[0]); + char pos = 0, neg = 0; + char x = curr[0]; + char y = curr[1];; + int d; - int top = top_right[1] > top_left[1] ? top_right[1]: top_left[1]; - int bot = bot_right[1] > bot_left[1] ? bot_right[1]: bot_left[1]; - int left = top_left[0] > bot_left[0] ? top_left[0]: bot_left[0]; - int right = top_right[0] > bot_right[0] ? top_right[0]: bot_right[0]; - for (int y = top ; y <= bot ; ++y) { - for (int x = left ; x <= right ; ++x) { - if (slope_top * x + top <= y) - // TODO side check - output[COORD2INDEX(x, y)] = current_face; + for (char i = 0; i < 4; ++i) { + if (points[i][0] == curr[0] && points[i][1] == curr[1]) + return 1; + + //Form a segment between the i'th point + char x1 = points[i][0]; + char y1 = points[i][1]; + + //And the i+1'th, or if i is the last, with the first point + char i2 = (i + 1) % 4; + + char x2 = points[i2][0]; + char y2 = points[i2][1]; + + + //Compute the cross product + d = (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1); + + if (d > 0) ++pos; + if (d < 0) ++neg; + + //If the sign changes, then point is outside + if (pos > 0 && neg > 0) + return 0; + } + + return 1; +} + +void +fill_quads(char current_face, char top[2], char left[2], + char right[2], char bot[2]) +{ + if (current_face != 0) return; + output[COUPLE2INDEX(top)] = RGB15(0, 0, 15); + output[COUPLE2INDEX(left)] = RGB15(0, 0, 15); + output[COUPLE2INDEX(right)] = RGB15(0, 0, 15); + + for (int y = top[1] ; y < bot[1] ; ++y) { + for (int x = left[0] ; x < right[0] ; ++x) { + char curr[2] = {x, y}; + if (isInQuad(curr, top, left, right, bot)) + //zbuffer issue + {} + //output[COORD2INDEX(x, y)] = current_face; } } } @@ -275,33 +299,37 @@ void detect_and_fill_quads() { for (int current_face = 0 ; current_face < NUM_FACES ; ++current_face) { - char top_left [2] = {VWIDTH, VHEIGHT}; - char top_right[2] = {0, VHEIGHT}; - char bot_left [2] = {VWIDTH, 0}; - char bot_right[2] = {0, 0}; + char last_top [2] = {VWIDTH, VHEIGHT}; + char last_left[2] = {VWIDTH, 0}; + char last_right [2] = {0, 0}; + char last_bot[2] = {0, 0}; + char top [2] = {VWIDTH, VHEIGHT}; + char left[2] = {VWIDTH, 0}; + char right [2] = {0, 0}; + char bot[2] = {0, 0}; for (char y = 0; y < VHEIGHT; ++y) { for (char x = 0; x < VWIDTH; ++x) { if (output[COORD2INDEX(x, y)] != current_face) continue; - if (x <= top_left[0] && y <= top_left[1]){ - top_left[0] = x; - top_left[1] = y; + if (x <= left[0]) { + left[0] = x; + left[1] = y; } - if (x >= top_right[0] && y <= top_right[1]){ - top_right[0] = x; - top_right[1] = y; + if (y <= top[1]) { + top[0] = x; + top[1] = y; } - if (x <= bot_left[0] && y >= bot_left[1]){ - bot_left[0] = x; - bot_left[1] = y; + if (x >= right[0]) { + right[0] = x; + right[1] = y; } - if (x >= bot_right[0] && y >= bot_right[1]){ - bot_right[0] = x; - bot_right[1] = y; + if (y >= bot[1]) { + bot[0] = x; + bot[1] = y; } } } - fill_quads(current_face, top_left, top_right, bot_left, bot_right); + fill_quads(current_face, top, left, right, bot); } } @@ -315,7 +343,7 @@ printAscii() MEM_VRAM_MODE3_FB[120 + 96 * GBA_SCREEN_W] = RGB15(currentCountR, 31 - currentCountR, 0); currentCountR = currentCountR == 31 ? 0 : 31; - //detect_and_fill_quads(); + detect_and_fill_quads(); for (int i = 0; i < VHEIGHT; ++i) { for (int j = 0; j < VWIDTH; ++j) { @@ -411,31 +439,30 @@ handleAngle(char input) // TODO if (currentlyMoving == 0) { currentlyMoving = input; - Last = Current; switch (input) { case 'w': case 'W': - Target = multQ(GET_ROTATE_X_Q(FLOAT2FIXED(M_PI_2)), Target); + Target = multQ(GET_ROTATE_X_Q(FLOAT2FIXED(M_PI_2)), Current); break; case 'a': case 'A': - Target = multQ(GET_ROTATE_Y_Q(-FLOAT2FIXED(M_PI_2)), Target); + Target = multQ(GET_ROTATE_Y_Q(-FLOAT2FIXED(M_PI_2)), Current); break; case 's': case 'S': - Target = multQ(GET_ROTATE_X_Q(-FLOAT2FIXED(M_PI_2)), Target); + Target = multQ(GET_ROTATE_X_Q(-FLOAT2FIXED(M_PI_2)), Current); break; case 'd': case 'D': - Target = multQ(GET_ROTATE_Y_Q(FLOAT2FIXED(M_PI_2)), Target); + Target = multQ(GET_ROTATE_Y_Q(FLOAT2FIXED(M_PI_2)), Current); break; case 'q': case 'Q': - Target = multQ(GET_ROTATE_Z_Q(-FLOAT2FIXED(M_PI_2)), Target); + Target = multQ(GET_ROTATE_Z_Q(-FLOAT2FIXED(M_PI_2)), Current); break; case 'e': case 'E': - Target = multQ(GET_ROTATE_Z_Q(FLOAT2FIXED(M_PI_2)), Target); + Target = multQ(GET_ROTATE_Z_Q(FLOAT2FIXED(M_PI_2)), Current); break; default: currentlyMoving = 0; @@ -480,7 +507,7 @@ main() cubeX <= CUBE_WIDTH_FP - STEP_FP; cubeX += STEP_FP) { for (FIXED_POINT cubeY = -CUBE_WIDTH_FP + STEP_FP; cubeY <= CUBE_WIDTH_FP - STEP_FP; cubeY += STEP_FP) { - switch (frontFacingFace) { + switch (FACE_FRONT) { case FACE_FRONT: rotateCube(cubeX, cubeY, -CUBE_WIDTH_FP, FACE_FRONT); break;