home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vroom / 3d_2_nff.pas < prev    next >
Pascal/Delphi Source File  |  1996-03-19  |  5KB  |  171 lines

  1. program threed_2_nff;
  2.  
  3. uses dos;
  4.  
  5. function file_exists(filename : string) : boolean;
  6. var dirinfo : searchrec;
  7. begin
  8.      findfirst(filename,anyfile,dirinfo);
  9.      file_exists:=(doserror=0);
  10. end;
  11.  
  12. var input_file, output_file, my_pal : string;
  13.     in_f, out_f, col_f : text;
  14.     ch : char;
  15.     gourard,ok : boolean;
  16.     n1,n2,n3,c,c2, failed : integer;
  17.     x,y,z, bright : real;
  18.     colour : byte;
  19.     r,g,b : array[0..15] of real;
  20.  
  21. procedure write_colours;
  22. var r1,g1,b1 : integer;
  23. begin
  24.      r1:=round(15*bright*r[colour]);
  25.      g1:=round(15*bright*g[colour]);
  26.      b1:=round(15*bright*b[colour]);
  27.      write(out_f,'0x');
  28.      case r1 of
  29.           0..9 : write(out_f,r1);
  30.             10 : write(out_f,'a');
  31.             11 : write(out_f,'b');
  32.             12 : write(out_f,'c');
  33.             13 : write(out_f,'d');
  34.             14 : write(out_f,'e');
  35.             15 : write(out_f,'f');
  36.      end;
  37.      case g1 of
  38.           0..9 : write(out_f,g1);
  39.             10 : write(out_f,'a');
  40.             11 : write(out_f,'b');
  41.             12 : write(out_f,'c');
  42.             13 : write(out_f,'d');
  43.             14 : write(out_f,'e');
  44.             15 : write(out_f,'f');
  45.      end;
  46.      case b1 of
  47.           0..9 : writeln(out_f,b1);
  48.             10 : writeln(out_f,'a');
  49.             11 : writeln(out_f,'b');
  50.             12 : writeln(out_f,'c');
  51.             13 : writeln(out_f,'d');
  52.             14 : writeln(out_f,'e');
  53.             15 : writeln(out_f,'f');
  54.      end;
  55. end;
  56.  
  57. begin
  58.      failed:=0;
  59.      write('3D Input File : ');
  60.      readln(input_file);
  61.      if not(file_exists(input_file)) then
  62.      begin
  63.           writeln;
  64.           writeln('Error! File does not exist');
  65.           halt;
  66.      end;
  67.      writeln;
  68.      write('NFF Output File : ');
  69.      readln(output_file);
  70.      writeln;
  71.      write('Gourard shaded ? : ');
  72.      readln(ch);
  73.      if ((ch='Y')or(ch='y')) then gourard:=true else gourard:=false;
  74.      write('Default colours ? : ');
  75.      readln(ch);
  76.      if ((ch='n')or(ch='N')) then
  77.      begin
  78.            write('Palette name : ');
  79.            readln(my_pal);
  80.      end
  81.      else
  82.          my_pal:='def.col';
  83.      if not(file_exists(my_pal)) then
  84.      begin
  85.           writeln;
  86.           writeln('Error! Palette does not exist');
  87.           halt;
  88.      end;
  89.      assign(col_f,my_pal);
  90.      reset(col_f);
  91.      for c:=0 to 15 do readln(col_f,r[c], g[c], b[c]);
  92.      close(col_f);
  93.  
  94.      writeln('Checking ',input_file);
  95.      assign(in_f,input_file);
  96.      reset(in_f);
  97.      readln(in_f,n1);                         {Number of vertices}
  98.      for c:=1 to n1 do readln(in_f,x,y,z);    {Transfer vertices}
  99.      readln(in_f,n1);
  100.      if n1<>0 then                            {Check digit}
  101.      begin
  102.           writeln('Error - failed check digit on input file');
  103.           halt;
  104.      end;
  105.      readln(in_f,n1);                         {Number of faces}
  106.      for c:=1 to n1 do
  107.      begin
  108.           readln(in_f,colour,bright);
  109.           readln(in_f,n2);                    {Number of points}
  110.           if n2<3 then inc(failed);
  111.           for c2:=1 to n2 do read(in_f,n3);
  112.           readln(in_f);
  113.           readln(in_f,n2);
  114.           if n2<>0 then
  115.           begin
  116.                writeln('Error - failed check digit on input file');
  117.                halt;
  118.           end;
  119.      end;
  120.      readln(in_f,n1);
  121.      if n1<>0 then
  122.      begin
  123.           writeln('Error - failed check digit on input file');
  124.           halt;
  125.      end;
  126.      close(in_f);
  127.  
  128.      if failed>0 then writeln(failed,' points or lines found which will not be converted');
  129.      writeln;
  130.      writeln('Starting conversion');
  131.      assign(in_f,input_file);
  132.      reset(in_f);
  133.      assign(out_f,output_file);
  134.      rewrite(out_f);
  135.      writeln(out_f,'nff');                    {Header}
  136.      writeln(out_f,'version 2.0');
  137.      writeln(out_f,input_file);               {Object Name}
  138.      readln(in_f,n1);                         {Number of vertices}
  139.      writeln(out_f,n1);
  140.      for c:=1 to n1 do                        {Transfer vertices}
  141.      begin
  142.           readln(in_f,x,y,z);
  143.           write(out_f,x,' ',y,' ',z,'   ');
  144.           if gourard then writeln(out_f,'N') else writeln(out_f);
  145.      end;
  146.      readln(in_f,n1);                         {Check digit}
  147.      readln(in_f,n1);                         {Number of faces}
  148.      writeln(out_f,n1-failed);
  149.      for c:=1 to n1 do
  150.      begin
  151.           readln(in_f,colour,bright);
  152.           readln(in_f,n2);                    {Number of points}
  153.           if n2>2 then ok:=true else ok:=false;
  154.           if ok then write(out_f,n2,' ');
  155.           for c2:=1 to n2 do
  156.           begin
  157.                read(in_f,n3);                 {Transfer points}
  158.                if ok then write(out_f,n3-1,' ');
  159.           end;
  160.           readln(in_f);
  161.           if ok then write_colours;
  162.           readln(in_f,n2);                    {Check digit}
  163.      end;
  164.      readln(in_f,n1);                         {Check digit}
  165.      close(in_f);
  166.      close(out_f);
  167.      writeln('Conversion completed');
  168. end.
  169.  
  170.  
  171.