0

i want to make a hole in openGL cube.i have tried certain methods like using stencils and alpha blending.but problem with stencils is it is dividing and displaying the only half part.My requirement is i have to stack cubes and should make a user specified number of hole(rectangle/ellipse shaped)to only the top object.I am able to stack the objects but not able to make a hole if needed. I am new to openGL and i din't find any direct solution for this.can someone give a sample program for this requirement?

Stencils code:

glClear(GL_STENCIL_BUFFER_BIT);
glColorMask(false, false, false, false);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_EQUAL, 0, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glDisable(GL_DEPTH_TEST);
glColor4f(0,0,1,1.0f);
//code for cube
glEnable(GL_DEPTH_TEST);
glColorMask(true, true, true, true);
glStencilFunc(GL_ALWAYS,0, 1);
glStencilOp(GL_REPLACE,GL_KEEP, GL_ZERO); 
//code for cylinder
glDisable(GL_STENCIL_TEST);

1 Answers1

0

In your sample, why do you render hole after the cube?? if you want your stencil to do something you should do the opposite.

With this pseudo code, hole should be plane mesh or nearly.

-enable depth test if not already done; (depth test should not be disabled, if the point of view place hole on back faces, you dont want to have it reflected in front...)

-Enable stencil test;

-set colormask to false;

-set stencil op replace; (if your hole are rendered and maybe moving each frame,
use replace to update the stencil)

-set stencil func always 1 1; (if you wants your hole to be rendered into the stencil
buffer you have to pass always)

-render your hole mesh with your current model view matrices;

-restore colormask to true;

-set stencilfunc notequal 1 1; (dont draw if something appear in stencil at this place)

-set stencil op keep:; (keep in any case)

-draw your scene: holes will be masked;
  1. Use FBO to Create new texture with alpha channel used for creating the hole.
j-p
  • 1,622
  • 10
  • 18
  • I am not able to understand 5th step.render your hole mesh with your current model view matrices – user3318209 Mar 20 '14 at 08:20
  • render the model of your mesh (a flat cylinder in your case) – j-p Mar 20 '14 at 08:29
  • I am sorry can you just explain me what is render your hole mesh with your current model view matrices step..I think i am getting it but not in a proper manner may be because i missed rendering step. – user3318209 Mar 20 '14 at 08:37
  • your current Projection and view matrix means render the object in your scene which are used to define the holes with the same transformations than your cubes (glOrtho and glProjection are the same) – j-p Mar 20 '14 at 08:52
  • hmm.no actually.some dark shade is coming only in left hand top corner.if i zoom the cube then only able to see it.after zoom or rotate black rectangle figure appears on center of cube.but it doesn't behave as proper hole. – user3318209 Mar 20 '14 at 09:03
  • I can past you a working copy of my shader but it implement shadows phong and transparency – j-p Mar 20 '14 at 09:18