hud on 3d terrain?

Started by
3 comments, last by lymantok 16 years, 5 months ago
Hi I'm trying to draw a HUD and crosshair on a terrain. I've drawn the 3D stuff first, then the 2D HUD as a sprite. What I get displayed is the HUD behind the 3D terrain which only becomes visible when I look skyway. Also the radar map (lower right quadrant of the screen) always obscures 1/4 of the HUD which is centered in the middle of the screen. Code below. Any help appreciated. Thx! void RenderToTexDemo:OnResetDevice() { //Render 3D Objects mGfxStats->onResetDevice(); mTerrain->onResetDevice(); mRadarMap->onResetDevice(); mSky->onResetDevice(); HR(mRadarFX->OnResetDevice()); float w = (float)md3dPP.BackBufferWidth; float h = (float)md3dPP.BackBufferHeight; mFirstPersonCamera.setLens(D3DX_PI * 0.25f, w/h, 1.0f, 2000.0f); mBirdsEyeCamera.setLens(D3DX_PI * 0.25f, w/h, 1.0f, 2000.0f); //Render 2D Objects (i.e., HUD and crosshair //gbb HUD start HR(mSprite->OnResetDevice()); // Sets up the camera 1000 units back looking at the origin. D3DXMATRIX V; D3DXVECTOR3 pos(0.0f, 0.0f, -1000.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXMatrixLookAtLH(&V, &pos, &target, &up); HR(gd3dDevice->SetTransform(D3DTS_VIEW, &V)); // The following code defines the volume of space the camera sees. D3DXMATRIX P; RECT R; GetClientRect(mhMainWnd, &R); float width = (float)R.right; float height = (float)R.bottom; D3DXMatrixPerspectiveFovLH(&P, D3DX_PI*0.25f, width/height, 1.0f, 5000.0f); HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &P)); // This code sets texture filters, which helps to smooth out distortions // when you scale a texture. //HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR)); //HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR)); //HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR)); // Disable Direct3D lighting. HR(gd3dDevice->SetRenderState(D3DRS_LIGHTING, false)); // Specify an alpha test and reference value. HR(gd3dDevice->SetRenderState(D3DRS_ALPHAREF, 10)); HR(gd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER)); // Setup alpha blending. HR(gd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE)); HR(gd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1)); HR(gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA)); HR(gd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA)); // Indicates that we are using 2D texture coordinates. HR(gd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2)); //gbb HUD end } void RenderToTexDemo::updateScene(float dt) { mGfxStats->update(dt); gDInput->poll(); gCamera->update(dt, mTerrain, 5.5f); } void RenderToTexDemo::drawCrossHair() { // Turn on the alpha test. HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true)); // Draw the crosshair. HR(mSprite->Draw(mCrossHairTex, 0, &mCrossHairCenter, 0, D3DCOLOR_XRGB(255, 255, 255))); HR(mSprite->Flush()); // Turn off the alpha test. HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false)); }
Advertisement
have you tried disabling the z-buffer before drawing your 2d stuff.
Thx. I added the following just before I disable D3D lighting.

HR(gd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE));

The problem now is that it messes up my 3D cube map terrain as the z-buffer is disabled.

The objects are drawn as follows: radar map, terrain, then the hud (sprite).

void RenderToTexDemo::drawScene()
{
// Render 3D objects
// Draw into radar map.
D3DXVECTOR3 pos(gCamera->pos().x, gCamera->pos().y + 1000.0f, gCamera->pos().z);
D3DXVECTOR3 up(0.0f, 0.0f, 1.0f);
mBirdsEyeCamera.lookAt(pos, gCamera->pos(), up);

mRadarMap->beginScene();
gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff000000, 1.0f, 0);
gCamera = &mBirdsEyeCamera
mTerrain->draw();
mRadarMap->endScene();

// Draw sky and terrain
gCamera = &mFirstPersonCamera

HR(gd3dDevice->BeginScene());
mSky->draw();

mTerrain->draw();

HR(gd3dDevice->SetStreamSource(0, mRadarVB, 0, sizeof(VertexPT)));
HR(gd3dDevice->SetVertexDeclaration (VertexPT::Decl));

HR(mRadarFX->SetTexture(mhTex, mRadarMap->d3dTex()));
UINT numPasses = 0;
HR(mRadarFX->Begin(&numPasses, 0));
HR(mRadarFX->BeginPass(0));
HR(gd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2));
HR(mRadarFX->EndPass());
HR(mRadarFX->End());

mGfxStats->display();

HR(gd3dDevice->EndScene());

// Present the backbuffer.
//HR(gd3dDevice->Present(0, 0, 0, 0));

// End Render 3D objects

// Render 2D HUD

HR(gd3dDevice->BeginScene());
HR(mSprite->Begin(/*D3DXSPRITE_OBJECTSPACE|*/D3DXSPRITE_DONOTMODIFY_RENDERSTATE));
drawCrossHair();
HR(mSprite->End());
HR(gd3dDevice->EndScene());

// Present the backbuffer.
HR(gd3dDevice->Present(0, 0, 0, 0));

// End Render 2D HUD
}

I still get no HUD. Any other ideas on how to overlay a HUD on a 3D terrain?

Thx.
change the rendering states to be a straight pixel draw (turn off all blending etc...)
--------------------------------------------[size="1"]Ratings are Opinion, not Fact
Hi thanks again.

I think turning off all blending will work fine for the rest of the hud, but I would think I need alpha blending on for the crosshairs, which I'm trying to get working now.

Should I be using SetTransform and PerspectiveFowLH to get the crosshairs to appear or is a straight draw using screen coordinates ok?

Thx.

This topic is closed to new replies.

Advertisement