- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
但我不想使用两个 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;
}
请记住,这只是关于高度场的创建。还没有水的阴影。
你知道为什么波会在没有阻尼的情况下自行消失吗?
最佳答案
如果我正确地阅读了代码,您将对前一帧的纹理颜色/高度进行采样,并使用四个相邻的像素/纹素来确定当前像素的颜色/高度。
当您计算(缩放)这些邻居时,您可能会错过包含您正在寻找的颜色/高度的纹素。它可能不是最高的纹理元素,只是旁边的一个稍微低一点,导致意外的阻尼。
这是您不只使用加法和减法的地方:
const float pixelSize = 1.0 / textureSize;
通过使用这个值,您可能会错过您正在寻找的纹素。
编辑
此外:您正在对样本进行平均,因此结果将始终小于样本的最大值。因此,您可以选择最大值而不是取平均值。这可能会产生奇怪的结果,但也会产生额外的洞察力。
关于c++ - SFML 中的二维水着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14579610/
我有一个包含纹理的文件。我加载到 sf::Texture并用 setTextureRect 拆分成 Sprite . 现在假设一个 Sprite 包含 20 像素宽的纹理的一部分。如何渲染它以适应例如
一段时间以来,我一直在使用 SDL 来制作小东西的原型(prototype)并了解有关图形编程的更多信息。 然后我看到了SFML并想试一试,发现它更面向对象并修复了我认为 SDL 存在的一些问题。 但
我正在使用 rust-sfml ( rsfml::graphics ) 目前,将像素绘制到屏幕上。 (我只是从 Rust 和项目开始。)我将数据存储在 Image 中。 , 然后复制到 Texture
我试图在 SFML 2 中获取关键输入,在 SFML 1.6 im 中使用 while (App.GetEvent(Event)) { if (App.GetI
我做了一个非常简单的双人游戏,一个是水母,另一个是SFML中的鲨鱼。唯一的问题是一次只能有一个 Sprite 移动。这可能修复吗?我该如何修复? 编辑:我的尝试是这样的: if (event.type
我已经尝试了几个小时,但似乎无法做到我已经下载了扩展程序并寻求帮助,但此时一切都让我感到困惑。 我想在我的项目中包含 SFML 库,我正在尝试使用 Visual Studio Code 编辑器,但由于
下面链接中的图片包含 20 多张图片(每个 body 部位和盔甲类型都在单独的文件中)。这个角色在名为“Spriter”的软件中被动画化。该代码采用 spriter 的输出文件 (.scml),然后计
我的第一个问题是 C++ 是否: pTexture.getSize().x 等于 C#(因为这对我来说没有结果): pTexture.Size().X 第二个问题是关于C++代码的(如何在SFML.n
我正在尝试在 Windows 10 下用 sfml 构建游戏。它在构建时显示成功。每当我调试解决方案时,它都会显示如下错误消息: Unhandled exception at 0x00007FFA62
我开始使用 SFML 和 C++ 创建游戏。但是,我遇到了一个问题。我有基本的骨架类代码,但由于某种原因,当我创建一个窗口(sf::Window)时,我立即崩溃了!此代码不包含任何事件检查代码,但它包
我的项目中有 2 个 CMakeLists.txt 文件。 CMakeLists.txt cmake_minimum_required(VERSION 3.2) project(Game) set(C
所以我正在尝试编写一些与网络相关的代码,特别是使用 SFML 的端口扫描器。我正在运行 VS 2017 并下载了 2015 年的 SFML 版本,但是人们说这应该适用于 2017 年。 到目前为止,我
我刚刚开始使用 C++ 和 SFML,最终一切都运行良好。在出现 undefined reference 错误之前,我意识到我一直在下载错误类型的 SFML,得到的是 SJLJ 而不是 DW2。问题已
尝试将新的 C++ 项目与 SFML 链接,我执行了以下操作: 创建了一个空项目。 C/C++ >> 常规:附加包含目录:添加了 SFML 包含目录并从项目默认值继承。 链接器 >> 附加库目录:添加
当我运行代码时,除了不播放声音外,一切正常。链接很好,当我放置断点并查看声音对象时,它显示 .我在正确的位置添加了 sfml-audio-d.lib 和 sfml-audio.lib。欢迎提出任何建议
下面的代码给出了未捕获的异常(特别是 txtr.loadFromFile("C:/Users/kidz/Documents/Visual Studio 2012/Projects/SFML/Debug
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
我想知道是否可以使用 SFML 库在 C 中使用颜色设置碰撞?因为我使用的是带有图 block 的 map ,我不知道它是否可以一起工作。 谢谢! 最佳答案 据我所知,您使用具有两种颜色的图像来定义碰
我正在制作 Frogger 的 C++ SFML 版本,目前正在尝试实现 Frog 和其他物体(汽车、卡车、原木等)之间的碰撞检测。我为 Frog 和游戏对象有单独的类,并希望使用边界框来检测交叉点,
嗨,大家好 为了缩放我的 sfml 应用程序,我正在尝试解决这个问题,该程序最初使用 运行(640, 480) 方面 显示这样的窗口... 我的目标是以全屏模式执行程序并放置旧的 '窗口' (检查图像
我是一名优秀的程序员,十分优秀!