Jump to content

  • Log In with Google      Sign In   
  • Create Account


randomguy2000

Member Since 25 Dec 2012
Offline Last Active Dec 29 2012 11:02 AM
-----

Topics I've Started

Using stencil buffer to draw reflections

25 December 2012 - 10:50 AM

Hi!

I have a bit of a problem with one of my projects. I've just recently started learning Open GL and I've hit my first major obstacle. I am making a simple 3D pong game and I want the ball to be reflected off of the walls (or leave a shadow on them - not yet sure). So I've been trying to use the stencil buffer to draw the reflections, which works. The reflection is only visible on the parts where I want it to be be visible. But there is a problem - the walls are all white now. I am using materials and lights to create that 3D feel and this works great until I try to use the stencil buffer. After that all the walls are white (without any depth or anything like that).

 

Here is the actual rendering code (it is a bit messy since I've been trying to figure out what is wrong):

 

    
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_EMISSION, allocFloats(new float[] { 0.5f, 0.0f, 0f, 1.0f}));
    //enemyBat.setPosition(0.9f, -0.5f, -30);
    enemyBat.render3D();
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_EMISSION, allocFloats(new float[] { 0.0f, 0.0f, 0f, 1.0f}));
    
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_EMISSION, allocFloats(new float[] { 0.9f, 0.9f, 0f, 1.0f}));
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT_AND_DIFFUSE, allocFloats(new float[] { 0.5f, 0.5f, 0.5f, 0.8f}));
    //kugla.setPosition(0, 0.6f, -20);
    kugla.render3D();
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_EMISSION, allocFloats(new float[] { 0.0f, 0.0f, 0f, 1.0f}));
    
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_EMISSION, allocFloats(new float[] { 0.0f, 0.5f, 0f, 1.0f}));
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT_AND_DIFFUSE, allocFloats(new float[] { 0.5f, 0.5f, 0.5f, 0.4f}));
    //playerBat.setPosition(posX, posY, -10);
    playerBat.render3D();
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_EMISSION, allocFloats(new float[] { 0.0f, 0.0f, 0f, 1.0f}));
    
    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, allocFloats(new float[] { kugla.m_nX, kugla.m_nY, kugla.m_nZ, 1.0f}));
    
    
    
    
    GL11.glEnable(GL11.GL_STENCIL_TEST); //Enable using the stencil buffer
    GL11.glColorMask(false, false, false, false); //Disable drawing colors to the screen
    GL11.glDisable(GL11.GL_DEPTH_TEST); //Disable depth testing
    GL11.glStencilFunc(GL11.GL_ALWAYS, 1, 1); //Make the stencil test always pass
    //Make pixels in the stencil buffer be set to 1 when the stencil test passes
    GL11.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_REPLACE);
    //Set all of the pixels covered by the floor to be 1 in the stencil buffer
    polje.render3D();
    
    GL11.glColorMask(true, true, true, true); //Enable drawing colors to the screen
    GL11.glEnable(GL11.GL_DEPTH_TEST); //Enable depth testing
    //Make the stencil test pass only when the pixel is 1 in the stencil buffer
    GL11.glStencilFunc(GL11.GL_EQUAL, 1, 1);
    GL11.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_KEEP); //Make the stencil buffer not change
    
    //Draw the cube, reflected vertically, at all pixels where the stencil
    //buffer is 1
    GL11.glPushMatrix();
    GL11.glScalef(1, -1, 1);
    GL11.glTranslatef(0, kugla.radius, 0);
    kugla.render3D();
    GL11.glPopMatrix();
    
    GL11.glDisable(GL11.GL_STENCIL_TEST); //Disable using the stencil buffer
    
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    //GL11.glTranslatef(0, 0, -2);
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT_AND_DIFFUSE, allocFloats(new float[] { 0.7f, 0.7f, 1f, 1.0f}));
    //GL11.glEnable(GL11.GL_COLOR_MATERIAL);
    //GL11.glColorMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT_AND_DIFFUSE);
    //GL11.glMaterial(GL11.GL_FRONT, GL11.GL_SPECULAR, allocFloats(new float[] { 0.0f, 0.5f,1f, 1.0f}));
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_EMISSION, allocFloats(new float[] { 0.0f, 0.1f, 0.4f, 1f}));
    //GL11.glMateriali(GL11.GL_FRONT, GL11.GL_SHININESS, 1); //med 1 in 128
    polje.render3D();
    GL11.glDisable(GL11.GL_BLEND);

    Display.sync(50);

 

 


PARTNERS