The background color is implemented through the setbackgroundcolour () member function in the viewport class in ogre. However, this class is only available for single-color settings !!
However, we can use indirect methods to create the gradient background color. the following details:
1. restructured the Ogre: rectangle2d class :( ogrecolourrectangle2d. h)
# Ifndef ogrecolouredrectangle2d_h
# Define ogrecolouredrectangle2d_h
# Include "ogrerectangle2d. H"
Class colouredrectangle2d: Public ogre: rectangle2d
{
Public:
Colouredrectangle2d (bool includetexturecoordinates = false );
~ Colouredrectangle2d ();
Void setcolours (const ogre: colourvalue & topleft, const ogre: colourvalue & bottomleft, const ogre: colourvalue & topright, const ogre: colourvalue & bottomright );
};
# Endif // ogrecolouredrectangle2d_h
2. Implementation of the refactoring class: (ogrecolourrectangle2d. cpp)
# Include "ogrecolouredrectangle2d. H"
# Include "ogrehardwarebuffermanager. H"
// # Define position_binding 0
// # Define normal_binding 1
// # Define texcoord_binding 2
# Define colour_binding 3
Colouredrectangle2d: colouredrectangle2d (bool includetexturecoordinates/* = false */): ogre: rectangle2d (includetexturecoordinates)
{
Ogre: vertexdeclaration * Decl = mrenderop. vertexdata-> vertexdeclaration;
Decl-> addelement (colour_binding, 0, ogre: vet_colour, ogre: ves_diffuse );
Ogre: vertexbufferbinding * bind = mrenderop. vertexdata-> vertexbufferbinding;
Ogre: hardwarevertexbuffersharedptr vbuf =
Ogre: hardwarebuffermanager: getsingleton (). createvertexbuffer (
Decl-> getvertexsize (colour_binding ),
Mrenderop. vertexdata-> vertexcount,
Ogre: hardwarebuffer: hbu_static_write_only );
// Bind Buffer
Bind-> setbinding (colour_binding, vbuf );
}
Colouredrectangle2d ::~ Colouredrectangle2d ()
{
}
Void resume: setcolours (const ogre: colourvalue & topleft, const ogre: colourvalue & bottomleft, const ogre: colourvalue & topright, const ogre: colourvalue & bottomright)
{
Ogre: hardwarevertexbuffersharedptr vbuf =
Mrenderop. vertexdata-> vertexbufferbinding-> getbuffer (colour_binding );
Unsigned int * puint32 = static_cast <unsigned int *> (vbuf-> lock (Ogre: hardwarebuffer: hbl_discard ));
Const ogre: vertexelementtype srctype = ogre: vertexelement: getbestcolourvertexelementtype ();
* Puint32 ++ = ogre: vertexelement: convertcolourvalue (topleft, srctype );
* Puint32 ++ = ogre: vertexelement: convertcolourvalue (bottomleft, srctype );
* Puint32 ++ = ogre: vertexelement: convertcolourvalue (topright, srctype );
* Puint32 ++ = ogre: vertexelement: convertcolourvalue (bottomright, srctype );
Vbuf-> unlock ();
}
3. refactoring applications
// Create background material
Ogre: materialptr material = ogre: materialmanager: getsingleton (). Create ("background", "general ");
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
Colouredrectangle2d * rect = new colouredrectangle2d ();
Rect-> setcorners (-1.0, 1.0, 1.0,-1.0 );
Rect-> setmaterial ("background ");
// Set the colours
Rect-> setcolours (Ogre: colourvalue: Red, ogre: colourvalue: Green, ogre: colourvalue: blue, ogre: colourvalue: Black );
// Render the background before everything else
Rect-> setrenderqueuegroup (Ogre: render_queue_background );
// Use infinite Aab to always stay visible
Ogre: axisalignedbox aabinf;
Aabinf. setinfinite ();
Rect-> setboundingbox (aabinf );
// Attach background to the scene
Scenenode * node = mscenemgr-> getrootscenenode ()-> createchildscenenode ("background ");
Node-> attachobject (rect );
After completing the preceding three steps, you can implement a gradient background color of the viewport. The basic principle is: Create a 2D rectangle with a gradient color and render it as a background. Notes:
1. # define colour_binding 3, and the locations where colour_binding will be used in the future. Due to the differences in different ogre versions (I use version 1.7), some earlier versions of ogre do not implement the first three annotations in CPP, namely, position, normal, and texcoord, if you use colour, unexpected errors may occur. (The current mogre version does not implement three. Which of the following are not clear)
2. rect-> setrenderqueuegroup (Ogre: render_queue_background); this sentence is very important.
3. If you want to use a specific image as the background, it is much simpler: You can try it yourself if you are interested.