#### Archived

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

# creating indices for terrain

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

Hi all, I want to generate quads for my terrain using index buffers, now I can get the indices correct for the first row of them, but I''m confused as how to do the indices for the next row ? Any help is much appreciated, Steve

##### Share on other sites
Here''s one meothod. WIDTH is the width of your terrain (in vertices so it would be in the order of 2^n+1). Also, I assume you are rendering triangle strips.

  int i=0;for (int z=0; z<WIDTH-1; z++){  for (int x=0; x<WIDTH; x++)  {    myIndices[i]=z*WIDTH+x;    myIndices[i+1]=(z+1)*WIDTH+x;    i+=2;  }}

myIndices now holds in sequence the indices for (WIDTH-1) triangle strips. Each strip consists of (WIDTH*2) indices.

Sander Maréchal

##### Share on other sites
Thanks Sander,

I am actually rendering with triangle lists...

Here is how I do the indices for one row of quads :

  int offset = 0;for(int z=0; z < m_zDim; z++)		{			m_pIndices[index] = z + offset;                			m_pIndices[index+1] = z + 1 + offset;    			m_pIndices[index+2] = z + 2 + offset;			m_pIndices[index+3] = z + 3 + offset;			m_pIndices[index+4] = z + 2 + offset;			m_pIndices[index+5] = z + 1 + offset;			index += 6;			offset += 1;		}

Thus indices for first row of three quads :

I''ve hard coded the values for the next row to see what they should be, this looks like :

  	// 1st quad 2nd row	m_pIndices[index] = 1;	m_pIndices[index+1] = 8;     // 7 diff	m_pIndices[index+2] = 3;	m_pIndices[index+3] = 9;	m_pIndices[index+4] = 3;	m_pIndices[index+5] = 8;	index += 6;	// 2nd quad 2nd row	m_pIndices[index] = 3;	m_pIndices[index+1] = 9;     // 6 diff	m_pIndices[index+2] = 5;	m_pIndices[index+3] = 10;	m_pIndices[index+4] = 5;	m_pIndices[index+5] = 9;	index += 6;	// 3rd quad 2nd row	m_pIndices[index] = 5;	m_pIndices[index+1] = 10;    // 5 diff	m_pIndices[index+2] = 7;	m_pIndices[index+3] = 11;	m_pIndices[index+4] = 7;	m_pIndices[index+5] = 10;

I just cannot see a pattern to all this which is making it difficult for me to write a loop to do all the indices ?! I guess I''m making this more complex than it should be ?

Any suggestions ?

Thanks,
Steve

As I get older, my mind becomes more cluttered.

##### Share on other sites
It seems your triangle vertices are arranged like this:
8 - 9 -10 -11|   |   |   |1 - 3 - 5 - 7|   |   |   |0 - 2 - 4 - 6

??

That seems a little strange. A better arrangement would be:

8 - 9 -10 -11|   |   |   |4 - 5 - 6 - 7|   |   |   |0 - 1 - 2 - 3

This way your algorithm will work on each row...

##### Share on other sites
Or to render a MxM patch with only one index buffer call.

m_pIndexBuffer = new unsigned short[(PATCH_SIZE+1) * 2 * (PATCH_SIZE)];	// Create triangle strips	int idx = 0;	for (int y=0; y < PATCH_SIZE; ++y)	{		if (y % 2 == 0)		{			for (int x = 0; x <= PATCH_SIZE; ++x)			{				m_pIndexBuffer[idx++] = y * (PATCH_SIZE+1) + x;				m_pIndexBuffer[idx++] = (y + 1) * (PATCH_SIZE+1) + x;			}		}		else		{			for (int x = PATCH_SIZE; x >= 0; --x)			{				m_pIndexBuffer[idx++] = (y + 1) * (PATCH_SIZE+1) + x;				m_pIndexBuffer[idx++] = y * (PATCH_SIZE+1) + x;			}		}	}

But this will also generate degenerate tris when it loops back. Most of the time this is ok, but you may need to tweak the source data a bit to make it work. What happens is when you get verts like \./ (three verts, one in middle lowest. If that happens on an edge you will get a degenerate tri that shows up. Either tweak the data or find some other method...I''ve yet to find anyone who knows how to render a square patch with one tri strip without the degenerates.

##### Share on other sites
Thanks guys,

I now have my tri's arranged as follows :

0 - 1 - 2 - 3

4 - 5 - 6 - 7

8 - 9 -10 -11

Obviously now easier to do the algoritm now!

Kindest regards,
Steve

As I get older, my mind becomes more cluttered.

[edited by - steg on May 8, 2003 3:47:46 AM]

##### Share on other sites
Just out of interest, this is how I now create my indices :

    	int index = 0;	int offset = 0;	m_pIndices = new WORD[m_xDim * m_yDim];	for(int row=0; row<=m_yDim; row++)	{		for(int col=0; col < m_xDim; col++)		{			m_pIndices[index] = col + 4 + offset;			m_pIndices[index+1] = col + offset;			m_pIndices[index+2] = col + 5 + offset;			m_pIndices[index+3] = col + 1 + offset;			m_pIndices[index+4] = col + 5 + offset;			m_pIndices[index+5] = col + offset;			index += 6;		}		offset += 4;	}

Regards,
Steve

As I get older, my mind becomes more cluttered.

[edited by - steg on May 8, 2003 5:09:08 AM]

##### Share on other sites
Well, that''s good and it gets the job done. But what happens when you change the width to say, 8? You have to go through and change all those magic numbers don''t you? You have the width of the mesh in m_xDim, so you could change the inner loop to:

{  m_pIndices[index] = col + m_xDim + offset;  m_pIndices[index+1] = col + offset;  m_pIndices[index+2] = col + (m_xDim + 1) + offset;  m_pIndices[index+3] = col + 1 + offset;  m_pIndices[index+4] = col + (m_xDim + 1) + offset;  m_pIndices[index+5] = col + offset;  index += 6;}offset += m_xDim;

##### Share on other sites
Thanks BFuselek,

Just found this bug in my code and changed it as you have shown!

Thanks again.
Steve

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
632567
• Total Posts
3007113

×