{$N+}
program transform_4;
uses crt;

type nod=record
      name:string[20];
      ambient:array [1..3] of real;
      difuse:array [1..3] of real;
      specular:array [1..3] of real;
      shininess:real;
      transparency:real;
      texture:string;
     end;
     fisier=file of nod;

var st,s,x,y,z:string;
    te,out:text;
    i,j,k,l,f,v,ll:integer;
    u,di:array [1..3] of real;
    dac,t:boolean;
    v1,v2,v3: array [1..5000] of single;
    nrv:integer;
    name_mli,out_file:string;
    fis:fisier;
    a:nod;

procedure trans(y:string);
var ss:string;
    t:integer;
begin
inc(nrv);
y:=copy(y,2,length(y)-1);
ss:=copy(y,1,pos(' ',y)-1);
val(ss,v1[nrv],t);
y:=copy(y,pos(' ',y)+1,length(y)-pos(' ',y));
ss:=copy(y,1,pos(' ',y)-1);
val(ss,v2[nrv],t);
y:=copy(y,pos(' ',y)+1,length(y)-pos(' ',y));
val(y,v3[nrv],t);
end;

begin
if paramcount=3 then
                begin
                st:=paramstr(1);
                name_mli:=paramstr(2);
                out_file:=paramstr(3);
                end
                else
                begin
                writeln('   That program translates from the .asc file and a material file ');
                writeln('(which which was before translated by you, using transmli) ');
                writeln('to a wrl file.');
                writeln('   The syntax is transfer <input_name.asc> <input_library.ext> <output_name.wrl>');
                halt;
                end;
