[SOLVED] Rendering a 2D sprite toolbar ontop of a 3D World

Started by
6 comments, last by CHTHONIC 17 years, 7 months ago
This is probably easy to solve, a simple device state change i am overlooking or something. I am trying to render sprites (a 2D sprite toolbar) to screen coordinates on top of a 3D world. If I dont render the 3D world, the toolbar renders fine. If i do render the 3D world the toolbar does not render at all (or the world hides it). I was just wondering if anyone could suggest any reasons as to why this might be. If you want any code/pseudo-code posting for how i am attempting to do any part of this then please specify. Thanks, Alex. [Edited by - CHTHONIC on September 18, 2006 3:54:14 AM]
Advertisement
Do you clear and/or disable the z buffer? That could be a definite reason, since (depending on your projection matrix) it's quite likely that the GPU thinks the majority of your 2D interface is "behind" all the objects in your world. Since the 2D interface and the 3D world operate within entirely independent "depth realms", it would make sense to reset the z buffer before rendering 2D.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
So I should be changing my code from something like:

m_device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, m_engineSetup->backColour, 1.0f, 0); m_device->BeginScene();m_sceneManager->RenderSceneObjects(m_simulationRunning);m_sceneManager->RenderToolbar(m_toolbar);m_device->EndScene();m_device->Present(NULL, NULL, NULL, NULL);


To something like:

m_device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, m_engineSetup->backColour, 1.0f, 0); m_device->BeginScene();m_sceneManager->RenderSceneObjects(m_simulationRunning);m_device->Clear(0, 0, D3DCLEAR_ZBUFFER, m_engineSetup->backColour, 1.0f, 0); m_sceneManager->RenderToolbar(m_toolbar);m_device->EndScene();m_device->Present(NULL, NULL, NULL, NULL);


Thanks for your time,

Alex.
That looks right, yes. Also, if that doesn't fix it, could you post the render states that you set within RenderSceneObjects() and RenderToolbar()? There is a definite possibility that render states could cause this problem, though I can't think of anything offhand.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
It did not work, and i've tried a few other things, messing around with the D3DRS_ZENABLE, D3DRS_ZWRITEENABLE and D3DRS_ZFUNC render states. The only render states that will have been set are in initialisation which are setting up fog, ambient lighting and turning on the Z-Buffer.

It might be worth noting that I have rendered sprites in model space in the RenderSceneObject call which changes some render states.

Alex.
How are you rendering your 2D elements? Have you ensured you've cleared/set the relevant matrices? Or set up the correct projection matrix (such as an orthographic projection)?
Could also be that it's applying the world transformation to the sprites. Make darn sure your Sprite->Begin and Sprite->End are exactly around the 2d stuff.
Here's something similar to what I have. It seems to work ok:
void render() {	HANDLE_LOST_DEVICES //handles lost device if any	m_device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,                         m_engineSetup->backColour, 1.0f, 0); 	m_device->BeginScene();	m_device->SetTransform(D3DTS_VIEW, &matView);	m_device->SetTransform(D3DTS_PROJECTION, &matProjection);	D3DXMatrixIdentity(&matWorld);	matWorld=ViewDesc.ScaleMatrix * ViewDesc.RotationMatrix *                  ViewDesc.TranslationMatrix;	m_device->SetTransform(D3DTS_WORLD, &matWorld);		// D R A W  3 D  S T U F F 	if (mesh) {		for(DWORD i = 0; i < g_NumMaterials; i++) {					m_device->SetMaterial(&basic_material);			m_device->SetTexture(0,basic_texture);							if (g_NumMaterials>0) {				m_device->SetMaterial(&g_materials);				if(g_textures != NULL)					m_device->SetTexture(0, g_textures);			} 			mesh->DrawSubset(i);		}	}	// D R A W  2 D  S T U F F	D3DXMatrixIdentity(&matrix); g_sprite->SetTransform(&matrix);	//------------------------------------------------------------			    g_sprite->Begin(D3DXSPRITE_ALPHABLEND); // S P R I T E  B E G I N	g_sprite->Draw(menu_images[0],NULL,NULL,NULL,0xFFFFFFFF);		g_sprite->Draw(images_2d[DOG],NULL,NULL,&position,0xFFFFFFFF);	D3DXMatrixIdentity(&matrix); g_sprite->SetTransform(&matrix);	    g_sprite->End(); // S P R I T E  E N D-----------------------------------//------------------------------------------------------------        m_device->SetTexture(0,NULL);        m_device->EndScene();	ghr=m_device->Present(NULL,NULL,NULL,NULL);			if(ghr==D3DERR_DEVICELOST) {DeviceLost = true; }}

Maybe the begin end thing?
Best of luck! :)
Thanks for all your help guys. Renman, that was it, i needed to set the sprite transform back to the identity matrix before i rendered my toolbar as the sprites being rendered in model-space obviously changed that. I should have spotted that sooner!

Cheers Alex.

This topic is closed to new replies.

Advertisement