From 8fd099f341e72b84713001e75b7e6d7b10fad323 Mon Sep 17 00:00:00 2001 From: Matthias Goldhoorn Date: Wed, 4 Feb 2015 12:30:11 +0100 Subject: [PATCH 1/4] First rought version of ply-exporter --- graphics/src/GraphicsCamera.cpp | 2 + graphics/src/GraphicsWidget.cpp | 66 +++++++++++++++++++++++++++++++-- graphics/src/GraphicsWidget.h | 4 +- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/graphics/src/GraphicsCamera.cpp b/graphics/src/GraphicsCamera.cpp index 3888be947..f93939475 100644 --- a/graphics/src/GraphicsCamera.cpp +++ b/graphics/src/GraphicsCamera.cpp @@ -76,6 +76,8 @@ namespace mars { f_nearPlane = MY_ZNEAR; //0.01; f_farPlane = MY_ZFAR; //1000; + +#warning WTF, a hack in mainstream? FILE* hack_aperture = fopen("aperture.cfg", "r"); if(hack_aperture) { fscanf(hack_aperture, "%lf\n", &f_aperture); diff --git a/graphics/src/GraphicsWidget.cpp b/graphics/src/GraphicsWidget.cpp index 3819e03f1..c210dfba9 100644 --- a/graphics/src/GraphicsWidget.cpp +++ b/graphics/src/GraphicsWidget.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #define CULL_LAYER (1 << (widgetID-1)) @@ -898,6 +899,7 @@ namespace mars { 1, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV); osgCamera->attach(osg::Camera::COLOR_BUFFER, rttImage.get()); rttTexture->setImage(rttImage); + } // depth component rttDepthTexture = new osg::Texture2D(); @@ -923,7 +925,6 @@ namespace mars { rttDepthTexture->setImage(rttDepthImage); - } graphicsCamera = new GraphicsCamera(osgCamera, widgetWidth, widgetHeight); } @@ -1062,7 +1063,7 @@ namespace mars { else { //slow but works... - void *data; + void *data=0; postDrawCallback->getImageData(&data, width, height); memcpy(buffer, data, width*height*4); free(data); @@ -1083,10 +1084,11 @@ namespace mars { void GraphicsWidget::getRTTDepthData(float* buffer, int& width, int& height) { - if(isRTTWidget) { + if(rttDepthImage) { GLuint* data2 = (GLuint *)rttDepthImage->data(); width = rttDepthImage->s(); height = rttDepthImage->t(); + assert(width*height >= (sizeof(buffer) / sizeof(buffer[0]))); double fovy, aspectRatio, Zn, Zf; graphicsCamera->getOSGCamera()->getProjectionMatrixAsPerspective( fovy, aspectRatio, Zn, Zf ); @@ -1307,6 +1309,14 @@ namespace mars { case '9' : if (myHUD) myHUD->switchCullElement(key); break; + case 'p' : + { + std::stringstream s; + static int i=0; + s << i++ << "out.ply"; + savePLY(s.str()); + break; + } case '.' : graphicsCamera->toggleStereoMode(); break; @@ -1726,5 +1736,55 @@ namespace mars { } } + + void GraphicsWidget::savePLY(std::string filename){ + setGrabFrames(true); + + double fovy,ratio,zNear,zFar; + getMainCamera()->getProjectionMatrixAsPerspective(fovy,ratio,zNear,zFar); + int width,height,cwidth,cheight; + size_t w = rttDepthImage->s(),h=rttDepthImage->t(); + float buffer[w*h]; + getRTTDepthData(buffer,width,height); + char cbuffer[width*height*4]; + getImageData(cbuffer,cwidth,cheight); + double angle_x = ((fovy*ratio)/180.0*M_PI)/width; + double angle_y = (fovy/180.0*M_PI)/height; + if(cheight != height || cwidth != width){ + std::cerr << "Sizes are differ" << std::endl; + assert(false); + } + + std::ofstream file(filename.c_str(), std::ofstream::out | std::fstream::trunc); + assert(file.is_open()); + file << "ply" << std::endl; + file << "format ascii 1.0" << std::endl; + file << "element vertex " << width*height << std::endl; + file << "property float x" << std::endl; + file << "property float y" << std::endl; + file << "property float z" << std::endl; + file << "property char red" << std::endl; + file << "property char green" << std::endl; + file << "property char blue" << std::endl; + file << "end_header" << std::endl; + for(unsigned int x=0;x Date: Wed, 25 Feb 2015 08:37:19 +0100 Subject: [PATCH 2/4] Update GraphicsCamera.cpp --- graphics/src/GraphicsCamera.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/graphics/src/GraphicsCamera.cpp b/graphics/src/GraphicsCamera.cpp index f93939475..ca5b67ad9 100644 --- a/graphics/src/GraphicsCamera.cpp +++ b/graphics/src/GraphicsCamera.cpp @@ -77,7 +77,6 @@ namespace mars { f_nearPlane = MY_ZNEAR; //0.01; f_farPlane = MY_ZFAR; //1000; -#warning WTF, a hack in mainstream? FILE* hack_aperture = fopen("aperture.cfg", "r"); if(hack_aperture) { fscanf(hack_aperture, "%lf\n", &f_aperture); From 564c30e9a30bb011040c18d7d439e3539ed8195f Mon Sep 17 00:00:00 2001 From: Matthias Goldhoorn Date: Wed, 18 Mar 2015 13:09:20 +0100 Subject: [PATCH 3/4] Update GraphicsCamera.cpp --- graphics/src/GraphicsCamera.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/graphics/src/GraphicsCamera.cpp b/graphics/src/GraphicsCamera.cpp index ca5b67ad9..3888be947 100644 --- a/graphics/src/GraphicsCamera.cpp +++ b/graphics/src/GraphicsCamera.cpp @@ -76,7 +76,6 @@ namespace mars { f_nearPlane = MY_ZNEAR; //0.01; f_farPlane = MY_ZFAR; //1000; - FILE* hack_aperture = fopen("aperture.cfg", "r"); if(hack_aperture) { fscanf(hack_aperture, "%lf\n", &f_aperture); From c425181adac2e0989f555e11bc8e21018f522cc7 Mon Sep 17 00:00:00 2001 From: Matthias Goldhoorn Date: Mon, 17 Aug 2015 07:13:30 +0200 Subject: [PATCH 4/4] Render the Depth-Image only iff it is requested --- graphics/src/GraphicsManager.cpp | 6 ++++++ graphics/src/GraphicsManager.h | 1 + graphics/src/GraphicsWidget.cpp | 7 ++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/graphics/src/GraphicsManager.cpp b/graphics/src/GraphicsManager.cpp index b677ed0e2..801656222 100644 --- a/graphics/src/GraphicsManager.cpp +++ b/graphics/src/GraphicsManager.cpp @@ -1939,6 +1939,12 @@ namespace mars { void GraphicsManager::removeOSGNode(void* node) { scene->removeChild((osg::Node*)node); } + + bool GraphicsManager::isDepthImageActive(){ + if(!cfg) return false; + return cfg->getOrCreateProperty("Graphics", "renderDepthImage", false, this).bValue; + } + } // end of namespace graphics } // end of namespace mars diff --git a/graphics/src/GraphicsManager.h b/graphics/src/GraphicsManager.h index dba6aeab5..6a4fb1fa1 100644 --- a/graphics/src/GraphicsManager.h +++ b/graphics/src/GraphicsManager.h @@ -296,6 +296,7 @@ namespace mars { void removeGraphicsWidget(unsigned long id); virtual bool isInitialized() const {return initialized;} + virtual bool isDepthImageActive(); private: mars::interfaces::GraphicData graphicOptions; diff --git a/graphics/src/GraphicsWidget.cpp b/graphics/src/GraphicsWidget.cpp index c210dfba9..e46349e05 100644 --- a/graphics/src/GraphicsWidget.cpp +++ b/graphics/src/GraphicsWidget.cpp @@ -901,6 +901,9 @@ namespace mars { rttTexture->setImage(rttImage); } + + + if(gm->isDepthImageActive()){ // depth component rttDepthTexture = new osg::Texture2D(); rttDepthTexture->setResizeNonPowerOfTwoHint(false); @@ -919,11 +922,9 @@ namespace mars { 1, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT); osgCamera->attach(osg::Camera::DEPTH_BUFFER, rttDepthImage.get()); - std::fill(rttDepthImage->data(), rttDepthImage->data() + widgetWidth * widgetHeight * sizeof(GLuint), 0); - rttDepthTexture->setImage(rttDepthImage); - + } graphicsCamera = new GraphicsCamera(osgCamera, widgetWidth, widgetHeight); }