assign(te,st);
reset(te);
assign(out,'tmp.tmp');
rewrite(out);
assign(fis,name_mli);
repeat
readln(te,s);
if (s<>'') and (pos('Page',s)=0) then writeln(out,s);
until eof(te);
close(te);
close(out);
erase(te);  {filtru pentru liniile goale sau inutile}
rename(out,st);
reset(te);
assign(out,out_file);
rewrite(out);
writeln(out,'#VRML V1.0 ascii');
writeln(out,'Separator {');
writeln(out,'Info {');
writeln(out,'string "Created using the 3ds to Vrml converter done by Marius Dumitru"');
writeln(out,' }');
writeln(out,'Info {');
writeln(out,'string "You can reach converter''s author at: gaman@topedge.com"');
writeln(out,' }');
repeat
z:=st;
readln(te,st);
if pos('Tri-mesh',st)<>0 then
                         begin
                         i:=length('Tri-mesh, Vertices: ');
                         st:=copy(st,i+1,length(st)-i);
                         i:=pos(' ',st);
                         s:=copy(st,1,i-1);
                         val(s,v,j); {nr de vertices}
                         j:=length(st);
                         while st[j]<>' ' do
                           dec(j);
                         s:=copy(st,j+1,length(st)-j);
                         val(s,f,j); {nr de faces}
                         repeat
                         readln(te,st);
                         until pos('Vertex list',st)<>0;
                         dac:=true;
                         nrv:=0;
                         l:=0;
                         repeat
                         readln(te,st);
                         if pos('Vertex',st)<>0 then
			    begin
                            for i:=1 to length(st) do
                                if st[i]=':' then
                                begin
                                x:=copy(st,i+1,length(st)-i+1);
                                i:=length(st);
                                end;
                            k:=0;y:='';
                            for i:=1 to length(x) do
                                if k<4 then
                                begin
                                if (x[i]>='0') and (x[i]<='9') or (x[i]='-') then y:=y+x[i];
                                if (x[i]=' ') and (y[length(y)]<>' ') then begin inc(k);y:=y+' ';end;
                                if (x[i]='.') then y:=y+'.';
                                end;
                            inc(l);
                            trans(y);
                            end;
                         until l=v;
                         ll:=0;
                         s:='';
                         repeat
                         readln(te,st);
                         if (pos('Face',st)<>0) and (st[6] in ['0'..'9'])
                            then
                            begin
                            y:='';
                            i:=pos('A',st);
                            j:=pos('B',st);
                            k:=pos('C',st);
                            l:=pos('AB',st);
                            y:=copy(st,i+2,j-i-3)+','+copy(st,j+2,k-j-3)+','+copy(st,k+2,l-k-3)+',-1';
                            st:='';
                            inc(ll);
                            readln(te,x);
                            if pos('Material',x)<>0 then
                               x:=copy(x,pos('"',x)+1,length(x)-pos('"',x)-1)
                               else
                               x:='No name';
                            if dac then
                               begin
                               dac:=false;
                               writeln(out,'Separator{ #'+z);
                               if x<>'No name'
                                  then
                                  begin
                                  reset(fis);
                                  t:=false;
                                  for i:=1 to filesize(fis) do
                                      begin
                                          read(fis,a);
                                          if a.name=x
                                             then
                                             begin
                                             t:=true;
                                             i:=filesize(fis);
                                             end;
                                      end;
                                  close(fis);
                                  if t
                                     then
                                     begin
                                     writeln(out,'Material { #'+x);
                                     writeln(out,'ambientColor ',a.ambient[1]:2:3,' ',a.ambient[2]:2:3,' ',a.ambient[3]:2:3);
                                     writeln(out,'diffuseColor ',a.difuse[1]:2:3,' ',a.difuse[2]:2:3,' ',a.difuse[3]:2:3);
                                     writeln(out,'specularColor ',a.specular[1]:2:3,' ',
                                                                a.specular[2]:2:3,' ',a.specular[3]:2:3);
                                     writeln(out,'shininess ',a.shininess:2:3);
                                     writeln(out,'transparency ',a.transparency:2:3);
                                     writeln(out,'}');
                                     if a.texture<>'NONE'
                                        then writeln(out,'Texture2 {filename "'+a.texture+'"}');
                                     end;
                                     end;
                               writeln(out,'   Coordinate3{');
                               writeln(out,'point[');
                               for i:=1 to nrv do
                                   begin
                                   write(out,v1[i]:3:5);
                                   write(out,' ');
                                   write(out,v2[i]:3:5);
                                   write(out,' ');
                                   write(out,v3[i]:3:5);
                                   if i=nrv then writeln(out,']')
                                            else writeln(out,',');
                                   end;
                               writeln(out,'}');
                               writeln(out,'IndexedFaceSet {');
                               writeln(out,'coordIndex[');
                               s:=x;
                               end
                               else
                               begin
                               if s<>x then
                                  begin
                                  writeln(out,']');
                                  writeln(out,'}');
                                  writeln(out,'}');
                                  writeln(out,'Separator{ #'+z);
                               if x<>'No name'
                                  then
                                  begin
                                  reset(fis);
                                  t:=false;
                                  for i:=1 to filesize(fis) do
                                      begin
                                          read(fis,a);
                                          if a.name=x
                                             then
                                             begin
                                             t:=true;
                                             i:=filesize(fis);
                                             end;
                                      end;
                                  close(fis);
                                  if t
                                     then
                                     begin
                                     writeln(out,'Material { #'+x);
                                     writeln(out,'ambientColor ',a.ambient[1]:2:3,' ',a.ambient[2]:2:3,' ',a.ambient[3]:2:3);
                                     writeln(out,'diffuseColor ',a.difuse[1]:2:3,' ',a.difuse[2]:2:3,' ',a.difuse[3]:2:3);
                                     writeln(out,'specularColor ',a.specular[1]:2:3,' ',
                                                                a.specular[2]:2:3,' ',a.specular[3]:2:3);
                                     writeln(out,'shininess ',a.shininess:2:3);
                                     writeln(out,'transparency ',a.transparency:2:3);
                                     writeln(out,'}');
                                     if a.texture<>'NONE'
                                        then writeln(out,'Texture2 {filename "'+a.texture+'"}');
                                     end;
                                     end;
                                  writeln(out,'   Coordinate3{');
                                  writeln(out,'point[');
                                  for i:=1 to nrv do
                                      begin
                                      write(out,v1[i]:3:5);
                                      write(out,' ');
                                      write(out,v2[i]:3:5);
                                      write(out,' ');
                                      write(out,v3[i]:3:5);
                                      if i=nrv then writeln(out,']')
                                               else writeln(out,',');
                                      end;
                                  writeln(out,'}');
                                  writeln(out,'IndexedFaceSet {');
                                  writeln(out,'coordIndex[');
                                  s:=x;
                                  end
                                  else
                                  writeln(out,',');
                               end;
                            write(out,y);  {scrie cate o fata}
                            end;
                         until ll=f;
                         writeln(out,']');
                         writeln(out,'}');
                         writeln(out,'}');
                         end;

