📄 3dkit.c
字号:
for (j = 0; j < l - 1; j++, k++) { for (i = 0; i < w - 1; i++, k++) { (*dl) (temp[k + 1].x, temp[k + 1].y, temp[k].x, temp[k].y, c); (*dl) (temp[k + w].x, temp[k + w].y, temp[k].x, temp[k].y, c); } (*dl) (temp[k + w].x, temp[k + w].y, temp[k].x, temp[k].y, c); } for (i = 0; i < w - 1; i++, k++) (*dl) (temp[k + 1].x, temp[k + 1].y, temp[k].x, temp[k].y, c);}void xchg (int *a, int *b){ int t = *a; *a = *b; *b = t;}void TD_drawsurface (TD_Solid * s, int which){ TD_Surface *surf = &s->surf[which]; int w = surf->w; int l = surf->l; int i = 0, j = 0, k = 0, c = surf->mesh_color; void (*dl) (int, int, int, int, int) = s->draw_line; void (*dt) (int, int, int, int, int, int, int, int, int, int) = s->draw_triangle; void (*ds) (int, int, int, int, int, int, int, int) = s->draw_striangle; int mesh; long d1, d2, d3, d4, d; long x1, y1, c1; long x2, y2, c2; long x3, y3, c3; long x4, y4, c4; int furthest, clockwise = 0; TD_tridata tri; tri.bitmap1 = surf->bitmap1; tri.bitmap2 = surf->bitmap2; if (s->option_flags & TDOPTION_ALL_SAME_RENDER) mesh = (s->render == TD_MESH_AND_SOLID); else mesh = (surf->render == TD_MESH_AND_SOLID);/*distance of four corners (numbered clockwise): */ d1 = TD_finddistance (s, &surf->point[0]); d2 = TD_finddistance (s, &surf->point[w - 1]); d3 = TD_finddistance (s, &surf->point[w * l - 1]); d4 = TD_finddistance (s, &surf->point[w * (l - 1)]);/*find furthest point */ furthest = 1; d = d1; if (d2 > d) { furthest = 2; d = d2; } if (d3 > d) { furthest = 3; d = d3; } if (d4 > d) furthest = 4;/*draw scanning from the furthest point to the second furthest point *//*there are eight possibilities: */ switch (furthest) { case 1: if (d2 > d4) { clockwise = 0; for (j = 0; j < l; j++) for (i = 0; i < w; i++) { TD_translate (s, &surf->point[i + j * w], &temp[k]); temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which); k++; } } else { xchg (&l, &w); clockwise = 1; for (j = 0; j < l; j++) for (i = 0; i < w; i++) { TD_translate (s, &surf->point[i * l + j], &temp[k]); temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which); k++; } } break; case 2: if (d1 > d3) { clockwise = 1; for (j = 0; j < l; j++) for (i = w - 1; i >= 0; i--) { TD_translate (s, &surf->point[i + j * w], &temp[k]); temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which); k++; } } else { xchg (&l, &w); clockwise = 0; for (j = l - 1; j >= 0; j--) for (i = 0; i < w; i++) { TD_translate (s, &surf->point[i * l + j], &temp[k]); temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which); k++; } } break; case 3: if (d4 > d2) { clockwise = 0; for (j = l - 1; j >= 0; j--) for (i = w - 1; i >= 0; i--) { TD_translate (s, &surf->point[i + j * w], &temp[k]); temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which); k++; } } else { xchg (&l, &w); clockwise = 1; for (j = l - 1; j >= 0; j--) for (i = w - 1; i >= 0; i--) { TD_translate (s, &surf->point[i * l + j], &temp[k]); temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which); k++; } } break; case 4: if (d3 > d1) { clockwise = 1; for (j = l - 1; j >= 0; j--) for (i = 0; i < w; i++) { TD_translate (s, &surf->point[i + j * w], &temp[k]); temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which); k++; } } else { xchg (&l, &w); clockwise = 0; for (j = 0; j < l; j++) for (i = w - 1; i >= 0; i--) { TD_translate (s, &surf->point[i * l + j], &temp[k]); temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which); k++; } } break; } if (!surf->backfacing) clockwise = 2; for (k = 0, j = 0; j < l - 1; j++, k++) { for (i = 0; i < w - 1; i++, k++) {/*define the grid square we are currently drawing: */ x1 = temp[k].x; y1 = temp[k].y; c1 = temp[k].c; x2 = temp[k + 1].x; y2 = temp[k + 1].y; c2 = temp[k + 1].c; x3 = temp[k + w + 1].x; y3 = temp[k + w + 1].y; c3 = temp[k + w + 1].c; x4 = temp[k + w].x; y4 = temp[k + w].y; c4 = temp[k + w].c; /*draw with two triangles */ if (furthest & 1) { /*draw with hypotenuse from point 1 to point 3 */ if (s->option_flags & TDOPTION_FLAT_TRIANGLE) { c1 = (c1 + c2 + c3 + c4) >> 2; (*ds) (x1, y1, x2, y2, x3, y3, c1, clockwise); (*ds) (x1, y1, x3, y3, x4, y4, c1, clockwise); } else { (*dt) (x1, y1, c1, x2, y2, c2, x3, y3, c3, clockwise); (*dt) (x1, y1, c1, x3, y3, c3, x4, y4, c4, clockwise); } } else { /*draw with hypotenuse from point 2 to point 4 */ if (s->option_flags & TDOPTION_FLAT_TRIANGLE) { c1 = (c1 + c2 + c3 + c4) >> 2; (*ds) (x1, y1, x2, y2, x4, y4, c1, clockwise); (*ds) (x2, y2, x3, y3, x4, y4, c1, clockwise); } else { (*dt) (x1, y1, c1, x2, y2, c2, x4, y4, c4, clockwise); (*dt) (x2, y2, c2, x3, y3, c3, x4, y4, c4, clockwise); } } if (mesh) { (*dl) (x1, y1, x2, y2, c); (*dl) (x1, y1, x4, y4, c); } } if (mesh) (*dl) (temp[k + w].x, temp[k + w].y, temp[k].x, temp[k].y, c); } if (mesh) { for (i = 0; i < w - 1; i++, k++) (*dl) (temp[k + 1].x, temp[k + 1].y, temp[k].x, temp[k].y, c); }}int compare (const void *vp, const void *vq){ const int *p = vp; const int *q = vq; int diff = *p - *q; return ((diff >= 0) ? ((diff > 0) ? -1 : 0) : +1);}struct disttype { long distance; int number;};void TD_draw_solid (TD_Solid * s){ int n = s->num_surfaces, w, l, i, j, render, num_existing_surfaces; long max = 0; struct disttype *sortarray = NULL; temp = NULL; gl_trisetdrawpoint(s->draw_point); if ((sortarray = malloc (s->num_surfaces * sizeof (struct disttype))) == NULL) { fprintf (stderr, "1. Error allocating memory.\n"); goto fin; } if (s->option_flags & TDOPTION_INIT_ROTATION_MATRIX) TD_calc_rotation_matrix (s); for (j = 0, i = 0; i < n; i++) { if((s->surf[i].point)) { sortarray[j++].number = i; w = s->surf[i].w; if (max < w) max = w; l = s->surf[i].l; /*find the largest surface */ if (max < l) max = l; } } num_existing_surfaces = j; if(!num_existing_surfaces) goto fin; if (s->option_flags & TDOPTION_SORT_SURFACES) { for (j = 0, i = 0; i < n; i++) { if((s->surf[i].point)) { sortarray[j++].distance = TD_finddistance (s, &s->surf[i].point[s->surf[i].w / 2 + s->surf[i].w * (s->surf[i].l / 2)]); /*the distance of the middle point of the surface */ } } qsort (sortarray, num_existing_surfaces, sizeof (struct disttype), compare); } max++; if ((temp = malloc (max * max * sizeof (TD_Short_Point))) == NULL) { fprintf (stderr, "2. Error allocating memory.\n"); goto fin; } if (s->option_flags & TDOPTION_ROTATE_OBJECT) { s->x_cam = 0; s->y_cam = 0; s->z_cam = 0; s->s_cam = s->distance * TD_MULCONSTANT; } else { s->s_cam = 0; } for (i = 0; i < num_existing_surfaces; i++) { if (s->option_flags & TDOPTION_ALL_SAME_RENDER) render = s->render; else render = s->surf[sortarray[i].number].render; switch (render) { case TD_SOLID: case TD_MESH_AND_SOLID: if ((long) s->surf[sortarray[i].number].bitmap1 | (long) s->surf[sortarray[i].number].bitmap2) TD_drawwrapsurface (s, sortarray[i].number); else TD_drawsurface (s, sortarray[i].number); break; case TD_EDGES_ONLY: TD_drawwire (s, sortarray[i].number); break; case TD_MESH: TD_drawmesh (s, sortarray[i].number); break; default: TD_drawmesh (s, sortarray[i].number); } } fin: if(temp) free (temp); if(sortarray) free (sortarray);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -