Perl script to add texture coordinates to .OBJ files

New Message Reply Date view Thread view Subject view Author view

Andreas Schiffler (schiffler++at++zkm.de)
Thu, 10 Apr 1997 17:03:36 -0400


Hi all,

something that might be useful to anyone out there using .obj files in
performer is this little script that adds texture coordinates. Simply
pipe your .obj file through the script:
        objtexture_planar_xy.pl

The change the .mtl file to include the line describing the texture map
image file:
        map_Kd image.rgb

The code can be changed easily to use cylindrical or spherical mapping
by redefining how the s and t coordinates get calculated. For example:
        $x-=$xa;$y-=$ya;
        $s=0.5*atan2($y,$x)/$pi+0.5;
        $t=($z-$zmin)/($zmax-$zmin);

Bye now
  Andreas

--------------- objtexture_planar_xy.pl ----------------------
#!/usr/bin/perl

$usage="Calculate planar texture coordinates and fill them in";

$i=0;
$xa=$ya=$za=0;
$ymin=$xmin=$zmin=100000;
$ymax=$xmax=$zmax=-100000;
$pi=4*atan2(1,1);
$f=0.5/$pi;

while(<>)
{ if($_=~/^v\s+(\S+\s+\S+\s+\S+)/)
  { ($x,$y,$z)=split(/\s+/,$1);
    $vert[$i++]=join(" ",$x,$y,$z);
    $xa+=$x;$ya+=$y;$za+=$z;
    if($y<$ymin) {
     $ymin=$y;
    }
    if($y>$ymax) {
     $ymax=$y;
    }
    if($x<$xmin) {
     $xmin=$x;
    }
    if($x>$xmax) {
     $xmax=$x;
    }
    if($z<$zmin) {
     $zmin=$z;
    }
    if($z>$zmax) {
     $zmax=$z;
    }
    print $_;
  }
  else
  { if($i>0)
    {
      $i1=1/$i;
      $xa*=$i1; $ya*=$i1; $za*=$i1;
      for($j=0;$j<$i;$j++)
      { ($x,$y,$z)=split(/\s+/,$vert[$j]);
        $s=($x-$xmin)/($xmax-$xmin);
        $t=($y-$ymin)/($ymax-$ymin);
        print "vt $s $t\n";
      }
      $i=0;
    }
    if($_=~/^f /)
    { print "f";
      if($'=~/\\/)
      { $_=<>;++at++face=split; }
      else
      { ++at++face=split;shift ++at++face; }
      foreach $c ( ++at++face )
      { ($v,$vt,$vn)=split(/\//,$c);
        print " $v/$v/$vn";
      }
      print "\n";
    }
    else
    { print $_; }
  }
}
=======================================================================
List Archives, FAQ, FTP: http://www.sgi.com/Technology/Performer/
            Submissions: info-performer++at++sgi.com
        Admin. requests: info-performer-request++at++sgi.com


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b2 on Mon Aug 10 1998 - 17:55:02 PDT

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.