Saturday, May 14, 2011

GL_BLEND example c c++ objc

[GL_BLEND example] Following code fragment shows a GL_BLEND 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. [GL_BLEND 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();