#### Archived

This topic is now archived and is closed to further replies.

# Vertices drawn in a very strange way

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

## Recommended Posts

OK here is my problem...I have terrain loaded from a heightmap (very cheesy) and then it does it stuff and renders...however, when i defined the indices clockwise and turned on counter clockwise culling...you see a half-drawn square (1 triangle) drawn in the incorrect order...however, when i changed the indices to counter-clockwise, with counter-clockwise culling still on, it drew all the polys correctly...i dont know if this is becuz of my vertex buffer or is D3D is screwing up...here is the code Index Buffer


WORD a = 0;
WORD b = m_wRows + 2;
WORD c = b - 1;

WORD x, z, i = 0;
for(z = 0; z < m_wCols; z++)
{
for(x = 0; x < m_wRows; x++)
{
pIndices[i]	= a;
pIndices[i + 1]	= c;			//REALLY STRANGE

pIndices[i + 2]	= b;			//FOR SOME REASON (IN THIS CODE SOMEWHERE)

pIndices[i + 3]	= a;			//WE MUST SET INDICES COUNTER CLOCKWISE

pIndices[i + 4]	= b;			//SO THEY ARE DRAWN CORRECTLY WHEN CULLING IS

pIndices[i + 5]	= a+1;			//SET TO COUNTERCLOCKWISE (SEE THE PROBLEM???)

a++;
b++;
c++;
i += 6;
}
a++;
b++;
c++;
}

And the vertex buffer (this is where i think the problem lies, i think it is somehow creating the vertices backwards thus reversing the indices order...however i cant see how it is doing this)

DWORD i = 0; //used to index pcvVertices

VOID* pVertices; //to lock vertex buffer

ENGINE_CUSTOM_VERTEX* pcvVertices = new ENGINE_CUSTOM_VERTEX[m_dwNumOfVertices];	//Array holds the veritces

WORD x = 0;
WORD z = 0;

//Centre terrain around the origin

float zStart	= (0.0f - (m_wSize/2.0f));
float xStart	= (0.0f - (m_wSize/2.0f));

//Setup vertices for terrain

for(z = 0; z < m_wSize; z += m_iStepSize)
{
for(x = 0; x < m_wSize; x += m_iStepSize)
{

pcvVertices[i].x = x + xStart;
pcvVertices[i].y = (float)m_HeightMap[x + (z * m_wSize)] / m_fHeightScale; //value from heightmap

pcvVertices[i].z = z + zStart;

pcvVertices[i].Diffuse = D3DCOLOR_XRGB(m_HeightMap[x + (z * m_wSize)], 0, 0); //color value from height value

pcvVertices[i].nx = 0.0;
pcvVertices[i].ny = 1.0;
pcvVertices[i].nz = 0.0;

i++;
}
}

any ideas appreciated

##### Share on other sites
I think there might be a bug in the inner loop of your index buffer filling code... try:

pIndices[i ] = a;pIndices[i + 1] = c;pIndices[i + 2] = b;pIndices[i + 3] = a;pIndices[i + 4] = a + 1;pIndices[i + 5] = c;

or for the opposite winding order:

pIndices[i ] = a;pIndices[i + 1] = b;pIndices[i + 2] = c;pIndices[i + 3] = a;pIndices[i + 4] = c;pIndices[i + 5] = a + 1;

##### Share on other sites
well u see...your second suggestion would define the indices clockwise on the first 3 and counterclockwise on the second...therefore culling half. in your first suggestion it is the same thing...u are defining the first set counterclockwise and the second set clockwise...so you see this does not help...
and by the way i do not believe it is the indices becuz when they are defined clockwise they are culled when culling is set to counterclockwise....as commented in the source...my best bet is the vertices being defined backwards i just cant see why

BTW: the code i posted currently works when the indices are counterclockwise and so is culling

[edited by - SnakeHunta on March 6, 2003 9:54:20 PM]

##### Share on other sites
quote:

your second suggestion would define the indices clockwise on the first 3 and counterclockwise on the second...

Doh! I made the crazy assumption that b < c, but I now see that''s not the case.

How are you reversing the winding order? Do you swap the indices, or change the vertex data?

  pIndices[i]	= a;pIndices[i + 1]	= b;pIndices[i + 2]	= c;pIndices[i + 3]	= a;pIndices[i + 4]	= a + 1;pIndices[i + 5]	= b;

or

  pcvVertices[i].x = (m_wSize - m_iStepSize - x) + xStart;pcvVertices[i].y = (float)m_HeightMap[x + (z * m_wSize)] / m_fHeightScale; //value from heightmappcvVertices[i].z = z + zStart;

##### Share on other sites
thanks the second one where u changed the assignment for X coordinate worked...i guess it was just reversing the x value of the vertices...when i tried to fix it i also changed the z value...no wonder i could never fix it...changing x and z value to the reverse will keep the same :D next time ill only change one to reverse it ... to reverse the winding order i was swapping the indices (sorry for not saying)
thanks for ur help :D:D now i can move on to octree and so forth

[edited by - SnakeHunta on March 7, 2003 4:56:00 PM]

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002075
×