Ogre1.7 cookbook_ogrewin32application

Source: Internet
Author: User

Environment configuration: win32application

Under the DEBUG directory of the new project, you must:

Ogre. cfg

Resources_d.cfg

Plugins_d.cfg

Ogremain_d.dll

Rendersystem_direct3d9_d.dll (rendersystem_gl_d.dll)

Plugin_participant fx_d.dll

 

# PragmaOnce# Include"Ogre. h"ClassCengine {Public: Cengine (hwnd );~Cengine ();Public: Ogre: Root*M_root; ogre: scenemanager*M_scenemgr; ogre: renderwindow*M_renderwindow; ogre: Camera*M_camera ;};
# Include "  Stdafx. h "  # Include  "  Engine. h  "  # Include < String > Cengine: cengine (hwnd) {tchar szpath [max_path]; getmodulefilename (null, szpath, max_path); STD ::  String  PATH (szpath );  Int Pos = path. find_last_of ( "  \\  " );  If (Pos! = STD :: String  : NPOs) {path = Path. substr ( 0  , POS); Path + = "  \\  "  ;} M_root = New Ogre: Root ( "" , "" , Ogre: string (PATH) + ogre: string ( " Ogrewin32.log  "  ); Ogre: configfile ogreconfigfile; ogreconfigfile. Load (Ogre: string (PATH) + Ogre: string ( "  Ogre. cfg  "  ); Ogre: String rendersystemname = Ogreconfigfile. getsetting ( "  Render system  "  );  If (Rendersystemname = " Direct3D9 rendering Subsystem  "  ) {# Ifdef _ debug m_root -> Loadplugin ( "  Rendersystem_direct3d9_d  "  );  # Else  M_root -> Loadplugin ( "  Rendersystem_direct3d9  "  );  # Endif  } Else   If (Rendersystemname = "  OpenGL rendering Subsystem  "  ) {# Ifdef _ debug m_root -> Loadplugin ( "  Rendersystem_gl_d  "  );  # Else  M_root -> Loadplugin ( "  Rendersystem_gl  " );  # Endif  }  Else  {# Ifdef _ debug m_root -> Loadplugin ( "  Rendersystem_direct3d9_d  "  );  # Else  M_root -> Loadplugin ( "  Rendersystem_direct3d9  "  ); # Endif  } # Ifdef _ debug m_root -> Loadplugin ( "  Plugin_participant fx_d  "  );  # Else  M_root -> Loadplugin ( "  Plugin_participant FX  "  );  # Endif  Ogre: rendersystemlist renderlist = M_root->Getavailablerenderers (); m_root -> Setrendersystem (renderlist [ 0  ]); M_root -> Initialise ( False  ); M_scenemgr = M_root-> createscenemanager (Ogre: st_generic, "  Ogrewin32  "  ); Ogre: namevaluepairlist  Params  ;  Params [ " External?whandle  " ] = Ogre: stringconverter: tostring (( Long  ) Hwnd );  Params [ "  Vsync  " ] = "  True  "  ; Rect; getclientrect (hwnd, & Rect); ogre: rendertarget * Renderwindow =NULL;  Try  {M_renderwindow = M_root-> createrenderwindow ( "  Ogrewin32  " , Rect. Right- Rect. Left, rect. Bottom -Rect. Top, False ,& Params  );}  Catch  (...) {MessageBox (hwnd,  " Failed create ogre render window  " , "  Initialize rendr System  "  , Mb_ OK | Mb_iconstop); exit (exit_success);} ogre: configfile CF; Cf. Load (Ogre: string (PATH) + Ogre: string ( "  Resources_d.cfg  "  ); Ogre: configfile: sectioniterator SECI =Cf. getsectioniterator (); ogre: String secname, typename, archname;  While  (SECI. hasmoreelements () {secname = SECI. peeknextkey (); ogre: configfile: settingsmultimap * Settings = SECI. getnext (); ogre: configfile: settingsmultimap: iterator ite;  For (ITE = settings-> begin (); ite! = Settings-> end (); ++ ITE) {typename = Ite-> First; archname = Ite->Second; ogre: resourcegroupmanager: getsingleton (). addresourcelocation (archname, typename, secname) ;}} ogre: resourcegroupmanager: getsingleton (). Submit (); m_camera = M_scenemgr-> createcamera ( "  Ogrewin32 camera  "  ); M_camera -> Setnearclipdistance ( 0.5  ); M_camera -> Setfarclipdistance ( 5000  ); M_camera -> Setcastshadows ( False ); M_camera -> Setuserenderingdistance ( True  ); M_camera -> Setposition (Ogre: vector3 ( 200.0 , 50.0 , 100.0  ); Ogre: scenenode * Camernode = NULL; camernode = M_scenemgr-> getrootscenenode ()-> createchildscenenode ( "  Camer Node  "  ); Ogre: viewport * Viewport = NULL;  If ( 0 = M_renderwindow-> Getnumviewports () {viewport = M_renderwindow-> Addviewport (m_camera); viewport -> Setbackgroundcolour (Ogre: colourvalue ( 0.8 , 1.0 , 0.8  );} M_camera -> Setautoaspectratio ( True ); Ogre: meshmanager: getsingleton (). createplane (  "  Floor  "  , Ogre: resourcegroupmanager: default_resource_group_name, ogre: plane (Ogre: vector3: unit_y, - 30 ), 1000 , 1000 , 10 , 10 , True , 1 , 8 , 8  , Ogre: vector3: unit_z );  //  Create a floor entity, give it a material, and place it at the origin Ogre: entity * floor = m_scenemgr-> createentity ( "  Floor  " , "  Floor  "  ); Floor -> Setmaterialname ( "  Examples/bumpymetal "  ); M_scenemgr -> Getrootscenenode ()-> Attachobject (floor); ogre: Entity * Entity = m_scenemgr-> createentity ( "  Sinbad  " , "  Sinbad. Mesh  "  ); Ogre: scenenode * Sinbadnode = m_scenemgr-> getrootscenenode ()-> Createchildscenenode (); sinbadnode -> Attachobject (entity); sinbadnode -> Scale ( 10.0f , 10.0f , 10.0f  ); Sinbadnode -> Setposition (Ogre: vector3 ( 0 , 30 , 0  ); Ogre: axisalignedbox robotbox = Entity-> Getboundingbox (); ogre: vector3 Center = Robotbox. getcenter (); m_camera -> Lookat (center); m_scenemgr -> Setambientlight (Ogre: colourvalue ( 0.3 , 0.3 , 0.3  ); Ogre: Light * Light = m_scenemgr-> Createlight (); light -> Settype (Ogre: Light: lt_point); light -> Setposition (- 10 , 40 , 20  ); Light -> Setspecularcolour (Ogre: colourvalue: White);} cengine :: ~Cengine (){  //  Clear scene ..      If  (M_scenemgr) m_scenemgr -> Clearscene ();  //  Unload resource .. Ogre: resourcegroupmanager: resourcemanageriterator resmgrs = Ogre: resourcegroupmanager: getsingleton (). getresourcemanageriterator ();  While  (Resmgrs. hasmoreelements () {resmgrs. getnext () ->Unloadunreferencedresources ();}  If  (M_scenemgr) {m_root -> Destroyscenemanager (m_scenemgr); m_scenemgr = 0  ;}  If  (M_renderwindow) {m_renderwindow -> Removeallviewports ();}  If  (M_root) {Delete m_root; m_root = 0 ;}} 

