TinyRendererSetup.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #include "RaytracerSetup.h"
  2. #include "../CommonInterfaces/CommonGraphicsAppInterface.h"
  3. #include "Bullet3Common/b3Quaternion.h"
  4. #include "Bullet3Common/b3AlignedObjectArray.h"
  5. #include "../CommonInterfaces/CommonRenderInterface.h"
  6. #include "../TinyRenderer/TinyRenderer.h"
  7. #include "../CommonInterfaces/Common2dCanvasInterface.h"
  8. #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
  9. #include "../CommonInterfaces/CommonExampleInterface.h"
  10. #include "LinearMath/btAlignedObjectArray.h"
  11. #include "btBulletCollisionCommon.h"
  12. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  13. #include "../ExampleBrowser/CollisionShape2TriangleMesh.h"
  14. #include "../Importers/ImportMeshUtility/b3ImportMeshUtility.h"
  15. #include "../OpenGLWindow/GLInstanceGraphicsShape.h"
  16. #include "../CommonInterfaces/CommonParameterInterface.h"
  17. struct TinyRendererSetupInternalData
  18. {
  19. TGAImage m_rgbColorBuffer;
  20. b3AlignedObjectArray<float> m_depthBuffer;
  21. b3AlignedObjectArray<int> m_segmentationMaskBuffer;
  22. int m_width;
  23. int m_height;
  24. btAlignedObjectArray<btConvexShape*> m_shapePtr;
  25. btAlignedObjectArray<btTransform> m_transforms;
  26. btAlignedObjectArray<TinyRenderObjectData*> m_renderObjects;
  27. btVoronoiSimplexSolver m_simplexSolver;
  28. btScalar m_pitch;
  29. btScalar m_roll;
  30. btScalar m_yaw;
  31. int m_textureHandle;
  32. int m_animateRenderer;
  33. TinyRendererSetupInternalData(int width, int height)
  34. :m_roll(0),
  35. m_pitch(0),
  36. m_yaw(0),
  37. m_width(width),
  38. m_height(height),
  39. m_rgbColorBuffer(width,height,TGAImage::RGB),
  40. m_textureHandle(0),
  41. m_animateRenderer(0)
  42. {
  43. m_depthBuffer.resize(m_width*m_height);
  44. // m_segmentationMaskBuffer.resize(m_width*m_height);
  45. }
  46. void updateTransforms()
  47. {
  48. int numObjects = m_shapePtr.size();
  49. m_transforms.resize(numObjects);
  50. for (int i=0;i<numObjects;i++)
  51. {
  52. m_transforms[i].setIdentity();
  53. //btVector3 pos(0.f,-(2.5* numObjects * 0.5)+i*2.5f, 0.f);
  54. btVector3 pos(0.f,+i*2.5f, 0.f);
  55. m_transforms[i].setIdentity();
  56. m_transforms[i].setOrigin( pos );
  57. btQuaternion orn;
  58. if (i < 2)
  59. {
  60. orn.setEuler(m_yaw,m_pitch,m_roll);
  61. m_transforms[i].setRotation(orn);
  62. }
  63. }
  64. if (m_animateRenderer)
  65. {
  66. m_pitch += 0.005f;
  67. m_yaw += 0.01f;
  68. }
  69. }
  70. };
  71. struct TinyRendererSetup : public CommonExampleInterface
  72. {
  73. struct GUIHelperInterface* m_guiHelper;
  74. struct CommonGraphicsApp* m_app;
  75. struct TinyRendererSetupInternalData* m_internalData;
  76. bool m_useSoftware;
  77. TinyRendererSetup(struct GUIHelperInterface* guiHelper);
  78. virtual ~TinyRendererSetup();
  79. virtual void initPhysics();
  80. virtual void exitPhysics();
  81. virtual void stepSimulation(float deltaTime);
  82. virtual void physicsDebugDraw(int debugFlags);
  83. virtual void syncPhysicsToGraphics(struct GraphicsPhysicsBridge& gfxBridge);
  84. virtual bool mouseMoveCallback(float x,float y);
  85. virtual bool mouseButtonCallback(int button, int state, float x, float y);
  86. virtual bool keyboardCallback(int key, int state);
  87. virtual void renderScene()
  88. {
  89. }
  90. void animateRenderer(int animateRendererIndex)
  91. {
  92. m_internalData->m_animateRenderer = animateRendererIndex;
  93. }
  94. void selectRenderer(int rendererIndex)
  95. {
  96. m_useSoftware = (rendererIndex==0);
  97. }
  98. void resetCamera()
  99. {
  100. float dist = 11;
  101. float pitch = 52;
  102. float yaw = 35;
  103. float targetPos[3]={0,0.46,0};
  104. m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
  105. }
  106. };
  107. TinyRendererSetup::TinyRendererSetup(struct GUIHelperInterface* gui)
  108. {
  109. m_useSoftware = false;
  110. m_guiHelper = gui;
  111. m_app = gui->getAppInterface();
  112. m_internalData = new TinyRendererSetupInternalData(gui->getAppInterface()->m_window->getWidth(),gui->getAppInterface()->m_window->getHeight());
  113. m_app->m_renderer->enableBlend(true);
  114. const char* fileName = "textured_sphere_smooth.obj";
  115. fileName = "cube.obj";
  116. {
  117. {
  118. int shapeId = -1;
  119. b3ImportMeshData meshData;
  120. if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(fileName, meshData))
  121. {
  122. int textureIndex = -1;
  123. if (meshData.m_textureImage)
  124. {
  125. textureIndex = m_guiHelper->getRenderInterface()->registerTexture(meshData.m_textureImage,meshData.m_textureWidth,meshData.m_textureHeight);
  126. }
  127. shapeId = m_guiHelper->getRenderInterface()->registerShape(&meshData.m_gfxShape->m_vertices->at(0).xyzw[0],
  128. meshData.m_gfxShape->m_numvertices,
  129. &meshData.m_gfxShape->m_indices->at(0),
  130. meshData.m_gfxShape->m_numIndices,
  131. B3_GL_TRIANGLES,
  132. textureIndex);
  133. float position[4]={0,0,0,1};
  134. float orn[4]={0,0,0,1};
  135. float color[4]={1,1,1,1};
  136. float scaling[4]={1,1,1,1};
  137. m_guiHelper->getRenderInterface()->registerGraphicsInstance(shapeId,position,orn,color,scaling);
  138. m_guiHelper->getRenderInterface()->writeTransforms();
  139. m_internalData->m_shapePtr.push_back(0);
  140. TinyRenderObjectData* ob = new TinyRenderObjectData(
  141. m_internalData->m_rgbColorBuffer,
  142. m_internalData->m_depthBuffer,
  143. &m_internalData->m_segmentationMaskBuffer,
  144. m_internalData->m_renderObjects.size());
  145. meshData.m_gfxShape->m_scaling[0] = scaling[0];
  146. meshData.m_gfxShape->m_scaling[1] = scaling[1];
  147. meshData.m_gfxShape->m_scaling[2] = scaling[2];
  148. const int* indices = &meshData.m_gfxShape->m_indices->at(0);
  149. ob->registerMeshShape(&meshData.m_gfxShape->m_vertices->at(0).xyzw[0],
  150. meshData.m_gfxShape->m_numvertices,
  151. indices,
  152. meshData.m_gfxShape->m_numIndices,color, meshData.m_textureImage,meshData.m_textureWidth,meshData.m_textureHeight);
  153. ob->m_localScaling.setValue(scaling[0],scaling[1],scaling[2]);
  154. m_internalData->m_renderObjects.push_back(ob);
  155. delete meshData.m_gfxShape;
  156. delete meshData.m_textureImage;
  157. }
  158. }
  159. }
  160. }
  161. TinyRendererSetup::~TinyRendererSetup()
  162. {
  163. m_app->m_renderer->enableBlend(false);
  164. delete m_internalData;
  165. }
  166. const char* itemsanimate[] = {"Fixed", "Rotate"};
  167. void TinyRendererComboCallbackAnimate(int combobox, const char* item, void* userPointer)
  168. {
  169. TinyRendererSetup* cl = (TinyRendererSetup*) userPointer;
  170. b3Assert(cl);
  171. int index=-1;
  172. int numItems = sizeof(itemsanimate)/sizeof(char*);
  173. for (int i=0;i<numItems;i++)
  174. {
  175. if (!strcmp(item,itemsanimate[i]))
  176. {
  177. index = i;
  178. }
  179. }
  180. cl->animateRenderer(index);
  181. }
  182. const char* items[] = {"Software", "OpenGL"};
  183. void TinyRendererComboCallback(int combobox, const char* item, void* userPointer)
  184. {
  185. TinyRendererSetup* cl = (TinyRendererSetup*) userPointer;
  186. b3Assert(cl);
  187. int index=-1;
  188. int numItems = sizeof(items)/sizeof(char*);
  189. for (int i=0;i<numItems;i++)
  190. {
  191. if (!strcmp(item,items[i]))
  192. {
  193. index = i;
  194. }
  195. }
  196. cl->selectRenderer(index);
  197. }
  198. void TinyRendererSetup::initPhysics()
  199. {
  200. //request a visual bitma/texture we can render to
  201. m_app->setUpAxis(2);
  202. CommonRenderInterface* render = m_app->m_renderer;
  203. m_internalData->m_textureHandle = render->registerTexture(m_internalData->m_rgbColorBuffer.buffer(),m_internalData->m_width,m_internalData->m_height);
  204. {
  205. ComboBoxParams comboParams;
  206. comboParams.m_userPointer = this;
  207. comboParams.m_numItems=sizeof(items)/sizeof(char*);
  208. comboParams.m_startItem = 1;
  209. comboParams.m_items=items;
  210. comboParams.m_callback =TinyRendererComboCallback;
  211. m_guiHelper->getParameterInterface()->registerComboBox( comboParams);
  212. }
  213. {
  214. ComboBoxParams comboParams;
  215. comboParams.m_userPointer = this;
  216. comboParams.m_numItems=sizeof(itemsanimate)/sizeof(char*);
  217. comboParams.m_startItem = 0;
  218. comboParams.m_items=itemsanimate;
  219. comboParams.m_callback =TinyRendererComboCallbackAnimate;
  220. m_guiHelper->getParameterInterface()->registerComboBox( comboParams);
  221. }
  222. }
  223. void TinyRendererSetup::exitPhysics()
  224. {
  225. }
  226. void TinyRendererSetup::stepSimulation(float deltaTime)
  227. {
  228. m_internalData->updateTransforms();
  229. if (!m_useSoftware)
  230. {
  231. for (int i=0;i<m_internalData->m_transforms.size();i++)
  232. {
  233. m_guiHelper->getRenderInterface()->writeSingleInstanceTransformToCPU(m_internalData->m_transforms[i].getOrigin(),m_internalData->m_transforms[i].getRotation(),i);
  234. }
  235. m_guiHelper->getRenderInterface()->writeTransforms();
  236. m_guiHelper->getRenderInterface()->renderScene();
  237. } else
  238. {
  239. TGAColor clearColor;
  240. clearColor.bgra[0] = 200;
  241. clearColor.bgra[1] = 200;
  242. clearColor.bgra[2] = 200;
  243. clearColor.bgra[3] = 255;
  244. for(int y=0;y<m_internalData->m_height;++y)
  245. {
  246. for(int x=0;x<m_internalData->m_width;++x)
  247. {
  248. m_internalData->m_rgbColorBuffer.set(x,y,clearColor);
  249. m_internalData->m_depthBuffer[x+y*m_internalData->m_width] = -1e30f;
  250. }
  251. }
  252. ATTRIBUTE_ALIGNED16(btScalar modelMat2[16]);
  253. ATTRIBUTE_ALIGNED16(float viewMat[16]);
  254. ATTRIBUTE_ALIGNED16(float projMat[16]);
  255. CommonRenderInterface* render = this->m_app->m_renderer;
  256. render->getActiveCamera()->getCameraViewMatrix(viewMat);
  257. render->getActiveCamera()->getCameraProjectionMatrix(projMat);
  258. for (int o=0;o<this->m_internalData->m_renderObjects.size();o++)
  259. {
  260. const btTransform& tr = m_internalData->m_transforms[o];
  261. tr.getOpenGLMatrix(modelMat2);
  262. for (int i=0;i<4;i++)
  263. {
  264. for (int j=0;j<4;j++)
  265. {
  266. m_internalData->m_renderObjects[o]->m_modelMatrix[i][j] = float(modelMat2[i+4*j]);
  267. m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i+4*j];
  268. m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i+4*j];
  269. btVector3 lightDirWorld;
  270. switch (m_app->getUpAxis())
  271. {
  272. case 1:
  273. lightDirWorld = btVector3(-50.f,100,30);
  274. break;
  275. case 2:
  276. lightDirWorld = btVector3(-50.f,30,100);
  277. break;
  278. default:{}
  279. };
  280. m_internalData->m_renderObjects[o]->m_lightDirWorld = lightDirWorld.normalized();
  281. }
  282. }
  283. TinyRenderer::renderObject(*m_internalData->m_renderObjects[o]);
  284. }
  285. //m_app->drawText("hello",500,500);
  286. render->activateTexture(m_internalData->m_textureHandle);
  287. render->updateTexture(m_internalData->m_textureHandle,m_internalData->m_rgbColorBuffer.buffer());
  288. float color[4] = {1,1,1,1};
  289. m_app->drawTexturedRect(0,0,m_app->m_window->getWidth(), m_app->m_window->getHeight(),color,0,0,1,1,true);
  290. }
  291. }
  292. void TinyRendererSetup::physicsDebugDraw(int debugDrawFlags)
  293. {
  294. }
  295. bool TinyRendererSetup::mouseMoveCallback(float x,float y)
  296. {
  297. return false;
  298. }
  299. bool TinyRendererSetup::mouseButtonCallback(int button, int state, float x, float y)
  300. {
  301. return false;
  302. }
  303. bool TinyRendererSetup::keyboardCallback(int key, int state)
  304. {
  305. return false;
  306. }
  307. void TinyRendererSetup::syncPhysicsToGraphics(GraphicsPhysicsBridge& gfxBridge)
  308. {
  309. }
  310. CommonExampleInterface* TinyRendererCreateFunc(struct CommonExampleOptions& options)
  311. {
  312. return new TinyRendererSetup(options.m_guiHelper);
  313. }