instead of glRotatef build a matrix

Started by
9 comments, last by Jernej.L 12 years, 11 months ago
Ok, thanks to everyone, here's the final working version:


function CreateGlRotateMatrix(angle, x, y, z: single) : TMatrix;
var
axis: TVector3f;
b, c, ac, s: single;
invLen : Single;
begin

angle:= vectorgeometry.degtorad(angle);

invLen:= RSqrt(x * x + y * y + z * z);
x:= x * invLen;
y:= y * invLen;
z:= z * invLen;

result:= IdentityHmgMatrix;

c:= cos(angle);
s:= sin(angle);

result[0,0] := (x*x) * (1-c)+c;
result[1,0] := x*y * (1-c)-z*s;
result[2,0] := x*z * (1-c)+y*s;

result[0,1] := y*x * (1-c)+z*s;
result[1,1] := (y*y) * (1-c)+c;
result[2,1] := y*z * (1-c)-x*s;

result[0,2] := x*z * (1-c)-y*s;
result[1,2] := y*z * (1-c)+x*s;
result[2,2] := (z*z) * (1-c)+c;

end;


This finally matches the game's map files to build a proper matrix, it uses this with some "odd" quaternion conversion routine, which i couldn't reproduce with any other quaternion to matrix algorythm on the internet:


// preprocess quarternion rotations
X := rx;
Y := ry;
Z := rz;
W := -rw;
S := Sqrt(1.0 - W * W);

// divide by zero
if not (S = 0) then
begin
Axis[0] := X / S;
Axis[1] := Y / S;
Axis[2] := Z / S;
Angle := 2 * geometry.ArcCos(W);

if not (Angle = 0) then begin

rotang:= CreateGlRotateMatrix( Angle * 180 / Pi, Axis[0], Axis[1], Axis[2]);
glMultMatrixf(@rotang[0,0]);

//glRotatef(Angle * 180 / Pi, Axis[0], Axis[1], Axis[2]);

end;
end;


But this only works on game file quaternions. If i use this on the game's quaternions from the memory i get some strange results, i'll post that as another question if i can't figure it out soon :S

Projects: Top Down City: http://mathpudding.com/

Advertisement

I'm not very skilled with math, but i'm wondering what does glRotatef do internally?

I have some legacy code which does some math i don't really understand and sends values to glRotatef. I am implementing a physics engine to go with this legacy code and i need a 4x4 matrix instead of that output, i don't really understand how the rotation along a vector glrotatef thing works, but i just need a resulting matrix - which if i multiplied with glMultMatrixf instead of glrotatef would result in same working output.

Thanks.


It multiplies the internal modelview matrix by a rotation matrix that was constructed with your parameters.
You can see how OpenGL constructs the rotation matrix in the respective page http://www.opengl.org/sdk/docs/man/xhtml/glRotate.xml

This topic is closed to new replies.

Advertisement