Skip to main content

History: Create Tetrahedron with MOGRE

Source of version: 20 (current)

Copy to clipboard
            {DIV(align="right",float="right")}||{img src="img/wiki_up/Tetrahedron-turning-120px.gif" alt="Tetrahedron-turning-120px.gif"}
{img src="img/wiki_up/Tetrahedron.jpg" alt="" align="right"}||{DIV}
This is a method to create a tetrahedron as ((ManualObject)). 

For Ogre users it should be easy to port. 
If somebody do so, please publish the code (or send it to user ((User:Beauty|Beauty))).

It's going easy on resources (just 4 ((-vertex|vertices)) and 4 triangles) and usefull if you need many objects in your scene. For example to add marks at different positions where an airplane was flying along.

If there are problems with the code ask user ((User:Beauty|Beauty)).

Alternatively you can use ((-Particle))s. They can have nice visual effects and needs low performance (depends to its complexity).


!!!CreateTetrahedron()

{CODE(wrap="1", colors="c#")}/// <summary>
/// Create a tetrahedron with point of origin in middle of volume. 
/// It will be added to the SceneManager as ManualObject. The material must still exists.
/// </summary>
/// <param name="position">Position in scene</param>
/// <param name="scale">Size of the tetrahedron</param>
/// <param name="name">Name of the ManualObject that will be created</param>
/// <param name="materialName">Name of the used material</param>
///
void CreateTetrahedron(String name, Vector3 position, Single scale, String materialName)
{
    ManualObject manObTetra = new ManualObject(name);
    manObTetra.CastShadows = false;

    // render just before overlays (so all objects behind the transparent tetrahedron are visible)
    manObTetra.RenderQueueGroup = (byte)RenderQueueGroupID.RENDER_QUEUE_OVERLAY - 1; // = 99

    Vector3[] c = new Vector3[4]; // corners

    // calculate corners of tetrahedron (with point of origin in middle of volume)
    Single mbot = scale * 0.2f;      // distance middle to bottom
    Single mtop = scale * 0.62f;     // distance middle to top    
    Single mf   = scale * 0.289f;    // distance middle to front
    Single mb   = scale * 0.577f;    // distance middle to back
    Single mlr  = scale * 0.5f;      // distance middle to left right 
    //               width / height / depth
    c[0] = new Vector3(-mlr, -mbot,  mf);  // left bottom front
    c[1] = new Vector3( mlr, -mbot,  mf);  // right bottom front
    c[2] = new Vector3(   0, -mbot, -mb);  // (middle) bottom back
    c[3] = new Vector3(   0,  mtop,   0);  // (middle) top (middle)

    // add position offset for all corners (move tetrahedron)
    for (Int16 i = 0;   i <= 3;   i++)
        c[i] += position;

    // create bottom
    manObTetra.Begin(materialName, RenderOperation.OperationTypes.OT_TRIANGLE_LIST);
    manObTetra.Position(c[2]);
    manObTetra.Position(c[1]);
    manObTetra.Position(c[0]);
    manObTetra.Triangle(0, 1, 2);
    manObTetra.End();
    // create right back side
    manObTetra.Begin(materialName, RenderOperation.OperationTypes.OT_TRIANGLE_LIST);
    manObTetra.Position(c[1]);
    manObTetra.Position(c[2]);
    manObTetra.Position(c[3]);
    manObTetra.Triangle(0, 1, 2);
    manObTetra.End();
    // create left back side
    manObTetra.Begin(materialName, RenderOperation.OperationTypes.OT_TRIANGLE_LIST);
    manObTetra.Position(c[3]);
    manObTetra.Position(c[2]);
    manObTetra.Position(c[0]);
    manObTetra.Triangle(0, 1, 2);
    manObTetra.End();
    // create front side
    manObTetra.Begin(materialName, RenderOperation.OperationTypes.OT_TRIANGLE_LIST);
    manObTetra.Position(c[0]);
    manObTetra.Position(c[1]);
    manObTetra.Position(c[3]);
    manObTetra.Triangle(0, 1, 2);
    manObTetra.End();

} // CreateTetrahedron{CODE}



%%%
!Usage example

{CODE(wrap="1", colors="c#")}// a material with the name materialName must be created or loaded!

Vector3 position = new Vector3(1, 1, -1);
Single size = 1;
String tetraName = "tetra";

// create manual object
CreateTetrahedron(tetraName, position, size, materialName);

// attach to scene
mScene.Smgr.RootSceneNode.AttachObject(mScene.Smgr.GetManualObject(tetraName));{CODE}

{CODE(wrap="1", colors="c#")}// remove tetrahedrons
if (mScene.Smgr.HasManualObject(tetraName))
    mScene.Smgr.GetManualObject(tetraName).Clear();{CODE}


%%%
!See also

* ((MOGRE Line 3D)) - includes some code how to __create a material__ on the fly
* ((ManualObject))
* [http://www.ogre3d.org/docs/api/html/classOgre_1_1ManualObject.html#ManualObject|ManualObject API] - class reference with description
* ((Manual Object|CodeSnippets for ManualObject))
* ((-Material|Material))
* ((-Particle|Particle))
* ((Ogre Procedural Geometry Library)) - A library to quickly create geometric primitives
* ((GeometricMesh)) - Creating various geometric/geodesic shapes 
* ((Debug Drawing Utility Class))
** [http://www.ogre3d.org/addonforums/viewtopic.php?f=8&t=14554|Mogre port] of Debug Drawing Utility Class