# [Solved] Z coordinate clipping?

This topic is 4311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I recently made this small function to create a basic Cube with only passing a few parameters. The odd problem is, though, that when I pass in anything from 0 ~ 0.75f for the Z parameter, it draws properly, but when I place 0.76f ~ 1 as the Z paramter, i am unable to see it (although it continues to go through the function). I tried already changing my gluPerspective() call to have a larger clipping area, but that did not provide any effect (as the clipping area passed to gluPerspective was from 0 to 100 anyway). Here's the code:
	// x, y, z are coordinates
// size The length/size of each of the cube's edges
// center is true if the coordinates are the center of the cube
// ...and is false if the coordinates are the top-left front of the cube
// fill is whether the cube is filled or an outline
void drawCube(GLfloat x, GLfloat y, GLfloat z, GLfloat size, bool center, bool fill)
{
if(size == 0)
return;

float x_p, x_m, y_p, y_m, z_p, z_m;

if(center == false)
{
x_p = x + size;
x_m = x;
y_p = y;
y_m = y - size;
z_p = z + size;
z_m = z;
} else {
float half_size = (size / 2);
x_p = x + half_size;
x_m = x - half_size;
y_p = y + half_size;
y_m = y - half_size;
z_p = z + half_size;
z_m = z - half_size;
}

// Front
glBegin((fill == true) ? GL_QUADS : GL_LINE_LOOP);
glVertex3f(x_m, y_p, z_m);
glVertex3f(x_p, y_p, z_m);
glVertex3f(x_p, y_m, z_m);
glVertex3f(x_m, y_m, z_m);
glEnd();

// Right Side
glBegin((fill == true) ? GL_QUADS : GL_LINE_LOOP);
glVertex3f(x_p, y_p, z_m);
glVertex3f(x_p, y_p, z_p);
glVertex3f(x_p, y_m, z_p);
glVertex3f(x_p, y_m, z_m);
glEnd();

// Back
glBegin((fill == true) ? GL_QUADS : GL_LINE_LOOP);
glVertex3f(x_p, y_p, z_p);
glVertex3f(x_m, y_p, z_p);
glVertex3f(x_m, y_m, z_p);
glVertex3f(x_p, y_m, z_p);
glEnd();

// Left Side
glBegin((fill == true) ? GL_QUADS : GL_LINE_LOOP);
glVertex3f(x_m, y_p, z_p);
glVertex3f(x_m, y_p, z_m);
glVertex3f(x_m, y_m, z_m);
glVertex3f(x_m, y_m, z_p);
glEnd();

// Bottom
glBegin((fill == true) ? GL_QUADS : GL_LINE_LOOP);
glVertex3f(x_m, y_m, z_p);
glVertex3f(x_p, y_m, z_p);
glVertex3f(x_p, y_m, z_m);
glVertex3f(x_m, y_m, z_m);
glEnd();

// Top
glBegin((fill == true) ? GL_QUADS : GL_LINE_LOOP);
glVertex3f(x_m, y_p, z_p);
glVertex3f(x_p, y_p, z_p);
glVertex3f(x_p, y_p, z_m);
glVertex3f(x_m, y_p, z_m);
glEnd();

return;
}

Any help is appreciated; thanks! [Edited by - codemastermm on January 30, 2007 10:44:45 AM]

##### Share on other sites
Quote:
 Original post by codemastermmI tried already changing my gluPerspective() call to have a larger clipping area, but that did not provide any effect (as the clipping area passed to gluPerspective was from 0 to 100 anyway).

This threw a flag for me when you mentioned your clipping planes. Generally, you should set your near clipping plane as far from 0 as possible, definitely not right at it. Read more here
Quote:
 Specifying a zNear clipping plane value of 0.0 to gluPerspective() won't generate an OpenGL error, but it might cause depth buffering to act as if it's disabled. A negative zNear or zFar clipping plane value would produce undesirable results.A zNear or zFar clipping plane value of zero or negative, when passed to glFrustum(), will produce an error that you can retrieve by calling glGetError(). The function will then act as a no-op.
and here. This may not be what's causing your problem, but it's something to be aware of and keep in mind.

-jouley

##### Share on other sites
I changed my gluPerspective to use 1.0f and 1000. and still had no luck :(

##### Share on other sites
Quote:
 float half_size = (size / 2);

Should be
float half_size = (size / 2.0f);
It may not truncate anything, but it's always best to be explicit (and positive it works and clear in your code and ...).

Also, disable backface culling for the cube: glDisable(GL_CULL_FACE); just to make sure everything's getting drawn.

Anything?

If not, step through it in the debugger and see what all of the values of your variables are as they're used, not just as they get passed to the function.

-jouley

[Edit: After changing the clipping planes, does the same behavior result, or something different? What about if you use 0.1f for the near plane?]

[Edited by - jouley on January 30, 2007 1:02:47 AM]

##### Share on other sites
It seems that if I change the gluPerspective call's near parameter to something beyond 0.0f, the cube fails to render (if I place it as 0.0f, it renders exactly where I want it, but only if I set the Z coordinate to <= 0.75f)

I tried setting it to 0.1f,1000.0f and it revealed no luck for me, so I am unsure if jouley's extra changes helped anything.

##### Share on other sites
Shouldn't those z co-ordinates be negative if you want it to be in front of you?

##### Share on other sites
How do you set up your matrices? Normally, OpenGL's viewing coordinate system idiomatically looks down the negative Z axis; the cube is likely behind you if you are giving it positive Z coordinates...

##### Share on other sites
yep, TGA and jpetrie were correct; it was that I was using positive values instead of negative (since OpenGL uses right-handed coordinates, while I was thinking in left-handed coordinates).

Thanks! I was feeling horrid because I was stuck on something so minor and easy.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633387
• Total Posts
3011620
• ### Who's Online (See full list)

There are no registered users currently online

×