Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Cutting holes within shapes

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2007
    Location
    Romania
    Posts
    18

    Cutting holes within shapes

    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:
    Code :
    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:

    Code :
    #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

  2. #2
    Junior Member Newbie
    Join Date
    Apr 2007
    Location
    Romania
    Posts
    18

    Re: Cutting holes within shapes

    Fixed. It was all about lighting... added a light inside the cube and the edges are visible now.
    In Open Source we trust!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •