main.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "OpenGLWindow/SimpleOpenGL3App.h"
  2. #include "Bullet3Common/b3Quaternion.h"
  3. #include "Bullet3Common/b3CommandLineArgs.h"
  4. #include "assert.h"
  5. #include <stdio.h>
  6. char* gVideoFileName = 0;
  7. char* gPngFileName = 0;
  8. static b3WheelCallback sOldWheelCB = 0;
  9. static b3ResizeCallback sOldResizeCB = 0;
  10. static b3MouseMoveCallback sOldMouseMoveCB = 0;
  11. static b3MouseButtonCallback sOldMouseButtonCB = 0;
  12. static b3KeyboardCallback sOldKeyboardCB = 0;
  13. //static b3RenderCallback sOldRenderCB = 0;
  14. float gWidth = 1024;
  15. float gHeight = 768;
  16. void MyWheelCallback(float deltax, float deltay)
  17. {
  18. if (sOldWheelCB)
  19. sOldWheelCB(deltax,deltay);
  20. }
  21. void MyResizeCallback( float width, float height)
  22. {
  23. gWidth = width;
  24. gHeight = height;
  25. if (sOldResizeCB)
  26. sOldResizeCB(width,height);
  27. }
  28. void MyMouseMoveCallback( float x, float y)
  29. {
  30. printf("Mouse Move: %f, %f\n", x,y);
  31. if (sOldMouseMoveCB)
  32. sOldMouseMoveCB(x,y);
  33. }
  34. void MyMouseButtonCallback(int button, int state, float x, float y)
  35. {
  36. if (sOldMouseButtonCB)
  37. sOldMouseButtonCB(button,state,x,y);
  38. }
  39. void MyKeyboardCallback(int keycode, int state)
  40. {
  41. //keycodes are in examples/CommonInterfaces/CommonWindowInterface.h
  42. //for example B3G_ESCAPE for escape key
  43. //state == 1 for pressed, state == 0 for released.
  44. // use app->m_window->isModifiedPressed(...) to check for shift, escape and alt keys
  45. printf("MyKeyboardCallback received key:%c in state %d\n",keycode,state);
  46. if (sOldKeyboardCB)
  47. sOldKeyboardCB(keycode,state);
  48. }
  49. int main(int argc, char* argv[])
  50. {
  51. {
  52. b3CommandLineArgs myArgs(argc, argv);
  53. SimpleOpenGL3App* app = new SimpleOpenGL3App("SimpleOpenGL3App", gWidth, gHeight, true);
  54. app->m_instancingRenderer->getActiveCamera()->setCameraDistance(13);
  55. app->m_instancingRenderer->getActiveCamera()->setCameraPitch(0);
  56. app->m_instancingRenderer->getActiveCamera()->setCameraTargetPosition(0, 0, 0);
  57. sOldKeyboardCB = app->m_window->getKeyboardCallback();
  58. app->m_window->setKeyboardCallback(MyKeyboardCallback);
  59. sOldMouseMoveCB = app->m_window->getMouseMoveCallback();
  60. app->m_window->setMouseMoveCallback(MyMouseMoveCallback);
  61. sOldMouseButtonCB = app->m_window->getMouseButtonCallback();
  62. app->m_window->setMouseButtonCallback(MyMouseButtonCallback);
  63. sOldWheelCB = app->m_window->getWheelCallback();
  64. app->m_window->setWheelCallback(MyWheelCallback);
  65. sOldResizeCB = app->m_window->getResizeCallback();
  66. app->m_window->setResizeCallback(MyResizeCallback);
  67. myArgs.GetCmdLineArgument("mp4_file", gVideoFileName);
  68. if (gVideoFileName)
  69. app->dumpFramesToVideo(gVideoFileName);
  70. myArgs.GetCmdLineArgument("png_file", gPngFileName);
  71. char fileName[1024];
  72. int textureWidth = 128;
  73. int textureHeight = 128;
  74. unsigned char* image = new unsigned char[textureWidth*textureHeight * 4];
  75. int textureHandle = app->m_renderer->registerTexture(image, textureWidth, textureHeight);
  76. int cubeIndex = app->registerCubeShape(1, 1, 1);
  77. b3Vector3 pos = b3MakeVector3(0, 0, 0);
  78. b3Quaternion orn(0, 0, 0, 1);
  79. b3Vector3 color = b3MakeVector3(1, 0, 0);
  80. b3Vector3 scaling = b3MakeVector3 (1, 1, 1);
  81. app->m_renderer->registerGraphicsInstance(cubeIndex, pos, orn, color, scaling);
  82. app->m_renderer->writeTransforms();
  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; pi += 3;
  104. }
  105. }
  106. app->m_renderer->activateTexture(textureHandle);
  107. app->m_renderer->updateTexture(textureHandle, image);
  108. float color[4] = { 255, 1, 1, 1 };
  109. app->m_primRenderer->drawTexturedRect(100, 200, gWidth / 2 - 50, gHeight / 2 - 50, color, 0, 0, 1, 1, true);
  110. app->m_instancingRenderer->init();
  111. app->m_instancingRenderer->updateCamera();
  112. app->m_renderer->renderScene();
  113. app->drawGrid();
  114. char bla[1024];
  115. sprintf(bla, "Simple test frame %d", frameCount);
  116. app->drawText(bla, 10, 10);
  117. app->swapBuffer();
  118. } while (!app->m_window->requestedExit());
  119. delete app;
  120. delete[] image;
  121. }
  122. return 0;
  123. }