简体   繁体   中英

Enable transparency in 3D - pyopengl

I have a GLViewWidget, where I add different objects (GLMeshItem) setting these properties, in order to enable transparency:

        opt = {
            GL_DEPTH_TEST: True,
            'glBlendFunc': (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA),
            }
        obj.setGLOptions(opt)

This is almost working, in the following sense: If I have two objects obj1 and obj2, and add them to the widget in this order, I will see obj2 in the back of obj1, but not the opposite.

I tried to add the objects in a different order, but clearly I will see obj1 behind obj2, and not the opposite. How can I make this to work for all the objects indifferently?

Depth testing and transparency don't work too well together.

Suppose we draw your translucent object obj1 . This will also write it's depth to the depth buffer. Now suppose we want draw your second object, obj2 , which has a greater depth. We would expect we would see obj2 through obj1 as obj1 is translucent.

However, as the depth of obj2 is greater, overlapping pixels will not be drawn as the depth test fails - so we will only see the parts of obj2 not overlapped by obj1 .

Depth testing does not take transparency into account. So in order to get it too work, as BDL said in the comments, you will have to manually sort the objects and draw them in the right order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM