Jump to content
  • Advertisement
Sign in to follow this  
jack872

OpenGL Integrating GUI to DirectX game

This topic is 1928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there,
I'm trying to integrate libRocket to a game by revering engeering, I hooked the creation of the DirectX device with no problems, but I can't make the RenderInterface work properlly. I took a look at the DirectX sample given by the library to see how the states were setted, but looks like not all the states are showed, because when I try to render I just see grey boxes and the funny thing is they also gets translated when the game camera moves. I'm new with the DirectX, I've always used OpenGL so I really don't know what states are giving me this issue. For the camera part I think I just have to reset the view matrix, right? But for the states, could you please help me?

Thanks in advice.

Edited by jack872

Share this post


Link to post
Share on other sites
Advertisement

This is the way I set the states:

void myIDirect3DDevice9::saveStates()
{
Utils::debug(LOGGER_DEBUG, "Saving DX states...");
 
HRESULT rs;
rs = m_pIDirect3DDevice9->CreateStateBlock(D3DSBT_ALL, m_pStateBlock);
CDAssert (rs != D3D_OK, (rs ==  D3DERR_OUTOFVIDEOMEMORY) ? "CreateStateBlock error: out of video memory" : "CreateStateBlock error");
rs = m_pIDirect3DDevice9->CreateStateBlock(D3DSBT_ALL, m_pStateBlock+1);
CDAssert (rs != D3D_OK, (rs ==  D3DERR_OUTOFVIDEOMEMORY) ? "CreateStateBlock error: out of video memory" : "CreateStateBlock error");
 
m_pStateBlock[1]->Capture();
 
// Z Buffer states
m_pIDirect3DDevice9->SetRenderState(D3DRS_ZENABLE, FALSE);
m_pIDirect3DDevice9->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
m_pIDirect3DDevice9->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
 
// Alpha states
m_pIDirect3DDevice9->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
m_pIDirect3DDevice9->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_ALWAYS);
m_pIDirect3DDevice9->SetRenderState(D3DRS_ALPHAREF, 0);
m_pIDirect3DDevice9->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
 
// Blending states
m_pIDirect3DDevice9->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
m_pIDirect3DDevice9->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
 
// Stencil buffer states
m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILENABLE, FALSE);
m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILREF, 0);
m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);
m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
m_pIDirect3DDevice9->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
m_pIDirect3DDevice9->SetRenderState(D3DRS_LIGHTING, FALSE);
 
// Sampler states
m_pIDirect3DDevice9->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
m_pIDirect3DDevice9->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
 
// Texture states
m_pIDirect3DDevice9->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_pIDirect3DDevice9->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pIDirect3DDevice9->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_pIDirect3DDevice9->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_pIDirect3DDevice9->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
 
m_pStateBlock[0]->Capture();
m_pStateBlock[1]->Apply();
 
Utils::debug(LOGGER_DEBUG, "DX states saved.");
}
 
This is the rendering:
HRESULT myIDirect3DDevice9::Present(CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion)
{
if (m_pStateBlock[0] && m_pStateBlock[1]) 
{
m_pStateBlock[1]->Capture();
m_pStateBlock[0]->Apply();
RocketGUI::getInstance().render();
m_pStateBlock[1]->Apply();
}
 
HRESULT hRes = m_pIDirect3DDevice9->Present(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
return hRes; 
} 

and this is how the librocket renderinterface works:

void RenderInterfaceDirectX::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry, const Rocket::Core::Vector2f& translation)
{
D3DXMATRIX world_transform;
D3DXMatrixTranslation(&world_transform, translation.x, translation.y, 0);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &world_transform);
 
RocketD3D9CompiledGeometry* d3d9_geometry = (RocketD3D9CompiledGeometry*) geometry;
 
g_pd3dDevice->SetFVF(vertex_fvf);
g_pd3dDevice->SetStreamSource(0, d3d9_geometry->vertices, 0, sizeof(RocketD3D9Vertex));
g_pd3dDevice->SetIndices(d3d9_geometry->indices);
 
if (d3d9_geometry->texture != NULL)
g_pd3dDevice->SetTexture(0, d3d9_geometry->texture);
else
g_pd3dDevice->SetTexture(0, NULL);
 
g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 
d3d9_geometry->num_vertices, 0, d3d9_geometry->num_primitives);
}
But this is what I get
lwwo.png

As you can see it renders a big grey rectangle which gets rototranslated when the game camera moves

Share this post


