hey,guys,又上来贴代码啦。
继续OGRE的学习。废话少说看代码。
1 #include <CEGUI/CEGUISystem.h>
2 #include <CEGUI/CEGUISchemeManager.h>
3 #include <OgreCEGUIRenderer.h>
4
5 #include "ExampleApplication.h"
6
7 class MouseQueryListener : public ExampleFrameListener, public OIS::MouseListener
8 {
9 public:
10
11 MouseQueryListener(RenderWindow* win, Camera* cam, SceneManager *sceneManager, CEGUI::Renderer *renderer)
12 : ExampleFrameListener(win, cam, false, true), mGUIRenderer(renderer)
13 {
14 mCount = 0;
15 mCurrentObject = NULL;
16 mLMouseDown = false;
17 mRMouseDown = false;
18 mSceneMgr = sceneManager;
19
20 // Reduce move speed
21 mMoveSpeed = 50;
22 mRotateSpeed /= 500;
23
24 // Register this so that we get mouse events.
25 mMouse->setEventCallback(this);
26
27 // Create RaySceneQuery
28 mRaySceneQuery = mSceneMgr->createRayQuery(Ray());
29
30 } // MouseQueryListener
31
32 ~MouseQueryListener()
33 {
34 // We created the query, and we are also responsible for deleting it.
35 mSceneMgr->destroyQuery(mRaySceneQuery);
36
37 }
38
39 bool frameStarted(const FrameEvent &evt)
40 {
41 // Process the base frame listener code. Since we are going to be
42 // manipulating the translate vector, we need this to happen first.
43 if (!ExampleFrameListener::frameStarted(evt))
44 return false;
45
46 // 我们的目标及时找到摄像机的当前位置,并沿着它向地面发射一条射线。这被称为射线场景查询,它会告诉我们我们下面的地面的高度。
47 // Setup the scene query
48 Vector3 camPos = mCamera->getPosition();
49 Ray cameraRay(Vector3(camPos.x, 5000.0f, camPos.z), Vector3::NEGATIVE_UNIT_Y);
50 mRaySceneQuery->setRay(cameraRay);
51
52 // 现在我们需要执行查询,得到结果。查询结果是std::iterator类型的。
53 // Perform the scene query
54 RaySceneQueryResult &result = mRaySceneQuery->execute();
55 RaySceneQueryResult::iterator itr = result.begin();
56
57 // Get the results, set the camera height
58 if (itr != result.end() && itr->worldFragment)
59 {
60 Real terrainHeight = itr->worldFragment->singleIntersection.y;
61 if ((terrainHeight + 10.0f) > camPos.y)
62 mCamera->setPosition( camPos.x, terrainHeight + 10.0f, camPos.z );
63 }
64
65 return true;
66
67 }
68
69 /* MouseListener callbacks. */
70 bool mouseMoved(const OIS::MouseEvent &arg)
71 {
72 // Update CEGUI with the mouse motion
73 CEGUI::System::getSingleton().injectMouseMove(arg.state.X.rel, arg.state.Y.rel);
74
75 return true;
76 }
77
78 bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
79 {
80 // Left mouse button down
81 if (id == OIS::MB_Left)
82 {
83 // Setup the ray scene query, use CEGUI's mouse position
84 CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
85 Ray mouseRay = mCamera->getCameraToViewportRay(mousePos.d_x/float(arg.state.width), mousePos.d_y/float(arg.state.height));
86 mRaySceneQuery->setRay(mouseRay);
87
88 // 我们不想让机器人悬在空中的哈
89 // Execute query
90 RaySceneQueryResult &result = mRaySceneQuery->execute();
91 RaySceneQueryResult::iterator itr = result.begin( );
92
93 // Get results, create a node/entity on the position
94 if (itr != result.end() && itr->worldFragment)
95 {
96 char name[16];
97 sprintf( name, "Robot%d", mCount++ );
98 Entity *ent = mSceneMgr->createEntity(name, "robot.mesh");
99 mCurrentObject = mSceneMgr->getRootSceneNode()->createChildSceneNode(String(name) + "Node", itr->worldFragment->singleIntersection);
100 mCurrentObject->attachObject(ent);
101 mCurrentObject->setScale(0.1f, 0.1f, 0.1f);
102 } // if
103
104 mLMouseDown = true;
105 } // if
106
107 // Right mouse button down
108 else if (id == OIS::MB_Right)
109 {
110 CEGUI::MouseCursor::getSingleton().hide();
111 mRMouseDown = true;
112 } // else if
113
114
115 // If we are dragging the left mouse button.
116 if (mLMouseDown)
117 {
118 } // if
119
120 // If we are dragging the right mouse button.
121 else if (mRMouseDown)
122 {
123 mCamera->yaw(Degree(-arg.state.X.rel * mRotateSpeed));
124 mCamera->pitch(Degree(-arg.state.Y.rel * mRotateSpeed));
125 } // else if
126
127
128
129 return true;
130 }
131
132 bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
133 {
134 // Left mouse button up
135 if (id == OIS::MB_Left)
136 {
137 mLMouseDown = false;
138 } // if
139
140 // Right mouse button up
141 else if (id == OIS::MB_Right)
142 {
143 CEGUI::MouseCursor::getSingleton().show();
144 mRMouseDown = false;
145 } // else if
146
147 return true;
148 }
149
150
151 protected:
152 RaySceneQuery *mRaySceneQuery; // The ray scene query pointer
153 bool mLMouseDown, mRMouseDown; // True if the mouse buttons are down
154 int mCount; // The number of robots on the screen
155 SceneManager *mSceneMgr; // A pointer to the scene manager
156 SceneNode *mCurrentObject; // The newly created object
157 CEGUI::Renderer *mGUIRenderer; // CEGUI renderer
158 };
159
160 class MouseQueryApplication : public ExampleApplication
161 {
162 protected:
163 CEGUI::OgreCEGUIRenderer *mGUIRenderer;
164 CEGUI::System *mGUISystem; // cegui system
165 public:
166 MouseQueryApplication()
167 {
168 }
169
170 ~MouseQueryApplication()
171 {
172 }
173 protected:
174 void chooseSceneManager(void)
175 {
176 // Use the terrain scene manager.
177 mSceneMgr = mRoot->createSceneManager(ST_EXTERIOR_CLOSE);
178 }
179
180 void createScene(void)
181 {
182 // Set ambient light
183 mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
184 mSceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8);
185
186 // World geometry
187 mSceneMgr->setWorldGeometry("terrain.cfg");
188
189 // Set camera look point
190 mCamera->setPosition(40, 100, 580);
191 mCamera->pitch(Degree(-30));
192 mCamera->yaw(Degree(-45));
193
194 // CEGUI setup
195 mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
196 mGUISystem = new CEGUI::System(mGUIRenderer);
197
198 // Mouse
199 CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
200 CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
201
202 }
203
204 void createFrameListener(void)
205 {
206 mFrameListener = new MouseQueryListener(mWindow, mCamera, mSceneMgr, mGUIRenderer);
207 mFrameListener->showDebugOverlay(true);
208 mRoot->addFrameListener(mFrameListener);
209 }
210 };
211
212
213 #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
214 #define WIN32_LEAN_AND_MEAN
215 #include "windows.h"
216
217 INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
218 #else
219 int main(int argc, char **argv)
220 #endif
221 {
222 // Create application object
223 MouseQueryApplication app;
224
225 try {
226 app.go();
227 } catch(Exception& e) {
228 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
229 MessageBox(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
230 #else
231 fprintf(stderr, "An exception has occurred: %s\n",
232 e.getFullDescription().c_str());
233 #endif
234 }
235
236 return 0;
237 }
238
还是有键盘输入的问题,不知道为什么这次使用非缓冲输入点击右键旋转摄像机只响应一次?有高手帮忙解答一下!
12.18 问题已解决.it's an easy question