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

Author Topic: [resolved]Mixing Textures in Shader  (Read 1235 times)

0 Members and 1 Guest are viewing this topic.

JoseMan

  • Newbie
  • *
  • Posts: 8
    • View Profile
[resolved]Mixing Textures in Shader
« 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
« Last Edit: April 14, 2016, 11:54:36 am by JoseMan »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Mixing Textures in Shader
« Reply #1 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.
Laurent Gomila - SFML developer

JoseMan

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Mixing Textures in Shader
« Reply #2 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.
« Last Edit: April 14, 2016, 11:06:11 am by JoseMan »

JoseMan

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Mixing Textures in Shader
« Reply #3 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...
« Last Edit: April 14, 2016, 11:55:41 am by JoseMan »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
AW: [resolved]Mixing Textures in Shader
« Reply #4 on: April 14, 2016, 12:01:55 pm »
What's the solution?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything