# Enable the depth for text

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

## Recommended Posts

Hi,

I'm using sprites for text rendering. I need to enable z-buffer (depth) for text, but standart function does not require 3rd component, only X and Y.

Vector3 point = Vector3.Project(....);

if (!string.IsNullOrEmpty(textToRender))
{
infoLine.DrawText(sprite, textToRender, (int)point.X, (int)point.Y, black);
}


So my text is always "at the top", and I need to hide it under other elements. What can I do?

EDIT: infoLine is a SharpDX.Direct3D9.Font class member

Edited by Yura

##### Share on other sites

You need to set transform to your sprite object. Don't know SharpDX but for pure DX9 it's like this:

void D3DFont::DrawText(RECT rect, LPCWSTR lpText, DWORD dFlags, float f, D3DCOLOR color)
{
m_mTransform(3,2) = 1.0f - f;
m_lpD3DSprite->SetTransform(&m_mTransform);
m_lpFont->DrawText(m_lpD3DSprite, lpText, -1, &rect, dFlags, color);
}


Whereas m_mTransform is a D3DXMATRIX-object initialized to identity -> D3DXMatrixIdentity(&m_mTransform). Check out the SharpDX documentation for the equivalent to this.

##### Share on other sites

You could pass a constant in a constant buffer for your text that indicates the depth that it should reside at.  Then you can directly copy the depth value from your constant buffer into the z-coordinate of your output vertex structure in the vertex shader.  This will give you direct control of the text depth without modifying your vertex sizes or anything like that.

##### Share on other sites

You could pass a constant in a constant buffer for your text that indicates the depth that it should reside at.  Then you can directly copy the depth value from your constant buffer into the z-coordinate of your output vertex structure in the vertex shader.  This will give you direct control of the text depth without modifying your vertex sizes or anything like that.

And how do I check if input vertice is a part of the text or a part of the primitive? Primitives can have 0.0 z-component as well...

##### Share on other sites

You need to set transform to your sprite object. Don't know SharpDX but for pure DX9 it's like this:

void D3DFont::DrawText(RECT rect, LPCWSTR lpText, DWORD dFlags, float f, D3DCOLOR color)
{
m_mTransform(3,2) = 1.0f - f;
m_lpD3DSprite->SetTransform(&m_mTransform);
m_lpFont->DrawText(m_lpD3DSprite, lpText, -1, &rect, dFlags, color);
}


Whereas m_mTransform is a D3DXMATRIX-object initialized to identity -> D3DXMatrixIdentity(&m_mTransform). Check out the SharpDX documentation for the equivalent to this.

Can you make some explanation: what if F-value (float f)?

And m_mTransform is World matrix?

##### Share on other sites

Can you make some explanation: what if F-value (float f)?

And m_mTransform is World matrix?

Naming failure ftw.

"float f" is meant to be "float z", therefore the 3rd coordinate, and therefore the depth for your text. m_mTransform initialially is an identity matrix, getting filled with the inverse (depending on how you sort your sprites - front to back or back to front, so you might want to try on that one) z coordinate. You can create it in the function call, but I decided to "cache" it in the class for (small) performance reason. So the better understandable function might go:

void D3DFont::DrawText(RECT rect, LPCWSTR lpText, DWORD dFlags, float z, D3DCOLOR color)
{
D3DXMATRIX mTransform;
D3DXMatrixIdentity(&mTransform);
m_mTransform(3,2) = 1.0f - z;
m_lpD3DSprite->SetTransform(&mTransform);
m_lpFont->DrawText(m_lpD3DSprite, lpText, -1, &rect, dFlags, color);
}

Edited by The King2

##### Share on other sites

Can you make some explanation: what if F-value (float f)?

And m_mTransform is World matrix?

Naming failure ftw.

"float f" is meant to be "float z", therefore the 3rd coordinate, and therefore the depth for your text. m_mTransform initialially is an identity matrix, getting filled with the inverse (depending on how you sort your sprites - front to back or back to front, so you might want to try on that one) z coordinate. You can create it in the function call, but I decided to "cache" it in the class for (small) performance reason. So the better understandable function might go:

void D3DFont::DrawText(RECT rect, LPCWSTR lpText, DWORD dFlags, float z, D3DCOLOR color)
{
D3DXMATRIX mTransform;
D3DXMatrixIdentity(&mTransform);
m_mTransform(3,2) = 1.0f - z;
m_lpD3DSprite->SetTransform(&mTransform);
m_lpFont->DrawText(m_lpD3DSprite, lpText, -1, &rect, dFlags, color);
}


Nope, text is still at the top

sprite.Begin(SpriteFlags.AlphaBlend);
Matrix matr = Matrix.Identity;
matr.M32 = -5; //hardcoded, just a try variat, primitives min z is -1
sprite.Transform = matr; //transform sprite with this matrix
SignFont.DrawText(sprite, "o", new Rectangle((int)(screenPoint.X - 1), (int)(screenPoint.Y - 1),
(int)(screenPoint.X + 1), (int)(screenPoint.Y + 1)), FontDrawFlags.NoClip | FontDrawFlags.Center | FontDrawFlags.VerticalCenter,
new ColorBGRA(100, 0, 255, 255));
sprite.Transform = Matrix.Identity; // after rendering text cancel transform for other elements
.............................
sprite.End();


##### Share on other sites

Did you try different values for matr.M32? 0.0f, 1.0f, 5.0f ...? It must be in the same range as the z for your sprites, obviously, but if changing the value doesn't do anything, well, at least we know there is some issue with eigther the transformation as is or the z-sorting itself. Also, make sure you enable depth sorting - if you do not manually sort the text along with your sprites and draw accordingly, you need to Begin() your sprite with the Alpha-Blending flag, eigther front to back.

Edited by The King2

##### Share on other sites

You could pass a constant in a constant buffer for your text that indicates the depth that it should reside at.  Then you can directly copy the depth value from your constant buffer into the z-coordinate of your output vertex structure in the vertex shader.  This will give you direct control of the text depth without modifying your vertex sizes or anything like that.

And how do I check if input vertice is a part of the text or a part of the primitive? Primitives can have 0.0 z-component as well...

Do you perform separate draw calls for your meshes and your text?  I would assume so, since text rendering is significantly different than mesh rendering and would probably use different shaders.  Sorry if I wasn't clear about that - you need to have a specialized shader that is only used on your text.

##### Share on other sites

Do you perform separate draw calls for your meshes and your text? I would assume so, since text rendering is significantly different than mesh rendering and would probably use different shaders. Sorry if I wasn't clear about that - you need to have a specialized shader that is only used on your text.

From what he posted it looks like he is using SharpDXs LPD3DXSPRITE-encapsulation, so I suppose there is any shader involved at all. At least the functions and objects he uses are perfect meshes for raw DirectX9's sprite/font objects.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 13
• 13
• 60
• 14
• 15
• ### Forum Statistics

• Total Topics
632124
• Total Posts
3004248

×