gpt4 book ai didi

c++ - SFML 2.0 GLSL 体积光散射着色器

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:55:33 24 4
gpt4 key购买 nike

使用 Kenny Mitchell 在 GPU Gems 3 中提供的 GLSL 着色器源代码,我尝试使用 SFML 2.0 创建一些 2D 神光。目前,每当我编译和调试项目时, mask 纹理和 Sprite (分别为“image.png”和“ Sprite ”)完全消失。我目前设置的项目非常粗糙,仅包含一个 main.cpp 文件和着色器文件,但我感觉我需要的比现在更多。任何帮助将非常感激!下面将提供源代码。

主要.cpp:

#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <iostream>

void main()
{
sf::RenderWindow _window(sf::VideoMode(800, 480, 32), "Lighting Test");
_window.setFramerateLimit(60);

sf::Shader lightingShader;
sf::RenderStates renderState;

sf::Texture texture;
texture.loadFromFile("image.png");

sf::Sprite sprite;
sprite.setTexture(texture);

sf::Texture backgroundTexture;
backgroundTexture.loadFromFile("light.png");

sf::Sprite background;
background.setTexture(backgroundTexture);

while (_window.isOpen())
{
int x = sf::Mouse::getPosition(_window).x;
int y = sf::Mouse::getPosition(_window).y;

lightingShader.loadFromFile("lightingShader.vert", "lightingShader.frag");
lightingShader.setParameter("exposure", 0.25f);
lightingShader.setParameter("decay", 0.97f);
lightingShader.setParameter("density", 0.97f);
lightingShader.setParameter("weight", 0.5f);
lightingShader.setParameter("lightPositionOnScreen", sf::Vector2f(0.5f, 0.5f));
lightingShader.setParameter("myTexture", sf::Shader::CurrentTexture);
renderState.shader = &lightingShader;

_window.clear(sf::Color::Black);
sprite.setPosition(x, y);
//sprite.setColor(sf::Color::Black);
//background.setPosition(400, 240);
_window.draw(background);
_window.draw(sprite, renderState);
_window.display();
}
}

lightingShader.vert:

void main() 
{

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}

lightingShader.frag:

uniform float exposure;
uniform float decay;
uniform float density;
uniform float weight;
uniform vec2 lightPositionOnScreen;
uniform sampler2D myTexture;
const int NUM_SAMPLES = 100 ;
void main()
{
vec2 deltaTextCoord = vec2( gl_TexCoord[0].st - lightPositionOnScreen.xy );
vec2 textCoord = gl_TexCoord[0].st;
deltaTextCoord *= 1.0 / float(NUM_SAMPLES) * density;
float illuminationDecay = 1.0;


for(int i=0; i < NUM_SAMPLES ; i++)
{
textCoord -= deltaTextCoord;
vec4 sample = texture2D(myTexture, textCoord);

sample *= illuminationDecay * weight;

gl_FragColor += sample;

illuminationDecay *= decay;
}


gl_FragColor *= exposure;
}

最佳答案

我终于自己解决了这个问题。原来我创建的顶点着色器是不必要的,我所要做的就是替换:

lightingShader.loadFromFile("lightingShader.vert", "lightingShader.frag");

与:

lightingShader.loadFromFile("lightingShader.frag", sf::Shader::Type::Fragment);.

关于c++ - SFML 2.0 GLSL 体积光散射着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13489703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com