// 根据 render_state 绘制原始三角形 void device_draw_primitive(device_t *device, const vertex_t *v1, const vertex_t *v2, const vertex_t *v3) { point_t p1, p2, p3, c1, c2, c3; int render_state = device->render_state; // 按照 Transform 变化 transform_apply(&device->transform, &c1, &v1->pos); transform_apply(&device->transform, &c2, &v2->pos); transform_apply(&device->transform, &c3, &v3->pos); // 裁剪,注意此处可以完善为具体判断几个点在 cvv内以及同cvv相交平面的坐标比例 // 进行进一步精细裁剪,将一个分解为几个完全处在 cvv内的三角形 if (transform_check_cvv(&c1) != 0) return; if (transform_check_cvv(&c2) != 0) return; if (transform_check_cvv(&c3) != 0) return; // 归一化 transform_homogenize(&device->transform, &p1, &c1); transform_homogenize(&device->transform, &p2, &c2); transform_homogenize(&device->transform, &p3, &c3); // 纹理或者色彩绘制 if (render_state & (RENDER_STATE_TEXTURE | RENDER_STATE_COLOR)) { vertex_t t1 = *v1, t2 = *v2, t3 = *v3; trapezoid_t traps[2]; int n; t1.pos = p1; t2.pos = p2; t3.pos = p3; t1.pos.w = c1.w; t2.pos.w = c2.w; t3.pos.w = c3.w; vertex_rhw_init(&t1); // 初始化 w vertex_rhw_init(&t2); // 初始化 w vertex_rhw_init(&t3); // 初始化 w // 拆分三角形为0-2个梯形,并且返回可用梯形数量 n = trapezoid_init_triangle(traps, &t1, &t2, &t3); if (n >= 1) device_render_trap(device, &traps[0]); if (n >= 2) device_render_trap(device, &traps[1]); } if (render_state & RENDER_STATE_WIREFRAME) { // 线框绘制 device_draw_line(device, (int)p1.x, (int)p1.y, (int)p2.x, (int)p2.y, device->foreground); device_draw_line(device, (int)p1.x, (int)p1.y, (int)p3.x, (int)p3.y, device->foreground); device_draw_line(device, (int)p3.x, (int)p3.y, (int)p2.x, (int)p2.y, device->foreground); } }
char character_buildAt(character_t* c, kindOf_building_t* t, float x, float y) { if (!transform_check(&t->build, &c->inventory)) return 0; character_delHome(c); building_t* b = world_addBuilding(c->w, x, y, t, c); if (b == NULL) return 0; transform_apply(&t->build, &c->inventory, 1); c->hasBuilding = b->o.uuid; return 1; }