1
\$\begingroup\$

I am making some OpenGL program, and I have a lost of cubes next to each other like this:

enter image description here

When I am making this hole, I am just skipping those cubes, just don't draw them.

for (int i=0;i<NUM_OF_CUBES;i++) {
        for (int j = 0; j < NUM_OF_CUBES; j++) {
                if ((i==3 && j==3) || (i==4 && j==3) || 
                    (i==3 && j==4) || (i==4 && j==4))
                        continue;
                cubes[i][j].draw(true);
        }
}

The result is: enter image description here

But when I make those cubes invisible, by setting alpha to 0, the result is:

enter image description here

The code is:

for (int i=0;i<NUM_OF_CUBES;i++) {
        for (int j = 0; j < NUM_OF_CUBES; j++) {
                if ((i==3 && j==3) || (i==4 && j==3) || 
                    (i==3 && j==4) || (i==4 && j==4))
                        cubes[i][j].draw(true);
                else
                        cubes[i][j].draw(false);
        }
}

true/false fleg which I send to draw function just sets alpha to 0 or 1, if true alpha coordinate of glColor4f is set to 0, and if false is sent alpha coordinate is set to 1.

Does anyone have idea why it looks like some sides of the other cubes are cut? Like they don't exist.

\$\endgroup\$
3
\$\begingroup\$

It has to do with draw order. Not drawing something and drawing it as transparent/translucent are not the same thing. The transparent/translucent cubes are actually in front of the lower cubes so they get culled out even though they should be still visible.

Generally all transparent/translucent objects are drawn after all opaque objects to help mitigate the issue. There are ways to draw "order-independent transparency" but they get complicated quickly.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.