|
|
我在写个raytrace程序,大致思路是这样的:加载屏幕大小的texture,获取其surface ,通过对屏幕每个像素进行raytrace后,将得到的颜色信息放到surface里,这样我就得到了一个屏幕大小的texture,而我的texture里的数据就是我raytrace得到的结果。最后用屏幕坐标显示我的texture,就得到了raytrace的结果。可是最后屏幕显示的还是空白(我clear的时候是0xffffffff)。然后自己就舍弃D3D显示的一套,直接用GDI的setpixel来对每一个象素进行着色,最后成功了。我现在纳闷,为什么用D3D那套不行啊。我就简单给surface上的每个象素都搞成0xffff0000,结果也不行(还是白色)。
D3D那套大致代码如下:
//顶点格式声明 :
struct VIEWPORTVERT
{
float x,y,z,rhw; // transformed coords
float tu,tv; // texture coords
};
#define D3DFVF_VIEWPORTVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1)
//初始化texture
// create a texture to store the color of the viewport
D3DXCreateTexture(gd3dDevice,md3dPP.BackBufferWidth,md3dPP.BackBufferHeight,0,0,
D3DFMT_X8B8G8R8,D3DPOOL_MANAGED,&mWriteTex);
mWriteTex->GetSurfaceLevel(0,&mWriteSurface);
//初始化顶点,也就四个点,viewport的四个角
// The aspect ratio depends on the backbuffer dimensions, which can
// possibly change after a reset. So rebuild the projection matrix.
float viewPortWidth = (float)md3dPP.BackBufferWidth;
float viewPortHeight = (float)md3dPP.BackBufferHeight;
VIEWPORTVERT vertices[] =
{
{ 0.0f,0.0f,0.5f,1.0f,0.0f,0.0f },//x,y,z,rhw,tu,tv
{ 0.0f,viewPortWidth,0.5f,1.0f,0.0f,1.0f},
{ viewPortHeight,0.0f,0.5f,1.0f,1.0f,0.0f},
{ viewPortHeight,viewPortWidth,0.5f,1.0f,1.0f,1.0f},
};
if( FAILED( gd3dDevice->CreateVertexBuffer( 4*sizeof(VIEWPORTVERT), 0, D3DFVF_VIEWPORTVERTEX, D3DPOOL_DEFAULT, &vertexBuffer,NULL) ) )
MessageBox(0,"can't create vertex buffer","error",1);
void* pVertices;
HR(vertexBuffer->Lock(0,sizeof(vertices),(void**)&pVertices,0));
memcpy(pVertices,vertices,sizeof(vertices));
vertexBuffer->Unlock();
//修改surface数据,都搞成0xffff0000
D3DSURFACE_DESC surfaceDesc;
mWriteSurface->GetDesc(&surfaceDesc);
// Get a pointer to the surface pixel data.
D3DLOCKED_RECT lockedRect;
HRESULT re = mWriteSurface->LockRect(
&lockedRect,// pointer to receive locked data
0, // lock entire surface
0); // no lock flags specified
// Iterate through each pixel in the surface and set it to red.
DWORD* imageData = (DWORD*)lockedRect.pBits;
for(int i = 0; i < surfaceDesc.Height; i++)
{
for(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;
}
}
mWriteSurface->UnlockRect();
// 用修改过的texture去显示
// Clear the backbuffer and depth buffer
HR(gd3dDevice->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0xffffffff,1.0f,0));
HR(gd3dDevice->BeginScene());
HR(gd3dDevice->SetTexture(0,mWriteTex));
HR(gd3dDevice->SetStreamSource(0,vertexBuffer,0,sizeof(VIEWPORTVERT)));
HR(gd3dDevice->SetFVF(D3DFVF_VIEWPORTVERTEX));
HR(gd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2));
HR(gd3dDevice->EndScene());
// Present the backbuffer.
HR(gd3dDevice-> resent(0,0,0,0));
谢谢了,诸位哥们,帮我看看。
|
|