//////////////////////////////////////// ////////////////////////

Cengine * pengine = NULL;
Void initengine (hwnd );
Void closeengine ();

//////////////////////////////////////// //////////////////////////

Bool initinstance (hinstance, int ncmdshow)
{
Hwnd;

Hinst = hinstance; // store the instance handle in a global variable

Hwnd = createwindow (szwindowclass, sztitle, ws_overlappedwindow,
Cw_usedefault, 0, cw_usedefault, 0, null, null, hinstance, null );

If (! Hwnd)
{
Return false;
}

Initengine (hwnd );

Showwindow (hwnd, ncmdshow );
Updatewindow (hwnd );

 

Return true;
}

//////////////////////////////////////// ///

Case wm_paint:
HDC = beginpaint (hwnd, & PS );
// Todo: add any drawing hereCode...
Pengine-> m_root-> renderoneframe ();
Endpaint (hwnd, & PS );
Break;
Case wm_destroy:
Postquitmessage (0 );

Closeengine ();

Break;

//////////////////////////////////////// ///////

Void initengine (hwnd)
{
Pengine = new cengine (hwnd );
}

Void closeengine ()
{
If (pengine)
{
Delete pengine;
Pengine = NULL;
}
}

Note:

After adding the. mesh to a node for the first time, only the model is drawn, but there is no skin, because the material is not properly loaded.

To load materials correctly, ogre: resourcegroupmanager: getsingleton (). initialiseallresourcegroups ();

Must be called.

In addition, locateresource (read resources_d.cfg) and the above loadresource must be called after the Ogre: rendertarget * renderwindow is created.

In addition, the DLL plugin_participant fx_d.dll is required, which is used in initialiseallresourcegroups.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.