Sign in to follow this  
Yura

Enable the depth for text

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

The King2

I've tried different variants with M32 value - result the same, text is at the top..

 

 

Jason Z

The King2 is right, I'm using Sprites for text rendering, they are out of shader at all. But if there is no other chance to enable Z-value with sprites, I'm ready to try to implement your variant

Edited by Yura

Share this post


Link to post
Share on other sites

Does it work if you call flush on your sprite object afterwards? Try that with different z-values..

 

No, it doesnt. Still the same. I've tried with different values

Share this post


Link to post
Share on other sites
An idea I had was ID3DXSprite might thrash the depth-related render states, but there aren't any flags for Begin and the SharpDX docs list all the states - no depth related stuff. This is really the moment to fire up PIX and check states, your vertex positions and look at the depth buffer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this