main.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "OpenGLWindow/SimpleOpenGL3App.h"
  2. #include "Bullet3Common/b3Quaternion.h"
  3. #include "Bullet3Common/b3CommandLineArgs.h"
  4. #include "Bullet3Common/b3Transform.h"
  5. #include "assert.h"
  6. #include <stdio.h>
  7. char* gVideoFileName = 0;
  8. char* gPngFileName = 0;
  9. static b3WheelCallback sOldWheelCB = 0;
  10. static b3ResizeCallback sOldResizeCB = 0;
  11. static b3MouseMoveCallback sOldMouseMoveCB = 0;
  12. static b3MouseButtonCallback sOldMouseButtonCB = 0;
  13. static b3KeyboardCallback sOldKeyboardCB = 0;
  14. //static b3RenderCallback sOldRenderCB = 0;
  15. float gWidth = 0 ;
  16. float gHeight = 0;
  17. void MyWheelCallback(float deltax, float deltay)
  18. {
  19. if (sOldWheelCB)
  20. sOldWheelCB(deltax,deltay);
  21. }
  22. void MyResizeCallback( float width, float height)
  23. {
  24. gWidth = width;
  25. gHeight = height;
  26. if (sOldResizeCB)
  27. sOldResizeCB(width,height);
  28. }
  29. void MyMouseMoveCallback( float x, float y)
  30. {
  31. printf("Mouse Move: %f, %f\n", x,y);
  32. if (sOldMouseMoveCB)
  33. sOldMouseMoveCB(x,y);
  34. }
  35. void MyMouseButtonCallback(int button, int state, float x, float y)
  36. {
  37. if (sOldMouseButtonCB)
  38. sOldMouseButtonCB(button,state,x,y);
  39. }
  40. void MyKeyboardCallback(int keycode, int state)
  41. {
  42. //keycodes are in examples/CommonInterfaces/CommonWindowInterface.h
  43. //for example B3G_ESCAPE for escape key
  44. //state == 1 for pressed, state == 0 for released.
  45. // use app->m_window->isModifiedPressed(...) to check for shift, escape and alt keys
  46. printf("MyKeyboardCallback received key:%c in state %d\n",keycode,state);
  47. if (sOldKeyboardCB)
  48. sOldKeyboardCB(keycode,state);
  49. }
  50. #include "TinyRenderer.h"
  51. int main(int argc, char* argv[])
  52. {
  53. b3CommandLineArgs myArgs(argc,argv);
  54. SimpleOpenGL3App* app = new SimpleOpenGL3App("SimpleOpenGL3App",640,480,true);
  55. app->m_instancingRenderer->getActiveCamera()->setCameraDistance(13);
  56. app->m_instancingRenderer->getActiveCamera()->setCameraPitch(0);
  57. app->m_instancingRenderer->getActiveCamera()->setCameraTargetPosition(0,0,0);
  58. sOldKeyboardCB = app->m_window->getKeyboardCallback();
  59. app->m_window->setKeyboardCallback(MyKeyboardCallback);
  60. sOldMouseMoveCB = app->m_window->getMouseMoveCallback();
  61. app->m_window->setMouseMoveCallback(MyMouseMoveCallback);
  62. sOldMouseButtonCB = app->m_window->getMouseButtonCallback();
  63. app->m_window->setMouseButtonCallback(MyMouseButtonCallback);
  64. sOldWheelCB = app->m_window->getWheelCallback();
  65. app->m_window->setWheelCallback(MyWheelCallback);
  66. sOldResizeCB = app->m_window->getResizeCallback();
  67. app->m_window->setResizeCallback(MyResizeCallback);
  68. int textureWidth = gWidth;
  69. int textureHeight = gHeight;
  70. TGAImage rgbColorBuffer(gWidth,gHeight,TGAImage::RGB);
  71. b3AlignedObjectArray<float> depthBuffer;
  72. depthBuffer.resize(gWidth*gHeight);
  73. TinyRenderObjectData renderData(rgbColorBuffer,depthBuffer);//, "african_head/african_head.obj");//floor.obj");
  74. //renderData.loadModel("african_head/african_head.obj");
  75. renderData.loadModel("floor.obj");
  76. //renderData.createCube(1,1,1);
  77. myArgs.GetCmdLineArgument("mp4_file",gVideoFileName);
  78. if (gVideoFileName)
  79. app->dumpFramesToVideo(gVideoFileName);
  80. myArgs.GetCmdLineArgument("png_file",gPngFileName);
  81. char fileName[1024];
  82. unsigned char* image=new unsigned char[textureWidth*textureHeight*4];
  83. int textureHandle = app->m_renderer->registerTexture(image,textureWidth,textureHeight);
  84. int cubeIndex = app->registerCubeShape(1,1,1);
  85. b3Vector3 pos = b3MakeVector3(0,0,0);
  86. b3Quaternion orn(0,0,0,1);
  87. b3Vector3 color=b3MakeVector3(1,0,0);
  88. b3Vector3 scaling=b3MakeVector3 (1,1,1);
  89. app->m_renderer->registerGraphicsInstance(cubeIndex,pos,orn,color,scaling);
  90. app->m_renderer->writeTransforms();
  91. do
  92. {
  93. static int frameCount = 0;
  94. frameCount++;
  95. if (gPngFileName)
  96. {
  97. printf("gPngFileName=%s\n",gPngFileName);
  98. sprintf(fileName,"%s%d.png",gPngFileName,frameCount++);
  99. app->dumpNextFrameToPng(fileName);
  100. }
  101. app->m_instancingRenderer->init();
  102. app->m_instancingRenderer->updateCamera();
  103. ///clear the color and z (depth) buffer
  104. for(int y=0;y<textureHeight;++y)
  105. {
  106. unsigned char* pi=image+(y)*textureWidth*3;
  107. for(int x=0;x<textureWidth;++x)
  108. {
  109. TGAColor color;
  110. color.bgra[0] = 255;
  111. color.bgra[1] = 255;
  112. color.bgra[2] = 255;
  113. color.bgra[3] = 255;
  114. renderData.m_rgbColorBuffer.set(x,y,color);
  115. renderData.m_depthBuffer[x+y*textureWidth] = -1e30f;
  116. }
  117. }
  118. float projMat[16];
  119. app->m_instancingRenderer->getActiveCamera()->getCameraProjectionMatrix(projMat);
  120. float viewMat[16];
  121. app->m_instancingRenderer->getActiveCamera()->getCameraViewMatrix(viewMat);
  122. B3_ATTRIBUTE_ALIGNED16(float modelMat[16]);
  123. //sync the object transform
  124. b3Transform tr;
  125. tr.setIdentity();
  126. static float posUp = 0.f;
  127. // posUp += 0.001;
  128. b3Vector3 org = b3MakeVector3(0,posUp,0);
  129. tr.setOrigin(org);
  130. tr.getOpenGLMatrix(modelMat);
  131. for (int i=0;i<4;i++)
  132. {
  133. for (int j=0;j<4;j++)
  134. {
  135. renderData.m_viewMatrix[i][j] = viewMat[i+4*j];
  136. renderData.m_modelMatrix[i][j] = modelMat[i+4*j];
  137. }
  138. }
  139. //render the object
  140. TinyRenderer::renderObject(renderData);
  141. #if 1
  142. //update the texels of the texture using a simple pattern, animated using frame index
  143. for(int y=0;y<textureHeight;++y)
  144. {
  145. unsigned char* pi=image+(y)*textureWidth*3;
  146. for(int x=0;x<textureWidth;++x)
  147. {
  148. TGAColor color = renderData.m_rgbColorBuffer.get(x,y);
  149. pi[0] = color.bgra[2];
  150. pi[1] = color.bgra[1];
  151. pi[2] = color.bgra[0];
  152. pi[3] = 255;
  153. pi+=3;
  154. }
  155. }
  156. #else
  157. //update the texels of the texture using a simple pattern, animated using frame index
  158. for(int y=0;y<textureHeight;++y)
  159. {
  160. const int t=(y+frameCount)>>4;
  161. unsigned char* pi=image+y*textureWidth*3;
  162. for(int x=0;x<textureWidth;++x)
  163. {
  164. TGAColor color = renderData.m_rgbColorBuffer.get(x,y);
  165. const int s=x>>4;
  166. const unsigned char b=180;
  167. unsigned char c=b+((s+(t&1))&1)*(255-b);
  168. pi[0]=pi[1]=pi[2]=pi[3]=c;
  169. pi+=3;
  170. }
  171. }
  172. #endif
  173. app->m_renderer->activateTexture(textureHandle);
  174. app->m_renderer->updateTexture(textureHandle,image);
  175. float color[4] = {1,1,1,1};
  176. app->m_primRenderer->drawTexturedRect(0,0,gWidth/3,gHeight/3,color,0,0,1,1,true);
  177. app->m_renderer->renderScene();
  178. app->drawGrid();
  179. char bla[1024];
  180. sprintf(bla,"Simple test frame %d", frameCount);
  181. app->drawText(bla,10,10);
  182. app->swapBuffer();
  183. } while (!app->m_window->requestedExit());
  184. delete app;
  185. return 0;
  186. }