diff --git a/examples/RenderingExamples/TinyRendererSetup.cpp b/examples/RenderingExamples/TinyRendererSetup.cpp index 11258779d..8df1a76ad 100644 --- a/examples/RenderingExamples/TinyRendererSetup.cpp +++ b/examples/RenderingExamples/TinyRendererSetup.cpp @@ -183,7 +183,7 @@ TinyRendererSetup::TinyRendererSetup(struct GUIHelperInterface* gui) m_guiHelper->getRenderInterface()->writeTransforms(); m_internalData->m_shapePtr.push_back(0); - TinyRenderObjectData* ob = new TinyRenderObjectData(m_internalData->m_width,m_internalData->m_height, + TinyRenderObjectData* ob = new TinyRenderObjectData( m_internalData->m_rgbColorBuffer, m_internalData->m_depthBuffer); //ob->loadModel("cube.obj"); @@ -363,7 +363,7 @@ void TinyRendererSetup::stepSimulation(float deltaTime) } } - TinyRenderer::renderObject(*m_internalData->m_renderObjects[o]); + TinyRenderer::renderObject(*m_internalData->m_renderObjects[o], m_internalData->m_width,m_internalData->m_height); } //m_app->drawText("hello",500,500); render->activateTexture(m_internalData->m_textureHandle); diff --git a/examples/SharedMemory/PhysicsClientC_API.cpp b/examples/SharedMemory/PhysicsClientC_API.cpp index 914c9d091..c6468cb89 100644 --- a/examples/SharedMemory/PhysicsClientC_API.cpp +++ b/examples/SharedMemory/PhysicsClientC_API.cpp @@ -735,6 +735,16 @@ void b3RequestCameraImageSetCameraMatrices(b3SharedMemoryCommandHandle commandHa command->m_updateFlags |= REQUEST_PIXEL_ARGS_HAS_CAMERA_MATRICES; } +void b3RequestCameraImageSetPixelResolution(b3SharedMemoryCommandHandle commandHandle, int width, int height ) +{ + struct SharedMemoryCommand* command = (struct SharedMemoryCommand*) commandHandle; + b3Assert(command); + b3Assert(command->m_type == CMD_REQUEST_CAMERA_IMAGE_DATA); + command->m_requestPixelDataArguments.m_pixelWidth = width; + command->m_requestPixelDataArguments.m_pixelHeight = height; + command->m_updateFlags |= REQUEST_PIXEL_ARGS_SET_PIXEL_WIDTH_HEIGHT; +} + void b3GetCameraImageData(b3PhysicsClientHandle physClient, struct b3CameraImageData* imageData) { PhysicsClient* cl = (PhysicsClient* ) physClient; diff --git a/examples/SharedMemory/PhysicsClientC_API.h b/examples/SharedMemory/PhysicsClientC_API.h index 37008aceb..b67f078ae 100644 --- a/examples/SharedMemory/PhysicsClientC_API.h +++ b/examples/SharedMemory/PhysicsClientC_API.h @@ -68,6 +68,7 @@ void b3GetDebugLines(b3PhysicsClientHandle physClient, struct b3DebugLines* l ///request an image from a simulated camera, using a software renderer. b3SharedMemoryCommandHandle b3InitRequestCameraImage(b3PhysicsClientHandle physClient); void b3RequestCameraImageSetCameraMatrices(b3SharedMemoryCommandHandle command, float viewMatrix[16], float projectionMatrix[16]); +void b3RequestCameraImageSetPixelResolution(b3SharedMemoryCommandHandle command, int width, int height ); void b3GetCameraImageData(b3PhysicsClientHandle physClient, struct b3CameraImageData* imageData); diff --git a/examples/SharedMemory/PhysicsClientExample.cpp b/examples/SharedMemory/PhysicsClientExample.cpp index 30da039b3..e7e02ba51 100644 --- a/examples/SharedMemory/PhysicsClientExample.cpp +++ b/examples/SharedMemory/PhysicsClientExample.cpp @@ -21,8 +21,8 @@ struct MyMotorInfo2 int m_qIndex; }; -int camVisualizerWidth = 640;//1024/3; -int camVisualizerHeight = 480;//768/3; +int camVisualizerWidth = 320;//1024/3; +int camVisualizerHeight = 240;//768/3; #define MAX_NUM_MOTORS 128 @@ -77,10 +77,10 @@ protected: virtual void resetCamera() { - float dist = 5; + float dist = 1.1; float pitch = 50; float yaw = 35; - float targetPos[3]={0,0,0};//-3,2.8,-2.5}; + float targetPos[3]={0,0,0.5};//-3,2.8,-2.5}; m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]); } @@ -248,7 +248,8 @@ void PhysicsClientExample::prepareAndSubmitCommand(int commandId) this->m_guiHelper->getRenderInterface()->getActiveCamera()->getCameraProjectionMatrix(projectionMatrix); this->m_guiHelper->getRenderInterface()->getActiveCamera()->getCameraViewMatrix(viewMatrix); b3RequestCameraImageSetCameraMatrices(commandHandle, viewMatrix,projectionMatrix); - b3SubmitClientCommand(m_physicsClientHandle, commandHandle); + b3RequestCameraImageSetPixelResolution(commandHandle, camVisualizerWidth,camVisualizerHeight); + b3SubmitClientCommand(m_physicsClientHandle, commandHandle); break; } case CMD_CREATE_BOX_COLLISION_SHAPE: @@ -619,18 +620,18 @@ void PhysicsClientExample::stepSimulation(float deltaTime) b3GetCameraImageData(m_physicsClientHandle,&imageData); if (m_canvas && m_canvasIndex >=0) { - for (int i=0;isetPixel(m_canvasIndex,xIndex,camVisualizerHeight-1-yIndex, + int pixelIndex = (xIndex+yIndex*imageData.m_pixelWidth)*bytesPerPixel; + m_canvas->setPixel(m_canvasIndex,i,camVisualizerHeight-1-j, imageData.m_rgbColorData[pixelIndex], imageData.m_rgbColorData[pixelIndex+1], diff --git a/examples/SharedMemory/PhysicsClientSharedMemory.cpp b/examples/SharedMemory/PhysicsClientSharedMemory.cpp index 82062b0c8..728358b93 100644 --- a/examples/SharedMemory/PhysicsClientSharedMemory.cpp +++ b/examples/SharedMemory/PhysicsClientSharedMemory.cpp @@ -450,6 +450,13 @@ const SharedMemoryStatus* PhysicsClientSharedMemory::processServerStatus() { (unsigned char*)&m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0]; // printf("pixel = %d\n", rgbaPixelsReceived[0]); + float* depthBuffer = (float*)&(m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[serverCmd.m_sendPixelDataArguments.m_numPixelsCopied*4]); + + for (int i=0;im_cachedCameraDepthBuffer[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex] = depthBuffer[i]; + } + for (int i=0;im_cachedCameraPixelsRGBA[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex*numBytesPerPixel] diff --git a/examples/SharedMemory/PhysicsDirect.cpp b/examples/SharedMemory/PhysicsDirect.cpp index 37baeaa83..478e31031 100644 --- a/examples/SharedMemory/PhysicsDirect.cpp +++ b/examples/SharedMemory/PhysicsDirect.cpp @@ -210,8 +210,15 @@ bool PhysicsDirect::processCamera(const struct SharedMemoryCommand& orgCommand) unsigned char* rgbaPixelsReceived = (unsigned char*)&m_data->m_bulletStreamDataServerToClient[0]; - printf("pixel = %d\n", rgbaPixelsReceived[0]); + + float* depthBuffer = (float*)&(m_data->m_bulletStreamDataServerToClient[serverCmd.m_sendPixelDataArguments.m_numPixelsCopied*4]); + + // printf("pixel = %d\n", rgbaPixelsReceived[0]); + for (int i=0;im_cachedCameraDepthBuffer[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex] = depthBuffer[i]; + } for (int i=0;im_cachedCameraPixelsRGBA[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex*numBytesPerPixel] @@ -227,6 +234,7 @@ bool PhysicsDirect::processCamera(const struct SharedMemoryCommand& orgCommand) command.m_requestPixelDataArguments.m_startPixelIndex = serverCmd.m_sendPixelDataArguments.m_startingPixelIndex + serverCmd.m_sendPixelDataArguments.m_numPixelsCopied; + } else { m_data->m_cachedCameraPixelsWidth = serverCmd.m_sendPixelDataArguments.m_imageWidth; diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 2f662aa4a..89490caa6 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -972,6 +972,14 @@ bool PhysicsServerCommandProcessor::processCommand(const struct SharedMemoryComm int width, height; int numPixelsCopied = 0; + if ( + (clientCmd.m_requestPixelDataArguments.m_startPixelIndex==0) && + (clientCmd.m_updateFlags & REQUEST_PIXEL_ARGS_SET_PIXEL_WIDTH_HEIGHT)!=0) + { + m_data->m_visualConverter.setWidthAndHeight(clientCmd.m_requestPixelDataArguments.m_pixelWidth, + clientCmd.m_requestPixelDataArguments.m_pixelHeight); + } + if ((clientCmd.m_updateFlags & REQUEST_PIXEL_ARGS_USE_HARDWARE_OPENGL)!=0) { m_data->m_guiHelper->copyCameraImageData(0,0,0,0,0,&width,&height,0); @@ -1005,6 +1013,7 @@ bool PhysicsServerCommandProcessor::processCommand(const struct SharedMemoryComm { // printf("-------------------------------\nRendering\n"); + if ((clientCmd.m_updateFlags & REQUEST_PIXEL_ARGS_HAS_CAMERA_MATRICES)!=0) { m_data->m_visualConverter.render( diff --git a/examples/SharedMemory/SharedMemoryCommands.h b/examples/SharedMemory/SharedMemoryCommands.h index 8328b485a..fa67d7673 100644 --- a/examples/SharedMemory/SharedMemoryCommands.h +++ b/examples/SharedMemory/SharedMemoryCommands.h @@ -129,12 +129,16 @@ struct RequestPixelDataArgs float m_viewMatrix[16]; float m_projectionMatrix[16]; int m_startPixelIndex; + int m_pixelWidth; + int m_pixelHeight; }; enum EnumRequestPixelDataUpdateFlags { REQUEST_PIXEL_ARGS_HAS_CAMERA_MATRICES=1, REQUEST_PIXEL_ARGS_USE_HARDWARE_OPENGL=2, + REQUEST_PIXEL_ARGS_SET_PIXEL_WIDTH_HEIGHT=4, + }; diff --git a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp index fed633c1b..7c3507b25 100644 --- a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp +++ b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp @@ -462,7 +462,7 @@ void TinyRendererVisualShapeConverter::convertVisualShapes(int linkIndex, const if (vertices.size() && indices.size()) { - TinyRenderObjectData* tinyObj = new TinyRenderObjectData(m_data->m_swWidth,m_data->m_swHeight,m_data->m_rgbColorBuffer,m_data->m_depthBuffer); + TinyRenderObjectData* tinyObj = new TinyRenderObjectData(m_data->m_rgbColorBuffer,m_data->m_depthBuffer); tinyObj->registerMeshShape(&vertices[0].xyzw[0],vertices.size(),&indices[0],indices.size(),rgbaColor); visuals->m_renderObjects.push_back(tinyObj); } @@ -576,7 +576,7 @@ void TinyRendererVisualShapeConverter::render(const float viewMat[16], const flo renderObj->m_lightDirWorld = lightDirWorld; } } - TinyRenderer::renderObject(*renderObj); + TinyRenderer::renderObject(*renderObj,m_data->m_swWidth,m_data->m_swHeight); } } //printf("write tga \n"); @@ -590,6 +590,17 @@ void TinyRendererVisualShapeConverter::getWidthAndHeight(int& width, int& height height = m_data->m_swHeight; } + +void TinyRendererVisualShapeConverter::setWidthAndHeight(int width, int height) +{ + m_data->m_swWidth = width; + m_data->m_swHeight = height; + + m_data->m_depthBuffer.resize(m_data->m_swWidth*m_data->m_swHeight); + m_data->m_rgbColorBuffer = TGAImage(width, height, TGAImage::RGB); + +} + void TinyRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int* widthPtr, int* heightPtr, int* numPixelsCopied) { int w = m_data->m_rgbColorBuffer.get_width(); @@ -612,6 +623,10 @@ void TinyRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixels { for (int i=0;im_depthBuffer[i+startPixelIndex]; + } if (pixelsRGBA) { pixelsRGBA[i*numBytesPerPixel] = m_data->m_rgbColorBuffer.buffer()[(i+startPixelIndex)*3+0]; diff --git a/examples/SharedMemory/TinyRendererVisualShapeConverter.h b/examples/SharedMemory/TinyRendererVisualShapeConverter.h index 3bce1add7..6baff2491 100644 --- a/examples/SharedMemory/TinyRendererVisualShapeConverter.h +++ b/examples/SharedMemory/TinyRendererVisualShapeConverter.h @@ -24,6 +24,7 @@ struct TinyRendererVisualShapeConverter : public LinkVisualShapesConverter void resetAll(); void getWidthAndHeight(int& width, int& height); + void setWidthAndHeight(int width, int height); void copyCameraImageData(unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, float* depthBuffer, int depthBufferSizeInPixels, int startPixelIndex, int* widthPtr, int* heightPtr, int* numPixelsCopied); diff --git a/examples/TinyRenderer/TinyRenderer.cpp b/examples/TinyRenderer/TinyRenderer.cpp index 6b9fdbb35..e13f97077 100644 --- a/examples/TinyRenderer/TinyRenderer.cpp +++ b/examples/TinyRenderer/TinyRenderer.cpp @@ -88,10 +88,8 @@ struct Shader : public IShader { }; -TinyRenderObjectData::TinyRenderObjectData(int width, int height,TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer) -:m_width(width), -m_height(height), -m_rgbColorBuffer(rgbColorBuffer), +TinyRenderObjectData::TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer) +:m_rgbColorBuffer(rgbColorBuffer), m_depthBuffer(depthBuffer), m_model(0), m_userData(0), @@ -103,11 +101,6 @@ m_userIndex(-1) m_lightDirWorld.setValue(0,0,0); m_localScaling.setValue(1,1,1); m_modelMatrix = Matrix::identity(); - m_viewMatrix = lookat(eye, center, up); - //m_viewportMatrix = viewport(width/8, height/8, width*3/4, height*3/4); - //m_viewportMatrix = viewport(width/8, height/8, width*3/4, height*3/4); - m_viewportMatrix = viewport(0,0,width,height); - m_projectionMatrix = projection(-1.f/(eye-center).norm()); } @@ -238,7 +231,7 @@ TinyRenderObjectData::~TinyRenderObjectData() delete m_model; } -void TinyRenderer::renderObject(TinyRenderObjectData& renderData) +void TinyRenderer::renderObject(TinyRenderObjectData& renderData, int width, int height) { Vec3f light_dir_local = Vec3f(renderData.m_lightDirWorld[0],renderData.m_lightDirWorld[1],renderData.m_lightDirWorld[2]); Model* model = renderData.m_model; @@ -247,13 +240,8 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData) - //renderData.m_viewMatrix = lookat(eye, center, up); - int width = renderData.m_width; - int height = renderData.m_height; - //renderData.m_viewportMatrix = viewport(width/8, height/8, width*3/4, height*3/4); - renderData.m_viewportMatrix = viewport(0,0,renderData.m_width,renderData.m_height); - //renderData.m_projectionMatrix = projection(-1.f/(eye-center).norm()); - + renderData.m_viewportMatrix = viewport(0,0,width, height); + b3AlignedObjectArray& zbuffer = renderData.m_depthBuffer; TGAImage& frame = renderData.m_rgbColorBuffer; diff --git a/examples/TinyRenderer/TinyRenderer.h b/examples/TinyRenderer/TinyRenderer.h index 0cb2ffca9..cfe857eca 100644 --- a/examples/TinyRenderer/TinyRenderer.h +++ b/examples/TinyRenderer/TinyRenderer.h @@ -25,12 +25,11 @@ struct TinyRenderObjectData //class IShader* m_shader; todo(erwincoumans) expose the shader, for now we use a default shader //Output - int m_width; - int m_height; + TGAImage& m_rgbColorBuffer; b3AlignedObjectArray& m_depthBuffer; - TinyRenderObjectData(int width, int height,TGAImage& rgbColorBuffer,b3AlignedObjectArray& depthBuffer); + TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray& depthBuffer); virtual ~TinyRenderObjectData(); void loadModel(const char* fileName); @@ -48,7 +47,7 @@ struct TinyRenderObjectData class TinyRenderer { public: - static void renderObject(TinyRenderObjectData& renderData); + static void renderObject(TinyRenderObjectData& renderData, int width, int height); }; #endif // TINY_RENDERER_Hbla diff --git a/examples/pybullet/pybullet.c b/examples/pybullet/pybullet.c index 43ac64db9..0bac58313 100644 --- a/examples/pybullet/pybullet.c +++ b/examples/pybullet/pybullet.c @@ -362,6 +362,11 @@ pybullet_getNumJoints(PyObject* self, PyObject* args) static PyObject* pybullet_setJointPositions(PyObject* self, PyObject* args) { + if (0==sm) + { + PyErr_SetString(SpamError, "Not connected to physics server."); + return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -372,17 +377,22 @@ pybullet_setJointPositions(PyObject* self, PyObject* args) static PyObject* pybullet_renderImage(PyObject* self, PyObject* args) { - + if (0==sm) + { + PyErr_SetString(SpamError, "Not connected to physics server."); + return NULL; + } + ///request an image from a simulated camera, using a software renderer. struct b3CameraImageData imageData; PyObject* objViewMat,* objProjMat; + int width, height; - b3SharedMemoryCommandHandle command = b3InitRequestCameraImage(sm); - - if (PyArg_ParseTuple(args, "OO", &objViewMat, &objProjMat)) + if (PyArg_ParseTuple(args, "iiOO", &width, &height, &objViewMat, &objProjMat)) { - PyObject* seq; - int i, len; + + PyObject* seq; + int i, len; PyObject* item; float viewMatrix[16]; float projectionMatrix[16]; @@ -391,124 +401,133 @@ static PyObject* pybullet_renderImage(PyObject* self, PyObject* args) seq = PySequence_Fast(objViewMat, "expected a sequence"); len = PySequence_Size(objViewMat); //printf("objViewMat size = %d\n", len); - if (len==16) - { - - if (PyList_Check(seq)) - { - for (i = 0; i < len; i++) + if (len==16) { + + if (PyList_Check(seq)) + { + for (i = 0; i < len; i++) + { item = PyList_GET_ITEM(seq, i); - viewMatrix[i] = PyFloat_AsDouble(item); - float v = viewMatrix[i]; + viewMatrix[i] = PyFloat_AsDouble(item); + float v = viewMatrix[i]; //printf("view %d to %f\n", i,v); + } + } + else + { + for (i = 0; i < len; i++) + { + item = PyTuple_GET_ITEM(seq,i); + viewMatrix[i] = PyFloat_AsDouble(item); + } + } + } else + { + valid = 0; } } - else - { - for (i = 0; i < len; i++) - { - item = PyTuple_GET_ITEM(seq,i); - viewMatrix[i] = PyFloat_AsDouble(item); - } - } - } else - { - valid = 0; - } - } + { - seq = PySequence_Fast(objProjMat, "expected a sequence"); - len = PySequence_Size(objProjMat); - //printf("projMat len = %d\n", len); - if (len==16) - { - if (PyList_Check(seq)) - { - for (i = 0; i < len; i++) - { - item = PyList_GET_ITEM(seq, i); - projectionMatrix[i] = PyFloat_AsDouble(item); - } - } - else - { - for (i = 0; i < len; i++) - { - item = PyTuple_GET_ITEM(seq,i); - projectionMatrix[i] = PyFloat_AsDouble(item); - } - } - } else - { - valid = 0; - } - } - + seq = PySequence_Fast(objProjMat, "expected a sequence"); + len = PySequence_Size(objProjMat); + //printf("projMat len = %d\n", len); + if (len==16) + { + if (PyList_Check(seq)) + { + for (i = 0; i < len; i++) + { + item = PyList_GET_ITEM(seq, i); + projectionMatrix[i] = PyFloat_AsDouble(item); + } + } + else + { + for (i = 0; i < len; i++) + { + item = PyTuple_GET_ITEM(seq,i); + projectionMatrix[i] = PyFloat_AsDouble(item); + } + } + } else + { + valid = 0; + } + } Py_DECREF(seq); if (valid) { + b3SharedMemoryCommandHandle command; + + command = b3InitRequestCameraImage(sm); + //printf("set b3RequestCameraImageSetCameraMatrices\n"); b3RequestCameraImageSetCameraMatrices(command, viewMatrix, projectionMatrix); - } - } - - if (b3CanSubmitCommand(sm)) - { - b3SharedMemoryStatusHandle statusHandle; - int statusType; - statusHandle = b3SubmitClientCommandAndWaitStatus(sm, command); - statusType = b3GetStatusType(statusHandle); - if (statusType==CMD_CAMERA_IMAGE_COMPLETED) - { - PyObject *item2; - PyObject* pyResultList;//store 4 elements in this result: width, height, rgbData, depth - - b3GetCameraImageData(sm, &imageData); - //todo: error handling if image size is 0 - pyResultList = PyTuple_New(4); - PyTuple_SetItem(pyResultList, 0, PyInt_FromLong(imageData.m_pixelWidth)); - PyTuple_SetItem(pyResultList, 1, PyInt_FromLong(imageData.m_pixelHeight)); - - PyObject *pylistPos; - PyObject* pylistDep; - - //printf("image width = %d, height = %d\n", imageData.m_pixelWidth, imageData.m_pixelHeight); - { - - PyObject *item; - int bytesPerPixel = 3;//Red, Green, Blue, each 8 bit values - int num=bytesPerPixel*imageData.m_pixelWidth*imageData.m_pixelHeight; - pylistPos = PyTuple_New(num); - pylistDep = PyTuple_New(imageData.m_pixelWidth*imageData.m_pixelHeight); - - for (int i=0;i