读取深度值出错

happyzhanglan 2010-09-20 10:09:08
IDirect3DSurface9 * ppZStencilSurface;
Device->GetDepthStencilSurface(&ppZStencilSurface);

D3DLOCKED_RECT pLockedRect;
ppZStencilSurface->LockRect(&pLockedRect,NULL,0);
float * p=(float *)(pLockedRect.pBits);

FILE *fp;
fp=fopen("data.txt","a+");
for (int x=0;x<ViewHeight;x++)//ViewWidth、ViewHeight为视口宽和高
{
for (int y=0;y<ViewWidth;y++)
{
fprintf(fp,"depth[%d][%d]=%f\n",x,y,&p[x*(pLockedRect.Pitch/4)+y]);
}
}

fclose(fp);
ppZStencilSurface->UnlockRect();

想利用以上代码把深度缓冲区中的深度值读取出来并写道文本文档中,但是结果出错,请问错误出在哪里。以下为实验结果
depth[0][0]=0.000000
depth[0][1]=0.000000
depth[0][2]=0.000000
depth[0][3]=0.000000
depth[0][4]=0.000000
depth[0][5]=0.000000
depth[0][6]=0.000000
depth[0][7]=0.000000
depth[0][8]=0.000000
depth[0][9]=0.000000
depth[0][10]=0.000000
depth[0][11]=0.000000
depth[0][12]=0.000000
depth[0][13]=0.000000
depth[0][14]=0.000000
depth[0][15]=0.000000
depth[0][16]=0.000000
depth[0][17]=0.000000
depth[0][18]=0.000000
depth[0][19]=0.000000
depth[0][20]=0.000000
depth[0][21]=0.000000
depth[0][22]=0.000000
......
...全文
134 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueswhen 2010-10-20
  • 打赏
  • 举报
回复
你用CreateOffscreenPlainSurface所建的surface真的成功了吗,updatesurface成功了吗,我表示强烈怀疑,用FAILED函数和MessageBox检验一下,lock部分的代码肯定没错,唯一能联想到的原因就是updatesurface这步根本就没成功,你lock的surface里根本就没数据,当然也就出不来该有的结果了。
happyzhanglan 2010-09-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 happyzhanglan 的回复:]
.....
FLOAT * p=(FLOAT*)(pLockedRect.pBits);
FLOAT a[ViewHeight*ViewWidth];
.....
int index=x*(pLockedRect.Pitch/2)+y;
a[index]=p[index];
a[index]中的值有的为负,有的很大,而且不能够全部赋值,有一部分是空的,是怎么回事呢
[/Quote]
无奈了,放假归来,还是没有找到问题所在
xingzhe2001 2010-09-21
  • 打赏
  • 举报
回复
那是16位浮点,不能直接printf,要用D3DXFLOAT16结构的FLOAT操作符
happyzhanglan 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xingzhe2001 的回复:]
这是基本的c语言语法问题。
&p[index]这句为啥要用取地址?那肯定返回的是地址

改为

C/C++ code
fprintf(fp,"depth[%d][%d]=%ld\n",x,y,p[index]);
看看

另外你自己现在vc里调试看看内存里有值不。
[/Quote]
改完之后的值是下面这样的,不是在0~1之间的,是不是定义时有什么需要注意的啊,怎么取出来的值总是不对呢
happyzhanglan 2010-09-21
  • 打赏
  • 举报
回复

.....
FLOAT * p=(FLOAT*)(pLockedRect.pBits);
FLOAT a[ViewHeight*ViewWidth];
.....
int index=x*(pLockedRect.Pitch/2)+y;
a[index]=p[index];
a[index]中的值有的为负,有的很大,而且不能够全部赋值,有一部分是空的,是怎么回事呢
happyzhanglan 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xingzhe2001 的回复:]
那是16位浮点,不能直接printf,要用D3DXFLOAT16结构的FLOAT操作符
[/Quote]
那我怎么输出到文本文档看结果是否对呢
happyzhanglan 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 void_wuyu 的回复:]
ppZStencilSurface->LockRect
调用成功了?
[/Quote]
乐CC 2010-09-20
  • 打赏
  • 举报
回复
ppZStencilSurface->LockRect
调用成功了?
happyzhanglan 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xingzhe2001 的回复:]
回家了,祝你好运
[/Quote]
谢谢啦
xingzhe2001 2010-09-20
  • 打赏
  • 举报
回复
回家了,祝你好运
xingzhe2001 2010-09-20
  • 打赏
  • 举报
回复
这是基本的c语言语法问题。
&p[index]这句为啥要用取地址?那肯定返回的是地址

改为
fprintf(fp,"depth[%d][%d]=%ld\n",x,y,p[index]);
看看

另外你自己现在vc里调试看看内存里有值不。
happyzhanglan 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xingzhe2001 的回复:]
不要直接读,用CreateOffscreenPlainSurface创建一个新的面,用updateSurface把你得到的那个surface拷贝到这个surface,再读取这个新surface的值
这个是微软的说明http://msdn.microsoft.com/en-us/library/bb219800(VS.85).aspx#Reading_DepthStencil_Buffers
这……
[/Quote]
IDirect3DSurface9 * ppZStencilSurface;
Device->GetDepthStencilSurface(&ppZStencilSurface);
IDirect3DSurface9* ppSurface;
Device->CreateOffscreenPlainSurface(ViewWidth,ViewHeight,D3DFMT_D16_LOCKABLE,D3DPOOL_DEFAULT,&ppSurface,NULL);

Device->UpdateSurface(ppZStencilSurface,NULL,ppSurface,NULL);

D3DLOCKED_RECT pLockedRect;
HRESULT hr=ppSurface->LockRect(&pLockedRect,NULL,0);
long * p=(long*)(pLockedRect.pBits);

FILE *fp;
fp=fopen("data.txt","a+");
for (int x=0;x<ViewHeight;x++)
{
for (int y=0;y<ViewWidth;y++)
{
int index=x*(pLockedRect.Pitch/4)+y;
fprintf(fp,"depth[%d][%d]=%ld\n",x,y,&p[index]);
}
}

fclose(fp);
ppZStencilSurface->UnlockRect();
结果还是不对,若类型为long,则取出的值为地址值,若是float,取出的值为全为0。以下为long类型时的结果depth[0][0]=15431680
depth[0][1]=15431684
depth[0][2]=15431688
depth[0][3]=15431692
depth[0][4]=15431696
depth[0][5]=15431700
depth[0][6]=15431704
depth[0][7]=15431708
depth[0][8]=15431712
depth[0][9]=15431716
depth[0][10]=15431720
depth[0][11]=15431724
depth[0][12]=15431728
depth[0][13]=15431732
depth[0][14]=15431736
depth[0][15]=15431740
...
xingzhe2001 2010-09-20
  • 打赏
  • 举报
回复
不要直接读,用CreateOffscreenPlainSurface创建一个新的面,用updateSurface把你得到的那个surface拷贝到这个surface,再读取这个新surface的值
这个是微软的说明http://msdn.microsoft.com/en-us/library/bb219800(VS.85).aspx#Reading_DepthStencil_Buffers
这个是把depth保存到文件的讨论http://www.gamedev.net/community/forums/topic.asp?topic_id=345239

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