Why does my colored cube not work with GL_BLEND?

2019-05-31 09:27发布

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 http://img408.imageshack.us/img408/2853/normalcube.png

And here's what it actually looks like:

Dark cube http://img7.imageshack.us/img7/7133/darkcube.png

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 ...

标签: c++ opengl blend
3条回答
太酷不给撩
2楼-- · 2019-05-31 09:41

You could try disabling all lighting before drawing the cube:

glDisable(GL_LIGHTING);
查看更多
Emotional °昔
3楼-- · 2019-05-31 09:43

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.

查看更多
放荡不羁爱自由
4楼-- · 2019-05-31 09:52

It looks like you have lighting enabled on the second one,

try with a glShadeModel( GL_FLAT ) before drawing,

查看更多
登录 后发表回答