quad doesn't connect to last one

Started by
2 comments, last by steg 21 years ago
Hi all, I am rendering some quads for a terrain, problem is as soon as I render the second quad, it isn''t connected to the first one due to it being a different height, code is shown below (I am using just triangle list - gotta start somewhere) : for(int j=0; j<31; j++) // 1024 is height size { for (int i=0; i<31; i++) { quadVertices[index].x = (i)* ScaleX; quadVertices[index].z = j+1; quadVertices[index].y = GetHeight(i,j+1) / 4; // next side quadVertices[index+1].x = (i+1)* ScaleX; quadVertices[index+1].z = j; quadVertices[index+1].y = GetHeight(i,j) / 4; // last side of triangle quadVertices[index+2].x = (i)* ScaleX; quadVertices[index+2].z = j; quadVertices[index+2].y = GetHeight(i,j) / 4; // next triangle quadVertices[index+3].x = (i)* ScaleX; quadVertices[index+3].z = j+1; quadVertices[index+3].y = GetHeight(i,j + 1) / 4; quadVertices[index+4].x = (i + 1)*ScaleX; quadVertices[index+4].z = j+1; quadVertices[index+4].y = GetHeight(i,j + 1) / 4; quadVertices[index+5].x = (i+1)* ScaleX; quadVertices[index+5].z = j; quadVertices[index+5].y = GetHeight(i,j) / 4; index = index + 6; } How can I get the next quad to connect to previous one ? Kind regards, Steve

If it isn't working, take a bath, have a think and try again...

Advertisement
Hi,

You should use your vertices only to represent your heights.. if so much edges are shared like in a terrain you should always use indices (which enables caching on your gfx card and saves heaps of memory). If you want to calculate your vertices for a batch of 32x32 (heightpoints), you get 32x32 vertices and 31x31 Quads (= 31*31*6 Indices). Now you have to create your Triangles by setting your Indices this way (prepare for awful ASCII-mania

1--2--3
| /| /|
|/ |/ |
4--5--6
| /| /|
|/ |/ |
7--8--9

That''s 4 quads = 8 triangles.. now create your triangles that way:

t1: 1-2-4
t2: 2-5-4
t3: 2-3-5

.. be careful that the winding is always clockwise or counter-clockwise. These values can be directly stored in your batch IndexBuffer and voila.. DrawIndexedPrimitive()

Hope that helps,
narbo
Thanks guys,

My terrain is working with triangle lists at the moment, it is now working fine, I found some code on the web which was using same method as mine but there height worked! I''m still curious though as to how you do get the y value, I know it is from the height map, but in my code I am doing as follows:

quadVertices[index].x = (i)* ScaleX;
quadVertices[index].z = j;
quadVertices[index].y = GetHeight(i,j) / 15;

quadVertices[index+1].x = (i+1)* ScaleX;
quadVertices[index+1].z = j;
quadVertices[index+1].y = GetHeight(i+1,j) / 15;

quadVertices[index+2].x = (i+1)* ScaleX;
quadVertices[index+2].z = j+1;
quadVertices[index+2].y = GetHeight(i+1,j+1) / 15;
quadVertices[index+3].x = (i)* ScaleX;
quadVertices[index+3].z = j;
quadVertices[index+3].y = GetHeight(i,j) / 15;

quadVertices[index+4].x = (i + 1)*ScaleX;
quadVertices[index+4].z = j+1;
quadVertices[index+4].y = GetHeight(i+1,j+1) / 15;

quadVertices[index+5].x = (i)* ScaleX;
quadVertices[index+5].z = j+1;
quadVertices[index+5].y = GetHeight(i,j+1) / 15;

float GetHeight(int i, int j)
{
return pData[i*31+j];
}

If you look how I get the height for the y value, I''m still not sure how this works, how would I draw this down on paper?

Many thanks,
Steve


As I get older, my mind becomes more cluttered.

If it isn't working, take a bath, have a think and try again...

You might want to look at implementing index buffers. It will greatly improve your FPS and I think it actually makes the code more readable:


// First add each vertex.
// Add vertex just adds the vertex to the vertex buffer, increments the vertex count, sets up texture coords, and returns the index.

nCenter = AddVertex ( NW_X + nRadius, NW_Y + nRadius, GetHeight ( NW_X + nRadius, NW_Y + nRadius ) );
nNE = AddVertex (NW_X + nQuadWidth, NW_Y,GetHeight ( NW_X + nQuadWidth, NW_Y ) );
nNW = AddVertex (NW_X, NW_Y, GetHeight ( NW_X, NW_Y ) );
nSW = AddVertex (NW_X, NW_Y + nQuadWidth , GetHeight ( NW_X, NW_Y + nQuadWidth ) );
nSE = AddVertex (NW_X + nQuadWidth, NW_Y + nQuadWidth ,GetHeight ( NW_X + nQuadWidth, NW_Y +nQuadWidth ) );

// First triangle
m_Indices[m_IndexCount++] = nNW;
m_Indices[m_IndexCount++] = nSW;
m_Indices[m_IndexCount++] = nNE;

//Second triangle
m_Indices[m_IndexCount++] = nNE;
m_Indices[m_IndexCount++] = nSW;
m_Indices[m_IndexCount++] = nSE;


My engine is still very much in development, but you're welcome to take a look at the full source:

CVS - http://cvs.bpopp.net/cgi-bin/cvsweb.cgi/terrain/
Journal - http://www.bpopp.net/articles/view.php?id=403

[edited by - bpopp on March 26, 2003 12:10:09 PM]

This topic is closed to new replies.

Advertisement