krad_perspective_t *krad_perspective_create (int width, int height) { krad_perspective_t *krad_perspective = (krad_perspective_t *)calloc (1, sizeof(krad_perspective_t)); int rx; int ry; int x; int y; int w; int h; krad_position_t top; krad_position_t bot; krad_position_t r; krad_position_t in; krad_perspective->w = width; krad_perspective->h = height; w = krad_perspective->w; h = krad_perspective->h; krad_perspective->tl.x = 0.0; krad_perspective->tl.y = 0.0; krad_perspective->tr.x = 1.0; krad_perspective->tr.y = 0.0; krad_perspective->bl.x = 0.0; krad_perspective->bl.y = 1.0; krad_perspective->br.x = 1.0; krad_perspective->br.y = 1.0; krad_perspective->map = (int *)calloc (1, krad_perspective->w * krad_perspective->h * 4); sub_vec2( &top, &krad_perspective->tr, &krad_perspective->tl ); sub_vec2( &bot, &krad_perspective->br, &krad_perspective->bl ); for( y = 0; y < h; y++ ) { for ( x = 0; x < w; x++ ) { in.x = (double)x / (double)w; in.y = (double)y / (double)h; get_pixel_position( &r, &top, &bot, &krad_perspective->tl, &krad_perspective->bl, &in ); rx = lrint(r.x * (float)w); ry = lrint(r.y * (float)h); if ( rx < 0 || rx >= w || ry < 0 || ry >= h ) { continue; } krad_perspective->map[x + w * y] = rx + w * ry; } } return krad_perspective; }
static void perspective_map (kr_perspective_t *perspective) { int32_t *map; kr_coord_t tl; kr_coord_t bl; kr_coord_t top; kr_coord_t bot; kr_coord_t r; kr_coord_t in; int32_t rx; int32_t ry; int32_t x; int32_t y; int32_t w; int32_t h; //krad_timer_start (perspective->priv->map_timer); w = perspective->priv->width; h = perspective->priv->height; tl = perspective->priv->tl; bl = perspective->priv->bl; map = perspective->priv->map; sub_vec2 (&top, &perspective->priv->tr, &tl); sub_vec2 (&bot, &perspective->priv->br, &bl); for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { in.x = (double)x / (double)w; in.y = (double)y / (double)h; get_pixel_position (&r, &top, &bot, &tl, &bl, &in ); rx = lrint (r.x * (float)w); ry = lrint (r.y * (float)h); if (rx < 0 || rx >= w || ry < 0 || ry >= h) { continue; } map[x + w * y] = rx + w * ry; } } //krad_timer_status (perspective->priv->map_timer); }
static void get_pixel_position (kr_coord_t* r, kr_coord_t* t, kr_coord_t* b, kr_coord_t* tl, kr_coord_t* bl,kr_coord_t* in ) { kr_coord_t t_x; kr_coord_t b_x; kr_coord_t k; mul_vec2 (&t_x, t, in->x); mul_vec2 (&b_x, b, in->x); add_vec2 (&t_x, &t_x, tl); add_vec2 (&b_x, &b_x, bl); sub_vec2 (&k, &b_x, &t_x); mul_vec2 (&k, &k, in->y); add_vec2 (r, &k, &t_x); }