Sign in to follow this  
Nandd

Using a quaternion and a vector to define a plane

Recommended Posts

Hi all, I am doing an assignment where I need to create a simple raycast program. My problem is that the image plane (through which the rays are shot through from the camera to the objects) is to be defined by a quaternion and a (x,y,z) vector, and I don't know how to do this. Specifically, the quaternion and the (x,y,z) vector define the position and the orientation of the image plane. Can anybody offer any tips on how to approach this? Thanks

Share this post


Link to post
Share on other sites
If you're moving the camera through the scene, the camera has an orientation and a position in the scene.
If you're moving the scene around the camera, the scene will have a rotation and an offset.
These two modes are basically the same, but my examples below assume you're moving the world around the camera.
So, to represent the orientation of the scene, you need a rotation (which is what quaternions do). To represent the offset, you need 3 coordinates (x,y,z) - which is what the vector stores. So all you need to figure out is how to pass this data to OpenGL.

The translation / position is trivial : glTranslatef(vec.x, vec.y, vec.z);

The rotation is a bit trickier, because you need to convert the quaternion into data which OpenGL understands. In particular, you need to convert the quaternion data into rotation about an arbitrary axis:
float theta, ax, ay, az;
Quaternion q; // Use the camera's quaternion here!
q.normalize();
theta = 114.591559f * acos(q.w);
float s = sqrt(1.0f - q.w*q.w);
if(abs(s) < 0.0005f) s = 1; // prevents problems near and at s == 0
ax = q.x / s;
ay = q.y / s;
az = q.z / s;

glRotatef(theta, ax, ay, az);




I Recommend you Put the above code block into a member function of the Quaternion class, so you can convert a quaternion into a rotation about an arbitrary axis any time you may need it.

Hope this helps!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this