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

Author Topic: sf::Shader + sf::RenderTexture = Malfunction  (Read 3847 times)

0 Members and 1 Guest are viewing this topic.

krikri

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
sf::Shader + sf::RenderTexture = Malfunction
« on: April 07, 2012, 08:52:36 pm »
Hello!
I'm trying to draw the resulting texture after applying a shader, into a new texture.
But the problem is that after doing this:
shaderTexture.create(1280,720);
 
the area where he painted the resulting texture now is just white.

My code is :
// Inclusion de OpenGL
#include <SFML/OpenGL.hpp>

#include <SFML/Graphics.hpp>

#define NORMAL_OTHER "shader/otro_normal.png"
#define TEXTURE_OTHER "shader/otro_texture.png"

int main() {
        sf::RenderWindow window(sf::VideoMode(1280, 720), "window!!!");
        sf::Shader miShader;
        miShader.loadFromFile("shader/bumpmap.frag",sf::Shader::Fragment);

        sf::RenderTexture shaderTexture;
        if(!shaderTexture.create(1280,720))
                return -1;

        glShadeModel (GL_SMOOTH);
       
        GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
        GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
        GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
        GLfloat mat_shininess[] = { 100.0 };
        GLfloat light_position[] = { 200.0, 200.0, 800.0, 0.0 };

        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);

        // Normal Map
        sf::Texture texturaOtra;
        if (!texturaOtra.loadFromFile(NORMAL_OTHER)) {
                return EXIT_FAILURE;
        }
        // Load a sprite to display
        sf::Texture otra;
        if (!otra.loadFromFile(TEXTURE_OTHER)) {
                return EXIT_FAILURE;
        }
        sf::Sprite spriteOtra(otra);
       
        spriteOtra.setPosition(150,150);
        sf::Vector2f p;
        p = spriteOtra.getPosition();
        sf::Vector3f p2 = sf::Vector3f(p.x,p.y,0.0);
        miShader.setParameter("position",p2);
        miShader.setParameter("normalTexture", texturaOtra);
        miShader.setParameter("texture", sf::Shader::CurrentTexture);

    // Start the game loop
        bool done = false;

        while (!done) {                
                // Process events
                sf::Event Event;
                while (window.pollEvent(Event)) {
            // Close window : exit
                        if (Event.type == sf::Event::Closed) {
                                done = true;
                        }
        }

        // Clear screen
                window.clear();
               
                shaderTexture.clear();         
                shaderTexture.draw(spriteOtra,&miShader);
                shaderTexture.display();
                window.draw(sf::Sprite(shaderTexture.getTexture()));
               
                window.display();

                }
        window.close();
    return EXIT_SUCCESS;
}
 

My system specifications are:
OS: Windows 7 Professional 64bits
Video Card: AMD Radeon HD 6670
IDE: Visual Studio 2008 Professional.
SFML version: 2.0 (downloaded from the official site in early March).

Sorry for my terrible English....

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #1 on: April 07, 2012, 09:13:28 pm »
There are a few things that you can do to narrow down the problem: try without your custom GL lighting calls, with a simple shader that just outputs the source color, etc.
Laurent Gomila - SFML developer

krikri

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #2 on: April 07, 2012, 10:35:05 pm »
The problem is simple, I can not apply the bump map. I tried shaders only change the color and working but not with the following fragment shader.
And the problem with the OpenGL light is solved by putting the following lines
sf::RenderTexture shaderTexture;
        if(!shaderTexture.create(1280,720))
                return -1;
 
below the shader creation.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #3 on: April 08, 2012, 08:39:09 am »
If it's your shader which is broken, you should show it ;)
Laurent Gomila - SFML developer

krikri

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #4 on: April 08, 2012, 09:35:20 pm »
the shader's code is:
// Textura y Bumpmap
uniform sampler2D normalTexture;
uniform sampler2D texture;
//variables internas
uniform vec3 position;

void main()
{
        // El color del pixel actual en el Normal Map
        vec4 normColor = texture2D(normalTexture, gl_TexCoord[0].st);
       
        // El color del pixel actual en la Textura
        vec4 texColor = texture2D(texture, gl_TexCoord[0].st );
       
        // El vector normal al Normal Map
        vec3 normalMapVector = 2.0 *(normColor.rgb - 0.5);
       
        // Calculamos el vector de la Luz
        vec3 light = normalize(gl_LightSource[0].position.xyz - position);
       
        // Calculamos el vector del Ojo
        vec3 E = normalize(-position); // we are in Eye Coordinates, so EyePos is (0,0,0)  
       
        // Calculamos el vector ??
        vec3 R = normalize(-reflect(light,normalMapVector));    
       
        // Calculamos el efecto de diffuse
    vec4 Idiff = gl_LightSource[0].diffuse * max(dot(normalMapVector,light), 0.0);  
    Idiff = clamp(Idiff, 0.0, 1.0);
   
    // Calculamos el efecto de ambient
    vec4 Iamb = gl_LightSource[0].ambient;
   
    // calculate Specular Term:
    vec4 Ispec = gl_LightSource[0].specular * pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
    Ispec = clamp(Ispec, 0.0, 1.0);
   
    //
        gl_FragColor = Idiff*texColor + Iamb + Ispec;  
}
 

