Showing posts with label openGL alpha blending example. Show all posts
Showing posts with label openGL alpha blending example. Show all posts

Saturday, May 14, 2011

openGL alpha blending example in c/c++

[openGL alpha blending example] Following code fragment shows a openGL alpha blending example. When drawing GL shapes, you can control how to blend images by calling glBlendFunc() function. To enable blending, you should call glEnable(GL_BLEND) before calling glBlendFunc() function. [openGL alpha blending example]

 glEnable(GL_BLEND);  // Enable blending.

  glBlendFunc(GL_ONE, GL_ZERO);  //  Just the source image.
 
  glBegin(GL_TRIANGLES);  // Drawing Using Triangles
  glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
  glVertex4f( 0.0f, 1.2f, 0.0f, 1.0f); 
  glVertex4f(-1.2f,-1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.2f,-1.0f, 0.0f, 1.0f); 
  glEnd(); 

 // Alpha blending.
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  
  glBegin(GL_QUADS);  // Draw A Quad
  glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
  glVertex4f(-1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f,-1.0f, 0.0f, 1.0f); 
  glVertex4f(-1.0f,-1.0f, 0.0f, 1.0f); 
  glEnd();