From d339cf5b74c754064b0336b9e71f881c2e29805c Mon Sep 17 00:00:00 2001 From: Jasmine Hsu Date: Fri, 24 Jun 2016 15:30:43 -0700 Subject: [PATCH 1/4] ability to call renderImage with three vectors: camera position, target position, and up vector --- examples/pybullet/pybullet.c | 78 +++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/examples/pybullet/pybullet.c b/examples/pybullet/pybullet.c index 8574fa308..bf55cc46a 100644 --- a/examples/pybullet/pybullet.c +++ b/examples/pybullet/pybullet.c @@ -976,6 +976,32 @@ static int pybullet_internalSetMatrix(PyObject* objMat, float matrix[16]) return 0; } +// internal function to set a float vector[3] +// used to initialize camera position with +// a view and projection matrix in renderImage() +// +// // Args: +// matrix - float[16] which will be set by values from objMat +static int pybullet_internalSetVector(PyObject* objMat, float vector[3]) +{ + int i, len; + PyObject* seq; + + seq = PySequence_Fast(objMat, "expected a sequence"); + len = PySequence_Size(objMat); + if (len==3) + { + for (i = 0; i < len; i++) + { + vector[i] = pybullet_internalGetFloatFromSequence(seq,i); + } + Py_DECREF(seq); + return 1; + } + Py_DECREF(seq); + return 0; +} + // Render an image from the current timestep of the simulation // // Examples: @@ -995,10 +1021,20 @@ static PyObject* pybullet_renderImage(PyObject* self, PyObject* args) ///request an image from a simulated camera, using a software renderer. struct b3CameraImageData imageData; PyObject* objViewMat,* objProjMat; - int width, height; + PyObject* objCameraPos,*objTargetPos,* objCameraUp; + + int width, height; int size= PySequence_Size(args); float viewMatrix[16]; float projectionMatrix[16]; + + float cameraPos[3]; + float targetPos[3]; + float cameraUp[3]; + + float left, right, bottom, top, aspect; + float nearVal = .001; + float farVal = 1000; // inialize cmd b3SharedMemoryCommandHandle command; @@ -1035,6 +1071,46 @@ static PyObject* pybullet_renderImage(PyObject* self, PyObject* args) } } + if (size==5) // set camera resoluation and view and projection matrix + { + if (PyArg_ParseTuple(args, "iiOOO", &width, &height, &objCameraPos, &objTargetPos, &objCameraUp)) + { + b3RequestCameraImageSetPixelResolution(command,width,height); + if (pybullet_internalSetVector(objCameraPos, cameraPos) && + pybullet_internalSetVector(objTargetPos, targetPos) && + pybullet_internalSetVector(objCameraUp, cameraUp)) + { + printf("\ncamera pos:\n"); + for(int i =0;i<3; i++) { + printf(" %f", cameraPos[i]); + } + + printf("\ntargetPos pos:\n"); + for(int i =0;i<3; i++) { + printf(" %f", targetPos[i]); + } + + printf("\ncameraUp pos:\n"); + for(int i =0;i<3; i++) { + printf(" %f", cameraUp[i]); + } + printf("\n"); + b3RequestCameraImageSetViewMatrix(command, cameraPos, targetPos, cameraUp); + printf("\n"); + + } + aspect = width/height; + left = -aspect * nearVal; + right = aspect * nearVal; + bottom = -nearVal; + top = nearVal; + + b3RequestCameraImageSetProjectionMatrix(command, left, right, bottom, top, nearVal, farVal); + printf("\n"); + + } + } + if (b3CanSubmitCommand(sm)) { b3SharedMemoryStatusHandle statusHandle; From 5d5e7df7c5bc93ef4b1b23c464aeb4d994e6379d Mon Sep 17 00:00:00 2001 From: Jasmine Hsu Date: Mon, 27 Jun 2016 11:19:57 -0700 Subject: [PATCH 2/4] exposing near/far values as params; commented out debug printf which caused failed checks --- examples/pybullet/pybullet.c | 42 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/examples/pybullet/pybullet.c b/examples/pybullet/pybullet.c index bf55cc46a..aa0115e95 100644 --- a/examples/pybullet/pybullet.c +++ b/examples/pybullet/pybullet.c @@ -1033,8 +1033,9 @@ static PyObject* pybullet_renderImage(PyObject* self, PyObject* args) float cameraUp[3]; float left, right, bottom, top, aspect; - float nearVal = .001; - float farVal = 1000; + float nearVal, farVal; + // float nearVal = .001; + // float farVal = 1000; // inialize cmd b3SharedMemoryCommandHandle command; @@ -1073,32 +1074,33 @@ static PyObject* pybullet_renderImage(PyObject* self, PyObject* args) if (size==5) // set camera resoluation and view and projection matrix { - if (PyArg_ParseTuple(args, "iiOOO", &width, &height, &objCameraPos, &objTargetPos, &objCameraUp)) + if (PyArg_ParseTuple(args, "iiOOOii", &width, &height, &objCameraPos, &objTargetPos, &objCameraUp, &nearVal, %farVal)) { b3RequestCameraImageSetPixelResolution(command,width,height); if (pybullet_internalSetVector(objCameraPos, cameraPos) && pybullet_internalSetVector(objTargetPos, targetPos) && pybullet_internalSetVector(objCameraUp, cameraUp)) { - printf("\ncamera pos:\n"); - for(int i =0;i<3; i++) { - printf(" %f", cameraPos[i]); - } - - printf("\ntargetPos pos:\n"); - for(int i =0;i<3; i++) { - printf(" %f", targetPos[i]); - } - - printf("\ncameraUp pos:\n"); - for(int i =0;i<3; i++) { - printf(" %f", cameraUp[i]); - } - printf("\n"); + // printf("\ncamera pos:\n"); + // for(int i =0;i<3; i++) { + // printf(" %f", cameraPos[i]); + // } + // + // printf("\ntargetPos pos:\n"); + // for(int i =0;i<3; i++) { + // printf(" %f", targetPos[i]); + // } + // + // printf("\ncameraUp pos:\n"); + // for(int i =0;i<3; i++) { + // printf(" %f", cameraUp[i]); + // } + // printf("\n"); b3RequestCameraImageSetViewMatrix(command, cameraPos, targetPos, cameraUp); - printf("\n"); + // printf("\n"); } + aspect = width/height; left = -aspect * nearVal; right = aspect * nearVal; @@ -1106,7 +1108,7 @@ static PyObject* pybullet_renderImage(PyObject* self, PyObject* args) top = nearVal; b3RequestCameraImageSetProjectionMatrix(command, left, right, bottom, top, nearVal, farVal); - printf("\n"); + // printf("\n"); } } From e24ec9e8c0eaa0225a774d08796f9c739c7eedab Mon Sep 17 00:00:00 2001 From: Jasmine Hsu Date: Mon, 27 Jun 2016 13:18:12 -0700 Subject: [PATCH 3/4] nearVal and farVal as params --- examples/pybullet/pybullet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pybullet/pybullet.c b/examples/pybullet/pybullet.c index aa0115e95..c12085b30 100644 --- a/examples/pybullet/pybullet.c +++ b/examples/pybullet/pybullet.c @@ -1074,7 +1074,7 @@ static PyObject* pybullet_renderImage(PyObject* self, PyObject* args) if (size==5) // set camera resoluation and view and projection matrix { - if (PyArg_ParseTuple(args, "iiOOOii", &width, &height, &objCameraPos, &objTargetPos, &objCameraUp, &nearVal, %farVal)) + if (PyArg_ParseTuple(args, "iiOOOii", &width, &height, &objCameraPos, &objTargetPos, &objCameraUp, &nearVal, &farVal)) { b3RequestCameraImageSetPixelResolution(command,width,height); if (pybullet_internalSetVector(objCameraPos, cameraPos) && From f6bead7152028c9ae0a0cbb0fbbf6ee2375c1a4c Mon Sep 17 00:00:00 2001 From: Jasmine Hsu Date: Mon, 27 Jun 2016 13:51:28 -0700 Subject: [PATCH 4/4] edit method definitions (docstring) for calling renderImage() --- examples/pybullet/pybullet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pybullet/pybullet.c b/examples/pybullet/pybullet.c index c12085b30..c8d2bef04 100644 --- a/examples/pybullet/pybullet.c +++ b/examples/pybullet/pybullet.c @@ -1225,7 +1225,7 @@ static PyMethodDef SpamMethods[] = { //applyLinkForce {"renderImage", pybullet_renderImage, METH_VARARGS, - "Render an image (given the pixel resolution width & height and camera view & projection matrices), and return the 8-8-8bit RGB pixel data and floating point depth values"}, + "Render an image (given the pixel resolution width, height, view matrix, projection matrix, near and far vlues), and return the 8-8-8bit RGB pixel data and floating point depth values"}, {NULL, NULL, 0, NULL} /* Sentinel */ };