Skip to main content
GameDev.net gamedev.net
🔒 Locked

Drawing text causes incorrect geometry rendering

Started by blackLightG Dec 2, 2012 at 8:04 AM 9 replies 2.9k views
Original Post
blackLightG
blackLightG
I have been trying to add text to my application using ID3DX10Font and D3DX10CreateFont but every time I say DrawText my other geometry stops rendering correctly. I have attached images of output window with text and with text disabled. I use this code to initialize:
[source lang="cpp"]g_pFont = nullptr;

HRESULT hr = D3DX10CreateFont(g_pd3dDevice,
35, // the font height
0, // the font width
FW_BOLD, // the weight of the font
1, // number of mip levels
FALSE, // this is not an italic font
DEFAULT_CHARSET, // default character set
OUT_DEFAULT_PRECIS, // default size mapping
DEFAULT_QUALITY, // default quality mapping
DEFAULT_PITCH | FF_DONTCARE, // default pitch
L"Helvetica", // use Helvetica as the basis for this font
&g_pFont); // the output

// make sure the font was created correctly
if (FAILED(hr))
{
return hr;
}
return S_OK;[/source]

and to draw:
[source lang="cpp"]HRESULT hr = g_pFont->DrawText( NULL,
TEXT("This is a test string"),
-1,
&rc,
DT_LEFT,
D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );[/source]

Any help appreciated.
Bacterius
Bacterius
It looks like the DrawText method is trashing your projection matrix, and perhaps modifying your fill mode and other stuff? How are you drawing your squares?
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
blackLightG
blackLightG
Actually they are cubes. I update the transform matrix, set the effect file variables and then I draw the cubes. This is done in a loop.

[source lang="cpp"]D3DXMatrixTranslation( &g_World, translate.x, translate.y, translate.z );
.
.
.
g_pWorldVariable->SetMatrix( g_World );
g_pViewVariable->SetMatrix( g_View );
g_pProjectionVariable->SetMatrix( g_Projection );
.
.
.
D3D10_TECHNIQUE_DESC techDesc;
g_pTechnique->GetDesc( &techDesc );
for( UINT p = 0; p < techDesc.Passes; ++p )
{
g_pTechnique->GetPassByIndex( p )->Apply( 0 );
g_pd3dDevice->DrawIndexed( 36, 0, 0 ); // 36 vertices needed for 12 triangles in a triangle list
}[/source]
blackLightG
blackLightG
The actual loop:
[source lang="cpp"]renderer.beginRender();
//text.render();

for(int i = 0; i < 10; ++i)
{
cube.update(t, D3DXVECTOR3(0, i * 2, i * 3), D3DXVECTOR3(0, 1, 0), D3DXVECTOR3(1, 1, 1));
mat.setWorldViewProjection(cube.getWorld(), cam.getView(), cam.getProjection());
cube.render(renderer.g_pd3dDevice, mat.getTechnique());
}

renderer.endRender();[/source]
kauna
kauna
Maybe you need to set textures to NULL or to the texture used by the cube before rendering the cubes?

Cheers!
blackLightG
blackLightG
There are no textures used here, the cubes have vertex colours which are interpolated in the shader
kauna
kauna
But your font engine uses textures ... does that ring a bell?

Cheers!

[edit] sorry I was in a hurry, I realized just after that of course the font engine changes other states too than the textures etc.
Erik Rufelt
Erik Rufelt
The text-rendering will change every render-state known to man. Move any setup you have before your render-loop or before the text-rendering and do everything just before rendering your cubes. That includes anything you do in any init() function that you expect to remain the same since you never change it. The font will change it.
That includes setting the geometry-shader to NULL if you don't use it.
smasherprog
smasherprog
Like Erik said, the drawing function changes states, so you can use the

ID3DX10Sprite::begin(D3DX10_SPRITE_SAVE_STATE);
draw text inside here
ID3DX10Sprite::end();

This will make sure your states are changed back to what they were before the draw text call.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
ankhd
ankhd
no the font messes with the states
blackLightG
blackLightG
smasherprog's idea of using D3DX10_SPRITE_SAVE_STATE worked but now it appears to ignore the z-buffer test. I used
[source lang="cpp"]ID3DX10Sprite *ppSprite;
D3DX10CreateSprite(g_pd3dDevice, 512, &ppSprite);
ppSprite->Begin(D3DX10_SPRITE_SAVE_STATE);
// text rendering
ppSprite->End();[/source]

I'm just going to reset the depth-buffer. I found a useful article here: http://hide1713.wordpress.com/2009/07/06/directx-10-drawtext-disable-depth-buffer-heres-how-to-fix-it/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.