PD: I find another issue with renderTexture.

krikri

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #5 on: April 09, 2012, 01:19:26 am »
My code is now like this:

// Inclusion de OpenGL
#include <SFML/OpenGL.hpp>

#include <SFML/Graphics.hpp>

#define NORMAL_OTHER "shader/otro_normal.png"
#define TEXTURE_OTHER "shader/otro_texture.png"

int main() {

        sf::RenderWindow window(sf::VideoMode(1280, 720), "window!!!");

        sf::Shader miShader;
        miShader.loadFromFile("shader/bumpmap.frag",sf::Shader::Fragment);

        glShadeModel (GL_SMOOTH);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
       
        GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
        GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
        GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
        GLfloat mat_shininess[] = { 100.0 };
        GLfloat light_position[] = { 200.0, 200.0, 800.0, 0.0 };

        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

       
        // Normal Map
        sf::Texture texturaOtra; 8)
        if (!texturaOtra.loadFromFile(NORMAL_OTHER)) {
                return EXIT_FAILURE;
        }
        // Load a sprite to display
        sf::Texture otra;
        if (!otra.loadFromFile(TEXTURE_OTHER)) {
                return EXIT_FAILURE;
        }
        sf::Sprite spriteOtra(otra);
       
        spriteOtra.setPosition(150,150);
        sf::Vector2f p;
        p = spriteOtra.getPosition();
        sf::Vector3f p2 = sf::Vector3f(p.x,p.y,0.0);
        miShader.setParameter("position",p2);
        miShader.setParameter("normalTexture", texturaOtra);
        miShader.setParameter("texture", sf::Shader::CurrentTexture);

        sf::RenderTexture shaderTexture;
        if(!shaderTexture.create(1280,720))
                return -1;

    // Start the game loop
        bool done = false;

        while (!done) {                
                // Process events
                sf::Event Event;
                while (window.pollEvent(Event)) {
            // Close window : exit
                        if (Event.type == sf::Event::Closed) {
                                done = true;
                        }
        }

        // Clear screen
                window.clear();
               
                shaderTexture.clear();          
                shaderTexture.draw(spriteOtra);
                shaderTexture.display();

                window.draw(sf::Sprite(shaderTexture.getTexture()),&miShader);
               
                window.display();

                }
        window.close();
    return EXIT_SUCCESS;
}
 

With this code, the lights and the shader works, but another problem arises: Bad News

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #6 on: April 09, 2012, 10:16:38 am »
Quote
With this code, the lights and the shader works, but another problem arises: Bad News
This is not a problem, have you read the comments in the issue?
Laurent Gomila - SFML developer

krikri

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #7 on: April 10, 2012, 12:42:52 pm »
This is not a problem, have you read the comments in the issue?
I read the comments and YES it is a problem for the shader that the image is upside down with a horizontally flip, modifying the normal way it works...


A patch solution to this abnormal behavior is to invest in the same sense the image of the bump map, which I think is inappropriate and impractical.


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #8 on: April 10, 2012, 12:59:40 pm »
Quote
I read the comments and YES it is a problem for the shader that the image is upside down with a horizontally flip, modifying the normal way it works...
Then it's a different issue than #116, which says that the texture is flipped when you forget to call display() -- which is not your case.

So what's your problem? What happens exactly?
Laurent Gomila - SFML developer

krikri

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #9 on: April 10, 2012, 01:16:02 pm »
Excuse me.. my case is if i don't call
    shaderTexture.display();
 
When I draw another sf:Sprite into the renderTexture, they  are drawing in sf::Window upside down.

If I make the call to the method, the issue with the Sprites.... disappears but  the relief generated by the shader is turned upside down and flip horizontal.


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #10 on: April 10, 2012, 01:27:25 pm »
I'm really sorry but you lost me.

Can we start again from zero, with clear, detailed explanations and code samples? :)
Laurent Gomila - SFML developer

krikri

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: sf::Shader + sf::RenderTexture = Malfunction
« Reply #11 on: April 10, 2012, 02:06:51 pm »
Of course  ;)

 

anything