Sign in to follow this  

Polygons not appearing? Z buffer issue? (*solved thru stable_sort*)

This topic is 4585 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

Hi, I am using C++ and Direct3D9. I'm making a 2d app, and currently I am using a z buffer and sorting my vertices by z value. Unfortunately I am having the below problem: In this image the button (made up of two polygons) does not show propably (only one polygon is showing). I have no idea why it is doing this. Note that I have not simply forgotten to add that missing polygon. The missing polygon is there (in the vertex buffer) and is not showing. 1)Is this that 'Z Fighting' I have heard about? 2)Is this likely a result of the Z buffer, so if I was to get rid of the z buffer and sort the vertices by z value some other way, would this problem not occur? Anyone able to help? [Edited by - utilae on May 29, 2005 9:05:34 PM]

Share this post


Link to post
Share on other sites
This is most probably not a Z buffer issue but really a missing face.

Check if you're calling DrawPrimitive (or Display for the vertexbuffer) with 2 primitives, not only one.
Check if the primitive type matches your vertexbuffer (triangle list/fan/strip).
Try to crank up debug output to see if anythings wrong with the parameters.

Share this post


Link to post
Share on other sites
All the vertices in the vertex buffer are rendered from one call to drawprimitive, so there is nothing wrong with the parameters. Basically I have noticed that changing the z values, effects this.

Share this post


Link to post
Share on other sites
Quote:
Original post by utilae
All the vertices in the vertex buffer are rendered from one call to drawprimitive, so there is nothing wrong with the parameters. Basically I have noticed that changing the z values, effects this.

It'd be easier if you posted some code. Until then, we can only make guesses:
- Does it help to turn off culling? (D3DCULL_NONE)
If so, your second triangle is defined as a backface, and you'll need to reverse the winding order
- Does it go away if you turn off zbuffering and draw the button last?

Share this post


Link to post
Share on other sites
I have culling off.

The window type thing is made up of 9 polygons with z values as follows:
92-100 (together they make the window, in the form of one square). The button has a z value of 91. Closer to 0 is rendered first. My 2d ortho matrix has a z range of 1 to 1000.

For each texture I do the following (psuedocode):
1)AddSquare() //adds six vertices to a list for a square using triangle lists
//do for window and button (12 vertices)
2)FillVertexBuffer() //I sort the vertices by z value and copy them into the vertex buffer
3)DrawPrimitive() //I draw all the primitives in the vertex buffer.

Some code:
//AddSquare() code

//Add six vertices to make up a square
void CVertexManager::AddSquare(RECT &recPos,const float &fZOrder,TILE &TilePos)
{
if(m_lstVertexList.empty()==true)
m_nPrimitiveCount=0;

//vertices must be sort in order of Z value (1.0 close, 1000.0 far). Z values closer to 0 are rendered first.
//Use RECT coordinates to create vertices
VERTEX vrtBottomLeft,vrtTopLeft,vrtTopRight,vrtBottomRight;
vrtBottomLeft.x=recPos.left; vrtBottomLeft.y=recPos.bottom; vrtBottomLeft.z=fZOrder; vrtBottomLeft.tu=TilePos.m_tuLeft; vrtBottomLeft.tv=TilePos.m_TvBottom;
vrtTopLeft.x=recPos.left; vrtTopLeft.y=recPos.top; vrtTopLeft.z=fZOrder; vrtTopLeft.tu=TilePos.m_tuLeft; vrtTopLeft.tv=TilePos.m_tvTop;
vrtTopRight.x=recPos.right; vrtTopRight.y=recPos.top; vrtTopRight.z=fZOrder; vrtTopRight.tu=TilePos.m_tuRight; vrtTopRight.tv=TilePos.m_tvTop;
vrtBottomRight.x=recPos.right; vrtBottomRight.y=recPos.bottom; vrtBottomRight.z=fZOrder; vrtBottomRight.tu=TilePos.m_tuRight; vrtBottomRight.tv=TilePos.m_TvBottom;

//add six vertices to the list of vertices
// v2---v3|v5 //0.0-----0.0 //tu=horizontal
// | /| //| / | //tv=vertical
// | / | //| / |
// | / | //| / |
//v1|v4---v6 //0.0-----1.0
m_lstVertexList.push_back(vrtBottomLeft);
m_lstVertexList.push_back(vrtTopLeft);
m_lstVertexList.push_back(vrtTopRight);
m_lstVertexList.push_back(vrtBottomLeft);
m_lstVertexList.push_back(vrtTopRight);
m_lstVertexList.push_back(vrtBottomRight);

//update how many primitives and vertices there are to render
++m_nPrimitiveCount;++m_nPrimitiveCount;
}


//FillVertexBuffer() code

