0

I'm implementing a renderer where the shading requires front-to-back rendering. I'm having issues figuring out how to initialize the blending function.

Here's what I tried.

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); 
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

This results in a black screen.

The blog post says to use GL_ONE_MINUS_DST_ALPHA, GL_ONE and initialize the background to all black all translucent, which is what I think I'm doing. The post cites an nvidia whitepaper so I looked into that as well. I looked through the code there and they seem to do somewhat the same as me. However something is clearly wrong as it isn't working. If I don't use blending or use other blending functions things seem to work.

EDIT:

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutCreateWindow("Window");
Jens
  • 113
  • 1
  • 10
  • 1
    Did you request your framebuffer to have an alpha component, and check that you actually got it? – Reto Koradi Oct 31 '15 at 01:42
  • Hey, i havent done any more initialization than i put in the code there. I just render directly to the default framebuffer – Jens Oct 31 '15 at 01:48
  • 1
    What framework/toolkit are you using to set up your context and window? The format of the default framebuffer is specified during this initial setup. The details are highly platform/toolkit/framework dependent. – Reto Koradi Oct 31 '15 at 01:55
  • i'm using freeglut to create the window and context – Jens Oct 31 '15 at 01:56
  • 1
    Can you add the initialization code to the post, particularly the `glutInitDisplayMode()` call? – Reto Koradi Oct 31 '15 at 02:00
  • @RetoKoradi Added them now – Jens Oct 31 '15 at 02:03

1 Answers1

1

You need to specifically request alpha planes during your GLUT initialization if you use blending that involves destination alpha. Change the call to:

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA);

You might think that specifying GL_RGBA would be sufficient. At least I did, until I just checked the documentation, which says:

Note that GLUT_RGBA selects the RGBA color model, but it does not request any bits of alpha (sometimes called an alpha buffer or destination alpha) be allocated. To request alpha, specify GLUT_ALPHA.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • Thanks for the response. I'm using glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA); now, but unfortunately the screen is still back when I use glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE) instead of glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). – Jens Oct 31 '15 at 02:17
  • Ops, clearcolor was 0,0,0,1 when i tried. It's working. – Jens Oct 31 '15 at 11:22