2

I've just finished my heightmap, rendered with triangle strip per row. My solution was:

height=data[k];
coords.push_back((float)col);
coords.push_back((float)(height/heightscale));
coords.push_back((float)row);
k+=3; //red is enough, im using greyscale

and the indexes for the strip:

for(int row=0;row<infoheader.biHeight-1;row++)
   {
    for(int col=0;col<infoheader.biWidth;col++)
    {
        indexes.push_back((row+1)*infoheader.biWidth+col);
        indexes.push_back(row*infoheader.biWidth+col);
    }
   }

I use a VertexArrayObject and drawelements in a loop per row. Now I'm using it with a single shader coloring based on height, but i want it to combine with textures(also based on height but still not thats my problem)

I want to put the texture on 2 triangles forming a quad,

0,1-1,1     
 |  /|
 | / |
 |/  |
0,0-1,0

...but I really have no idea how to implement it, I've read all topics about this but they didn't work(only solid color appeared or the whole map was black).

So, is it possible to make an array only for the first "quad" (2 triangles) and it textures the whole map based on it by for example increasing the indexes? If yes how can i do this. And one more: how can I put 1 texture on top of the whole map, scaling it over the terrain.

I could do them with a single quad or a triangle strip with 2 triangles, but not for ~3 000 000 triangles.

The main problem is the indexing, because if I'm right, it draws not only the vertice position based on the indexes but the colors and texture coordinates.

my map: http://kepfeltoltes.hu/view/130827/1094341850Untitled_www.kepfeltoltes.hu_.png I know its a pretty "overasked" question but I still have not found the right way. EDIT: I want to tile textures.

4

1 回答 1

1

我将对你的网格和纹理做出以下假设(可以放宽,但你会明白我的想法):

  • 它们都是方形的
  • 网格在 x 和 z 中的范围从 0 到 TERRAIN_WIDTH(这是一个正整数,包括)。

如果上述假设成立,则地形网格的某些顶点的纹理坐标

v = (v_x, v_y, v_z)

简直就是

texcoord = (v_x / TERRAIN_WIDTH, v_z / TERRAIN_WIDTH)

这个 texcoord 被传递给你的片段着色器并用于纹理查找。您可以使用 const 值或更好的统一值直接在顶点着色器中生成此 texcoord。

uniform float TERRAIN_WIDTH; // or a less flexible const float TERRAIN_WIDTH;
uniform mat4  ModelViewProjection;

in  vec4 Position; // x and z assumed to be in 0 .. TERRAIN_WIDTH (inclusive)

out vec2 TexCoord;

void main()
{
    TexCoord    = Position.xz / TERRAIN_WIDTH:
    gl_Position = ModelViewProjection * Position;
}

警告:未经测试的代码!

编辑:关于平铺:以上内容仍然成立。但是,应将 TERRAIN_WIDTH 替换为 TILE_WIDTH。我仍然假设一个整体的方形网格和方形瓷砖。

例如,如果您的地形尺寸为 256x256,并且由 4 个尺寸为 128x128 的图块组成,则 (240, 0, 240) 处的顶点的 texcoords 计算为:

texcoord = (240 / TILE_WIDTH, 240 / TILE_WIDTH) = (240 / 128, 240 / 128) = (1.875, 1.875)

使用 GL_REPEAT 作为 TEXTURE_WRAP_S 和 TEXTURE_WRAP_T 模式(默认),整数部分(即 1)将被简单地忽略,结果 texcoord 为 (0.875, 0.875)。

您可以看到,这又在 [0, 1] 中,并且将为 (240, 0, 240) 查找与 (112, 0, 112) 相同的纹素。因此,您可以使用相同的纹理获得平铺效果。

注意:为了使平铺在视觉上起作用,您的纹理也需要平铺,否则错觉会在接缝处破裂。;)

于 2013-08-27T14:47:21.973 回答