aolabNeuro / brain-python-interface

Other
4 stars 2 forks source link

blending issue for opengl renderer #26

Closed leoscholl closed 3 years ago

leoscholl commented 3 years ago

There seems to be a bug somewhere that's causing blending not to work properly. In 3D it is not very noticeable: 3d_blending_issue

but with a 2d orthographic projection and phong reflection turned off, it's quite obvious: 2d_blending_issue

Can be "fixed" with the following hacky solution:

glBlendEquation(GL_MAX)
glBlendFunc(GL_ONE, GL_ONE)

I'm wondering if there are two targets on top of each other? Or something like that...

leoscholl commented 3 years ago

the hacky solution works for the most part but sometimes it causes its own graphical glitches (haven't caught them on camera) that look similar but vertical and only when the targets disappear. it also causes weird distortion around the target when the cursor intersects the edge of the target.

leoscholl commented 3 years ago

this has come back to bite us, we want to add transparency but the GL_MAX blend equation just sets alpha to 1 for everything.

one thing that seems to help and might point to a solution: making the size of the world much bigger, like 1000x1000cm, with 200cm targets, fixes the shading issues.

leoscholl commented 3 years ago

asked for help on r/opengl

The key is that this only happens when alpha is less than 1. That first image looks like the triangle loop going around the sphere is drawn in the wrong back-to-front order and the alpha blending doesn't work. Some of the triangles are drawn behind alpha blended triangles that were already added from the front. It's actually difficult to correctly draw a partially transparent sphere. What I normally do is draw the back first with front face culling, and then the front face with back face culling.

I don't know what's going on with your second no lighting image though. Maybe this is a different problem. Maybe in this case the left half is properly alpha blended, while the right half is. I'm not sure how you managed to get that result.

if i call

        glEnable(GL_CULL_FACE)
        glCullFace(GL_FRONT)

in screen_init only the back half of the spheres is drawn and the problem goes away in the 3D projection. but for the 2D projection it's the front half of the spheres that need to be drawn so glCullFace(GL_BACK) fixes the problem.

I don't think this is a proper solution, since now half the sphere isn't being rendered. But it is compatible with transparency!

Also it raises the question of why the 2D and 3D projections are drawing different sides of the sphere... i suppose they draw the back of the sphere in the 3D projection to get the x axis to be mirrored?

leoscholl commented 3 years ago

closing for now since the temporary fix seems to be working