Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 3 123 LastLast
Results 1 to 10 of 33

Thread: Quaternion functions for GLSL

Hybrid View

  1. #1
    Junior Member Regular Contributor oc2k1's Avatar
    Join Date
    Jan 2007
    Location
    germany
    Posts
    171

    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. #2

    Re: Quaternion functions for GLSL

    Quaternion to Matrix should be a good fonction. (it's part of "Rotate Vector by quaternion")
    website : sebastien.hillaire.free.fr

  3. #3
    Junior Member Regular Contributor oc2k1's Avatar
    Join Date
    Jan 2007
    Location
    germany
    Posts
    171

    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);
    	}
    (24 scalar mul/madd instructions?)

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

    * mat3 to quaternion
    * quaternion to mat3

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    3,768

    Re: Quaternion functions for GLSL

    24 scalar mul/madd instructions?
    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. #5
    Junior Member Regular Contributor oc2k1's Avatar
    Join Date
    Jan 2007
    Location
    germany
    Posts
    171

    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. #6
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    3,768

    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. #7
    Junior Member Regular Contributor oc2k1's Avatar
    Join Date
    Jan 2007
    Location
    germany
    Posts
    171

    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. #8
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    3,768

    Re: Quaternion functions for GLSL

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

  9. #9
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    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. #10
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    3,768

    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
  •