PDA

View Full Version : Clipping problem



Darwin
10-27-2010, 01:36 PM
Hey people. I have a problem with 3D projection in my project.
(Before I start, I'm using C# and OpenTK bindings but it really shouldn't make a difference)

Post from another forum:
So, I've played around with 3D perspective and got everything working nicely with non-deprecated OpenGL but I have a problem.
I render a rotating rectangle (textured) and it rotates (roll) fine but when I pitch it down a bit and keep rolling it I get clipping on the left and right.

Here's a sceeenshot:
http://b.imagehost.org/0360/clipping.png

and here's the shader code as I think the problem is there:


#version 110

uniform float timer;
uniform mat4 window_matrix;
uniform mat4 model_matrix;
uniform int animated;
uniform int frames;
uniform int current_frame;

attribute vec2 texcoord;
attribute vec4 position;

varying vec2 frag_texcoord;

mat4 view_frustum(
float angle_of_view,
float z_near,
float z_far
) {
return mat4(
vec4( 1.0 / tan(angle_of_view), 0.0, 0.0, 0.0),
vec4( 0.0, 1.0 / tan(angle_of_view), 0.0, 0.0),
vec4( 0.0, 0.0, (z_far + z_near) / (z_far - z_near), 0.1),
vec4( 0.0, 0.0, -2.0 * z_far * z_near / (z_far - z_near), 1.0)
);
}

void main() {

vec2 temp = texcoord;

if (animated == 1) {

float step = 1.0 / float(frames);

if ( temp.x == 0.0 ){

temp.x = step * float(current_frame);
} else if ( temp.x == 1.0) {

temp.x = step * float(current_frame) + step;
}
}

frag_texcoord = temp;

gl_Position = window_matrix * model_matrix * view_frustum( radians( 60.0 ), 0.0, 100.0 ) * position;
}






I did some debugging and it appears the the clipping appears where the z coordinate is >1 or <-1 which didn't really make sense to me since the polygon isn't even supposed to rotate that way to make that position possible.

If there's nothing wrong with my shader code and you think it's something else I can post other code (the way I calculate the model matrix, tho I'm pretty sure it's correct)


Oh, and the window matrix is
WindowMatrix = new Matrix4( 0.625F, 0, 0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 );




Thanks in advance if anyone takes the time to help.

david_f_knight
10-27-2010, 11:48 PM
My guess is that the problem is the fourth row of the matrix returned by view_frustum(). I suspect it needs to be something more like [0 0 1 0] or maybe [0 0 -1 0]. I'm not sure about that, though, but easy enough to try.

Darwin
10-28-2010, 08:25 AM
Well setting the last one to 0 doesn't render anything but with
[0 0 -1 1] I get the same thing I got before. Clipping on the sides.