SFML中的2D水着色器

时间:2013-01-29 09:31:40

标签: c++ glsl sfml

我想实现描述herehere的2D水面算法。

但是我没有使用两个int数组并在CPU上计算,而是想使用SFML的sf::RenderTexture(基本上是FBO)和GLSL着色器来运行GPU上的所有内容。我想使用SFML,因为它非常简单,之前我已经使用过它,所以我知道我的方法。

到目前为止,我取得了一些进展。我能够在它们之间正确设置3 sf::RenderTextures和乒乓(因为除了int数组之外,你不能同时读取和写入同一个sf::RenderTexture)。我还能够使高度场创建形式的算法在-32.767到32.767的范围内调整到0到1的范围(或者更精确地-0.5到0.5用于计算)。另外,添加新的涟漪也有一定的作用。所以到目前为止,你实际上可以看到一些波动。

现在我的问题出现了:波浪真的消失了,真的很快,我甚至都没有应用任何阻尼。根据该算法,如果没有施加阻尼,则波纹不会停止。甚至是相反的方式。如果我应用“放大”,波浪看起来接近你期望它们看起来的样子(但是它们仍然消失而没有任何阻尼应用于它们)。我的第一个想法是,这是因为我在范围0 - 1而不是整数中使用float,但我只看到这是一个问题,如果使用乘法,但我只使用加法和减法。

这是我的SFML C ++代码:

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML works!");
    window.setFramerateLimit(12);

    sf::RenderTexture buffers[3];
    buffers[0].create(500, 500);
    buffers[1].create(500, 500);
    buffers[2].create(500, 500);
    sf::RenderTexture* firstBuffer = buffers;
    sf::RenderTexture* secondBuffer = &buffers[1];
    sf::RenderTexture* finalBuffer = &buffers[2];

    firstBuffer->clear(sf::Color(128, 128, 128));
    secondBuffer->clear(sf::Color(128, 128, 128));
    finalBuffer->clear(sf::Color(128, 128, 128));

    sf::Shader waterHeightmapShader;
    waterHeightmapShader.loadFromFile("waterHeightmapShader.glsl", sf::Shader::Fragment);

    sf::Sprite spritefirst;
    spritefirst.setPosition(0, 0);
    spritefirst.setTexture(firstBuffer->getTexture());

    sf::Sprite spritesecond;
    spritesecond.setPosition(500, 0);
    spritesecond.setTexture(secondBuffer->getTexture());

    sf::Sprite spritefinal;
    spritefinal.setPosition(0, 500);
    spritefinal.setTexture(finalBuffer->getTexture());

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();

            if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Escape)
                window.close();
        }


        waterHeightmapShader.setParameter("mousePosition", sf::Vector2f(-1.f, -1.f));
        // if mouse button is pressed add new ripples
        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
            if(mousePosition.x < 500 && mousePosition.y < 500)
            {
                sf::Vector2f mouse(mousePosition);

                mouse.x /= 500.f;
                mouse.y /= 500.f;

                mouse.y = 1 - mouse.y;


                std::cout << mouse.x << " " << mouse.y << std::endl;

                waterHeightmapShader.setParameter("mousePosition", mouse);
            }
        }


        waterHeightmapShader.setParameter("textureTwoFramesAgo", firstBuffer->getTexture());
        waterHeightmapShader.setParameter("textureOneFrameAgo", secondBuffer->getTexture());

        // create the heightmap
        secondBuffer->display();
        finalBuffer->clear(sf::Color(128, 128, 128));
        finalBuffer->draw(sf::Sprite(secondBuffer->getTexture()), &waterHeightmapShader);
        finalBuffer->display();


        spritefirst.setTexture(firstBuffer->getTexture());
        spritesecond.setTexture(secondBuffer->getTexture());
        spritefinal.setTexture(finalBuffer->getTexture());

        window.clear();
        window.draw(spritefirst);
        window.draw(spritesecond);
        window.draw(spritefinal);
        window.display();

        // swap the buffers around, first becomes second, second becomes third and third becomes first
        sf::RenderTexture* swapper = firstBuffer;
        firstBuffer = secondBuffer;
        secondBuffer = finalBuffer;
        finalBuffer = swapper;
    }

    return 0;
}

这是我的GLSL着色器代码:

uniform sampler2D textureTwoFramesAgo;
uniform sampler2D textureOneFrameAgo;
uniform vec2 mousePosition;

