chex_5

12-14-2012, 01:13 AM

Hello, I'm not quite sure if this is question has already been asked before but I couldn't find anything after searching Google for hours.

Basically, I'm trying to move my camera class away from vector rotations, due to the inevitable gimbal lock issue, and towards quaternion-based orientations. So far though, I've tried several different implementations using quaternions (even switched to matrices with no luck) but no matter what I try, I keep getting the same results that I had with rotating vectors. Can someone look over my code and help me figure this out? I feel as though the gimbal lock is rooted somewhere in between my euler angle conversion and the fact that I base my rotations on world axes.

//-----------------------------------------------------------------------------

// Camera Object - Movement & Orientation

//-----------------------------------------------------------------------------

void camera::look( const glm::vec3& inTarget ) {

glm::vec3 diffTarget ( pos - inTarget );

glm::quat rotations ( 0.f, diffTarget );

glm::vec3 angles ( glm::angle( rotations ) );

pitch = angles.x;

yaw = angles.y;

roll = angles.z;

}

void camera::move( float dx, float dy, float dz ) {

pos.x += dx;

pos.y += dy;

pos.z += dz;

}

void camera::rotate( float dPitch, float dYaw, float dRoll ) {

pitch = dPitch;

yaw = dYaw;

roll = dRoll;

}

//-----------------------------------------------------------------------------

// Camera - Updating

//-----------------------------------------------------------------------------

void camera::update() {

//update the rotations

glm::quat rotateX( std::cos( pitch ), std::sin( pitch ), 0.f, 0.f );

glm::quat rotateY( std::cos( yaw ), 0.f, std::sin( yaw ), 0.f );

glm::quat rotateZ( std::cos( roll ), 0.f, 0.f, std::sin( roll ) );

glm::quat endRot = rotateZ * rotateY * rotateX;

switch( camType ) {

default:

case CAM_TYPE_FPS:

target.x = endRot.x;

target.y = endRot.y;

target.z = endRot.z;

break;

case CAM_TYPE_SPECTATOR:

eye = endRot;

break;

}

mvp

= projMat

* glm::lookAt( pos, target, up )

* glm::mat4_cast( eye );

}

I also tried removing the yaw, pitch, and roll variables from my camera class since that seemed more natural, although my brain just isn't really wired for quaternions.

Basically, I'm trying to move my camera class away from vector rotations, due to the inevitable gimbal lock issue, and towards quaternion-based orientations. So far though, I've tried several different implementations using quaternions (even switched to matrices with no luck) but no matter what I try, I keep getting the same results that I had with rotating vectors. Can someone look over my code and help me figure this out? I feel as though the gimbal lock is rooted somewhere in between my euler angle conversion and the fact that I base my rotations on world axes.

//-----------------------------------------------------------------------------

// Camera Object - Movement & Orientation

//-----------------------------------------------------------------------------

void camera::look( const glm::vec3& inTarget ) {

glm::vec3 diffTarget ( pos - inTarget );

glm::quat rotations ( 0.f, diffTarget );

glm::vec3 angles ( glm::angle( rotations ) );

pitch = angles.x;

yaw = angles.y;

roll = angles.z;

}

void camera::move( float dx, float dy, float dz ) {

pos.x += dx;

pos.y += dy;

pos.z += dz;

}

void camera::rotate( float dPitch, float dYaw, float dRoll ) {

pitch = dPitch;

yaw = dYaw;

roll = dRoll;

}

//-----------------------------------------------------------------------------

// Camera - Updating

//-----------------------------------------------------------------------------

void camera::update() {

//update the rotations

glm::quat rotateX( std::cos( pitch ), std::sin( pitch ), 0.f, 0.f );

glm::quat rotateY( std::cos( yaw ), 0.f, std::sin( yaw ), 0.f );

glm::quat rotateZ( std::cos( roll ), 0.f, 0.f, std::sin( roll ) );

glm::quat endRot = rotateZ * rotateY * rotateX;

switch( camType ) {

default:

case CAM_TYPE_FPS:

target.x = endRot.x;

target.y = endRot.y;

target.z = endRot.z;

break;

case CAM_TYPE_SPECTATOR:

eye = endRot;

break;

}

mvp

= projMat

* glm::lookAt( pos, target, up )

* glm::mat4_cast( eye );

}

I also tried removing the yaw, pitch, and roll variables from my camera class since that seemed more natural, although my brain just isn't really wired for quaternions.