PDA

View Full Version : Matrix LookAt



User137
15-05-2006, 11:58 AM
Anyone know where i could find function that does the LookAt? Meaning, it creates a rotation matrix that points to a specific point in 3D space. There is function like gluLookAt that does this but i need it for my bone editing mode :P Google has not helped much, other than this:
http://www.euclideanspace.com/maths/algebra/vectors/lookat/index.htm

void LookAt(const vector3& dir, const vector3& up, matrix33& m) {
vector3 z(dir);
z.norm();
vector3 x( up * z ); // x = up cross z
x.norm();
vector3 y( z * x ); // y = z cross x
m.set_components(x,y,z );
}

I'll post here if i can get that translated but help or ready source is appreciated :wink:

JSoftware
15-05-2006, 12:40 PM
for opengl i use this:


function lookat(cameraPosition, cameraTarget: tvector4f): tmatrix4x4;
var zaxis, xaxis, yaxis: tvector4f;
begin
cameratarget := multiply(cameratarget,-1);
zaxis := normalize(sub(cameraTarget, cameraPosition));
xaxis := normalize(cross(vector4f(0,1,0,0), zaxis));
yaxis := cross(zaxis, xaxis);

result := matrix4x4(xaxis.x, yaxis.x, zaxis.x, 0,
xaxis.y, yaxis.y, zaxis.y, 0,
xaxis.z, yaxis.z, zaxis.z, 0,
-dot(xaxis, cameraPosition), -dot(yaxis, cameraPosition), -dot(zaxis, cameraPosition), 1);
end;

grudzio
15-05-2006, 12:45 PM
You can find gluLookAt implementation in Mesa3D source code. http://mesa3d.org/

Nitrogen
15-05-2006, 08:14 PM
Just be aware that the Up vector is hard coded in the example above... If you want it to be Really flexible, you can replace vector4f(0,1,0,0) with your own Up direction in your engine.

JSoftware
15-05-2006, 08:15 PM
Just be aware that the Up vector is hard coded in the example above... If you want it to be Really flexible, you can replace vector4f(0,1,0,0) with your own Up direction in your engine.
Oh yes indeed.. i forgot to tell that :)

User137
16-05-2006, 02:34 PM
Thanks for quick helps, i got the first code and JSoftware's version compiled and i think working well. Good signs is both functions acted about same way in my program. But the problem was not solved with that :(

The actual issue is about "modifying bones"... This means that i have a bone tree, i can focus on 1 bone and when i hold mouse the bone should follow the cursor. For this purpose i have already calculated a virtual plane at which cursor hovers at and creates a 3D point. That point is then transformed into last bone's rotation space. Thus far everything works perfectly, but when i try to get bone rotation towards cursor point it misses the target...