示例#1
0
void TextureSource::get_texels_2x2(
    TextureCache&               texture_cache,
    const int                   ix,
    const int                   iy,
    Color4f&                    t00,
    Color4f&                    t10,
    Color4f&                    t01,
    Color4f&                    t11) const
{
    const Vector<size_t, 2> p00 =
        constrain_to_canvas(
            m_texture_instance.get_addressing_mode(),
            m_texture_props.m_canvas_width,
            m_texture_props.m_canvas_height,
            ix + 0,
            iy + 0);

    const Vector<size_t, 2> p11 =
        constrain_to_canvas(
            m_texture_instance.get_addressing_mode(),
            m_texture_props.m_canvas_width,
            m_texture_props.m_canvas_height,
            ix + 1,
            iy + 1);

    const Vector<size_t, 2> p10(p11.x, p00.y);
    const Vector<size_t, 2> p01(p00.x, p11.y);

    // Compute the coordinates of the tile containing each texel.
    const size_t tile_x_00 = truncate<size_t>(p00.x * m_texture_props.m_rcp_tile_width);
    const size_t tile_y_00 = truncate<size_t>(p00.y * m_texture_props.m_rcp_tile_height);
    const size_t tile_x_11 = truncate<size_t>(p11.x * m_texture_props.m_rcp_tile_width);
    const size_t tile_y_11 = truncate<size_t>(p11.y * m_texture_props.m_rcp_tile_height);

    // Check whether all four texels are part of the same tile.
    const size_t tile_x_mask = tile_x_00 ^ tile_x_11;
    const size_t tile_y_mask = tile_y_00 ^ tile_y_11;

    if (tile_x_mask | tile_y_mask)
    {
        // Compute the tile space coordinates of each texel.
        const size_t pixel_x_00 = p00.x - tile_x_00 * m_texture_props.m_tile_width;
        const size_t pixel_y_00 = p00.y - tile_y_00 * m_texture_props.m_tile_height;
        const size_t pixel_x_11 = p11.x - tile_x_11 * m_texture_props.m_tile_width;
        const size_t pixel_y_11 = p11.y - tile_y_11 * m_texture_props.m_tile_height;

        // Sample the tile.
        sample_tile(texture_cache, m_assembly_uid, m_texture_uid, tile_x_00, tile_y_00, pixel_x_00, pixel_y_00, t00);
        sample_tile(texture_cache, m_assembly_uid, m_texture_uid, tile_x_11, tile_y_00, pixel_x_11, pixel_y_00, t10);
        sample_tile(texture_cache, m_assembly_uid, m_texture_uid, tile_x_00, tile_y_11, pixel_x_00, pixel_y_11, t01);
        sample_tile(texture_cache, m_assembly_uid, m_texture_uid, tile_x_11, tile_y_11, pixel_x_11, pixel_y_11, t11);
    }
    else
    {
        // Compute the tile space coordinates of each texel.
        const size_t org_x = tile_x_00 * m_texture_props.m_tile_width;
        const size_t org_y = tile_y_00 * m_texture_props.m_tile_height;
        const size_t pixel_x_00 = p00.x - org_x;
        const size_t pixel_y_00 = p00.y - org_y;
        const size_t pixel_x_11 = p11.x - org_x;
        const size_t pixel_y_11 = p11.y - org_y;

        // Retrieve the tile.
        const Tile& tile =
            texture_cache.get(
                m_assembly_uid,
                m_texture_uid,
                tile_x_00,
                tile_y_00);

        // Sample the tile.
        if (tile.get_channel_count() == 3)
        {
            Color3f rgb;

            tile.get_pixel(pixel_x_00, pixel_y_00, rgb);
            t00[0] = rgb[0];
            t00[1] = rgb[1];
            t00[2] = rgb[2];
            t00[3] = 1.0f;

            tile.get_pixel(pixel_x_11, pixel_y_00, rgb);
            t10[0] = rgb[0];
            t10[1] = rgb[1];
            t10[2] = rgb[2];
            t10[3] = 1.0f;

            tile.get_pixel(pixel_x_00, pixel_y_11, rgb);
            t01[0] = rgb[0];
            t01[1] = rgb[1];
            t01[2] = rgb[2];
            t01[3] = 1.0f;

            tile.get_pixel(pixel_x_11, pixel_y_11, rgb);
            t11[0] = rgb[0];
            t11[1] = rgb[1];
            t11[2] = rgb[2];
            t11[3] = 1.0f;
        }
        else
        {
            tile.get_pixel(pixel_x_00, pixel_y_00, t00);
            tile.get_pixel(pixel_x_11, pixel_y_00, t10);
            tile.get_pixel(pixel_x_00, pixel_y_11, t01);
            tile.get_pixel(pixel_x_11, pixel_y_11, t11);
        }
    }
}