java - LWJGL Matrix lookAt method -


i need calculating lookat method here method

    public void lookat(vector3f position, vector3f direction, vector3f up) {     vector3f f = new vector3f();     vector3f u = new vector3f();     vector3f s = new vector3f();     vector3f.sub(direction, position, f);     f.normalise(f);     up.normalise(u);     vector3f.cross(f, u, s);     s.normalise(s);     vector3f.cross(s, f, u);      this.setidentity();     this.m00 = s.x;     this.m10 = s.y;     this.m20 = s.z;     this.m01 = u.x;     this.m11 = u.y;     this.m21 = u.z;     this.m02 = -f.x;     this.m12 = -f.y;     this.m22 = -f.z;     this.m30 = -vector3f.dot(s, position);     this.m31 = -vector3f.dot(u, position);     this.m32 = vector3f.dot(f, position); } 

but when test camera.lookat(position, new vector3f(1, 0 ,0), new vector3f(0, -1, 0)); camera looking down, end if camera.lookat(position, new vector3f(10000, 0 ,0), new vector3f(0, -1, 0));, camera looking forward. can please ?

p.s. sorry english

the second parameter of lookat function not direction in want point want at. far can see, calculation of method expects second point , not direction (which calculated 2 point , stored in f).

in conclusion, results correct me, except passed wrong parameters function.

how create function creates matrix point , direction

the rotation required 1 maps minus z-direction onto view vector. additionally, want x-vector mapped such perpendicular plane spanned view , vector. can relatively easy achieved writing -view in third row of 3x3 matrix. other 2 vectors can computed cross products of view , vector results in right-vector onto x-axis should mapped. last vector (the target mapping y-axis) computed cross product of view , right vector. cross products last vector used, since know rotation matrices have base vectors perpendicular:

viewv = normalize(-view) rightv = normalize(cross(view, up)) upv = normalize(cross(view, up))                        -- rightv -- rotation_matrix = [   --   upv  -- ]                       --  viewv -- 

when camera located in origin, done now. since in general not case, have add translation part transforms scene such camera origin. t = -camera.

the final matrix composed first translating space , rotating according our calculated matrix:

lookat_matrix = rotation_matrix * translate(-camera) 

since late here , depending on notation use might rotation matrix has transposed , signs have adjusted.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -