PDA

View Full Version : I have a trouble in gluperspective method.

openfire
01-15-2010, 09:00 AM
I have a trouble in gluperspective method.
I want to implement the gluperspective method, but I failed.
Could you help me? many thanks!
this method has some bugs which I couldn't find.
_3DPoint _sysPerspective(double fovy,
double aspect,
double zNear, double zFar,
double x,double y,double z)
{

_3DPoint point;
printf("x=%f y=%f z=%f",x,y,z);

double sine, cotangent, deltaZ;
double radians = fovy / 2 * PI / 180;

deltaZ = zFar - zNear;
sine = sin(radians);
if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
return point;
}
cotangent = cos(radians) / sine;

point.x= (cotangent/aspect)*x/z;
point.y = cotangent*y/z;
point.z = z/deltaZ-zNear/deltaZ;
printf(" ->x=%f y=%f z=%f",x,y,z);
return point;
}
fovy=60 aspect=800/600 zNear=5 zFar=100
I try to execute the following args.
x=50 y=50 z=50 ->x=1.732051 y=1.732051 z=0.473684
x=100 y=50 z=50 ->x=3.464102 y=1.732051 z=0.473684
x=50 y=100 z=50 ->x=1.732051 y=3.464102 z=0.473684
x=100 y=100 z=50 ->x=3.464102 y=3.464102 z=0.473684
x=50 y=50 z=100 ->x=0.866025 y=0.866025 z=1.000000
x=100 y=50 z=100 ->x=1.732051 y=0.866025 z=1.000000
x=50 y=100 z=100 ->x=0.866025 y=1.732051 z=1.000000
x=100 y=100 z=100 ->x=1.732051 y=1.732051 z=1.000000
------
x=300 y=300 z=300 ->x=1.732051 y=1.732051 z=3.105263
x=500 y=300 z=300 ->x=2.886751 y=1.732051 z=3.105263
x=300 y=500 z=300 ->x=1.732051 y=2.886751 z=3.105263
x=500 y=500 z=300 ->x=2.886751 y=2.886751 z=3.105263
x=300 y=300 z=500 ->x=1.039230 y=1.039230 z=5.210526
x=500 y=300 z=500 ->x=1.732051 y=1.039230 z=5.210526
x=300 y=500 z=500 ->x=1.039230 y=1.732051 z=5.210526
x=500 y=500 z=500 ->x=1.732051 y=1.732051 z=5.210526
I get the error results. what's the right results?
how can I modify it?
how can I get the real screen pixel point? which method or ways?

Dark Photon
01-15-2010, 11:49 AM
I have a trouble in gluperspective method.
I want to implement the gluperspective method, but I failed.
Could you help me? many thanks!
this method has some bugs which I couldn't find.
...
I get the error results. what's the right results?
how can I modify it?
how can I get the real screen pixel point? which method or ways?
Please, give us a break. We're not going to debug your math code for you or help you out at all with a post like that.

You don't even say what specific problems you're having with it that you're trying to debug and what you've found thus far -- you just that it has some bugs and errors.

Go to Mesa3D (http://www.mesa3d.org/) and look at the source for the function.

Jackis
01-18-2010, 05:09 AM
First of all, you have wrong comprehension of how perspective works. Actually, it's nothing more then matrix multiplication and homogenous division. So the best you can do (and what you have to do actually) - is to create perspective matrix (like described in OpenGL specification), then multiply your point (x,y,z,1) by it and divide resulting XYZ by resulting W.