Ogre 2011-12-06 - 완벽한 Sample 코드
38552 단어 sample
1 #include "ogre/Ogre.h"
2 #include "ois/OIS.h"
3
4 class MyFrameListener : public Ogre::FrameListener
5 {
6 public:
7 MyFrameListener(Ogre::RenderWindow* win, Ogre::Camera* cam, Ogre::Viewport* vp,
8 Ogre::SceneNode* node, Ogre::Entity* ent)
9 {
10 camera_ = cam;
11 movementSpeed_ = 50.0f;
12 vp_ = vp;
13
14 node_ = node;
15 walkSpeed_ = 50.0f;
16
17 size_t winHandle = 0;
18 std::ostringstream winHandleStr;
19 OIS::ParamList pl;
20 win->getCustomAttribute("WINDOW", &winHandle);
21 winHandleStr << winHandle;
22 pl.insert(make_pair("WINDOW", winHandleStr.str()));
23 inputMgr_ = OIS::InputManager::createInputSystem(pl);
24 keyBoard_ = static_cast<OIS::Keyboard*>(inputMgr_->createInputObject(OIS::OISKeyboard, false));
25 mouse_ = static_cast<OIS::Mouse*>(inputMgr_->createInputObject(OIS::OISMouse, false));
26
27 bComp1On_ = false;
28 bComp2On_ = false;
29 bComp3On_ = false;
30 bDown1_ = false;
31 bDown2_ = false;
32 bDown3_ = false;
33
34 animStateRun_ = ent->getAnimationState("RunBase");
35 animStateRun_->setLoop(false);
36 animStateTop_ = ent->getAnimationState("RunTop");
37 animStateTop_->setLoop(false);
38 animStateJump_ = ent->getAnimationState("JumpStart");
39 animStateJump_->setLoop(false);
40 }
41
42 ~MyFrameListener()
43 {
44 inputMgr_->destroyInputObject(keyBoard_);
45 inputMgr_->destroyInputObject(mouse_);
46 OIS::InputManager::destroyInputSystem(inputMgr_);
47 }
48
49 // false
50 bool frameStarted(const Ogre::FrameEvent& evt)
51 {
52 keyBoard_->capture();
53 if (keyBoard_->isKeyDown(OIS::KC_ESCAPE))
54 return false;
55
56 Ogre::Vector3 translate(0, 0, 0);
57 if (keyBoard_->isKeyDown(OIS::KC_W))
58 translate += Ogre::Vector3(0, 0, -1);
59 if (keyBoard_->isKeyDown(OIS::KC_S))
60 translate += Ogre::Vector3(0, 0, 1);
61 if (keyBoard_->isKeyDown(OIS::KC_A))
62 translate += Ogre::Vector3(-1, 0, 0);
63 if (keyBoard_->isKeyDown(OIS::KC_D))
64 translate += Ogre::Vector3(1, 0, 0);
65 camera_->moveRelative(translate * evt.timeSinceLastFrame * movementSpeed_);
66
67 mouse_->capture();
68 float rotX = mouse_->getMouseState().X.rel * evt.timeSinceLastFrame * -1;
69 float rotY = mouse_->getMouseState().Y.rel * evt.timeSinceLastFrame * -1;
70 camera_->yaw(Ogre::Radian(rotX));
71 camera_->pitch(Ogre::Radian(rotY));
72
73 if (keyBoard_->isKeyDown(OIS::KC_1) && !bDown1_)
74 {
75 bDown1_ = true;
76 bComp1On_ = !bComp1On_;
77 Ogre::CompositorManager::getSingleton().setCompositorEnabled(vp_, "Compositor1", bComp1On_);
78 }
79 if (keyBoard_->isKeyDown(OIS::KC_2) && !bDown2_)
80 {
81 bDown2_ = true;
82 bComp2On_ = !bComp2On_;
83 Ogre::CompositorManager::getSingleton().setCompositorEnabled(vp_, "Compositor2", bComp2On_);
84 }
85 if (keyBoard_->isKeyDown(OIS::KC_3) && !bDown3_)
86 {
87 bDown3_ = true;
88 bComp3On_ = !bComp3On_;
89 Ogre::CompositorManager::getSingleton().setCompositorEnabled(vp_, "Compositor3", bComp3On_);
90 }
91 if (!keyBoard_->isKeyDown(OIS::KC_1))
92 bDown1_ = false;
93 if (!keyBoard_->isKeyDown(OIS::KC_2))
94 bDown2_ = false;
95 if (!keyBoard_->isKeyDown(OIS::KC_3))
96 bDown3_ = false;
97
98 // move the node_
99 bool walked = false;
100 Ogre::Vector3 sinBadTranslate(0, 0, 0);
101 float rotation = 0;
102 if (keyBoard_->isKeyDown(OIS::KC_UP))
103 {
104 sinBadTranslate += Ogre::Vector3(0, 0, -1);
105 rotation = 3.14f;
106 walked = true;
107 }
108 if (keyBoard_->isKeyDown(OIS::KC_DOWN))
109 {
110 sinBadTranslate+= Ogre::Vector3(0, 0, 1);
111 rotation = 0.0f;
112 walked = true;
113 }
114 if (keyBoard_->isKeyDown(OIS::KC_LEFT))
115 {
116 sinBadTranslate+= Ogre::Vector3(-1, 0, 0);
117 rotation = -1.57f;
118 walked = true;
119 }
120 if (keyBoard_->isKeyDown(OIS::KC_RIGHT))
121 {
122 sinBadTranslate+= Ogre::Vector3(1, 0, 0);
123 rotation = 1.57f;
124 walked = true;
125 }
126 node_->translate(sinBadTranslate * evt.timeSinceLastFrame * walkSpeed_);
127 node_->resetOrientation();
128 node_->yaw(Ogre::Radian(rotation));
129 camera_->moveRelative(sinBadTranslate * evt.timeSinceLastFrame * walkSpeed_);
130
131 // play animation
132 if (walked)
133 {
134 animStateRun_->setEnabled(true);
135 animStateTop_->setEnabled(true);
136 if (animStateRun_->hasEnded())
137 animStateRun_->setTimePosition(0.0f);
138 if (animStateTop_->hasEnded())
139 animStateTop_->setTimePosition(0.0f);
140
141 animStateRun_->addTime(evt.timeSinceLastFrame);
142 animStateTop_->addTime(evt.timeSinceLastFrame);
143 }
144 else
145 {
146 animStateRun_->setEnabled(false);
147 animStateTop_->setEnabled(false);
148 animStateRun_->setTimePosition(0.0f);
149 animStateTop_->setTimePosition(0.0f);
150 }
151
152 if (keyBoard_->isKeyDown(OIS::KC_SPACE))
153 {
154 animStateJump_->setEnabled(true);
155 if (animStateJump_->hasEnded())
156 animStateJump_->setTimePosition(0.0f);
157 animStateJump_->addTime(evt.timeSinceLastFrame);
158 }
159 else
160 animStateJump_->setEnabled(false);
161
162 return true;
163 }
164
165 // false
166 bool frameRenderingQueued(const Ogre::FrameEvent& evt)
167 {
168 return true;
169 }
170
171 // false
172 bool frameEnded(const Ogre::FrameEvent& evt)
173 {
174 return true;
175 }
176
177 private:
178 OIS::InputManager* inputMgr_;
179 OIS::Keyboard* keyBoard_;
180 OIS::Mouse* mouse_;
181 Ogre::Camera* camera_;
182 float movementSpeed_;
183
184 Ogre::Viewport* vp_;
185 // which compositor on
186 bool bComp1On_;
187 bool bComp2On_;
188 bool bComp3On_;
189 bool bDown1_;
190 bool bDown2_;
191 bool bDown3_;
192
193 //
194 float walkSpeed_;
195 Ogre::SceneNode* node_;
196
197 //
198 Ogre::AnimationState* animStateRun_;
199 Ogre::AnimationState* animStateTop_;
200 Ogre::AnimationState* animStateJump_;
201 };
202
203 class MyAppliaction
204 {
205 public:
206 MyAppliaction()
207 {
208 root_ = 0;
209 sceneMgr_ = 0;
210 frameListener_ = 0;
211 keepRunning_ = true;
212 sinBadEntity_ = 0;
213 }
214
215 ~MyAppliaction()
216 {
217 delete root_;
218 delete frameListener_;
219 }
220
221 void LoadResources()
222 {
223 Ogre::ConfigFile cf;
224 cf.load("resources_d.cfg");
225 Ogre::ConfigFile::SectionIterator ite = cf.getSectionIterator();
226 Ogre::String sectionName, typeName, dataName;
227 while (ite.hasMoreElements())
228 {
229 sectionName = ite.peekNextKey();
230 Ogre::ConfigFile::SettingsMultiMap* setting = ite.getNext();
231 Ogre::ConfigFile::SettingsMultiMap::iterator i;
232 for (i = setting->begin(); i != setting->end(); ++i)
233 {
234 typeName = i->first;
235 dataName = i->second;
236 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(dataName, typeName, sectionName);
237 }
238 }
239 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
240 }
241
242 void CreateScene()
243 {
244 Ogre::Plane plane(Ogre::Vector3::UNIT_Y, -5);
245 Ogre::MeshManager::getSingleton().createPlane("plane",
246 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 1500, 1500, 200, 200,
247 true, 1, 5, 5, Ogre::Vector3::UNIT_Z);
248 Ogre::Entity* ground = sceneMgr_->createEntity("planeEntity", "plane");
249 sceneMgr_->getRootSceneNode()->createChildSceneNode()->attachObject(ground);
250 ground->setMaterialName("Examples/BeachStones");
251
252 Ogre::Light* light = sceneMgr_->createLight("light1");
253 light->setType(Ogre::Light::LT_DIRECTIONAL);
254 light->setDirection(Ogre::Vector3(1, -1, 0));
255 sceneMgr_->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
256
257 sinBadEntity_ = sceneMgr_->createEntity("Sinbad.mesh");
258 sinBadNode_ = sceneMgr_->getRootSceneNode()->createChildSceneNode();
259 sinBadNode_->attachObject(sinBadEntity_);
260
261 Ogre::Entity* sword1 = sceneMgr_->createEntity("sword1", "Sword.mesh");
262 Ogre::Entity* sword2 = sceneMgr_->createEntity("sword2", "Sword.mesh");
263
264 sinBadEntity_->attachObjectToBone("Handle.L", sword1);
265 sinBadEntity_->attachObjectToBone("Handle.R", sword2);
266
267 //
268 Ogre::AnimationStateSet* set = sinBadEntity_->getAllAnimationStates();
269 Ogre::AnimationStateIterator ite = set->getAnimationStateIterator();
270 while (ite.hasMoreElements())
271 {
272 std::cout<<ite.getNext()->getAnimationName()<<std::endl;
273 }
274 }
275
276 bool KeepRunning() const
277 {
278 return keepRunning_;
279 }
280
281 void RenderOneFrame()
282 {
283 //
284 Ogre::WindowEventUtilities::messagePump();
285 keepRunning_ = root_->renderOneFrame();
286 }
287
288 int StartUp()
289 {
290 root_ = new Ogre::Root("plugins_d.cfg");
291 if (!root_->showConfigDialog())
292 return -1;
293
294 Ogre::RenderWindow* window = root_->initialise(true, "HKX");
295 sceneMgr_ = root_->createSceneManager(Ogre::ST_GENERIC);
296
297 Ogre::Camera* camera = sceneMgr_->createCamera("camera");
298 camera->setPosition(Ogre::Vector3(0, 0, 50));
299 camera->lookAt(0, 0, 0);
300 camera->setNearClipDistance(5);
301
302 Ogre::Viewport* vp = window->addViewport(camera, 0, 0, 0, 1.0, 1.0);
303 vp->setBackgroundColour(Ogre::ColourValue(0.0, 0.0, 0.0, 0.0));
304 camera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
305
306 LoadResources();
307 CreateScene();
308 // root_->startRendering();
309
310 frameListener_ = new MyFrameListener(window, camera, vp, sinBadNode_, sinBadEntity_);
311 root_->addFrameListener(frameListener_);
312
313 //
314 Ogre::CompositorManager::getSingleton().addCompositor(vp, "Compositor1");
315 Ogre::CompositorManager::getSingleton().addCompositor(vp, "Compositor2");
316 Ogre::CompositorManager::getSingleton().addCompositor(vp, "Compositor3");
317
318 return 0;
319 }
320
321 private:
322 Ogre::Root* root_;
323 Ogre::SceneManager* sceneMgr_;
324 MyFrameListener* frameListener_;
325
326 bool keepRunning_;
327 Ogre::SceneNode* sinBadNode_;
328 Ogre::Entity* sinBadEntity_;
329 };
330
331
332 int main()
333 {
334 MyAppliaction app;
335 app.StartUp();
336
337 while(app.KeepRunning())
338 {
339 app.RenderOneFrame();
340 }
341
342 return 0;
343 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
101 LINQ Samples: Miscellaneous OperatorsThis sample uses Concat to create one sequence that contains each array's values, one after the other. This sample uses ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.