[英]SFML 2.0 GLSL Volumetric Light Scattering Shader
使用Kenny Mitchell在GPU Gems 3中提供的GLSL着色器源代碼,我嘗試使用SFML 2.0創建一些2D神光線。 目前,每當我編譯和調試項目時,掩碼紋理和精靈(分別為“image.png”和“精靈”)都會完全消失。 我目前設置的項目非常粗糙,只包含一個main.cpp文件和着色器文件,但我覺得我需要的比現在更多。 任何幫助將非常感激! 源代碼將在下面提供。
main.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);
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.