Skip to content

Commit

Permalink
First rought version of ply-exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
goldhoorn committed Feb 10, 2015
1 parent c556629 commit c99d44d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions graphics/src/GraphicsCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
66 changes: 63 additions & 3 deletions graphics/src/GraphicsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <osgGA/FlightManipulator>
#include <osgGA/TerrainManipulator>
#include <osgWidget/Frame>
#include <sstream>

#define CULL_LAYER (1 << (widgetID-1))

Expand Down Expand Up @@ -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();
Expand All @@ -923,7 +925,6 @@ namespace mars {
rttDepthTexture->setImage(rttDepthImage);


}
graphicsCamera = new GraphicsCamera(osgCamera, widgetWidth, widgetHeight);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<width;x++){
for(unsigned int y=0;y<height;y++){
float dist = buffer[(y*width)+x];
unsigned char *c = (unsigned char*) &cbuffer[(y*width*4)+(x*4)];
double py = dist * -tan(angle_x*(x-width/2.0));
double pz = dist * tan(angle_y*(y-height/2.0));
double px = dist;
if(isnan(dist)){ //check for nan
px=0;
py=0;
pz=0;
}
file << px << " " << py << " " << pz << " " << (int)c[0] << " " << (int)c[1] << " " << (int)c[2] << std::endl;
}
}
}



} // end of namespace graphics
} // end of namespace mars
4 changes: 3 additions & 1 deletion graphics/src/GraphicsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ namespace mars {
// called post drawing
PostDrawCallback *postDrawCallback;

private:
void savePLY(std::string filename);

protected:
// the widget id
unsigned long widgetID;

Expand Down

0 comments on commit c99d44d

Please sign in to comment.