TinyRendererSetup.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. #include "../Utils/b3BulletDefaultFileIO.h"
  18. struct TinyRendererSetupInternalData
  19. {
  20. TGAImage m_rgbColorBuffer;
  21. b3AlignedObjectArray<float> m_depthBuffer;
  22. b3AlignedObjectArray<float> m_shadowBuffer;
  23. b3AlignedObjectArray<int> m_segmentationMaskBuffer;
  24. int m_width;
  25. int m_height;
  26. btAlignedObjectArray<btConvexShape*> m_shapePtr;
  27. btAlignedObjectArray<btTransform> m_transforms;
  28. btAlignedObjectArray<TinyRenderObjectData*> m_renderObjects;
  29. btVoronoiSimplexSolver m_simplexSolver;
  30. btScalar m_pitch;
  31. btScalar m_roll;
  32. btScalar m_yaw;
  33. int m_textureHandle;
  34. int m_animateRenderer;
  35. btVector3 m_lightPos;
  36. TinyRendererSetupInternalData(int width, int height)
  37. : m_rgbColorBuffer(width, height, TGAImage::RGB),
  38. m_width(width),
  39. m_height(height),
  40. m_pitch(0),
  41. m_roll(0),
  42. m_yaw(0),
  43. m_textureHandle(0),
  44. m_animateRenderer(0)
  45. {
  46. m_lightPos.setValue(-3, 15, 15);
  47. m_depthBuffer.resize(m_width * m_height);
  48. m_shadowBuffer.resize(m_width * m_height);
  49. // m_segmentationMaskBuffer.resize(m_width*m_height);
  50. }
  51. void updateTransforms()
  52. {
  53. int numObjects = m_shapePtr.size();
  54. m_transforms.resize(numObjects);
  55. for (int i = 0; i < numObjects; i++)
  56. {
  57. m_transforms[i].setIdentity();
  58. //btVector3 pos(0.f,-(2.5* numObjects * 0.5)+i*2.5f, 0.f);
  59. btVector3 pos(0.f, +i * 2.5f, 0.f);
  60. m_transforms[i].setIdentity();
  61. m_transforms[i].setOrigin(pos);
  62. btQuaternion orn;
  63. if (i < 2)
  64. {
  65. orn.setEuler(m_yaw, m_pitch, m_roll);
  66. m_transforms[i].setRotation(orn);
  67. }
  68. }
  69. if (m_animateRenderer)
  70. {
  71. m_pitch += 0.005f;
  72. m_yaw += 0.01f;
  73. }
  74. }
  75. };
  76. struct TinyRendererSetup : public CommonExampleInterface
  77. {
  78. struct GUIHelperInterface* m_guiHelper;
  79. struct CommonGraphicsApp* m_app;
  80. struct TinyRendererSetupInternalData* m_internalData;
  81. bool m_useSoftware;
  82. TinyRendererSetup(struct GUIHelperInterface* guiHelper);
  83. virtual ~TinyRendererSetup();
  84. virtual void initPhysics();
  85. virtual void exitPhysics();
  86. virtual void stepSimulation(float deltaTime);
  87. virtual void physicsDebugDraw(int debugFlags);
  88. virtual void syncPhysicsToGraphics(struct GraphicsPhysicsBridge& gfxBridge);
  89. virtual bool mouseMoveCallback(float x, float y);
  90. virtual bool mouseButtonCallback(int button, int state, float x, float y);
  91. virtual bool keyboardCallback(int key, int state);
  92. virtual void renderScene();
  93. void animateRenderer(int animateRendererIndex)
  94. {
  95. m_internalData->m_animateRenderer = animateRendererIndex;
  96. }
  97. void selectRenderer(int rendererIndex)
  98. {
  99. m_useSoftware = (rendererIndex == 0);
  100. }
  101. void resetCamera()
  102. {
  103. float dist = 11;
  104. float pitch = -35;
  105. float yaw = 52;
  106. float targetPos[3] = {0, 0.46, 0};
  107. m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
  108. }
  109. };
  110. TinyRendererSetup::TinyRendererSetup(struct GUIHelperInterface* gui)
  111. {
  112. m_useSoftware = false;
  113. m_guiHelper = gui;
  114. m_app = gui->getAppInterface();
  115. m_internalData = new TinyRendererSetupInternalData(gui->getAppInterface()->m_window->getWidth(), gui->getAppInterface()->m_window->getHeight());
  116. const char* fileName = "textured_sphere_smooth.obj";
  117. fileName = "cube.obj";
  118. fileName = "torus/torus_with_plane.obj";
  119. {
  120. {
  121. int shapeId = -1;
  122. b3ImportMeshData meshData;
  123. b3BulletDefaultFileIO fileIO;
  124. if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(fileName, meshData,&fileIO))
  125. {
  126. int textureIndex = -1;
  127. if (meshData.m_textureImage1)
  128. {
  129. textureIndex = m_guiHelper->getRenderInterface()->registerTexture(meshData.m_textureImage1, meshData.m_textureWidth, meshData.m_textureHeight);
  130. }
  131. shapeId = m_guiHelper->getRenderInterface()->registerShape(&meshData.m_gfxShape->m_vertices->at(0).xyzw[0],
  132. meshData.m_gfxShape->m_numvertices,
  133. &meshData.m_gfxShape->m_indices->at(0),
  134. meshData.m_gfxShape->m_numIndices,
  135. B3_GL_TRIANGLES,
  136. textureIndex);
  137. float position[4] = {0, 0, 0, 1};
  138. float orn[4] = {0, 0, 0, 1};
  139. float color[4] = {1, 1, 1, 1};
  140. float scaling[4] = {1, 1, 1, 1};
  141. m_guiHelper->getRenderInterface()->registerGraphicsInstance(shapeId, position, orn, color, scaling);
  142. m_guiHelper->getRenderInterface()->writeTransforms();
  143. m_internalData->m_shapePtr.push_back(0);
  144. TinyRenderObjectData* ob = new TinyRenderObjectData(
  145. m_internalData->m_rgbColorBuffer,
  146. m_internalData->m_depthBuffer,
  147. &m_internalData->m_shadowBuffer,
  148. &m_internalData->m_segmentationMaskBuffer,
  149. m_internalData->m_renderObjects.size(), -1);
  150. meshData.m_gfxShape->m_scaling[0] = scaling[0];
  151. meshData.m_gfxShape->m_scaling[1] = scaling[1];
  152. meshData.m_gfxShape->m_scaling[2] = scaling[2];
  153. const int* indices = &meshData.m_gfxShape->m_indices->at(0);
  154. ob->registerMeshShape(&meshData.m_gfxShape->m_vertices->at(0).xyzw[0],
  155. meshData.m_gfxShape->m_numvertices,
  156. indices,
  157. meshData.m_gfxShape->m_numIndices, color, meshData.m_textureImage1, meshData.m_textureWidth, meshData.m_textureHeight);
  158. ob->m_localScaling.setValue(scaling[0], scaling[1], scaling[2]);
  159. m_internalData->m_renderObjects.push_back(ob);
  160. delete meshData.m_gfxShape;
  161. if (!meshData.m_isCached)
  162. {
  163. delete meshData.m_textureImage1;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. TinyRendererSetup::~TinyRendererSetup()
  170. {
  171. delete m_internalData;
  172. }
  173. const char* itemsanimate[] = {"Fixed", "Rotate"};
  174. void TinyRendererComboCallbackAnimate(int combobox, const char* item, void* userPointer)
  175. {
  176. TinyRendererSetup* cl = (TinyRendererSetup*)userPointer;
  177. b3Assert(cl);
  178. int index = -1;
  179. int numItems = sizeof(itemsanimate) / sizeof(char*);
  180. for (int i = 0; i < numItems; i++)
  181. {
  182. if (!strcmp(item, itemsanimate[i]))
  183. {
  184. index = i;
  185. }
  186. }
  187. cl->animateRenderer(index);
  188. }
  189. const char* items[] = {"Software", "OpenGL"};
  190. void TinyRendererComboCallback(int combobox, const char* item, void* userPointer)
  191. {
  192. TinyRendererSetup* cl = (TinyRendererSetup*)userPointer;
  193. b3Assert(cl);
  194. int index = -1;
  195. int numItems = sizeof(items) / sizeof(char*);
  196. for (int i = 0; i < numItems; i++)
  197. {
  198. if (!strcmp(item, items[i]))
  199. {
  200. index = i;
  201. }
  202. }
  203. cl->selectRenderer(index);
  204. }
  205. void TinyRendererSetup::initPhysics()
  206. {
  207. //request a visual bitma/texture we can render to
  208. m_app->setUpAxis(2);
  209. CommonRenderInterface* render = m_app->m_renderer;
  210. m_internalData->m_textureHandle = render->registerTexture(m_internalData->m_rgbColorBuffer.buffer(), m_internalData->m_width, m_internalData->m_height);
  211. {
  212. ComboBoxParams comboParams;
  213. comboParams.m_userPointer = this;
  214. comboParams.m_numItems = sizeof(items) / sizeof(char*);
  215. comboParams.m_startItem = 1;
  216. comboParams.m_items = items;
  217. comboParams.m_callback = TinyRendererComboCallback;
  218. m_guiHelper->getParameterInterface()->registerComboBox(comboParams);
  219. }
  220. {
  221. ComboBoxParams comboParams;
  222. comboParams.m_userPointer = this;
  223. comboParams.m_numItems = sizeof(itemsanimate) / sizeof(char*);
  224. comboParams.m_startItem = 0;
  225. comboParams.m_items = itemsanimate;
  226. comboParams.m_callback = TinyRendererComboCallbackAnimate;
  227. m_guiHelper->getParameterInterface()->registerComboBox(comboParams);
  228. }
  229. {
  230. SliderParams slider("LightPosX", &m_internalData->m_lightPos[0]);
  231. slider.m_minVal = -10;
  232. slider.m_maxVal = 10;
  233. if (m_guiHelper->getParameterInterface())
  234. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  235. }
  236. {
  237. SliderParams slider("LightPosY", &m_internalData->m_lightPos[1]);
  238. slider.m_minVal = -10;
  239. slider.m_maxVal = 10;
  240. if (m_guiHelper->getParameterInterface())
  241. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  242. }
  243. {
  244. SliderParams slider("LightPosZ", &m_internalData->m_lightPos[2]);
  245. slider.m_minVal = -10;
  246. slider.m_maxVal = 10;
  247. if (m_guiHelper->getParameterInterface())
  248. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  249. }
  250. }
  251. void TinyRendererSetup::exitPhysics()
  252. {
  253. }
  254. void TinyRendererSetup::stepSimulation(float deltaTime)
  255. {
  256. m_internalData->updateTransforms();
  257. }
  258. void TinyRendererSetup::renderScene()
  259. {
  260. m_internalData->updateTransforms();
  261. btVector4 from(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1], m_internalData->m_lightPos[2], 1);
  262. btVector4 toX(m_internalData->m_lightPos[0] + 0.1, m_internalData->m_lightPos[1], m_internalData->m_lightPos[2], 1);
  263. btVector4 toY(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1] + 0.1, m_internalData->m_lightPos[2], 1);
  264. btVector4 toZ(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1], m_internalData->m_lightPos[2] + 0.1, 1);
  265. btVector4 colorX(1, 0, 0, 1);
  266. btVector4 colorY(0, 1, 0, 1);
  267. btVector4 colorZ(0, 0, 1, 1);
  268. int width = 2;
  269. m_guiHelper->getRenderInterface()->drawLine(from, toX, colorX, width);
  270. m_guiHelper->getRenderInterface()->drawLine(from, toY, colorY, width);
  271. m_guiHelper->getRenderInterface()->drawLine(from, toZ, colorZ, width);
  272. if (!m_useSoftware)
  273. {
  274. btVector3 lightPos(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1], m_internalData->m_lightPos[2]);
  275. m_guiHelper->getRenderInterface()->setLightPosition(lightPos);
  276. for (int i = 0; i < m_internalData->m_transforms.size(); i++)
  277. {
  278. m_guiHelper->getRenderInterface()->writeSingleInstanceTransformToCPU(m_internalData->m_transforms[i].getOrigin(), m_internalData->m_transforms[i].getRotation(), i);
  279. }
  280. m_guiHelper->getRenderInterface()->writeTransforms();
  281. m_guiHelper->getRenderInterface()->renderScene();
  282. }
  283. else
  284. {
  285. TGAColor clearColor;
  286. clearColor.bgra[0] = 200;
  287. clearColor.bgra[1] = 200;
  288. clearColor.bgra[2] = 200;
  289. clearColor.bgra[3] = 255;
  290. for (int y = 0; y < m_internalData->m_height; ++y)
  291. {
  292. for (int x = 0; x < m_internalData->m_width; ++x)
  293. {
  294. m_internalData->m_rgbColorBuffer.set(x, y, clearColor);
  295. m_internalData->m_depthBuffer[x + y * m_internalData->m_width] = -1e30f;
  296. m_internalData->m_shadowBuffer[x + y * m_internalData->m_width] = -1e30f;
  297. }
  298. }
  299. ATTRIBUTE_ALIGNED16(btScalar modelMat2[16]);
  300. ATTRIBUTE_ALIGNED16(float viewMat[16]);
  301. ATTRIBUTE_ALIGNED16(float projMat[16]);
  302. CommonRenderInterface* render = this->m_app->m_renderer;
  303. render->getActiveCamera()->getCameraViewMatrix(viewMat);
  304. render->getActiveCamera()->getCameraProjectionMatrix(projMat);
  305. for (int o = 0; o < this->m_internalData->m_renderObjects.size(); o++)
  306. {
  307. const btTransform& tr = m_internalData->m_transforms[o];
  308. tr.getOpenGLMatrix(modelMat2);
  309. for (int i = 0; i < 4; i++)
  310. {
  311. for (int j = 0; j < 4; j++)
  312. {
  313. m_internalData->m_renderObjects[o]->m_modelMatrix[i][j] = float(modelMat2[i + 4 * j]);
  314. m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i + 4 * j];
  315. m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i + 4 * j];
  316. btVector3 lightDirWorld = btVector3(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1], m_internalData->m_lightPos[2]);
  317. m_internalData->m_renderObjects[o]->m_lightDirWorld = lightDirWorld.normalized();
  318. btVector3 lightColor(1.0, 1.0, 1.0);
  319. m_internalData->m_renderObjects[o]->m_lightColor = lightColor;
  320. m_internalData->m_renderObjects[o]->m_lightDistance = 10.0;
  321. m_internalData->m_renderObjects[o]->m_lightAmbientCoeff = 0.6;
  322. m_internalData->m_renderObjects[o]->m_lightDiffuseCoeff = 0.35;
  323. m_internalData->m_renderObjects[o]->m_lightSpecularCoeff = 0.05;
  324. }
  325. }
  326. TinyRenderer::renderObjectDepth(*m_internalData->m_renderObjects[o]);
  327. }
  328. for (int o = 0; o < this->m_internalData->m_renderObjects.size(); o++)
  329. {
  330. const btTransform& tr = m_internalData->m_transforms[o];
  331. tr.getOpenGLMatrix(modelMat2);
  332. for (int i = 0; i < 4; i++)
  333. {
  334. for (int j = 0; j < 4; j++)
  335. {
  336. m_internalData->m_renderObjects[o]->m_modelMatrix[i][j] = float(modelMat2[i + 4 * j]);
  337. m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i + 4 * j];
  338. m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i + 4 * j];
  339. btVector3 lightDirWorld = btVector3(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1], m_internalData->m_lightPos[2]);
  340. m_internalData->m_renderObjects[o]->m_lightDirWorld = lightDirWorld.normalized();
  341. btVector3 lightColor(1.0, 1.0, 1.0);
  342. m_internalData->m_renderObjects[o]->m_lightColor = lightColor;
  343. m_internalData->m_renderObjects[o]->m_lightDistance = 10.0;
  344. m_internalData->m_renderObjects[o]->m_lightAmbientCoeff = 0.6;
  345. m_internalData->m_renderObjects[o]->m_lightDiffuseCoeff = 0.35;
  346. m_internalData->m_renderObjects[o]->m_lightSpecularCoeff = 0.05;
  347. }
  348. }
  349. TinyRenderer::renderObject(*m_internalData->m_renderObjects[o]);
  350. }
  351. //m_app->drawText("hello",500,500);
  352. render->activateTexture(m_internalData->m_textureHandle);
  353. render->updateTexture(m_internalData->m_textureHandle, m_internalData->m_rgbColorBuffer.buffer());
  354. float color[4] = {1, 1, 1, 1};
  355. m_app->drawTexturedRect(0, 0, m_app->m_window->getWidth(), m_app->m_window->getHeight(), color, 0, 0, 1, 1, true);
  356. }
  357. }
  358. void TinyRendererSetup::physicsDebugDraw(int debugDrawFlags)
  359. {
  360. }
  361. bool TinyRendererSetup::mouseMoveCallback(float x, float y)
  362. {
  363. return false;
  364. }
  365. bool TinyRendererSetup::mouseButtonCallback(int button, int state, float x, float y)
  366. {
  367. return false;
  368. }
  369. bool TinyRendererSetup::keyboardCallback(int key, int state)
  370. {
  371. return false;
  372. }
  373. void TinyRendererSetup::syncPhysicsToGraphics(GraphicsPhysicsBridge& gfxBridge)
  374. {
  375. }
  376. CommonExampleInterface* TinyRendererCreateFunc(struct CommonExampleOptions& options)
  377. {
  378. return new TinyRendererSetup(options.m_guiHelper);
  379. }