//Fill the vertex buffer with all the vertices in
void CVertexManager::FillVertexBuffer(void)
{
//sort vertices in order of z value
sort(m_lstVertexList.begin(),m_lstVertexList.end());

//fill vertex buffer
void *vb_vertices;
g_pVertexBuffer->Lock(0,0,&vb_vertices,0);
memcpy(vb_vertices,&(m_lstVertexList[0]),m_lstVertexList.size()*sizeof(VERTEX));
g_pVertexBuffer->Unlock();

//clear list of vertices
m_lstVertexList.clear();
}




This has worked well up til now, where I found that having some polygons next to each other (where the vertices were the same except z values) caused some polygons not to show. I was thinking that this could be all the result of the z buffer, so I was wondering if I should just get rid of the z buffer and sort vertices by z value my own way (without the z buffer), I could solve this problem that way.

Share this post


Link to post
Share on other sites
What hits my eye on second glance, the triangle seems to be upside down from the way you're actually placing it. According to your layout it should be upside down?

What matrices did you set?
Any reason why you aren't using pretransformed vertices?

Share this post


Link to post
Share on other sites
Quote:

What matrices did you set?


Here's the code to set up mt matrix:

//setup matrix
D3DXMATRIX Ortho2D;
D3DXMATRIX Identity;

//1024x768 2d perspective (1 is near, 1000 is far)
D3DXMatrixOrthoOffCenterLH(&Ortho2D,0,g_nMaxResoultionX,g_nMaxResoultionY,0,1.0,1000.0);
D3DXMatrixIdentity(&Identity);

g_pD3DDevice9->SetTransform(D3DTS_PROJECTION,&Ortho2D);
g_pD3DDevice9->SetTransform(D3DTS_WORLD,&Identity);
g_pD3DDevice9->SetTransform(D3DTS_VIEW,&Identity);


Ah, how would I make that triangle appear the other way, ie not upside down?

Quote:

Any reason why you aren't using pretransformed vertices?

No, though I forget how to make them pre transformed. Would pretransformed vertices help me? I forget, but do pre transformed vertices have limitations?

Share this post


Link to post
Share on other sites
Matrices look ok so far. What z value (fZOrder) are you actually using? If you're getting close to on of the range ends some cards happen to z cull a vertex although they shouldn't.

Pretransformed vertices are not affected by the set matrices or lighting (can be good, can be a limitation, depends on the use). Their coordinates are screen coordinates already, so it's a lot easier to get them exactly right.

All you need to do is to add a RHW value (which you can set to 1.0f) between position and color in the vertex struct.

Sample struct:

struct CUSTOMVERTEX
{
D3DXVECTOR3 position; // The position
float fRHW;
D3DCOLOR color; // The color
float fTU,
fTV;
};


Then set the FVF like this: SetFVF( D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1 );

You may have to offset the position of every vertex by -0.5, -0.5 if you want exact texel to pixel mapping.

The z value of the vertex has to be in the range 0-1 then, it will be set to the z buffer if it's enabled.

Share this post


Link to post
Share on other sites

//I tried switching from this:
// v2---v3|v5 //0.0-----0.0 //tu=horizontal
// | /| //| / | //tv=vertical
// | / | //| / |
// | / | //| / |
//v1|v4---v6 //0.0-----1.0

//To this:
//v1|4----v2 //0.0-----0.0 //tu=horizontal
// |\ | //| / | //tv=vertical
// | \ | //| / |
// | \ | //| / |
//v5----v3|6 //0.0-----1.0



While my button showed up properly, another polygon disappeared.

The Z values I am using range from 100 to 91 (closest). The Z range is 1 to 1000.

---

I tried your idea of using RHW vertices, eg
struct VERTEX
{
float x, y, z;//x,y and z (0.0 close, 10.0 far)
float rhw;
float tu, tv;//texture coordinates
};
const DWORD g_dwFVF=D3DFVF_XYZRHW|D3DFVF_TEX1;

But I ended up with the same result in the screenshot in post 1.
The z range is now 0-1, so the z values this time are:
//9 parts of window:
Window z=0.9
Window z=0.89
Window z=0.88
Window z=0.87
Window z=0.86
Window z=0.85
Window z=0.84
Window z=0.83
Window z=0.82
//button:
Button z=0.819

I am thinking I might try having no Z buffer and just sort the vertices by z value.

Share this post


Link to post
Share on other sites
Removing the z buffer did not solve my problem.

I did however solve my problem. It turns out that when I was sorting my vertices, vertices with duplicate z values were getting reordered, which is not good. So I tried stable_sort instead of sort and my problems vanished. The button shows properly, no polygons are missing.

Thanks everyone for your help.

Share this post


Link to post
Share on other sites

This topic is 4585 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.

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this