z缓冲区导致东西不能绘制
Z-buffer is causing things not to draw
我在DirectX9中有一些问题。
这是我的渲染函数:
void Render(GameWorld& world)
{
DWORD BackgroundColour = 0x00102010;
g_pd3dDevice -> Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, BackgroundColour, 1.0f, 0);
// Begin rendering the scene.
if (SUCCEEDED(g_pd3dDevice -> BeginScene()))
{
world.draw();
g_pd3dDevice -> EndScene();
}
// Present the backbuffer to the display.
g_pd3dDevice -> Present(NULL, NULL, NULL, NULL);
}
我用下面的代码设置DirectX:
// Create the D3D object, return failure if this can't be done.
if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION))) return E_FAIL;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// Create the D3DDevice
if (FAILED(g_pD3D -> CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice)))
{
return E_FAIL;
}
// Turn on the Z buffer
g_pd3dDevice -> SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
g_pd3dDevice -> SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
g_pd3dDevice -> SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
// Start with lighting off - we can add lights later.
g_pd3dDevice -> SetRenderState(D3DRS_LIGHTING, FALSE);
// Set transparencies
g_pd3dDevice -> SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pd3dDevice -> SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pd3dDevice -> SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
g_pd3dDevice -> SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
// Set culling
g_pd3dDevice -> SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
return S_OK;
实际上,这将绘制一个没有任何内容的backgroundcolor屏幕。
通过关闭z缓冲区[即g_pd3dDevice -> SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);]所有东西都按照它们的"绘制"函数调用的顺序绘制。之后绘制的对象会模糊之前绘制的对象。
我也试着做了这些改变:
g_pd3dDevice -> SetRenderState(D3DRS_ZFUNC, D3DCMP_GREATER);
和
g_pd3dDevice -> Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, BackgroundColour, 0.0f, 0);
现在的效果是,先绘制的对象会掩盖后面绘制的对象,无论它们的空间位置如何。
我想要的效果是让近的物体模糊远的物体。
有人能解释一下吗?
谢谢。
尝试删除这些行:
g_pd3dDevice -> SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
g_pd3dDevice -> SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
g_pd3dDevice -> SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
z-buffer是默认启用的,所以它应该工作。
确保你用正确的WorldViewProjection矩阵变换对象。
如果你的对象没有渲染,检查是否调用了World.draw()。也许BeginScene()失败了?
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的for循环不能正确获取argv
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 为什么模板类中的对象不能返回值
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 数组长度,为什么从命令行获取时不能使用它?
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- OpenGL不能在核心配置文件中使用glVertexAttrib进行绘制
- 为什么我不能在SDL2中绘制纹理
- OpenGL 3.3不能绘制任何东西
- 不能重复使用画笔来绘制文本和矩形
- z缓冲区导致东西不能绘制
- c++ SFML精灵不能通过类绘制
- 使用OpenGL ES 2.0的SDL2 -不能在iOS上绘制简单的三角形