示例#1
0
SbBool
generateTextureMap (SoNode *root, SoTexture2 *texture,
   short textureWidth, short textureHeight)
{
   SbViewportRegion myViewport(textureWidth, textureHeight);
   SoOffscreenRenderer::Components comp = SoOffscreenRenderer::RGB;

   // Render the scene
   SoOffscreenRenderer *myRenderer = new SoOffscreenRenderer(myViewport);
   myRenderer->setComponents(comp);
   myRenderer->setBackgroundColor(SbColor(0.8, 0.8, 0.0));
   if (!myRenderer->render(root)) {
      delete myRenderer;
      return FALSE;
   }

   // Generate the texture
   texture->image.setValue(SbVec2s(textureWidth, textureHeight), comp, 
                           myRenderer->getBuffer());
   delete myRenderer;
   return TRUE; 
} 
示例#2
0
void Snapshot_dialog::take_snapshot( const QString& filename, const QString& f ) {
  if( filename.isEmpty() ) return;

  QApplication::setOverrideCursor( Qt::WaitCursor );

  // Create an offscreen renderer which copies the settings from the Viewer
  SoOffscreenRenderer *snapshot = 
    new SoOffscreenRenderer( render_area_->getViewportRegion() );
  //snapshot->setTransparencyType(SoGLRenderAction::BLEND);
  snapshot->setBackgroundColor( render_area_->getBackgroundColor() );
  snapshot->setComponents( SoOffscreenRenderer::RGB );

  // Create the buffer in snapshot
  if ( !snapshot->render( render_area_->getSceneManager()->getSceneGraph()) ) {
    appli_warning("Snapshot: Failed to render the scene.");
    return;
  }
  
  QString format = f;

  // Write the buffer to file
    
  if( format.contains( "postscript", Qt::CaseInsensitive ) || format.contains( "PS", Qt::CaseInsensitive ) ) {
    FILE *fp = fopen( qstring2string(filename).c_str(), "w");
    if( !fp ) {
      GsTLcerr << "Can't create file " << qstring2string(filename) << "\n" << gstlIO::end; 
      return;
    }
    snapshot->writeToPostScript(fp);
    fclose(fp);
  }


  if( format.contains( "PPM", Qt::CaseInsensitive ) || format.contains( "PNG", Qt::CaseInsensitive ) ||
      format.contains( "BMP", Qt::CaseInsensitive ) ) {
//    fclose(fp);
//    std::ofstream ofile( filename.latin1() );

    format = format.section( " (", 0, 0 );
    SbVec2s size = 
      render_area_->getGLRenderAction()->getViewportRegion().getViewportSizePixels();
    int x = size[0];
    int y = size[1];
 
//    write_to_ppm_format( ofile, snapshot->getBuffer(), x, y );
 
    QImage qimage( x, y, QImage::Format_RGB32 );
    uchar* image = snapshot->getBuffer();
    
    //for( int j=y-1 ; j>=0 ; j-- ) {
    for( int i = 0 ; i < x ; i++ ) {
      for( int j = 0 ; j < y ; j++ ) {
        int r,g,b;
        r= image[3*(j*x+i)];
        g= image[3*(j*x+i)+1];
        b= image[3*(j*x+i)+2];
        uint *p = (uint *)qimage.scanLine(y-(j+1)) + i;
        *p = qRgb(r,g,b);   
      }
    }
    qimage.save( filename, qstring2string(format).c_str() );

  }

//  QSound::play( "shutter.wav" );

  delete snapshot;

  QApplication::restoreOverrideCursor();
}