Thursday, April 28, 2011

Why does my colored cube not work with GL_BLEND?

My cube isn't rendering as expected when I use GL_BLEND.

glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

I'm also having a similar problem with drawing some semi-opaque vertices in front, which could well be related.

Related: Why do my semi-opaque vertices make background objects brighter in OpenGL?

Here's what it's supposed to look like:

Normal cube

And here's what it actually looks like:

Dark cube

Please see the code used to create the colored cube, and the code used to actually draw the cube.

The cube is being drawn like so:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glLoadIdentity();

// ... do some translation, rotation, etc ...

drawCube();

glPopMatrix();

// ... swap the buffers ...
From stackoverflow
  • It looks like you have lighting enabled on the second one,

    try with a glShadeModel( GL_FLAT ) before drawing,

    nbolton : Hmm, no, this makes the sides of the cube solid colours, but only 3 of them...
  • You could try disabling all lighting before drawing the cube:

    glDisable(GL_LIGHTING);
    
  • This has me stomped. What it looks like is that some vertices have some alpha values that are non-opaque. However the code you posted has all 1. for alpha. So... in order to debug more, did you try to change your clear color to something non-black ? Say green ? From the code, I doubt lighting is turned on, since no normals were specified.

    Last comment, offtopic... You should really not use glBegin/glEnd (2 function calls per vertex + 2 per primitive is really not a good usage of the recent developments in OpenGL). Try glDrawElements with QUAD_LIST, or even better, TRIANGLE_LIST. You already have the data nicely laid out for that.

0 comments:

Post a Comment