我正在尝试在多边形模型/线框上实现水波纹效果。我遵循了这两个非常清晰的指南: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 值,波开始向各个方向传播,包括天空中的某个地方,永远不会返回 - 如下图所示。
最佳答案
我想我已经找到你的问题了。将模数运算符 %
替换为简单的除法 /
,正如您提供的两个链接所建议的那样,看看它的作用。
关于java - 水波纹效果错误(Java Opengl),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22922610/