PDA

View Full Version : stencil test not working on different machine



Neonic
02-16-2009, 12:17 PM
Hello everyone!

OK this is really weird! I wrote a small app yesterday, in which a cube and its reflection on a flat surface were to appear. I'm using the stencil buffer to make sure the reflection is only drawn were the surface is:



void Init()
{
glEnable(GL_CULL_FACE);

glEnable(GL_DEPTH_TEST);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);

glEnable(GL_STENCIL_TEST);
glClearStencil(0);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient_ligh t);

glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse_light);
glEnable(GL_LIGHT0);
}
bool RenderFunc()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL _STENCIL_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float dist=10.0;
vec3 campos(dist*sin(PI/4),dist*sin(PI/4),dist*sin(PI/4));

gluLookAt( campos.x,campos.y,campos.z, 0,0,0, 0,1,0 );

GLfloat light_pos[4]={-dist*sin(PI/4),dist*sin(PI/4),dist*sin(PI/4),1.0f};
GLfloat light_pos2[4]={-dist*sin(PI/4),-dist*sin(PI/4),dist*sin(PI/4),1.0f};
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);

glEnable(GL_LIGHTING);

glCullFace(GL_FRONT);

//-----------------------------------------
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

DrawWater();
//-----------------------------------------

///////////////////////////////////////////
glStencilFunc(GL_ALWAYS, 0, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

glPushMatrix();

vec3 camZ=(-campos); camZ.y=0; camZ=camZ.Unit();
vec3 camX=Cross(VEC_UP,camZ);
vec3 pos=x*camX-z*camZ;
glTranslatef(pos.x,pos.y+CUBE_Y,pos.z);
DrawCube();

glPopMatrix();
///////////////////////////////////////////

glCullFace(GL_BACK);

//.........................................
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

GLdouble eqr[] = {0.0,-1.0, 0.0, 0.0};
glEnable(GL_CLIP_PLANE0);
glClipPlane(GL_CLIP_PLANE0, eqr);

glClear(GL_DEPTH_BUFFER_BIT);

glLightfv(GL_LIGHT0,GL_POSITION,light_pos2);
glPushMatrix();

glScalef(1,-1,1);
glTranslatef(pos.x,pos.y+CUBE_Y,pos.z);

DrawCube();

glPopMatrix();

glDisable(GL_CLIP_PLANE0);
//.........................................

return true;
}


Yesterday it worked just fine, but today I moved the app to a different machine (without recompiling) and suddenly it's as if the stencil testing doesn't work anymore. The reflection is drawn outside the flat surface as well.

I recompiled the whole thing, still no change.

I tried an example application from the net (Nehe lesson 26) which also uses the stencil buffer, and it works just fine on this new machine.

Can someone help me solve this puzzle?

ZbuffeR
02-16-2009, 01:16 PM
Do you explicitly ask for a stencil buffer ?
16. Be Sure to Allocate Ancillary Buffers that You Use (http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/)

Neonic
02-16-2009, 01:24 PM
No I didn't do that.

It appears I have to request that in the pixelformat declaration:



static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
1, // Use Stencil Buffer ( * Important * )
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};


Now it works. Great!

Thank you.