From 1fbd78ece55dc50664125905f606db4d77f0ea87 Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Tue, 29 Nov 2016 13:19:35 -0800 Subject: [PATCH] Add shared memory API for setting shadow and light source distance. --- examples/SharedMemory/PhysicsClientC_API.cpp | 18 +++++++++++++ examples/SharedMemory/PhysicsClientC_API.h | 2 ++ .../SharedMemory/PhysicsClientExample.cpp | 27 +++++++++++++++---- .../PhysicsServerCommandProcessor.cpp | 10 +++++++ examples/SharedMemory/SharedMemoryCommands.h | 4 +++ examples/SharedMemory/SharedMemoryPublic.h | 1 + .../TinyRendererVisualShapeConverter.cpp | 2 +- 7 files changed, 58 insertions(+), 6 deletions(-) diff --git a/examples/SharedMemory/PhysicsClientC_API.cpp b/examples/SharedMemory/PhysicsClientC_API.cpp index 1edfc5edf..5cabfc362 100644 --- a/examples/SharedMemory/PhysicsClientC_API.cpp +++ b/examples/SharedMemory/PhysicsClientC_API.cpp @@ -1256,6 +1256,24 @@ void b3RequestCameraImageSetLightColor(b3SharedMemoryCommandHandle commandHandle command->m_updateFlags |= REQUEST_PIXEL_ARGS_SET_LIGHT_COLOR; } +void b3RequestCameraImageSetLightDistance(b3SharedMemoryCommandHandle commandHandle, float lightDistance) +{ + struct SharedMemoryCommand* command = (struct SharedMemoryCommand*) commandHandle; + b3Assert(command); + b3Assert(command->m_type == CMD_REQUEST_CAMERA_IMAGE_DATA); + command->m_requestPixelDataArguments.m_lightDistance = lightDistance; + command->m_updateFlags |= REQUEST_PIXEL_ARGS_SET_LIGHT_DISTANCE; +} + +void b3RequestCameraImageSetShadow(b3SharedMemoryCommandHandle commandHandle, bool hasShadow) +{ + struct SharedMemoryCommand* command = (struct SharedMemoryCommand*) commandHandle; + b3Assert(command); + b3Assert(command->m_type == CMD_REQUEST_CAMERA_IMAGE_DATA); + command->m_requestPixelDataArguments.m_hasShadow = hasShadow; + command->m_updateFlags |= REQUEST_PIXEL_ARGS_SET_SHADOW; +} + void b3ComputeViewMatrixFromPositions(const float cameraPosition[3], const float cameraTargetPosition[3], const float cameraUp[3], float viewMatrix[16]) { b3Vector3 eye = b3MakeVector3(cameraPosition[0], cameraPosition[1], cameraPosition[2]); diff --git a/examples/SharedMemory/PhysicsClientC_API.h b/examples/SharedMemory/PhysicsClientC_API.h index 023962c2a..a28a6d09f 100644 --- a/examples/SharedMemory/PhysicsClientC_API.h +++ b/examples/SharedMemory/PhysicsClientC_API.h @@ -98,6 +98,8 @@ void b3RequestCameraImageSetCameraMatrices(b3SharedMemoryCommandHandle command, 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 b3RequestCameraImageSetLightDistance(b3SharedMemoryCommandHandle commandHandle, float lightDistance); +void b3RequestCameraImageSetShadow(b3SharedMemoryCommandHandle commandHandle, bool hasShadow); void b3RequestCameraImageSelectRenderer(b3SharedMemoryCommandHandle commandHandle, int renderer); void b3GetCameraImageData(b3PhysicsClientHandle physClient, struct b3CameraImageData* imageData); diff --git a/examples/SharedMemory/PhysicsClientExample.cpp b/examples/SharedMemory/PhysicsClientExample.cpp index a6aa6aca3..98c59b2d5 100644 --- a/examples/SharedMemory/PhysicsClientExample.cpp +++ b/examples/SharedMemory/PhysicsClientExample.cpp @@ -89,10 +89,10 @@ protected: virtual void resetCamera() { - float dist = 1.233281; - float pitch = 193; - float yaw = 25; - float targetPos[3]={0.103042,-0.469102,0.631005};//-3,2.8,-2.5}; + float dist = 4; + float pitch = 193; + float yaw = 25; + float targetPos[3]={0,0,0.5};//-3,2.8,-2.5}; m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]); } @@ -242,7 +242,7 @@ void PhysicsClientExample::prepareAndSubmitCommand(int commandId) { case CMD_LOAD_URDF: { - b3SharedMemoryCommandHandle commandHandle = b3LoadUrdfCommandInit(m_physicsClientHandle, "torus/torus_with_separate_plane.urdf"); + b3SharedMemoryCommandHandle commandHandle = b3LoadUrdfCommandInit(m_physicsClientHandle, "kuka_iiwa/model.urdf"); //setting the initial position, orientation and other arguments are optional double startPosX = 0; static double startPosY = 0; @@ -481,6 +481,22 @@ void PhysicsClientExample::prepareAndSubmitCommand(int commandId) } break; } + case CMD_SET_SHADOW: + { + b3SharedMemoryCommandHandle commandHandle = b3InitRequestCameraImage(m_physicsClientHandle); + float viewMatrix[16]; + float projectionMatrix[16]; + m_guiHelper->getRenderInterface()->getActiveCamera()->getCameraProjectionMatrix(projectionMatrix); + m_guiHelper->getRenderInterface()->getActiveCamera()->getCameraViewMatrix(viewMatrix); + + b3RequestCameraImageSetCameraMatrices(commandHandle, viewMatrix,projectionMatrix); + b3RequestCameraImageSetPixelResolution(commandHandle, camVisualizerWidth,camVisualizerHeight); + bool hasShadow = true; + b3RequestCameraImageSetShadow(commandHandle, hasShadow); + b3SubmitClientCommand(m_physicsClientHandle, commandHandle); + break; + } + default: { b3Error("Unknown buttonId"); @@ -556,6 +572,7 @@ void PhysicsClientExample::createButtons() createButton("Load URDF",CMD_LOAD_URDF, isTrigger); createButton("Load SDF",CMD_LOAD_SDF, isTrigger); createButton("Save World",CMD_SAVE_WORLD, isTrigger); + createButton("Set Shadow",CMD_SET_SHADOW, isTrigger); createButton("Get Camera Image",CMD_REQUEST_CAMERA_IMAGE_DATA,isTrigger); createButton("Step Sim",CMD_STEP_FORWARD_SIMULATION, isTrigger); createButton("Realtime Sim",CMD_CUSTOM_SET_REALTIME_SIMULATION, isTrigger); diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 373f11989..68cb5ce8b 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -1425,6 +1425,16 @@ bool PhysicsServerCommandProcessor::processCommand(const struct SharedMemoryComm { 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_SET_LIGHT_DISTANCE) != 0) + { + m_data->m_visualConverter.setLightDistance(clientCmd.m_requestPixelDataArguments.m_lightDistance); + } + + if ((clientCmd.m_updateFlags & REQUEST_PIXEL_ARGS_SET_SHADOW) != 0) + { + m_data->m_visualConverter.setShadow(clientCmd.m_requestPixelDataArguments.m_hasShadow); + } if ((clientCmd.m_updateFlags & REQUEST_PIXEL_ARGS_HAS_CAMERA_MATRICES)!=0) { diff --git a/examples/SharedMemory/SharedMemoryCommands.h b/examples/SharedMemory/SharedMemoryCommands.h index 07c40d90c..7f9f4771b 100644 --- a/examples/SharedMemory/SharedMemoryCommands.h +++ b/examples/SharedMemory/SharedMemoryCommands.h @@ -140,6 +140,8 @@ struct RequestPixelDataArgs int m_pixelHeight; float m_lightDirection[3]; float m_lightColor[3]; + float m_lightDistance; + bool m_hasShadow; }; enum EnumRequestPixelDataUpdateFlags @@ -148,6 +150,8 @@ enum EnumRequestPixelDataUpdateFlags REQUEST_PIXEL_ARGS_SET_PIXEL_WIDTH_HEIGHT=2, REQUEST_PIXEL_ARGS_SET_LIGHT_DIRECTION=4, REQUEST_PIXEL_ARGS_SET_LIGHT_COLOR=8, + REQUEST_PIXEL_ARGS_SET_LIGHT_DISTANCE=16, + REQUEST_PIXEL_ARGS_SET_SHADOW=32, //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 d6a8e246a..c92d30141 100644 --- a/examples/SharedMemory/SharedMemoryPublic.h +++ b/examples/SharedMemory/SharedMemoryPublic.h @@ -40,6 +40,7 @@ enum EnumSharedMemoryClientCommand CMD_REQUEST_VISUAL_SHAPE_INFO, CMD_UPDATE_VISUAL_SHAPE, CMD_LOAD_TEXTURE, + CMD_SET_SHADOW, CMD_USER_DEBUG_DRAW, //don't go beyond this command! diff --git a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp index a36dc4b1f..dc5d7a561 100644 --- a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp +++ b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp @@ -90,7 +90,7 @@ struct TinyRendererVisualShapeConverterInternalData m_rgbColorBuffer(START_WIDTH,START_HEIGHT,TGAImage::RGB), m_hasLightDirection(false), m_hasLightColor(false), - m_hasShadow(true) + m_hasShadow(false) { m_depthBuffer.resize(m_swWidth*m_swHeight); m_shadowBuffer.resize(m_swWidth*m_swHeight);