上一篇介绍了如何渲染矩形,这一篇介绍如何将叠加的部分透明显示
void DrawRectangle(void)
{GLfloat xl, yt, xr, yb, w, h;glPushMatrix();glLoadIdentity();glTranslatef(0.0, 0.0, 0.f);w = 1.2;h = 1.0;xl = -0.8;yt = -0.8;yb = yt + h;xr = xl + w;// 红色glColor3f(1.f, 0.f, 0.f);glRectf(xl, yt, xr, yb);glEnable(GL_BLEND); //开启混合模式glBlendFunc(GL_SRC_ALPHA, GL_ONE);// 绿色glColor4f(0.f, 1.f, 0.f, 0.5f);xl += 0.2;xr += 0.2;yt += 0.2;yb += 0.2;glRectf(xl, yt, xr, yb);glDisable(GL_BLEND); //关闭混合模式// 蓝色glColor3f(0.f, 0.f, 1.f);xl += 0.2;xr += 0.2;yt += 0.2;yb += 0.2;glRectf(xl, yt, xr, yb);glEnd();glPopMatrix();return;
}