Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Enable the depth for text


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 02 April 2013 - 06:21 AM

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, 02 April 2013 - 06:22 AM.


Sponsor:

#2 Juliean   GDNet+   -  Reputation: 2720

Like
1Likes
Like

Posted 02 April 2013 - 06:40 AM

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.



#3 Jason Z   Crossbones+   -  Reputation: 5282

Like
1Likes
Like

Posted 02 April 2013 - 06:59 PM

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.



#4 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 03 April 2013 - 01:35 AM

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...



#5 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 03 April 2013 - 01:41 AM

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?



#6 Juliean   GDNet+   -  Reputation: 2720

Like
1Likes
Like

Posted 03 April 2013 - 03:33 AM

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, 03 April 2013 - 03:40 AM.


#7 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 03 April 2013 - 04:02 AM

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();


#8 Juliean   GDNet+   -  Reputation: 2720

Like
1Likes
Like

Posted 03 April 2013 - 04:24 AM

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, 03 April 2013 - 04:35 AM.


#9 Jason Z   Crossbones+   -  Reputation: 5282

Like
1Likes
Like

Posted 03 April 2013 - 03:57 PM

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.



#10 Juliean   GDNet+   -  Reputation: 2720

Like
1Likes
Like

Posted 03 April 2013 - 03:59 PM

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.



#11 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 04 April 2013 - 03:20 AM

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, 04 April 2013 - 03:28 AM.


#12 Juliean   GDNet+   -  Reputation: 2720

Like
1Likes
Like

Posted 04 April 2013 - 05:39 AM

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


Edited by The King2, 04 April 2013 - 05:39 AM.


#13 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 04 April 2013 - 05:50 AM

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



#14 Juliean   GDNet+   -  Reputation: 2720

Like
1Likes
Like

Posted 04 April 2013 - 06:27 AM

Did you explicitely set this flag to eigther front to back or back to front?



#15 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 04 April 2013 - 06:41 AM

Did you explicitely set this flag to eigther front to back or back to front?

 

I've just tried it. Didn't help.



#16 unbird   Crossbones+   -  Reputation: 5990

Like
0Likes
Like

Posted 04 April 2013 - 04:42 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS