Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 11, 2015, 9:28:39 PM (9 years ago)
Author:
landauf
Message:

added customized visualization for cylinders and cones to BulletDebugDrawer.
improved DebugDrawer by making circle, cylinder, and sphere rotatable. added rendering function for cones.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/libraries/tools/DebugDrawer.cc

    r10190 r10191  
    8484    }
    8585
    86     void DebugDrawer::buildCircle(const Ogre::Vector3& centre, float radius, int segmentsCount, const Ogre::ColourValue& colour, float alpha)
     86    void DebugDrawer::buildCircle(const Ogre::Matrix4& transform, float radius, int segmentsCount, const Ogre::ColourValue& colour, float alpha)
    8787    {
    8888        int index = linesIndex;
     
    9292        for (int i = 0; i < segmentsCount; i++)
    9393        {
    94             addLineVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y, centre.z + radius * Ogre::Math::Sin(angle)),
     94            addLineVertex(transform * Ogre::Vector3(radius * Ogre::Math::Cos(angle), 0, radius * Ogre::Math::Sin(angle)),
    9595                    Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    9696            angle += increment;
     
    101101    }
    102102
    103     void DebugDrawer::buildFilledCircle(const Ogre::Vector3& centre, float radius, int segmentsCount, const Ogre::ColourValue& colour, float alpha)
     103    void DebugDrawer::buildFilledCircle(const Ogre::Matrix4& transform, float radius, int segmentsCount, const Ogre::ColourValue& colour, bool up, float alpha)
    104104    {
    105105        int index = trianglesIndex;
     
    109109        for (int i = 0; i < segmentsCount; i++)
    110110        {
    111             addTriangleVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y, centre.z + radius * Ogre::Math::Sin(angle)),
     111            addTriangleVertex(transform * Ogre::Vector3(radius * Ogre::Math::Cos(angle), 0, radius * Ogre::Math::Sin(angle)),
    112112                    Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    113113            angle += increment;
    114114        }
    115115
    116         addTriangleVertex(centre, Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    117 
    118         for (int i = 0; i < segmentsCount; i++)
    119             addTriangleIndices(i + 1 < segmentsCount ? index + i + 1 : index, index + i, index + segmentsCount);
    120     }
    121 
    122     void DebugDrawer::buildCylinder(const Ogre::Vector3& centre, float radius, int segmentsCount, float height, const Ogre::ColourValue& colour, float alpha)
     116        addTriangleVertex(transform.getTrans(), Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
     117
     118        for (int i = 0; i < segmentsCount; i++)
     119        {
     120            if (up)
     121                addTriangleIndices(i + 1 < segmentsCount ? index + i + 1 : index, index + i, index + segmentsCount);
     122            else
     123                addTriangleIndices(index + i, i + 1 < segmentsCount ? index + i + 1 : index, index + segmentsCount);
     124        }
     125    }
     126
     127    void DebugDrawer::buildCylinder(const Ogre::Vector3& centre, const Ogre::Quaternion& rotation, float radius, int segmentsCount, float height, const Ogre::ColourValue& colour, float alpha)
    123128    {
    124129        int index = linesIndex;
    125         float increment = 2 * Ogre::Math::PI / segmentsCount;
    126         float angle = 0.0f;
    127 
    128         // Top circle
    129         for (int i = 0; i < segmentsCount; i++)
    130         {
    131             addLineVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y + height / 2, centre.z + radius * Ogre::Math::Sin(angle)),
    132                     Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    133             angle += increment;
    134         }
    135 
    136         angle = 0.0f;
    137 
    138         // Bottom circle
    139         for (int i = 0; i < segmentsCount; i++)
    140         {
    141             addLineVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y - height / 2, centre.z + radius * Ogre::Math::Sin(angle)),
    142                     Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    143             angle += increment;
    144         }
    145 
    146         for (int i = 0; i < segmentsCount; i++)
    147         {
    148             addLineIndices(index + i, i + 1 < segmentsCount ? index + i + 1 : index);
    149             addLineIndices(segmentsCount + index + i, i + 1 < segmentsCount ? segmentsCount + index + i + 1 : segmentsCount + index);
     130
     131        Ogre::Matrix4 transform(rotation);
     132        transform.setTrans(centre + rotation * Ogre::Vector3(0, height / 2, 0));
     133        this->buildCircle(transform, radius, segmentsCount, colour, alpha);
     134        transform.setTrans(centre + rotation * Ogre::Vector3(0, -height / 2, 0));
     135        this->buildCircle(transform, radius, segmentsCount, colour, alpha);
     136
     137        for (int i = 0; i < segmentsCount; i++)
    150138            addLineIndices(index + i, segmentsCount + index + i);
    151         }
    152     }
    153 
    154     void DebugDrawer::buildFilledCylinder(const Ogre::Vector3& centre, float radius, int segmentsCount, float height, const Ogre::ColourValue& colour,
     139    }
     140
     141    void DebugDrawer::buildFilledCylinder(const Ogre::Vector3& centre, const Ogre::Quaternion& rotation, float radius, int segmentsCount, float height, const Ogre::ColourValue& colour,
    155142            float alpha)
    156143    {
    157144        int index = trianglesIndex;
    158         float increment = 2 * Ogre::Math::PI / segmentsCount;
    159         float angle = 0.0f;
    160 
    161         // Top circle
    162         for (int i = 0; i < segmentsCount; i++)
    163         {
    164             addTriangleVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y + height / 2, centre.z + radius * Ogre::Math::Sin(angle)),
    165                     Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    166             angle += increment;
    167         }
    168 
    169         addTriangleVertex(Ogre::Vector3(centre.x, centre.y + height / 2, centre.z), Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    170 
    171         angle = 0.0f;
    172 
    173         // Bottom circle
    174         for (int i = 0; i < segmentsCount; i++)
    175         {
    176             addTriangleVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y - height / 2, centre.z + radius * Ogre::Math::Sin(angle)),
    177                     Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    178             angle += increment;
    179         }
    180 
    181         addTriangleVertex(Ogre::Vector3(centre.x, centre.y - height / 2, centre.z), Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
    182 
    183         for (int i = 0; i < segmentsCount; i++)
    184         {
    185             addTriangleIndices(i + 1 < segmentsCount ? index + i + 1 : index, index + i, index + segmentsCount);
    186 
    187             addTriangleIndices(i + 1 < segmentsCount ? (segmentsCount + 1) + index + i + 1 : (segmentsCount + 1) + index,
    188                     (segmentsCount + 1) + index + segmentsCount, (segmentsCount + 1) + index + i);
    189 
     145
     146        Ogre::Matrix4 transform(rotation);
     147        transform.setTrans(centre + rotation * Ogre::Vector3(0, height / 2, 0));
     148        this->buildCircle(transform, radius, segmentsCount, colour, alpha);
     149        this->buildFilledCircle(transform, radius, segmentsCount, colour, true, alpha);
     150
     151        transform.setTrans(centre + rotation * Ogre::Vector3(0, -height / 2, 0));
     152        this->buildCircle(transform, radius, segmentsCount, colour, alpha);
     153        this->buildFilledCircle(transform, radius, segmentsCount, colour, false, alpha);
     154
     155        for (int i = 0; i < segmentsCount; i++)
     156        {
    190157            addQuadIndices(index + i, i + 1 < segmentsCount ? index + i + 1 : index,
    191158                    i + 1 < segmentsCount ? (segmentsCount + 1) + index + i + 1 : (segmentsCount + 1) + index, (segmentsCount + 1) + index + i);
    192159        }
     160    }
     161
     162    void DebugDrawer::buildCone(const Ogre::Vector3& centre, const Ogre::Quaternion& rotation, float radius, int segmentsCount, float height, const Ogre::ColourValue& colour, float alpha)
     163    {
     164        int index = linesIndex;
     165
     166        Ogre::Matrix4 transform(rotation);
     167        transform.setTrans(centre + rotation * Ogre::Vector3(0, -height / 2, 0));
     168        this->buildCircle(transform, radius, segmentsCount, colour, alpha);
     169
     170        addLineVertex(centre + rotation * Ogre::Vector3(0, height / 2, 0), Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
     171
     172        for (int i = 0; i < segmentsCount; i++)
     173            addLineIndices(index + i, index + segmentsCount);
     174    }
     175
     176    void DebugDrawer::buildFilledCone(const Ogre::Vector3& centre, const Ogre::Quaternion& rotation, float radius, int segmentsCount, float height, const Ogre::ColourValue& colour,
     177            float alpha)
     178    {
     179        int index = trianglesIndex;
     180
     181        Ogre::Matrix4 transform(rotation);
     182        transform.setTrans(centre + rotation * Ogre::Vector3(0, -height / 2, 0));
     183        this->buildCircle(transform, radius, segmentsCount, colour, alpha);
     184        this->buildFilledCircle(transform, radius, segmentsCount, colour, false, alpha);
     185
     186        addTriangleVertex(centre + rotation * Ogre::Vector3(0, height / 2, 0), Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
     187
     188        for (int i = 0; i < segmentsCount; i++)
     189            addTriangleIndices(index + i, index + segmentsCount + 1, i + 1 < segmentsCount ? index + i + 1 : index);
    193190    }
    194191
     
    301298    }
    302299
    303     void DebugDrawer::drawCircle(const Ogre::Vector3& centre, float radius, int segmentsCount, const Ogre::ColourValue& colour, bool isFilled)
    304     {
    305         buildCircle(centre, radius, segmentsCount, colour);
    306         if (isFilled)
    307             buildFilledCircle(centre, radius, segmentsCount, colour, fillAlpha);
    308     }
    309 
    310     void DebugDrawer::drawCylinder(const Ogre::Vector3& centre, float radius, int segmentsCount, float height, const Ogre::ColourValue& colour, bool isFilled)
    311     {
    312         buildCylinder(centre, radius, segmentsCount, height, colour);
    313         if (isFilled)
    314             buildFilledCylinder(centre, radius, segmentsCount, height, colour, fillAlpha);
     300    void DebugDrawer::drawCircle(const Ogre::Vector3& centre, const Ogre::Quaternion& rotation, float radius, const Ogre::ColourValue& colour, bool isFilled)
     301    {
     302        int segmentsCount = std::min(100.0, radius / 2.5);
     303
     304        Ogre::Matrix4 transform(rotation);
     305        transform.setTrans(centre);
     306
     307        buildCircle(transform, radius, segmentsCount, colour);
     308        if (isFilled)
     309            buildFilledCircle(transform, radius, segmentsCount, colour, fillAlpha);
     310    }
     311
     312    void DebugDrawer::drawCylinder(const Ogre::Vector3& centre, const Ogre::Quaternion& rotation, float radius, float height, const Ogre::ColourValue& colour, bool isFilled)
     313    {
     314        int segmentsCount = std::min(100.0, radius / 2.5);
     315
     316        if (isFilled)
     317            buildFilledCylinder(centre, rotation, radius, segmentsCount, height, colour, fillAlpha);
     318        else
     319            buildCylinder(centre, rotation, radius, segmentsCount, height, colour);
     320    }
     321
     322    void DebugDrawer::drawCone(const Ogre::Vector3& centre, const Ogre::Quaternion& rotation, float radius, float height, const Ogre::ColourValue& colour, bool isFilled)
     323    {
     324        int segmentsCount = std::min(100.0, radius / 2.5);
     325
     326        if (isFilled)
     327            buildFilledCone(centre, rotation, radius, segmentsCount, height, colour, fillAlpha);
     328        else
     329            buildCone(centre, rotation, radius, segmentsCount, height, colour);
    315330    }
    316331
     
    329344    }
    330345
    331     void DebugDrawer::drawSphere(const Ogre::Vector3& centre, float radius, const Ogre::ColourValue& colour, bool isFilled)
     346    void DebugDrawer::drawSphere(const Ogre::Vector3& centre, const Ogre::Quaternion& rotation, float radius, const Ogre::ColourValue& colour, bool isFilled)
    332347    {
    333348        const IcoSphere& sphere = this->getIcoSphere(radius);
    334349
    335         int baseIndex = linesIndex;
    336         linesIndex += sphere.addToVertices(&lineVertices, centre, colour, radius);
    337         sphere.addToLineIndices(baseIndex, &lineIndices);
    338 
    339         if (isFilled)
    340         {
    341             baseIndex = trianglesIndex;
     350        if (isFilled)
     351        {
     352            this->drawCircle(centre, rotation * Ogre::Quaternion(Ogre::Degree(90), Ogre::Vector3(1, 0, 0)), radius, colour, false);
     353            this->drawCircle(centre, rotation * Ogre::Quaternion(Ogre::Degree(90), Ogre::Vector3(0, 1, 0)), radius, colour, false);
     354            this->drawCircle(centre, rotation * Ogre::Quaternion(Ogre::Degree(90), Ogre::Vector3(0, 0, 1)), radius, colour, false);
     355
     356            int baseIndex = trianglesIndex;
    342357            trianglesIndex += sphere.addToVertices(&triangleVertices, centre, Ogre::ColourValue(colour.r, colour.g, colour.b, fillAlpha), radius);
    343358            sphere.addToTriangleIndices(baseIndex, &triangleIndices);
     359        }
     360        else
     361        {
     362            int baseIndex = linesIndex;
     363            linesIndex += sphere.addToVertices(&lineVertices, centre, colour, radius);
     364            sphere.addToLineIndices(baseIndex, &lineIndices);
    344365        }
    345366    }
Note: See TracChangeset for help on using the changeset viewer.