Question

I have a problem which I have been going crazy over the last few days, I cannot find out what I am doing wrong here. I wrote some code which is supposed to generate indices for a plane consisting of Triangles. The plane is divided into quads, which in turn consist of two triangles - so 6 indices per quad.

This is what the code looks like:

function TaeTerrainTile.GenerateLODIndexBuffer(gbo: TaeGraphicsBufferObject; tile_size, tile_res: integer): integer;
var
  neededIndices: word;
  i_row, i_col: integer;
  i_top_left, i_bottom_right: word;
begin
  // calculate needed indices
  neededIndices := (tile_size div tile_res) * (tile_size div tile_res) * 2 * 3;
  gbo.PreallocateIndices(neededIndices);

  i_col := 0;
  i_row := 0;
  while i_col < (tile_size div tile_res) -1 do
  begin
    while i_row < (tile_size div tile_res) -1 do
    begin


      // top left vertex
      i_top_left := (i_col * tile_size * tile_res) + (i_row * tile_res);
      gbo.addIndex(i_top_left);

      // bottom right vertex
      i_bottom_right := i_top_left + tile_size * tile_res + tile_res;
      gbo.addIndex(i_bottom_right);

      // top right vertex
      gbo.addIndex(i_top_left + tile_res);


      // bottom triangle

      // top left vertex
      gbo.addIndex(i_top_left);
      // bottom left vertex
      gbo.addIndex(i_top_left + tile_size * tile_res);
      // bottom right vertex
      gbo.addIndex(i_bottom_right);

      inc(i_row);
    end;

    i_row := 0;
    inc(i_col);

  end;
end;

It produces indices which look almost perfect:

  Good

Now, you may see that there's missing row and colum in each quad - for this, I thought to remove the two "minus 1" in the two while loops in the code above.

If I do that, it produces this:

  Bad

I am raking my brain about why this happens, but can't find out why. Does anybody see my problem?

Edit: I know that there must be some index out of bound, but I cannot see what is wrong with my formula above.

Edit2: The data layout of the vertices is grid based, here is the code which handles that :

  for y := 0 to AE_TERRAIN_TILE_SIZE - 1 do
  begin
    v[2] := ty + y;
    for x := 0 to AE_TERRAIN_TILE_SIZE - 1 do
    begin
      v[0] := tx + x;
      v[1] := Self._GetHeightCall(v[0], v[2]);
      gbo_high.addVertex(v);
    end;
  end;
Was it helpful?

Solution

I think that the first code is correct, but what you are missing is the code merging adjacent quads. It's normal the vertices of adjacent quads don't overlap.

In particular when they have different resolutions, you need to realize a code that merges quads with 2N with 2N+1 subdivisions, to perform a correct triangulation.

And looking better in your images, you have quads with resolutions 2 (violet), 8 (red) and 32 (green). That's quite strange, since usually when using multi-resolution, it's important to limit adjacent tiles have similar resolutions... you have to handle the triangulation in resolution jumps from 2N to 2N+2.

I mean, I'm surprised that there aren't tiles with 4 and 16-resolution in the middle, but it's just a free observation - it's not wrong in principle, just strange for some experiences I had.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top