OpenGLExampleBrowser.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. #include "OpenGLExampleBrowser.h"
  2. #include "LinearMath/btQuickprof.h"
  3. #include "../OpenGLWindow/OpenGLInclude.h"
  4. #include "../OpenGLWindow/SimpleOpenGL2App.h"
  5. #ifndef NO_OPENGL3
  6. #include "../OpenGLWindow/SimpleOpenGL3App.h"
  7. #endif
  8. #include "../CommonInterfaces/CommonRenderInterface.h"
  9. #ifdef __APPLE__
  10. #include "../OpenGLWindow/MacOpenGLWindow.h"
  11. #else
  12. #ifdef _WIN32
  13. #include "../OpenGLWindow/Win32OpenGLWindow.h"
  14. #else
  15. //let's cross the fingers it is Linux/X11
  16. #include "../OpenGLWindow/X11OpenGLWindow.h"
  17. #endif //_WIN32
  18. #endif//__APPLE__
  19. #include "../ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.h"
  20. #include "Bullet3Common/b3Vector3.h"
  21. #include "assert.h"
  22. #include <stdio.h>
  23. #include "GwenGUISupport/gwenInternalData.h"
  24. #include "GwenGUISupport/gwenUserInterface.h"
  25. #include "../Utils/b3Clock.h"
  26. #include "GwenGUISupport/GwenParameterInterface.h"
  27. #include "GwenGUISupport/GwenProfileWindow.h"
  28. #include "GwenGUISupport/GwenTextureWindow.h"
  29. #include "GwenGUISupport/GraphingTexture.h"
  30. #include "../CommonInterfaces/Common2dCanvasInterface.h"
  31. #include "../CommonInterfaces/CommonExampleInterface.h"
  32. #include "Bullet3Common/b3CommandLineArgs.h"
  33. #include "../OpenGLWindow/SimpleCamera.h"
  34. #include "../OpenGLWindow/SimpleOpenGL2Renderer.h"
  35. #include "ExampleEntries.h"
  36. #include "OpenGLGuiHelper.h"
  37. #include "Bullet3Common/b3FileUtils.h"
  38. #include "LinearMath/btIDebugDraw.h"
  39. //quick test for file import, @todo(erwincoumans) make it more general and add other file formats
  40. #include "../Importers/ImportURDFDemo/ImportURDFSetup.h"
  41. #include "../Importers/ImportBullet/SerializeSetup.h"
  42. static CommonGraphicsApp* s_app=0;
  43. static CommonWindowInterface* s_window = 0;
  44. static CommonParameterInterface* s_parameterInterface=0;
  45. static CommonRenderInterface* s_instancingRenderer=0;
  46. static OpenGLGuiHelper* s_guiHelper=0;
  47. static MyProfileWindow* s_profWindow =0;
  48. #define DEMO_SELECTION_COMBOBOX 13
  49. const char* startFileName = "0_Bullet3Demo.txt";
  50. char staticPngFileName[1024];
  51. static GwenUserInterface* gui = 0;
  52. static int sCurrentDemoIndex = -1;
  53. static int sCurrentHightlighted = 0;
  54. static CommonExampleInterface* sCurrentDemo = 0;
  55. static b3AlignedObjectArray<const char*> allNames;
  56. static float gFixedTimeStep = 0;
  57. bool gAllowRetina = true;
  58. static class ExampleEntries* gAllExamples=0;
  59. bool sUseOpenGL2 = false;
  60. bool drawGUI=true;
  61. #ifndef USE_OPENGL3
  62. extern bool useShadowMap;
  63. #endif
  64. static bool visualWireframe=false;
  65. static bool renderVisualGeometry=true;
  66. static bool renderGrid = true;
  67. static bool renderGui = true;
  68. static bool enable_experimental_opencl = false;
  69. int gDebugDrawFlags = 0;
  70. static bool pauseSimulation=false;
  71. int midiBaseIndex = 176;
  72. extern bool gDisableDeactivation;
  73. int gSharedMemoryKey=-1;
  74. ///some quick test variable for the OpenCL examples
  75. int gPreferredOpenCLDeviceIndex=-1;
  76. int gPreferredOpenCLPlatformIndex=-1;
  77. int gGpuArraySizeX=15;
  78. int gGpuArraySizeY=15;
  79. int gGpuArraySizeZ=15;
  80. //#include <float.h>
  81. //unsigned int fp_control_state = _controlfp(_EM_INEXACT, _MCW_EM);
  82. void deleteDemo()
  83. {
  84. if (sCurrentDemo)
  85. {
  86. sCurrentDemo->exitPhysics();
  87. s_instancingRenderer->removeAllInstances();
  88. delete sCurrentDemo;
  89. sCurrentDemo=0;
  90. delete s_guiHelper;
  91. s_guiHelper = 0;
  92. }
  93. }
  94. const char* gPngFileName = 0;
  95. b3KeyboardCallback prevKeyboardCallback = 0;
  96. void MyKeyboardCallback(int key, int state)
  97. {
  98. //b3Printf("key=%d, state=%d", key, state);
  99. bool handled = false;
  100. if (gui && !handled )
  101. {
  102. handled = gui->keyboardCallback(key, state);
  103. }
  104. if (!handled && sCurrentDemo)
  105. {
  106. handled = sCurrentDemo->keyboardCallback(key,state);
  107. }
  108. //checkout: is it desired to ignore keys, if the demo already handles them?
  109. //if (handled)
  110. // return;
  111. if (key=='a' && state)
  112. {
  113. gDebugDrawFlags ^= btIDebugDraw::DBG_DrawAabb;
  114. }
  115. if (key=='c' && state)
  116. {
  117. gDebugDrawFlags ^= btIDebugDraw::DBG_DrawConstraints;
  118. gDebugDrawFlags ^= btIDebugDraw::DBG_DrawContactPoints;
  119. }
  120. if (key == 'd' && state)
  121. {
  122. gDebugDrawFlags ^= btIDebugDraw::DBG_NoDeactivation;
  123. gDisableDeactivation = ((gDebugDrawFlags & btIDebugDraw::DBG_NoDeactivation) != 0);
  124. }
  125. if (key=='l' && state)
  126. {
  127. gDebugDrawFlags ^= btIDebugDraw::DBG_DrawConstraintLimits;
  128. }
  129. if (key=='w' && state)
  130. {
  131. visualWireframe=!visualWireframe;
  132. gDebugDrawFlags ^= btIDebugDraw::DBG_DrawWireframe;
  133. }
  134. if (key=='v' && state)
  135. {
  136. renderVisualGeometry = !renderVisualGeometry;
  137. }
  138. if (key=='g' && state)
  139. {
  140. renderGrid = !renderGrid;
  141. renderGui = !renderGui;
  142. }
  143. if (key=='i' && state)
  144. {
  145. pauseSimulation = !pauseSimulation;
  146. }
  147. #ifndef NO_OPENGL3
  148. if (key=='s' && state)
  149. {
  150. useShadowMap=!useShadowMap;
  151. }
  152. #endif
  153. if (key==B3G_F1)
  154. {
  155. static int count=0;
  156. if (state)
  157. {
  158. b3Printf("F1 pressed %d", count++);
  159. if (gPngFileName)
  160. {
  161. b3Printf("disable image dump");
  162. gPngFileName=0;
  163. } else
  164. {
  165. gPngFileName = gAllExamples->getExampleName(sCurrentDemoIndex);
  166. b3Printf("enable image dump %s",gPngFileName);
  167. }
  168. } else
  169. {
  170. b3Printf("F1 released %d",count++);
  171. }
  172. }
  173. if (key==B3G_ESCAPE && s_window)
  174. {
  175. s_window->setRequestExit();
  176. }
  177. if (prevKeyboardCallback)
  178. prevKeyboardCallback(key,state);
  179. }
  180. b3MouseMoveCallback prevMouseMoveCallback = 0;
  181. static void MyMouseMoveCallback( float x, float y)
  182. {
  183. bool handled = false;
  184. if (sCurrentDemo)
  185. handled = sCurrentDemo->mouseMoveCallback(x,y);
  186. if (!handled && gui)
  187. handled = gui->mouseMoveCallback(x,y);
  188. if (!handled)
  189. {
  190. if (prevMouseMoveCallback)
  191. prevMouseMoveCallback(x,y);
  192. }
  193. }
  194. b3MouseButtonCallback prevMouseButtonCallback = 0;
  195. static void MyMouseButtonCallback(int button, int state, float x, float y)
  196. {
  197. bool handled = false;
  198. //try picking first
  199. if (sCurrentDemo)
  200. handled = sCurrentDemo->mouseButtonCallback(button,state,x,y);
  201. if (!handled && gui)
  202. handled = gui->mouseButtonCallback(button,state,x,y);
  203. if (!handled)
  204. {
  205. if (prevMouseButtonCallback )
  206. prevMouseButtonCallback (button,state,x,y);
  207. }
  208. // b3DefaultMouseButtonCallback(button,state,x,y);
  209. }
  210. #include <string.h>
  211. void openFileDemo(const char* filename)
  212. {
  213. if (sCurrentDemo)
  214. {
  215. sCurrentDemo->exitPhysics();
  216. s_instancingRenderer->removeAllInstances();
  217. delete sCurrentDemo;
  218. sCurrentDemo=0;
  219. delete s_guiHelper;
  220. s_guiHelper = 0;
  221. }
  222. s_guiHelper= new OpenGLGuiHelper(s_app, sUseOpenGL2);
  223. s_parameterInterface->removeAllParameters();
  224. CommonExampleOptions options(s_guiHelper,1);
  225. options.m_fileName = filename;
  226. char fullPath[1024];
  227. sprintf(fullPath, "%s", filename);
  228. b3FileUtils::toLower(fullPath);
  229. if (strstr(fullPath, ".urdf"))
  230. {
  231. sCurrentDemo = ImportURDFCreateFunc(options);
  232. } else
  233. {
  234. if (strstr(fullPath, ".bullet"))
  235. {
  236. sCurrentDemo = SerializeBulletCreateFunc(options);
  237. }
  238. }
  239. //physicsSetup->setFileName(filename);
  240. if (sCurrentDemo)
  241. {
  242. sCurrentDemo->initPhysics();
  243. sCurrentDemo->resetCamera();
  244. }
  245. }
  246. void selectDemo(int demoIndex)
  247. {
  248. bool resetCamera = (sCurrentDemoIndex != demoIndex);
  249. sCurrentDemoIndex = demoIndex;
  250. sCurrentHightlighted = demoIndex;
  251. int numDemos = gAllExamples->getNumRegisteredExamples();
  252. if (demoIndex>numDemos)
  253. {
  254. demoIndex = 0;
  255. }
  256. deleteDemo();
  257. CommonExampleInterface::CreateFunc* func = gAllExamples->getExampleCreateFunc(demoIndex);
  258. if (func)
  259. {
  260. s_parameterInterface->removeAllParameters();
  261. int option = gAllExamples->getExampleOption(demoIndex);
  262. s_guiHelper= new OpenGLGuiHelper(s_app, sUseOpenGL2);
  263. CommonExampleOptions options(s_guiHelper, option);
  264. sCurrentDemo = (*func)(options);
  265. if (sCurrentDemo)
  266. {
  267. if (gui)
  268. {
  269. gui->setStatusBarMessage("Status: OK", false);
  270. }
  271. b3Printf("Selected demo: %s",gAllExamples->getExampleName(demoIndex));
  272. gui->setExampleDescription(gAllExamples->getExampleDescription(demoIndex));
  273. sCurrentDemo->initPhysics();
  274. if(resetCamera)
  275. {
  276. sCurrentDemo->resetCamera();
  277. }
  278. }
  279. }
  280. }
  281. #include <stdio.h>
  282. static void saveCurrentSettings(int currentEntry,const char* startFileName)
  283. {
  284. FILE* f = fopen(startFileName,"w");
  285. if (f)
  286. {
  287. fprintf(f,"--start_demo_name=%s\n", gAllExamples->getExampleName(sCurrentDemoIndex));
  288. fprintf(f,"--mouse_move_multiplier=%f\n", s_app->getMouseMoveMultiplier());
  289. fprintf(f,"--mouse_wheel_multiplier=%f\n", s_app->getMouseWheelMultiplier());
  290. float red,green,blue;
  291. s_app->getBackgroundColor(&red,&green,&blue);
  292. fprintf(f,"--background_color_red= %f\n", red);
  293. fprintf(f,"--background_color_green= %f\n", green);
  294. fprintf(f,"--background_color_blue= %f\n", blue);
  295. fprintf(f,"--fixed_timestep= %f\n", gFixedTimeStep);
  296. if (!gAllowRetina)
  297. {
  298. fprintf(f,"--disable_retina");
  299. }
  300. if (enable_experimental_opencl)
  301. {
  302. fprintf(f,"--enable_experimental_opencl\n");
  303. }
  304. if (sUseOpenGL2 )
  305. {
  306. fprintf(f,"--opengl2\n");
  307. }
  308. fclose(f);
  309. }
  310. };
  311. static void loadCurrentSettings(const char* startFileName, b3CommandLineArgs& args)
  312. {
  313. int currentEntry= 0;
  314. FILE* f = fopen(startFileName,"r");
  315. if (f)
  316. {
  317. int result;
  318. char oneline[1024];
  319. char* argv[] = {0,&oneline[0]};
  320. while( fgets (oneline, 1024, f)!=NULL )
  321. {
  322. char *pos;
  323. if ((pos=strchr(oneline, '\n')) != NULL)
  324. *pos = '\0';
  325. args.addArgs(2,argv);
  326. }
  327. fclose(f);
  328. }
  329. };
  330. void MyComboBoxCallback(int comboId, const char* item)
  331. {
  332. //printf("comboId = %d, item = %s\n",comboId, item);
  333. if (comboId==DEMO_SELECTION_COMBOBOX)
  334. {
  335. //find selected item
  336. for (int i=0;i<allNames.size();i++)
  337. {
  338. if (strcmp(item,allNames[i])==0)
  339. {
  340. selectDemo(i);
  341. saveCurrentSettings(sCurrentDemoIndex,startFileName);
  342. break;
  343. }
  344. }
  345. }
  346. }
  347. void MyGuiPrintf(const char* msg)
  348. {
  349. printf("b3Printf: %s\n",msg);
  350. if (gui)
  351. {
  352. gui->textOutput(msg);
  353. gui->forceUpdateScrollBars();
  354. }
  355. }
  356. void MyStatusBarPrintf(const char* msg)
  357. {
  358. printf("b3Printf: %s\n", msg);
  359. if (gui)
  360. {
  361. bool isLeft = true;
  362. gui->setStatusBarMessage(msg,isLeft);
  363. }
  364. }
  365. void MyStatusBarError(const char* msg)
  366. {
  367. printf("Warning: %s\n", msg);
  368. if (gui)
  369. {
  370. bool isLeft = false;
  371. gui->setStatusBarMessage(msg,isLeft);
  372. gui->textOutput(msg);
  373. gui->forceUpdateScrollBars();
  374. }
  375. }
  376. struct MyMenuItemHander :public Gwen::Event::Handler
  377. {
  378. int m_buttonId;
  379. MyMenuItemHander( int buttonId)
  380. :m_buttonId(buttonId)
  381. {
  382. }
  383. void onButtonA(Gwen::Controls::Base* pControl)
  384. {
  385. //const Gwen::String& name = pControl->GetName();
  386. Gwen::Controls::TreeNode* node = (Gwen::Controls::TreeNode*)pControl;
  387. // Gwen::Controls::Label* l = node->GetButton();
  388. Gwen::UnicodeString la = node->GetButton()->GetText();// node->GetButton()->GetName();// GetText();
  389. Gwen::String laa = Gwen::Utility::UnicodeToString(la);
  390. // const char* ha = laa.c_str();
  391. //printf("selected %s\n", ha);
  392. //int dep = but->IsDepressed();
  393. //int tog = but->GetToggleState();
  394. // if (m_data->m_toggleButtonCallback)
  395. // (*m_data->m_toggleButtonCallback)(m_buttonId, tog);
  396. }
  397. void onButtonB(Gwen::Controls::Base* pControl)
  398. {
  399. Gwen::Controls::Label* label = (Gwen::Controls::Label*) pControl;
  400. Gwen::UnicodeString la = label->GetText();// node->GetButton()->GetName();// GetText();
  401. Gwen::String laa = Gwen::Utility::UnicodeToString(la);
  402. //const char* ha = laa.c_str();
  403. selectDemo(sCurrentHightlighted);
  404. saveCurrentSettings(sCurrentDemoIndex, startFileName);
  405. }
  406. void onButtonC(Gwen::Controls::Base* pControl)
  407. {
  408. /*Gwen::Controls::Label* label = (Gwen::Controls::Label*) pControl;
  409. Gwen::UnicodeString la = label->GetText();// node->GetButton()->GetName();// GetText();
  410. Gwen::String laa = Gwen::Utility::UnicodeToString(la);
  411. const char* ha = laa.c_str();
  412. printf("onButtonC ! %s\n", ha);
  413. */
  414. }
  415. void onButtonD(Gwen::Controls::Base* pControl)
  416. {
  417. /* Gwen::Controls::Label* label = (Gwen::Controls::Label*) pControl;
  418. Gwen::UnicodeString la = label->GetText();// node->GetButton()->GetName();// GetText();
  419. Gwen::String laa = Gwen::Utility::UnicodeToString(la);
  420. const char* ha = laa.c_str();
  421. */
  422. // printf("onKeyReturn ! \n");
  423. selectDemo(sCurrentHightlighted);
  424. saveCurrentSettings(sCurrentDemoIndex, startFileName);
  425. }
  426. void onButtonE(Gwen::Controls::Base* pControl)
  427. {
  428. // printf("select %d\n",m_buttonId);
  429. sCurrentHightlighted = m_buttonId;
  430. gui->setExampleDescription(gAllExamples->getExampleDescription(sCurrentHightlighted));
  431. }
  432. void onButtonF(Gwen::Controls::Base* pControl)
  433. {
  434. //printf("selection changed!\n");
  435. }
  436. void onButtonG(Gwen::Controls::Base* pControl)
  437. {
  438. //printf("onButtonG !\n");
  439. }
  440. };
  441. #include "Bullet3Common/b3HashMap.h"
  442. struct GL3TexLoader : public MyTextureLoader
  443. {
  444. b3HashMap<b3HashString,GLint> m_hashMap;
  445. virtual void LoadTexture( Gwen::Texture* pTexture )
  446. {
  447. Gwen::String namestr = pTexture->name.Get();
  448. const char* n = namestr.c_str();
  449. GLint* texIdPtr = m_hashMap[n];
  450. if (texIdPtr)
  451. {
  452. pTexture->m_intData = *texIdPtr;
  453. }
  454. }
  455. virtual void FreeTexture( Gwen::Texture* pTexture )
  456. {
  457. }
  458. };
  459. void quitCallback()
  460. {
  461. s_window->setRequestExit();
  462. }
  463. void fileOpenCallback()
  464. {
  465. char filename[1024];
  466. int len = s_window->fileOpenDialog(filename,1024);
  467. if (len)
  468. {
  469. //todo(erwincoumans) check if it is actually URDF
  470. //printf("file open:%s\n", filename);
  471. openFileDemo(filename);
  472. }
  473. }
  474. #define MAX_GRAPH_WINDOWS 5
  475. struct QuickCanvas : public Common2dCanvasInterface
  476. {
  477. GL3TexLoader* m_myTexLoader;
  478. MyGraphWindow* m_gw[MAX_GRAPH_WINDOWS];
  479. GraphingTexture* m_gt[MAX_GRAPH_WINDOWS];
  480. int m_curNumGraphWindows;
  481. QuickCanvas(GL3TexLoader* myTexLoader)
  482. :m_myTexLoader(myTexLoader),
  483. m_curNumGraphWindows(0)
  484. {
  485. for (int i=0;i<MAX_GRAPH_WINDOWS;i++)
  486. {
  487. m_gw[i] = 0;
  488. m_gt[i] = 0;
  489. }
  490. }
  491. virtual ~QuickCanvas() {}
  492. virtual int createCanvas(const char* canvasName, int width, int height)
  493. {
  494. if (m_curNumGraphWindows<MAX_GRAPH_WINDOWS)
  495. {
  496. //find a slot
  497. int slot = m_curNumGraphWindows;
  498. btAssert(slot<MAX_GRAPH_WINDOWS);
  499. if (slot>=MAX_GRAPH_WINDOWS)
  500. return 0;//don't crash
  501. m_curNumGraphWindows++;
  502. MyGraphInput input(gui->getInternalData());
  503. input.m_width=width;
  504. input.m_height=height;
  505. input.m_xPos = 10000;//GUI will clamp it to the right//300;
  506. input.m_yPos = 10000;//GUI will clamp it to bottom
  507. input.m_name=canvasName;
  508. input.m_texName = canvasName;
  509. m_gt[slot] = new GraphingTexture;
  510. m_gt[slot]->create(width,height);
  511. int texId = m_gt[slot]->getTextureId();
  512. m_myTexLoader->m_hashMap.insert(canvasName, texId);
  513. m_gw[slot] = setupTextureWindow(input);
  514. return slot;
  515. }
  516. return -1;
  517. }
  518. virtual void destroyCanvas(int canvasId)
  519. {
  520. btAssert(canvasId>=0);
  521. destroyTextureWindow(m_gw[canvasId]);
  522. m_curNumGraphWindows--;
  523. }
  524. virtual void setPixel(int canvasId, int x, int y, unsigned char red, unsigned char green,unsigned char blue, unsigned char alpha)
  525. {
  526. btAssert(canvasId>=0);
  527. btAssert(canvasId<m_curNumGraphWindows);
  528. m_gt[canvasId]->setPixel(x,y,red,green,blue,alpha);
  529. }
  530. virtual void getPixel(int canvasId, int x, int y, unsigned char& red, unsigned char& green,unsigned char& blue, unsigned char& alpha)
  531. {
  532. btAssert(canvasId>=0);
  533. btAssert(canvasId<m_curNumGraphWindows);
  534. m_gt[canvasId]->getPixel(x,y,red,green,blue,alpha);
  535. }
  536. virtual void refreshImageData(int canvasId)
  537. {
  538. m_gt[canvasId]->uploadImageData();
  539. }
  540. };
  541. OpenGLExampleBrowser::OpenGLExampleBrowser(class ExampleEntries* examples)
  542. {
  543. gAllExamples = examples;
  544. }
  545. OpenGLExampleBrowser::~OpenGLExampleBrowser()
  546. {
  547. deleteDemo();
  548. gAllExamples = 0;
  549. }
  550. #include "EmptyExample.h"
  551. bool OpenGLExampleBrowser::init(int argc, char* argv[])
  552. {
  553. b3CommandLineArgs args(argc,argv);
  554. loadCurrentSettings(startFileName, args);
  555. args.GetCmdLineArgument("fixed_timestep",gFixedTimeStep);
  556. ///The OpenCL rigid body pipeline is experimental and
  557. ///most OpenCL drivers and OpenCL compilers have issues with our kernels.
  558. ///If you have a high-end desktop GPU such as AMD 7970 or better, or NVIDIA GTX 680 with up-to-date drivers
  559. ///you could give it a try
  560. ///Note that several old OpenCL physics examples still have to be ported over to this new Example Browser
  561. if (args.CheckCmdLineFlag("enable_experimental_opencl"))
  562. {
  563. enable_experimental_opencl = true;
  564. gAllExamples->initOpenCLExampleEntries();
  565. }
  566. if (args.CheckCmdLineFlag("disable_retina"))
  567. {
  568. gAllowRetina = false;
  569. }
  570. int width = 1024;
  571. int height=768;
  572. #ifndef NO_OPENGL3
  573. SimpleOpenGL3App* simpleApp=0;
  574. sUseOpenGL2 =args.CheckCmdLineFlag("opengl2");
  575. #else
  576. sUseOpenGL2 = true;
  577. #endif
  578. const char* appTitle = "Bullet Physics ExampleBrowser";
  579. #if defined (_DEBUG) || defined (DEBUG)
  580. const char* optMode = "Debug build (slow)";
  581. #else
  582. const char* optMode = "Release build";
  583. #endif
  584. if (sUseOpenGL2 )
  585. {
  586. char title[1024];
  587. sprintf(title,"%s using limited OpenGL2 fallback. %s", appTitle,optMode);
  588. s_app = new SimpleOpenGL2App(title,width,height);
  589. s_app->m_renderer = new SimpleOpenGL2Renderer(width,height);
  590. }
  591. #ifndef NO_OPENGL3
  592. else
  593. {
  594. char title[1024];
  595. sprintf(title,"%s using OpenGL3+. %s", appTitle,optMode);
  596. simpleApp = new SimpleOpenGL3App(title,width,height, gAllowRetina);
  597. s_app = simpleApp;
  598. }
  599. #endif
  600. char* gVideoFileName = 0;
  601. args.GetCmdLineArgument("mp4",gVideoFileName);
  602. #ifndef NO_OPENGL3
  603. if (gVideoFileName)
  604. simpleApp->dumpFramesToVideo(gVideoFileName);
  605. #endif
  606. s_instancingRenderer = s_app->m_renderer;
  607. s_window = s_app->m_window;
  608. prevMouseMoveCallback = s_window->getMouseMoveCallback();
  609. s_window->setMouseMoveCallback(MyMouseMoveCallback);
  610. prevMouseButtonCallback = s_window->getMouseButtonCallback();
  611. s_window->setMouseButtonCallback(MyMouseButtonCallback);
  612. prevKeyboardCallback = s_window->getKeyboardCallback();
  613. s_window->setKeyboardCallback(MyKeyboardCallback);
  614. s_app->m_renderer->getActiveCamera()->setCameraDistance(13);
  615. s_app->m_renderer->getActiveCamera()->setCameraPitch(0);
  616. s_app->m_renderer->getActiveCamera()->setCameraTargetPosition(0,0,0);
  617. float mouseMoveMult= s_app->getMouseMoveMultiplier();
  618. if (args.GetCmdLineArgument("mouse_move_multiplier", mouseMoveMult))
  619. {
  620. s_app->setMouseMoveMultiplier(mouseMoveMult);
  621. }
  622. float mouseWheelMult= s_app->getMouseWheelMultiplier();
  623. if (args.GetCmdLineArgument("mouse_wheel_multiplier",mouseWheelMult))
  624. {
  625. s_app->setMouseWheelMultiplier(mouseWheelMult);
  626. }
  627. args.GetCmdLineArgument("shared_memory_key", gSharedMemoryKey);
  628. float red,green,blue;
  629. s_app->getBackgroundColor(&red,&green,&blue);
  630. args.GetCmdLineArgument("background_color_red",red);
  631. args.GetCmdLineArgument("background_color_green",green);
  632. args.GetCmdLineArgument("background_color_blue",blue);
  633. s_app->setBackgroundColor(red,green,blue);
  634. b3SetCustomWarningMessageFunc(MyGuiPrintf);
  635. b3SetCustomPrintfFunc(MyGuiPrintf);
  636. b3SetCustomErrorMessageFunc(MyStatusBarError);
  637. assert(glGetError()==GL_NO_ERROR);
  638. gui = new GwenUserInterface;
  639. GL3TexLoader* myTexLoader = new GL3TexLoader;
  640. Gwen::Renderer::Base* gwenRenderer = 0;
  641. if (sUseOpenGL2 )
  642. {
  643. gwenRenderer = new Gwen::Renderer::OpenGL_DebugFont();
  644. }
  645. #ifndef NO_OPENGL3
  646. else
  647. {
  648. sth_stash* fontstash=simpleApp->getFontStash();
  649. gwenRenderer = new GwenOpenGL3CoreRenderer(simpleApp->m_primRenderer,fontstash,width,height,s_window->getRetinaScale(),myTexLoader);
  650. }
  651. #endif
  652. //
  653. gui->init(width,height,gwenRenderer,s_window->getRetinaScale());
  654. // gui->getInternalData()->m_explorerPage
  655. Gwen::Controls::TreeControl* tree = gui->getInternalData()->m_explorerTreeCtrl;
  656. //gui->getInternalData()->pRenderer->setTextureLoader(myTexLoader);
  657. s_profWindow= setupProfileWindow(gui->getInternalData());
  658. profileWindowSetVisible(s_profWindow,false);
  659. gui->setFocus();
  660. s_parameterInterface = s_app->m_parameterInterface = new GwenParameterInterface(gui->getInternalData());
  661. s_app->m_2dCanvasInterface = new QuickCanvas(myTexLoader);
  662. ///add some demos to the gAllExamples
  663. int numDemos = gAllExamples->getNumRegisteredExamples();
  664. //char nodeText[1024];
  665. //int curDemo = 0;
  666. int selectedDemo = 0;
  667. Gwen::Controls::TreeNode* curNode = tree;
  668. MyMenuItemHander* handler2 = new MyMenuItemHander(-1);
  669. char* demoNameFromCommandOption = 0;
  670. args.GetCmdLineArgument("start_demo_name", demoNameFromCommandOption);
  671. if (demoNameFromCommandOption) {
  672. selectedDemo = -1;
  673. }
  674. tree->onReturnKeyDown.Add(handler2, &MyMenuItemHander::onButtonD);
  675. int firstAvailableDemoIndex=-1;
  676. Gwen::Controls::TreeNode* firstNode=0;
  677. for (int d = 0; d<numDemos; d++)
  678. {
  679. // sprintf(nodeText, "Node %d", i);
  680. Gwen::UnicodeString nodeUText = Gwen::Utility::StringToUnicode(gAllExamples->getExampleName(d));
  681. if (gAllExamples->getExampleCreateFunc(d))//was test for gAllExamples[d].m_menuLevel==1
  682. {
  683. Gwen::Controls::TreeNode* pNode = curNode->AddNode(nodeUText);
  684. if (firstAvailableDemoIndex<0)
  685. {
  686. firstAvailableDemoIndex = d;
  687. firstNode = pNode;
  688. }
  689. if (d == selectedDemo)
  690. {
  691. firstAvailableDemoIndex = d;
  692. firstNode = pNode;
  693. //pNode->SetSelected(true);
  694. //tree->ExpandAll();
  695. // tree->ForceUpdateScrollBars();
  696. //tree->OnKeyLeft(true);
  697. // tree->OnKeyRight(true);
  698. //tree->ExpandAll();
  699. // selectDemo(d);
  700. }
  701. if (demoNameFromCommandOption )
  702. {
  703. const char* demoName = gAllExamples->getExampleName(d);
  704. int res = strcmp(demoName, demoNameFromCommandOption);
  705. if (res==0)
  706. {
  707. firstAvailableDemoIndex = d;
  708. firstNode = pNode;
  709. }
  710. }
  711. MyMenuItemHander* handler = new MyMenuItemHander(d);
  712. pNode->onNamePress.Add(handler, &MyMenuItemHander::onButtonA);
  713. pNode->GetButton()->onDoubleClick.Add(handler, &MyMenuItemHander::onButtonB);
  714. pNode->GetButton()->onDown.Add(handler, &MyMenuItemHander::onButtonC);
  715. pNode->onSelect.Add(handler, &MyMenuItemHander::onButtonE);
  716. pNode->onReturnKeyDown.Add(handler, &MyMenuItemHander::onButtonG);
  717. pNode->onSelectChange.Add(handler, &MyMenuItemHander::onButtonF);
  718. // pNode->onKeyReturn.Add(handler, &MyMenuItemHander::onButtonD);
  719. // pNode->GetButton()->onKeyboardReturn.Add(handler, &MyMenuItemHander::onButtonD);
  720. // pNode->onNamePress.Add(handler, &MyMenuItemHander::onButtonD);
  721. // pNode->onKeyboardPressed.Add(handler, &MyMenuItemHander::onButtonD);
  722. // pNode->OnKeyPress
  723. }
  724. else
  725. {
  726. curNode = tree->AddNode(nodeUText);
  727. }
  728. }
  729. if (sCurrentDemo==0)
  730. {
  731. if (firstAvailableDemoIndex>=0)
  732. {
  733. firstNode->SetSelected(true);
  734. while (firstNode != tree)
  735. {
  736. firstNode->ExpandAll();
  737. firstNode = (Gwen::Controls::TreeNode*)firstNode->GetParent();
  738. }
  739. selectDemo(firstAvailableDemoIndex);
  740. }
  741. }
  742. btAssert(sCurrentDemo!=0);
  743. if (sCurrentDemo==0)
  744. {
  745. printf("Error, no demo/example\n");
  746. exit(0);
  747. }
  748. gui->registerFileOpenCallback(fileOpenCallback);
  749. gui->registerQuitCallback(quitCallback);
  750. return true;
  751. }
  752. CommonExampleInterface* OpenGLExampleBrowser::getCurrentExample()
  753. {
  754. btAssert(sCurrentDemo);
  755. return sCurrentDemo;
  756. }
  757. bool OpenGLExampleBrowser::requestedExit()
  758. {
  759. return s_window->requestedExit();
  760. }
  761. void OpenGLExampleBrowser::update(float deltaTime)
  762. {
  763. assert(glGetError()==GL_NO_ERROR);
  764. s_instancingRenderer->init();
  765. DrawGridData dg;
  766. dg.upAxis = s_app->getUpAxis();
  767. {
  768. BT_PROFILE("Update Camera and Light");
  769. s_instancingRenderer->updateCamera(dg.upAxis);
  770. }
  771. if (renderGrid)
  772. {
  773. BT_PROFILE("Draw Grid");
  774. glPolygonOffset(3.0, 3);
  775. glEnable(GL_POLYGON_OFFSET_FILL);
  776. s_app->drawGrid(dg);
  777. }
  778. static int frameCount = 0;
  779. frameCount++;
  780. if (0)
  781. {
  782. BT_PROFILE("Draw frame counter");
  783. char bla[1024];
  784. sprintf(bla,"Frame %d", frameCount);
  785. s_app->drawText(bla,10,10);
  786. }
  787. if (sCurrentDemo)
  788. {
  789. if (!pauseSimulation)
  790. {
  791. //printf("---------------------------------------------------\n");
  792. //printf("Framecount = %d\n",frameCount);
  793. if (gPngFileName)
  794. {
  795. static int skip = 0;
  796. skip++;
  797. if (skip>4)
  798. {
  799. skip=0;
  800. //printf("gPngFileName=%s\n",gPngFileName);
  801. static int s_frameCount = 100;
  802. sprintf(staticPngFileName,"%s%d.png",gPngFileName,s_frameCount++);
  803. //b3Printf("Made screenshot %s",staticPngFileName);
  804. s_app->dumpNextFrameToPng(staticPngFileName);
  805. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  806. }
  807. }
  808. if (gFixedTimeStep>0)
  809. {
  810. sCurrentDemo->stepSimulation(gFixedTimeStep);
  811. } else
  812. {
  813. sCurrentDemo->stepSimulation(deltaTime);//1./60.f);
  814. }
  815. }
  816. if (renderVisualGeometry && ((gDebugDrawFlags&btIDebugDraw::DBG_DrawWireframe)==0))
  817. {
  818. if (visualWireframe)
  819. {
  820. glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
  821. }
  822. BT_PROFILE("Render Scene");
  823. sCurrentDemo->renderScene();
  824. }
  825. {
  826. glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  827. sCurrentDemo->physicsDebugDraw(gDebugDrawFlags);
  828. }
  829. }
  830. {
  831. if (s_guiHelper && s_guiHelper->getRenderInterface() && s_guiHelper->getRenderInterface()->getActiveCamera())
  832. {
  833. char msg[1024];
  834. float camDist = s_guiHelper->getRenderInterface()->getActiveCamera()->getCameraDistance();
  835. float pitch = s_guiHelper->getRenderInterface()->getActiveCamera()->getCameraPitch();
  836. float yaw = s_guiHelper->getRenderInterface()->getActiveCamera()->getCameraYaw();
  837. float camTarget[3];
  838. s_guiHelper->getRenderInterface()->getActiveCamera()->getCameraTargetPosition(camTarget);
  839. sprintf(msg,"dist=%f, pitch=%f, yaw=%f,target=%f,%f,%f", camDist,pitch,yaw,camTarget[0],camTarget[1],camTarget[2]);
  840. gui->setStatusBarMessage(msg, true);
  841. }
  842. }
  843. static int toggle = 1;
  844. if (renderGui)
  845. {
  846. if (!pauseSimulation)
  847. processProfileData(s_profWindow,false);
  848. if (sUseOpenGL2)
  849. {
  850. saveOpenGLState(s_instancingRenderer->getScreenWidth(),s_instancingRenderer->getScreenHeight());
  851. }
  852. BT_PROFILE("Draw Gwen GUI");
  853. gui->draw(s_instancingRenderer->getScreenWidth(),s_instancingRenderer->getScreenHeight());
  854. if (sUseOpenGL2)
  855. {
  856. restoreOpenGLState();
  857. }
  858. }
  859. toggle=1-toggle;
  860. {
  861. BT_PROFILE("Sync Parameters");
  862. s_parameterInterface->syncParameters();
  863. }
  864. {
  865. BT_PROFILE("Swap Buffers");
  866. s_app->swapBuffer();
  867. }
  868. gui->forceUpdateScrollBars();
  869. }