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

Author Topic: [SFML2] Using Shaders  (Read 4733 times)

0 Members and 4 Guests are viewing this topic.

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[SFML2] Using Shaders
« on: January 29, 2011, 04:20:57 pm »
How would I create a shader instance from a file / from a string in SFML.NET 2.0?

I've tried:
Code: [Select]
TestShader = new Shader( "Shader.sfx" );
But I get a LoadingFailedException. The file definitely exists at the specified location.

Shader.sfx is just an example shader:
Code: [Select]
uniform sampler2D tex;

void main()
{
   gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);
}


Loading from a string would be more suitable for my needs, but the LoadFromString() method requires an instance of Shader. I would have expected it to be a static method.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SFML2] Using Shaders
« Reply #1 on: January 29, 2011, 05:37:11 pm »
Quote
But I get a LoadingFailedException. The file definitely exists at the specified location.

Are you sure that your working directory is the one you think it is? When you run the application from the IDE it's often the project directory, not the executable one.

Quote
Loading from a string would be more suitable for my needs, but the LoadFromString() method requires an instance of Shader. I would have expected it to be a static method.

You're right, this was a mistake: as Shader doesn't have a default constructor, this function is not usable. I renamed LoadFromString to FromString and made it static :)
Laurent Gomila - SFML developer

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[SFML2] Using Shaders
« Reply #2 on: January 29, 2011, 05:54:29 pm »
Thank you for the quick reply!

The file is in the bin\Debug and bin\Release folders with the executable, and is copied there from the project directory on compile (therefore it is in both).

Does the previously mentioned exception occur if the file is found but the contents are bad?

And thanks for changing FromString to be static, I will probably use that instead of from a file.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SFML2] Using Shaders
« Reply #3 on: January 29, 2011, 06:14:12 pm »
Quote
Does the previously mentioned exception occur if the file is found but the contents are bad?

Yes. This is the only exception that is thrown, therefore it gathers all kind of errors that may happen.
Laurent Gomila - SFML developer

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[SFML2] Using Shaders
« Reply #4 on: January 29, 2011, 06:35:46 pm »
It may have just been something wrong with the contents then.

Anyway, I got a Shader working through the FromString method. Thanks for your help, and thanks for SFML! :D

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[SFML2] Using Shaders
« Reply #5 on: January 30, 2011, 03:04:05 pm »
Another question related to shaders, how would I set the value of an item within an array in the shader?

For example, if I have:
Code: [Select]
uniform vec2 points[16];

How would I change values in "points" with SFML?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SFML2] Using Shaders
« Reply #6 on: January 30, 2011, 05:58:24 pm »
I don't know, you have to check this in a GLSL tutorial. Maybe with "points
  • " (with x a number) as the key?
Laurent Gomila - SFML developer