IDE: MSVC++ 2005
I'm trying to create my own GUI, and recently just implemented a simple bitmap font renderer. However, for some reason, my texts are not rendered consistently.
Here is a sample:
The first one (Starting at the top) is rendered correctly. But all the others are rendered incorrectly. You can fx. see that the body of the letter 'T' is thinner in at least 2 of the other texts. And it seems a row a pixels is cut off of the top of all the letters in text 3 (Still from the top).
Basically, my question is: What could be the reason for my texts being rendered like this?
I know there's probably hundreds of things that could be the reason, so let me explain what I've found out so far.
My texts are consisting of quads, where each letter is a quad. These quads are then textured with a texture that contains all characters, generated by AngelCode's Bitmap Font Generator. Parsing the font file and generating the vertices works as it should. The vertices are created starting from (0,0,0) and while creating the vertices, I do no transformations whatsoever. This is confirmed by PIX, because when I debug the text's, their vertices have the exact same values, both coordinates, color and UV coordinates.
So the meshes of the individual texts are the same, there's no difference. They have the same coordinates, colors and UV coordinates.
Right before I draw the texts, I translate them into position by calling Device->SetTransform(D3DTS_WORLD, &position). (Just as a side note, I don't use any scaling anywhere in my code, so that can't be the problem either) Here is the code that draws a text:
m_device->SetStreamSource(0, m_vertexBuffer, 0, sizeof(CharVertex));
m_device->SetFVF(CharVertex::FVF);
if (m_fontTexture)
m_device->SetTexture(0, m_fontTexture);
m_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
m_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
DWORD min, mag, mip;
m_device->GetSamplerState(0, D3DSAMP_MINFILTER, &min);
m_device->GetSamplerState(0, D3DSAMP_MAGFILTER, &mag);
m_device->GetSamplerState(0, D3DSAMP_MIPFILTER, &mip);
m_device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
m_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
m_device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
m_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2 * text.length()); // I use 2 primitives per char. Number of primitives = number of chars * 2
m_device->SetSamplerState(0, D3DSAMP_MINFILTER, min);
m_device->SetSamplerState(0, D3DSAMP_MAGFILTER, mag);
m_device->SetSamplerState(0, D3DSAMP_MIPFILTER, mip);
m_device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
In PIX, I've looked at the letter 'T' in 2 different texts. One that renders correctly (The top one) and one that doesn't. In the Pre-Vertex Shader state, both chars have exactly the same values. In the Post-Vertex Shader state, the positions are obviously different, as they are rendered different places on the screen, but the colors and UV coordinates are the same.
So it seems to me, that it can't be a problem in the creation of the quads for the texts. But what can it be then? If I move my texts around on the screen, they are rendered the same way the whole time, meaning they don't change from "Rendered correctly" to "Rendered incorrectly". So at least, the error is consistent.
Regarding the first text, which is rendered correctly in the above image, if I offset the position of the text by just 0.001f, the text is rendered incorrectly, with the same symptoms as the other ones on the image above. I offset the text by just adding the 0.001f to the line where I calculate the transform matrix, like this:
D3DXMATRIX translation, transform;
D3DXMatrixTranslation(&translation, 0.0f, m_window.GetHeight(), 0.0f); // The first text is rendered correctly
//D3DXMatrixTranslation(&translation, 0.001f, m_window.GetHeight(), 0.0f); // The first text is rendered incorrectly
D3DXMatrixMultiply(&transform, &m_window.GetTransformMatrix(), &translation);
m_device->SetTransform(D3DTS_WORLD, &transform);
The same things happens if I offset the text in Y. This makes absolutely no sense at all to me. If I don't offset, the text is rendered correctly, even when I move it around on the screen! But if I offset it by just a tiny bit, the text is rendered incorrectly, also when I move the text around on the screen. How can that be!? The transform matrix is the same if I offset the position or don't offset but move the text while my app is running.
I've spent so much trying to figure this one out, but I've run out of ideas of what it can be. If anyone has any suggestions on what it could be, or maybe just suggestions to what I can try and look for in PIX to locate the problem, I would really appreciate it!
Thanks alot guys!
PS: I don't know if it's helpful or not for you, but you can download my PIX run and see for yourself. It's
here (3,43 mb)
[Edited by - rene_g on May 16, 2008 10:25:12 AM]