Jump to content
  • Advertisement
Sign in to follow this  
BigDplayboy

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

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

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement
Quote:
Original post by lc_overlord
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


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!