我正在尝试使用以下代码使用子图像更新128x128 D3DLOCKED_RECT,但似乎沿顶部向下挤压它们,X偏移被忽略,y偏移为60%。

我还尝试过将纹理设置为正确的大小,并使用RECT将其复制到正确位置的128x128纹理中,但是这非常慢,并且在尝试时似乎无法正常工作。一定有办法使用原始像素数据吗?

任何帮助将非常感激 :)

编辑:我使用以下代码进行了半工作,现在位置正确并且大小正确。但是它仅使用蓝色通道,并且一切都是灰度(蓝色比例?)

srcdata = (byte *) pixels;

dstdata = (unsigned int *)lockrect.pBits;

for (y = yoffset; y < (yoffset + height); y++)
{
    for (x = xoffset; x < (xoffset + width); x++)
    {
        dstdata[ ( y * lockrect.Pitch / dstbytes + x ) + 0] = (unsigned int)srcdata[0];
        dstdata[ ( y * lockrect.Pitch / dstbytes + x ) + 1] = (unsigned int)srcdata[1];
        dstdata[ ( y * lockrect.Pitch / dstbytes + x ) + 2] = (unsigned int)srcdata[0];
        dstdata[ ( y * lockrect.Pitch / dstbytes + x ) + 3] = (unsigned int)srcdata[3];

        srcdata += srcbytes;
    }
}'


结束编辑

创建128x128纹理后测试调用:

int x, y;
byte    temp[132*132*4];

// Test texture (pink and black checker)
for( y = 0; y < 16; y++ )
{
    for( x = 0; x < 16; x++ )
    {
        if(( y < 8 ) ^ ( x < 8 ))
            ((uint *)&temp)[y*16+x] = 0xFFFF00FF;
        else ((uint *)&temp)[y*16+x] = 0xFF000000;
    }
}

UpdateSubImage (0, 0, 16, 16, temp )


更新功能:

void UpdateSubImage (int xoffset, int yoffset, int width, int height, const
GLvoid *pixels)
{
int x, y;
int srcbytes = 4; //Hard coded for now, as all tests are RGBA
int dstbytes = 4; // ^
byte *srcdata;
byte *dstdata;

D3DLOCKED_RECT lockrect;

pTexture->LockRect( 0, &lockrect, NULL, 0);

srcdata = (byte *) pixels;
dstdata = (byte *) lockrect.pBits;
dstdata += (yoffset * width + xoffset) * dstbytes;

for (y = yoffset; y < (yoffset + height); y++)
{
    for (x = xoffset; x < (xoffset + width); x++)
    {
        if (srcbytes == 1)
        {
            if (dstbytes == 1)
                dstdata[0] = srcdata[0];
            else if (dstbytes == 4)
            {
                dstdata[0] = srcdata[0];
                dstdata[1] = srcdata[0];
                dstdata[2] = srcdata[0];
                dstdata[3] = srcdata[0];
            }
        }
        else if (srcbytes == 3)
        {
            if (dstbytes == 1)
                dstdata[0] = ((int) srcdata[0] + (int) srcdata[1] + (int) srcdata[2]) / 3;
            else if (dstbytes == 4)
            {
                dstdata[0] = srcdata[2];
                dstdata[1] = srcdata[1];
                dstdata[2] = srcdata[0];
                dstdata[3] = 255;
            }
        }
        else if (srcbytes == 4)
        {
            if (dstbytes == 1)
                dstdata[0] = ((int) srcdata[0] + (int) srcdata[1] + (int) srcdata[2]) / 3;
            else if (dstbytes == 4)
            {
                dstdata[0] = srcdata[2];
                dstdata[1] = srcdata[1];
                dstdata[2] = srcdata[0];
                dstdata[3] = srcdata[3];
            }
        }

        // advance
        srcdata += srcbytes;
        dstdata += dstbytes;
    }
}

pTexture->UnlockRect(0);
}


输出结果如下:

c&#43;&#43; - RGBA像素数据进入D3DLOCKED_RECT-LMLPHP

输出应如下所示:
c&#43;&#43; - RGBA像素数据进入D3DLOCKED_RECT-LMLPHP

最佳答案

您假设通过lockrect.pBits可访问的数据在内存中是线性的。通常情况并非如此。相反,您在行之间具有由lockrect.Pitch值定义的恒定偏移量。

要获取目标中像素的地址,请使用:

byte * destAddr = (lockrect.pBits + y * lockrect.Pitch + 4 * x);
// for 32 bit images. For other formats adjust the hard-coded 4.

10-08 07:51