user134304
user134304

Reputation:

OpenGL Alpha blending with wrong color

I am trying to create a simple ray tracer. I have a perspective view which shows the rays visibly for debugging purposes.

In my example screenshot below I have a single white sphere to be raytraced and a green sphere representing the eye.

Rays are drawn as lines with

glLineWidth(10.0f)

If a ray misses the sphere it is given color

glColor4ub(100,100,100,100);

in my initialization code I have the following:

    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER, 0.0f);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA,GL_SRC_ALPHA);

You can see in the screen shot that for some reason, the rays passing between the perspective view point and the sphere are being color blended with the axis line behind the sphere, rather than with the sphere itself.

Here is a screenshot:

screenshot

Can anyone explain what I am doing wrong here?

Thanks!!

Upvotes: 3

Views: 2366

Answers (3)

wrosecrans
wrosecrans

Reputation: 1085

AlphaTest is only for discarding fragments - not for blending them. Check the spec By using it, you are telling OpenGL that you want it to throw away the pixels instead of drawing them, so you won't can any transparent blending. The most common blending function is glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); You can also check out the OpenGL Transparency FAQ.

Upvotes: 2

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81868

  1. You dont need the glAlphaFunc, disable it.
  2. Light rays should be blended by adding to the buffer: glBlendFunc(GL_ONE, GL_ONE) (for premultiplied alpha, which you chose.
  3. Turn off depth buffer writing (not testing) when rendering the rays: glDepthMask(GL_FALSE)
  4. Render the rays last.

Upvotes: 2

EFraim
EFraim

Reputation: 13028

Is it a possibility you cast those rays before you draw the sphere?

Then if Z-buffer is enabled, the sphere's fragments simply won't be rendered, as those parts of rays are closer. When you are drawing something semi-transparent (using blending), you should watch the order you draw things carefully.

In fact I think you cannot use Z-buffer in any sensible way together with ray-tracing process. You'll have to track Z-order manually. While we are at it OpenGL might not be the best API to visualize ray-tracing process. (It will do so possibly much slower than pure software ray-tracer)

Upvotes: 8

Related Questions