[Font Rendering] Z-fighting problem [Solved!]

Recommended Posts

I'm working on this FontRenderer and I want to have control over the text per-character. For example: I want this so I can do fading in/out per character like in WoW-quest texts, or letting characters fly in the screen one by one. Or simply updating a single character without having to draw the whole textstring again into a single texture. So, at the moment, every character in a textstring has it's own quad (two triangles) and every quad is textured with the same texture, but different UV-coordinates. The vertices of all the characters are stored in one vertexbuffer (so I can kind of batch them). The texture is one big alphamask with all the characters of the font mapped on it (transparent parts are black, solid parts are white). Performance-wise I'd be better off blitting the whole textstring in a single texture and have to render a single quad, I know, and I will implement that too, but then I don't have any per-character control over it :( ... So I hope I can fix this bug. Now, the problem is, lots of quads are overlapping eachother and have exactly the same local z-position (0.0f). Result: Z-fighting, see this screenshot; lots of characters are drawn only partially and flicker when moving the camera around. How exactly could there be z-fighting between a part that transparent and a part that isn't transparent? :S Maybe my pixelshader-code isn't that great after all? This is what happens for every triangle/quad (in Cg shader language):
void p_AlphaMaskColor(	in float2      		alphaTCoord : TEXCOORD0,
uniform sampler2D	alphaSampler,
uniform float4		inColor,
out float4		outColor : COLOR)
{
float4 alphaMaskPixel = tex2D(alphaSampler, alphaTCoord);
outColor.rgb = inColor.rgb;
outColor.a = inColor.a * alphaMaskPixel.r;
}


It picks a texel from the alphamask-texture, sets the destpixel.rgb to the wanted rgb (a solid color) and sets the destpixel.alpha to the amount of whiteness in the alphamask (through the alphamask's red-channel). When the text is drawn I enable Alpha blending, with next parameters (pseudocode):
Blending = true
SrcBlend = SBF_SRC_ALPHA;
DstBlend = DBF_ONE_MINUS_SRC_ALPHA;


But this only has effect on the textstring versus triangles rendered behind it (like the nice girl in the screenshot :)) Giving each character an incrementing z-pos value does fix the z-fighting (ofcourse), butthat is not exactly what I had in mind :) Does anybody have pointers on how to fix the z-fighting problem without having to draw the characters in a single texture? Thanks :) [Edited by - BigDplayboy on January 21, 2009 12:44:31 PM]

Share on other sites
there are two things you could try.
1. disable depth writing, that should kill any z fighting.
2. discard any pixels that have alpha=0

Share on other sites
Quote:
 Original post by lc_overlordthere are two things you could try.1. disable depth writing, that should kill any z fighting.2. discard any pixels that have alpha=0

Perfect! ... Disabling depthwriting worked like a charm... Thanks a lot!
Do I feel stupid haha... but thanks :)

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

• Forum Statistics

• Total Topics
628342
• Total Posts
2982177

• 9
• 24
• 9
• 9
• 13