Skip to main content

History: Circle3D

Source of version: 4

Copy to clipboard
            To draw circles in 3D, the [http://www.ogre3d.org/docs/api/html/classOgre_1_1ManualObject.html|ManualObject] class (starting from Ogre 1.2 and up) can be easily used for that purpose, here is how:
{CODE(wrap="1", colors="c++")}    // Assuming scene_mgr is your SceneManager.
    ManualObject * circle = scene_mgr->createManualObject("circle_name");
    
    float const radius = 7;
    
    // accuracy is the count of points (and lines).
    // Higher values make the circle smoother, but may slowdown the performance.
    // The performance also is related to the count of circles.
    float const accuracy = 35;
    
    circle->begin("BaseWhiteNoLighting", RenderOperation::OT_LINE_STRIP);
    
    unsigned point_index = 0;
    for(float theta = 0; theta <= 2 * Math::PI; theta += Math::PI / accuracy) {
        circle->position(radius * cos(theta), 0, radius * sin(theta));
        circle->index(point_index++);
    }
    circle->index(0); // Rejoins the last point to the first.
    
    circle->end();
    
    scene_mgr->getRootSceneNode()->createChildSceneNode()->attachObject(circle);{CODE}

This give a white simple circle in the XZ-Plane. If you want it in another plane, say the XY-Plane, just replace this line:

{CODE(wrap="1", colors="c++")}        circle->position(radius * cos(theta), 0, radius * sin(theta));{CODE}

by this line:

{CODE(wrap="1", colors="c++")}        circle->position(radius * cos(theta), radius * sin(theta), 0);{CODE}

It's as simple as that.

!!!Circle line with certain thickness
Now if you want your circle to have a certain thickness, you'll have to use triangles. The code below does just that.

{CODE(wrap="1", colors="c++")}    ManualObject * circle = scene_mgr->createManualObject("circle_name");

    float const radius = 7,
                thickness = 2, // Of course this must be less than the radius value.
                accuracy = 35;

    circle->begin("BaseWhiteNoLighting", RenderOperation::OT_TRIANGLE_LIST);

    unsigned point_index = 0;
    for(float theta = 0; theta <= 2 * Math::PI; theta += Math::PI / accuracy) {
        circle->position(radius * cos(theta),
                         0,
                         radius * sin(theta));
        circle->position(radius * cos(theta - Math::PI / accuracy),
                         0,
                         radius * sin(theta - Math::PI / accuracy));
        circle->position((radius - thickness) * cos(theta - Math::PI / accuracy),
                         0,
                         (radius - thickness) * sin(theta - Math::PI / accuracy));
        circle->position((radius - thickness) * cos(theta),
                         0,
                         (radius - thickness) * sin(theta));
        // Join the 4 vertices created above to form a quad.
        circle->quad(point_index, point_index + 1, point_index + 2, point_index + 3);
        point_index += 4;
    }

    circle->end();

    scene_mgr->getRootSceneNode()->createChildSceneNode()->attachObject(circle);{CODE}

!!!Remark
If there are problems, you can try the [http://www.ogre3d.org/wiki/index.php?title=Circle&oldid=15066|old version].

This was not optimal, because in the old code the count of points (what is a value for accuracy / smoothness) was dependend to the radius. Now the current code should be fine and the count of points is independend of radius. The radius doesn't matter at quality.

With the code of '' Circle line with certain thickness'' I'm not sure, if it is ok. I didn't test it, because I'm a ((MOGRE|Mogre)) user. If __both__ snippits works, please remove this remark. --((User:Beauty|Beauty))

!!!References
This code was based on these 3 articles:

* ((ManualObject)) which shows how to draw a square as an example of using the ManualObject class
* ((Line 3D)) which shows how to use the ManualObject class to draw a line, but also, shows how to render lines in version of Ogre previous to 1.2
* [http://www.ogre3d.org/docs/api/html/classOgre_1_1ManualObject.html|ManualObject Ogre Reference]

        

History

Information Version
Sun 31 of Jul, 2011 23:33 GMT-0000 jacmoe 7
Sun 30 of Jan, 2011 12:53 GMT-0000 Beauty added link 6
Wed 15 of Sep, 2010 09:54 GMT-0000 Beauty added link to ((DynamicLineDrawing)) 5
Sat 02 of Jan, 2010 04:15 GMT-0000 jacmoe 4
Thu 24 of Dec, 2009 16:59 GMT-0000 jacmoe 3
Thu 24 of Dec, 2009 16:58 GMT-0000 jacmoe 2
Sun 14 of Dec, 2008 11:49 GMT-0000 Spacegaier [[Circle]] moved to [[Circle3D]]: accord it to Line3D 1