31

Here's my code:

void display(void);

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable( GL_BLEND );
    glutInitWindowSize(600,600);
    glutInitWindowPosition(200,50);
    glutCreateWindow("glut test");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(8);
    glBegin(GL_POINTS);
    glColor4f(.23,.78,.32,1.0);
    glVertex2f(0,0);
    glColor4f(.23,.78,.32,0.1);
    glVertex2f(0.1,0);
    glEnd();
    glFlush();
}

The problem is that these two points appear identical (even when I set the alpha to 0). Is there something I missed to enable alpha transparency?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
H-H
  • 4,431
  • 6
  • 33
  • 41

2 Answers2

76

have you glEnable'd alpha blending? And have you set up your blend parameters? You can't just set the alpha you need to setup various other parameters in OpenGL.

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );
Goz
  • 61,365
  • 24
  • 124
  • 204
  • OpenGL requires most everything to be enabled. A good place to start looking. (Disabling and enabling different operations at different times can lead to the most interesting effects.) –  Oct 24 '09 at 08:49
  • 1
    The initialization order also matters. Seems the window has to be initialized before glEnable() and glBlendFunc() is set. – user3078414 Feb 25 '16 at 23:02
  • @user3078414 The context needs to be created, but not (strictly speaking) attached to a window. OpenGL only cares about contexts, an off-screen one will do fine as far as it is concerned. – Segmented Jul 03 '17 at 13:52
20

Just a guess, but could it be that you dont have a background color ? So, when your rendering the second vertex which has alpha 0.1, there is no background to compute the proper color ? Just a guess, been years since i used opengl.

Andrew Keith
  • 7,515
  • 1
  • 25
  • 41
  • 9
    Yep, this was it. I should've added: `glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable( GL_BLEND ); glClearColor(0.0,0.0,0.0,0.0);` but only after glutCreateWindow() – H-H Oct 24 '09 at 09:09