Fluxtah

Hi

I am having a few problems creating an index buffer for a triangle strip for a terrain section.

I have a section similar to the following.

```0 - 1 - 2
| / | / |
3 - 4 - 5
| / | / |
6 - 7 - 8
```

I define my triangle strip using the following indices also with one degenerate triangle, I have bolded it so it stands out better.

0, 3, 1, 4, 2, 5, 5, 3, 3, 6, 4, 7, 5, 8

My first question is, is this degenerate correct it seems to be correct, but my next problem is what confuses matters as it really affects wether my terrain tile renders correctly without gaps.

When it comes round to making my DrawIndexedPrimitives call, I have to specify the number of vertices, and the total number of primitives.

I would have thought the number of vertices will be the total count of indices (indices.length) and I would have thought that the total number of primitives would be:

(number of quads * 2) + rows - 1

where rows - 1 would define the number of degenerate triangles.

Something is terribly going wrong with this, if because it renders the strips with gaps inbetween them, its only when I bump up the number of primitives to some wrong value (but more triangles than there is) then it displays the terrain correctly without gaps.

Can anyone help me here and tell me what I might be doing wrong, or help me calculate the correct number of primitives for a DrawIndexedPrimitives call using a triangle strip.

This is probably a simple thing to a 3d guru but I am unfortunately not that :(

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Inaki Ayucar

Just for some knowledge:

in DrawIndexedPrimitives you specify the number of primitives to render. As you are telling the API you will render a TriangleStrip, it knows how to deal with the vertices passed through. i.e. If you are rendering "Lines", you specify the number of lines (for example 2) with the array of vertices (for example 4, two for each line). But you tell the function numberOfPrimitives = 2. The API expects 4 vertices as is expecting "Lines". In TriangleStrip is nearly the same.

I guess you cannot express that kind of geometry arrangement with a single triangleStrip. In triangleStrips, each triangle shares two vertices with the previous one and thatĄŻs not your case. I mean, in triStrips, the first triangle is rendered with the first 3 vertices, and after that, each new triangle is rendered with the last 2 vertices of the previous tri, and a new one.

You will have to think of another way of arranging your geometry. Dont even think in triangle fans, as they wonĄŻt work either (in a fan, all the triangles share one vertex).

IĄŻm afraid you will have to store every vertices and indices separately (with no vertex sharing).

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Fluxtah

Hi Inaki, thanks for the info.

It might seem strange using a trianglestrip in this way, however it is possible and is the recommended way to render a terrain section. the trick is the degenerate triangle at the end of each row, it has 0 area and is the connector to the next row, so essentially the strip can have multiple rows because of this, my book 'Real Tile 3D Terrain Engines' explains this.

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Inaki Ayucar

Hey! thatĄŻs a good one!

I didnĄŻt know that (thought it was a mistake, repeating that tri). Thank you very much.

So know, you should just set the proper number of vertices. In your example:

Vertices: 0, 3, 1, 4, 2, 5, 5, 3, 3, 6, 4, 7, 5, 8

NumberOfPrimitives = 12

NumberOfVertices = 14

That should work.

Anyway, as you are using TriangleStrips, is it really necessary to use an IndexBuffer. I mean, the vertex indices is implicit in the form of a triangleStrip. Have you considered using just the DrawPrimitives method (setting the indexBuffer to null) instead of using DrawIndexedPrimitives

Thanks again.

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Fluxtah

cool, I managed to sort it out in the end as its just vertices - 2 :( so simple and it had me very confused! thanks for the help on that, helps confirm if I am doing it correctly.

Well I use an index buffer otherwise I will be unable to specify the degenerate triangle, my terrain tiles are variable but I go with 33x33 vertice tiles. I have a method that can build index buffers of several layers by skipping vertices, effectively giving me multiple LOD layers.

thanks for the help!

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Inaki Ayucar

Well you can create the degenerate triangle in the vertex buffer, by duplicating the needed vertices. In that way you wouldnĄŻt need the index buffer. If discarding the index buffer is not good for you, you can fill it up following the TriangleStrip shape:

`Vertex buffer:`
```0 - 1 - 2
| / | / |
3 - 4 - 5
| / | / |
6 - 7 - 8
```
`Index buffer:`
`face 0: 0, 3, 1, `
`face 1: 1, 3, 4, `
`face 2: 1, 4, 2`
`face 3: 2, 4, 5`
`I guess your magical degenearte would come here... to allow continuing`
`cheers !`

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Fluxtah

yeh thats possible however for 1024x1024 vertices, I only use 32x32 indices, because the terrain is in tiles, I only need one index buffer, and also the cool thing about using an index buffer, is I am able to do LOD by skipping over vertices at various levels.

oioi thanks

Fluxtah

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Darkside

Fluxtah

Could you give an example of that implementation or a link

I am very interested how your Tiling the Terrain and then applying LOD to it.

Darkside

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Fluxtah

Well my code is pretty much in the early stages, I have LOD levels and I can switch through them for the entire terrain, but thats just the first step, there are several things I can do with having a number of LOD levels per terrain section, but they are all quite complicated.

Before I even venture into using LOD for terrain, the first thing I am aiming for is culling which can be easily done with a quadtree implementation.

Most of the information I on how to do things one way, is from the book 'Real Time 3D Terrain Engines' by Greg Snook, but I cant say that I have followed it fully, I just took the idea of having a several shared index buffers that represent the LOD levels of a terrain section. Here is a link to that book on Amazon http://www.amazon.com/Real-Time-Terrain-Engines-DirectX-Development/dp/1584502045 I highly recommend it.

There is another article thats very interesting http://www.gamedev.net/columns/hardcore/geomorph/default.asp which has some useful information.

I am not sure if you know but an MSDN forum user known as ClydeCoulter has kindly provided a quadtree terrain implementation found here http://www.codeplex.com/quadtreeload

When I get my sketchpad terrain system fully working I may provide source code for it, but it is part of an engine and has dependencies on certain libraries in the engine, I havent taken the component approach as I am not to keen on XNA's component implementation, I would prefer just to write a framework for developing 3d scenes and possibly games.

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Darkside

Thanks for that all usefull info, have been using clyde's quadtree project in some simulations for a while now, good project.

Let me know if you release anything else.

Rgds

Darkside

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

ClydeCoulter

I have just implemented the triangle strip into the Quadtree project, and it did require the degerates as you have done. The default is CW then CCW in the ordering (by default) so I generated a degenerate triangle to start with.

I ran into the problem of forgetting to include the degenerates in the triangle count.......stumped me for a bit (with missing triangles at the end of each sector).

I had planned on doing the LOD thing, but hey, Fluxtah, you and I seem to be on somewhat the same path........I would be interested in conversation between us (and others that use the Quadtree project or yours (if you release it)). Some conversation about detail textures has started on the Quadtree project site (I noticed that you had been wrangling with that one a short time ago in the XNA forums).

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Fluxtah

Hi Clyde, let me know the best way we can have a conversation, I have msn messenger if that helps.

I got the detail textures working for my needs right now and can share the shader, its probably the simplest splatting approach, I am still trying to decide how many layers to support as not to kill performance, I have 3 at the moment and one lightmap.

The LOD is nothing more than several index buffers at different tesselations that I build with one method that takes a step parameter which is essentially uses to skip over vertices, I am doing no distance based switching on this yet.

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

ClydeCoulter

Fluxtah, I don't have a messenger installed at this time.....perhaps email (clydecoulter_AT_tds.net_without_the_underscores and be sure to include Fluxtah in the subject, if not your email name, or it will get deleted as junk)

I am currently sorting front to back on the nodes of the quadtree (version to be released soon, I hope) so that I can use that to change out which vertices (index buffer) to use for lod and when to add grass/weed billboards. I hope to do some sort of quick check for droping nodes that are occluded (since the nodes are sorted) by other nodes if I can figure out a really fast way to do it....otherwise it may be faster just to let the depth buffer test fail them.

### Re: Game Technologies: General problems creating a triangle strip for a terrain section

Fluxtah

cool, I am still working on my shader, just getting lightmapping working, it was simple when I was only dealing with 3 textures but now I am lerping them all together I am confused at where to multiply in the heightmap, im working and a try it and see kind of basis :) Once I have it reasonably done I will email to you.