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; 这是用于创建4 * 3地形的代码,其中z为0 *
任何人都可以向我简要解释一下这段代码,提前谢谢..
答案 0 :(得分: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