0

http://www.riemers.net/eng/Tutorials/DirectX/C++/Series1/tut10.php

在链接中给出的地形创建教程中,我无法理解代码,基本上这段代码中发生了什么

short s_Indices[(WIDTH-1)*(HEIGHT-1)*3];


for (int x=0;x< WIDTH-1;x++){
    for (int y=0; y< HEIGHT-1;y++)    {
        s_Indices[(x+y*(WIDTH-1))*3+2] = x+y*WIDTH;
        s_Indices[(x+y*(WIDTH-1))*3+1] = (x+1)+y*WIDTH;
        s_Indices[(x+y*(WIDTH-1))*3] = (x+1)+(y+1)*WIDTH;
    }
}

short s_Indices[(WIDTH-1)*(HEIGHT-1)*6];


for (int x=0;x< WIDTH-1;x++){
    for (int y=0; y< HEIGHT-1;y++)    {
        s_Indices[(x+y*(WIDTH-1))*6+2] = x+y*WIDTH;
        s_Indices[(x+y*(WIDTH-1))*6+1] = (x+1)+y*WIDTH;
        s_Indices[(x+y*(WIDTH-1))*6] = (x+1)+(y+1)*WIDTH;

        s_Indices[(x+y*(WIDTH-1))*6+3] = (x+1)+(y+1)*WIDTH;
        s_Indices[(x+y*(WIDTH-1))*6+4] = x+y*WIDTH;
        s_Indices[(x+y*(WIDTH-1))*6+5] = x+(y+1)*WIDTH;
    }
}

什么是 (x+y (WIDTH-1))*3+2] 以及为什么它等于 x+y*WIDTH;这是创建 z 为 0* 的 4*3 地形的代码

任何人都可以向我简要解释一下这段代码吗,在此先感谢..

4

1 回答 1

1

它设置了一个索引数组,枚举用于绘制几何图形的顶点。每组 3 个索引代表一个三角形(正如您在链接到的页面上看到的那样,有一个带有一组三角形的图形说明了这一点)。

所以例如

s_Indices[(x+y*(WIDTH-1))*3+2] = x+y*WIDTH;

是这样一个三角形的左下角。

s_Indices[(x+y*(WIDTH-1))*3] = (x+1)+(y+1)*WIDTH;

是右上角。

            x+1,y+1
          /|
         / |
    x,y /__| x+1,y
于 2013-03-17T09:21:58.817 回答