游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1499|回复: 3

How to get the pixel color in surface (RenderTarget)

[复制链接]

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
发表于 2008-3-19 23:52:00 | 显示全部楼层 |阅读模式
I create a rendertarget texture and draw some object in its surface.
Now I want to get each pixel color in the surface.
But LockRect function always returns -2005530516 (D3DERR_INVALIDCALL).
How do I get the pixel color in surface (rendertarget texture)?


  1.         // setup shadow map objects
  2.         V_RETURN(m_pd3dDevice->CreateTexture(
  3.                 SHADOW_RESULT_MAP_SIZE_WIDTH,
  4.                 SHADOW_RESULT_MAP_SIZE_HEIGHT,
  5.                 1,
  6.                 D3DUSAGE_RENDERTARGET,//<====render target
  7.                 SHADOW_MAP_FORMAT,
  8.                 D3DPOOL_DEFAULT,
  9.                 &m_pShadowMapTex,
  10.                 NULL
  11.                 ));

  12.         V_RETURN(m_pShadowMapTex->GetSurfaceLevel(0, &m_pShadowMapSurf));

  13.         V_RETURN(m_pd3dDevice->CreateDepthStencilSurface(
  14.                 SHADOW_RESULT_MAP_SIZE_WIDTH,
  15.                 SHADOW_RESULT_MAP_SIZE_HEIGHT,
  16.                 D3DFMT_D24S8,
  17.                 D3DMULTISAMPLE_NONE,
  18.                 0,
  19.                 TRUE,
  20.                 &m_pShadowMapZ,
  21.                 NULL
  22.                 ));

  23. //lock surface and get color

  24.         D3DLOCKED_RECT lrect;

  25.         D3DSURFACE_DESC Desc;
  26.         m_pShadowMapSurf->GetDesc(&Desc);
  27.         hr=m_pShadowMapSurf->LockRect(&lrect,NULL,D3DLOCK_DONOTWAIT|D3DLOCK_READONLY );//<==hr=-2005530516 D3DERR_INVALIDCALL

  28.         for (int y=0;y<Desc.Height;y++)
  29.         {
  30.                 for (int x=0;Desc.Width;x++)
  31.                 {

  32.                         float dwColor = ((float*)lrect.pBits)[y*(lrect.Pitch/sizeof(float))+x];//Y*Width+X

  33.                         int r=(int)(dwColor*255.0f);

  34.                         if (dwColor<0.3f)
  35.                         {
  36.                                 nPixel++;
  37.                         }
  38.                 }
  39.         }
  40.         m_pShadowMapSurf->UnlockRect();
复制代码

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
 楼主| 发表于 2008-3-20 01:30:00 | 显示全部楼层

Re:How to get the pixel color in surface (RenderTarget)

我已?解?Q了

  1.         int nPixel=0;

  2.         D3DLOCKED_RECT lrect;
  3.         D3DSURFACE_DESC Desc;
  4.         m_pShadowMapSurf->GetDesc(&Desc);

  5.         LPDIRECT3DSURFACE9 pDestSurface;
  6.         m_pd3dDevice->CreateOffscreenPlainSurface(Desc.Width,Desc.Height,Desc.Format,D3DPOOL_SYSTEMMEM,&pDestSurface,NULL);//Must be D3DPOOL_SYSTEMMEM for MSDN

  7.         m_pd3dDevice->GetRenderTargetData(m_pShadowMapSurf,pDestSurface);

  8.         hr=pDestSurface->LockRect(&lrect,NULL,D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_READONLY );

  9.         for (int y=0;y<Desc.Height;y++)
  10.         {
  11.                 for (int x=0;x<Desc.Width;x++)
  12.                 {
  13.                         float dwColor = ((float*)lrect.pBits)[y*(lrect.Pitch/sizeof(float))+x];//Y*Width+X

  14.                         int r=(int)(dwColor*255.0f);

  15.                         if (dwColor<0.3f)
  16.                         {
  17.                                 nPixel++;
  18.                         }
  19.                 }
  20.         }
  21.         pDestSurface->UnlockRect();

  22.         SAFE_RELEASE(pDestSurface);
复制代码

2

主题

141

帖子

141

积分

注册会员

Rank: 2

积分
141
发表于 2008-3-21 10:40:00 | 显示全部楼层

Re:How to get the pixel color in surface (RenderTarget)

你这程序有问题的
LockRect后不能直接nPixel++
应当如下
DWORD* imageData = (DWORD*)lockedRect.pBits;
for(int i = 0; i < surfaceDesc.Height; i++)
{
  for(int j = 0; j < surfaceDesc.Width; j++)
  {
    int index = i * lockedRect.Pitch / 4 + j;
    imageData[index] = 0xffff0000; // red
  }
}
为什么要用i * lockedRect.Pitch / 4 + j不能直接用象素你去看文档

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
 楼主| 发表于 2008-3-21 23:16:00 | 显示全部楼层

Re:How to get the pixel color in surface (RenderTarget)

我的texture format??32F,nPixel是在??某???色???念?色的pixel?
"LockRect后不能直接nPixel++",?句?是什?意思??道要Unlock後才可以nPixel++????惺颤N影???
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-20 15:46

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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