0

This image is rendered using three passes.

  1. In the first pass, I render a three axis.
  2. in the second pass a transparent cylinder is rendered (glEnable(GL_BLEND)) alpha = 0.5f.
  3. finally the golden and grey spheres are rendered in the third pass(glEnable(GL_BLEND)).

The alpha value of the golden spheres = 1.0f and the grey sphere = 0.2f.

enter image description here

The problem: As you can see,

  1. the cylinder overlaps the spheres even though we enable blending.
  2. the axes overlap the cylinder and the spheres!

Here is my code:

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClearDepthf(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CCW);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

here the data is prepared and sent to shaders (first pass)

    glDrawElements(GL_POINTS, 256, GL_UNSIGNED_INT, reinterpret_cast<void*>(0));

ps: a geometry shader is used to render lines from the given points.

Then we prepare and pass the cylinder data

    glEnable(GL_BLEND);
    glCullFace(GL_FRONT);
    glDrawElements(GL_POINTS, 256, GL_UNSIGNED_INT, reinterpret_cast<void*>(0));
    glCullFace(GL_BACK);
    glDrawElements(GL_POINTS, 256, GL_UNSIGNED_INT, reinterpret_cast<void*>(0));
    glDisable( GL_BLEND);

ps: a geometry shader, also, is used to render the mesh of the cylinders from the given points.

Finally, I render the golden sphere and the grey sphere in one pass

    glEnable(GL_BLEND);
    glDrawElements(GL_LINE_STRIP, goldenSphereNumber, GL_UNSIGNED_INT, (void*)0);
    glDrawElements(GL_LINE_STRIP, sphereIndexCount, GL_UNSIGNED_INT, (void*)0);
    glDisable( GL_BLEND);

ps: here also a geometry shader is used to render the mesh of the cylinders from the given lines.

Do you see any wrong? Could you help, please?

A. Student
  • 57
  • 1
  • 6
  • 4
    I don't really get what you think your problem is. You render spheres after the cylinder, thenre is no way you'll see back spheres through cylinder - it's already rendered at that moment. Blending blends resulting colour with framebuffer based on resulting alpha value; after that, your 'transparency' is gone, you can't add something behind what is already drawn (or at least not directly via blending). The part about axes is even more obscure, what result did you expect? – keltar Apr 27 '18 at 12:23
  • "The part about axes is even more obscure, what result did you expect?" I expect to not see the axis! They should be covered by the spheres. Sorry, I've forgotten to say that the axes are centered in the middle of the cylinder. – A. Student Apr 27 '18 at 13:22
  • "I don't really get what you think your problem is." I'm wondering why I can see the cylinder that right behind the focus cylinder while I can not see the spheres behind it. – A. Student Apr 27 '18 at 13:30
  • Both points have the same reason then. That's how blending works. That's why you have to draw cylinder's back faces before its front faces (try to reverse that - you'll no longer see back surface through front one). Blending reads 'current' framebuffer value, and uses blend function to produce 'blended' result, then writes back this result to framebuffer, making it new 'current'. Next primitive will read this updated value and blend its fragment with it, etc.. Alpha is discarded after each blend, framebuffer only have colour. Blending can't add something behind already drawn primitives. – keltar Apr 27 '18 at 13:58
  • 1
    The common way to render somthing like this is: 1. render the opaque objects with depth test enabled (read/write). 2. render the transparent objects in sorted order from the back to front, with enabled blending and read only depth test (disbled depth mask). – Rabbid76 Apr 27 '18 at 14:11
  • @Rabbid76 Thank you for the idea. I'll give try and let you know the result. – A. Student Apr 27 '18 at 16:02
  • see [OpenGL - How to create Order Independent transparency?](https://stackoverflow.com/a/37783085/2521214) – Spektre Apr 29 '18 at 07:56

0 Answers0