From 93ba8af0238bc0b6e4f5bcc732f533c34fcce6cf Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Sun, 20 Nov 2016 12:52:12 -0800 Subject: [PATCH 1/2] Add shared memory API to change light color. --- examples/SharedMemory/PhysicsClientC_API.cpp | 11 +++++++++++ examples/SharedMemory/PhysicsClientC_API.h | 1 + .../PhysicsServerCommandProcessor.cpp | 5 +++++ examples/SharedMemory/SharedMemoryCommands.h | 6 ++++-- .../TinyRendererVisualShapeConverter.cpp | 19 +++++++++++++++++-- .../TinyRendererVisualShapeConverter.h | 1 + examples/TinyRenderer/TinyRenderer.cpp | 11 +++++++++-- examples/TinyRenderer/TinyRenderer.h | 1 + 8 files changed, 49 insertions(+), 6 deletions(-) diff --git a/examples/SharedMemory/PhysicsClientC_API.cpp b/examples/SharedMemory/PhysicsClientC_API.cpp index d0b0413f1..1edfc5edf 100644 --- a/examples/SharedMemory/PhysicsClientC_API.cpp +++ b/examples/SharedMemory/PhysicsClientC_API.cpp @@ -1244,6 +1244,17 @@ void b3RequestCameraImageSetLightDirection(b3SharedMemoryCommandHandle commandHa command->m_updateFlags |= REQUEST_PIXEL_ARGS_SET_LIGHT_DIRECTION; } +void b3RequestCameraImageSetLightColor(b3SharedMemoryCommandHandle commandHandle, const float lightColor[3]) +{ + struct SharedMemoryCommand* command = (struct SharedMemoryCommand*) commandHandle; + b3Assert(command); + b3Assert(command->m_type == CMD_REQUEST_CAMERA_IMAGE_DATA); + for (int i = 0; i<3; i++) + { + command->m_requestPixelDataArguments.m_lightColor[i] = lightColor[i]; + } + command->m_updateFlags |= REQUEST_PIXEL_ARGS_SET_LIGHT_COLOR; +} void b3ComputeViewMatrixFromPositions(const float cameraPosition[3], const float cameraTargetPosition[3], const float cameraUp[3], float viewMatrix[16]) { diff --git a/examples/SharedMemory/PhysicsClientC_API.h b/examples/SharedMemory/PhysicsClientC_API.h index 529e50e94..023962c2a 100644 --- a/examples/SharedMemory/PhysicsClientC_API.h +++ b/examples/SharedMemory/PhysicsClientC_API.h @@ -97,6 +97,7 @@ b3SharedMemoryCommandHandle b3InitRequestCameraImage(b3PhysicsClientHandle physC void b3RequestCameraImageSetCameraMatrices(b3SharedMemoryCommandHandle command, float viewMatrix[16], float projectionMatrix[16]); void b3RequestCameraImageSetPixelResolution(b3SharedMemoryCommandHandle command, int width, int height ); void b3RequestCameraImageSetLightDirection(b3SharedMemoryCommandHandle commandHandle, const float lightDirection[3]); +void b3RequestCameraImageSetLightColor(b3SharedMemoryCommandHandle commandHandle, const float lightColor[3]); void b3RequestCameraImageSelectRenderer(b3SharedMemoryCommandHandle commandHandle, int renderer); void b3GetCameraImageData(b3PhysicsClientHandle physClient, struct b3CameraImageData* imageData); diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index ee7d06624..373f11989 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -1420,6 +1420,11 @@ bool PhysicsServerCommandProcessor::processCommand(const struct SharedMemoryComm { m_data->m_visualConverter.setLightDirection(clientCmd.m_requestPixelDataArguments.m_lightDirection[0], clientCmd.m_requestPixelDataArguments.m_lightDirection[1], clientCmd.m_requestPixelDataArguments.m_lightDirection[2]); } + + if ((clientCmd.m_updateFlags & REQUEST_PIXEL_ARGS_SET_LIGHT_COLOR) != 0) + { + m_data->m_visualConverter.setLightColor(clientCmd.m_requestPixelDataArguments.m_lightColor[0], clientCmd.m_requestPixelDataArguments.m_lightColor[1], clientCmd.m_requestPixelDataArguments.m_lightColor[2]); + } if ((clientCmd.m_updateFlags & REQUEST_PIXEL_ARGS_HAS_CAMERA_MATRICES)!=0) { diff --git a/examples/SharedMemory/SharedMemoryCommands.h b/examples/SharedMemory/SharedMemoryCommands.h index 40b62f518..07c40d90c 100644 --- a/examples/SharedMemory/SharedMemoryCommands.h +++ b/examples/SharedMemory/SharedMemoryCommands.h @@ -139,13 +139,15 @@ struct RequestPixelDataArgs int m_pixelWidth; int m_pixelHeight; float m_lightDirection[3]; + float m_lightColor[3]; }; enum EnumRequestPixelDataUpdateFlags { REQUEST_PIXEL_ARGS_HAS_CAMERA_MATRICES=1, - REQUEST_PIXEL_ARGS_SET_PIXEL_WIDTH_HEIGHT=4, - REQUEST_PIXEL_ARGS_SET_LIGHT_DIRECTION=8, + REQUEST_PIXEL_ARGS_SET_PIXEL_WIDTH_HEIGHT=2, + REQUEST_PIXEL_ARGS_SET_LIGHT_DIRECTION=4, + REQUEST_PIXEL_ARGS_SET_LIGHT_COLOR=8, //don't exceed (1<<15), because this enum is shared with EnumRenderer in SharedMemoryPublic.h }; diff --git a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp index 676ca65d3..34478bee7 100644 --- a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp +++ b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp @@ -75,6 +75,8 @@ struct TinyRendererVisualShapeConverterInternalData b3AlignedObjectArray m_segmentationMaskBuffer; btVector3 m_lightDirection; bool m_hasLightDirection; + btVector3 m_lightColor; + bool m_hasLightColor; SimpleCamera m_camera; @@ -83,7 +85,8 @@ struct TinyRendererVisualShapeConverterInternalData m_swWidth(START_WIDTH), m_swHeight(START_HEIGHT), m_rgbColorBuffer(START_WIDTH,START_HEIGHT,TGAImage::RGB), - m_hasLightDirection(false) + m_hasLightDirection(false), + m_hasLightColor(false) { m_depthBuffer.resize(m_swWidth*m_swHeight); m_segmentationMaskBuffer.resize(m_swWidth*m_swHeight,-1); @@ -117,6 +120,11 @@ void TinyRendererVisualShapeConverter::setLightDirection(float x, float y, float m_data->m_hasLightDirection = true; } +void TinyRendererVisualShapeConverter::setLightColor(float x, float y, float z) +{ + m_data->m_lightColor.setValue(x, y, z); + m_data->m_hasLightColor = true; +} void convertURDFToVisualShape(const UrdfVisual* visual, const char* urdfPathPrefix, const btTransform& visualTransform, btAlignedObjectArray& verticesOut, btAlignedObjectArray& indicesOut, btAlignedObjectArray& texturesOut, b3VisualShapeData& visualShapeOut) { @@ -704,6 +712,12 @@ void TinyRendererVisualShapeConverter::render(const float viewMat[16], const flo lightDirWorld.normalize(); + btVector3 lightColor(1.0,1.0,1.0); + if (m_data->m_hasLightColor) + { + lightColor = m_data->m_lightColor; + } + // printf("num m_swRenderInstances = %d\n", m_data->m_swRenderInstances.size()); for (int i=0;im_swRenderInstances.size();i++) { @@ -737,6 +751,7 @@ void TinyRendererVisualShapeConverter::render(const float viewMat[16], const flo renderObj->m_viewMatrix[i][j] = viewMat[i+4*j]; renderObj->m_localScaling = colObj->getCollisionShape()->getLocalScaling(); renderObj->m_lightDirWorld = lightDirWorld; + renderObj->m_lightColor = lightColor; } } TinyRenderer::renderObject(*renderObj); @@ -900,4 +915,4 @@ int TinyRendererVisualShapeConverter::loadTextureFile(const char* filename) return registerTexture(image, width, height); } return -1; -} \ No newline at end of file +} diff --git a/examples/SharedMemory/TinyRendererVisualShapeConverter.h b/examples/SharedMemory/TinyRendererVisualShapeConverter.h index 1a4a02f76..b2ed166e6 100644 --- a/examples/SharedMemory/TinyRendererVisualShapeConverter.h +++ b/examples/SharedMemory/TinyRendererVisualShapeConverter.h @@ -33,6 +33,7 @@ struct TinyRendererVisualShapeConverter : public LinkVisualShapesConverter void getWidthAndHeight(int& width, int& height); void setWidthAndHeight(int width, int height); void setLightDirection(float x, float y, float z); + void setLightColor(float x, float y, float z); void copyCameraImageData(unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels,int* segmentationMaskBuffer, int segmentationMaskSizeInPixels, int startPixelIndex, int* widthPtr, int* heightPtr, int* numPixelsCopied); diff --git a/examples/TinyRenderer/TinyRenderer.cpp b/examples/TinyRenderer/TinyRenderer.cpp index 0a5b7c861..76b9e4053 100644 --- a/examples/TinyRenderer/TinyRenderer.cpp +++ b/examples/TinyRenderer/TinyRenderer.cpp @@ -19,6 +19,7 @@ struct Shader : public IShader { Model* m_model; Vec3f m_light_dir_local; + Vec3f m_light_color; Matrix& m_modelMat; Matrix m_invModelMat; @@ -32,9 +33,10 @@ struct Shader : public IShader { mat<3,3,float> varying_nrm; // normal per vertex to be interpolated by FS //mat<3,3,float> ndc_tri; // triangle in normalized device coordinates - Shader(Model* model, Vec3f light_dir_local, Matrix& modelView, Matrix& projectionMatrix, Matrix& modelMat, Vec3f localScaling, const Vec4f& colorRGBA) + Shader(Model* model, Vec3f light_dir_local, Vec3f light_color, Matrix& modelView, Matrix& projectionMatrix, Matrix& modelMat, Vec3f localScaling, const Vec4f& colorRGBA) :m_model(model), m_light_dir_local(light_dir_local), + m_light_color(light_color), m_modelView1(modelView), m_projectionMatrix(projectionMatrix), m_modelMat(modelMat), @@ -83,6 +85,10 @@ struct Shader : public IShader { color.bgra[1] *= m_colorRGBA[1]; color.bgra[2] *= m_colorRGBA[2]; color.bgra[3] *= m_colorRGBA[3]; + + color.bgra[0] *= m_light_color[0]; + color.bgra[1] *= m_light_color[1]; + color.bgra[2] *= m_light_color[2]; return false; } @@ -260,6 +266,7 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData) int height = renderData.m_rgbColorBuffer.get_height(); Vec3f light_dir_local = Vec3f(renderData.m_lightDirWorld[0],renderData.m_lightDirWorld[1],renderData.m_lightDirWorld[2]); + Vec3f light_color = Vec3f(renderData.m_lightColor[0],renderData.m_lightColor[1],renderData.m_lightColor[2]); Model* model = renderData.m_model; if (0==model) return; @@ -278,7 +285,7 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData) { Matrix modelViewMatrix = renderData.m_viewMatrix*renderData.m_modelMatrix; Vec3f localScaling(renderData.m_localScaling[0],renderData.m_localScaling[1],renderData.m_localScaling[2]); - Shader shader(model, light_dir_local, modelViewMatrix, renderData.m_projectionMatrix,renderData.m_modelMatrix, localScaling, model->getColorRGBA()); + Shader shader(model, light_dir_local, light_color, modelViewMatrix, renderData.m_projectionMatrix,renderData.m_modelMatrix, localScaling, model->getColorRGBA()); //printf("Render %d triangles.\n",model->nfaces()); for (int i=0; infaces(); i++) diff --git a/examples/TinyRenderer/TinyRenderer.h b/examples/TinyRenderer/TinyRenderer.h index 1819790a6..fcb8298bd 100644 --- a/examples/TinyRenderer/TinyRenderer.h +++ b/examples/TinyRenderer/TinyRenderer.h @@ -18,6 +18,7 @@ struct TinyRenderObjectData Matrix m_viewportMatrix; btVector3 m_localScaling; btVector3 m_lightDirWorld; + btVector3 m_lightColor; //Model (vertices, indices, textures, shader) Matrix m_modelMatrix; From be5b8a3d7b5536920164f54d40e7c07423498341 Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Sun, 20 Nov 2016 13:14:18 -0800 Subject: [PATCH 2/2] Set light color in pybullet. --- examples/pybullet/pybullet.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/pybullet/pybullet.c b/examples/pybullet/pybullet.c index 17e782b8b..1e3c10a38 100644 --- a/examples/pybullet/pybullet.c +++ b/examples/pybullet/pybullet.c @@ -2209,17 +2209,18 @@ static PyObject* pybullet_getContactPointData(PyObject* self, PyObject* args, Py /// Render an image from the current timestep of the simulation, width, height are required, other args are optional -// getCameraImage(w, h, view[16], projection[16], lightpos[3]) +// getCameraImage(w, h, view[16], projection[16], lightDir[3], lightColor[3]) static PyObject* pybullet_getCameraImage(PyObject* self, PyObject* args, PyObject *keywds) { /// request an image from a simulated camera, using a software renderer. struct b3CameraImageData imageData; - PyObject* objViewMat = 0, *objProjMat = 0, *lightDirObj = 0; + PyObject* objViewMat = 0, *objProjMat = 0, *lightDirObj = 0, *lightColorObj = 0; int width, height; int size = PySequence_Size(args); float viewMatrix[16]; float projectionMatrix[16]; float lightDir[3]; + float lightColor[3]; // inialize cmd b3SharedMemoryCommandHandle command; @@ -2232,9 +2233,9 @@ static PyObject* pybullet_getCameraImage(PyObject* self, PyObject* args, PyObjec command = b3InitRequestCameraImage(sm); // set camera resolution, optionally view, projection matrix, light direction - static char *kwlist[] = { "width", "height", "viewMatrix", "projectionMatrix", "lightDirection",NULL }; + static char *kwlist[] = { "width", "height", "viewMatrix", "projectionMatrix", "lightDirection", "lightColor", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, keywds, "ii|OOO", kwlist, &width, &height, &objViewMat, &objProjMat, &lightDirObj)) + if (!PyArg_ParseTupleAndKeywords(args, keywds, "ii|OOOO", kwlist, &width, &height, &objViewMat, &objProjMat, &lightDirObj, &lightColorObj)) { return NULL; } @@ -2245,11 +2246,16 @@ static PyObject* pybullet_getCameraImage(PyObject* self, PyObject* args, PyObjec { b3RequestCameraImageSetCameraMatrices(command, viewMatrix, projectionMatrix); } - //set light pos only if function succeeds + //set light direction only if function succeeds if (pybullet_internalSetVector(lightDirObj, lightDir)) { b3RequestCameraImageSetLightDirection(command, lightDir); } + //set light color only if function succeeds + if (pybullet_internalSetVector(lightColorObj, lightColor)) + { + b3RequestCameraImageSetLightColor(command, lightColor); + } if (b3CanSubmitCommand(sm))