if pos('Direct light',st)<>0 then
                             begin
                             readln(te,st);
                             j:=pos('X',st);
                             st:=copy(st,j+2,length(st)-j-1);
                             j:=1;
                             i:=0;
                             x:='';
                             while i<length(st) do
                                begin
                                inc(i);  {citeste pozitia sursei}
                                if (ord(st[i])-ord('0') in [0..9]) or (st[i]='-') or (st[i]='.') then
                                                        begin
                                                        x:=x+st[i];
                                                        end;
                                if (st[i]=' ') or (i=length(st)) then
                                            begin
                                            val(x,u[j],k);
                                            x:='';
                                            inc(j);
                                            end;
                                end;
                             readln(te,st);
                             if pos('Light color',st)<>0 then
                                begin
                                writeln(out,'PointLight { #'+z);
                                write(out,' color ');
                                i:=pos('Red',st);
                                st:=copy(st,i+4,length(st)-3);
                                i:=pos(' ',st);
                                x:=copy(st,1,i-1);
                                write(out,x+' ');
                                st:=copy(st,i+1,length(st)-i);
                                i:=pos(' ',st);
                                x:=copy(st,7,i-7);
                                write(out,x+' ');
                                x:=copy(st,i+6,length(st)-i-5);
                                writeln(out,x);
                                write(out,' location ');
                                writeln(out,u[1]:3:3,' ',u[2]:3:3,' ',u[3]:3:3);
                                writeln(out,'}');
                                end
                                else
                                begin
                                writeln(out,'DirectionalLight { #'+z);
                                j:=pos('X',st);
                                st:=copy(st,j+2,length(st)-j-1);
                                j:=1;
                                i:=0;
                                x:='';
                                while i<length(st) do
                                  begin
                                  inc(i);  {citeste directia sursei}
                                  if (ord(st[i])-ord('0') in [0..9]) or (st[i]='-') or (st[i]='.') then
                                                        begin
                                                        x:=x+st[i];
                                                        end;
                                  if (st[i]=' ') or (i=length(st)) then
                                            begin
                                            val(x,di[j],k);
                                            x:='';
                                            inc(j);
                                            end;
                                  end;
                                readln(te,st);{cu unghiul de hotspot}
                                readln(te,st);{cu unghiul de falloff}
                                readln(te,st);
                                write(out,' color ');
                                i:=pos('Red',st);
                                st:=copy(st,i+4,length(st)-3);
                                i:=pos(' ',st);
                                x:=copy(st,1,i-1);
                                write(out,x+' ');
                                st:=copy(st,i+1,length(st)-i);
                                i:=pos(' ',st);
                                x:=copy(st,7,i-7);
                                write(out,x+' ');
                                x:=copy(st,i+6,length(st)-i-5);
                                writeln(out,x);
                                write(out,' direction ');
                                writeln(out,u[1]-di[1]:3:3,' ',u[2]-di[2]:3:3,' ',u[3]-di[3]:3:3);
                                writeln(out,'}');
                                end;
                             end;


until eof(te);
writeln(out,'}');
close(te);
close(out);
end.
