PDA

View Full Version : Rotation with Quaternion

JonasJ
05-31-2006, 01:31 PM
Hi,
I have tried to rotate the position of teh vertex over an axis in 3d. But with no success. So I read some topics about programming quaternions and so on, but I didn't find the mistake in my code. :(
My shader is still running, but the newPos isn't there where it habe to be ...

Maybe someone knows where the code is wrong ...

vec4 mult (vec4 qa, vec4 qb)
{
vec4 result;

result.w = qa.w * qb.w - dot(qa.xyz,qb.xyz);
result.xyz = cross (qa.xyz, qb.xyz) + (qa.xyz * qb.w) + (qb.xyz * qa.w);

return (result);
}

vec3 rotate (vec3 vector, vec3 axis, float angle)
{
axis = normalize(axis);
float sin_a = sin(angle/2.0);
float cos_a = cos(angle/2.0);

vec4 quaternion;
quaternion.x = axis.x * sin_a;
quaternion.y = axis.y * sin_a;
quaternion.z = axis.z * sin_a;
quaternion.w = cos_a;
quaternion = normalize(quaternion);

vec4 rotVector = mult (quaternion, vec4 (vector.x, vector.y, vector.z, 0.0));
rotVector = mult (rotVector, vec4 (-quaternion.x, -quaternion.y, -quaternion.z, quaternion.w));

return (rotVector.xyz);
}

// In Application:

vec4 newPos = vec4(rotate(oldPos.xyz, axis, 30.0), 1.0);
gl_Position = gl_ModelViewProjectionMatrix * vec4(newPos.xyz, 1.0);The old Position and the axis is wright, I've proofed this a several times. But why doesn't this work? :confused:

Humus
06-02-2006, 11:59 AM
Your angle appears to be in degrees, but sin() and cos() expects radians.