简体   繁体   中英

Clipping surface to viewport

I'm trying to render a surface with a method that takes a Surface and X, Y position to render at. The problem is that when the surface is outside the screen by one small pixel, it doesn't render at all.

Why is that? I'm trying to search for DirectX clipping but can't find anything at all.

void Draw(LPDIRECT3DSURFACE9 src, int x, int y)
{
    D3DSURFACE_DESC desc;
    src->GetDesc(&desc);

    D3DVIEWPORT9 viewport;
    Device->GetViewport(&viewport);

    int vX = viewport.X;
    int vY = viewport.Y;
    int vWidth = viewport.X + viewport.Width;
    int vHeight = viewport.Y + viewport.Height;

    RECT source;
    source.left = max(x, vX);
    source.right = min(source.left + desc.Width, vWidth);
    source.top = max(y, vY);
    source.bottom = min(source.top + desc.Height, vHeight);

    RECT destination;
    destination.left = max(x, vX);
    destination.right = min(destination.left + desc.Width + x, vWidth);
    destination.top = max(y, vY);
    destination.bottom = min(destination.top + desc.Height + y, vHeight);

    Device->StretchRect(src, &source, BackBuffer, &destination, D3DTEXF_POINT);
};

...and X = 100, Y = 100 doesn't work but -100 does but it doesn't stretch it right.

Example image of desired effect: 在此处输入图片说明

I've also been trying to set the *pSourceRect and *pDestRect to something that clips the surface but without any luck.

I'm using DirectX9.

I'm not sure, whether this works (haven't tested it), but theoretically you should solve your problem with the right rects. Choose them, so that it doesn't exceed the border and that only the overlapping part is copied. I tried to visualize the thought into following picture:

重叠矩形

If I'm not mistaken it should be

RECT source;
source.left = max(min(-x, desc.Width - 1),0);
source.right = max(min(source.left + viewport.Width -1, desc.Width - 1),0);
source.top = max(min(-y, desc.Height - 1),0);
source.bottom = max(min(source.top + viewport.Height -1, desc.Width - 1),0);

RECT destination;
destination.left = max(min(x, viewport.Width - 1),0);
destination.right = max(min(destination.left + desc.Width -1, viewport.Width - 1),0);
destination.top = max(min(y, viewport.Height - 1),0);
destination.bottom = max(min(destination.top + desc.Height -1, viewport.Height - 1),0);

in your case. The target is that the rectangles don't exceed the related surface, so all coordinates of them must be clipped at the edges.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM