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 标签:

发表评论

电子邮件地址不会被公开。