Javier Ramírez
Javier Ramírez

Reputation: 1011

C + OpenGL quaternions

A quaternion is a number of the form:

a + bi + cj + dk

Right? Good, but...

  1. How do I interpret this number in C language?

  2. If for example I want to rotate a cube What multiplied by the quaternion? A vector?

Upvotes: 2

Views: 425

Answers (2)

didierc
didierc

Reputation: 14750

For your first question, I think you mean "how do I represent", not "interpret".

The simplest way is to use a struct:

typedef struct quaternion_t {
  double x,y,z,w;
} quaternion_t;

Note that a usual practice, as used above, is also to use x, y, z, and w for the component names (but your naming is perfectly acceptable, as long as you know which one is which). The use of double or single precision floats for components depends on your needs: accuracy or space.

Simple operations are then easy to implement:

void conjugate(quaternion_t *q){
    q->x = -q->x;
    q->y = -q->y;
    q->z = -q->z;
}

double product(quaternion_t *q1, quaternion_t *q2){
    return q1->x * q2->x + q1->y * q2->y + q1->z * q2->z + q1->w * q2->w;
}

double norm(quaternion_t *q){
    double p = product(q,q);
    return sqrt(p);
}

// etc

For your second question, I suggest that you look for a good tutorial on that topic. Meanwhile, the wikipedia pages:

provide a good introduction.

Upvotes: 6

Related Questions