简体   繁体   中英

Camera in opengl not working

The problem is when I face my camera down the z axis for example and pitch this works fine however, after I have finished the pitch and would like to yaw on this new axis it begins to roll for some unknown reason =s.

    void FrustumCamera::xAxisRotation(float angle)
{
//  angle = angle * (double)degToRad;
    Vector3<float> x = m_orientation.getXAxis();
    Vector3<float> y = m_orientation.getYAxis();
    Vector3<float> z = m_orientation.getZAxis();

    y.rotateAroundAxis(x,angle);
    x = m_orientation.getXAxis();
    z.rotateAroundAxis(x,angle);

    m_orientation.setYAxis(y);
    m_orientation.setZAxis(z);

}
void FrustumCamera::yAxisRotation(float angle)
{
//  angle = angle * (double)degToRad;

    Vector3<float> x = m_orientation.getXAxis();
    Vector3<float> y = m_orientation.getYAxis();
    Vector3<float> z = m_orientation.getZAxis();

    x.rotateAroundAxis(y,angle);
    y = m_orientation.getYAxis();
    z.rotateAroundAxis(y,angle);

    m_orientation.setXAxis(x);
    m_orientation.setZAxis(z);
}
void FrustumCamera::zAxisRotation(float angle)
{
    Vector3<float> x = m_orientation.getXAxis();
    Vector3<float> y = m_orientation.getYAxis();
    Vector3<float> z = m_orientation.getZAxis();

    x.rotateAroundAxis(z,angle);
    z = m_orientation.getYAxis();
    y.rotateAroundAxis(z,angle);

    m_orientation.setXAxis(x);
    m_orientation.setYAxis(y);
}

template <class Type>
void Vector3<Type>::rotateAroundAxis(Vector3<Type> axis, const float angle)
{
    float radians = static_cast<Type>(angle * degToRad);
    Type sinAngle = static_cast<Type>(sin(radians));
    Type cosAngle = 0.0;

    if (angle == 90 || angle == -90)
        cosAngle = 0.0;
    else
        cosAngle = cos(radians); 

    normalise(axis);    // normalise the axis

    Type oneMinusCos = 1 - cosAngle;        // (1 - cos(theta))

    // construct the rotation matrix
    Type tempMatrix[3][3];
    tempMatrix[0][0] = (axis.x * axis.x) * oneMinusCos + cosAngle;
    tempMatrix[0][1] = (axis.x * axis.y) * oneMinusCos + axis.z * sinAngle;
    tempMatrix[0][2] = (axis.x * axis.z) * oneMinusCos - axis.y * sinAngle;
    tempMatrix[1][0] = (axis.x * axis.y) * oneMinusCos - axis.z * sinAngle;
    tempMatrix[1][1] = (axis.y * axis.y) * oneMinusCos + cosAngle;
    tempMatrix[1][2] = (axis.y * axis.z) * oneMinusCos + axis.x * sinAngle;
    tempMatrix[2][0] = (axis.x * axis.z) * oneMinusCos + axis.y * sinAngle;
    tempMatrix[2][1] = (axis.y * axis.z) * oneMinusCos - axis.x * sinAngle;
    tempMatrix[2][2] = (axis.z * axis.z) * oneMinusCos + cosAngle;

    Vector3<Type> temp(*this);
    Vector3<Type> result;

    result.x = (temp.x * tempMatrix[0][0]) + (temp.y * tempMatrix[1][0]) + (temp.z * tempMatrix[2][0]);
    result.y = (temp.x * tempMatrix[0][1]) + (temp.y * tempMatrix[1][1]) + (temp.z * tempMatrix[2][1]);
    result.z = (temp.x * tempMatrix[0][2]) + (temp.y * tempMatrix[1][2]) + (temp.z * tempMatrix[2][2]); 

    *this = result;
}

void OpenGLRenderer::startDraw(unsigned long mask)
{
    //sortBuffer();                                     // sort draw queue
    clearBuffers(mask);                                 // clear buffers
    loadIdentity();
    glTranslatef(-1*m_frustumCamera->getViewMatrix().getTranslationAxis().x,-1*m_frustumCamera->getViewMatrix().getTranslationAxis().y,-1*m_frustumCamera->getViewMatrix().getTranslationAxis().z);
    glMultMatrixf(m_frustumCamera->getViewMatrix().getMatrix());
    glTranslatef(m_frustumCamera->getViewMatrix().getTranslationAxis().x,m_frustumCamera->getViewMatrix().getTranslationAxis().y,m_frustumCamera->getViewMatrix().getTranslationAxis().z);// load identity

    //

    // push matrix stack
    matrixStackPush();                                          
}

You might be experiencing Gimbal Lock ; this can happen if you pitch all the way up or down so your look vector becomes parallel with your up vector, In which case a yaw will be the same as a roll.

This can be a downside of constructing rotations piecemeal via Euler angles. You may want to look into quaternions. (Note that you cant rotate with Euler angles; they are just a representation for rotation (you need to convert it to matrix or quats), but the way you are tackling it is very much an 'Euler angle' way of thinking about it)

The strength of matrix multiplication is that any sequence of multiple rotations can be represented (and concatenated) as a single rotation matrix. What you need to be doing is something like this:

void Transformable::yaw(float angle)
{
    float4x4 rot; // temp rotation matrix
    float3 translate(&_transform._41); // save our translation
    float3 up(&_transform._21); // y axis

    // build the rotation matrix for rotation around y
    MatrixRotationAxis(&rot, &up, angle);

    // multiply our transform by the rotation matrix
    // note that order of multiplication matters and depends on
    // if your matrices are column-major or row-major
    MatrixMultiply(&_transform, &_transform, &rot);

    // write back our original translation
    memcpy(&_transform._41, &translate, sizeof(float3));

    // might want to reorthogonalise every now and then
    // to make sure basis vectors are orthonormal
    // or you will probably get matrix creep after a few operations
}

instead of trying to rotate one basis vector at a time. In this case _transform would be a 4x4 homogenous matrix representing the transformation matrix. (rotation and translation). The topleft 3x3 submatrix is simply the basis vectors of the orientation space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM