How to use StencilState

Hi,



I’m trying to port some code written in JOGL/openGL to Jme, so that I can make use of some of Jme’s awesomeness. I’m coming up against my lack of knowledge of SceneGraphs and OpenGL. Hoping someone else can help.



I’m having trouble understanding how to do stencilling with Jme. I want to render a subset of my scene in a “window”, so that objects behind the window are “clipped” by it. This diagram tries to explain what I want.







(The camera is at Z > 0)



In the openGL code I have, this is achieved using a stencil:



[pre]

        gl.glColorMask(false,false,false,false);

        gl.glEnable(GL.GL_STENCIL_TEST);

        gl.glStencilFunc(GL.GL_ALWAYS, 1, 1);

        gl.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_REPLACE);

        gl.glDisable(GL.GL_DEPTH_TEST);



        // draw stencil geometry

        gl.glPushMatrix();

        gl.glBegin(GL.GL_QUADS);

        gl.glVertex3f(halfSize, halfSize, 0.0f);

        gl.glVertex3f(halfSize, -halfSize, 0.0f);

        gl.glVertex3f(-halfSize, -halfSize, 0.0f);

        gl.glVertex3f(-halfSize, halfSize, 0.0f);

        gl.glEnd();

        gl.glPopMatrix();



        gl.glEnable(GL.GL_DEPTH_TEST);

        gl.glColorMask(true,true,true,true);

        gl.glStencilFunc(GL.GL_EQUAL, 1, 1);

        gl.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_KEEP);



        renderItemsBehind(gl);

       

        gl.glDisable(GL.GL_STENCIL_TEST);



        renderItemsInfront(gl);

[/pre]



I need help in translating this into Jme code. Do I need to use two passes? I haven’t been able to find an example of using StencilStates anywhere. Any help would be much appreciated.



Cheers,

-Brendan