Salut, j'essaye de mettre en place une technique de rendu des objets semi-transparents.

Cela marche bien avec des couleurs semi-transparente :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
const std::string aBufferGenFragShader =
                   "#version 130 \n"
                   "in mat4 projMat;\n"
                   "uniform sampler2D texture;\n"
                   "uniform float haveTexture;\n"
                   "uniform vec3 resolution;\n"
                   "void main() {"
                   "    vec2 position = ( gl_FragCoord.xy / resolution.xy );\n"
                   "    vec4 pixel = (haveTexture==1) ? gl_Color * texture2D(texture, gl_TexCoord[0].xy) : gl_Color;\n"
                   "    float z = (gl_FragCoord.w != 1.f) ? (inverse(projMat) * vec4(0, 0, 0, gl_FragCoord.w)).w : gl_FragCoord.z;\n"
                   "    float dz = ((gl_DepthRange.near - gl_DepthRange.far) * (z - gl_DepthRange.far)) / (gl_DepthRange.near - gl_DepthRange.far);"
                   "    float weight = pixel.a * max(pow(10, -2), 3 * pow(10, 3) * pow (1 - dz, 3));"
                   "    gl_FragColor = vec4(pixel.rgb * weight, pixel.a);"
                   "}";
                   const std::string aBufferGenFragShader2 =
                   "#version 130 \n"
                   "in mat4 projMat;\n"
                   "uniform sampler2D texture;\n"
                   "uniform float haveTexture;\n"
                   "uniform vec3 resolution;\n"
                   "void main() {"
                   "    vec4 pixel = (haveTexture==1) ? gl_Color * texture2D(texture, gl_TexCoord[0].xy) : gl_Color;\n"
                   "    float z = (gl_FragCoord.w != 1.f) ? (inverse(projMat) * vec4(0, 0, 0, gl_FragCoord.w)).w : gl_FragCoord.z;\n"
                   "    float dz = ((gl_DepthRange.near - gl_DepthRange.far)  * (z - gl_DepthRange.far)) / (gl_DepthRange.near - gl_DepthRange.far);"
                   "    float weight = pixel.a * max(pow(10, -2), 3 * pow(10, 3) * pow (1 - dz, 3));"
                   "    gl_FragColor.a = pixel.a * weight;"
                   "}";
                   const std::string frameBufferGenFragShader =
                   "#version 130 \n"
                   "uniform sampler2D rt0;\n"
                   "uniform sampler2D rt1;\n"
                   "uniform vec3 resolution;\n"
                   "void main() {\n"
                   "    vec2 position = ( gl_FragCoord.xy / resolution.xy );\n"
                   "    vec4 accum = texture2D(rt0, position);"
                   "    float reveal = accum.a;"
                   "    accum.a = texture2D(rt1, position).a;"
                   "    gl_FragColor = vec4(accum.rgb / clamp(accum.a, 1e-4, 5e4), reveal);"
                   "}\n";
Mais cela ne fonctionne pas avec des textures, je me retrouve avec des textures toutes blanche. :/

Voici comment j'effectue le blending :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
for (unsigned int i = 0; i < m_instances.size(); i++) {
                VertexArray va = m_instances[i]->getVertexArray();
                currentStates.texture = m_instances[i]->getMaterial().getTexture();
                currentStates.blendMode = sf::BlendAlpha;
                currentStates.shader = nullptr;
                frameBuffer.pushGLStates();
                glCheck(glEnable(GL_DEPTH_TEST));
                frameBuffer.draw(va, currentStates);
                frameBuffer.popGLStates();
                if (currentStates.texture == nullptr) {
                    aBufferGenerator.setParameter("haveTexture", 0);
                    aBufferGenerator2.setParameter("haveTexture", 0);
                } else {
                    aBufferGenerator.setParameter("haveTexture", 1);
                    aBufferGenerator2.setParameter("haveTexture", 1);
                }
                currentStates.blendMode = sf::BlendAdd;
                currentStates.shader = &aBufferGenerator;
                aBuffer.draw(va, currentStates);
                currentStates.blendMode = modeTwo;
                currentStates.shader = &aBufferGenerator2;
                aBuffer2.draw(va, currentStates);
            }
            sf::BlendMode mode(sf::BlendMode::Factor::OneMinusSrcAlpha, sf::BlendMode::Factor::SrcAlpha,sf::BlendMode::Equation::Add,sf::BlendMode::Factor::OneMinusSrcAlpha, sf::BlendMode::Factor::SrcAlpha, sf::BlendMode::Equation::Add);
            currentStates.shader = &frameBufferGenerator;
            currentStates.blendMode = mode;
            frameBuffer.draw(*clearTile, currentStates);
Merci d'avance pour votre aide. (Je trouve ça bizarre que ça fonctionne bien avec gl_Color mais pas avec des textures ...)