diff --git a/examples/CommonInterfaces/CommonGUIHelperInterface.h b/examples/CommonInterfaces/CommonGUIHelperInterface.h index 9171ed5c4..916a82343 100644 --- a/examples/CommonInterfaces/CommonGUIHelperInterface.h +++ b/examples/CommonInterfaces/CommonGUIHelperInterface.h @@ -94,6 +94,8 @@ 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 setProjectiveTexture(bool useProjectiveTexture)=0; virtual void autogenerateGraphicsObjects(btDiscreteDynamicsWorld* rbWorld) =0; @@ -178,6 +180,14 @@ struct DummyGUIHelper : public GUIHelperInterface if (numPixelsCopied) *numPixelsCopied = 0; } + + virtual void setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]) + { + } + + virtual void setProjectiveTexture(bool useProjectiveTexture) + { + } virtual void autogenerateGraphicsObjects(btDiscreteDynamicsWorld* rbWorld) { diff --git a/examples/CommonInterfaces/CommonRenderInterface.h b/examples/CommonInterfaces/CommonRenderInterface.h index 50ac24c03..a64fe7dde 100644 --- a/examples/CommonInterfaces/CommonRenderInterface.h +++ b/examples/CommonInterfaces/CommonRenderInterface.h @@ -49,6 +49,8 @@ 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 setProjectiveTexture(bool useProjectiveTexture){}; 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..71970893a 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.cpp +++ b/examples/ExampleBrowser/OpenGLGuiHelper.cpp @@ -1098,6 +1098,15 @@ 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::setProjectiveTexture(bool useProjectiveTexture) +{ + m_data->m_glApp->m_renderer->setProjectiveTexture(useProjectiveTexture); +} 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..3a43a0221 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.h +++ b/examples/ExampleBrowser/OpenGLGuiHelper.h @@ -61,6 +61,9 @@ 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 setProjectiveTexture(bool useProjectiveTexture); virtual void autogenerateGraphicsObjects(btDiscreteDynamicsWorld* rbWorld) ; diff --git a/examples/OpenGLWindow/GLInstancingRenderer.cpp b/examples/OpenGLWindow/GLInstancingRenderer.cpp index 3533e1fb9..5d91a3bb6 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.cpp +++ b/examples/OpenGLWindow/GLInstancingRenderer.cpp @@ -17,7 +17,6 @@ subject to the following restrictions: ///todo: make this configurable in the gui bool useShadowMap = true;// true;//false;//true; -bool useProjectiveTexture = false; int shadowMapWidth= 4096; int shadowMapHeight= 4096; float shadowMapWorldSize=10; @@ -225,7 +224,10 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData GLfloat m_projectionMatrix[16]; GLfloat m_viewMatrix[16]; + GLfloat m_projectiveTextureProjectionMatrix[16]; + GLfloat m_projectiveTextureViewMatrix[16]; GLfloat m_viewMatrixInverse[16]; + bool m_useProjectiveTexture; b3Vector3 m_lightPos; b3Vector3 m_lightSpecularIntensity; @@ -257,8 +259,11 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData m_projectionMatrix[i]=0; m_viewMatrix[i]=0; m_viewMatrixInverse[i]=0; + m_projectiveTextureProjectionMatrix[i]=0; + m_projectiveTextureViewMatrix[i]=0; } + m_useProjectiveTexture = false; } @@ -1466,6 +1471,20 @@ 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::setProjectiveTexture(bool useProjectiveTexture) +{ + m_data->m_useProjectiveTexture = useProjectiveTexture; + useShadowMap = !useProjectiveTexture; +} void GLInstancingRenderer::updateCamera(int upAxis) { @@ -1583,9 +1602,8 @@ void GLInstancingRenderer::renderScene() renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE); } - else if (useProjectiveTexture) + else if (m_data->m_useProjectiveTexture) { - //renderSceneInternal(B3_CREATE_SHADOWMAP_RENDERMODE); renderSceneInternal(B3_USE_PROJECTIVE_TEXTURE_RENDERMODE); } else @@ -2181,7 +2199,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..310d4f03f 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.h +++ b/examples/OpenGLWindow/GLInstancingRenderer.h @@ -131,6 +131,8 @@ 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 setProjectiveTexture(bool useProjectiveTexture); 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/PhysicsClientC_API.h b/examples/SharedMemory/PhysicsClientC_API.h index 7b0799de8..ba9db70ee 100644 --- a/examples/SharedMemory/PhysicsClientC_API.h +++ b/examples/SharedMemory/PhysicsClientC_API.h @@ -213,6 +213,9 @@ B3_SHARED_API void b3RequestCameraImageSetFlags(b3SharedMemoryCommandHandle comm B3_SHARED_API void b3GetCameraImageData(b3PhysicsClientHandle physClient, struct b3CameraImageData* imageData); + +///set projective texture camera matrices. +B3_SHARED_API void b3RequestCameraImageSetProjectiveTextureMatrices(b3SharedMemoryCommandHandle commandHandle, float viewMatrix[/*16*/], float projectionMatrix[/*16*/]); ///compute a view matrix, helper function for b3RequestCameraImageSetCameraMatrices B3_SHARED_API void b3ComputeViewMatrixFromPositions(const float cameraPosition[/*3*/], const float cameraTargetPosition[/*3*/], const float cameraUp[/*3*/], float viewMatrix[/*16*/]); diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 59c078081..4352fd5ba 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -70,8 +70,6 @@ #include "BulletDynamics/Featherstone/btMultiBodyDynamicsWorld.h" #endif - - int gInternalSimFlags = 0; bool gResetSimulation = 0; int gVRTrackingObjectUniqueId = -1; @@ -3156,6 +3154,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]; @@ -3185,11 +3185,36 @@ bool PhysicsServerCommandProcessor::processRequestCameraImageCommand(const struc projMat[i] = tmpCamResult.m_projectionMatrix[i]; } } - } + } bool handled = false; if ((clientCmd.m_updateFlags & ER_BULLET_HARDWARE_OPENGL)!=0) { + if ((flags & ER_USE_PROJECTIVE_TEXTURE) != 0) + { + this->m_data->m_guiHelper->setProjectiveTexture(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 + { + this->m_data->m_guiHelper->setProjectiveTexture(false); + } m_data->m_guiHelper->copyCameraImageData(viewMat, projMat,pixelRGBA,numRequestedPixels, diff --git a/examples/SharedMemory/PhysicsServerExample.cpp b/examples/SharedMemory/PhysicsServerExample.cpp index 63af47751..0d2908ae5 100644 --- a/examples/SharedMemory/PhysicsServerExample.cpp +++ b/examples/SharedMemory/PhysicsServerExample.cpp @@ -1119,6 +1119,16 @@ 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); + } + + virtual void setProjectiveTexture(bool useProjectiveTexture) + { + m_childGuiHelper->getAppInterface()->m_renderer->setProjectiveTexture(useProjectiveTexture); + } 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/SharedMemory/SharedMemoryPublic.h b/examples/SharedMemory/SharedMemoryPublic.h index 1be0128b9..c1b3b96c3 100644 --- a/examples/SharedMemory/SharedMemoryPublic.h +++ b/examples/SharedMemory/SharedMemoryPublic.h @@ -616,7 +616,9 @@ enum EnumRenderer enum EnumRendererAuxFlags { ER_SEGMENTATION_MASK_OBJECT_AND_LINKINDEX=1, + ER_USE_PROJECTIVE_TEXTURE=2, }; + ///flags to pick the IK solver and other options enum EnumCalculateInverseKinematicsFlags { diff --git a/examples/pybullet/examples/projective_texture.py b/examples/pybullet/examples/projective_texture.py index 576a7a095..00ae2320d 100644 --- a/examples/pybullet/examples/projective_texture.py +++ b/examples/pybullet/examples/projective_texture.py @@ -25,7 +25,12 @@ if (useRealTimeSimulation): while 1: if (useRealTimeSimulation): + 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 f4a316da4..3907a3938 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 @@ -9150,6 +9156,7 @@ initpybullet(void) PyModule_AddIntConstant(m, "ER_TINY_RENDERER", ER_TINY_RENDERER); PyModule_AddIntConstant(m, "ER_BULLET_HARDWARE_OPENGL", ER_BULLET_HARDWARE_OPENGL); PyModule_AddIntConstant(m, "ER_SEGMENTATION_MASK_OBJECT_AND_LINKINDEX", ER_SEGMENTATION_MASK_OBJECT_AND_LINKINDEX); + PyModule_AddIntConstant(m, "ER_USE_PROJECTIVE_TEXTURE", ER_USE_PROJECTIVE_TEXTURE); PyModule_AddIntConstant(m, "IK_DLS", IK_DLS); PyModule_AddIntConstant(m, "IK_SDLS", IK_SDLS);