1

i'm fairly new to OpenGL and came across this problem: I am trying to render multiple semi-transparent cubes which are inside of each other and some faces in the back of the cubes just get cut out. It also depends from where im looking inside the cube (see the gif).

https://i.imgur.com/bL4U8BS.gifv

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, transparentTexture);

    for (int i = 0; i < CacheSizeInfo.size(); ++i)
        {
            int totalCacheSize = CacheSizeInfo[i][0]* CacheSizeInfo[i][1]* CacheSizeInfo[i][2];
            std::vector<float> Cachecolor = { CacheColorInfo[i][0], CacheColorInfo[i][1], CacheColorInfo[i][2]};
            int cacheOffset = (renderIndex / totalCacheSize) * totalCacheSize;

            mat4 translationMatrix = glm::translate(mat4(1.0f), modelMatrices[cacheOffset]);
            glUniformMatrix4fv(glGetUniformLocation(shaderID, "translation"), 1, GL_FALSE, &translationMatrix[0][0]);
            glUniform3fv(glGetUniformLocation(shaderID, "color"), 1, &Cachecolor[0]);

            glBindVertexArray(CacheVAOInfo[i]);
            glDrawArrays(GL_TRIANGLES, 0, CacheVertexCountInfo[i]);
            glBindVertexArray(0);
        }
    }

    mat4 translationMatrix = glm::mat4(1.0f);
    glUniformMatrix4fv(glGetUniformLocation(shaderID, "translation"), 1, GL_FALSE, &translationMatrix[0][0]);
    glUniform3fv(glGetUniformLocation(shaderID, "color"), 1, &matColor[0]);

    glBindVertexArray(VAO_Matrix);
    glDrawArrays(GL_TRIANGLES,0, mainMatSize*3);
    glBindVertexArray(0);

First I enable the Depth Test and set it to GL_LESS. Then I bind the transparent texture. After that, in the for loop, I render the green matrix and after that, outside of the loop, the white one.

Kaaarl
  • 33
  • 3

1 Answers1

1

The Depth Test is enabled

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

The geometry is "cut out" because of the depth test. The faces in the back are not drawn, when the faces in the front have been draw before, because the depth test fails and the fragments are discarded.
Disable the depth test to make the faces in the back visible.

But note, Blending depends on the drawing order. You've to render the geometry from the back to the front, so the depth test (GL_LESS) becomes superfluous.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • So I have to sort them everytime before I render them, right? Or is there another workaround? – Kaaarl Jun 25 '19 at 13:08
  • @Kaaarl There are [Order-independent transparency](http://melinzweb:8080/btelite/bugs.aspx?) techniques, but they are not that easy to implement. e.g. [Adaptive Transparency](https://software.intel.com/en-us/articles/adaptive-transparency) – Rabbid76 Jun 25 '19 at 13:10
  • Okay, thanks. I'll look into it. – Kaaarl Jun 25 '19 at 13:12
  • @Kaaarl It there are just 2 layers and the alpha channel of both layers is 0.5, then the order doesn't matter, for e.g. `glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)` – Rabbid76 Jun 25 '19 at 13:16