Displaying 2D Backgrounds

From Ogre Wiki

Jump to: navigation, search

A frequently asked question is how to display a 2D background image in OGRE. Here is an example of how to achieve this using the Rectangle2D class. This also demonstrates e.g. how to easily scroll the background.

// Create background material
MaterialPtr material = MaterialManager::getSingleton().create("Background", "General");
material->getTechnique(0)->getPass(0)->createTextureUnitState("rockwall.tga");
material->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false);
material->getTechnique(0)->getPass(0)->setLightingEnabled(false);

// Create background rectangle covering the whole screen
Rectangle2D* rect = new Rectangle2D(true);
rect->setCorners(-1.0, 1.0, 1.0, -1.0);
rect->setMaterial("Background");

// Render the background before everything else
rect->setRenderQueueGroup(RENDER_QUEUE_BACKGROUND);

// Hacky, but we need to set the bounding box to something big
// NOTE: If you are using Eihort, please see the note below on setting the bounding box
rect->setBoundingBox(AxisAlignedBox(-100000.0*Vector3::UNIT_SCALE, 100000.0*Vector3::UNIT_SCALE));

// Attach background to the scene
SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Background");
node->attachObject(rect);

// Example of background scrolling
material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setScrollAnimation(-0.25, 0.0);

Note: You cannot add the 2D Rectangle directly to the root scene node. It will not show up.

Note on bounding box in Eihort: In Eihort bounding boxes can now be infinite, which means that an object with an infinite bounding box will always be visible. This is practical in connection with Rectangle2D and replaces the hack of setting the bounding box to something very big. Instead just do this when setting the bounding box:

// Use infinite AAB to always stay visible
AxisAlignedBox aabInf;
aabInf.setInfinite();
rect->setBoundingBox(aabInf);

Here is the port to MOGRE:

// Create background material
MaterialPtr material = MaterialManager.Singleton.Create("Background", "General");
material.GetTechnique(0).GetPass(0).CreateTextureUnitState("rockwall.tga");
material.GetTechnique(0).GetPass(0).DepthCheckEnabled = false;
material.GetTechnique(0).GetPass(0).DepthWriteEnabled = false;
material.GetTechnique(0).GetPass(0).LightingEnabled = false;

// Create background rectangle covering the whole screen
Rectangle2D rect = new Rectangle2D(true);
rect.SetCorners(-1.0f, 1.0f, 1.0f, -1.0f);
rect.SetMaterial("Background");

// Render the background before everything else
rect.RenderQueueGroup = (byte)RenderQueueGroupID.RENDER_QUEUE_BACKGROUND;

// Use infinite AAB to always stay visible
AxisAlignedBox aab = new AxisAlignedBox();
aab.SetInfinite();
rect.BoundingBox = aab;

// Attach background to the scene
SceneNode node = mSmgr.RootSceneNode.CreateChildSceneNode("Background");
node.AttachObject(rect);

// Example of background scrolling
material.GetTechnique(0).GetPass(0).GetTextureUnitState(0).SetScrollAnimation(-0.25f, 0.0f);

See also

Personal tools
administration