Hello.
I’m trying to make a cube with a hole on one face. I’m using blending to add transparency when drawing the cube. Is it possible to draw a polygon (hole) on one face of the cube and keep the cube blending options? I would like to see through that hole the inside edges/corners of the cube.
I tried to use Stencil and this is the result I got:
As you can see, other objects are visible through the stencil surface but the inside corners of the same object not.
This is the code I used to generate the objects:
GLuint GLWidget::makeObject(GLuint listid, GLfloat x, GLfloat y, GLfloat z)
{
GLuint list = glGenLists(listid);
glNewList(list, GL_COMPILE);
// glPushMatrix();
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glClearStencil(0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, Pearl[AMBIENT]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, Pearl[SHININESS]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Pearl[DIFFUSE]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Pearl[SPECULAR]);
// glColorMask(0,0,0,0);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);
// glDepthMask(GL_FALSE);
glBegin(GL_QUADS);
glNormal3f( 0, y, 0 );
glVertex3f( x/2, y, -z/2 );
glNormal3f( 0, y, 0 );
glVertex3f( -x/2, y, -z/2 );
glNormal3f( 0, y, 0 );
glVertex3f( -x/2, y, z/2 );
glNormal3f( 0, y, 0 );
glVertex3f( x/2, y, z/2 );
glEnd();
// glColorMask(1,1,1,1);
// glDepthMask(GL_TRUE);
glStencilFunc(GL_NOTEQUAL, 1, 1);
glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, Ruby[AMBIENT]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, Ruby[SHININESS]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Ruby[DIFFUSE]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Ruby[SPECULAR]);
glBegin(GL_QUADS);
//top
glNormal3f( 0, y, 0 );
glVertex3f( x, y, -z );
glNormal3f( 0, y, 0 );
glVertex3f( -x, y, -z );
glNormal3f( 0, y, 0 );
glVertex3f( -x, y, z );
glNormal3f( 0, y, 0 );
glVertex3f( x, y, z );
glEnd();
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
glBegin(GL_QUADS);
//bottom
glNormal3f( 0, -y, 0 );
glVertex3f( x, -y, z );
glNormal3f( 0, -y, 0 );
glVertex3f( -x, -y, z );
glNormal3f( 0, -y, 0 );
glVertex3f( -x, -y, -z );
glNormal3f( 0, -y, 0 );
glVertex3f( x, -y, -z );
//front
glNormal3f( 0, 0, z );
glVertex3f( x, y, z );
glNormal3f( 0, 0, z );
glVertex3f( -x, y, z );
glNormal3f( 0, 0, z );
glVertex3f( -x, -y, z );
glNormal3f( 0, 0, z );
glVertex3f( x, -y, z );
//back
glNormal3f( 0, 0, -z );
glVertex3f( x, -y, -z );
glNormal3f( 0, 0, -z );
glVertex3f( -x, -y, -z );
glNormal3f( 0, 0, -z );
glVertex3f( -x, y, -z );
glNormal3f( 0, 0, -z );
glVertex3f( x, y, -z );
//left
glNormal3f( -x, 0, 0 );
glVertex3f( -x, y, z );
glNormal3f( -x, 0, 0 );
glVertex3f( -x, y, -z );
glNormal3f( -x, 0, 0 );
glVertex3f( -x, -y, -z );
glNormal3f( -x, 0, 0 );
glVertex3f( -x, -y, z );
//right
glNormal3f( x, 0, 0 );
glVertex3f( x, y, -z );
glNormal3f( x, 0, 0 );
glVertex3f( x, y, z );
glNormal3f( x, 0, 0 );
glVertex3f( x, -y, z );
glNormal3f( x, 0, 0 );
glVertex3f( x, -y, -z );
glEnd();
glDisable(GL_STENCIL_TEST);
// glPopMatrix();
glEndList();
return list;
}
The materials are defined as follows:
#define AMBIENT 0
#define DIFFUSE 1
#define SPECULAR 2
#define SHININESS 3
static float Pearl[][4] = {
{0.250000, 0.207250, 0.207250, 0.22000}, //0.922000
{1.000000, 0.829000, 0.829000, 0.22000},
{0.296648, 0.296648, 0.296648, 0.22000},
{11.264000}
};
static float Ruby[][4] = {
{0.174500, 0.011750, 0.011750, 0.550000},
{0.614240, 0.041360, 0.041360, 0.550000},
{0.727811, 0.626959, 0.626959, 0.550000},
{76.800003}
};
Am I setting up the stencils wrong? I dont think I understands very well how does they work…
Any help would be appreciated.
Thanks