Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML 2.0 Shader - Question  (Read 3877 times)

0 Members and 2 Guests are viewing this topic.

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
SFML 2.0 Shader - Question
« on: March 18, 2012, 07:19:04 pm »
Hello,

I have some problem with implementing shaders and showing these on the Application Window, I'm loading the Shader with this piece of Code:

Code: [Select]
// The Image Object
sf::Image img;

// Create an Image with the Size of the Window
img.Create(App.GetWidth() + 1, App.GetHeight() + 1);

// The Texture Object
sf::Texture tex;

// Load the Image
tex.LoadFromImage(img);

// The Sprite Object
sf::Sprite sprite;

// Set the Texture
sprite.SetTexture(tex);

// The Shader
sf::Shader sh;

// Load the Shader Files
sh.LoadFromFile("shader.vert", "shader.frag");

// Renderstates Object
sf::RenderStates r;

// Set active Shader
r.Shader = &sh;

   // ...
   App.Draw(sprite, r);


The Vertex Shader looks like this:

Code: [Select]
void main(void)
{
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}


And the Frament Shader looks like this:

Code: [Select]
uniform float time;
//uniform vec2 mouse;
uniform vec2 resolution;

void main(void)
{
const float stars = 512.0;
const float depth = 512.0;
const float skew = 16.0;
float gradient = 0.0;
float speed = 128.0 * time; //negate to reverse

vec2 pos = (((gl_FragCoord.xy / resolution.xy)) - 0.5) * skew;
vec2 pos_center = pos - vec2(0.5);

for (float i = 1.0; i < stars; i++)
{
//i * i will get a typical starfield effect
float x = sin(i) * 256.0;
float y = cos(i) * 256.0;
float z = mod(i - speed, depth);

vec2 blob_coord = vec2(x, y) / z;
float fade = (depth - z) / 512.0;
gradient += ((fade / depth) / pow(length(pos_center - blob_coord), 1.8));
}
float r = abs(sin(time * 1.13));
float g = abs(sin(time * 2.23));
float b = abs(sin(time * 3.33));

gl_FragColor = vec4(gradient * r, gradient * g, gradient * b, 1.0);
}


I tooked the Frament Shader Code from this Site:
http://glsl.heroku.com/e#1885.0

First I thought the Problem is that the Code is for GLSL ES but it seems like the Vertex and Fragment Shader are linked and installed on the GPU sucessfully... and the Problem is that I don't see anything on the Application Window... only a Blackscreen...

I really read all references about GLSL that I found on google, but it seems like that this references all shows the same Shaders as Example that are very complicated and more specially for 3D Stuff and not suitable for beginners.

So I looked for a Site with shaders and found this live Shader Sandbox Site, tooked the Shader and dissassembled the code to understand whats going on, and it works on this Browser Sandbox, but in the SFML Window the Blackscreen stoped me...

I hope you can help me :)
Thanks in advance!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML 2.0 Shader - Question
« Reply #1 on: March 18, 2012, 07:40:10 pm »
I don't see in your code where you send the "resolution" and "time" parameters to your shader.
Laurent Gomila - SFML developer

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
SFML 2.0 Shader - Question
« Reply #2 on: March 18, 2012, 07:49:45 pm »
Laurent, sometimes I ask myself why there is an Switch behind my brain and a Label "off" next to it... and what it means... :?

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
SFML 2.0 Shader - Question
« Reply #3 on: March 18, 2012, 09:36:02 pm »
Omg... how I forgot to say "Thanks"?
Thank you for the help!!  :D

 

anything