Kenny Mitchell이 제공 한 GLSL 쉐이더 소스 코드를 GPU Gems 3에 사용하여 필자는 SFML 2.0을 사용하여 2D god rays를 생성하려고 시도했습니다. 현재 프로젝트를 컴파일하고 디버깅 할 때마다 마스크 텍스처와 스프라이트 (각각 "image.png"와 "sprite")가 완전히 사라집니다. 내가 현재 설정 한 프로젝트는 매우 원시적인데, 단지 하나의 main.cpp 파일과 쉐이더 파일로 구성되어 있습니다. 그러나 현재 가지고있는 것보다 더 많이 필요로한다고 생각합니다. 어떤 도움을 많이 주시면 감사하겠습니다! 아래에 소스 코드가 제공됩니다.SFML 2.0 GLSL 체적 광 산란 쉐이더
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;
}