MOGRE Line 3D        

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 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.


Create material:
(call only once)

// create resource group
String resourceGroupName = "debugger";
if (ResourceGroupManager.Singleton.ResourceGroupExists(resourceGroupName) == false)

// create material (colour)
MaterialPtr moMaterial = MaterialManager.Singleton.Create("line_material", resourceGroupName);
moMaterial.ReceiveShadows = false;
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

Create and attach line object:

// 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

// create SceneNode and attach the line
SceneNode moNode = Smgr.RootSceneNode.CreateChildSceneNode("line_node");

If you want to remove the line you can do it like this:

// destroy the line
// destroy the SceneNode (or keep it to add other manual objects)

// alternatively just hide, if you want to use the line same again

See also

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.)