Index: apps/plugins/cube.c =================================================================== --- apps/plugins/cube.c (revision 17945) +++ apps/plugins/cube.c (working copy) @@ -346,15 +346,26 @@ struct line { int start, end; -}; +}; struct face { - int corner[4]; - int line[4]; + int nb_vertices; + int corner[7]; + int line[7]; }; +struct shape { + int nb_points; + int nb_lines; + int nb_faces; + const struct point_3D * points; + const struct line * lines; + const struct face * faces; + const unsigned * face_colors; +}; + /* initial, unrotated cube corners */ -static const struct point_3D sommet[8] = +static const struct point_3D cube_points[] = { {-DIST, -DIST, -DIST}, { DIST, -DIST, -DIST}, @@ -367,29 +378,96 @@ }; /* The 12 lines forming the edges */ -static const struct line lines[12] = +static const struct line cube_lines[] = { {0, 1}, {1, 2}, {2, 3}, {3, 0}, {4, 7}, {7, 6}, {6, 5}, {5, 4}, {0, 4}, {1, 5}, {2, 6}, {3, 7} }; -static bool lines_drawn[12]; +static bool lines_drawn[32]; /* The 6 faces of the cube; points are in clockwise order when viewed from the outside */ -static const struct face faces[6] = +static const struct face cube_faces[] = { - {{0, 1, 2, 3}, {0, 1, 2, 3}}, - {{4, 7, 6, 5}, {4, 5, 6, 7}}, - {{0, 4, 5, 1}, {8, 7, 9, 0}}, - {{2, 6, 7, 3}, {10, 5, 11, 2}}, - {{0, 3, 7, 4}, {3, 11, 4, 8}}, - {{1, 5, 6, 2}, {9, 6, 10, 1}} + {4, {0, 1, 2, 3}, {0, 1, 2, 3}}, + {4, {4, 7, 6, 5}, {4, 5, 6, 7}}, + {4, {0, 4, 5, 1}, {8, 7, 9, 0}}, + {4, {2, 6, 7, 3}, {10, 5, 11, 2}}, + {4, {0, 3, 7, 4}, {3, 11, 4, 8}}, + {4, {1, 5, 6, 2}, {9, 6, 10, 1}} }; +static const struct point_3D cobra_points[] = +{ + { DIST/4,0,DIST*.59}, + { -DIST/4,0,DIST*.59}, + {0,DIST*.2,DIST*.19}, + {-DIST*.94,-DIST*.02,-DIST*.06}, + {DIST*.94,-DIST*.02,-DIST*.06}, + {-DIST*.69,DIST*.13,-DIST*.31}, + {DIST*.69,DIST*.13,-DIST*.31}, + {DIST,-DIST*.06,-DIST*.31}, + {-DIST,-DIST*.06,-DIST*.31}, + {0,DIST*.2,-DIST*.31}, + {-DIST/4,-DIST*.19,-DIST*.31}, + {DIST/4,-DIST*.19,-DIST*.31}, +/* + {-DIST*.28,DIST*.06,-DIST*.31}, + {-DIST*.06,DIST*.09,-DIST*.31}, + {DIST*.06,DIST*.09,-DIST*.31}, + {DIST*.28,DIST*.06,-DIST*.31}, + {DIST*.28,-DIST*.09,-DIST*.31}, + {DIST*.06,-DIST*.13,-DIST*.31}, + {-DIST*.06,-DIST*.13,-DIST*.31}, + {-DIST*.28,-DIST*.09,-DIST*.31}, + {0,0,DIST*.59}, + {0,0,DIST*.7}, + {-DIST*.63,-DIST*.05,-DIST*.31}, + {-DIST*.63,DIST*.05,-DIST*.31}, + {-DIST*.69,0,-DIST*.31}, + {DIST*.63,DIST*.05,-DIST*.31}, + {DIST*.69,0,-DIST*.31}, + {DIST*.63,-DIST*.05,-DIST*.31} +*/ +}; + +static const struct line cobra_lines[] = +{ + {0,1},{1,2},{2,0}, + {1,10},{10,11},{11,0}, + {2,6},{6,0}, + {0,4},{4,6}, + {4,7},{7,11}, + {2,5},{5,1}, + {1,3},{3,5}, + {3,8},{8,10}, + {5,9},{9,2}, + {6,9}, + {5,8}, + {6,7} +}; + +static const struct face cobra_faces[] = +{ + {3,{0,1,2},{0,1,2}}, + {4,{1,0,11,10},{0,3,4,5}}, + {3,{0,2,6},{2,6,7}}, + {3,{0,6,4},{8,9,7}}, + {4,{0,4,7,11},{8,10,11,5}}, + {3,{1,5,2},{1,12,13}}, + {3,{1,3,5},{14,15,13}}, + {4,{3,1,10,8},{14,16,17,3}}, + {3,{2,5,9},{12,18,19}}, + {3,{2,9,6},{6,20,19}}, + {3,{3,8,5},{15,21,16}}, + {3,{4,6,7},{9,22,10}}, + {7,{5,8,10,11,7,6,9},{21,17,4,11,22,20,18}} +}; + #if LCD_DEPTH > 1 || defined(USE_GSLIB) -static const unsigned face_colors[6] = +static const unsigned cube_face_colors[] = { #ifdef HAVE_LCD_COLOR LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(0, 255, 0), @@ -407,8 +485,38 @@ LCD_DARKGRAY, LCD_BLACK, LCD_BLACK #endif }; + +static const unsigned cobra_face_colors[] = +{ +#ifdef HAVE_LCD_COLOR + LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(255, 128, 0), LCD_RGBPACK(255, 255, 0), + LCD_RGBPACK(128, 255, 0), LCD_RGBPACK(0, 255, 0), LCD_RGBPACK(0, 255, 128), + LCD_RGBPACK(0,255,255),LCD_RGBPACK(0,128,255),LCD_RGBPACK(0,0,255), + LCD_RGBPACK(128,0,255),LCD_RGBPACK(255,0,255),LCD_RGBPACK(255,0,128), + LCD_RGBPACK(128,128,128), +#elif defined(USE_GSLIB) + GREY_LIGHTGRAY, GREY_LIGHTGRAY, GREY_DARKGRAY, + GREY_DARKGRAY, GREY_BLACK, GREY_BLACK, + GREY_LIGHTGRAY, GREY_LIGHTGRAY, GREY_DARKGRAY, + GREY_DARKGRAY, GREY_BLACK, GREY_BLACK, + GREY_LIGHTGRAY, +#else + LCD_LIGHTGRAY, LCD_LIGHTGRAY, LCD_DARKGRAY, + LCD_DARKGRAY, LCD_BLACK, LCD_BLACK + LCD_LIGHTGRAY, LCD_LIGHTGRAY, LCD_DARKGRAY, + LCD_DARKGRAY, LCD_BLACK, LCD_BLACK + LCD_LIGHTGRAY, #endif +}; +#endif + +static const struct shape shapes[] = +{ + {8,12,6,cube_points, cube_lines, cube_faces, cube_face_colors}, + {12,23,13,cobra_points,cobra_lines,cobra_faces,cobra_face_colors}, +}; + enum { #if LCD_DEPTH > 1 || defined(USE_GSLIB) SOLID, @@ -420,16 +528,16 @@ static int mode = 0; -static struct point_3D point3D[8]; -static struct point_2D point2D[8]; +static struct point_3D point3D[12]; +static struct point_2D point2D[12]; static long matrice[3][3]; -static const int nb_points = 8; + static long z_off = 600; static const struct plugin_api* rb; -static void cube_rotate(int xa, int ya, int za) +static void cube_rotate(int xa, int ya, int za, int nb_points, const struct point_3D * points) { int i; /* Just to prevent unnecessary lookups */ @@ -458,18 +566,18 @@ /* apply translation matrix to all points */ for (i = 0; i < nb_points; i++) { - point3D[i].x = matrice[0][0] * sommet[i].x + matrice[1][0] * sommet[i].y - + matrice[2][0] * sommet[i].z; + point3D[i].x = matrice[0][0] * points[i].x + matrice[1][0] * points[i].y + + matrice[2][0] * points[i].z; - point3D[i].y = matrice[0][1] * sommet[i].x + matrice[1][1] * sommet[i].y - + matrice[2][1] * sommet[i].z; + point3D[i].y = matrice[0][1] * points[i].x + matrice[1][1] * points[i].y + + matrice[2][1] * points[i].z; - point3D[i].z = matrice[0][2] * sommet[i].x + matrice[1][2] * sommet[i].y - + matrice[2][2] * sommet[i].z; + point3D[i].z = matrice[0][2] * points[i].x + matrice[1][2] * points[i].y + + matrice[2][2] * points[i].z; } } -static void cube_viewport(void) +static void cube_viewport(int nb_points) { int i; @@ -488,20 +596,27 @@ } } -static void cube_draw(void) +static void cube_draw(const struct shape * myshape) { - int i, j, line; + int i, j, line, q; #if LCD_DEPTH > 1 || defined(USE_GSLIB) unsigned old_foreground; -#endif +#endif + const struct face * faces = myshape->faces; + const struct line * lines = myshape->lines; + const unsigned * face_colors = myshape->face_colors; + + int nb_faces = myshape->nb_faces; + int nb_lines = myshape->nb_lines; + switch (mode) { #if LCD_DEPTH > 1 || defined(USE_GSLIB) case SOLID: old_foreground = MY_GET_FOREGROUND(); - for (i = 0; i < 6; i++) + for (i = 0; i < nb_faces; i++) { /* backface culling; if the shape winds counter-clockwise, we are * looking at the backface, and the (simplified) cross product @@ -513,18 +628,13 @@ continue; MY_SET_FOREGROUND(face_colors[i]); + for(q=2;qmemset(lines_drawn, 0, sizeof(lines_drawn)); - for (i = 0; i < 6; i++) + for (i = 0; i < nb_faces; i++) { /* backface culling; if the shape winds counter-clockwise, we are * looking at the backface, and the (simplified) cross product @@ -545,7 +655,7 @@ * (point2D[faces[i].corner[2]].x - point2D[faces[i].corner[1]].x)) continue; - for (j = 0; j < 4; j++) + for (j = 0; j < faces[i].nb_vertices; j++) { line = faces[i].line[j]; if (!lines_drawn[line]) @@ -562,7 +672,7 @@ case WIREFRAME: - for (i = 0; i < 12; i++) + for (i = 0; i < nb_lines; i++) MYLCD(drawline)(point2D[lines[i].start].x, point2D[lines[i].start].y, point2D[lines[i].end].x, @@ -641,6 +751,8 @@ pgfx_display(3, 0); #endif + const struct shape * myshape = &shapes[ 1 ]; + while(!exit) { if (highspeed) @@ -651,9 +763,9 @@ if (redraw) { MYLCD(clear_display)(); - cube_rotate(xa, ya, za); - cube_viewport(); - cube_draw(); + cube_rotate(xa, ya, za, myshape->nb_points, myshape->points); + cube_viewport(myshape->nb_points); + cube_draw(myshape); redraw = false; }