IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

Weighted blended OIT.


Sujet :

Langage C++

  1. #1
    Invité
    Invité(e)
    Par défaut Weighted blended OIT.
    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 ...)

  2. #2
    Membre habitué Avatar de robinsondesbois
    Homme Profil pro
    Etudiant
    Inscrit en
    Avril 2012
    Messages
    171
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Etudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2012
    Messages : 171
    Points : 173
    Points
    173
    Par défaut
    Tu devrais penser à vérifier tes contextes OpenGL. En effet il est possible que la SFML charge tes image dans un contexte OpenGL différent de celui que tu utilise. (J'ai eu ce problème avec la version 1.6).
    Tu devrais aussi vérifier avec des shaders plus simple.
    Genre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    void main()
    {
        color = vec4(texture(sample2D, texCoords.xy).xyz, 0.5);
    }
    Cela affiche-t-il ta texture ?
    As tu bien activé les options de blending ?

  3. #3
    Invité
    Invité(e)
    Par défaut
    Re,
    avec un shader simple je n'ai pas de problèmes.

    Il doit y avoir un soucis avec cette formule :

    http://jcgt.org/published/0002/02/09/paper.pdf

    Car dans le rendu ils n'utilisent que des couleurs et pas des textures, de plus, il n'y a aucun code source à télécharger. :/

Discussions similaires

  1. Alpha blending
    Par Francky033 dans le forum DirectX
    Réponses: 4
    Dernier message: 31/05/2005, 00h23
  2. Réponses: 3
    Dernier message: 28/04/2005, 18h07
  3. Probleme avec le blending...
    Par ludobooz dans le forum OpenGL
    Réponses: 4
    Dernier message: 14/01/2005, 17h04
  4. Alpha blending
    Par Freakazoid dans le forum DirectX
    Réponses: 2
    Dernier message: 23/05/2002, 19h37
  5. Alpha blending et Z-buffer directx 8
    Par Cesar4 dans le forum DirectX
    Réponses: 1
    Dernier message: 23/05/2002, 12h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo