CEGUI与DX(Direct3D)渲染的冲突问题

今天在工程里直接调用D3D的API画了一个旋转的三角体(可以理解为金字塔),但是一调用CEGUI的渲染之后,金字塔就无法渲染出来,只要不调用 CEGUI::System的renderGUI(),就可以渲染出来,所以觉得应该是跟CEGUI内部渲染有冲突。

查了下网上的资料,果然有反映说CEGUI更改了渲染状态,但没有改回来导致渲染失败。看了下CEGUI的源码,如下:

void Direct3D9Renderer::beginRendering()
{
d_device->SetFVF(D3DFVF_XYZ D3DFVF_DIFFUSE D3DFVF_TEX1);

// no shaders initially

d\_device->SetVertexShader(0);
d\_device->SetPixelShader(0);

// set device states
d\_device->SetRenderState(D3DRS\_LIGHTING, FALSE);
d\_device->SetRenderState(D3DRS\_FOGENABLE, FALSE);
d\_device->SetRenderState(D3DRS\_ZENABLE, D3DZB\_FALSE);
d\_device->SetRenderState(D3DRS\_ALPHATESTENABLE, FALSE);
d\_device->SetRenderState(D3DRS\_CULLMODE, D3DCULL\_NONE);
d\_device->SetRenderState(D3DRS\_FILLMODE, D3DFILL\_SOLID);
d\_device->SetRenderState(D3DRS\_SCISSORTESTENABLE, TRUE);
d\_device->SetRenderState(D3DRS\_ZWRITEENABLE, FALSE);

// setup texture addressing settings
d\_device->SetSamplerState(0, D3DSAMP\_ADDRESSU, D3DTADDRESS\_CLAMP);
d\_device->SetSamplerState(0, D3DSAMP\_ADDRESSV, D3DTADDRESS\_CLAMP);

// setup colour calculations
d\_device->SetTextureStageState(0, D3DTSS\_COLORARG1, D3DTA\_TEXTURE);
d\_device->SetTextureStageState(0, D3DTSS\_COLORARG2, D3DTA\_DIFFUSE);
d\_device->SetTextureStageState(0, D3DTSS\_COLOROP, D3DTOP\_MODULATE);

// setup alpha calculations
d\_device->SetTextureStageState(0, D3DTSS\_ALPHAARG1, D3DTA\_TEXTURE);
d\_device->SetTextureStageState(0, D3DTSS\_ALPHAARG2, D3DTA\_DIFFUSE);
d\_device->SetTextureStageState(0, D3DTSS\_ALPHAOP, D3DTOP\_MODULATE);

// setup filtering
d\_device->SetSamplerState(0, D3DSAMP\_MINFILTER, D3DTEXF\_LINEAR);
d\_device->SetSamplerState(0, D3DSAMP\_MAGFILTER, D3DTEXF\_LINEAR);

// disable texture stages we do not need.
d\_device->SetTextureStageState(1, D3DTSS\_COLOROP, D3DTOP\_DISABLE);

// setup scene alpha blending
d\_device->SetRenderState(D3DRS\_ALPHABLENDENABLE, TRUE);
d\_device->SetRenderState(D3DRS\_SRCBLEND, D3DBLEND\_SRCALPHA);
d\_device->SetRenderState(D3DRS\_DESTBLEND, D3DBLEND\_INVSRCALPHA);

// set view matrix back to identity.
d\_device->SetTransform(D3DTS\_VIEW, &s\_identityMatrix);

}

//—————————————————————————-//
void Direct3D9Renderer::endRendering()
{
}

果然如此,结束时并没有对更改过的渲染状态做任何改变。于是在渲染自己的东西前,增加以下代码:

// Revert RenderState
m_pDirect3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
m_pDirect3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
m_pDirect3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );

m_pDirect3DDevice->SetTextureStageState( 0 , D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pDirect3DDevice->SetTextureStageState( 0 , D3DTSS_COLORARG2, D3DTA_DIFFUSE );
m_pDirect3DDevice->SetTextureStageState( 0 , D3DTSS_COLOROP, D3DTSS_COLORARG2 );

// Position and aim the camera.

D3DXVECTOR3 pos(0.0f, 1.0f, -3.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &pos, &target, &up);
m_pDirect3DDevice->SetTransform(D3DTS_VIEW, &V);

// Set the projection matrix.

D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)800 / (float)600,
1.0f,
1000.0f);
m_pDirect3DDevice->SetTransform(D3DTS_PROJECTION, &proj);

最终成功哦也

 2010-05-06

总结解决办法:

1 更改CEGUI源代码,使用完CEGUI后将其更改过的状态全部设置回来(不靠谱,因为不知道哪些改变过,哪些其实并没改变过)、

2 在调用DX渲染之前,务必将所有应该设置的渲染状态,还有摄像机镜头位置等设置一遍,要意识到CEGUI有对其经行过改动。

Tag 标签: CEGUID3D渲染