From ed49edc1af95feb0c3a55ce18332c01edb174704 Mon Sep 17 00:00:00 2001 From: erwincoumans Date: Sat, 20 Oct 2018 15:56:56 -0700 Subject: [PATCH] PyBullet change API (since it was broken): pybullet_changeVisualShape with textureUniqueId ==-1 will clear the texture b3InitUpdateVisualShape doesn't take textureUniqueId by default. new API b3UpdateVisualShapeTexture to change texture (-1 will clear texture) PyBullet/BulletRobotics: allow to reset the textureUniqueId to -1, to clear a texture PyBullet/BulletRobotics: save all texture handles --- .../OpenGLWindow/GLInstancingRenderer.cpp | 4 ++ examples/SharedMemory/PhysicsClientC_API.cpp | 25 +++++-- examples/SharedMemory/PhysicsClientC_API.h | 3 +- .../PhysicsServerCommandProcessor.cpp | 72 +++++++++++-------- .../b3RobotSimulatorClientAPI_NoDirect.cpp | 7 +- .../b3RobotSimulatorClientAPI_NoDirect.h | 2 +- .../TinyRendererVisualShapeConverter.cpp | 10 ++- examples/pybullet/pybullet.c | 9 ++- 8 files changed, 90 insertions(+), 42 deletions(-) diff --git a/examples/OpenGLWindow/GLInstancingRenderer.cpp b/examples/OpenGLWindow/GLInstancingRenderer.cpp index eef648d4d..a35f3a1ad 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.cpp +++ b/examples/OpenGLWindow/GLInstancingRenderer.cpp @@ -1009,6 +1009,10 @@ void GLInstancingRenderer::replaceTexture(int shapeIndex, int textureId) { gfxObj->m_textureIndex = textureId; gfxObj->m_flags |= eGfxHasTexture; + } else + { + gfxObj->m_textureIndex = -1; + gfxObj->m_flags &= ~eGfxHasTexture; } } } diff --git a/examples/SharedMemory/PhysicsClientC_API.cpp b/examples/SharedMemory/PhysicsClientC_API.cpp index 4b557bcd4..84c287900 100644 --- a/examples/SharedMemory/PhysicsClientC_API.cpp +++ b/examples/SharedMemory/PhysicsClientC_API.cpp @@ -4008,7 +4008,7 @@ B3_SHARED_API int b3GetStatusTextureUniqueId(b3SharedMemoryStatusHandle statusHa return uid; } -B3_SHARED_API b3SharedMemoryCommandHandle b3InitUpdateVisualShape(b3PhysicsClientHandle physClient, int bodyUniqueId, int jointIndex, int shapeIndex, int textureUniqueId) +B3_SHARED_API b3SharedMemoryCommandHandle b3InitUpdateVisualShape(b3PhysicsClientHandle physClient, int bodyUniqueId, int jointIndex, int shapeIndex) { PhysicsClient* cl = (PhysicsClient*)physClient; b3Assert(cl); @@ -4019,16 +4019,27 @@ B3_SHARED_API b3SharedMemoryCommandHandle b3InitUpdateVisualShape(b3PhysicsClien command->m_updateVisualShapeDataArguments.m_bodyUniqueId = bodyUniqueId; command->m_updateVisualShapeDataArguments.m_jointIndex = jointIndex; command->m_updateVisualShapeDataArguments.m_shapeIndex = shapeIndex; - command->m_updateVisualShapeDataArguments.m_textureUniqueId = textureUniqueId; + command->m_updateVisualShapeDataArguments.m_textureUniqueId = -2; command->m_updateFlags = 0; - - if (textureUniqueId >= 0) - { - command->m_updateFlags |= CMD_UPDATE_VISUAL_SHAPE_TEXTURE; - } return (b3SharedMemoryCommandHandle)command; } +B3_SHARED_API void b3UpdateVisualShapeTexture(b3SharedMemoryCommandHandle commandHandle, int textureUniqueId) +{ + struct SharedMemoryCommand* command = (struct SharedMemoryCommand*)commandHandle; + b3Assert(command); + b3Assert(command->m_type == CMD_UPDATE_VISUAL_SHAPE); + + if (command->m_type == CMD_UPDATE_VISUAL_SHAPE) + { + if (textureUniqueId >= -1) + { + command->m_updateFlags |= CMD_UPDATE_VISUAL_SHAPE_TEXTURE; + command->m_updateVisualShapeDataArguments.m_textureUniqueId = textureUniqueId; + } + } +} + B3_SHARED_API void b3UpdateVisualShapeRGBAColor(b3SharedMemoryCommandHandle commandHandle, const double rgbaColor[4]) { struct SharedMemoryCommand* command = (struct SharedMemoryCommand*)commandHandle; diff --git a/examples/SharedMemory/PhysicsClientC_API.h b/examples/SharedMemory/PhysicsClientC_API.h index 795013e43..5350eedb9 100644 --- a/examples/SharedMemory/PhysicsClientC_API.h +++ b/examples/SharedMemory/PhysicsClientC_API.h @@ -308,7 +308,8 @@ extern "C" B3_SHARED_API b3SharedMemoryCommandHandle b3CreateChangeTextureCommandInit(b3PhysicsClientHandle physClient, int textureUniqueId, int width, int height, const char* rgbPixels); - B3_SHARED_API b3SharedMemoryCommandHandle b3InitUpdateVisualShape(b3PhysicsClientHandle physClient, int bodyUniqueId, int jointIndex, int shapeIndex, int textureUniqueId); + B3_SHARED_API b3SharedMemoryCommandHandle b3InitUpdateVisualShape(b3PhysicsClientHandle physClient, int bodyUniqueId, int jointIndex, int shapeIndex); + B3_SHARED_API void b3UpdateVisualShapeTexture(b3SharedMemoryCommandHandle commandHandle, int textureUniqueId); B3_SHARED_API void b3UpdateVisualShapeRGBAColor(b3SharedMemoryCommandHandle commandHandle, const double rgbaColor[/*4*/]); B3_SHARED_API void b3UpdateVisualShapeSpecularColor(b3SharedMemoryCommandHandle commandHandle, const double specularColor[/*3*/]); diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 1503b29bd..98545600a 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -2755,7 +2755,7 @@ bool PhysicsServerCommandProcessor::processImportedObjects(const char* fileName, SaveWorldObjectData sd; sd.m_fileName = fileName; - int currentOpenGLTextureIndex = 0; + for (int m = 0; m < u2b.getNumModels(); m++) { @@ -2971,38 +2971,38 @@ bool PhysicsServerCommandProcessor::processImportedObjects(const char* fileName, } { - int startShapeIndex = 0; + if (m_data->m_pluginManager.getRenderInterface()) { + int currentOpenGLTextureIndex=0; int totalNumVisualShapes = m_data->m_pluginManager.getRenderInterface()->getNumVisualShapes(bodyUniqueId); - //int totalBytesPerVisualShape = sizeof (b3VisualShapeData); - //int visualShapeStorage = bufferSizeInBytes / totalBytesPerVisualShape - 1; - b3VisualShapeData tmpShape; - int remain = totalNumVisualShapes - startShapeIndex; - int shapeIndex = startShapeIndex; - - int success = m_data->m_pluginManager.getRenderInterface()->getVisualShapesData(bodyUniqueId, shapeIndex, &tmpShape); - if (success) + for (int shapeIndex=0;shapeIndex= 0) + b3VisualShapeData tmpShape; + int success = m_data->m_pluginManager.getRenderInterface()->getVisualShapesData(bodyUniqueId, shapeIndex, &tmpShape); + if (success) { - int openglTextureUniqueId = -1; - - //find companion opengl texture unique id and create a 'textureUid' - if (currentOpenGLTextureIndex < u2b.getNumAllocatedTextures()) + if (tmpShape.m_tinyRendererTextureId >= 0) { - openglTextureUniqueId = u2b.getAllocatedTexture(currentOpenGLTextureIndex); - currentOpenGLTextureIndex++; - } + int openglTextureUniqueId = -1; - int texHandle = m_data->m_textureHandles.allocHandle(); - InternalTextureHandle* texH = m_data->m_textureHandles.getHandle(texHandle); - if (texH) - { - texH->m_tinyRendererTextureId = tmpShape.m_tinyRendererTextureId; - texH->m_openglTextureId = openglTextureUniqueId; + //find companion opengl texture unique id and create a 'textureUid' + if (currentOpenGLTextureIndex=0) + { + int texHandle = m_data->m_textureHandles.allocHandle(); + InternalTextureHandle* texH = m_data->m_textureHandles.getHandle(texHandle); + if (texH) + { + texH->m_tinyRendererTextureId = tmpShape.m_tinyRendererTextureId; + texH->m_openglTextureId = openglTextureUniqueId; + } + } } } } @@ -9779,9 +9779,12 @@ bool PhysicsServerCommandProcessor::processUpdateVisualShapeCommand(const struct if (clientCmd.m_updateFlags & CMD_UPDATE_VISUAL_SHAPE_TEXTURE) { - texHandle = m_data->m_textureHandles.getHandle(clientCmd.m_updateVisualShapeDataArguments.m_textureUniqueId); + if (clientCmd.m_updateVisualShapeDataArguments.m_textureUniqueId>=0) + { + texHandle = m_data->m_textureHandles.getHandle(clientCmd.m_updateVisualShapeDataArguments.m_textureUniqueId); + } - if (clientCmd.m_updateVisualShapeDataArguments.m_textureUniqueId >= 0) + if (clientCmd.m_updateVisualShapeDataArguments.m_textureUniqueId >= -1) { if (texHandle) { @@ -9792,6 +9795,12 @@ bool PhysicsServerCommandProcessor::processUpdateVisualShapeCommand(const struct clientCmd.m_updateVisualShapeDataArguments.m_shapeIndex, texHandle->m_tinyRendererTextureId); } + } else + { + m_data->m_pluginManager.getRenderInterface()->changeShapeTexture(clientCmd.m_updateVisualShapeDataArguments.m_bodyUniqueId, + clientCmd.m_updateVisualShapeDataArguments.m_jointIndex, + clientCmd.m_updateVisualShapeDataArguments.m_shapeIndex, + -1); } } } @@ -9812,10 +9821,13 @@ bool PhysicsServerCommandProcessor::processUpdateVisualShapeCommand(const struct int graphicsIndex = bodyHandle->m_multiBody->getBaseCollider()->getUserIndex(); if (clientCmd.m_updateFlags & CMD_UPDATE_VISUAL_SHAPE_TEXTURE) { + int shapeIndex = m_data->m_guiHelper->getShapeIndexFromInstance(graphicsIndex); if (texHandle) { - int shapeIndex = m_data->m_guiHelper->getShapeIndexFromInstance(graphicsIndex); m_data->m_guiHelper->replaceTexture(shapeIndex, texHandle->m_openglTextureId); + } else + { + m_data->m_guiHelper->replaceTexture(shapeIndex, -1); } } if (clientCmd.m_updateFlags & CMD_UPDATE_VISUAL_SHAPE_RGBA_COLOR) @@ -9843,11 +9855,15 @@ bool PhysicsServerCommandProcessor::processUpdateVisualShapeCommand(const struct int graphicsIndex = bodyHandle->m_multiBody->getLink(linkIndex).m_collider->getUserIndex(); if (clientCmd.m_updateFlags & CMD_UPDATE_VISUAL_SHAPE_TEXTURE) { + int shapeIndex = m_data->m_guiHelper->getShapeIndexFromInstance(graphicsIndex); if (texHandle) { - int shapeIndex = m_data->m_guiHelper->getShapeIndexFromInstance(graphicsIndex); m_data->m_guiHelper->replaceTexture(shapeIndex, texHandle->m_openglTextureId); } + else + { + m_data->m_guiHelper->replaceTexture(shapeIndex, -1); + } } if (clientCmd.m_updateFlags & CMD_UPDATE_VISUAL_SHAPE_RGBA_COLOR) { diff --git a/examples/SharedMemory/b3RobotSimulatorClientAPI_NoDirect.cpp b/examples/SharedMemory/b3RobotSimulatorClientAPI_NoDirect.cpp index 0116579f6..033ee9c6b 100644 --- a/examples/SharedMemory/b3RobotSimulatorClientAPI_NoDirect.cpp +++ b/examples/SharedMemory/b3RobotSimulatorClientAPI_NoDirect.cpp @@ -190,7 +190,12 @@ bool b3RobotSimulatorClientAPI_NoDirect::changeVisualShape(const struct b3RobotS b3SharedMemoryStatusHandle statusHandle; int statusType; - commandHandle = b3InitUpdateVisualShape(m_data->m_physicsClientHandle, objectUniqueId, jointIndex, shapeIndex, textureUniqueId); + commandHandle = b3InitUpdateVisualShape(m_data->m_physicsClientHandle, objectUniqueId, jointIndex, shapeIndex); + + if (textureUniqueId>=-1) + { + b3UpdateVisualShapeTexture(commandHandle, textureUniqueId); + } if (args.m_hasSpecularColor) { diff --git a/examples/SharedMemory/b3RobotSimulatorClientAPI_NoDirect.h b/examples/SharedMemory/b3RobotSimulatorClientAPI_NoDirect.h index c21c0713d..7e9c56c14 100644 --- a/examples/SharedMemory/b3RobotSimulatorClientAPI_NoDirect.h +++ b/examples/SharedMemory/b3RobotSimulatorClientAPI_NoDirect.h @@ -75,7 +75,7 @@ struct b3RobotSimulatorChangeVisualShapeArgs : m_objectUniqueId(-1), m_linkIndex(-1), m_shapeIndex(-1), - m_textureUniqueId(-1), + m_textureUniqueId(-2), m_rgbaColor(0, 0, 0, 1), m_hasRgbaColor(false), m_specularColor(1, 1, 1), diff --git a/examples/SharedMemory/plugins/tinyRendererPlugin/TinyRendererVisualShapeConverter.cpp b/examples/SharedMemory/plugins/tinyRendererPlugin/TinyRendererVisualShapeConverter.cpp index a414758cf..e2ea84a22 100644 --- a/examples/SharedMemory/plugins/tinyRendererPlugin/TinyRendererVisualShapeConverter.cpp +++ b/examples/SharedMemory/plugins/tinyRendererPlugin/TinyRendererVisualShapeConverter.cpp @@ -1159,7 +1159,7 @@ void TinyRendererVisualShapeConverter::resetAll() void TinyRendererVisualShapeConverter::changeShapeTexture(int objectUniqueId, int jointIndex, int shapeIndex, int textureUniqueId) { btAssert(textureUniqueId < m_data->m_textures.size()); - if (textureUniqueId >= 0 && textureUniqueId < m_data->m_textures.size()) + if (textureUniqueId >= -1 && textureUniqueId < m_data->m_textures.size()) { for (int n = 0; n < m_data->m_swRenderInstances.size(); n++) { @@ -1176,7 +1176,13 @@ void TinyRendererVisualShapeConverter::changeShapeTexture(int objectUniqueId, in if ((shapeIndex < 0) || (shapeIndex == v)) { - renderObj->m_model->setDiffuseTextureFromData(m_data->m_textures[textureUniqueId].textureData1, m_data->m_textures[textureUniqueId].m_width, m_data->m_textures[textureUniqueId].m_height); + if (textureUniqueId>=0) + { + renderObj->m_model->setDiffuseTextureFromData(m_data->m_textures[textureUniqueId].textureData1, m_data->m_textures[textureUniqueId].m_width, m_data->m_textures[textureUniqueId].m_height); + } else + { + renderObj->m_model->setDiffuseTextureFromData(0,0,0); + } } } } diff --git a/examples/pybullet/pybullet.c b/examples/pybullet/pybullet.c index a98e2eca7..9bfbbc66c 100644 --- a/examples/pybullet/pybullet.c +++ b/examples/pybullet/pybullet.c @@ -5584,7 +5584,7 @@ static PyObject* pybullet_changeVisualShape(PyObject* self, PyObject* args, PyOb int objectUniqueId = -1; int jointIndex = -1; int shapeIndex = -1; - int textureUniqueId = -1; + int textureUniqueId = -2; b3SharedMemoryCommandHandle commandHandle; b3SharedMemoryStatusHandle statusHandle; int statusType; @@ -5606,7 +5606,12 @@ static PyObject* pybullet_changeVisualShape(PyObject* self, PyObject* args, PyOb } { - commandHandle = b3InitUpdateVisualShape(sm, objectUniqueId, jointIndex, shapeIndex, textureUniqueId); + commandHandle = b3InitUpdateVisualShape(sm, objectUniqueId, jointIndex, shapeIndex); + + if (textureUniqueId>=-1) + { + b3UpdateVisualShapeTexture(commandHandle, textureUniqueId); + } if (specularColorObj) {