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

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

Recommended Posts

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]

Share on other sites
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.

Share on other sites
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);

Alex.

Share on other sites
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.

Share on other sites
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.

Share on other sites
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)?

Share on other sites
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[i]);				if(g_textures[i] != NULL)					m_device->SetTexture(0, g_textures[i]);			} 			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! :)

Share on other sites
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.

Share on other sites

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

Create an account

Register a new account

• Forum Statistics

• Total Topics
628710
• Total Posts
2984335

• 23
• 11
• 10
• 13
• 14