Drawing text causes incorrect geometry rendering

Started by
9 comments, last by blackLightG 11 years, 4 months ago
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.
Advertisement
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.”

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]
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]
Maybe you need to set textures to NULL or to the texture used by the cube before rendering the cubes?

Cheers!
There are no textures used here, the cubes have vertex colours which are interpolated in the shader
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.
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.
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.
no the font messes with the states

This topic is closed to new replies.

Advertisement