為了能使Z軸即能在垂直方向運(yùn)動(dòng),又能在水平方向運(yùn)動(dòng),則需要兩個(gè)數(shù)組來保存水平方向和垂直方向的Z坐標(biāo)值。然后Z坐標(biāo)為這兩個(gè)坐標(biāo)值的合成:
const int row = 45;
const int column = 45;
const float width= 9.0f;
const float height = 9.0f;
float coord[row][column][3]; // Flag的坐標(biāo)
float hzcoord[row][column]; // Flag的Z坐標(biāo)水平分量
float vzcoord[column][row]; // Flag的Z坐標(biāo)垂直分量
//計(jì)算Z坐標(biāo)的水平分量
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
hzcoord[i][j] = sin((float)j / (float)column * 360 * 3.1415 / 180);
}
}
//計(jì)算Z坐標(biāo)的垂直分量
for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
vzcoord[i][j] = sin((float)j / (float)column * 360 * 3.1415 / 180);
}
}
float disx = width / column;
float disy = height /row;
//計(jì)算每個(gè)點(diǎn)的坐標(biāo)
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
coord[i][j][0] = j * disx - width / 2.0;
coord[i][j][1] = i * disy - height / 2.0;
coord[i][j][2] = hzcoord[i][j] + vzoord[j][i];
}
}
上面已經(jīng)完成初始化每個(gè)點(diǎn)的坐標(biāo),下面就到了動(dòng)態(tài)的每一幀時(shí)Z坐標(biāo)的傳遞了:
//水平坐標(biāo)分量的傳遞
for (int i = 0; i < row; i++) {
float hold = hzcoord[i][0];
for (int j = 0; j < column - 1; j++) {
hzcoord[i][j] = hzcoord[i][j+1];
}
hzcoord[i][column-1] = hold;
}
//垂直坐標(biāo)分量的傳遞
for (int i = 0; i < column; i++) {
float hold = vzcoord[i][0];
for (int j = 0; j < row - 1; j++) {
vzcoord[i][j] = vzcoord[i][j+1];
}
vzcoord[i][row-1] = hold;
}
//每一幀時(shí)要計(jì)算的每個(gè)點(diǎn)的坐標(biāo)
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
//X與Y坐標(biāo)我們不用去變換,因?yàn)橹豢紤]了Z坐標(biāo)的變化
//coord[i][j][0] = j * disx - width / 2.0;
//coord[i][j][1] = i * disy - height / 2.0;
coord[i][j][2] = hzcoord[i][j] + vzoord[j][i];
}
}