Page 1 of 1
Rendering z-fighting
Posted: Fri Mar 08, 2013 6:47 pm
by KaoS
Hi there pros, I hope this hasn't been asked before, I searched for any ways of saying z-fighting I could think of and got nothing
![Sad :(](./images/smilies/ms-sad.png)
anyways I am making a basic rendering type deal for my first program (we prolly all do
![Glee ^^](./images/smilies/ms-glee.png)
) and I am having a few difficulties with the z-fighting of my polygons. basically I sort the table of polygons (sides of a rectangle) based on their center position's offset from the center of the rectangle, this seems to work fine except when I translate the cube it stops working... a link to the script is here:
http://dl.dropbox.com/u/84093549/Links/Render.love
drag the mouse with no buttons pressed to move the shape, try rotating it in its current position and then moving it and rotating it again... only certain sides have issues
any help would be greatly appreciated
Re: Rendering z-fighting
Posted: Sat Mar 09, 2013 10:12 am
by Jasoco
Are you checking for back face?
Code: Select all
function backface(x1, y1, x2, y2, x3, y3)
return not ((y3-y1)/(x3-x1) - (y2-y1)/(x2-x1) <= 0) ~= ((x1 <= x3) == (x1 > x2))
end
(This code is used for triangles, but will still work with squares. Not sure if there's a way to make one for 4-sided polygons.)
Your polygon points would be specified in clockwise order. With the colored face on the clockwise side. And the backface being nothing drawn at all.
Re: Rendering z-fighting
Posted: Sat Mar 09, 2013 1:16 pm
by KaoS
well all I do is sort the table so the back faces are at the beginning of the table and the front ones at the end (I sort it by the z and x cds to determine what's in front), then I render them one after the other and the front ones are drawn over the back ones
Re: Rendering z-fighting
Posted: Sun Mar 10, 2013 7:45 am
by Inny
When you put the 6 faces into your table, you put them in back to front with no rotations, and then rely on the painter's algorithm. Then, when you rotate them around, the original back surface in put in front of the original front surface (because you rotated it around to see the back of it), but then the draw order is the backface, then the other faces, and then the front face. The "Z" fighting is because your algorithm does nothing to render the faces in the correct order. Jasoco's solution is probably the best, i.e., don't rely on Painter's Algorithm. Check to see what direction the surface is facing, and only draw the ones that are facing you.