コード:
glEnable(GL_DEPTH_TEST);// 深度テスト
glEnable(GL_STENCIL_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0);// 背景色
/* いろいろ必要な処理 */
// 描画開始
// ステンシルの設定
glStencilOp(GL_KEEP,GL_REPLACE,GL_REPLACE);// 以降はステンシル値を書き込みます。
glColorMask(1,1,1,1);
glDepthMask(0);
// ここからはステンシル値を1で書き込みます。
glStencilFunc(GL_ALWAYS, 1, ~0);
// 分かりやすくするために色を付ける
unsigned int alpha = 100;
unsigned int red= 200;
unsigned int green = 0;
unsigned int blue = 0;
// 型紙1枚目描画
GLfloat triv[] = {
0, 0,
0, 500,
420,400,
};
//長方形を構成する四つの頂点の色を指定します
//ここではすべての頂点を同じ色にしています
const GLubyte triangleColors[] = {
static_cast<GLubyte>(red), static_cast<GLubyte>(green), static_cast<GLubyte>(blue) ,static_cast<GLubyte>(alpha),
static_cast<GLubyte>(red), static_cast<GLubyte>(green), static_cast<GLubyte>(blue) ,static_cast<GLubyte>(alpha),
static_cast<GLubyte>(red), static_cast<GLubyte>(green), static_cast<GLubyte>(blue) ,static_cast<GLubyte>(alpha),
};
//三角形を描画します
glVertexPointer(2, GL_FLOAT, 0, triv);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, triangleColors);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 3);
// 設定を絵の描画用に戻す。
glColorMask(1,1,1,1);
glDepthMask(1);
glStencilOp(GL_KEEP,GL_KEEP ,GL_KEEP);// 以降はステンシル値は変更しない。
// ここからはステンシル値が0の部分だけ描画します。
glStencilFunc(GL_EQUAL,0, ~0);
{
alpha = 100;
red= 0;
green = 200;
blue = 0;
GLubyte triangleColors2[] = {
static_cast<GLubyte>(red), static_cast<GLubyte>(green), static_cast<GLubyte>(blue) ,static_cast<GLubyte>(alpha),
static_cast<GLubyte>(red), static_cast<GLubyte>(green), static_cast<GLubyte>(blue) ,static_cast<GLubyte>(alpha),
static_cast<GLubyte>(red), static_cast<GLubyte>(green), static_cast<GLubyte>(blue) ,static_cast<GLubyte>(alpha),
};
GLfloat triv2[] = {
100, 0,
0, 500,
420,400,
};
//長方形を構成する四つの頂点の色を指定します
//ここではすべての頂点を同じ色にしています
//三角形を描画します
glVertexPointer(2, GL_FLOAT, 0, triv2);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, triangleColors2);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
glBindVertexArrayOES(0);