水波纹效应出错(Java Open GL)

时间:2014-04-07 20:37:55

标签: java opengl

我正在尝试在多边形模型/线框上实现水波纹效果。我按照这两个非常明确的指南:2D WaterThe 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;
    

  • 算法本身。我遍历所有顶点,计算它们的新y位置并平滑/阻尼效果
  • // 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;
        }
    }
    

  • (重新)绘制所有顶点,在这两个for循环中
  • gl.glVertex3f((float)i, heightMapCurr[i][j], (float)j); // for each vertex
    

  • 最后,正如指南告诉我的那样,我在两个数组中交换值 - waveMapPrev中的值现在在waveMapCurr中,反之亦然
  • 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];
        }
    }
    


    我虽然我很清楚那里发生了什么,但显然我不是,因为我的算法出了点问题。波从某个点传播到距离,保持圆形,这没关系。然而,水仍然在圆圈中间“冒泡”,一旦水波纹到达边界,一切都“冒泡”,它永远不会停止。涟漪可能甚至不会碰到所有边界。

    In the top image, you can see the water ripple going right from the specified point. The bottom image shows what happens after the ripple reaches borders. It is like this until exiting the app.

    你能告诉/解释我做错了什么以及如何纠正错误?我尝试了几个小时更改值(阻尼...),运算符(%为/,+为 - )和平滑功能,但我没有成功。


    更新:// 的 在上面的代码中,我使用模运算符(%)而不是(/)。造成这种情况的原因只是因为我总是得到完全错误的heightMap值,波浪开始向所有方向蔓延,包括在天空中的某个地方,从不回头 - 如下图所示。 Error in rendering when using / instead of %

    2 个答案:

    答案 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;
        }
    }