const float textureSize = 500.0;
const float pixelSize = 1.0 / textureSize;

void main()
{
    // pixels position
    vec2 position = gl_TexCoord[0].st;

    vec4 finalColor = ((texture2D(textureOneFrameAgo, vec2(position.x - pixelSize, position.y)) +
                        texture2D(textureOneFrameAgo, vec2(position.x + pixelSize, position.y)) +
                        texture2D(textureOneFrameAgo, vec2(position.x, position.y + pixelSize)) +
                        texture2D(textureOneFrameAgo, vec2(position.x, position.y - pixelSize)) - 2.0) / 2) -
                       (texture2D(textureTwoFramesAgo, position) - 0.5);

    // damping
//    finalColor.rgb *= 1.9;  // <---- uncomment this for the "amplifiction" ie. to see the waves better
    finalColor.rgb += 0.5;

    // add new ripples
    if(mousePosition.x > 0.0)
    {
        if(distance(position, mousePosition) < pixelSize * 5)
        {
            finalColor = vec4(0.9, 0.9, 0.9, 1.0);
        }
    }

    gl_FragColor = finalColor;

}

请记住,这只是高度场的创建。水没有阴影。

你知道为什么波浪在没有阻尼的情况下自我消失了吗?

2 个答案:

答案 0 :(得分:2)

如果我正确读取代码,则为纹理的颜色/高度采样前一帧,并使用四个相邻像素/纹素来确定当前像素的颜色/高度。

当您计算(缩放)这些邻居时,您可能会错过包含您正在寻找的颜色/高度的纹理元素。它可能不是最高的纹理元素,只是它旁边的一点点,导致意想不到的阻尼。

这是你的地方,只需使用加法和减法:

const float pixelSize = 1.0 / textureSize;

通过使用此值,您可能会错过正在寻找的纹素。

修改

另外:您对样本求平均值,因此结果将始终小于样本的最大值。因此,您可以选择最大值,而不是平均值。这可能会产生奇怪的结果,但也会产生额外的洞察力。

答案 1 :(得分:2)

以下是一些“处理”代码,它们实现了您在上面发布的相同算法,并且其阻尼是正确的,我希望您能从中获得一些观点:

// codes begin

int Width = 800;
int Height = 600;
int FullSize = 0;
//int Spacing = 10;

int[] source, dest;
PImage bg;

void setup()
{
  // if you want to run these codes by "Processing"
  // please make a picture named "HelloWorld.png"
  bg = loadImage("HelloWorld.png");
  Width = bg.width;
  Height = bg.height;
  FullSize = Width * Height;
  size(Width, Height);
  source = new int[FullSize];
  dest = new int[FullSize];
  for (int i=0; i< FullSize; i++)
    source[i] = dest[i] = 0;
}

void draw()
{
  for (int i=Width; i< FullSize-Width; i++)
  {
    // check for bounds
    int xi = i % Width;
    if ((xi==0) || (xi==Width-1)) continue;

    dest[i] = (
    ((source[i-1]+
      source[i+1]+
      source[i-Width]+
      source[i+Width]) >>1) ) -dest[i];

    int dampFactor = 1000;
    dest[i] -= (dest[i] >> dampFactor); // Damping - Quick divde by 32 (5 bits)
  }

  //image(bg, 0, 0);
  loadPixels();
  for (int i=Width; i< FullSize-Width; i++)
  {
    // check for bounds
    int xi = i % Width;
    if ((xi==0) || (xi==Width-1)) continue;

    int xoffset = dest[i-1] - dest[i+1];
    int yoffset = dest[i-Width] - dest[i+Width];

    int offset = i+xoffset+yoffset*Width;
    if (offset>0 && offset<FullSize)
    {
      // TODO: make better map
      pixels[i] = bg.pixels[offset];
    }
  }
  //bg.updatePixels();
  updatePixels();

  //swap
  int[] temp = source;
  source = dest;
  dest = temp;
}

void mouseDragged() 
{
    if (mouseX > 0 && mouseX < Width && mouseY > 0 && mouseY < Height)
      source[mouseY*Width+mouseX] = (int)random(50, 100); 
}

void mousePressed()
{
   // TODO: make a area pulse value, like a radius circle
   if (mouseX > 0 && mouseX < Width && mouseY > 0 && mouseY < Height)
      source[mouseY*Width+mouseX] = (int)random(50, 100); 
}

// codes end