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