talpa/iomesh.cpp
// ---------------------------------------------------------
///
/// Write a render-ready RIB file.
///
// ---------------------------------------------------------
Write a render-ready RIB file.
Write a render - ready RIB file .
bool output_rib( const std::vector<Vec3d>& positions, const NonDestructiveTriMesh& mesh, const char *filename_format, ...)
{
#ifdef _MSC_VER
    va_list ap;
    va_start(ap, filename_format);
    int len=_vscprintf(filename_format, ap) 
        +1; 
    char *filename=new char[len];
    vsprintf(filename, filename_format, ap);
    std::ofstream out;
    out.open( filename );
    delete[] filename;
    if( out == NULL )
    {
        return false;
    }
    len=_vscprintf("track%04d.tiff", ap) 
        +1; 
    filename=new char[len];
    vsprintf( filename, "track%04d.tiff", ap );
    len=_vscprintf("track%04d_shadow.shad", ap) 
        +1; 
    char *shadow_filename=new char[len];
    vsprintf( shadow_filename, "track%04d_shadow.shad", ap );
    va_end(ap);
#else
    va_list ap;
    va_start(ap, filename_format);
    char *filename;
    vasprintf(&filename, filename_format, ap);   
    std::ofstream out;
    out.open( filename );
    delete[] filename;
    if( out == NULL )
    {
        return false;
    }
    vasprintf( &filename, "track%04d.tiff", ap );
    char *shadow_filename;
    vasprintf( &shadow_filename, "track%04d_shadow.shad", ap );
    va_end(ap);
#endif
    std::vector<float> xs;
    for ( unsigned int i = 0; i < positions.size(); ++i )
    {
        xs.push_back( (float) positions[i][0] );
        xs.push_back( (float) positions[i][1] );
        xs.push_back( (float) positions[i][2] );
    }
    std::vector<Vec3f> normals;
    for ( unsigned int i = 0; i < positions.size(); ++i )
    {
        Vec3f n(0,0,0);
        for ( unsigned int j = 0; j < mesh.m_vertex_to_triangle_map[i].size(); ++j )
        {
            const Vec3st& tri = mesh.get_triangle( mesh.m_vertex_to_triangle_map[i][j] );
            Vec3d u = positions[ tri[1] ] - positions[ tri[0] ];
            Vec3d v = positions[ tri[2] ] - positions[ tri[0] ];
            Vec3d tn = normalized(cross(u, v));          
            n += Vec3f( (float)tn[0], (float)tn[1], (float)tn[2] ); 
        }
        normals.push_back( n / (float) mesh.m_vertex_to_triangle_map[i].size() );
    }
    out << "Display \"" << filename << "\" \"file\" \"rgb\"" << std::endl;
    delete[] filename;
    out << "Format " << Gluvi::winwidth << " " << Gluvi::winheight << " 1" << std::endl;
    out << "PixelSamples 2 2" << std::endl;
    out << "Exposure 1.0 2.2" << std::endl;
    Gluvi::camera->export_rib(out);
    out << "WorldBegin\n";
    out << "LightSource \"ambientlight\" 1 \"intensity\" .3 \"lightcolor\" [1 1 1]\n";
    out << "AttributeBegin\n";
    out << "  Color [0.6 0.6 0.2]\n";
    out << "  Opacity [1 1 1]\n";
    out << "  Surface \"matte\" \"Kd\" 1\n";
    const float plane_limit = 50.0f;
    const float plane_distance = 10.0f;
    char buf[256];
    sprintf( buf, "  Polygon \"P\" [-%f -%f -%f %f -%f -%f  %f %f -%f  -%f %f -%f]\n", 
            plane_limit, plane_limit, plane_distance, 
            plane_limit, plane_limit, plane_distance, 
            plane_limit, plane_limit, plane_distance, 
            plane_limit, plane_limit, plane_distance );
    out << buf;
    out << "AttributeEnd\n";
    out << "Color [0.3 0.3 0.9]\n";
    out << "Surface \"matte\" \n";
    write_ribfile( mesh, xs, out);
    out << " \"N\" [";
    for(unsigned int i=0; i<normals.size(); ++i)
    {
        out << normals[i] << "  ";
    }
    out << "]" << std::endl;
    out << "WorldEnd\n";
    out.flush();
    out.close();
    return true;
}
bool output_rib ( const std :: vector < Vec3d > & positions , const NonDestructiveTriMesh & mesh , const char * filename_format , ... ) { #ifdef _MSC_VER va_list ap ; va_start ( ap , filename_format ) ; int len = _vscprintf ( filename_format , ap ) + 1 ; char * filename = new char [ len ] ; vsprintf ( filename , filename_format , ap ) ; std :: ofstream out ; out . open ( filename ) ; delete [ ] filename ; if ( out == NULL ) { return false ; } len = _vscprintf ( " " , ap ) + 1 ; filename = new char [ len ] ; vsprintf ( filename , " " , ap ) ; len = _vscprintf ( " " , ap ) + 1 ; char * shadow_filename = new char [ len ] ; vsprintf ( shadow_filename , " " , ap ) ; va_end ( ap ) ; #else va_list ap ; va_start ( ap , filename_format ) ; char * filename ; vasprintf ( & filename , filename_format , ap ) ; std :: ofstream out ; out . open ( filename ) ; delete [ ] filename ; if ( out == NULL ) { return false ; } vasprintf ( & filename , " " , ap ) ; char * shadow_filename ; vasprintf ( & shadow_filename , " " , ap ) ; va_end ( ap ) ; #endif std :: vector < float > xs ; for ( unsigned int i = 0 ; i < positions . size ( ) ; ++ i ) { xs . push_back ( ( float ) positions [ i ] [ 0 ] ) ; xs . push_back ( ( float ) positions [ i ] [ 1 ] ) ; xs . push_back ( ( float ) positions [ i ] [ 2 ] ) ; } std :: vector < Vec3f > normals ; for ( unsigned int i = 0 ; i < positions . size ( ) ; ++ i ) { Vec3f n ( 0 , 0 , 0 ) ; for ( unsigned int j = 0 ; j < mesh . m_vertex_to_triangle_map [ i ] . size ( ) ; ++ j ) { const Vec3st & tri = mesh . get_triangle ( mesh . m_vertex_to_triangle_map [ i ] [ j ] ) ; Vec3d u = positions [ tri [ 1 ] ] - positions [ tri [ 0 ] ] ; Vec3d v = positions [ tri [ 2 ] ] - positions [ tri [ 0 ] ] ; Vec3d tn = normalized ( cross ( u , v ) ) ; n += Vec3f ( ( float ) tn [ 0 ] , ( float ) tn [ 1 ] , ( float ) tn [ 2 ] ) ; } normals . push_back ( n / ( float ) mesh . m_vertex_to_triangle_map [ i ] . size ( ) ) ; } out << " \" " << filename << " \" \" \" \" \" " << std :: endl ; delete [ ] filename ; out << " " << Gluvi :: winwidth << " " << Gluvi :: winheight << " " << std :: endl ; out << " " << std :: endl ; out << " " << std :: endl ; Gluvi :: camera -> export_rib ( out ) ; out << " \n " ; out << " \" \" \" \" \" \" \n " ; out << " \n " ; out << " \n " ; out << " \n " ; out << " \" \" \" \" \n " ; const float plane_limit = 50.0f ; const float plane_distance = 10.0f ; char buf [ 256 ] ; sprintf ( buf , " \" \" \n " , plane_limit , plane_limit , plane_distance , plane_limit , plane_limit , plane_distance , plane_limit , plane_limit , plane_distance , plane_limit , plane_limit , plane_distance ) ; out << buf ; out << " \n " ; out << " \n " ; out << " \" \" \n " ; write_ribfile ( mesh , xs , out ) ; out << " \" \" " ; for ( unsigned int i = 0 ; i < normals . size ( ) ; ++ i ) { out << normals [ i ] << " " ; } out << " " << std :: endl ; out << " \n " ; out . flush ( ) ; out . close ( ) ; return true ; }
Write a render-ready RIB file.
Write a render - ready RIB file .
// _vscprintf doesn't count // terminating '\0' // first line: what image file this RIB file should produce // _vscprintf doesn't count // terminating '\0' // _vscprintf doesn't count // terminating '\0' // first line: what image file this RIB file should produce // flatten // next line: image format (width and height in pixels, pixel aspect ratio) // then write out the camera specification // start the scene /*
     for ( unsigned int i = 0; i < lights.size(); ++i )
     {
     // compute location of light
     Vec3f light_pos( 0, 0, lights[i].dist );
     rotate( light_pos, lights[i].pitch, Vec3f(1,0,0) );
     rotate( light_pos, lights[i].heading, Vec3f(0,1,0) );
     light_pos += Vec3f( lights[i].target[0], lights[i].target[1], lights[i].target[2] );
     
     out << "LightSource \"singleshadowpoint\" 2 \"intensity\" 30 \"from\" [" << light_pos << "] \"shadowmap\" \"" << shadow_filename << "\"\n";
     }
     */ //out << "LightSource \"distantlight\" 3 \"intensity\" 0.3 \"from\" [-5 -10 20] \"to\" [0 0 0]\n"; // finish the scene