- Home /
Quaternion Multiplication in a shader.
Hi All!
I have this math visualization project I am working on which involves stereo-graphically projecting points on the 4 dimensional sphere onto a 3D plane.
I have the prototype working great! (https://www.facebook.com/100008697402693/videos/1513084485658117/?theater)
Anyways, I'm trying to move most of the calculations I am doing off the CPU and onto the GPU. I have most of it working except I cannot for the life of me get quaternion multiplication to work in CG. I really need a function in CG that takes two float4's and multiplies them the same way the * operator works with Unity Quaternions.
The function I am using now looks like:
float4 mulQuaternion(float4 q1, float4 q2)
{
float4 q =
{
(q1.w * q2.x) + (q1.x * q2.w) + (q1.y * q2.z) - (q1.z * q2.y),
(q1.w * q2.y) - (q1.x * q2.z) + (q1.y * q2.w) + (q1.z * q2.x),
(q1.w * q2.z) + (q1.x * q2.y) - (q1.y * q2.x) + (q1.z * q2.w),
(q1.w * q2.w) - (q1.x * q2.x) - (q1.y * q2.y) - (q1.z * q2.z),
};
return q;
}
I'm sure there is a small mistake somewhere, but I have been at this for a while and can't seem to fix it. Any help would be really appreciated. Thank you!
Answer by cjdev · Jan 19, 2016 at 10:58 PM
It could be that you're doing the multiplication in the wrong order since it's not communicative or you might have the wrong equation. You could try the Hamilton product, which it looks like you either have or are close to.
float4 q =
{
q1.x*q2.x - q1.y*q2.y - q1.z*q2.z - q1.w*q2.w,
q1.x*q2.y + q1.y*q2.x + q1.z*q2.w - q1.w*q2.z,
q1.x*q2.z - q1.y*q2.w + q1.z*q2.x + q1.w*q2.y,
q1.x*q2.w + q1.y*q2.z - q1.z*q2.y + q1.w*q2.x
}
Your answer
Follow this Question
Related Questions
How to rotate Quaternion around axis for a certain degree 2 Answers
Determining Look Rotation As > A Number? 1 Answer
How to use quaternions to apply an offset to a rotation to calibrate a controller 1 Answer
Constrain object rotation set by mouse? 1 Answer
How can I pitch and roll a circular platform without releasing vertical/horizontal input? 1 Answer