SFML community forums

Help => Graphics => Topic started by: JoseMan on April 14, 2016, 10:34:10 am

Title: [resolved]Mixing Textures in Shader
Post by: JoseMan on April 14, 2016, 10:34:10 am
Hi there,

I'm new here and maybe the problem that I discribe is allready resolved in another thread...

Okay, my problem:
I want to use 3 Textures in my shader and these textures I want to mix them but I don't know what I have to do to get this working.

Here is my code so far (c++):
void AClass::init(unsigned int wndWidth, unsigned int wndHeight)
{

        if (!mOffScreenTarget.create(wndWidth, wndHeight))
                return;
        mOffScreenTarget.setSmooth(true);

        mSchriftTex.loadFromFile("IntroGrafiken\\Logo.png");
        mSchriftSprite.setTexture(mSchriftTex);
        mRauschenSoftTex.loadFromFile("IntroGrafiken\\Rauschen.png");
        mRauschenSoftSprite.setTexture(mRauschenSoftTex);
        mRauschenHardTex.loadFromFile("IntroGrafiken\\20_rauschen.gif");
        mRauschenHardSprite.setTexture(mRauschenHardTex);

        mShader.loadFromFile("IntroShader\\IntroShader.vert","IntroShader\\IntroShader.frag");
       
        mShader.setParameter("schriftZugTextureSampler", mSchriftTex);
        mShader.setParameter("rauschSoftTextureSampler", mRauschenSoftTex);
        mShader.setParameter("rauscheHardTextureSampler", mRauschenHardTex);
}
 

and my draw-function:
        sf::RenderStates state = sf::RenderStates::Default;
        state.shader = &mShader;
        mOffScreenTarget.clear(sf::Color::White);
        mOffScreenTarget.draw(mSchriftSprite, state);
        mOffScreenTarget.draw(mRauschenHardSprite, state);
        mOffScreenTarget.draw(mRauschenSoftSprite, state);
        mOffScreenTarget.display();

        window.draw(sf::Sprite(mOffScreenTarget.getTexture()));

 
But this don't work. With this code all Sprites get drawn simple. But I want to mix them.
The shader code works I think...but I don't know what I have to do to get what I want!!!

Okay, I hope so you understand what I want and I hope my english isn't soo bad^^

Maybe you can post here some code...cause that says often more to me :)

Best regards
JoseMan
Title: Re: Mixing Textures in Shader
Post by: Laurent on April 14, 2016, 10:54:14 am
You'll have to explain in detail what "mix them" means, there are many ways to mix multiple sprites ;)

You can also show us your shaders, that could help to understand what you're trying to achieve.
Title: Re: Mixing Textures in Shader
Post by: JoseMan on April 14, 2016, 11:01:24 am
okay.
Here is my Vertex-Shader:
#version 330 core

void main()
{
    vec4 vertex = gl_ModelViewMatrix * gl_Vertex;
       
    gl_Position = gl_ProjectionMatrix * vertex;
    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
    gl_FrontColor = gl_Color;

}
 

And the fragment-shader:
#version 330 core

vec2 uvSchriftZugTexture;
vec2 uvRauschTexture;

// Ouput data
vec4 color;

uniform sampler2D schriftZugTextureSampler;
uniform sampler2D rauschSoftTextureSampler;
uniform sampler2D rauscheHardTextureSampler;

uniform float alpha;
vec4 colSchriftZugTexture;
vec4 colRauschSoft;
vec4 colRauschHard;

void main()
{
        uvSchriftZugTexture = gl_TexCoord[0];
        uvRauschTexture = gl_TexCoord[0];
        colSchriftZugTexture = texture2D( schriftZugTextureSampler, uvSchriftZugTexture).rgba;
        colRauschSoft = texture2D( rauschSoftTextureSampler, uvRauschTexture).rgba;
        colRauschHard = texture2D( rauscheHardTextureSampler, uvRauschTexture).rgba;
       
        color = mix(colSchriftZugTexture,colRauschSoft,0.55);
        color = mix(color,colRauschHard,0.30);
       
        gl_FragColor = color;
}
 

All I want to do is use the simple glsl-"mix"-function with 3 textures...but I don't know how to handle that with sfml.
Title: Re: Mixing Textures in Shader
Post by: JoseMan on April 14, 2016, 11:54:03 am
Okay, the problem is resolved.
Now I'm happy :)

The shader code was not right.
Now it works.

And I don't need to draw the sprites...

Thanks...
Title: AW: [resolved]Mixing Textures in Shader
Post by: eXpl0it3r on April 14, 2016, 12:01:55 pm
What's the solution?