java - 水波纹效果错误(Java Opengl)

标签 java opengl

我正在尝试在多边形模型/线框上实现水波纹效果。我遵循了这两个非常清晰的指南:2D WaterThe Water Effect Explained

按照这些指南,我最终得到了(按此顺序)

  • 在我的应用程序启动后,两个 float 组全是零,预计其中一个会引发链式 react
  • 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 %

    最佳答案

    我想我已经找到你的问题了。将模数运算符 % 替换为简单的除法 /,正如您提供的两个链接所建议的那样,看看它的作用。

    关于java - 水波纹效果错误(Java Opengl),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22922610/

    相关文章:

    java - javacv 中 getStructuringElement() 的 equal 方法是什么?

    java - OpenCV 在重新部署时使 webapp 崩溃

    objective-c - 我的 NSOpenGLView 无法工作。我什么都试过了

    c++ - win7下opengl窗口捕获的图像是黑色的

    qt - 如何从 QGraphicsItem::paint() 中将 QGLFrameBufferObject 绘制到画家上

    java - 如何在 JavaFX 中更改 .png 图像的颜色?

    java - Java限制文件大小的方法

    每次我想要获取登录信息时,JavaFX 任务都会将状态更改为失败

    c++ - 使用 OpenGL 和 GLUT 创建 FPS 风格的运动系统

    c++ - 加载 OpenGL > 1.1 函数 Windows