Link to post
Share on other sites
For GUI rendering you probably need set the view transform to identity and the projection to an orthographic.

Share this post


Link to post
Share on other sites

For GUI rendering you probably need set the view transform to identity and the projection to an orthographic.

 

just tried right now, doesn't work. It doesn't get translated anymore but it is still grey.

Share this post


Link to post
Share on other sites
Have you tried the DirectX sample that comes along ? Does that work ? I spot that you disable alpha blend, the sample enables it.

I'm probably not of much help anymore, though, since I don't know that library, sorry.

Share this post


Link to post
Share on other sites

Thanks unbird,

I've fixed that part of code, now it doesn't render anything (or at least I can't see it). Between, yes, I've compiled the sample and works quite well....I'm using the same states but doesn't work anyway in the game. Well, to be honest, it works at the beginning on the video intro, but when the game gets into it menu, starts to give me these issues. The problem is I don't know what states the game is changing. I really don't know what to do :(

Didn't do so many changes into the code, just added two lines here

HRESULT myIDirect3DDevice9::Present(CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion)
{
	if (m_pStateBlock[0] && m_pStateBlock[1]) 
	{
		D3DXMATRIX matView, projection;
		D3DXMatrixIdentity(&matView);
		Rocket::Core::Vector2i size = RocketGUI::getInstance().getSize();
		m_pIDirect3DDevice9->SetTransform(D3DTS_VIEW, &matView);
		D3DXMatrixOrthoOffCenterLH(&projection, .0f, (float)size.x, (float)size.y, .0f, -1.f, 1.f);
		m_pIDirect3DDevice9->SetTransform(D3DTS_PROJECTION, &projection);

		m_pStateBlock[1]->Capture();
		m_pStateBlock[0]->Apply();
		RocketGUI::getInstance().render();
		m_pStateBlock[1]->Apply();
	}

	HRESULT hRes = m_pIDirect3DDevice9->Present(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
	return hRes; 
}

And enabled the alpha-blending (like the sample https://github.com/lloydw/libRocket/blob/master/Samples/basic/directx/src/main.cpp)
 

void myIDirect3DDevice9::saveStates()
{
	Utils::debug(LOGGER_DEBUG, "Saving DX states...");

	HRESULT rs;
	rs = m_pIDirect3DDevice9->CreateStateBlock(D3DSBT_ALL, m_pStateBlock);
	CDAssert (rs != D3D_OK, (rs ==  D3DERR_OUTOFVIDEOMEMORY) ? "CreateStateBlock error: out of video memory" : "CreateStateBlock error");
	rs = m_pIDirect3DDevice9->CreateStateBlock(D3DSBT_ALL, m_pStateBlock+1);
	CDAssert (rs != D3D_OK, (rs ==  D3DERR_OUTOFVIDEOMEMORY) ? "CreateStateBlock error: out of video memory" : "CreateStateBlock error");

	m_pStateBlock[1]->Capture();

	// Z Buffer states
	m_pIDirect3DDevice9->SetRenderState(D3DRS_ZENABLE, FALSE);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);

	// Alpha states
	m_pIDirect3DDevice9->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_ALWAYS);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_ALPHAREF, 0);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);

	// Blending states
	m_pIDirect3DDevice9->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

	// Stencil buffer states
	m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILENABLE, FALSE);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILREF, 0);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
	m_pIDirect3DDevice9->SetRenderState(D3DRS_LIGHTING, FALSE);
	
	// Sampler states
	m_pIDirect3DDevice9->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
	m_pIDirect3DDevice9->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

	// Texture states
	m_pIDirect3DDevice9->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
	m_pIDirect3DDevice9->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	m_pIDirect3DDevice9->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	m_pIDirect3DDevice9->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
	m_pIDirect3DDevice9->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

	m_pStateBlock[0]->Capture();
	m_pStateBlock[1]->Apply();

	Utils::debug(LOGGER_DEBUG, "DX states saved.");
}

Share this post


Link to post
Share on other sites
I've never used the state blocks, but IIRC they do not record already set states, just the ones you change. Maybe that's a problem. Try setting everything before you start rendering the GUI instead of using the state blocks.
 
Alternatively: It's going to be a bit tedious but you will need to fire up PIX for this. First run the sample, grab a Frame (F12) and goto a draw call of the GUI stuff. Click on the device to inspect the device states. You can also save it as HTML. Then do the same with your code. Afterwards, compare (e.g. with a diff app).
 
With PIX you can inspect what is happening renderwise anyway, maybe you spot the problem already. Edited by unbird

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!