From 37696dd87ecfceb328046011dd2206c7ec170d2c Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Sun, 18 Mar 2018 18:45:54 -0700 Subject: [PATCH] Add Bullet C API and pybullet API to set projective texture matrices. --- .../CommonGUIHelperInterface.h | 5 +++++ .../CommonInterfaces/CommonRenderInterface.h | 1 + examples/ExampleBrowser/OpenGLGuiHelper.cpp | 4 ++++ examples/ExampleBrowser/OpenGLGuiHelper.h | 2 ++ .../OpenGLWindow/GLInstancingRenderer.cpp | 14 ++++++++++++- examples/OpenGLWindow/GLInstancingRenderer.h | 1 + examples/SharedMemory/PhysicsClientC_API.cpp | 13 ++++++++++++ .../PhysicsServerCommandProcessor.cpp | 21 ++++++++++++++++++- .../SharedMemory/PhysicsServerExample.cpp | 5 +++++ examples/SharedMemory/SharedMemoryCommands.h | 3 +++ .../pybullet/examples/projective_texture.py | 8 +++++-- examples/pybullet/pybullet.c | 12 ++++++++--- 12 files changed, 82 insertions(+), 7 deletions(-) diff --git a/examples/CommonInterfaces/CommonGUIHelperInterface.h b/examples/CommonInterfaces/CommonGUIHelperInterface.h index 9171ed5c4..20482d4c8 100644 --- a/examples/CommonInterfaces/CommonGUIHelperInterface.h +++ b/examples/CommonInterfaces/CommonGUIHelperInterface.h @@ -94,6 +94,7 @@ struct GUIHelperInterface int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels, int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied){} + virtual void setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16])=0; virtual void autogenerateGraphicsObjects(btDiscreteDynamicsWorld* rbWorld) =0; @@ -178,6 +179,10 @@ struct DummyGUIHelper : public GUIHelperInterface if (numPixelsCopied) *numPixelsCopied = 0; } + + virtual void setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]) + { + } virtual void autogenerateGraphicsObjects(btDiscreteDynamicsWorld* rbWorld) { diff --git a/examples/CommonInterfaces/CommonRenderInterface.h b/examples/CommonInterfaces/CommonRenderInterface.h index 50ac24c03..3706d0c6d 100644 --- a/examples/CommonInterfaces/CommonRenderInterface.h +++ b/examples/CommonInterfaces/CommonRenderInterface.h @@ -49,6 +49,7 @@ struct CommonRenderInterface virtual void setLightPosition(const float lightPos[3]) = 0; virtual void setLightPosition(const double lightPos[3]) = 0; + virtual void setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]){}; virtual void renderScene()=0; virtual void renderSceneInternal(int renderMode=B3_DEFAULT_RENDERMODE){}; diff --git a/examples/ExampleBrowser/OpenGLGuiHelper.cpp b/examples/ExampleBrowser/OpenGLGuiHelper.cpp index 5df588ec3..82fc9a50d 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.cpp +++ b/examples/ExampleBrowser/OpenGLGuiHelper.cpp @@ -1098,6 +1098,10 @@ bool OpenGLGuiHelper::getCameraInfo(int* width, int* height, float viewMatrix[16 return false; } +void OpenGLGuiHelper::setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]) +{ + m_data->m_glApp->m_renderer->setProjectiveTextureMatrices(viewMatrix, projectionMatrix); +} void OpenGLGuiHelper::copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, diff --git a/examples/ExampleBrowser/OpenGLGuiHelper.h b/examples/ExampleBrowser/OpenGLGuiHelper.h index 319f2ed21..47854b0f2 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.h +++ b/examples/ExampleBrowser/OpenGLGuiHelper.h @@ -61,6 +61,8 @@ struct OpenGLGuiHelper : public GUIHelperInterface int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels, int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied); + + virtual void setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]); virtual void autogenerateGraphicsObjects(btDiscreteDynamicsWorld* rbWorld) ; diff --git a/examples/OpenGLWindow/GLInstancingRenderer.cpp b/examples/OpenGLWindow/GLInstancingRenderer.cpp index fdaa24440..0462f053e 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.cpp +++ b/examples/OpenGLWindow/GLInstancingRenderer.cpp @@ -225,6 +225,8 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData GLfloat m_projectionMatrix[16]; GLfloat m_viewMatrix[16]; + GLfloat m_projectiveTextureProjectionMatrix[16]; + GLfloat m_projectiveTextureViewMatrix[16]; GLfloat m_viewMatrixInverse[16]; b3Vector3 m_lightPos; @@ -257,6 +259,8 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData m_projectionMatrix[i]=0; m_viewMatrix[i]=0; m_viewMatrixInverse[i]=0; + m_projectiveTextureProjectionMatrix[i]=0; + m_projectiveTextureViewMatrix[i]=0; } } @@ -1466,6 +1470,14 @@ void GLInstancingRenderer::setLightPosition(const double lightPos[3]) m_data->m_lightPos[2] = lightPos[2]; } +void GLInstancingRenderer::setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]) +{ + for (int i = 0; i < 16; i++) + { + m_data->m_projectiveTextureViewMatrix[i] = viewMatrix[i]; + m_data->m_projectiveTextureProjectionMatrix[i] = projectionMatrix[i]; + } +} void GLInstancingRenderer::updateCamera(int upAxis) { @@ -2180,7 +2192,7 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode) // TODO: Expose the projective texture matrix setup. Temporarily set it to be the same as camera view projection matrix. GLfloat textureMVP[16]; - b3Matrix4x4Mul16(m_data->m_projectionMatrix,m_data->m_viewMatrix,textureMVP); + b3Matrix4x4Mul16(m_data->m_projectiveTextureProjectionMatrix,m_data->m_projectiveTextureViewMatrix,textureMVP); //float m_frustumZNear=0.1; //float m_frustumZFar=100.f; diff --git a/examples/OpenGLWindow/GLInstancingRenderer.h b/examples/OpenGLWindow/GLInstancingRenderer.h index 073c05349..7f0e5a68e 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.h +++ b/examples/OpenGLWindow/GLInstancingRenderer.h @@ -131,6 +131,7 @@ public: virtual void setLightPosition(const float lightPos[3]); virtual void setLightPosition(const double lightPos[3]); void setLightSpecularIntensity(const float lightSpecularIntensity[3]); + virtual void setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]); virtual void resize(int width, int height); virtual int getScreenWidth() diff --git a/examples/SharedMemory/PhysicsClientC_API.cpp b/examples/SharedMemory/PhysicsClientC_API.cpp index 2a6009920..f3ddb03cd 100644 --- a/examples/SharedMemory/PhysicsClientC_API.cpp +++ b/examples/SharedMemory/PhysicsClientC_API.cpp @@ -3081,6 +3081,19 @@ B3_SHARED_API void b3RequestCameraImageSetShadow(b3SharedMemoryCommandHandle com command->m_updateFlags |= REQUEST_PIXEL_ARGS_SET_SHADOW; } +B3_SHARED_API void b3RequestCameraImageSetProjectiveTextureMatrices(b3SharedMemoryCommandHandle commandHandle, float viewMatrix[16], float projectionMatrix[16]) +{ + struct SharedMemoryCommand* command = (struct SharedMemoryCommand*) commandHandle; + b3Assert(command); + b3Assert(command->m_type == CMD_REQUEST_CAMERA_IMAGE_DATA); + for (int i=0;i<16;i++) + { + command->m_requestPixelDataArguments.m_projectiveTextureProjectionMatrix[i] = projectionMatrix[i]; + command->m_requestPixelDataArguments.m_projectiveTextureViewMatrix[i] = viewMatrix[i]; + } + command->m_updateFlags |= REQUEST_PIXEL_ARGS_HAS_PROJECTIVE_TEXTURE_MATRICES; +} + B3_SHARED_API void b3ComputePositionFromViewMatrix(const float viewMatrix[16], float cameraPosition[3], float cameraTargetPosition[3], float cameraUp[3]) { b3Matrix3x3 r(viewMatrix[0], viewMatrix[4], viewMatrix[8], viewMatrix[1], viewMatrix[5], viewMatrix[9], viewMatrix[2], viewMatrix[6], viewMatrix[10]); diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 2a2488e3b..fba94fa52 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -3157,6 +3157,8 @@ bool PhysicsServerCommandProcessor::processRequestCameraImageCommand(const struc serverStatusOut.m_numDataStreamBytes = numRequestedPixels * totalBytesPerPixel; float viewMat[16]; float projMat[16]; + float projTextureViewMat[16]; + float projTextureProjMat[16]; for (int i=0;i<16;i++) { viewMat[i] = clientCmd.m_requestPixelDataArguments.m_viewMatrix[i]; @@ -3186,7 +3188,7 @@ bool PhysicsServerCommandProcessor::processRequestCameraImageCommand(const struc projMat[i] = tmpCamResult.m_projectionMatrix[i]; } } - } + } bool handled = false; if ((clientCmd.m_updateFlags & ER_BULLET_HARDWARE_OPENGL)!=0) @@ -3195,6 +3197,23 @@ bool PhysicsServerCommandProcessor::processRequestCameraImageCommand(const struc { useShadowMap = false; useProjectiveTexture = true; + if ((clientCmd.m_updateFlags & REQUEST_PIXEL_ARGS_HAS_PROJECTIVE_TEXTURE_MATRICES)!=0) + { + for (int i=0;i<16;i++) + { + projTextureViewMat[i] = clientCmd.m_requestPixelDataArguments.m_projectiveTextureViewMatrix[i]; + projTextureProjMat[i] = clientCmd.m_requestPixelDataArguments.m_projectiveTextureProjectionMatrix[i]; + } + } + else // If no specified matrices for projective texture, then use the camera matrices. + { + for (int i=0;i<16;i++) + { + projTextureViewMat[i] = viewMat[i]; + projTextureProjMat[i] = projMat[i]; + } + } + this->m_data->m_guiHelper->setProjectiveTextureMatrices(projTextureViewMat, projTextureProjMat); } else { diff --git a/examples/SharedMemory/PhysicsServerExample.cpp b/examples/SharedMemory/PhysicsServerExample.cpp index 63af47751..01c712af0 100644 --- a/examples/SharedMemory/PhysicsServerExample.cpp +++ b/examples/SharedMemory/PhysicsServerExample.cpp @@ -1119,6 +1119,11 @@ public: m_cs->setSharedParam(1,eGUIHelperDisplayCameraImageData); workerThreadWait(); } + + virtual void setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]) + { + m_childGuiHelper->getAppInterface()->m_renderer->setProjectiveTextureMatrices(viewMatrix, projectionMatrix); + } btDiscreteDynamicsWorld* m_dynamicsWorld; diff --git a/examples/SharedMemory/SharedMemoryCommands.h b/examples/SharedMemory/SharedMemoryCommands.h index e23eefa69..9331c87d9 100644 --- a/examples/SharedMemory/SharedMemoryCommands.h +++ b/examples/SharedMemory/SharedMemoryCommands.h @@ -240,6 +240,8 @@ struct RequestPixelDataArgs float m_lightSpecularCoeff; int m_hasShadow; int m_flags; + float m_projectiveTextureViewMatrix[16]; + float m_projectiveTextureProjectionMatrix[16]; }; enum EnumRequestPixelDataUpdateFlags @@ -254,6 +256,7 @@ enum EnumRequestPixelDataUpdateFlags REQUEST_PIXEL_ARGS_SET_DIFFUSE_COEFF=128, REQUEST_PIXEL_ARGS_SET_SPECULAR_COEFF=256, REQUEST_PIXEL_ARGS_HAS_FLAGS = 512, + REQUEST_PIXEL_ARGS_HAS_PROJECTIVE_TEXTURE_MATRICES=1024, //don't exceed (1<<15), because this enum is shared with EnumRenderer in SharedMemoryPublic.h diff --git a/examples/pybullet/examples/projective_texture.py b/examples/pybullet/examples/projective_texture.py index e1e4931b7..00ae2320d 100644 --- a/examples/pybullet/examples/projective_texture.py +++ b/examples/pybullet/examples/projective_texture.py @@ -25,8 +25,12 @@ if (useRealTimeSimulation): while 1: if (useRealTimeSimulation): - p.getCameraImage(300, 300, lightColor=[1.0,0.0,0.0], renderer=p.ER_BULLET_HARDWARE_OPENGL, flags=p.ER_USE_PROJECTIVE_TEXTURE) + camera = p.getDebugVisualizerCamera() + viewMat = camera[2] + projMat = camera[3] + #An example of setting the view matrix for the projective texture. + #viewMat = p.computeViewMatrix(cameraEyePosition=[7,0,0], cameraTargetPosition=[0,0,0], cameraUpVector=[0,0,1]) + p.getCameraImage(300, 300, renderer=p.ER_BULLET_HARDWARE_OPENGL, flags=p.ER_USE_PROJECTIVE_TEXTURE, projectiveTextureView=viewMat, projectiveTextureProj=projMat) p.setGravity(0,0,0) - sleep(0.01) # Time in seconds. else: p.stepSimulation() diff --git a/examples/pybullet/pybullet.c b/examples/pybullet/pybullet.c index 5bd7e90b9..761da3f52 100644 --- a/examples/pybullet/pybullet.c +++ b/examples/pybullet/pybullet.c @@ -6761,10 +6761,12 @@ static PyObject* pybullet_getCameraImage(PyObject* self, PyObject* args, PyObjec { /// request an image from a simulated camera, using software or hardware renderer. struct b3CameraImageData imageData; - PyObject *objViewMat = 0, *objProjMat = 0, *lightDirObj = 0, *lightColorObj = 0; + PyObject *objViewMat = 0, *objProjMat = 0, *lightDirObj = 0, *lightColorObj = 0, *objProjectiveTextureView = 0, *objProjectiveTextureProj = 0; int width, height; float viewMatrix[16]; float projectionMatrix[16]; + float projectiveTextureView[16]; + float projectiveTextureProj[16]; float lightDir[3]; float lightColor[3]; float lightDist = -1; @@ -6779,9 +6781,9 @@ static PyObject* pybullet_getCameraImage(PyObject* self, PyObject* args, PyObjec int physicsClientId = 0; b3PhysicsClientHandle sm = 0; // set camera resolution, optionally view, projection matrix, light direction, light color, light distance, shadow - static char* kwlist[] = {"width", "height", "viewMatrix", "projectionMatrix", "lightDirection", "lightColor", "lightDistance", "shadow", "lightAmbientCoeff", "lightDiffuseCoeff", "lightSpecularCoeff", "renderer", "flags", "physicsClientId", NULL}; + static char* kwlist[] = {"width", "height", "viewMatrix", "projectionMatrix", "lightDirection", "lightColor", "lightDistance", "shadow", "lightAmbientCoeff", "lightDiffuseCoeff", "lightSpecularCoeff", "renderer", "flags", "projectiveTextureView", "projectiveTextureProj", "physicsClientId", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, keywds, "ii|OOOOfifffiii", kwlist, &width, &height, &objViewMat, &objProjMat, &lightDirObj, &lightColorObj, &lightDist, &hasShadow, &lightAmbientCoeff, &lightDiffuseCoeff, &lightSpecularCoeff, &renderer, &flags, &physicsClientId)) + if (!PyArg_ParseTupleAndKeywords(args, keywds, "ii|OOOOfifffiiOOi", kwlist, &width, &height, &objViewMat, &objProjMat, &lightDirObj, &lightColorObj, &lightDist, &hasShadow, &lightAmbientCoeff, &lightDiffuseCoeff, &lightSpecularCoeff, &renderer, &flags, &objProjectiveTextureView, &objProjectiveTextureProj, &physicsClientId)) { return NULL; } @@ -6837,6 +6839,10 @@ static PyObject* pybullet_getCameraImage(PyObject* self, PyObject* args, PyObjec { b3RequestCameraImageSetFlags(command, flags); } + if (objProjectiveTextureView && objProjectiveTextureProj && pybullet_internalSetMatrix(objProjectiveTextureView, projectiveTextureView) && (pybullet_internalSetMatrix(objProjectiveTextureProj, projectiveTextureProj))) + { + b3RequestCameraImageSetProjectiveTextureMatrices(command, projectiveTextureView, projectiveTextureProj); + } if (renderer>=0) { b3RequestCameraImageSelectRenderer(command, renderer);//renderer could be ER_BULLET_HARDWARE_OPENGL