Issue #2 resolved

Mogre.Math.Sin() has problems

Anonymous created an issue

Details posted by user manski here: http://www.ogre3d.org/addonforums/viewtopic.php?p=72109#p72109

Quote: Specifically in my case (using Mogre 1.7.1 beta):

Mogre.Math.Sin(0.0f, true)

throws a DivisionByZero exception but Mogre.Math.Sin(0.0f, false)

doesn't. The same is true for "Cos(0.0f, true)".

Update: Neither of both functions seem to work at all when using lookup tables - no matter what angle is specified.

Update2: Found out that one needs to create a new instance of "Mogre.Math" to initialize its static trig table lookup functions to work. (WTF?) It's not done automatically for whatever reason.

Comments (8)

  1. Beauty

    The related code in in file MogreMath.h

    static inline Real Sin (Real fValue, bool useTables) 
    {
        return (!useTables) ? Real(System::Math::Sin(fValue)) : SinTable(fValue);
    }
    

    The method SinTable is in file MogreMath.cpp

    Real Math::SinTable (Real fValue)
    {
        // Convert range to index values, wrap if required
        int idx;
        if (fValue >= 0)
        {
            idx = int(fValue * mTrigTableFactor) % mTrigTableSize;
        }
        else
        {
            idx = mTrigTableSize - (int(-fValue * mTrigTableFactor) % mTrigTableSize) - 1;
        }
    
        return mSinTable[idx];
    }
    

    Related code in the same file:

    Math::Math()     // constructor
    {
        msAngleUnit = AngleUnit::AU_DEGREE;
    
        mTrigTableSize = 4096;
        mTrigTableFactor = mTrigTableSize / Math::TWO_PI;
    
        mSinTable = gcnew array<Real>(mTrigTableSize);
        mTanTable = gcnew array<Real>(mTrigTableSize);
    
        buildTrigTables();
    }
    
    
    Math::Math( unsigned int trigTableSize )     // constructor 2
    {
        msAngleUnit = AngleUnit::AU_DEGREE;
    
        mTrigTableSize = trigTableSize;
        mTrigTableFactor = mTrigTableSize / Math::TWO_PI;
    
        mSinTable = gcnew array<Real>(mTrigTableSize);
        mTanTable = gcnew array<Real>(mTrigTableSize);
    
        buildTrigTables();
    }
    
    //---------------------------------------
    
    
    void Math::buildTrigTables(void)
    {
        // Build trig lookup tables
        // Could get away with building only PI sized Sin table but simpler this 
        // way. Who cares, it'll ony use an extra 8k of memory anyway and I like 
        // simplicity.
        Real angle;
        for (int i = 0; i < mTrigTableSize; ++i)
        {
            angle = Math::TWO_PI * i / mTrigTableSize;
            mSinTable[i] = sin(angle);
            mTanTable[i] = tan(angle);
        }
    }
    
  2. BrainScan

    The Math class has been changed to a static class with a static constructor in Mogre 1.8. This allows the trig tables to be generated automatically without the need to create an instance of Math.

  3. Beauty

    I hope the table will be created only once and not for each call of a static method. (Just thoughts, I don't know details.)

    An other point: Mogre uses a ported Math class instead of wrapper usage. So I suppose, our Mogre Math class needs an update. Otherwise the 1.8 changes have no effect. Do you know more details about that?

  4. BrainScan

    Yes the tables will only be created once.

    On the other point, I already updated all the ported classes (including Math) for 1.8 in the following commit: 16a834e

    Previously, I did the same for 1.7 here: 68040e0

  5. Log in to comment