Skip to main content

History: MOGRE Line 3D

Source of version: 8 (current)

Copy to clipboard
            This is a fine code snippet to create a line in 3D space. Independent of distance to the camera it's always to see with one pixel width (unless there is no object between).

Regard that you call this code only once and not every frame. If you need moving a line adapt the snippet. 

The material can be reused. Also the ((ManualObject|ManualObject)) can be reused (for that call ''manOb.Clear()''). 

For this you have to declare the objects in the class and assign/update them in a method.

If you need more than one line, regard to create ManualObjects and SceneNodes with different name.

The code is a port of ((Line 3D)), what was created by DWORD and jacmoe.

!!Code

Create material:
(call only once)
{CODE(wrap="1", colors="c#")}
// create resource group
String resourceGroupName = "debugger";
if (ResourceGroupManager.Singleton.ResourceGroupExists(resourceGroupName) == false)
    ResourceGroupManager.Singleton.CreateResourceGroup(resourceGroupName);

// create material (colour)
MaterialPtr moMaterial = MaterialManager.Singleton.Create("line_material", resourceGroupName);
moMaterial.ReceiveShadows = false;
moMaterial.GetTechnique(0).SetLightingEnabled(true);
moMaterial.GetTechnique(0).GetPass(0).SetDiffuse(0, 0, 1, 0);
moMaterial.GetTechnique(0).GetPass(0).SetAmbient(0, 0, 1);
moMaterial.GetTechnique(0).GetPass(0).SetSelfIllumination(0, 0, 1);
moMaterial.Dispose();  // dispose pointer, not the material
{CODE}

Create and attach line object:
{CODE(wrap="1", colors="c#")}
// create line object
ManualObject manOb = Smgr.CreateManualObject("line");
manOb.Begin("line_material", RenderOperation.OperationTypes.OT_LINE_LIST);
manOb.Position(30, 20, 10);
manOb.Position(40, 10, 0);
// ... maybe more points
manOb.End();

// create SceneNode and attach the line
SceneNode moNode = Smgr.RootSceneNode.CreateChildSceneNode("line_node");
moNode.SetPosition(Vector3.ZERO);
moNode.AttachObject(manOb);{CODE}

If you want to remove the line you can do it like this:
{CODE(wrap="1", colors="c#")}// destroy the line
Smgr.DestroyManualObject("line"); 
                 
// destroy the SceneNode (or keep it to add other manual objects)
Smgr.DestroySceneNode("line_node");                

// alternatively just hide, if you want to use the line same again
Smgr.GetSceneNode("line_node").SetVisible(false);  
{CODE}

%%%
!!See also

* ((Line 3D))
* ((ManualObject))
* ((ManualObject 2D))
* ((Circle3D))
* ((Create Tetrahedron with MOGRE))
* ((DynamicLineDrawing)) - create a line by the class ''SimpleRenderable'', which can be modified with better performance
* ((Ogre Line Chart))
* Forum topic: [http://www.ogre3d.org/addonforums/viewtopic.php?f=8&t=11506|Rendering lines with thickness -- using BillboardChain] (Mogre)
* Forum topic: [http://www.ogre3d.org/addonforums/viewtopic.php?f=8&t=11503|DynamicLines - Mogre version]
* ((Debug Drawing Utility Class))
** [http://www.ogre3d.org/addonforums/viewtopic.php?f=8&t=14554|Mogre port] of Debug Drawing Utility Class

Note: If somebody ports some C++ code to C#, please publish it. (Just make a quick paste to the Mogre forum and we will add it to the wiki.)