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

  1. program test;
  2.  
  3. var temp_string, input_file, output_file : string;
  4.     fin, fout : text;
  5.     string_length, hash_pos, mode, temp, vertices, faces, count: integer;
  6.     vertex_x, vertex_y, vertex_z, scale : real;
  7.  
  8. type
  9.     plg_face_vertexptr=^plg_face_vertex;
  10.     plg_face_vertex=record
  11.        vertex_num : integer;
  12.        next : plg_face_vertexptr;
  13.     end;
  14.  
  15. function empty_string(this_string : string) : boolean;
  16. begin
  17.      for count:=1 to length(this_string) do
  18.      begin
  19.           if ((this_string[count]>='0') and (this_string[count]<='9'))
  20.           or ((this_string[count]>='a') and (this_string[count]<='z'))
  21.           or ((this_string[count]>='A') and (this_string[count]<='Z'))
  22.           then begin
  23.                     empty_string:=false;
  24.                     exit;
  25.           end;
  26.      end;
  27.      empty_string:=true;
  28. end;
  29.  
  30. function find_string_integer(var string_integer: integer; this_string : string; var start_point : integer) : boolean;
  31. var pos, temp_int : integer;
  32.     counting,found,negative : boolean;
  33. begin
  34.      if start_point>length(this_string) then
  35.      begin
  36.           find_string_integer:=false;
  37.           exit;
  38.      end;
  39.      temp_int:=0;
  40.      pos:=start_point-1;
  41.      found:=false;
  42.      counting:=false;
  43.      negative:=false;
  44.      repeat
  45.            inc(pos);
  46.            if (this_string[pos]>='0') and (this_string[pos]<='9') then
  47.            begin
  48.                 counting:=true;
  49.                 temp_int:=temp_int*10+(ord(this_string[pos])-48);
  50.            end
  51.            else
  52.            begin
  53.                 if (this_string[pos]='-') then
  54.                 begin
  55.                      if not(counting) and ((this_string[pos+1]>='0') and (this_string[pos]<='9')) then
  56.                      begin
  57.                           counting:=true;
  58.                           negative:=true;
  59.                      end;
  60.                 end
  61.                 else if counting then found:=true;
  62.            end;
  63.      until (pos=length(this_string)) or found;
  64.      if ((pos=length(this_string)) and counting and not(found)) then
  65.      begin
  66.           found:=true;
  67.           pos:=pos+1;
  68.      end;
  69.      start_point:=pos;
  70.      if found then
  71.         if not(negative) then string_integer:=temp_int else string_integer:=-1*temp_int;
  72.      find_string_integer:=found;
  73. end;
  74.  
  75. function find_string_real(var string_real: real; this_string : string; var start_point : integer) : boolean;
  76. var old_count, int1, int2, power_count, power : integer;
  77.     junk : boolean;
  78. begin
  79.      if not(find_string_integer(int1,this_string,start_point)) then
  80.      begin
  81.           find_string_real:=false;
  82.           exit;
  83.      end;
  84.      if (((start_point+1)>length(this_string)) or (this_string[start_point]<>'.')) then
  85.      begin
  86.           string_real:=int1;
  87.           find_string_real:=true;
  88.           exit;
  89.      end;
  90.      if ((this_string[start_point]='.') { and ((this_string[start_point+1]>='0') and (this_string[start_point+1]<='9'))} )
  91.      then begin
  92.                old_count:=start_point;
  93.                junk:=find_string_integer(int2,this_string,start_point);
  94.      end;
  95.      power:=1;
  96.      for power_count:=(old_count+2) to start_point do power:=power*10;
  97.      if int1>0 then string_real:=int1+(int2/power)
  98.                else string_real:=int1-(int2/power);
  99.      find_string_real:=true;
  100. end;
  101.  
  102. function hex_to_dec(char_in : char) : integer;
  103. begin
  104.      if ((char_in>='0') and (char_in<='9')) then
  105.      begin
  106.           hex_to_dec:=ord(char_in)-48;
  107.           exit;
  108.      end;
  109.      if ((char_in>='a') and (char_in<='f')) then
  110.      begin
  111.           hex_to_dec:=ord(char_in)-87;
  112.           exit;
  113.      end;
  114.      if ((char_in>='A') and (char_in<='F')) then
  115.      begin
  116.           hex_to_dec:=ord(char_in)-55;
  117.           exit;
  118.      end;
  119.      writeln('Error - not a valid hex number!');
  120.      halt;
  121. end;
  122.  
  123.  
  124. procedure read_face_data(var face_type, face_hue : word; var face_brightness : real);
  125. var temp_char : char;
  126.     temp_int : integer;
  127. begin
  128.      count:=1;
  129.      while ((count<(length(temp_string)-5)) and (temp_string[count]<>'0')) do inc(count);
  130.      if (count<(length(temp_string)-5)) and (temp_string[count+1]='x') then
  131.      begin
  132.           face_type:=ord(temp_string[count+2])-48;
  133.           if face_type=8 then
  134.           begin
  135.                writeln('Warning - a user defined surface has been encountered!');
  136.                writeln('          The surface has been redefined as colour 0, brightness 1');
  137.                face_hue:=0;
  138.           end
  139.           else
  140.           begin
  141.                if ((face_type<0) or (face_type>3)) then
  142.                begin
  143.                     writeln('Error - type is outside range 0 to 3!');
  144.                     halt;
  145.                end;
  146.                face_hue:=hex_to_dec(temp_string[count+3]);
  147.           end;
  148.           temp_int:=(hex_to_dec(temp_string[count+4])*16+hex_to_dec(temp_string[count+5]));
  149.           if face_type=0 then
  150.           begin
  151.                if (face_hue=0) then
  152.                begin
  153.                     face_hue:=temp_int div 16;
  154.                     face_brightness:=(temp_int mod 16)/15;
  155.                end
  156.                else
  157.                    face_brightness:=temp_int/255;
  158.           end
  159.           else
  160.               if face_type=1 then
  161.                   face_brightness:=temp_int/255
  162.               else
  163.                   face_brightness:=1;
  164.           count:=count+6;
  165.      end
  166.      else
  167.      begin
  168.           count:=1;
  169.           if not(find_string_integer(temp_int,temp_string,count)) then
  170.           begin
  171.                writeln('Error! - Can''t find face data');
  172.                halt;
  173.           end;
  174.           face_type:=temp_int DIV 4096;
  175.           temp_int:=temp_int MOD 4096;
  176.           face_hue:=temp_int DIV 256;
  177.           temp_int:=temp_int MOD 256;
  178.           if face_type=8 then
  179.           begin
  180.                writeln('Warning - a user defined surface has been encountered!');
  181.                writeln('          The surface has been redefined as colour 0, brightness 1');
  182.                face_hue:=0;
  183.           end
  184.           else
  185.           begin
  186.                if ((face_type<0) or (face_type>3)) then
  187.                begin
  188.                     writeln('Error - type is outside range 0 to 3!');
  189.                     halt;
  190.                end;
  191.           end;
  192.           if face_type=0 then
  193.           begin
  194.                if (face_hue=0) then
  195.                begin
  196.                     face_hue:=temp_int div 16;
  197.                     face_brightness:=(temp_int mod 16)/15;
  198.                end
  199.                else
  200.                    face_brightness:=temp_int/255;
  201.           end
  202.           else
  203.               if face_type=1 then
  204.                   face_brightness:=temp_int/255
  205.               else
  206.                   face_brightness:=1;
  207.      end;
  208. end;
  209.  
  210. procedure initial_data;
  211. begin
  212.      count:=1;
  213.      if not(find_string_integer(vertices,temp_string,count)) then
  214.      begin
  215.           writeln('Error - can''t find number of vertices!');
  216.           halt;
  217.      end;
  218.      if not(find_string_integer(faces,temp_string,count)) then
  219.      begin
  220.           writeln('Error - can''t find number of faces!');
  221.           halt;
  222.      end;
  223.      if (find_string_integer(temp,temp_string,count)) then
  224.      begin
  225.           writeln('Error - too many numbers on PLG title line!');
  226.           halt;
  227.      end;
  228.      writeln('Vertices = ',vertices,' : Faces = ',faces);
  229. end;
  230.  
  231. procedure read_vertex;
  232. begin
  233.      count:=1;
  234.      if not(find_string_real(vertex_x,temp_string,count)) then
  235.      begin
  236.           writeln('Error - can''t find x vertex!');
  237.           halt;
  238.      end;
  239.      if not(find_string_real(vertex_y,temp_string,count)) then
  240.      begin
  241.           writeln('Error - can''t find y vertex!');
  242.           halt;
  243.      end;
  244.      if not(find_string_real(vertex_z,temp_string,count)) then
  245.      begin
  246.           writeln('Error - can''t find z vertex!');
  247.           halt;
  248.      end;
  249.      if (find_string_integer(temp,temp_string,count)) then
  250.      begin
  251.           writeln('Error - extra coordinate on vertex');
  252.           halt;
  253.      end;
  254. end;
  255.  
  256.  
  257. procedure read_face;
  258. var f_type, f_hue : word;
  259.     f_brightness : real;
  260.     num_vertices, temp_count : integer;
  261.     first_face_vertex, temp_face_vertex : plg_face_vertexptr;
  262. begin
  263.      count:=1;
  264.      first_face_vertex:=nil;
  265.      read_face_data(f_type, f_hue, f_brightness);
  266.      if not(find_string_integer(num_vertices,temp_string,count)) then
  267.      begin
  268.           writeln('Error - can''t find number of vertices in face!');
  269.           halt;
  270.      end;
  271.      if num_vertices=2 then writeln('Warning - a single line has been encountered!');
  272.      if num_vertices=1 then writeln('Warning - a single point has been encountered!');
  273.      writeln(fout,f_hue,' ',f_brightness : 2);
  274.      writeln(fout,num_vertices);
  275.      temp_count:=num_vertices;
  276.      while temp_count>0 do
  277.      begin
  278.           if not(find_string_integer(temp,temp_string,count)) then
  279.           begin
  280.                writeln('Error - run out of face vertices!');
  281.                halt;
  282.           end;
  283.           new(temp_face_vertex);
  284.           temp_face_vertex^.vertex_num:=temp+1;
  285.           temp_face_vertex^.next:=first_face_vertex;
  286.           first_face_vertex:=temp_face_vertex;
  287.           dec(temp_count);
  288.      end;
  289. {     if (find_string_integer(temp,temp_string,count)) then
  290.      begin
  291.           writeln(' = ',num_vertices,' Bad vertex = ',temp);
  292.           writeln('Error - too many face vertices!');
  293.           halt;
  294.      end;
  295. }     temp_face_vertex:=first_face_vertex;
  296.      while temp_face_vertex<>nil do
  297.      begin
  298.           write(fout,temp_face_vertex^.vertex_num,'       ');
  299.           temp_face_vertex:=temp_face_vertex^.next;
  300.      end;
  301.      writeln(fout);
  302.      writeln(fout,0);
  303. end;
  304.  
  305.  
  306. begin
  307.      mode:=1;  {1 = looking for initial info}
  308.      write('PLG Input File : ');
  309.      readln(input_file);
  310.      writeln;
  311.      write('3D Output File : ');
  312.      readln(output_file);
  313.      writeln;
  314.      repeat
  315.            write('Scale Factor : ');
  316.            readln(scale);
  317.            if scale<=0 then writeln('Error - scale must be greater than 0 - please re-enter');
  318.      until scale>0;
  319.      writeln;
  320.      assign(fin,input_file);
  321.      reset(fin);
  322.      assign(fout,output_file);
  323.      rewrite(fout);
  324.      while (not(eof(fin) and (mode=4))) do
  325.      begin
  326.           readln(fin,temp_string);
  327.           string_length:=length(temp_string);
  328.           {Remove Comments}
  329.           hash_pos:=pos('#',temp_string);
  330.           if hash_pos<>0 then delete(temp_string,hash_pos,(string_length-hash_pos)+1);
  331.           if not(empty_string(temp_string)) then
  332.           begin
  333.                if mode=1 then
  334.                begin
  335.                     initial_data;
  336.                     writeln(fout,vertices);
  337.                     if vertices=0 then
  338.                     begin
  339.                          writeln('Error - object has 0 vertices!');
  340.                          halt;
  341.                     end;
  342.                     if faces=0 then
  343.                     begin
  344.                          writeln('Error - object has 0 faces!');
  345.                          halt;
  346.                     end;
  347.                     mode:=2;
  348.                end
  349.                else
  350.                if mode=2 then
  351.                begin
  352.                     read_vertex;
  353.                     dec(vertices);
  354.                     writeln(fout,vertex_x*scale,'       ',vertex_y*scale,'       ',vertex_z*scale);
  355.                     if vertices=0 then
  356.                     begin
  357.                          mode:=3;
  358.                          writeln(fout,0);
  359.                          writeln(fout,faces);
  360.                     end;
  361.                end
  362.                else
  363.                if mode=3 then
  364.                begin
  365.                     read_face;
  366.                     dec(faces);
  367.                     if faces=0 then mode:=4;
  368.                end;
  369.           end;
  370.      end;
  371.      writeln(fout,0);
  372.      close(fout);
  373.      close(fin);
  374.      if mode<>4 then writeln('Error - file finished prematurely!');
  375. end.