Skip to main content

History: Displaying 2D Backgrounds

Source of version: 12

Copy to clipboard
            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 [http://www.ogre3d.org/docs/api/html/classOgre_1_1Rectangle2D.html|Rectangle2D] class. This also demonstrates e.g. how to easily scroll the background.

{CODE(wrap="1", colors="c++")}// 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);

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

// 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);{CODE}
__Note:__ You cannot add the 2D Rectangle directly to the root scene node. It will not show up.

Here is the port to ((MOGRE)):
{CODE(wrap="1", colors="c++")}// 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);{CODE}

!!See also
* [http://www.ogre3d.org/docs/api/html/structOgre_1_1Rectangle.html|Rectangle] in Ogre API
* {LEX()}Overlay{LEX}
* {LEX()}Material{LEX}
* {LEX()}AABB{LEX}
---
Alias: (alias(Displaying_2D_Backgrounds))
        

History

Information Version
Sat 29 of Aug, 2020 01:05 GMT-0000 sercero 14
Fri 22 of Jun, 2012 12:39 GMT-0000 scrawl delete 13
Wed 20 of Jun, 2012 16:20 GMT-0000 scrawl remove the Eihort note 12
Tue 09 of Nov, 2010 11:54 GMT-0000 spacegaier corrected link markup 11
Tue 09 of Nov, 2010 11:52 GMT-0000 spacegaier Lexicon fixes, Ogre API links and minor tweaks 10
Sun 04 of Jul, 2010 17:52 GMT-0000 JustBoo 9
Wed 30 of Jun, 2010 22:49 GMT-0000 JustBoo 8
Wed 30 of Jun, 2010 22:48 GMT-0000 JustBoo 7
Wed 30 of Jun, 2010 22:47 GMT-0000 JustBoo 6
Wed 30 of Jun, 2010 22:46 GMT-0000 JustBoo 5
Wed 30 of Jun, 2010 22:32 GMT-0000 JustBoo 4
Sat 02 of Jan, 2010 04:17 GMT-0000 jacmoe 3
Thu 24 of Dec, 2009 16:59 GMT-0000 jacmoe 2
Wed 08 of Jul, 2009 15:29 GMT-0000 Beauty linked words + added link to Ogre API 1