Jump to content

Import 16bit greyscale pgm files directly into unigine


photo

Recommended Posts

pgm from the netpbm suite is the only 16bit greyscale image format that Global Mapper supports (a tool we use for getting real terrain into our worlds). This would be a very easy format to write a loader for we would appreciate it very much if Unigine had this capability.

Link to comment

Yes some 16bit heightmap format such as (geotiff, pgm or terragen .ter) out of a common GIS package like GlobalMapper would be good.

 

Also I am trying to make a tile with a 16385x16385 heightmap. It appears that i would then need to create 64x64 detail diffuse maps = 4096 individual maps named correctly (1 for each surface). Am I missing something or is this an absurb proposition.

 

If this is going to be the only way to make a terrain so large maybe Unigine could import a 16385x16385 diffuse detail map and automatically cut it up into the 4096 diffuse tiles (513x513).

 

Otherwise I think the terrain system is going to be pretty pointless for us, unless ive missed something.

Link to comment

Also I am trying to make a tile with a 16385x16385 heightmap. It appears that i would then need to create 64x64 detail diffuse maps = 4096 individual maps named correctly (1 for each surface). Am I missing something or is this an absurb proposition.

 

I don't think that this has to be done by hand. Have a look into script code import_diffuse_clicked() in data/core/editor/editor_objects_terrain.h.

 

If you want a per-surface resolution of 512x512 diffuse texture pixels, than create a terrain with 32x32 surface size. On import of your 16k diffuse map specify a coarse texture size much smaller (e.g. 2048/4096) than import diffuse texture. This should trigger automatic diffuse input image splitting into 32x32 surfaces without any manual work.

Link to comment

pgm from the netpbm suite is the only 16bit greyscale image format that Global Mapper supports (a tool we use for getting real terrain into our worlds). This would be a very easy format to write a loader for we would appreciate it very much if Unigine had this capability.

 

Following code adds generic PGM 8-/16-bit grayscale raw format load support to image class. @UNIGINE Some copy and paste and *.PGM file mask in image file dialogs should be sufficent for inclusion in upcomming SDK release.

 

 

framework/Image.h

class Image {
   ....
private:
   ....
   int load_pgm(const char *name);
   ....
}

 

framework/Image.cpp

int Image::load(const char *name) {
   ....
   if(strstr(name,".pgm") || strstr(name,".PGM")) return load_pgm(name);

   Log::error("Image::load(): unknown format of \"%s\" file\n",name);
   return 0;
}

 

/******************************************************************************\
*
* PGM 8-/16-bit grayscale raw format 
*
\******************************************************************************/

/*
*/
int Image::load_pgm(const char *name) {

   File file;
   if(file.open(name,"rb") == 0) {
       Log::error("Image::load_pgm(): can't open \"%s\" file\n",name);
       return 0;
   }

   // read PGM header
   char buffer[256];

   // check raw PGM P5 magic 
   file.readLine( buffer, 256);

   if( ( buffer[0] != 'p' && buffer[0] != 'P' ) || buffer[1] != '5' ) {
       Log::error("Image::load_pgm(): invalid PGM type, only P5 raw PGM supported");
       file.close();
       return 0;
   }

   // get image size
   file.readLine( buffer, 256 );

   while( buffer[0] == '#' )
   {
       // skip comment lines
       file.readLine( buffer, 256 );
   }

   int width  = 0;
   int height = 0;

   sscanf(buffer, "%d %d", &width, &height );

   if( width <= 0 || height <= 0 ) {
       Log::error("Image::load_pgm(): invalid size %d x %d", width, height);
       file.close();
       return 0;
   }

   size_t size = width * height;

   // get max grayscale
   file.readLine( buffer, 256 );

   while( buffer[0] == '#' )
   {
       // skip comment lines
       file.readLine( buffer, 256 );
   }

   int grayscale_max = 0;

   sscanf(buffer, "%d", &grayscale_max );

   if( grayscale_max < 256 )
   {
       // 8 bit grayscale
       create2D(width,height,FORMAT_R8,1,0);

       file.readUCharArray(data,(int)size);
   }
   else if( grayscale_max < 65536 )
   {
       // 16 bit grayscale
       create2D(width,height,FORMAT_R16,1,0);

       file.readUShortArrayBig((unsigned short*)data,(int)size);
   }
   else 
   {
       Log::error("Image::load_pgm(): invalid grayscale max value %d", grayscale_max);
       file.close();
       return 0;
   }

   file.close();

   return 1;
}

 

tools/ImageView/main.cpp

ImageViewer::ImageViewer(int argc,char **argv) {

   .....
   // load directory
   Dir dir;
   if(dir.open("./")) {
       for(int i = 0; i < dir.getNumFiles(); i++) {
           const char *name = dir.getFileName(i);
           if(strstr(name,".tga") || strstr(name,".TGA") ||
              ....
              strstr(name,".pgm") || strstr(name,".PGM")) {
                  images.append(String(name));
              }
       }
   dir.close();
   }
   ....

pgm-test-samples.zip

Link to comment
  • 4 weeks later...

I forgot to add new extension into the editor.

 

You should edit data/core/editor/editor_dialogs.h file on 2936 line, and add ".ppm.pgm" string to extensions.

Link to comment

I have applied the add ".ppm.pgm" string to extensions. however it crashes on import of the greyscale pgm file. I have attached one if you could have a look at it.

 

Also, it seems that Unigine always mirrors the maps vertically onto the terrain. Can we get this changed or have an option to align it right way up.

 

Thanks

Link to comment
×
×
  • Create New...