1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2006 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | You may use this sample code for anything you like, it is not covered by the
|
---|
11 | LGPL like the rest of the engine.
|
---|
12 | -----------------------------------------------------------------------------
|
---|
13 | */
|
---|
14 |
|
---|
15 | #include "OceanDemo.h"
|
---|
16 | #include "Ogre.h"
|
---|
17 |
|
---|
18 | #include <cstdlib>
|
---|
19 |
|
---|
20 |
|
---|
21 | /**********************************************************************
|
---|
22 | Static declarations
|
---|
23 | **********************************************************************/
|
---|
24 | // Lights
|
---|
25 | #define NUM_LIGHTS 1
|
---|
26 |
|
---|
27 | // the light
|
---|
28 | Ogre::Light* mLights[NUM_LIGHTS];
|
---|
29 | // billboards for lights
|
---|
30 | Ogre::BillboardSet* mLightFlareSets[NUM_LIGHTS];
|
---|
31 | Ogre::Billboard* mLightFlares[NUM_LIGHTS];
|
---|
32 | // Positions for lights
|
---|
33 | Ogre::Vector3 mLightPositions[NUM_LIGHTS] =
|
---|
34 | {
|
---|
35 | Ogre::Vector3(00, 400, 00)
|
---|
36 | };
|
---|
37 | // Base orientations of the lights
|
---|
38 | Ogre::Real mLightRotationAngles[NUM_LIGHTS] = { 35 };
|
---|
39 | Ogre::Vector3 mLightRotationAxes[NUM_LIGHTS] = {
|
---|
40 | Ogre::Vector3::UNIT_X
|
---|
41 | };
|
---|
42 | // Rotation speed for lights, degrees per second
|
---|
43 | Ogre::Real mLightSpeeds[NUM_LIGHTS] = { 30};
|
---|
44 |
|
---|
45 | // Colours for the lights
|
---|
46 | Ogre::ColourValue mDiffuseLightColours[NUM_LIGHTS] =
|
---|
47 | {
|
---|
48 | Ogre::ColourValue(0.6, 0.6, 0.6)
|
---|
49 | };
|
---|
50 |
|
---|
51 | Ogre::ColourValue mSpecularLightColours[NUM_LIGHTS] =
|
---|
52 | {
|
---|
53 | Ogre::ColourValue(0.5, 0.5, 0.5)
|
---|
54 | };
|
---|
55 |
|
---|
56 | // Which lights are enabled
|
---|
57 | bool mLightState[NUM_LIGHTS] =
|
---|
58 | {
|
---|
59 | true
|
---|
60 | };
|
---|
61 |
|
---|
62 | // the light nodes
|
---|
63 | Ogre::SceneNode* mLightNodes[NUM_LIGHTS];
|
---|
64 | // the light node pivots
|
---|
65 | Ogre::SceneNode* mLightPivots[NUM_LIGHTS];
|
---|
66 |
|
---|
67 |
|
---|
68 | #define TEXTWIDGET_SIZE Size(0.16, 0.03)
|
---|
69 | #define NUMBERWIDGET_SIZE Size(0.065, 0.03)
|
---|
70 | #define SCROLLWIDGET_SIZE Size(0.21, 0.02)
|
---|
71 |
|
---|
72 | #define TEXTWIDGET_XPOS 0.02
|
---|
73 | #define NUMBERWIDGET_XPOS 0.36
|
---|
74 | #define SCROLLWIDGET_XPOS 0.50
|
---|
75 |
|
---|
76 | #define WIDGET_YSTART 0.2f
|
---|
77 | #define WIDGET_YOFFSET 0.15f
|
---|
78 |
|
---|
79 | /*************************************************************************
|
---|
80 | sub-class for ListboxTextItem that auto-sets the selection brush
|
---|
81 | image.
|
---|
82 | *************************************************************************/
|
---|
83 | class MyListItem : public CEGUI::ListboxTextItem
|
---|
84 | {
|
---|
85 | public:
|
---|
86 | MyListItem(const CEGUI::String& text, CEGUI::uint id) : CEGUI::ListboxTextItem(text, id)
|
---|
87 | {
|
---|
88 | setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
|
---|
89 | }
|
---|
90 | };
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | /*********************************************************************
|
---|
95 | Main Program Entry Point
|
---|
96 | ***********************************************************************/
|
---|
97 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
98 | #define WIN32_LEAN_AND_MEAN
|
---|
99 | #include "windows.h"
|
---|
100 |
|
---|
101 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
102 | #else
|
---|
103 | int main(int argc, char *argv[])
|
---|
104 | #endif
|
---|
105 | {
|
---|
106 | // Create application object
|
---|
107 | OceanDemo app;
|
---|
108 |
|
---|
109 | try {
|
---|
110 | app.go();
|
---|
111 |
|
---|
112 | } catch( Ogre::Exception& e ) {
|
---|
113 | #if OGRE_PLATFORM == PLATFORM_WIN32
|
---|
114 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
115 | #else
|
---|
116 | std::cerr << "An exception has occured: " <<
|
---|
117 | e.getFullDescription().c_str() << std::endl;
|
---|
118 | #endif
|
---|
119 | }
|
---|
120 |
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /*************************************************************************
|
---|
126 | OceanDemo Methods
|
---|
127 | *************************************************************************/
|
---|
128 | OceanDemo::~OceanDemo()
|
---|
129 | {
|
---|
130 | delete mGUISystem;
|
---|
131 | delete mGUIRenderer;
|
---|
132 | delete mFrameListener;
|
---|
133 |
|
---|
134 | // get rid of the shared pointers before shutting down ogre or exceptions occure
|
---|
135 | mActiveFragmentProgram.setNull();
|
---|
136 | mActiveFragmentParameters.setNull();
|
---|
137 | mActiveVertexProgram.setNull();
|
---|
138 | mActiveVertexParameters.setNull();
|
---|
139 | mActiveMaterial.setNull();
|
---|
140 |
|
---|
141 | delete mRoot;
|
---|
142 | }
|
---|
143 |
|
---|
144 | //--------------------------------------------------------------------------
|
---|
145 | void OceanDemo::go(void)
|
---|
146 | {
|
---|
147 | if (!setup())
|
---|
148 | return;
|
---|
149 |
|
---|
150 | mRoot->startRendering();
|
---|
151 | }
|
---|
152 |
|
---|
153 | //--------------------------------------------------------------------------
|
---|
154 | bool OceanDemo::setup(void)
|
---|
155 | {
|
---|
156 | mRoot = new Ogre::Root();
|
---|
157 |
|
---|
158 | setupResources();
|
---|
159 | bool carryOn = configure();
|
---|
160 | if (!carryOn) return false;
|
---|
161 |
|
---|
162 | chooseSceneManager();
|
---|
163 | createCamera();
|
---|
164 | createViewports();
|
---|
165 |
|
---|
166 | // Set default mipmap level (NB some APIs ignore this)
|
---|
167 | Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
|
---|
168 | loadResources();
|
---|
169 |
|
---|
170 | // Create the scene
|
---|
171 | createScene();
|
---|
172 |
|
---|
173 | createFrameListener();
|
---|
174 |
|
---|
175 | // load some GUI stuff for demo.
|
---|
176 | loadAllMaterialControlFiles(mMaterialControlsContainer);
|
---|
177 | initDemoEventWiring();
|
---|
178 | initComboBoxes();
|
---|
179 |
|
---|
180 | return true;
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|
184 | //--------------------------------------------------------------------------
|
---|
185 | bool OceanDemo::configure(void)
|
---|
186 | {
|
---|
187 | // Show the configuration dialog and initialise the system
|
---|
188 | // You can skip this and use root.restoreConfig() to load configuration
|
---|
189 | // settings if you were sure there are valid ones saved in ogre.cfg
|
---|
190 | if(mRoot->showConfigDialog())
|
---|
191 | {
|
---|
192 | // If returned true, user clicked OK so initialise
|
---|
193 | // Here we choose to let the system create a default rendering window by passing 'true'
|
---|
194 | mWindow = mRoot->initialise(true);
|
---|
195 | return true;
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | return false;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | //--------------------------------------------------------------------------
|
---|
204 | void OceanDemo::chooseSceneManager(void)
|
---|
205 | {
|
---|
206 | // Get the SceneManager, in this case a generic one
|
---|
207 | mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "ExampleSMInstance");
|
---|
208 | }
|
---|
209 |
|
---|
210 | //--------------------------------------------------------------------------
|
---|
211 | void OceanDemo::createCamera(void)
|
---|
212 | {
|
---|
213 | // Create the camera
|
---|
214 | mCamera = mSceneMgr->createCamera("PlayerCam");
|
---|
215 |
|
---|
216 | // Position it at 500 in Z direction
|
---|
217 | mCamera->setPosition(Ogre::Vector3(0,0,0));
|
---|
218 | // Look back along -Z
|
---|
219 | mCamera->lookAt(Ogre::Vector3(0,0,-300));
|
---|
220 | mCamera->setNearClipDistance(1);
|
---|
221 |
|
---|
222 | }
|
---|
223 |
|
---|
224 | //--------------------------------------------------------------------------
|
---|
225 | void OceanDemo::createViewports(void)
|
---|
226 | {
|
---|
227 | // Create one viewport, entire window
|
---|
228 | Ogre::Viewport* vp = mWindow->addViewport(mCamera);
|
---|
229 | vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
|
---|
230 |
|
---|
231 | // Alter the camera aspect ratio to match the viewport
|
---|
232 | mCamera->setAspectRatio(
|
---|
233 | Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
|
---|
234 | }
|
---|
235 |
|
---|
236 | //--------------------------------------------------------------------------
|
---|
237 | void OceanDemo::setupResources(void)
|
---|
238 | {
|
---|
239 | // Load resource paths from config file
|
---|
240 | Ogre::ConfigFile cf;
|
---|
241 | cf.load("resources.cfg");
|
---|
242 |
|
---|
243 | // Go through all sections & settings in the file
|
---|
244 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
|
---|
245 |
|
---|
246 | Ogre::String secName, typeName, archName;
|
---|
247 | while (seci.hasMoreElements())
|
---|
248 | {
|
---|
249 | secName = seci.peekNextKey();
|
---|
250 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
|
---|
251 | Ogre::ConfigFile::SettingsMultiMap::iterator i;
|
---|
252 | for (i = settings->begin(); i != settings->end(); ++i)
|
---|
253 | {
|
---|
254 | typeName = i->first;
|
---|
255 | archName = i->second;
|
---|
256 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
|
---|
257 | archName, typeName, secName);
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | Ogre::LogManager::getSingleton().logMessage( "Resource directories setup" );
|
---|
262 |
|
---|
263 | }
|
---|
264 |
|
---|
265 | //-----------------------------------------------------------------------------------
|
---|
266 | void OceanDemo::loadResources(void)
|
---|
267 | {
|
---|
268 | // Initialise, parse scripts etc
|
---|
269 | Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
|
---|
270 |
|
---|
271 | }
|
---|
272 |
|
---|
273 | //--------------------------------------------------------------------------
|
---|
274 | void OceanDemo::createScene(void)
|
---|
275 | {
|
---|
276 | // setup GUI system
|
---|
277 |
|
---|
278 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
|
---|
279 | // load scheme and set up defaults
|
---|
280 |
|
---|
281 | mGUISystem = new CEGUI::System(mGUIRenderer, (CEGUI::ScriptModule*)0, (CEGUI::ResourceProvider *)0, (CEGUI::utf8*)"OceanDemoCegui.config");
|
---|
282 | CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
|
---|
283 |
|
---|
284 | // Set ambient light
|
---|
285 | mSceneMgr->setAmbientLight(Ogre::ColourValue(0.3, 0.3, 0.3));
|
---|
286 | mSceneMgr->setSkyBox(true, "SkyBox", 1000);
|
---|
287 |
|
---|
288 | mMainNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
289 |
|
---|
290 |
|
---|
291 | for (unsigned int i = 0; i < NUM_LIGHTS; ++i)
|
---|
292 | {
|
---|
293 | mLightPivots[i] = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
294 | mLightPivots[i]->rotate(mLightRotationAxes[i], Ogre::Angle(mLightRotationAngles[i]));
|
---|
295 | // Create a light, use default parameters
|
---|
296 | mLights[i] = mSceneMgr->createLight("Light" + Ogre::StringConverter::toString(i));
|
---|
297 | mLights[i]->setPosition(mLightPositions[i]);
|
---|
298 | mLights[i]->setDiffuseColour(mDiffuseLightColours[i]);
|
---|
299 | mLights[i]->setSpecularColour(mSpecularLightColours[i]);
|
---|
300 | mLights[i]->setVisible(mLightState[i]);
|
---|
301 | //mLights[i]->setAttenuation(400, 0.1 , 1 , 0);
|
---|
302 | // Attach light
|
---|
303 | mLightPivots[i]->attachObject(mLights[i]);
|
---|
304 | // Create billboard for light
|
---|
305 | mLightFlareSets[i] = mSceneMgr->createBillboardSet("Flare" + Ogre::StringConverter::toString(i));
|
---|
306 | mLightFlareSets[i]->setMaterialName("LightFlare");
|
---|
307 | mLightPivots[i]->attachObject(mLightFlareSets[i]);
|
---|
308 | mLightFlares[i] = mLightFlareSets[i]->createBillboard(mLightPositions[i]);
|
---|
309 | mLightFlares[i]->setColour(mDiffuseLightColours[i]);
|
---|
310 | mLightFlareSets[i]->setVisible(mLightState[i]);
|
---|
311 | }
|
---|
312 |
|
---|
313 | // move the camera a bit right and make it look at the knot
|
---|
314 | mCamera->moveRelative(Ogre::Vector3(50, 0, 100));
|
---|
315 | mCamera->lookAt(0, 0, 0);
|
---|
316 |
|
---|
317 | // Define a plane mesh that will be used for the ocean surface
|
---|
318 | Ogre::Plane oceanSurface;
|
---|
319 | oceanSurface.normal = Ogre::Vector3::UNIT_Y;
|
---|
320 | oceanSurface.d = 20;
|
---|
321 | Ogre::MeshManager::getSingleton().createPlane("OceanSurface",
|
---|
322 | Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
---|
323 | oceanSurface,
|
---|
324 | 1000, 1000, 40, 40, true, 1, 1, 1, Ogre::Vector3::UNIT_Z);
|
---|
325 |
|
---|
326 | mOceanSurfaceEnt = mSceneMgr->createEntity( "OceanSurface", "OceanSurface" );
|
---|
327 | mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(mOceanSurfaceEnt);
|
---|
328 |
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | //--------------------------------------------------------------------------
|
---|
333 | void OceanDemo::initComboBoxes(void)
|
---|
334 | {
|
---|
335 | using namespace CEGUI;
|
---|
336 |
|
---|
337 |
|
---|
338 | Combobox* cbobox = (Combobox*)WindowManager::getSingleton().getWindow("ShaderCombos");
|
---|
339 |
|
---|
340 | for(size_t idx = 0; idx < mMaterialControlsContainer.size(); ++idx )
|
---|
341 | {
|
---|
342 | cbobox->addItem(new MyListItem( mMaterialControlsContainer[idx].getDisplayName().c_str(), static_cast<CEGUI::uint>(idx)));
|
---|
343 | }
|
---|
344 |
|
---|
345 | // make first item visible
|
---|
346 | if (cbobox->getItemCount() > 0)
|
---|
347 | cbobox->setItemSelectState((CEGUI::uint)0, true);
|
---|
348 |
|
---|
349 | Editbox* eb;
|
---|
350 | // set text in combobox
|
---|
351 | if (!mMaterialControlsContainer.empty())
|
---|
352 | {
|
---|
353 | eb = (Editbox*)WindowManager::getSingleton().getWindow(cbobox->getName() + "__auto_editbox__");
|
---|
354 | eb->setText(mMaterialControlsContainer[0].getDisplayName().c_str());
|
---|
355 | handleShaderComboChanged(CEGUI::WindowEventArgs(cbobox));
|
---|
356 | }
|
---|
357 |
|
---|
358 | cbobox = (Combobox*)WindowManager::getSingleton().getWindow("ModelCombos");
|
---|
359 | Ogre::StringVectorPtr meshStringVector = Ogre::ResourceGroupManager::getSingleton().findResourceNames( Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "*.mesh" );
|
---|
360 | std::vector<Ogre::String>::iterator meshFileNameIterator = meshStringVector->begin();
|
---|
361 |
|
---|
362 | while ( meshFileNameIterator != meshStringVector->end() )
|
---|
363 | {
|
---|
364 | cbobox->addItem(new MyListItem( (*meshFileNameIterator).c_str(), 0 ));
|
---|
365 | ++meshFileNameIterator;
|
---|
366 | }
|
---|
367 |
|
---|
368 | // make first item visible
|
---|
369 | //cbobox->setItemSelectState((CEGUI::uint)0, true);
|
---|
370 | // eb = (Editbox*)WindowManager::getSingleton().getWindow(cbobox->getName() + "__auto_editbox__");
|
---|
371 | // if (meshStringVector->begin() != meshStringVector->end())
|
---|
372 | // {
|
---|
373 | // eb->setText((*meshStringVector->begin()).c_str());
|
---|
374 | // handleModelComboChanged(CEGUI::WindowEventArgs(cbobox));
|
---|
375 | // }
|
---|
376 |
|
---|
377 | }
|
---|
378 |
|
---|
379 |
|
---|
380 | //--------------------------------------------------------------------------
|
---|
381 | void OceanDemo::initDemoEventWiring(void)
|
---|
382 | {
|
---|
383 | using namespace CEGUI;
|
---|
384 |
|
---|
385 | WindowManager::getSingleton().getWindow("ExitDemoBtn")->
|
---|
386 | subscribeEvent(PushButton::EventClicked, CEGUI::Event::Subscriber(&OceanDemo::handleQuit, this));
|
---|
387 |
|
---|
388 | WindowManager::getSingleton().getWindow("ModelCombos")->
|
---|
389 | subscribeEvent(Combobox::EventListSelectionAccepted, CEGUI::Event::Subscriber(&OceanDemo::handleModelComboChanged, this));
|
---|
390 |
|
---|
391 | WindowManager::getSingleton().getWindow("ShaderCombos")->
|
---|
392 | subscribeEvent(Combobox::EventListSelectionAccepted, CEGUI::Event::Subscriber(&OceanDemo::handleShaderComboChanged, this));
|
---|
393 |
|
---|
394 | Window* wndw = WindowManager::getSingleton().getWindow("root");
|
---|
395 |
|
---|
396 | wndw->subscribeEvent(Window::EventMouseMove, CEGUI::Event::Subscriber(&OceanDemo_FrameListener::handleMouseMove, mFrameListener));
|
---|
397 |
|
---|
398 | wndw->subscribeEvent(Window::EventMouseButtonUp, CEGUI::Event::Subscriber(&OceanDemo_FrameListener::handleMouseButtonUp, mFrameListener));
|
---|
399 |
|
---|
400 | wndw->subscribeEvent(Window::EventMouseButtonDown, CEGUI::Event::Subscriber(&OceanDemo_FrameListener::handleMouseButtonDown, mFrameListener));
|
---|
401 |
|
---|
402 | wndw->subscribeEvent(Window::EventMouseWheel, CEGUI::Event::Subscriber(&OceanDemo_FrameListener::handleMouseWheelEvent, mFrameListener));
|
---|
403 | wndw->subscribeEvent(Window::EventKeyDown, CEGUI::Event::Subscriber(&OceanDemo_FrameListener::handleKeyDownEvent, mFrameListener ));
|
---|
404 | wndw->subscribeEvent(Window::EventKeyUp, CEGUI::Event::Subscriber(&OceanDemo_FrameListener::handleKeyUpEvent, mFrameListener ));
|
---|
405 |
|
---|
406 | wndw = WindowManager::getSingleton().getWindow("ModelSpinCB");
|
---|
407 | wndw->subscribeEvent(Checkbox::EventCheckStateChanged, CEGUI::Event::Subscriber(&OceanDemo_FrameListener::handelModelSpinChange, mFrameListener ));
|
---|
408 |
|
---|
409 | wndw = WindowManager::getSingleton().getWindow("LightSpinCB");
|
---|
410 | wndw->subscribeEvent(Checkbox::EventCheckStateChanged, CEGUI::Event::Subscriber(&OceanDemo_FrameListener::handelLightSpinChange, mFrameListener ));
|
---|
411 |
|
---|
412 | wndw = WindowManager::getSingleton().getWindow("CameraRBtn");
|
---|
413 | wndw->subscribeEvent(RadioButton::EventSelectStateChanged, CEGUI::Event::Subscriber(&OceanDemo::handleMovementTypeChange, this ));
|
---|
414 |
|
---|
415 | wndw = WindowManager::getSingleton().getWindow("ModelRBtn");
|
---|
416 | wndw->subscribeEvent(RadioButton::EventSelectStateChanged, CEGUI::Event::Subscriber(&OceanDemo::handleMovementTypeChange, this ));
|
---|
417 |
|
---|
418 | mVertScroll = (Scrollbar*)WindowManager::getSingleton().getWindow("VerticalScroll");
|
---|
419 | mVertScroll->subscribeEvent(Scrollbar::EventScrollPositionChanged, CEGUI::Event::Subscriber(&OceanDemo::handleScrollControlsWindow, this ));
|
---|
420 |
|
---|
421 | }
|
---|
422 |
|
---|
423 |
|
---|
424 | //--------------------------------------------------------------------------
|
---|
425 | void OceanDemo::doErrorBox(const char* text)
|
---|
426 | {
|
---|
427 | using namespace CEGUI;
|
---|
428 |
|
---|
429 | WindowManager& winMgr = WindowManager::getSingleton();
|
---|
430 | Window* root = winMgr.getWindow("root_wnd");
|
---|
431 |
|
---|
432 | FrameWindow* errbox;
|
---|
433 |
|
---|
434 | try
|
---|
435 | {
|
---|
436 | errbox = (FrameWindow*)winMgr.getWindow("ErrorBox");
|
---|
437 | }
|
---|
438 | catch(UnknownObjectException x)
|
---|
439 | {
|
---|
440 | // create frame window for box
|
---|
441 | FrameWindow* fwnd = (FrameWindow*)winMgr.createWindow("TaharezLook/FrameWindow", "ErrorBox");
|
---|
442 | root->addChildWindow(fwnd);
|
---|
443 | fwnd->setPosition(Point(0.25, 0.25f));
|
---|
444 | fwnd->setMaximumSize(Size(1.0f, 1.0f));
|
---|
445 | fwnd->setSize(Size(0.5f, 0.5f));
|
---|
446 | fwnd->setText("CEGUI Demo - Error!");
|
---|
447 | fwnd->setDragMovingEnabled(false);
|
---|
448 | fwnd->setSizingEnabled(false);
|
---|
449 | fwnd->setAlwaysOnTop(true);
|
---|
450 | fwnd->setCloseButtonEnabled(false);
|
---|
451 |
|
---|
452 | // create error text message
|
---|
453 | StaticText* msg = (StaticText*)winMgr.createWindow("TaharezLook/StaticText", "ErrorBox/Message");
|
---|
454 | fwnd->addChildWindow(msg);
|
---|
455 | msg->setPosition(Point(0.1f, 0.1f));
|
---|
456 | msg->setSize(Size(0.8f, 0.5f));
|
---|
457 | msg->setVerticalFormatting(StaticText::VertCentred);
|
---|
458 | msg->setHorizontalFormatting(StaticText::HorzCentred);
|
---|
459 | msg->setBackgroundEnabled(false);
|
---|
460 | msg->setFrameEnabled(false);
|
---|
461 |
|
---|
462 | // create ok button
|
---|
463 | PushButton* btn = (PushButton*)winMgr.createWindow("TaharezLook/Button", "ErrorBox/OkButton");
|
---|
464 | fwnd->addChildWindow(btn);
|
---|
465 | btn->setPosition(Point(0.3f, 0.80f));
|
---|
466 | btn->setSize(Size(0.4f, 0.1f));
|
---|
467 | btn->setText("Okay!");
|
---|
468 |
|
---|
469 | // subscribe event
|
---|
470 | btn->subscribeEvent(PushButton::EventClicked, CEGUI::Event::Subscriber(&OceanDemo::handleErrorBox, this ));
|
---|
471 |
|
---|
472 | errbox = fwnd;
|
---|
473 | }
|
---|
474 |
|
---|
475 | errbox->getChild("ErrorBox/Message")->setText(text);
|
---|
476 | errbox->show();
|
---|
477 | errbox->activate();
|
---|
478 | }
|
---|
479 |
|
---|
480 |
|
---|
481 | //--------------------------------------------------------------------------
|
---|
482 | bool OceanDemo::handleQuit(const CEGUI::EventArgs& e)
|
---|
483 | {
|
---|
484 | mRoot->queueEndRendering();
|
---|
485 | return true;
|
---|
486 | }
|
---|
487 |
|
---|
488 | //--------------------------------------------------------------------------
|
---|
489 | void OceanDemo::setShaderControlVal(const float val, const size_t index)
|
---|
490 | {
|
---|
491 | char valTxtBuf[20];
|
---|
492 |
|
---|
493 | // set the text of the value
|
---|
494 | sprintf(valTxtBuf, "%3.3f", val );
|
---|
495 | mShaderControlContainer[index].NumberWidget->setText(valTxtBuf);
|
---|
496 | }
|
---|
497 |
|
---|
498 | //--------------------------------------------------------------------------
|
---|
499 | bool OceanDemo::handleShaderControl(const CEGUI::EventArgs& e)
|
---|
500 | {
|
---|
501 | using namespace CEGUI;
|
---|
502 | using namespace Ogre;
|
---|
503 |
|
---|
504 |
|
---|
505 | size_t index = ((Scrollbar*)((const WindowEventArgs&)e).window)->getID();
|
---|
506 | const ShaderControl& ActiveShaderDef = mMaterialControlsContainer[mCurrentMaterial].getShaderControl(index);
|
---|
507 |
|
---|
508 | float val = ((Scrollbar*)((const WindowEventArgs&)e).window)->getScrollPosition();
|
---|
509 | val = ActiveShaderDef.convertScrollPositionToParam(val);
|
---|
510 | setShaderControlVal( val, index );
|
---|
511 |
|
---|
512 | if(mActivePass)
|
---|
513 | {
|
---|
514 | switch(ActiveShaderDef.ValType)
|
---|
515 | {
|
---|
516 | case GPU_VERTEX:
|
---|
517 | case GPU_FRAGMENT:
|
---|
518 | {
|
---|
519 | GpuProgramParametersSharedPtr activeParameters =
|
---|
520 | (ActiveShaderDef.ValType == GPU_VERTEX) ?
|
---|
521 | mActiveVertexParameters : mActiveFragmentParameters;
|
---|
522 |
|
---|
523 | if(!activeParameters.isNull())
|
---|
524 | {
|
---|
525 | // use index to get RealConstantEntry
|
---|
526 | GpuProgramParameters::RealConstantEntry* entry = activeParameters->getRealConstantEntry( ActiveShaderDef.ConstantIndex );
|
---|
527 | // get val from array using element index
|
---|
528 | if(entry != NULL)
|
---|
529 | {
|
---|
530 | entry->val[ActiveShaderDef.ElementIndex] = val;
|
---|
531 | entry->isSet = true;
|
---|
532 | }
|
---|
533 | }
|
---|
534 | }
|
---|
535 | break;
|
---|
536 |
|
---|
537 | case MAT_SPECULAR:
|
---|
538 | {
|
---|
539 | // get the specular values from the material pass
|
---|
540 | ColourValue OldSpec(mActivePass->getSpecular());
|
---|
541 | OldSpec.val[ActiveShaderDef.ElementIndex] = val;
|
---|
542 | mActivePass->setSpecular( OldSpec );
|
---|
543 | }
|
---|
544 |
|
---|
545 | break;
|
---|
546 |
|
---|
547 | case MAT_DIFFUSE:
|
---|
548 | {
|
---|
549 | // get the specular values from the material pass
|
---|
550 | ColourValue OldSpec(mActivePass->getDiffuse());
|
---|
551 | OldSpec.val[ActiveShaderDef.ElementIndex] = val;
|
---|
552 | mActivePass->setDiffuse( OldSpec );
|
---|
553 | }
|
---|
554 | break;
|
---|
555 |
|
---|
556 | case MAT_AMBIENT:
|
---|
557 | {
|
---|
558 | // get the specular values from the material pass
|
---|
559 | ColourValue OldSpec(mActivePass->getAmbient());
|
---|
560 | OldSpec.val[ActiveShaderDef.ElementIndex] = val;
|
---|
561 | mActivePass->setAmbient( OldSpec );
|
---|
562 | }
|
---|
563 | break;
|
---|
564 |
|
---|
565 | case MAT_SHININESS:
|
---|
566 | // get the specular values from the material pass
|
---|
567 | mActivePass->setShininess( val );
|
---|
568 | break;
|
---|
569 | }
|
---|
570 | }
|
---|
571 |
|
---|
572 | return true;
|
---|
573 | }
|
---|
574 |
|
---|
575 | //--------------------------------------------------------------------------
|
---|
576 | void OceanDemo::configureShaderControls(void)
|
---|
577 | {
|
---|
578 | using namespace CEGUI;
|
---|
579 |
|
---|
580 | if (mMaterialControlsContainer.empty()) return;
|
---|
581 |
|
---|
582 | mActiveMaterial = Ogre::MaterialManager::getSingleton().getByName( mMaterialControlsContainer[mCurrentMaterial].getMaterialName() );
|
---|
583 | if(!mActiveMaterial.isNull())
|
---|
584 | {
|
---|
585 | Ogre::Technique* currentTechnique = mActiveMaterial->getSupportedTechnique(0);
|
---|
586 | if(currentTechnique)
|
---|
587 | {
|
---|
588 | mActivePass = currentTechnique->getPass(0);
|
---|
589 | if(mActivePass)
|
---|
590 | {
|
---|
591 | if (mActivePass->hasFragmentProgram())
|
---|
592 | {
|
---|
593 | mActiveFragmentProgram = mActivePass->getFragmentProgram();
|
---|
594 | mActiveFragmentParameters = mActivePass->getFragmentProgramParameters();
|
---|
595 | }
|
---|
596 | if (mActivePass->hasVertexProgram())
|
---|
597 | {
|
---|
598 | mActiveVertexProgram = mActivePass->getVertexProgram();
|
---|
599 | mActiveVertexParameters = mActivePass->getVertexProgramParameters();
|
---|
600 | }
|
---|
601 |
|
---|
602 | size_t activeControlCount = mMaterialControlsContainer[mCurrentMaterial].getShaderControlCount();
|
---|
603 | mVertScroll->setDocumentSize( activeControlCount / 5.0f );
|
---|
604 | mVertScroll->setScrollPosition(0.0f);
|
---|
605 |
|
---|
606 | // init the GUI controls
|
---|
607 | // check the material entry for Params GUI list
|
---|
608 | if(mMaterialControlsContainer[mCurrentMaterial].getShaderControlCount() > 0)
|
---|
609 | {
|
---|
610 | // if mShaderControlContainer size < Params list
|
---|
611 | if( activeControlCount > mShaderControlContainer.size())
|
---|
612 | {
|
---|
613 | // resize container
|
---|
614 | mShaderControlContainer.resize( activeControlCount );
|
---|
615 | }
|
---|
616 | Window* controlWindow = WindowManager::getSingleton().getWindow("ShaderControlsWin");
|
---|
617 | // initialize each widget based on control data
|
---|
618 | // iterate through the params GUI list
|
---|
619 | for( size_t i=0; i < activeControlCount; ++i )
|
---|
620 | {
|
---|
621 | const ShaderControl& ActiveShaderDef = mMaterialControlsContainer[mCurrentMaterial].getShaderControl(i);
|
---|
622 |
|
---|
623 | // if TextWidget is NULL
|
---|
624 | StaticText* activeTextWidget = mShaderControlContainer[i].TextWidget;
|
---|
625 | if(activeTextWidget == NULL)
|
---|
626 | {
|
---|
627 | // create TextWidget
|
---|
628 |
|
---|
629 | mShaderControlContainer[i].TextWidget = activeTextWidget =
|
---|
630 | (StaticText*)WindowManager::getSingleton().createWindow("TaharezLook/StaticText",
|
---|
631 | ( ("UniformTxt" + Ogre::StringConverter::toString(i)).c_str() ));
|
---|
632 | // add to Shader control window
|
---|
633 | controlWindow->addChildWindow( activeTextWidget );
|
---|
634 | // set position based on its index
|
---|
635 | activeTextWidget->setPosition(Point(TEXTWIDGET_XPOS, WIDGET_YSTART + WIDGET_YOFFSET * float(i)));
|
---|
636 | activeTextWidget->setHorizontalFormatting(StaticText::RightAligned);
|
---|
637 | activeTextWidget->setFrameEnabled(false);
|
---|
638 | activeTextWidget->setInheritsAlpha(false);
|
---|
639 | activeTextWidget->setBackgroundEnabled(false);
|
---|
640 | activeTextWidget->setMaximumSize( TEXTWIDGET_SIZE );
|
---|
641 | activeTextWidget->setMinimumSize( TEXTWIDGET_SIZE );
|
---|
642 | activeTextWidget->setSize( TEXTWIDGET_SIZE );
|
---|
643 | }
|
---|
644 |
|
---|
645 | // set TextWidget text to control name
|
---|
646 | activeTextWidget->setText( ActiveShaderDef.Name.c_str() );
|
---|
647 | // make TextWidget visible
|
---|
648 | activeTextWidget->show();
|
---|
649 |
|
---|
650 | // if NumberWidget is NULL
|
---|
651 | StaticText* activeNumberWidget = mShaderControlContainer[i].NumberWidget;
|
---|
652 | if(activeNumberWidget == NULL)
|
---|
653 | {
|
---|
654 | // create NumberWidget
|
---|
655 |
|
---|
656 | mShaderControlContainer[i].NumberWidget = activeNumberWidget =
|
---|
657 | (StaticText*)WindowManager::getSingleton().createWindow("TaharezLook/StaticText",
|
---|
658 | ( ("UniformNumTxt" + Ogre::StringConverter::toString(i)).c_str() ));
|
---|
659 | // add to Shader control window
|
---|
660 | controlWindow->addChildWindow( activeNumberWidget );
|
---|
661 | // set position based on its index
|
---|
662 | activeNumberWidget->setPosition(Point(NUMBERWIDGET_XPOS, WIDGET_YSTART + WIDGET_YOFFSET * float(i)));
|
---|
663 | activeNumberWidget->setHorizontalFormatting(StaticText::RightAligned);
|
---|
664 | activeNumberWidget->setFrameEnabled(false);
|
---|
665 | activeNumberWidget->setInheritsAlpha(false);
|
---|
666 | activeNumberWidget->setBackgroundEnabled(false);
|
---|
667 | activeNumberWidget->setMaximumSize( NUMBERWIDGET_SIZE );
|
---|
668 | activeNumberWidget->setMinimumSize( NUMBERWIDGET_SIZE );
|
---|
669 | activeNumberWidget->setSize( NUMBERWIDGET_SIZE );
|
---|
670 | }
|
---|
671 | // make TextWidget visible
|
---|
672 | activeNumberWidget->show();
|
---|
673 |
|
---|
674 | Scrollbar* activeScrollWidget = mShaderControlContainer[i].ScrollWidget;
|
---|
675 | // if ScrollWidget is NULL
|
---|
676 | if( activeScrollWidget == NULL )
|
---|
677 | {
|
---|
678 | // create ScrollWidget
|
---|
679 | mShaderControlContainer[i].ScrollWidget = activeScrollWidget =
|
---|
680 | (Scrollbar*)WindowManager::getSingleton().createWindow("TaharezLook/HorizontalScrollbar",
|
---|
681 | ( ("UniformSB" + Ogre::StringConverter::toString(i)).c_str() ));
|
---|
682 | // add to Shader control window
|
---|
683 | controlWindow->addChildWindow( activeScrollWidget );
|
---|
684 | // set position based on its index
|
---|
685 | activeScrollWidget->setPosition(Point(SCROLLWIDGET_XPOS, WIDGET_YSTART + WIDGET_YOFFSET * float(i)));
|
---|
686 | activeScrollWidget->setInheritsAlpha(false);
|
---|
687 | activeScrollWidget->setMaximumSize( SCROLLWIDGET_SIZE );
|
---|
688 | activeScrollWidget->setMinimumSize( SCROLLWIDGET_SIZE );
|
---|
689 | activeScrollWidget->setSize( SCROLLWIDGET_SIZE );
|
---|
690 | activeScrollWidget->setID( static_cast<CEGUI::uint>(i) );
|
---|
691 | activeScrollWidget->setOverlapSize(0);
|
---|
692 | // wire up ScrollWidget position changed event to handleShaderControl
|
---|
693 | activeScrollWidget->subscribeEvent(Scrollbar::EventScrollPositionChanged, CEGUI::Event::Subscriber(&OceanDemo::handleShaderControl, this )); }
|
---|
694 | // set max value of ScrollWidget
|
---|
695 | float maxval = ActiveShaderDef.getRange();
|
---|
696 | activeScrollWidget->setDocumentSize(maxval);
|
---|
697 | activeScrollWidget->setPageSize(0.000);
|
---|
698 | activeScrollWidget->setStepSize(maxval/20);
|
---|
699 | // get current value of param
|
---|
700 |
|
---|
701 | float uniformVal = 0.0;
|
---|
702 |
|
---|
703 | switch(ActiveShaderDef.ValType)
|
---|
704 | {
|
---|
705 | case GPU_VERTEX:
|
---|
706 | case GPU_FRAGMENT:
|
---|
707 | {
|
---|
708 | Ogre::GpuProgramParametersSharedPtr activeParameters =
|
---|
709 | (ActiveShaderDef.ValType == Ogre::GPT_VERTEX_PROGRAM) ?
|
---|
710 | mActiveVertexParameters : mActiveFragmentParameters;
|
---|
711 | if(!activeParameters.isNull())
|
---|
712 | {
|
---|
713 | // use param name to get index : use appropiate paramters ptr
|
---|
714 | size_t entryindex = activeParameters->getParamIndex(ActiveShaderDef.ParamName);
|
---|
715 | ActiveShaderDef.ConstantIndex = entryindex;
|
---|
716 | // use index to get RealConstantEntry
|
---|
717 | Ogre::GpuProgramParameters::RealConstantEntry* entry = activeParameters->getRealConstantEntry( entryindex );
|
---|
718 | // get val from array using element index
|
---|
719 | if(entry != NULL)
|
---|
720 | {
|
---|
721 | // set position of ScrollWidget as param value
|
---|
722 | uniformVal = entry->val[ActiveShaderDef.ElementIndex];
|
---|
723 | activeScrollWidget->setScrollPosition( ActiveShaderDef.convertParamToScrollPosition(uniformVal) );
|
---|
724 | }
|
---|
725 | }
|
---|
726 | }
|
---|
727 | break;
|
---|
728 |
|
---|
729 | case MAT_SPECULAR:
|
---|
730 | {
|
---|
731 | // get the specular values from the material pass
|
---|
732 |
|
---|
733 | Ogre::ColourValue OldSpec(mActivePass->getSpecular());
|
---|
734 | uniformVal = OldSpec.val[ActiveShaderDef.ElementIndex];
|
---|
735 | activeScrollWidget->setScrollPosition( ActiveShaderDef.convertParamToScrollPosition(uniformVal) );
|
---|
736 | }
|
---|
737 | break;
|
---|
738 |
|
---|
739 | case MAT_DIFFUSE:
|
---|
740 | {
|
---|
741 | // get the diffuse values from the material pass
|
---|
742 |
|
---|
743 | Ogre::ColourValue OldSpec(mActivePass->getDiffuse());
|
---|
744 | uniformVal = OldSpec.val[ActiveShaderDef.ElementIndex];
|
---|
745 | activeScrollWidget->setScrollPosition( ActiveShaderDef.convertParamToScrollPosition(uniformVal) );
|
---|
746 | }
|
---|
747 | break;
|
---|
748 |
|
---|
749 | case MAT_AMBIENT:
|
---|
750 | {
|
---|
751 | // get the ambient values from the material pass
|
---|
752 |
|
---|
753 | Ogre::ColourValue OldSpec(mActivePass->getAmbient());
|
---|
754 | uniformVal = OldSpec.val[ActiveShaderDef.ElementIndex];
|
---|
755 | activeScrollWidget->setScrollPosition( ActiveShaderDef.convertParamToScrollPosition(uniformVal) );
|
---|
756 | }
|
---|
757 | break;
|
---|
758 |
|
---|
759 | case MAT_SHININESS:
|
---|
760 | {
|
---|
761 | // get the ambient values from the material pass
|
---|
762 |
|
---|
763 | uniformVal = mActivePass->getShininess();
|
---|
764 | activeScrollWidget->setScrollPosition( ActiveShaderDef.convertParamToScrollPosition(uniformVal) );
|
---|
765 | }
|
---|
766 |
|
---|
767 | break;
|
---|
768 |
|
---|
769 | case MAT_EMISSIVE:
|
---|
770 | {
|
---|
771 | // get the ambient values from the material pass
|
---|
772 |
|
---|
773 | //ColourValue OldSpec(mActivePass->gete());
|
---|
774 | //activeScrollWidget->setScrollPosition( OldSpec.val[ActiveShaderDef->ElementIndex] );
|
---|
775 | }
|
---|
776 | break;
|
---|
777 | }
|
---|
778 |
|
---|
779 | setShaderControlVal( uniformVal, i );
|
---|
780 | ;
|
---|
781 | activeScrollWidget->show();
|
---|
782 | } // end of iterate
|
---|
783 | }
|
---|
784 |
|
---|
785 | // turn off extra GUI widgets
|
---|
786 | // iterate from 1 + active widgets to end
|
---|
787 | for( size_t i = activeControlCount; i < mShaderControlContainer.size(); i++ )
|
---|
788 | {
|
---|
789 | // hide widget
|
---|
790 | if( mShaderControlContainer[i].TextWidget != NULL )
|
---|
791 | {
|
---|
792 | mShaderControlContainer[i].TextWidget->hide();
|
---|
793 | }
|
---|
794 |
|
---|
795 | if( mShaderControlContainer[i].NumberWidget != NULL )
|
---|
796 | {
|
---|
797 | mShaderControlContainer[i].NumberWidget->hide();
|
---|
798 | }
|
---|
799 |
|
---|
800 | {
|
---|
801 | mShaderControlContainer[i].ScrollWidget->hide();
|
---|
802 | }
|
---|
803 |
|
---|
804 | }// end of iterate
|
---|
805 |
|
---|
806 | }
|
---|
807 | }
|
---|
808 | }
|
---|
809 |
|
---|
810 | }
|
---|
811 |
|
---|
812 | //--------------------------------------------------------------------------
|
---|
813 | bool OceanDemo::handleModelComboChanged(const CEGUI::EventArgs& e)
|
---|
814 | {
|
---|
815 | using namespace CEGUI;
|
---|
816 |
|
---|
817 | // get the selected mesh filename from the combo box
|
---|
818 | CEGUI::ListboxItem* item = ((Combobox*)((const WindowEventArgs&)e).window)->getSelectedItem();
|
---|
819 |
|
---|
820 | // convert from CEGUI::String to Ogre::String
|
---|
821 | Ogre::String meshName(item->getText().c_str());
|
---|
822 |
|
---|
823 | // hide the current entity
|
---|
824 | if (mCurrentEntity)
|
---|
825 | {
|
---|
826 | mCurrentEntity->setVisible(false);
|
---|
827 | // disconnect the entity from the scenenode
|
---|
828 | mMainNode->detachObject(mCurrentEntity->getName());
|
---|
829 | }
|
---|
830 |
|
---|
831 | // find the entity selected in combo box
|
---|
832 | // an exception is raised by getEntity if it doesn't exist
|
---|
833 | // so trap the exception if entity not found and load it
|
---|
834 | try
|
---|
835 | {
|
---|
836 | mCurrentEntity = mSceneMgr->getEntity( meshName );
|
---|
837 | }
|
---|
838 | catch (Ogre::Exception e)
|
---|
839 | // if not found create it
|
---|
840 | {
|
---|
841 | Ogre::MeshPtr pMesh = Ogre::MeshManager::getSingleton().load(meshName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
|
---|
842 | //Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY,
|
---|
843 | //Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY,
|
---|
844 | //true, true
|
---|
845 | ); //so we can still read it
|
---|
846 | // Build tangent vectors, all our meshes use only 1 texture coordset
|
---|
847 | pMesh->buildTangentVectors(0, 1);
|
---|
848 | // Create entity
|
---|
849 | mCurrentEntity = mSceneMgr->createEntity(meshName, meshName);
|
---|
850 | }
|
---|
851 |
|
---|
852 | // make the entity visible and attach it to our main scenenode
|
---|
853 | mCurrentEntity->setVisible(true);
|
---|
854 | mMainNode->attachObject(mCurrentEntity);
|
---|
855 |
|
---|
856 | // set the the entity's material to the current material
|
---|
857 | //mCurrentEntity->setMaterialName(mMaterialControlsContainer[mCurrentMaterial].getMaterialName());
|
---|
858 | configureShaderControls();
|
---|
859 | return true;
|
---|
860 | }
|
---|
861 |
|
---|
862 | //--------------------------------------------------------------------------
|
---|
863 | bool OceanDemo::handleShaderComboChanged(const CEGUI::EventArgs& e)
|
---|
864 | {
|
---|
865 | using namespace CEGUI;
|
---|
866 |
|
---|
867 | CEGUI::ListboxItem* item = ((Combobox*)((const WindowEventArgs&)e).window)->getSelectedItem();
|
---|
868 |
|
---|
869 | mCurrentMaterial = item->getID();
|
---|
870 | if (mOceanSurfaceEnt)
|
---|
871 | mOceanSurfaceEnt->setMaterialName(mMaterialControlsContainer[mCurrentMaterial].getMaterialName());
|
---|
872 | configureShaderControls();
|
---|
873 |
|
---|
874 | return true;
|
---|
875 | }
|
---|
876 |
|
---|
877 |
|
---|
878 | //--------------------------------------------------------------------------
|
---|
879 | bool OceanDemo::handleScrollControlsWindow(const CEGUI::EventArgs& e)
|
---|
880 | {
|
---|
881 | using namespace CEGUI;
|
---|
882 |
|
---|
883 | size_t controlCount = mShaderControlContainer.size();
|
---|
884 | float scrollval = ((Scrollbar*)((const WindowEventArgs&)e).window)->getScrollPosition();
|
---|
885 |
|
---|
886 | for (size_t i = 0; i < controlCount; i++)
|
---|
887 | {
|
---|
888 | float ypos = WIDGET_YSTART + WIDGET_YOFFSET * float(i) - scrollval;
|
---|
889 | mShaderControlContainer[i].TextWidget->setPosition(Point ( TEXTWIDGET_XPOS, ypos));
|
---|
890 | mShaderControlContainer[i].NumberWidget->setPosition(Point ( NUMBERWIDGET_XPOS, ypos));
|
---|
891 | mShaderControlContainer[i].ScrollWidget->setPosition(Point ( SCROLLWIDGET_XPOS, ypos ));
|
---|
892 | }
|
---|
893 |
|
---|
894 | return true;
|
---|
895 | }
|
---|
896 |
|
---|
897 | //--------------------------------------------------------------------------
|
---|
898 | bool OceanDemo::handleMovementTypeChange(const CEGUI::EventArgs& e)
|
---|
899 | {
|
---|
900 | using namespace CEGUI;
|
---|
901 | CEGUI::uint id = ((RadioButton*)((const WindowEventArgs&)e).window)->getSelectedButtonInGroup()->getID();
|
---|
902 | if (id == 0)
|
---|
903 | {
|
---|
904 | mMouseMovement = mv_CAMERA;
|
---|
905 | }
|
---|
906 | else
|
---|
907 | {
|
---|
908 | mMouseMovement = mv_MODEL;
|
---|
909 | }
|
---|
910 |
|
---|
911 | return true;
|
---|
912 | }
|
---|
913 |
|
---|
914 |
|
---|
915 | //--------------------------------------------------------------------------
|
---|
916 | bool OceanDemo::handleErrorBox(const CEGUI::EventArgs& e)
|
---|
917 | {
|
---|
918 | CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"ErrorBox")->hide();
|
---|
919 | return true;
|
---|
920 | }
|
---|
921 |
|
---|
922 | //--------------------------------------------------------------------------
|
---|
923 | void OceanDemo::createFrameListener(void)
|
---|
924 | {
|
---|
925 | mFrameListener= new OceanDemo_FrameListener(this);
|
---|
926 | mRoot->addFrameListener(mFrameListener);
|
---|
927 | }
|
---|
928 |
|
---|
929 |
|
---|
930 | /*************************************************************************
|
---|
931 | OceanDemo_FrameListener methods that handle all input for this GLSL demo.
|
---|
932 | *************************************************************************/
|
---|
933 |
|
---|
934 | OceanDemo_FrameListener::OceanDemo_FrameListener(OceanDemo* main)
|
---|
935 | : mMain(main),
|
---|
936 | mLMBDown(false), mRMBDown(false), mStatsOn(true), mLastMousePositionSet(false),
|
---|
937 | mMoveFwd(false), mMoveBck(false), mMoveLeft(false), mMoveRight(false), mProcessMovement(false),
|
---|
938 | mUpdateMovement(false), mSpinModel(true), mSpinLight(false), mWriteToFile(false)
|
---|
939 |
|
---|
940 | {
|
---|
941 | mRotateSpeed = 0;
|
---|
942 | mMoveSpeed = 100;
|
---|
943 |
|
---|
944 | mNumScreenShots = 0;
|
---|
945 | mTimeUntilNextToggle = 0;
|
---|
946 | mSceneDetailIndex = 0;
|
---|
947 | mMoveScale = 0.0f;
|
---|
948 | mSpeed = MINSPEED;
|
---|
949 | mRotX = 0;
|
---|
950 | mRotY = 0;
|
---|
951 | mRotScale = 0.0f;
|
---|
952 | mTranslateVector = Ogre::Vector3::ZERO;
|
---|
953 | mAniso = 1;
|
---|
954 | mFiltering = Ogre::TFO_BILINEAR;
|
---|
955 | mAvgFrameTime = 0.1;
|
---|
956 |
|
---|
957 | // using buffered input
|
---|
958 | mEventProcessor = new Ogre::EventProcessor();
|
---|
959 | mEventProcessor->initialise(mMain->getRenderWindow());
|
---|
960 | mEventProcessor->startProcessingEvents();
|
---|
961 | mEventProcessor->addKeyListener(this);
|
---|
962 | mEventProcessor->addMouseMotionListener(this);
|
---|
963 | mEventProcessor->addMouseListener(this);
|
---|
964 | mInputDevice = mEventProcessor->getInputReader();
|
---|
965 |
|
---|
966 | mQuit = false;
|
---|
967 | mSkipCount = 0;
|
---|
968 | mUpdateFreq = 50;
|
---|
969 |
|
---|
970 | mGuiRenderer = CEGUI::System::getSingleton().getRenderer();
|
---|
971 |
|
---|
972 | mGuiAvg = CEGUI::WindowManager::getSingleton().getWindow("OPAverageFPS");
|
---|
973 | mGuiCurr = CEGUI::WindowManager::getSingleton().getWindow("OPCurrentFPS");
|
---|
974 | mGuiBest = CEGUI::WindowManager::getSingleton().getWindow("OPBestFPS");
|
---|
975 | mGuiWorst = CEGUI::WindowManager::getSingleton().getWindow("OPWorstFPS");
|
---|
976 | mGuiTris = CEGUI::WindowManager::getSingleton().getWindow("OPTriCount");
|
---|
977 | mGuiDbg = CEGUI::WindowManager::getSingleton().getWindow("OPDebugMsg");
|
---|
978 | mRoot = CEGUI::WindowManager::getSingleton().getWindow("root");
|
---|
979 | }
|
---|
980 |
|
---|
981 | //--------------------------------------------------------------------------
|
---|
982 | OceanDemo_FrameListener::~OceanDemo_FrameListener()
|
---|
983 | {
|
---|
984 | delete mEventProcessor;
|
---|
985 | }
|
---|
986 |
|
---|
987 |
|
---|
988 | //--------------------------------------------------------------------------
|
---|
989 | bool OceanDemo_FrameListener::frameStarted(const Ogre::FrameEvent& evt)
|
---|
990 | {
|
---|
991 | if (mQuit)
|
---|
992 | return false;
|
---|
993 | else
|
---|
994 | {
|
---|
995 | mSkipCount++;
|
---|
996 | if (mSkipCount >= mUpdateFreq)
|
---|
997 | {
|
---|
998 | mSkipCount = 0;
|
---|
999 | updateStats();
|
---|
1000 | }
|
---|
1001 | // update movement process
|
---|
1002 | if(mProcessMovement || mUpdateMovement)
|
---|
1003 | {
|
---|
1004 | mTranslateVector.x += mMoveLeft ? mAvgFrameTime * -MOVESPEED : 0;
|
---|
1005 | mTranslateVector.x += mMoveRight ? mAvgFrameTime * MOVESPEED : 0;
|
---|
1006 | mTranslateVector.z += mMoveFwd ? mAvgFrameTime * -MOVESPEED : 0;
|
---|
1007 | mTranslateVector.z += mMoveBck ? mAvgFrameTime * MOVESPEED : 0;
|
---|
1008 | switch(mMain->getMouseMovement())
|
---|
1009 | {
|
---|
1010 | case mv_CAMERA:
|
---|
1011 | mMain->getCamera()->yaw(Ogre::Angle(mRotX));
|
---|
1012 | mMain->getCamera()->pitch(Ogre::Angle(mRotY));
|
---|
1013 | mMain->getCamera()->moveRelative(mTranslateVector);
|
---|
1014 | break;
|
---|
1015 |
|
---|
1016 | case mv_MODEL:
|
---|
1017 | mMain->getMainNode()->yaw(Ogre::Angle(mRotX));
|
---|
1018 | //mMain->getMainNode()->pitch(mRotY);
|
---|
1019 | mMain->getMainNode()->translate(mTranslateVector);
|
---|
1020 | break;
|
---|
1021 | default:
|
---|
1022 | break;
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | mUpdateMovement = false;
|
---|
1026 | mRotX = 0;
|
---|
1027 | mRotY = 0;
|
---|
1028 | mTranslateVector = Ogre::Vector3::ZERO;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | if(mSpinModel)
|
---|
1032 | {
|
---|
1033 | mRotateSpeed = mAvgFrameTime * 20;
|
---|
1034 | mMain->getMainNode()->yaw( Ogre::Angle(mRotateSpeed) );
|
---|
1035 | }
|
---|
1036 | if(mSpinLight)
|
---|
1037 | {
|
---|
1038 | mLightPivots[0]->rotate(mLightRotationAxes[0], Ogre::Angle(mRotateSpeed * 2.0f));
|
---|
1039 | }
|
---|
1040 | if(mWriteToFile)
|
---|
1041 | {
|
---|
1042 | char tmp[20];
|
---|
1043 | sprintf(tmp, "frame_%d.png", ++mNumScreenShots);
|
---|
1044 | mMain->getRenderWindow()->writeContentsToFile(tmp);
|
---|
1045 | }
|
---|
1046 | return true;
|
---|
1047 | }
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | //--------------------------------------------------------------------------
|
---|
1051 | void OceanDemo_FrameListener::mouseMoved (Ogre::MouseEvent *e)
|
---|
1052 | {
|
---|
1053 | CEGUI::System::getSingleton().injectMouseMove(e->getRelX() * mGuiRenderer->getWidth(), e->getRelY() * mGuiRenderer->getHeight());
|
---|
1054 | CEGUI::System::getSingleton().injectMouseWheelChange(e->getRelZ());
|
---|
1055 | e->consume();
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | //--------------------------------------------------------------------------
|
---|
1059 | void OceanDemo_FrameListener::mouseDragged (Ogre::MouseEvent *e)
|
---|
1060 | {
|
---|
1061 | mouseMoved(e);
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | //--------------------------------------------------------------------------
|
---|
1065 | void OceanDemo_FrameListener::keyPressed (Ogre::KeyEvent *e)
|
---|
1066 | {
|
---|
1067 | // give 'quitting' priority
|
---|
1068 | if (e->getKey() == Ogre::KC_ESCAPE)
|
---|
1069 | {
|
---|
1070 | mQuit = true;
|
---|
1071 | e->consume();
|
---|
1072 | return;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | if (e->getKey() == Ogre::KC_SYSRQ )
|
---|
1076 | {
|
---|
1077 | char tmp[20];
|
---|
1078 | sprintf(tmp, "screenshot_%d.png", ++mNumScreenShots);
|
---|
1079 | mMain->getRenderWindow()->writeContentsToFile(tmp);
|
---|
1080 | //mTimeUntilNextToggle = 0.5;
|
---|
1081 | mMain->getRenderWindow()->setDebugText(Ogre::String("Wrote ") + tmp);
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | // do event injection
|
---|
1085 | CEGUI::System& cegui = CEGUI::System::getSingleton();
|
---|
1086 |
|
---|
1087 | // key down
|
---|
1088 | cegui.injectKeyDown(e->getKey());
|
---|
1089 |
|
---|
1090 | // now character
|
---|
1091 | cegui.injectChar(e->getKeyChar());
|
---|
1092 |
|
---|
1093 | e->consume();
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | //--------------------------------------------------------------------------
|
---|
1097 | void OceanDemo_FrameListener::keyReleased (Ogre::KeyEvent *e)
|
---|
1098 | {
|
---|
1099 | CEGUI::System::getSingleton().injectKeyUp(e->getKey());
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | //--------------------------------------------------------------------------
|
---|
1103 | void OceanDemo_FrameListener::mousePressed (Ogre::MouseEvent *e)
|
---|
1104 | {
|
---|
1105 | CEGUI::System::getSingleton().injectMouseButtonDown(convertOgreButtonToCegui(e->getButtonID()));
|
---|
1106 | e->consume();
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | //--------------------------------------------------------------------------
|
---|
1110 | void OceanDemo_FrameListener::mouseReleased (Ogre::MouseEvent *e)
|
---|
1111 | {
|
---|
1112 | CEGUI::System::getSingleton().injectMouseButtonUp(convertOgreButtonToCegui(e->getButtonID()));
|
---|
1113 | e->consume();
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | //--------------------------------------------------------------------------
|
---|
1117 | CEGUI::MouseButton OceanDemo_FrameListener::convertOgreButtonToCegui(int ogre_button_id)
|
---|
1118 | {
|
---|
1119 | switch (ogre_button_id)
|
---|
1120 | {
|
---|
1121 | case Ogre::MouseEvent::BUTTON0_MASK:
|
---|
1122 | return CEGUI::LeftButton;
|
---|
1123 | break;
|
---|
1124 |
|
---|
1125 | case Ogre::MouseEvent::BUTTON1_MASK:
|
---|
1126 | return CEGUI::RightButton;
|
---|
1127 | break;
|
---|
1128 |
|
---|
1129 | case Ogre::MouseEvent::BUTTON2_MASK:
|
---|
1130 | return CEGUI::MiddleButton;
|
---|
1131 | break;
|
---|
1132 |
|
---|
1133 | case Ogre::MouseEvent::BUTTON3_MASK:
|
---|
1134 | return CEGUI::X1Button;
|
---|
1135 | break;
|
---|
1136 |
|
---|
1137 | default:
|
---|
1138 | return CEGUI::LeftButton;
|
---|
1139 | break;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | //--------------------------------------------------------------------------
|
---|
1145 | void OceanDemo_FrameListener::updateStats(void)
|
---|
1146 | {
|
---|
1147 | static CEGUI::String currFps = "Current FPS: ";
|
---|
1148 | static CEGUI::String avgFps = "Average FPS: ";
|
---|
1149 | static CEGUI::String bestFps = "Best FPS: ";
|
---|
1150 | static CEGUI::String worstFps = "Worst FPS: ";
|
---|
1151 | static CEGUI::String tris = "Triangle Count: ";
|
---|
1152 |
|
---|
1153 |
|
---|
1154 | const Ogre::RenderTarget::FrameStats& stats = mMain->getRenderWindow()->getStatistics();
|
---|
1155 |
|
---|
1156 | mGuiAvg->setText(avgFps + Ogre::StringConverter::toString(stats.avgFPS));
|
---|
1157 | mGuiCurr->setText(currFps + Ogre::StringConverter::toString(stats.lastFPS));
|
---|
1158 | mGuiBest->setText(bestFps + Ogre::StringConverter::toString(stats.bestFPS)
|
---|
1159 | + " " + Ogre::StringConverter::toString(stats.bestFrameTime)+" ms");
|
---|
1160 | mGuiWorst->setText(worstFps + Ogre::StringConverter::toString(stats.worstFPS)
|
---|
1161 | + " " + Ogre::StringConverter::toString(stats.worstFrameTime)+" ms");
|
---|
1162 |
|
---|
1163 | mGuiTris->setText(tris + Ogre::StringConverter::toString(stats.triangleCount));
|
---|
1164 | mGuiDbg->setText(mMain->getRenderWindow()->getDebugText());
|
---|
1165 | mAvgFrameTime = 1.0f/(stats.avgFPS + 1.0f);
|
---|
1166 | if (mAvgFrameTime > 0.1f) mAvgFrameTime = 0.1f;
|
---|
1167 |
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 |
|
---|
1171 | //--------------------------------------------------------------------------
|
---|
1172 | bool OceanDemo_FrameListener::handleMouseMove(const CEGUI::EventArgs& e)
|
---|
1173 | {
|
---|
1174 | using namespace CEGUI;
|
---|
1175 |
|
---|
1176 | if( mLMBDown && !mRMBDown)
|
---|
1177 | {
|
---|
1178 | // rotate camera
|
---|
1179 | mRotX += -((const MouseEventArgs&)e).moveDelta.d_x * mAvgFrameTime * 10.0;
|
---|
1180 | mRotY += -((const MouseEventArgs&)e).moveDelta.d_y * mAvgFrameTime * 10.0;
|
---|
1181 | MouseCursor::getSingleton().setPosition( mLastMousePosition );
|
---|
1182 | mUpdateMovement = true;
|
---|
1183 | }
|
---|
1184 | else
|
---|
1185 | {
|
---|
1186 | if( mRMBDown && !mLMBDown)
|
---|
1187 | {
|
---|
1188 | // translate camera
|
---|
1189 | mTranslateVector.x += ((const MouseEventArgs&)e).moveDelta.d_x * mAvgFrameTime * MOVESPEED;
|
---|
1190 | mTranslateVector.y += -((const MouseEventArgs&)e).moveDelta.d_y * mAvgFrameTime * MOVESPEED;
|
---|
1191 | //mTranslateVector.z = 0;
|
---|
1192 | MouseCursor::getSingleton().setPosition( mLastMousePosition );
|
---|
1193 | mUpdateMovement = true;
|
---|
1194 | }
|
---|
1195 | else
|
---|
1196 | {
|
---|
1197 | if( mRMBDown && mLMBDown)
|
---|
1198 | {
|
---|
1199 | mTranslateVector.z += (((const MouseEventArgs&)e).moveDelta.d_x + ((const MouseEventArgs&)e).moveDelta.d_y) * mAvgFrameTime * MOVESPEED;
|
---|
1200 | MouseCursor::getSingleton().setPosition( mLastMousePosition );
|
---|
1201 | mUpdateMovement = true;
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | }
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | return true;
|
---|
1208 | }
|
---|
1209 |
|
---|
1210 | //--------------------------------------------------------------------------
|
---|
1211 | bool OceanDemo_FrameListener::handleMouseButtonUp(const CEGUI::EventArgs& e)
|
---|
1212 | {
|
---|
1213 | using namespace CEGUI;
|
---|
1214 |
|
---|
1215 | //Window* wndw = ((const WindowEventArgs&)e).window;
|
---|
1216 | if( ((const MouseEventArgs&)e).button == LeftButton )
|
---|
1217 | {
|
---|
1218 | mLMBDown = false;
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | if( ((const MouseEventArgs&)e).button == RightButton )
|
---|
1222 | {
|
---|
1223 | mRMBDown = false;
|
---|
1224 | }
|
---|
1225 | if( !mLMBDown && !mRMBDown )
|
---|
1226 | {
|
---|
1227 | MouseCursor::getSingleton().show();
|
---|
1228 | if(mLastMousePositionSet)
|
---|
1229 | {
|
---|
1230 | MouseCursor::getSingleton().setPosition( mLastMousePosition );
|
---|
1231 | mLastMousePositionSet = false;
|
---|
1232 | }
|
---|
1233 | mRoot->releaseInput();
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | return true;
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | //--------------------------------------------------------------------------
|
---|
1240 | bool OceanDemo_FrameListener::handleMouseButtonDown(const CEGUI::EventArgs& e)
|
---|
1241 | {
|
---|
1242 | using namespace CEGUI;
|
---|
1243 |
|
---|
1244 | //Window* wndw = ((const WindowEventArgs&)e).window;
|
---|
1245 | if( ((const MouseEventArgs&)e).button == LeftButton )
|
---|
1246 | {
|
---|
1247 | mLMBDown = true;
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | if( ((const MouseEventArgs&)e).button == RightButton )
|
---|
1251 | {
|
---|
1252 | mRMBDown = true;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | if( mLMBDown || mRMBDown )
|
---|
1256 | {
|
---|
1257 | MouseCursor::getSingleton().hide();
|
---|
1258 | if (!mLastMousePositionSet)
|
---|
1259 | {
|
---|
1260 | mLastMousePosition = MouseCursor::getSingleton().getPosition();
|
---|
1261 | mLastMousePositionSet = true;
|
---|
1262 | }
|
---|
1263 | mRoot->captureInput();
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 | return true;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | //--------------------------------------------------------------------------
|
---|
1270 | bool OceanDemo_FrameListener::handleMouseWheelEvent(const CEGUI::EventArgs& e)
|
---|
1271 | {
|
---|
1272 | using namespace CEGUI;
|
---|
1273 | mTranslateVector.z += ((const MouseEventArgs&)e).wheelChange * -5.0;
|
---|
1274 | mUpdateMovement = true;
|
---|
1275 |
|
---|
1276 | return true;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | //--------------------------------------------------------------------------
|
---|
1280 | bool OceanDemo_FrameListener::handleKeyDownEvent(const CEGUI::EventArgs& e)
|
---|
1281 | {
|
---|
1282 | using namespace CEGUI;
|
---|
1283 |
|
---|
1284 | CheckMovementKeys( ((const KeyEventArgs&)e).scancode , true);
|
---|
1285 |
|
---|
1286 | return true;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | //--------------------------------------------------------------------------
|
---|
1290 | bool OceanDemo_FrameListener::handleKeyUpEvent(const CEGUI::EventArgs& e)
|
---|
1291 | {
|
---|
1292 | using namespace CEGUI;
|
---|
1293 | CheckMovementKeys( ((const KeyEventArgs&)e).scancode, false );
|
---|
1294 |
|
---|
1295 | return true;
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 |
|
---|
1299 | //--------------------------------------------------------------------------
|
---|
1300 | bool OceanDemo_FrameListener::handelModelSpinChange(const CEGUI::EventArgs& e)
|
---|
1301 | {
|
---|
1302 | using namespace CEGUI;
|
---|
1303 | mSpinModel = ((Checkbox*)((const WindowEventArgs&)e).window)->isSelected();
|
---|
1304 |
|
---|
1305 | return true;
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | //--------------------------------------------------------------------------
|
---|
1309 | bool OceanDemo_FrameListener::handelLightSpinChange(const CEGUI::EventArgs& e)
|
---|
1310 | {
|
---|
1311 | using namespace CEGUI;
|
---|
1312 | mSpinLight = ((Checkbox*)((const WindowEventArgs&)e).window)->isSelected();
|
---|
1313 |
|
---|
1314 | return true;
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | //--------------------------------------------------------------------------
|
---|
1318 | void OceanDemo_FrameListener::CheckMovementKeys( CEGUI::Key::Scan scancode, bool state )
|
---|
1319 | {
|
---|
1320 | using namespace CEGUI;
|
---|
1321 |
|
---|
1322 | switch ( scancode )
|
---|
1323 | {
|
---|
1324 | case Key::A:
|
---|
1325 | mMoveLeft = state;
|
---|
1326 | break;
|
---|
1327 |
|
---|
1328 | case Key::D:
|
---|
1329 | mMoveRight = state;
|
---|
1330 | break;
|
---|
1331 |
|
---|
1332 | case Key::S:
|
---|
1333 | mMoveBck = state;
|
---|
1334 | break;
|
---|
1335 |
|
---|
1336 | case Key::W:
|
---|
1337 | mMoveFwd = state;
|
---|
1338 | break;
|
---|
1339 |
|
---|
1340 | default:
|
---|
1341 | break;
|
---|
1342 |
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | mProcessMovement = mMoveLeft || mMoveRight || mMoveFwd || mMoveBck;
|
---|
1346 |
|
---|
1347 | }
|
---|