# OpenGL Oblique fustrum clipping

## Recommended Posts

I have been tryng to use the method displayed here : http://www.terathon.com/code/oblique.html But i just can't get it to work, it's like its clipping any geometry that i render clipPlane[0] = 0; clipPlane[1] = -1; clipPlane[2] = 0; clipPlane[3] = 12; With that, if i supply it to a standard clip plane, glClipPlane(GL_CLIP_PLANE0, clipPlane); It works just fine, however if i supply it to the oblique culling function, it just culls everything.. What i do is this :

void ModifyProjectionMatrix(const float clipPlane[4])
{
float	matrix[16];
float	q[4];

glGetFloatv(GL_PROJECTION_MATRIX, matrix);

q[0] = (sgn(clipPlane[0]) + matrix[8]) / matrix[0];
q[1] = (sgn(clipPlane[1]) + matrix[9]) / matrix[5];
q[2] = -1.0F;
q[3] = (1.0F + matrix[10]) / matrix[14];

// Calculate the scaled plane vector
float c[4];
c[0] = clipPlane[0] * (2.0F / (clipPlane[0] * q[0]));
c[1] = clipPlane[1] * (2.0F / (clipPlane[1] * q[1]));
c[2] = clipPlane[2] * (2.0F / (clipPlane[2] * q[2]));
c[3] = clipPlane[3] * (2.0F / (clipPlane[3] * q[3]));

// Replace the third row of the projection matrix
matrix[2] = c[0];
matrix[6] = c[1];
matrix[10] = c[2] + 1.0F;
matrix[14] = c[3];

// Load it back into OpenGL
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
}

glPushMatrix();
ModifyProjectionMatrix(clipPlane);
render_stuff();
glPopMatrix();

What could be wrong here ? thanks, Bruno [Edited by - Bruno on July 17, 2005 5:23:54 PM]

##### Share on other sites
Hi --

The problem is that in the following line of the original code,

Vector4D c = clipPlane * (2.0F / (clipPlane * q));

the clipPlane * q is a 4D dot product, but you've translated it as a componentwise product. I'll update my code to make it more explicit. If you change your code to the following, it ought to work. (Also remember that planes specified with glClipPlane are transformed by the inverse of the modelview matrix.)

void ModifyProjectionMatrix(const float clipPlane[4]){	float	matrix[16];	float	q[4];	glGetFloatv(GL_PROJECTION_MATRIX, matrix);	q[0] = (sgn(clipPlane[0]) + matrix[8]) / matrix[0];	q[1] = (sgn(clipPlane[1]) + matrix[9]) / matrix[5];	q[2] = -1.0F;	q[3] = (1.0F + matrix[10]) / matrix[14];	// Calculate the scaled plane vector        float d = 2.0F / (clipPlane[0] * q[0] + clipPlane[1] * q[1]                  + clipPlane[2] * q[2] + clipPlane[3] * q[3]);	float c[4];	c[0] = clipPlane[0] * d;	c[1] = clipPlane[1] * d;	c[2] = clipPlane[2] * d;        c[3] = clipPlane[3] * d;	// Replace the third row of the projection matrix	matrix[2] = c[0];	matrix[6] = c[1];	matrix[10] = c[2] + 1.0F;	matrix[14] = c[3];	// Load it back into OpenGL	glMatrixMode(GL_PROJECTION);	glLoadMatrixd(matrix);        glMatrixMode(GL_MODELVIEW);}

##### Share on other sites
Hi Eric,

Thanks a lot, that was the problem indeed, i made a incorrect translation.
However, there's seems to be another problem with this.

I'm using the to "cut" the water level in a reflection\refraction shader.
When i look at the ground the clipplane seems to move with the mouse and clips away the ground, if i look at the horizont is looks ok, if i look up, the clipplane moves again and "unclips" everything above water level.., Seems to me the near clip plane is moving with the mouse or something like that.
I tried adjusting the near clip plane \ far clip plane but did't had much look.
How is this fixable ?

thanks,

Bruno

##### Share on other sites
Hi again,

I messed around with this, but it just doens't want to work.. :(
Anyways, i googled around, and found a sample in nvidia, right here :

I converted their messy code, i replaced my clipplane with this, and it worked at the first try..,
I would still rather use your code Eric, seems more simple\faster and cleaner than nvidia, but what could be wrong here ?

thanks,
Bruno

##### Share on other sites
Hi --

Don't use the Nvidia sample -- it doesn't consider the effect on the far plane, and your depth buffer precision will be horrible.

Make sure that your clipping plane is specified in camera-space coordinates, and that its normal points into the view frustum. Note that unless your modelview matrix is the identity, this will not be the same plane as you would pass to glClipPlane().

-- Eric

##### Share on other sites
I just noticed that you're using the glLoadMatrixd() function to load the new projection matrix. You're loading garbage with that because it's reading doubles where you have floats. See what happens when you change it to glLoadMatrixf(). I'm not even sure how that compiled, unless you're using a straight C compiler with all sensible error checking turned off.

##### Share on other sites
you are right, glLoadMatrixd() is wrong there, i pasted it here with it in, but its not in the actual code, i forgot it there when i was tryng it with doubles for extra precision.

I believe the clipPlane i supply to your code, is in the same space as the camera, but about the modelview matrix being the identity matrix, if i reset it to the identity matrix i will loose my camera view, or am i missing something ?
I will have to try that, but just calling a glLoadIdentity to the modelview before calling your function, might to the job ?

Anyways, regarding the nvidia example, i don't have any precision artifacts, at least in this card ( nvidia gf6800 gt ), with a near plane of 0.03 and a far plane of 200.
Maybe the artifacts show up in bigger rations on near plane\far plane ? or maybe in other video cards ?

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627657
• Total Posts
2978472
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33