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

Author Topic: How to perform something like Shader::unbind (removed in recent versions)  (Read 7559 times)

0 Members and 2 Guests are viewing this topic.

sreiter

  • Newbie
  • *
  • Posts: 5
    • View Profile
Hi SFML-users,

this is my first post to this forum - hello to everyone.
I'm using SFML since approx. 1 year. Due to problems with ATI cards etc. I switched to SFML2.0 a while back and really like it. I'm doing my own rendering through OpenGL. Still I'm using some sfml classes, e.g. for text rendering and shading. Especially sf::Shader proved to be very useful.

Now here's the problem: sf::Shader used to feature a method Shader::unbind. As I have read, this method has been removed for some reasons in recent versions of sfml. However, when only applying shaders to some parts of a rendered scene, there has to be some means of unbinding the active shader.

Simply using an empty shader and calling bind() on it doesn't work, since bind only executes code if a program has been compiled (at least that's how I read the code).

Is there some other way to unbind the active shader? Since SFML already does all the shading related stuff it would be great if I wouldn't have to directly include and link against glew etc.

Thanks for any advise,
Sebastian

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
You must call
glUseProgram(0);
Laurent Gomila - SFML developer

sreiter

  • Newbie
  • *
  • Posts: 5
    • View Profile
Thank you very much for your fast reply!
First - I want to say thank you for providing such a great library and great support!

glUseProgram seems like a good option, however, I think it's not contained in the default OpenGL distributions for MS Windows - unless one uses extensions. SFML features everything I need beyond basic OpenGL stuff, so it would have been great if I could have circumvented those extensions... which probably isn't a good idea anyways.
I'm sure you had good reasons to remove Shader::unbind - however, it renders shader support of SFML to be somehow a little incomplete (in my opinion and to my knowledge) - which is a pitty, since sf::Shader is a great class.

Again - thank you very much for your reply!

Kind regards,
Sebastian

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
I can understand your point of view. But Shader::unbind() didn't make sense, since it didn't match the way glUseProgram works. With OpenGL you don't unbind a shader, you bind another one (including a null one).
Laurent Gomila - SFML developer

sreiter

  • Newbie
  • *
  • Posts: 5
    • View Profile
Thanks again for your reply,

Quote
With OpenGL you don't unbind a shader, you bind another one (including a null one).

Wouldn't it then be an option to "unbind" a shader when Shader::bind is called on an empty shader? Currently, as far as I understand the code, nothing is done if bind is called on an uninitialized shader.

My concrete suggestion (if you don't mind) is to alter Shader::bind() in a way so that
ensureGlContext();
 
and
glUseProgramObjectARB(...)
 
are either executed before the if-condition is evaluated or in an else branch:

void Shader::bind() const
{
    ensureGlContext();
    // Enable the program
    glCheck(glUseProgramObjectARB(m_shaderProgram));
 
    if (m_shaderProgram)
    {
        // Bind the textures
        bindTextures();
        // Bind the current texture
        if (m_currentTexture != -1)
            glCheck(glUniform1iARB(m_currentTexture, 0));
    }
}
 

This behavior would then be coherent with glUseProgram - at least in my understanding. And it would be of great use for people who use sf::Shader in the way I do it (probably a very small minority though...)

Kind regards,
Sebastian

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
I don't think it's a clean and intuitive API. Using an empty object feels wrong, and using an object to unbind another one feels wrong too.

If it was added back to the sf::Shader class, it would probably be a static function.

shader.bind();
// ...
sf::Shader::unbind();

Or

sf::Shader::bind(&shader);
// ...
sf::Shader::bind(NULL);
Laurent Gomila - SFML developer

sreiter

  • Newbie
  • *
  • Posts: 5
    • View Profile
It would be great if you would consider adding something like those static methods!

Thanks for listening to my issues!
Kind regards,
Sebastian

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
I implemented the second variant.
Laurent Gomila - SFML developer

sreiter

  • Newbie
  • *
  • Posts: 5
    • View Profile
Thanks so much!!!