Code & comments for basic tutorial 4 in ogre3d Wiki
Copyright mikefeng QQ: 76848502
The code in this article comes from the basic tutorial 4 of the Ogre official website wiki. This Code uses the example framework provided by ogre to implement the following functions:
- Read a ninja Model
- A point light source is used to illuminate the model.
- Click the left mouse button to control the light switch
- Right-click and drag to change the camera direction
- Arrow keys control the camera position
- Processing of non-buffered Input
- Switch between the two cameras by pressing 1 or 2
Because ogre wiki does not provide complete and consistent code, I would like to add some comments in the code to help you. below is
Lighting Effect
Effect after turning off the light
Below is the code
/**/File demo. h/brief specialisation of Ogre's framework application to show the multi-camera and unbuffered input feature. */# include "exampleapplication. H "class demoframelistener: Public exampleframelistener {public: bool mmousedown; // whether or not the left mouse button was down last frame real mtoggle; // the time left until next toggle real mrotate; // The rotate constant real mmove; // t He movement constant implements * mscenemgr; // The current scenemanager scenenode * mcamnode; // The scenenode the camera is currently attached to // unbuffered keyboard and mouse input partition (renderwindow * win, camera * cam, scenemanager * scenemgr): exampleframelistener (Win, Cam, false, false) {// key and mouse state tracking mmousedown = false; mtoggle = 0.0; // populate the camera And Scene Manager containers // the first parent will be the pitchnode, // and the camnode which we are looking for is the parent of that node. mcamnode = cam-> getparentscenenode (); mscenemgr = scenemgr; // set the rotation and move speed mrotate = 0.13; mmove = 250 ;} bool framestarted (const frameevent & EVT) {using namespace OIS; // 1. capture the current state of the keybo ARD and mouse mmouse-> capture (); mkeyboard-> capture (); // If the framestarted method return false, // ogre will exit from the main render loop if (mkeyboard-> iskeydown (kc_escape) return false; // get the current left mouse button state bool currmouse = mmouse-> getmousestate (). buttondown (mb_left); // If the left mouse button is pressed, and in last frame // This button is up, we do the light Toggle .. If (currmouse &&! Mmousedown) {Light * Light = mscenemgr-> getlight ("light1"); light-> setvisible (! Light-> isvisible ();} // mmousedown is set for the next frame. mmousedown = currmouse; // mtoggle is the time counter for changing the camera action if (mtoggle> = 0.0f) mtoggle-= EVT. timesincelastframe; // If mtoggle is less than 0, and key '1' is pressed if (mtoggle <0.0f) & mkeyboard-> iskeydown (kc_1 )) {// set the toggle back to 1 // This means only after 1 second, the toggle action will be excuted mtoggle = 1.0f; // detach the camera form camnode2, and attach to camnode1 mcamera-> detachobject (mcamera); mcamnode = mscenemgr-> getscenenode ("camnode1"); mscenemgr-> getscenenode ("pitchnode1 ") -> attachobject (mcamera);} // If mtoggle is less than 0, and key '1' is pressed else if (mtoggle <0.0f) & mkeyboard-> iskeydown (kc_2) {mtoggle = 0.1f; // detach the camera form camnode1, and attach to camnode2 mcamera-> extract ()-> detachobject (mcamera ); mcamnode = mscenemgr-> getscenenode ("camnode2"); mscenemgr-> getscenenode ("pitchnode2")-> attachobject (mcamera);} // direction keys, used for moving the camera vector3 transvector = vector3: zero; If (mkeyboard-> iskeydown (kc_up) | mkeyboard-> iskeydown (kc_w) transvector. z-= mmove; If (mkeyboard-> iskeydown (kc_down) | mkeyboard-> iskeydown (kc_s) transvector. Z + = mmove; If (mkeyboard-> iskeydown (kc_left) | mkeyboard-> iskeydown (kc_a) transvector. x-= mmove; If (mkeyboard-> iskeydown (kc_right) | mkeyboard-> iskeydown (kc_d) transvector. X + = mmove; If (mkeyboard-> iskeydown (kc_pgup) | mkeyboard-> iskeydown (kc_q) transvector. Y + = mmove; If (mkeyboard-> iskeydown (kc_pgdown) | mkeyboard-> iskeydown (kc_e) transvector. y-= mmove; // mcamnode-> translate (mcamnode-> getorientation () * // transvector * EVT. timesincelastframe); // move the camera mcamnode-> translate (mcamnode-> getorientation () * mcamnode-> getchild (0)-> getorientation () * transvector * EVT. timesincelastframe); // change the camare ction according to the right mouse button if (mmouse-> getmousestate (). buttondown (mb_right) {mcamnode-> yaw (degree (-mrotate * mmouse-> getmousestate (). x. rel); mcamnode-> getchild (0)-> pitch (degree (-mrotate * mmouse-> getmousestate (). y. rel);} return true ;}}; class demoapplication: Public exampleapplication {public: demoapplication () {} protected: // just override the mandatory create scene method void choosescenemanager () {mscenemgr = mroot-> createscenemanager (listener);} // create the frame listener we defined above void createframelistener () {mframelistener = new listener (mwindow, mcamera, mscenemgr ); mroot-> addframelistener (mframelistener);} void createscene (void) {// set ambient light mscenemgr-> setambientlight (colourvalue (0.25, 0.25, 0.25 )); // create a light * Light = mscenemgr-> createlight ("light1"); light-> settype (light: lt_point ); light-> setposition (vector3 (250,150,250); light-> setdiffusecolour (colourvalue: White); light-> setspecularcolour (colourvalue: White ); // loading ninja mesh entity * ent = mscenemgr-> createentity ("ninja", "ninja. mesh "); scenenode * node = mscenemgr-> getrootscenenode ()-> createchildscenenode (" ninjanode "); node-> attachobject (ENT ); // create the scene node = mscenemgr-> getrootscenenode ()-> createchildscenenode ("camnode1", vector3 (-400,200,400 )); // make it look towards the ninja node-> yaw (degree (-45); // create the pitch node = node-> createchildscenenode ("pitchnode1 "); node> attachobject (mcamera); // create the second camera node/pitch node = mscenemgr-> getrootscenenode ()-> createchildscenenode ("camnode2", vector3 (0,200,400 )); node = node-> createchildscenenode ("pitchnode2 ");}};