我正在尝试在多边形模型/线框上实现水波纹效果。我按照这两个非常明确的指南:2D Water和The Water Effect Explained
按照这些指南,我最终得到了(按此顺序)
float[][] heightMapPrev = new float[101][101];
float[][] heightMapCurr = new float[101][101];
// ... filling both arrays with 0.0f ...
heightMapCurr[30][40] = -0.5f; // changing one value to start a water wave
float damping = 0.4f;
// Loop through all the vertices and update their vertical position values according to their surrounding vertices' vertical positions
for (int i = 1; i < 100; i++) {
for (int j = 1; j < 100; j++) {
// Count new vertical position of each vertex
heightMapCurr[i][j] = (heightMapPrev[i+1][j] +
heightMapPrev[i-1][j] +
heightMapPrev[i][j+1] +
heightMapPrev[i][j-1]) % 2.0f -
heightMapCurr[i][j];
// Count water vertical velocity
float velocity = -heightMapCurr[i][j];
// Smooth buffers every frame to waves spread out the waves
float smoothed = (heightMapPrev[i+1][j] +
heightMapPrev[i-1][j] +
heightMapPrev[i][j+1] +
heightMapPrev[i][j-1]) / 4.0f;
// Calculate new height of the water; reduce the effect with *2
heightMapCurr[i][j] = smoothed * 2 + velocity;
// Damp ripples to make them loose energy
heightMapCurr[i][j] *= damping;
}
}
gl.glVertex3f((float)i, heightMapCurr[i][j], (float)j); // for each vertex
for (int i = 0; i < 101; i++) {
for (int j = 0; j < 101; j++) {
temp[i][j] = heightMapPrev[i][j];
heightMapPrev[i][j] = heightMapCurr[i][j];
heightMapCurr[i][j] = temp[i][j];
}
}
我虽然我很清楚那里发生了什么,但显然我不是,因为我的算法出了点问题。波从某个点传播到距离,保持圆形,这没关系。然而,水仍然在圆圈中间“冒泡”,一旦水波纹到达边界,一切都“冒泡”,它永远不会停止。涟漪可能甚至不会碰到所有边界。
你能告诉/解释我做错了什么以及如何纠正错误?我尝试了几个小时更改值(阻尼...),运算符(%为/,+为 - )和平滑功能,但我没有成功。
更新:// 的
在上面的代码中,我使用模运算符(%)而不是(/)。造成这种情况的原因只是因为我总是得到完全错误的heightMap值,波浪开始向所有方向蔓延,包括在天空中的某个地方,从不回头 - 如下图所示。
答案 0 :(得分:1)
我想我找到了你的问题。将模数运算符%
替换为一个简单的除法/
,因为您提供的两个链接都会提示并查看它的作用。
答案 1 :(得分:0)
解决方案甚至比指南提出的解决方案更简单。我找到了它here
根据后面提供的指南,这是整个算法的主体,甚至比任何人都期望的更简单。
// Loop through all the vertices and update their vertical position values according to their surrounding vertices' vertical positions
for (int i = 1; i < 100; i++) {
for (int j = 1; j < 100; j++) {
// Count new vertical position of each vertex
heightMapCurr[i][j] = (heightMapPrev[i+1][j] +
heightMapPrev[i-1][j] +
heightMapPrev[i][j+1] +
heightMapPrev[i][j-1]) / 2.0f -
heightMapCurr[i][j];
// Damp ripples to make them loose energy
heightMapCurr[i][j] -= heightMapCurr[i][j] * damping;
}
}