游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2081|回复: 0

如何用DX9得到当前屏幕的指定坐标的RGB值?

[复制链接]

16

主题

42

帖子

42

积分

注册会员

Rank: 2

积分
42
发表于 2007-10-23 00:27:00 | 显示全部楼层 |阅读模式
如何用DX9得到当前屏幕的指定坐标的RGB值?
有代码如下,但是。。。不能得到正确的RGB值。
RGBQUAD rgb;

g_pd3dDevice- >GetFrontBufferData(0, g_pSurface);

// Get the surface description.
D3DSURFACE_DESC surfaceDesc;
g_pSurface- >GetDesc(&surfaceDesc);

D3DLOCKED_RECT lockedRect;
if(FAILED(g_pSurface- >LockRect(&lockedRect,NULL,D3DLOCK_NO_DIRTY_UPDATE &brvbarD3DLOCK_NOSYSLOCK &brvbarD3DLOCK_READONLY)))
{
ErrorMessage("Unable to Lock Front Buffer Surface"); return RGBQUAD();
}

// Iterate through each pixel in the surface and set it to red.
DWORD* imageData = (DWORD*)lockedRect.pBits;

//for (unsigned int i = 0; i  < surfaceDesc.Height; i++)
//{
// for (unsigned int j = 0; j  < surfaceDesc.Width; j++)
// {
// // index into texture, note we use the pitch and divide by
// // four since the pitch is given in bytes and there are
// // 4 bytes per DWORD.
// int index = i * lockedRect.Pitch / 4 + j;
// imageData[index] = 0xffff0000; // red
// }
//}
// 得到指定坐标的颜色信息
int index = x * lockedRect.Pitch / 4 + y;

std::bitset <32 > color(imageData[index]);

std::string s;

// R
s.clear();
for (int j = 8; j  < 16; ++j)
{
if (color[j])
{
s.push_back( '1 ');

}
else
{
s.push_back( '0 ');
}
}
std::bitset <8 > r(s);
rgb.rgbRed = (unsigned char)r.to_ulong();

// G
s.clear();
for (int j = 16; j  < 24; ++j)
{
if (color[j])
{
s.push_back( '1 ');

}
else
{
s.push_back( '0 ');
}
}
std::bitset <8 > g(s);
rgb.rgbGreen = (unsigned char)g.to_ulong();

// B
s.clear();
for (int j = 24; j  < 32; ++j)
{
if (color[j])
{
s.push_back( '1 ');

}
else
{
s.push_back( '0 ');
}
}
std::bitset <8 > b(s);
rgb.rgbBlue = (unsigned char)b.to_ulong();

//for(int i=0;i <gScreenRect.bottom;i++)
//{
// memcpy((BYTE*)pBits+(gScreenRect.bottom-i-1)*gScreenRect.right*BITSPERPIXEL/8,
//    (BYTE*)lockedRect.pBits+i*lockedRect.Pitch,
//    gScreenRect.right*BITSPERPIXEL/8);//g_d3dpp.BackBufferHeight*g_d3dpp.BackBufferWidth*4);
//}


g_pSurface- >UnlockRect();

return rgb;
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-16 04:43

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表