Sign in to follow this  
Bruno

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);
	glLoadMatrixd(matrix);
         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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 :

http://download.developer.nvidia.com/developer/SDK/Individual_Samples/DEMOS/OpenGL/oblique_frustum.zip

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this