Implementation of glRotate(...) identical to the fixed function variant

Posted on 2011-05-03
Last Modified: 2012-06-22

In our own math lib we are implementing OpenGLs glRotate(..). It works, but differs slightly (never before the 8th decimal) from the (fixed function) glRotate(..).

I was just curious if "the most true" implementation of glRotate(..) is available. I'm now using the one attached.

I could imagine...
- doubles are used internally to store temporary results.
- sin/cos/sqrt implementations are slightly different.

Kind Regards,
Daniel Dekkers
void Mat4::rotate( float angle_deg, float x, float y, float z)
        float xx, yy, zz, xy, yz, zx, xs, ys, zs, s, c, one_minus_c, scale, angle_rad;
        Mat4 m;
        angle_rad = angle_deg*(M_PI/180.0);
        s = sin(angle_rad);
        c = cos(angle_rad);
        xx = x * x;
        yy = y * y;
        zz = z * z;
        scale = sqrtf(xx + yy + zz);
        if (scale < 1.0e-4)
        scale = 1.0 / scale;
        x *= scale;
        y *= scale;
        z *= scale;
        xy = x * y;
        yz = y * z;
        zx = z * x;
        xs = x * s;
        ys = y * s;
        zs = z * s;
        one_minus_c = 1.0 - c;
        m[0] = (one_minus_c * xx) + c;
        m[1] = (one_minus_c * xy) + zs;
        m[2] = (one_minus_c * zx) - ys;
        m[3] = 0.0;
        m[4] = (one_minus_c * xy) - zs;
        m[5] = (one_minus_c * yy) + c;
        m[6] = (one_minus_c * yz) + xs;
        m[7] = 0.0;
        m[8] = (one_minus_c * zx) + ys;
        m[9] = (one_minus_c * yz) - xs;
        m[10] = (one_minus_c * zz) + c;
        m[11] = 0.0;
        m[12] = 0.0;
        m[13] = 0.0;
        m[14] = 0.0;
        m[15] = 1.0;
        *this = (*this)*m;

Open in new window

Question by:DoZo1971
    LVL 53

    Expert Comment

    32bit IEEE754 float's really only have 7 decimal digits of precision. So anything after that depends entirely on the calculations.

    Small changes can have a big effect.

    Or in other words : what you observe is normal for floating point types, and can't be avoided.
    LVL 53

    Accepted Solution

    And with regards to the exact implementation of the glRotate function - that is quite platform dependent. Not only in the source code, but also in the compiled binary, the standard libraries, as well as the hardware. Different platforms will have slightly different results.

    But again : that's entirely normal and expected when using floating point types. The trick is to ignore the extra digits, and focus on the first few decimal digits only (keeping in mind that the more calculations you perform, the bigger the accumulated error can become).

    Author Closing Comment

    Still, it would be nice to for instance, see nVidia's calculation written down.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now