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

Author Topic: class sf::Shader doesn't load Vertex & Fragment Shader  (Read 1553 times)

0 Members and 1 Guest are viewing this topic.

krikri

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
class sf::Shader doesn't load Vertex & Fragment Shader
« on: April 10, 2012, 12:58:38 pm »
 Hello!
My problem starts when I use this way to load a shader:
sf::Shader miShader;
miShader.loadFromFile("shader/bumpmap.vert","shader/bumpmap.frag");
 

Vertex Shader:
void main(){
        gl_TexCoord[0] = gl_MultiTexCoord0;
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
 
Fragment Shader:
// 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;  
}
 
If i only use the fragment shader, no problem, but if i use both then only paints a rectangle of a color similar to the diffuse map.
« Last Edit: April 10, 2012, 01:20:12 pm by krikri »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: class sf::Shader doesn't load Vertex & Fragment Shader
« Reply #1 on: April 10, 2012, 01:29:09 pm »
You must multiply the texture coordinates by the texture matrix.

Look at the shaders of the official Shader example. That's why these examples exist, to serve as a working reference.
Laurent Gomila - SFML developer

krikri

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: class sf::Shader doesn't load Vertex & Fragment Shader
« Reply #2 on: April 10, 2012, 01:45:10 pm »
Oh my goodness!
I change the code of the Vertex Shader to:
void main(){
       
        gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
 
and.... It works!
I think I was using a deprecated way of calculating the gl_TexCoord [ 0], which strangely worked well in Shader Designer.

Thanks!
« Last Edit: April 10, 2012, 01:51:31 pm by krikri »