main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //#define USE_OPENGL2
  2. #ifdef USE_OPENGL2
  3. #include "OpenGLWindow/SimpleOpenGL2App.h"
  4. typedef SimpleOpenGL2App SimpleOpenGLApp;
  5. #else
  6. #include "OpenGLWindow/SimpleOpenGL3App.h"
  7. typedef SimpleOpenGL3App SimpleOpenGLApp;
  8. #endif //USE_OPENGL2
  9. #include "Bullet3Common/b3Quaternion.h"
  10. #include "Bullet3Common/b3CommandLineArgs.h"
  11. #include "assert.h"
  12. #include <stdio.h>
  13. static char* gVideoFileName = 0;
  14. static char* gPngFileName = 0;
  15. static b3WheelCallback sOldWheelCB = 0;
  16. static b3ResizeCallback sOldResizeCB = 0;
  17. static b3MouseMoveCallback sOldMouseMoveCB = 0;
  18. static b3MouseButtonCallback sOldMouseButtonCB = 0;
  19. static b3KeyboardCallback sOldKeyboardCB = 0;
  20. //static b3RenderCallback sOldRenderCB = 0;
  21. static float gWidth = 1024;
  22. static float gHeight = 768;
  23. void MyWheelCallback2(float deltax, float deltay)
  24. {
  25. if (sOldWheelCB)
  26. sOldWheelCB(deltax, deltay);
  27. }
  28. void MyResizeCallback2(float width, float height)
  29. {
  30. gWidth = width;
  31. gHeight = height;
  32. if (sOldResizeCB)
  33. sOldResizeCB(width, height);
  34. }
  35. void MyMouseMoveCallback2(float x, float y)
  36. {
  37. printf("Mouse Move: %f, %f\n", x, y);
  38. if (sOldMouseMoveCB)
  39. sOldMouseMoveCB(x, y);
  40. }
  41. void MyMouseButtonCallback2(int button, int state, float x, float y)
  42. {
  43. if (sOldMouseButtonCB)
  44. sOldMouseButtonCB(button, state, x, y);
  45. }
  46. static void MyKeyboardCallback2(int keycode, int state)
  47. {
  48. //keycodes are in examples/CommonInterfaces/CommonWindowInterface.h
  49. //for example B3G_ESCAPE for escape key
  50. //state == 1 for pressed, state == 0 for released.
  51. // use app->m_window->isModifiedPressed(...) to check for shift, escape and alt keys
  52. printf("MyKeyboardCallback received key:%c in state %d\n", keycode, state);
  53. if (sOldKeyboardCB)
  54. sOldKeyboardCB(keycode, state);
  55. }
  56. int main(int argc, char* argv[])
  57. {
  58. {
  59. b3CommandLineArgs myArgs(argc, argv);
  60. SimpleOpenGLApp* app = new SimpleOpenGLApp("SimpleOpenGL3App", 1024, 768);
  61. app->m_renderer->getActiveCamera()->setCameraDistance(13);
  62. app->m_renderer->getActiveCamera()->setCameraPitch(0);
  63. app->m_renderer->getActiveCamera()->setCameraTargetPosition(0, 0, 0);
  64. sOldKeyboardCB = app->m_window->getKeyboardCallback();
  65. app->m_window->setKeyboardCallback(MyKeyboardCallback2);
  66. sOldMouseMoveCB = app->m_window->getMouseMoveCallback();
  67. app->m_window->setMouseMoveCallback(MyMouseMoveCallback2);
  68. sOldMouseButtonCB = app->m_window->getMouseButtonCallback();
  69. app->m_window->setMouseButtonCallback(MyMouseButtonCallback2);
  70. sOldWheelCB = app->m_window->getWheelCallback();
  71. app->m_window->setWheelCallback(MyWheelCallback2);
  72. sOldResizeCB = app->m_window->getResizeCallback();
  73. app->m_window->setResizeCallback(MyResizeCallback2);
  74. myArgs.GetCmdLineArgument("mp4_file", gVideoFileName);
  75. if (gVideoFileName)
  76. app->dumpFramesToVideo(gVideoFileName);
  77. myArgs.GetCmdLineArgument("png_file", gPngFileName);
  78. char fileName[1024];
  79. int textureWidth = 128;
  80. int textureHeight = 128;
  81. unsigned char* image = new unsigned char[textureWidth * textureHeight * 4];
  82. int textureHandle = app->m_renderer->registerTexture(image, textureWidth, textureHeight);
  83. do
  84. {
  85. static int frameCount = 0;
  86. frameCount++;
  87. if (gPngFileName)
  88. {
  89. printf("gPngFileName=%s\n", gPngFileName);
  90. sprintf(fileName, "%s%d.png", gPngFileName, frameCount++);
  91. app->dumpNextFrameToPng(fileName);
  92. }
  93. //update the texels of the texture using a simple pattern, animated using frame index
  94. for (int y = 0; y < textureHeight; ++y)
  95. {
  96. const int t = (y + frameCount) >> 4;
  97. unsigned char* pi = image + y * textureWidth * 3;
  98. for (int x = 0; x < textureWidth; ++x)
  99. {
  100. const int s = x >> 4;
  101. const unsigned char b = 180;
  102. unsigned char c = b + ((s + (t & 1)) & 1) * (255 - b);
  103. pi[0] = pi[1] = pi[2] = pi[3] = c;
  104. pi += 3;
  105. }
  106. }
  107. app->m_renderer->activateTexture(textureHandle);
  108. app->m_renderer->updateTexture(textureHandle, image);
  109. float color[4] = {1, 0, 0, 1};
  110. app->m_primRenderer->drawTexturedRect(100, 200, gWidth / 2 - 50, gHeight / 2 - 50, color, 0, 0, 1, 1, true);
  111. app->m_renderer->init();
  112. int upAxis = 1;
  113. app->m_renderer->updateCamera(upAxis);
  114. app->m_renderer->renderScene();
  115. app->drawGrid();
  116. char bla[1024];
  117. sprintf(bla, "2d text:%d", frameCount);
  118. float yellow[4] = {1, 1, 0, 1};
  119. app->drawText(bla, 10, 10, 1, yellow);
  120. float position[3] = {1, 1, 1};
  121. float position2[3] = {0, 0, 5};
  122. float orientation[4] = {0, 0, 0, 1};
  123. app->drawText3D(bla, 0, 0, 1, 1);
  124. sprintf(bla, "3d bitmap camera facing text:%d", frameCount);
  125. app->drawText3D(bla, position2, orientation, color, 1, CommonGraphicsApp::eDrawText3D_OrtogonalFaceCamera);
  126. sprintf(bla, "3d bitmap text:%d", frameCount);
  127. app->drawText3D(bla, position, orientation, color, 0.001, 0);
  128. float green[4] = {0, 1, 0, 1};
  129. float blue[4] = {0, 0, 1, 1};
  130. sprintf(bla, "3d ttf camera facing text:%d", frameCount);
  131. app->drawText3D(bla, position2, orientation, green, 1, CommonGraphicsApp::eDrawText3D_TrueType | CommonGraphicsApp::eDrawText3D_OrtogonalFaceCamera);
  132. app->drawText3D(bla, position2, orientation, green, 1, CommonGraphicsApp::eDrawText3D_TrueType | CommonGraphicsApp::eDrawText3D_OrtogonalFaceCamera);
  133. sprintf(bla, "3d ttf text:%d", frameCount);
  134. b3Quaternion orn;
  135. orn.setEulerZYX(B3_HALF_PI / 2., 0, B3_HALF_PI / 2.);
  136. app->drawText3D(bla, position2, orn, blue, 1, CommonGraphicsApp::eDrawText3D_TrueType);
  137. app->swapBuffer();
  138. } while (!app->m_window->requestedExit());
  139. delete app;
  140. delete[] image;
  141. }
  142. return 0;
  143. }