Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: transparency does not work

  1. #1
    Intern Contributor
    Join Date
    Aug 2016
    Posts
    83

    transparency does not work

    i tried to add transparency to my ogl project. For now i just made it hardcoded in the fragmentshader. Later i'll get the value via pipeline. But first that will do. Or rather it does not do. It's still solid painted. Is there anything else i need to change?? All i did was changing this

    Code :
    gl_FragColor = texture2D(texture, v_texcoord);

    to

    Code :
    vec4 finalColor = vec4(texture2D(texture, v_texcoord));
    finalColor.a = 0.3;
    gl_FragColor = finalColor;


    Code :
    #ifdef GL_ES
    // Set default precision to medium
    precision mediump int;
    precision mediump float;
    #endif
     
    uniform sampler2D texture;
     
    varying vec2 v_texcoord;
     
    void main()
    {
    	// Set fragment color from texture
    	vec4 finalColor = vec4(texture2D(texture, v_texcoord));
    	finalColor.a = 0.3;
    	gl_FragColor = finalColor;
    }

  2. #2
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    401
    transparency does not happen by magic, you have to do it yourself
    most common approach is this:

    --> enable blending: glEnable(GL_BLEND);
    that means the color drawn into the framebuffer by your fragmentshader will be "somehow" mixed with the color that is already written into the framebuffer at that position

    most common approach is this:
    --> discard the alpha value that is already written into the framebuffer
    --> control "somehow" the transparency with the alpha value your fragmentshader writes into the framebuffer

    "somehow" means setting the math operation to:
    --> glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    that means these "weight factors":
    k(src) = alpha
    k(dest) = 1 - alpha

    final framebuffer color = k(src) * color(src) + k(dest) * color(dest)
    color(dest) = old framebuffer color
    color(scr) and alpha = fragmentshader output

    to achieve a acceptable result, you have to do depth sorting, and render EACH TRANSPARENT FACE (!!!) according to the distance to the scene camera (from furthest to nearest), and before that you render all opaque (non-transparent) faces

    http://www.opengl-tutorial.org/inter...-transparency/
    https://www.khronos.org/opengl/wiki/...rd_translucent

  3. #3
    Intern Contributor
    Join Date
    Aug 2016
    Posts
    83
    well, it works, now. But how can i manage, that the transparent parts are painted AFTER the others??

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,037
    Quote Originally Posted by 3DPrgmer View Post
    well, it works, now. But how can i manage, that the transparent parts are painted AFTER the others??
    When you're view-frustum culling your scene, you bin objects into different bins based on whether they use transparency or not. Then in your draw pass, you can draw the non-transparent (opaque) objects first.

    That's one way.

  5. #5
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    401
    if you remember what i told you in the "light" topic, "Materials" can contain additional parameters, like transparency etc. if you load a model data from files, like ".obj" model files, you can determine what face will use what material, i personally use std::map<Material, ...> to map all faces to the corresponding material, like this:

    std::map<Material, std::vector<Faces>> ...

    after all the model data is loaded, and before putting the vertices into your VBO, sorting all vertices is a good idea
    somehow like this:

    Code :
    std::vector<Vertex> modeldata_opaque;
    std::vector<Vertex> modeldata_transparent;
     
    // load model ...
     
    for each (vertex in modeldata)
    {
    if (material.transparency > 0.95f)
    --> put it into modeldata_opaque
    else
    --> put it into modeldata_transparent
    }
     
    merge modeldata_opaque and modeldata_transparent later, put everything into VBO

    keep track of the offset/count pair for the transparent and the opaque separately for each model
    each model can then be rendered in 2 draw calls

  6. #6
    Intern Contributor
    Join Date
    Aug 2016
    Posts
    83
    works just fine now. I have a vector with draw information (offset, size and materialindex) and when i fill that vector i check for the transparency and in that case i use pushback, otherwise pushfront. it's not the 100% solution, but it is ok for me.

    Just one question do you think this is a save code??
    Code :
    if (segmentIterator->textureIndex < m_materials->size() && m_materials->at(segmentIterator->textureIndex).transparent)
    	m_drawList.push_back(new_info);
    else
    	m_drawList.push_front(new_info);

    normally if the first part is wrong the 2nd shouldn't be touched. But i'm not sure

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •