# Thread: Quaternion functions for GLSL

1. ## Quaternion functions for GLSL

I think it would be a good idea to add quaternion functions to GLSL. Specially scalar GPUs could use other optimizations than vector based.

The most important functions are:

* Quaternion multiplication
* Quaternion multiplication with inverse quaternion
* Rotate Vector by quaternion
* SLERP interpolation

Language implemented functions could be better optimized for the hardware, than own functions in GLSL

2. ## Re: Quaternion functions for GLSL

Quaternion to Matrix should be a good fonction. (it's part of "Rotate Vector by quaternion")

3. ## Re: Quaternion functions for GLSL

Quaternion to matrix and a matrix vector multiplication is much slower than a optimized version of two quaternion multiplications:
Code :
``` vec3 qtransform( vec4 q, vec3 v ){
return v + 2.0*cross(cross(v, q.xyz ) + q.w*v, q.xyz);
}```

More important is a mat3 to quaternion function.
Two new functions for the list:

* mat3 to quaternion
* quaternion to mat3

4. ## Re: Quaternion functions for GLSL

There's supposed to be some kind of specialized mechanism using swizzles that makes vector/quaternion transforms take only 2 opcodes (in a vector shader).

5. ## Re: Quaternion functions for GLSL

That are more than 2 opcodes. Sometime quaternions looking very cheap but that is not true, because each quaternion multiplication needs 16 scalar multiplications.

I've wrote 24 mul/madd instruction because a quaternion multiplication can be represented as 4x4 matrix * vector multiplication. A optimization is to remove all part that are multiplied zero or that sum relult in zero. After removing that unneeded valuesit a 3x4 and a 4x3 matrix, both could be multiplied together to a 3x3 matrix (quaternion to matrix conversion), but (matrix * matrix) * vector is slower than matrix * (matrix * vector)

6. ## Re: Quaternion functions for GLSL

That are more than 2 opcodes. Sometime quaternions looking very cheap but that is not true, because each quaternion multiplication needs 16 scalar multiplications.
Never mind; I was thinking about the vector cross-product 2-opcode version.

7. ## Re: Quaternion functions for GLSL

There is another usefull function (the of the list):

* Quaternion to normal

* Quaternion multiplication
* Quaternion multiplication with inverse quaternion
* Rotate Vector by quaternion
* SLERP interpolation
* mat3 to quaternion
* quaternion to mat3

8. ## Re: Quaternion functions for GLSL

* Quaternion to normal
What is this? How do you turn a quaternion into a normal?

9. ## Re: Quaternion functions for GLSL

Quaternion used for rotations are basically directions. I don't know the math, but it sounds possible to convert to/from normals.

10. ## Re: Quaternion functions for GLSL

Quaternion used for rotations are basically directions.
Quaternions are not directions; they are orientations. A quaternion represents a unique orientation of an object.

They can represent orientations relative to the identity quaternion as well.

A quaternion is no more a direction than an angle/axis rotation or a rotation matrix. It only becomes a direction when you ask the question, "Where would this vector go if you rotated it by this quaternion?" And that question can already be asked with vector/quaternion rotation.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•