From 3c30e2f8211a019181b240148c89cd76f3f1e1f6 Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Thu, 11 Aug 2016 14:55:30 -0700 Subject: [PATCH] add segmentation mask rendering to TinyRenderer and shared memory API similar to the zbuffer, but storing the object index (int) instead of float depth --- .../CommonGUIHelperInterface.h | 13 +- .../ExampleBrowser/OpenGLExampleBrowser.cpp | 7 +- examples/ExampleBrowser/OpenGLGuiHelper.cpp | 7 +- examples/ExampleBrowser/OpenGLGuiHelper.h | 7 +- .../ImportURDFDemo/BulletUrdfImporter.cpp | 17 ++- .../ImportURDFDemo/BulletUrdfImporter.h | 5 +- .../LinkVisualShapesConverter.h | 2 +- .../Importers/ImportURDFDemo/URDF2Bullet.cpp | 2 +- .../ImportURDFDemo/URDFImporterInterface.h | 6 +- .../RenderingExamples/TinyRendererSetup.cpp | 5 +- examples/RoboticsLearning/b3RobotSimAPI.cpp | 13 +- .../SharedMemory/PhysicsClientExample.cpp | 144 +++++++++++++++--- .../PhysicsClientSharedMemory.cpp | 11 +- examples/SharedMemory/PhysicsDirect.cpp | 11 +- .../PhysicsServerCommandProcessor.cpp | 18 ++- .../SharedMemory/PhysicsServerExample.cpp | 13 +- examples/SharedMemory/SharedMemoryPublic.h | 3 +- .../TinyRendererVisualShapeConverter.cpp | 23 ++- .../TinyRendererVisualShapeConverter.h | 4 +- examples/TinyRenderer/TinyRenderer.cpp | 9 +- examples/TinyRenderer/TinyRenderer.h | 6 +- examples/TinyRenderer/our_gl.cpp | 3 +- examples/TinyRenderer/our_gl.h | 2 +- 23 files changed, 276 insertions(+), 55 deletions(-) diff --git a/examples/CommonInterfaces/CommonGUIHelperInterface.h b/examples/CommonInterfaces/CommonGUIHelperInterface.h index 726303aed..f871a7e0e 100644 --- a/examples/CommonInterfaces/CommonGUIHelperInterface.h +++ b/examples/CommonInterfaces/CommonGUIHelperInterface.h @@ -46,7 +46,11 @@ struct GUIHelperInterface virtual void resetCamera(float camDist, float pitch, float yaw, float camPosX,float camPosY, float camPosZ)=0; - virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied)=0; + virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], + unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, + float* depthBuffer, int depthBufferSizeInPixels, + int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels, + int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied)=0; virtual void autogenerateGraphicsObjects(btDiscreteDynamicsWorld* rbWorld) =0; @@ -107,7 +111,12 @@ struct DummyGUIHelper : public GUIHelperInterface { } - virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int width, int height, int* numPixelsCopied) + virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], + unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, + float* depthBuffer, int depthBufferSizeInPixels, + int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels, + int startPixelIndex, int width, int height, int* numPixelsCopied) + { if (numPixelsCopied) *numPixelsCopied = 0; diff --git a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp index d6fab65a3..e29eb6996 100644 --- a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp +++ b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp @@ -634,10 +634,12 @@ struct QuickCanvas : public Common2dCanvasInterface MyGraphWindow* m_gw[MAX_GRAPH_WINDOWS]; GraphingTexture* m_gt[MAX_GRAPH_WINDOWS]; int m_curNumGraphWindows; + int m_curXpos; QuickCanvas(GL3TexLoader* myTexLoader) :m_myTexLoader(myTexLoader), - m_curNumGraphWindows(0) + m_curNumGraphWindows(0), + m_curXpos(0) { for (int i=0;igetInternalData()); input.m_width=width; input.m_height=height; - input.m_xPos = 10000;//GUI will clamp it to the right//300; + input.m_xPos = m_curXpos;//GUI will clamp it to the right//300; + m_curXpos+=width+20; input.m_yPos = 10000;//GUI will clamp it to bottom input.m_name=canvasName; input.m_texName = canvasName; diff --git a/examples/ExampleBrowser/OpenGLGuiHelper.cpp b/examples/ExampleBrowser/OpenGLGuiHelper.cpp index a5d7e0c3d..d4aa246dc 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.cpp +++ b/examples/ExampleBrowser/OpenGLGuiHelper.cpp @@ -338,7 +338,12 @@ void OpenGLGuiHelper::resetCamera(float camDist, float pitch, float yaw, float c } -void OpenGLGuiHelper::copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied) +void OpenGLGuiHelper::copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], + unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, + float* depthBuffer, int depthBufferSizeInPixels, + int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels, + int startPixelIndex, int destinationWidth, + int destinationHeight, int* numPixelsCopied) { int sourceWidth = m_data->m_glApp->m_window->getWidth()*m_data->m_glApp->m_window->getRetinaScale(); int sourceHeight = m_data->m_glApp->m_window->getHeight()*m_data->m_glApp->m_window->getRetinaScale(); diff --git a/examples/ExampleBrowser/OpenGLGuiHelper.h b/examples/ExampleBrowser/OpenGLGuiHelper.h index 3ebdc4572..2fac0313e 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.h +++ b/examples/ExampleBrowser/OpenGLGuiHelper.h @@ -44,7 +44,12 @@ struct OpenGLGuiHelper : public GUIHelperInterface virtual void resetCamera(float camDist, float pitch, float yaw, float camPosX,float camPosY, float camPosZ); - virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied); + virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], + unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, + float* depthBuffer, int depthBufferSizeInPixels, + int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels, + int startPixelIndex, int destinationWidth, + int destinationHeight, int* numPixelsCopied); virtual void autogenerateGraphicsObjects(btDiscreteDynamicsWorld* rbWorld) ; diff --git a/examples/Importers/ImportURDFDemo/BulletUrdfImporter.cpp b/examples/Importers/ImportURDFDemo/BulletUrdfImporter.cpp index 425620c1a..992840850 100644 --- a/examples/Importers/ImportURDFDemo/BulletUrdfImporter.cpp +++ b/examples/Importers/ImportURDFDemo/BulletUrdfImporter.cpp @@ -45,6 +45,7 @@ struct BulletURDFInternalData UrdfParser m_urdfParser; struct GUIHelperInterface* m_guiHelper; char m_pathPrefix[1024]; + int m_bodyId; btHashMap m_linkColors; btAlignedObjectArray m_allocatedCollisionShapes; @@ -208,6 +209,18 @@ const char* BulletURDFImporter::getPathPrefix() return m_data->m_pathPrefix; } + +void BulletURDFImporter::setBodyUniqueId(int bodyId) +{ + m_data->m_bodyId =bodyId; +} + + +int BulletURDFImporter::getBodyUniqueId() const +{ + return m_data->m_bodyId; +} + BulletURDFImporter::~BulletURDFImporter() { @@ -1017,13 +1030,13 @@ bool BulletURDFImporter::getLinkContactInfo(int linkIndex, URDFLinkContactInfo& return false; } -void BulletURDFImporter::convertLinkVisualShapes2(int linkIndex, const char* pathPrefix, const btTransform& localInertiaFrame, class btCollisionObject* colObj) const +void BulletURDFImporter::convertLinkVisualShapes2(int linkIndex, const char* pathPrefix, const btTransform& localInertiaFrame, class btCollisionObject* colObj, int objectIndex) const { if (m_data->m_customVisualShapesConverter) { const UrdfModel& model = m_data->m_urdfParser.getModel(); - m_data->m_customVisualShapesConverter->convertVisualShapes(linkIndex,pathPrefix,localInertiaFrame, model, colObj); + m_data->m_customVisualShapesConverter->convertVisualShapes(linkIndex,pathPrefix,localInertiaFrame, model, colObj, objectIndex); } } diff --git a/examples/Importers/ImportURDFDemo/BulletUrdfImporter.h b/examples/Importers/ImportURDFDemo/BulletUrdfImporter.h index ea945aa33..127ed25b7 100644 --- a/examples/Importers/ImportURDFDemo/BulletUrdfImporter.h +++ b/examples/Importers/ImportURDFDemo/BulletUrdfImporter.h @@ -25,7 +25,8 @@ public: virtual bool loadSDF(const char* fileName, bool forceFixedBase = false); virtual int getNumModels() const; virtual void activateModel(int modelIndex); - + virtual void setBodyUniqueId(int bodyId); + virtual int getBodyUniqueId() const; const char* getPathPrefix(); void printTree(); //for debugging @@ -50,7 +51,7 @@ public: virtual int convertLinkVisualShapes(int linkIndex, const char* pathPrefix, const btTransform& inertialFrame) const; - virtual void convertLinkVisualShapes2(int linkIndex, const char* pathPrefix, const btTransform& inertialFrame, class btCollisionObject* colObj) const; + virtual void convertLinkVisualShapes2(int linkIndex, const char* pathPrefix, const btTransform& inertialFrame, class btCollisionObject* colObj, int objectIndex) const; ///todo(erwincoumans) refactor this convertLinkCollisionShapes/memory allocation diff --git a/examples/Importers/ImportURDFDemo/LinkVisualShapesConverter.h b/examples/Importers/ImportURDFDemo/LinkVisualShapesConverter.h index a9c941dc0..814838ced 100644 --- a/examples/Importers/ImportURDFDemo/LinkVisualShapesConverter.h +++ b/examples/Importers/ImportURDFDemo/LinkVisualShapesConverter.h @@ -3,7 +3,7 @@ struct LinkVisualShapesConverter { - virtual void convertVisualShapes(int linkIndex, const char* pathPrefix, const class btTransform& localInertiaFrame, const struct UrdfModel& model, class btCollisionObject* colObj)=0; + virtual void convertVisualShapes(int linkIndex, const char* pathPrefix, const class btTransform& localInertiaFrame, const struct UrdfModel& model, class btCollisionObject* colObj, int objectIndex)=0; }; #endif //LINK_VISUAL_SHAPES_CONVERTER_H diff --git a/examples/Importers/ImportURDFDemo/URDF2Bullet.cpp b/examples/Importers/ImportURDFDemo/URDF2Bullet.cpp index 597f1a4d9..367e674da 100644 --- a/examples/Importers/ImportURDFDemo/URDF2Bullet.cpp +++ b/examples/Importers/ImportURDFDemo/URDF2Bullet.cpp @@ -397,7 +397,7 @@ void ConvertURDF2BulletInternal(const URDFImporterInterface& u2b, MultiBodyCreat u2b.getLinkColor(urdfLinkIndex,color); creation.createCollisionObjectGraphicsInstance(urdfLinkIndex,col,color); - u2b.convertLinkVisualShapes2(urdfLinkIndex,pathPrefix,localInertialFrame,col); + u2b.convertLinkVisualShapes2(urdfLinkIndex,pathPrefix,localInertialFrame,col, u2b.getBodyUniqueId()); URDFLinkContactInfo contactInfo; u2b.getLinkContactInfo(urdfLinkIndex,contactInfo); diff --git a/examples/Importers/ImportURDFDemo/URDFImporterInterface.h b/examples/Importers/ImportURDFDemo/URDFImporterInterface.h index e3299eda5..ce688ec90 100644 --- a/examples/Importers/ImportURDFDemo/URDFImporterInterface.h +++ b/examples/Importers/ImportURDFDemo/URDFImporterInterface.h @@ -48,8 +48,10 @@ public: ///quick hack: need to rethink the API/dependencies of this virtual int convertLinkVisualShapes(int linkIndex, const char* pathPrefix, const btTransform& inertialFrame) const { return -1;} - virtual void convertLinkVisualShapes2(int linkIndex, const char* pathPrefix, const btTransform& inertialFrame, class btCollisionObject* colObj) const { } - + virtual void convertLinkVisualShapes2(int linkIndex, const char* pathPrefix, const btTransform& inertialFrame, class btCollisionObject* colObj, int objectIndex) const { } + virtual void setBodyUniqueId(int bodyId) {} + virtual int getBodyUniqueId() const { return 0;} + virtual class btCompoundShape* convertLinkCollisionShapes(int linkIndex, const char* pathPrefix, const btTransform& localInertiaFrame) const = 0; }; diff --git a/examples/RenderingExamples/TinyRendererSetup.cpp b/examples/RenderingExamples/TinyRendererSetup.cpp index fd3f80e41..89fd1437d 100644 --- a/examples/RenderingExamples/TinyRendererSetup.cpp +++ b/examples/RenderingExamples/TinyRendererSetup.cpp @@ -23,6 +23,7 @@ struct TinyRendererSetupInternalData TGAImage m_rgbColorBuffer; b3AlignedObjectArray m_depthBuffer; + b3AlignedObjectArray m_segmentationMaskBuffer; int m_width; @@ -185,7 +186,9 @@ TinyRendererSetup::TinyRendererSetup(struct GUIHelperInterface* gui) m_internalData->m_shapePtr.push_back(0); TinyRenderObjectData* ob = new TinyRenderObjectData( m_internalData->m_rgbColorBuffer, - m_internalData->m_depthBuffer); + m_internalData->m_depthBuffer, + m_internalData->m_segmentationMaskBuffer, + m_internalData->m_renderObjects.size(),0); //ob->loadModel("cube.obj"); const int* indices = &meshData.m_gfxShape->m_indices->at(0); ob->registerMeshShape(&meshData.m_gfxShape->m_vertices->at(0).xyzw[0], diff --git a/examples/RoboticsLearning/b3RobotSimAPI.cpp b/examples/RoboticsLearning/b3RobotSimAPI.cpp index 38914873e..adbef7a20 100644 --- a/examples/RoboticsLearning/b3RobotSimAPI.cpp +++ b/examples/RoboticsLearning/b3RobotSimAPI.cpp @@ -352,12 +352,19 @@ public: int m_rgbaBufferSizeInPixels; float* m_depthBuffer; int m_depthBufferSizeInPixels; + int* m_segmentationMaskBuffer; + int m_segmentationMaskBufferSizeInPixels; int m_startPixelIndex; int m_destinationWidth; int m_destinationHeight; int* m_numPixelsCopied; - virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied) + virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], + unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, + float* depthBuffer, int depthBufferSizeInPixels, + int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels, + int startPixelIndex, int destinationWidth, + int destinationHeight, int* numPixelsCopied) { m_cs->lock(); for (int i=0;i<16;i++) @@ -369,6 +376,8 @@ public: m_rgbaBufferSizeInPixels = rgbaBufferSizeInPixels; m_depthBuffer = depthBuffer; m_depthBufferSizeInPixels = depthBufferSizeInPixels; + m_segmentationMaskBuffer = segmentationMaskBuffer; + m_segmentationMaskBufferSizeInPixels = segmentationMaskBufferSizeInPixels; m_startPixelIndex = startPixelIndex; m_destinationWidth = destinationWidth; m_destinationHeight = destinationHeight; @@ -532,6 +541,8 @@ void b3RobotSimAPI::processMultiThreadedGraphicsRequests() m_data->m_multiThreadedHelper->m_rgbaBufferSizeInPixels, m_data->m_multiThreadedHelper->m_depthBuffer, m_data->m_multiThreadedHelper->m_depthBufferSizeInPixels, + m_data->m_multiThreadedHelper->m_segmentationMaskBuffer, + m_data->m_multiThreadedHelper->m_segmentationMaskBufferSizeInPixels, m_data->m_multiThreadedHelper->m_startPixelIndex, m_data->m_multiThreadedHelper->m_destinationWidth, m_data->m_multiThreadedHelper->m_destinationHeight, diff --git a/examples/SharedMemory/PhysicsClientExample.cpp b/examples/SharedMemory/PhysicsClientExample.cpp index 801dd0d89..718ad5967 100644 --- a/examples/SharedMemory/PhysicsClientExample.cpp +++ b/examples/SharedMemory/PhysicsClientExample.cpp @@ -44,7 +44,9 @@ protected: int m_selectedBody; int m_prevSelectedBody; struct Common2dCanvasInterface* m_canvas; - int m_canvasIndex; + int m_canvasRGBIndex; + int m_canvasDepthIndex; + int m_canvasSegMaskIndex; void createButton(const char* name, int id, bool isTrigger ); @@ -248,7 +250,7 @@ void PhysicsClientExample::prepareAndSubmitCommand(int commandId) case CMD_LOAD_SDF: { - b3SharedMemoryCommandHandle commandHandle = b3LoadSdfCommandInit(m_physicsClientHandle, "kuka_iiwa/model.sdf"); + b3SharedMemoryCommandHandle commandHandle = b3LoadSdfCommandInit(m_physicsClientHandle, "two_cubes.sdf");//kuka_iiwa/model.sdf"); b3SubmitClientCommand(m_physicsClientHandle, commandHandle); break; } @@ -460,7 +462,11 @@ m_prevSelectedBody(-1), m_numMotors(0), m_options(options), m_isOptionalServerConnected(false), -m_canvas(0) +m_canvas(0), +m_canvasRGBIndex(-1), +m_canvasDepthIndex(-1), +m_canvasSegMaskIndex(-1) + { b3Printf("Started PhysicsClientExample\n"); } @@ -479,9 +485,15 @@ PhysicsClientExample::~PhysicsClientExample() m_physicsServer.disconnectSharedMemory(deInitializeSharedMemory); } - if (m_canvas && (m_canvasIndex>=0)) + if (m_canvas) { - m_canvas->destroyCanvas(m_canvasIndex); + if (m_canvasRGBIndex>=0) + m_canvas->destroyCanvas(m_canvasRGBIndex); + if (m_canvasDepthIndex>=0) + m_canvas->destroyCanvas(m_canvasDepthIndex); + if (m_canvasSegMaskIndex>=0) + m_canvas->destroyCanvas(m_canvasSegMaskIndex); + } b3Printf("~PhysicsClientExample\n"); } @@ -613,9 +625,10 @@ void PhysicsClientExample::initPhysics() m_canvas = m_guiHelper->get2dCanvasInterface(); if (m_canvas) { - - - m_canvasIndex = m_canvas->createCanvas("Synthetic Camera",camVisualizerWidth, camVisualizerHeight); + + m_canvasRGBIndex = m_canvas->createCanvas("Synthetic Camera RGB data",camVisualizerWidth, camVisualizerHeight); + m_canvasDepthIndex = m_canvas->createCanvas("Synthetic Camera Depth data",camVisualizerWidth, camVisualizerHeight); + m_canvasSegMaskIndex = m_canvas->createCanvas("Synthetic Camera Segmentation Mask",camVisualizerWidth, camVisualizerHeight); for (int i=0;isetPixel(m_canvasIndex,i,j,red,green,blue,alpha); + m_canvas->setPixel(m_canvasRGBIndex,i,j,red,green,blue,alpha); + m_canvas->setPixel(m_canvasDepthIndex,i,j,red,green,blue,alpha); + m_canvas->setPixel(m_canvasSegMaskIndex,i,j,red,green,blue,alpha); + } } - m_canvas->refreshImageData(m_canvasIndex); + m_canvas->refreshImageData(m_canvasRGBIndex); + m_canvas->refreshImageData(m_canvasDepthIndex); + m_canvas->refreshImageData(m_canvasSegMaskIndex); + } @@ -698,8 +717,36 @@ void PhysicsClientExample::stepSimulation(float deltaTime) // sprintf(msg,"Camera image %d OK\n",counter++); b3CameraImageData imageData; b3GetCameraImageData(m_physicsClientHandle,&imageData); - if (m_canvas && m_canvasIndex >=0) + if (m_canvas) { + + //compute depth image range + float minDepthValue = 1e20f; + float maxDepthValue = -1e20f; + + for (int i=0;i=0) + { + int depthPixelIndex = (xIndex+yIndex*imageData.m_pixelWidth); + float depthValue = imageData.m_depthValues4[depthPixelIndex]; + //todo: rescale the depthValue to [0..255] + if (depthValue>-1e20) + { + maxDepthValue = btMax(maxDepthValue,depthValue); + minDepthValue = btMin(minDepthValue,depthValue); + } + } + } + } + for (int i=0;isetPixel(m_canvasIndex,i,j, - imageData.m_rgbColorData[pixelIndex], - imageData.m_rgbColorData[pixelIndex+1], - imageData.m_rgbColorData[pixelIndex+2], - 255); //alpha set to 255 + if (m_canvasRGBIndex >=0) + { + int rgbPixelIndex = (xIndex+yIndex*imageData.m_pixelWidth)*bytesPerPixel; + m_canvas->setPixel(m_canvasRGBIndex,i,j, + imageData.m_rgbColorData[rgbPixelIndex], + imageData.m_rgbColorData[rgbPixelIndex+1], + imageData.m_rgbColorData[rgbPixelIndex+2], + 255); //alpha set to 255 + } + + if (m_canvasDepthIndex >=0) + { + int depthPixelIndex = (xIndex+yIndex*imageData.m_pixelWidth); + float depthValue = imageData.m_depthValues4[depthPixelIndex]; + //todo: rescale the depthValue to [0..255] + if (depthValue>-1e20) + { + int rgb = (depthValue-minDepthValue)*(255. / (btFabs(maxDepthValue-minDepthValue))); + if (rgb<0 || rgb>255) + { + + printf("rgb=%d\n",rgb); + } + + m_canvas->setPixel(m_canvasDepthIndex,i,j, + rgb, + rgb, + 255, 255); //alpha set to 255 + } else + { + m_canvas->setPixel(m_canvasDepthIndex,i,j, + 0, + 0, + 0, 255); //alpha set to 255 + } + } + if (m_canvasSegMaskIndex>=0 && (0!=imageData.m_segmentationMaskValues)) + { + int segmentationMaskPixelIndex = (xIndex+yIndex*imageData.m_pixelWidth); + int segmentationMask = imageData.m_segmentationMaskValues[segmentationMaskPixelIndex]; + btVector4 palette[4] = {btVector4(32,255,32,255), + btVector4(32,32,255,255), + btVector4(255,255,32,255), + btVector4(32,255,255,255)}; + if (segmentationMask>=0) + { + btVector4 rgb = palette[segmentationMask&3]; + m_canvas->setPixel(m_canvasSegMaskIndex,i,j, + rgb.x(), + rgb.y(), + rgb.z(), 255); //alpha set to 255 + } else + { + m_canvas->setPixel(m_canvasSegMaskIndex,i,j, + 0, + 0, + 0, 255); //alpha set to 255 + } + } } } - m_canvas->refreshImageData(m_canvasIndex); + if (m_canvasRGBIndex >=0) + m_canvas->refreshImageData(m_canvasRGBIndex); + if (m_canvasDepthIndex >=0) + m_canvas->refreshImageData(m_canvasDepthIndex); + if (m_canvasSegMaskIndex >=0) + m_canvas->refreshImageData(m_canvasSegMaskIndex); + + + } // b3Printf(msg); diff --git a/examples/SharedMemory/PhysicsClientSharedMemory.cpp b/examples/SharedMemory/PhysicsClientSharedMemory.cpp index 433515091..e7002b04f 100644 --- a/examples/SharedMemory/PhysicsClientSharedMemory.cpp +++ b/examples/SharedMemory/PhysicsClientSharedMemory.cpp @@ -36,6 +36,7 @@ struct PhysicsClientSharedMemoryInternalData { int m_cachedCameraPixelsHeight; btAlignedObjectArray m_cachedCameraPixelsRGBA; btAlignedObjectArray m_cachedCameraDepthBuffer; + btAlignedObjectArray m_cachedSegmentationMaskBuffer; btAlignedObjectArray m_bodyIdsRequestInfo; SharedMemoryStatus m_tempBackupServerStatus; @@ -514,6 +515,7 @@ const SharedMemoryStatus* PhysicsClientSharedMemory::processServerStatus() { m_data->m_cachedCameraPixelsRGBA.reserve(numPixels*numBytesPerPixel); m_data->m_cachedCameraDepthBuffer.resize(numTotalPixels); + m_data->m_cachedSegmentationMaskBuffer.resize(numTotalPixels); m_data->m_cachedCameraPixelsRGBA.resize(numTotalPixels*numBytesPerPixel); @@ -522,12 +524,18 @@ const SharedMemoryStatus* PhysicsClientSharedMemory::processServerStatus() { // printf("pixel = %d\n", rgbaPixelsReceived[0]); float* depthBuffer = (float*)&(m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[serverCmd.m_sendPixelDataArguments.m_numPixelsCopied*4]); + int* segmentationMaskBuffer = (int*)&(m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[serverCmd.m_sendPixelDataArguments.m_numPixelsCopied*8]); for (int i=0;im_cachedCameraDepthBuffer[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex] = depthBuffer[i]; } + for (int i=0;im_cachedSegmentationMaskBuffer[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex] = segmentationMaskBuffer[i]; + } + for (int i=0;im_cachedCameraPixelsRGBA[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex*numBytesPerPixel] @@ -704,8 +712,9 @@ void PhysicsClientSharedMemory::getCachedCameraImage(struct b3CameraImageData* c { cameraData->m_pixelWidth = m_data->m_cachedCameraPixelsWidth; cameraData->m_pixelHeight = m_data->m_cachedCameraPixelsHeight; - cameraData->m_depthValues = m_data->m_cachedCameraDepthBuffer.size() ? &m_data->m_cachedCameraDepthBuffer[0] : 0; + cameraData->m_depthValues4 = m_data->m_cachedCameraDepthBuffer.size() ? &m_data->m_cachedCameraDepthBuffer[0] : 0; cameraData->m_rgbColorData = m_data->m_cachedCameraPixelsRGBA.size() ? &m_data->m_cachedCameraPixelsRGBA[0] : 0; + cameraData->m_segmentationMaskValues = m_data->m_cachedSegmentationMaskBuffer.size()?&m_data->m_cachedSegmentationMaskBuffer[0] : 0; } const float* PhysicsClientSharedMemory::getDebugLinesFrom() const { diff --git a/examples/SharedMemory/PhysicsDirect.cpp b/examples/SharedMemory/PhysicsDirect.cpp index 2f1b1f413..d913954c5 100644 --- a/examples/SharedMemory/PhysicsDirect.cpp +++ b/examples/SharedMemory/PhysicsDirect.cpp @@ -37,6 +37,8 @@ struct PhysicsDirectInternalData int m_cachedCameraPixelsHeight; btAlignedObjectArray m_cachedCameraPixelsRGBA; btAlignedObjectArray m_cachedCameraDepthBuffer; + btAlignedObjectArray m_cachedSegmentationMask; + PhysicsServerCommandProcessor* m_commandProcessor; @@ -205,6 +207,7 @@ bool PhysicsDirect::processCamera(const struct SharedMemoryCommand& orgCommand) m_data->m_cachedCameraPixelsRGBA.reserve(numPixels*numBytesPerPixel); m_data->m_cachedCameraDepthBuffer.resize(numTotalPixels); + m_data->m_cachedSegmentationMask.resize(numTotalPixels); m_data->m_cachedCameraPixelsRGBA.resize(numTotalPixels*numBytesPerPixel); @@ -212,6 +215,7 @@ bool PhysicsDirect::processCamera(const struct SharedMemoryCommand& orgCommand) (unsigned char*)&m_data->m_bulletStreamDataServerToClient[0]; float* depthBuffer = (float*)&(m_data->m_bulletStreamDataServerToClient[serverCmd.m_sendPixelDataArguments.m_numPixelsCopied*4]); + int* segmentationMaskBuffer = (int*)&(m_data->m_bulletStreamDataServerToClient[serverCmd.m_sendPixelDataArguments.m_numPixelsCopied*8]); // printf("pixel = %d\n", rgbaPixelsReceived[0]); @@ -219,6 +223,10 @@ bool PhysicsDirect::processCamera(const struct SharedMemoryCommand& orgCommand) { m_data->m_cachedCameraDepthBuffer[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex] = depthBuffer[i]; } + for (int i=0;im_cachedSegmentationMask[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex] = segmentationMaskBuffer[i]; + } for (int i=0;im_cachedCameraPixelsRGBA[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex*numBytesPerPixel] @@ -456,7 +464,8 @@ void PhysicsDirect::getCachedCameraImage(b3CameraImageData* cameraData) { cameraData->m_pixelWidth = m_data->m_cachedCameraPixelsWidth; cameraData->m_pixelHeight = m_data->m_cachedCameraPixelsHeight; - cameraData->m_depthValues = m_data->m_cachedCameraDepthBuffer.size() ? &m_data->m_cachedCameraDepthBuffer[0] : 0; + cameraData->m_depthValues4 = m_data->m_cachedCameraDepthBuffer.size() ? &m_data->m_cachedCameraDepthBuffer[0] : 0; cameraData->m_rgbColorData = m_data->m_cachedCameraPixelsRGBA.size() ? &m_data->m_cachedCameraPixelsRGBA[0] : 0; + cameraData->m_segmentationMaskValues = m_data->m_cachedSegmentationMask.size()? &m_data->m_cachedSegmentationMask[0] : 0; } } diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 1937448ce..d3ada5bca 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -751,7 +751,7 @@ bool PhysicsServerCommandProcessor::loadSdf(const char* fileName, char* bufferSe int bodyUniqueId = m_data->allocHandle(); InternalBodyHandle* bodyHandle = m_data->getHandle(bodyUniqueId); - + u2b.setBodyUniqueId(bodyUniqueId); { btScalar mass = 0; bodyHandle->m_rootLocalInertialFrame.setIdentity(); @@ -845,6 +845,7 @@ bool PhysicsServerCommandProcessor::loadUrdf(const char* fileName, const btVecto if (bodyUniqueIdPtr) *bodyUniqueIdPtr= bodyUniqueId; + u2b.setBodyUniqueId(bodyUniqueId); InternalBodyHandle* bodyHandle = m_data->getHandle(bodyUniqueId); { @@ -1165,15 +1166,21 @@ bool PhysicsServerCommandProcessor::processCommand(const struct SharedMemoryComm if (numRemainingPixels>0) { - int maxNumPixels = bufferSizeInBytes/8-1; + int totalBytesPerPixel = 4+4+4;//4 for rgb, 4 for depth, 4 for segmentation mask + int maxNumPixels = bufferSizeInBytes/totalBytesPerPixel-1; unsigned char* pixelRGBA = (unsigned char*)bufferServerToClient; int numRequestedPixels = btMin(maxNumPixels,numRemainingPixels); float* depthBuffer = (float*)(bufferServerToClient+numRequestedPixels*4); + int* segmentationMaskBuffer = (int*)(bufferServerToClient+numRequestedPixels*8); if ((clientCmd.m_updateFlags & ER_BULLET_HARDWARE_OPENGL)!=0) { - m_data->m_guiHelper->copyCameraImageData(clientCmd.m_requestPixelDataArguments.m_viewMatrix,clientCmd.m_requestPixelDataArguments.m_projectionMatrix,pixelRGBA,numRequestedPixels,depthBuffer,numRequestedPixels,startPixelIndex,width,height,&numPixelsCopied); + m_data->m_guiHelper->copyCameraImageData(clientCmd.m_requestPixelDataArguments.m_viewMatrix, + clientCmd.m_requestPixelDataArguments.m_projectionMatrix,pixelRGBA,numRequestedPixels, + depthBuffer,numRequestedPixels, + segmentationMaskBuffer, numRequestedPixels, + startPixelIndex,width,height,&numPixelsCopied); } else { @@ -1194,7 +1201,10 @@ bool PhysicsServerCommandProcessor::processCommand(const struct SharedMemoryComm } - m_data->m_visualConverter.copyCameraImageData(pixelRGBA,numRequestedPixels,depthBuffer,numRequestedPixels,startPixelIndex,&width,&height,&numPixelsCopied); + m_data->m_visualConverter.copyCameraImageData(pixelRGBA,numRequestedPixels, + depthBuffer,numRequestedPixels, + segmentationMaskBuffer, numRequestedPixels, + startPixelIndex,&width,&height,&numPixelsCopied); } //each pixel takes 4 RGBA values and 1 float = 8 bytes diff --git a/examples/SharedMemory/PhysicsServerExample.cpp b/examples/SharedMemory/PhysicsServerExample.cpp index 8a0099525..ca868b29f 100644 --- a/examples/SharedMemory/PhysicsServerExample.cpp +++ b/examples/SharedMemory/PhysicsServerExample.cpp @@ -400,12 +400,19 @@ public: int m_rgbaBufferSizeInPixels; float* m_depthBuffer; int m_depthBufferSizeInPixels; + int* m_segmentationMaskBuffer; + int m_segmentationMaskBufferSizeInPixels; int m_startPixelIndex; int m_destinationWidth; int m_destinationHeight; int* m_numPixelsCopied; - virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied) + virtual void copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], + unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, + float* depthBuffer, int depthBufferSizeInPixels, + int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels, + int startPixelIndex, int destinationWidth, + int destinationHeight, int* numPixelsCopied) { m_cs->lock(); for (int i=0;i<16;i++) @@ -417,6 +424,8 @@ public: m_rgbaBufferSizeInPixels = rgbaBufferSizeInPixels; m_depthBuffer = depthBuffer; m_depthBufferSizeInPixels = depthBufferSizeInPixels; + m_segmentationMaskBuffer = segmentationMaskBuffer; + m_segmentationMaskBufferSizeInPixels = segmentationMaskBufferSizeInPixels; m_startPixelIndex = startPixelIndex; m_destinationWidth = destinationWidth; m_destinationHeight = destinationHeight; @@ -768,6 +777,8 @@ void PhysicsServerExample::stepSimulation(float deltaTime) m_multiThreadedHelper->m_rgbaBufferSizeInPixels, m_multiThreadedHelper->m_depthBuffer, m_multiThreadedHelper->m_depthBufferSizeInPixels, + m_multiThreadedHelper->m_segmentationMaskBuffer, + m_multiThreadedHelper->m_segmentationMaskBufferSizeInPixels, m_multiThreadedHelper->m_startPixelIndex, m_multiThreadedHelper->m_destinationWidth, m_multiThreadedHelper->m_destinationHeight, diff --git a/examples/SharedMemory/SharedMemoryPublic.h b/examples/SharedMemory/SharedMemoryPublic.h index 0b6542806..9fee50a66 100644 --- a/examples/SharedMemory/SharedMemoryPublic.h +++ b/examples/SharedMemory/SharedMemoryPublic.h @@ -122,7 +122,8 @@ struct b3CameraImageData int m_pixelWidth; int m_pixelHeight; const unsigned char* m_rgbColorData;//3*m_pixelWidth*m_pixelHeight bytes - const float* m_depthValues;//m_pixelWidth*m_pixelHeight floats + const float* m_depthValues4;//m_pixelWidth*m_pixelHeight floats + const int* m_segmentationMaskValues;//m_pixelWidth*m_pixelHeight ints }; ///b3LinkState provides extra information such as the Cartesian world coordinates diff --git a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp index 9457652a6..1cce9ef57 100644 --- a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp +++ b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp @@ -66,6 +66,8 @@ struct TinyRendererVisualShapeConverterInternalData int m_swHeight; TGAImage m_rgbColorBuffer; b3AlignedObjectArray m_depthBuffer; + b3AlignedObjectArray m_segmentationMaskBuffer; + SimpleCamera m_camera; TinyRendererVisualShapeConverterInternalData() @@ -75,6 +77,7 @@ struct TinyRendererVisualShapeConverterInternalData m_rgbColorBuffer(START_WIDTH,START_HEIGHT,TGAImage::RGB) { m_depthBuffer.resize(m_swWidth*m_swHeight); + m_segmentationMaskBuffer.resize(m_swWidth*m_swHeight,-1); } }; @@ -440,7 +443,7 @@ void convertURDFToVisualShape(const UrdfVisual* visual, const char* urdfPathPref -void TinyRendererVisualShapeConverter::convertVisualShapes(int linkIndex, const char* pathPrefix, const btTransform& localInertiaFrame, const UrdfModel& model, class btCollisionObject* colObj) +void TinyRendererVisualShapeConverter::convertVisualShapes(int linkIndex, const char* pathPrefix, const btTransform& localInertiaFrame, const UrdfModel& model, class btCollisionObject* colObj, int objectIndex) { @@ -487,7 +490,7 @@ void TinyRendererVisualShapeConverter::convertVisualShapes(int linkIndex, const if (vertices.size() && indices.size()) { - TinyRenderObjectData* tinyObj = new TinyRenderObjectData(m_data->m_rgbColorBuffer,m_data->m_depthBuffer); + TinyRenderObjectData* tinyObj = new TinyRenderObjectData(m_data->m_rgbColorBuffer,m_data->m_depthBuffer, m_data->m_segmentationMaskBuffer, objectIndex,0); unsigned char* textureImage=0; int textureWidth=0; int textureHeight=0; @@ -535,6 +538,7 @@ void TinyRendererVisualShapeConverter::clearBuffers(TGAColor& clearColor) { m_data->m_rgbColorBuffer.set(x,y,clearColor); m_data->m_depthBuffer[x+y*m_data->m_swWidth] = -1e30f; + m_data->m_segmentationMaskBuffer[x+y*m_data->m_swWidth] = -1; } } @@ -624,7 +628,7 @@ void TinyRendererVisualShapeConverter::render(const float viewMat[16], const flo // printf("flipped!\n"); m_data->m_rgbColorBuffer.flip_vertically(); - //flip z-buffer + //flip z-buffer and segmentation Buffer { int half = m_data->m_swHeight>>1; for (int j=0; jm_swWidth;i++) { btSwap(m_data->m_depthBuffer[l1+i],m_data->m_depthBuffer[l2+i]); + btSwap(m_data->m_segmentationMaskBuffer[l1+i],m_data->m_segmentationMaskBuffer[l2+i]); } } } @@ -652,11 +657,16 @@ void TinyRendererVisualShapeConverter::setWidthAndHeight(int width, int height) m_data->m_swHeight = height; m_data->m_depthBuffer.resize(m_data->m_swWidth*m_data->m_swHeight); + m_data->m_segmentationMaskBuffer.resize(m_data->m_swWidth*m_data->m_swHeight); m_data->m_rgbColorBuffer = TGAImage(width, height, TGAImage::RGB); + } -void TinyRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int* widthPtr, int* heightPtr, int* numPixelsCopied) +void TinyRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, + float* depthBuffer, int depthBufferSizeInPixels, + int* segmentationMaskBuffer, int segmentationMaskSizeInPixels, + int startPixelIndex, int* widthPtr, int* heightPtr, int* numPixelsCopied) { int w = m_data->m_rgbColorBuffer.get_width(); int h = m_data->m_rgbColorBuffer.get_height(); @@ -682,6 +692,11 @@ void TinyRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixels { depthBuffer[i] = m_data->m_depthBuffer[i+startPixelIndex]; } + if (segmentationMaskBuffer) + { + segmentationMaskBuffer[i] = m_data->m_segmentationMaskBuffer[i+startPixelIndex]; + } + if (pixelsRGBA) { pixelsRGBA[i*numBytesPerPixel] = m_data->m_rgbColorBuffer.buffer()[(i+startPixelIndex)*3+0]; diff --git a/examples/SharedMemory/TinyRendererVisualShapeConverter.h b/examples/SharedMemory/TinyRendererVisualShapeConverter.h index 6baff2491..0602fe29c 100644 --- a/examples/SharedMemory/TinyRendererVisualShapeConverter.h +++ b/examples/SharedMemory/TinyRendererVisualShapeConverter.h @@ -13,7 +13,7 @@ struct TinyRendererVisualShapeConverter : public LinkVisualShapesConverter virtual ~TinyRendererVisualShapeConverter(); - virtual void convertVisualShapes(int linkIndex, const char* pathPrefix, const btTransform& localInertiaFrame, const UrdfModel& model, class btCollisionObject* colShape); + virtual void convertVisualShapes(int linkIndex, const char* pathPrefix, const btTransform& localInertiaFrame, const UrdfModel& model, class btCollisionObject* colShape, int objectIndex); void setUpAxis(int axis); @@ -26,7 +26,7 @@ struct TinyRendererVisualShapeConverter : public LinkVisualShapesConverter void getWidthAndHeight(int& width, int& height); void setWidthAndHeight(int width, int height); - void copyCameraImageData(unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int* widthPtr, int* heightPtr, int* numPixelsCopied); + void copyCameraImageData(unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels,int* segmentationMaskBuffer, int segmentationMaskSizeInPixels, int startPixelIndex, int* widthPtr, int* heightPtr, int* numPixelsCopied); void render(); void render(const float viewMat[16], const float projMat[16]); diff --git a/examples/TinyRenderer/TinyRenderer.cpp b/examples/TinyRenderer/TinyRenderer.cpp index 650948a1a..75518abd5 100644 --- a/examples/TinyRenderer/TinyRenderer.cpp +++ b/examples/TinyRenderer/TinyRenderer.cpp @@ -88,12 +88,14 @@ struct Shader : public IShader { }; -TinyRenderObjectData::TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer) +TinyRenderObjectData::TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer, b3AlignedObjectArray& segmentationMaskBuffer, int objectIndex,int objectIndex2) :m_rgbColorBuffer(rgbColorBuffer), m_depthBuffer(depthBuffer), +m_segmentationMaskBuffer(segmentationMaskBuffer), m_model(0), m_userData(0), -m_userIndex(-1) +m_userIndex(-1), +m_objectIndex(objectIndex) { Vec3f eye(1,1,3); Vec3f center(0,0,0); @@ -247,6 +249,7 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData) renderData.m_viewportMatrix = viewport(0,0,width, height); b3AlignedObjectArray& zbuffer = renderData.m_depthBuffer; + b3AlignedObjectArray& segmentationMaskBuffer = renderData.m_segmentationMaskBuffer; TGAImage& frame = renderData.m_rgbColorBuffer; @@ -264,7 +267,7 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData) for (int j=0; j<3; j++) { shader.vertex(i, j); } - triangle(shader.varying_tri, shader, frame, &zbuffer[0], renderData.m_viewportMatrix); + triangle(shader.varying_tri, shader, frame, &zbuffer[0], &segmentationMaskBuffer[0], renderData.m_viewportMatrix, renderData.m_objectIndex); } } diff --git a/examples/TinyRenderer/TinyRenderer.h b/examples/TinyRenderer/TinyRenderer.h index c6f4f9821..550134be7 100644 --- a/examples/TinyRenderer/TinyRenderer.h +++ b/examples/TinyRenderer/TinyRenderer.h @@ -28,8 +28,9 @@ struct TinyRenderObjectData TGAImage& m_rgbColorBuffer; b3AlignedObjectArray& m_depthBuffer; - - TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray& depthBuffer); + b3AlignedObjectArray& m_segmentationMaskBuffer; + + TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer, b3AlignedObjectArray& segmentationMaskBuffer,int objectIndex,int objectIndex2); virtual ~TinyRenderObjectData(); void loadModel(const char* fileName); @@ -41,6 +42,7 @@ struct TinyRenderObjectData void* m_userData; int m_userIndex; + int m_objectIndex; }; diff --git a/examples/TinyRenderer/our_gl.cpp b/examples/TinyRenderer/our_gl.cpp index 4f23b73e7..53e8cd021 100644 --- a/examples/TinyRenderer/our_gl.cpp +++ b/examples/TinyRenderer/our_gl.cpp @@ -59,7 +59,7 @@ Vec3f barycentric(Vec2f A, Vec2f B, Vec2f C, Vec2f P) { return Vec3f(-1,1,1); // in this case generate negative coordinates, it will be thrown away by the rasterizator } -void triangle(mat<4,3,float> &clipc, IShader &shader, TGAImage &image, float *zbuffer, const Matrix& viewPortMatrix) { +void triangle(mat<4,3,float> &clipc, IShader &shader, TGAImage &image, float *zbuffer, int* segmentationMaskBuffer, const Matrix& viewPortMatrix, int objectIndex) { mat<3,4,float> pts = (viewPortMatrix*clipc).transpose(); // transposed to ease access to each of the points @@ -100,6 +100,7 @@ void triangle(mat<4,3,float> &clipc, IShader &shader, TGAImage &image, float *zb bool discard = shader.fragment(bc_clip, color); if (!discard) { zbuffer[P.x+P.y*image.get_width()] = frag_depth; + segmentationMaskBuffer[P.x+P.y*image.get_width()] = objectIndex; image.set(P.x, P.y, color); } } diff --git a/examples/TinyRenderer/our_gl.h b/examples/TinyRenderer/our_gl.h index d80156904..60d5c400f 100644 --- a/examples/TinyRenderer/our_gl.h +++ b/examples/TinyRenderer/our_gl.h @@ -17,6 +17,6 @@ struct IShader { }; //void triangle(Vec4f *pts, IShader &shader, TGAImage &image, float *zbuffer); -void triangle(mat<4,3,float> &pts, IShader &shader, TGAImage &image, float *zbuffer, const Matrix& viewPortMatrix); +void triangle(mat<4,3,float> &pts, IShader &shader, TGAImage &image, float *zbuffer, int* segmentationMaskBuffer, const Matrix& viewPortMatrix, int objectIndex); #endif //__OUR_GL_H__