PDA

View Full Version : zNear clipping problem



darkrose
02-09-2010, 11:49 PM
I've been learning some OpenGL lately, running through some tutorials, but writting my own code rather than just copy/paste so I can actually have some idea what I'm doing.

I've got to the point where I'm trying to do a (very basic) first person "walk through the world" type of thing, basically drawing a floor and roof, and rotating/translating the modelview based on keyboard/mouse input.

All was working fine till I added the ability to look up and look down, at which point it seems the zNear clipping is being awkward. Basically when I look up (or down) the roof/floor is ending before the edge of the viewport, giving me a black line where the surface should continue. This screenshot shows it better than I can probably explain:
http://sandbox.ltmnet.com/images/zNear-clip.jpg

The init and predrawing code:


int init(void)
{
mouse.ld = 0;
mouse.cd = 0;
mouse.rd = 0;
mouse.px = 0;
mouse.py = 0;
mouse.x = 0;
mouse.y = 0;
screen_width = SCREEN_WIDTH;
screen_height = SCREEN_HEIGHT;
filling = 1;

glClearColor(0.0, 0.0, 0.0, 0.0);

glViewport(0,0,screen_width,screen_height);
//glEnable(GL_CULL_FACE);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,5.0f,1000.0f);
glMatrixMode(GL_MODELVIEW);

glShadeModel(GL_SMOOTH);

light_init();
player_init();

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

world_setup();
num_objects = 1;

return 0;
}




void predraw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

GLfloat xtrans = -player1.xpos;
GLfloat ztrans = -player1.zpos-3.0;
GLfloat ytrans = -player1.walkbias-0.5f;
GLfloat sceneroty = player1.yrot;

glRotatef(player1.lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);

glTranslatef(xtrans, ytrans, ztrans);

glEnable(GL_TEXTURE_2D);
}


This then goes on to loop through all the objects (only 1) and draw the vertices (8) for the triangles (4) which make up the floor and roof surfaces.

Any help would be most appreciated.

Dark Photon
02-10-2010, 06:12 AM
All was working fine till I added the ability to look up and look down, at which point it seems the zNear clipping is being awkward. Basically when I look up (or down) the roof/floor is ending before the edge of the viewport, giving me a black line where the surface should continue.

...
gluPerspective(45.0f,
(GLfloat)screen_width/(GLfloat)screen_height,
5.0f,
1000.0f);
...

Yes. And that near clip is a plane perpendicular to your look vector (eye-space -Z axis) exactly 5.0 world units from the eye.

So what's your question?

ZbuffeR
02-10-2010, 06:20 AM
gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,5.0f,1000.0f);

You clip at a distance of 5 units. What is the size/height/position of your floor and roof ?

Try with 1.0f and see if results are better.

darkrose
02-10-2010, 06:35 AM
I swear I tried it before, but I just set zNear to 0.1 and it's all good (1.0 is still to far away).

I'll experiment with different values to find the optimal.