uses crt,dos;

var asc,wrl:text;

DirInfo: SearchRec;

FileName:string[12];

line,NObj,LA:string;

Nvertex,Nfaces,i:longint;

X,Y,Z,Rr,Gg,Bb,SpotLtoX,SpotLtoY,SpotLtoZ,CutOffSpotLZ,LensAngle:real;

A,B,C:longint;

LineNo:longint;

LPosX,LPosY,LPosZ:real;

spotL,outL:boolean;

cameras,lights,logo:boolean;

 

function GetReal(s:string):real;

var ss:string;

i:integer;

r:real;

begin

ss:='';

i:=1;

while (i<=byte(s[0])) and (s[i]=' ') do inc(i);

while (i<=byte(s[0])) and (s[i] in ['0'..'9','-','.']) do begin

ss:=ss+s[i];

inc(i);

end;

val(ss,r,i);

GetReal:=r;

end;

 

function GetLong(s:string):longint;

var ss:string;

i,r:integer;

begin

ss:='';

i:=1;

while (i<=byte(s[0])) and (s[i]=' ') do inc(i);

while (i<=byte(s[0])) and (s[i] in ['0'..'9','-','.']) do begin

ss:=ss+s[i];

inc(i);

end;

val(ss,r,i);

GetLong:=r;

end;

function GetName(s:string):string;

var i:integer;

ss:string;

begin

ss:='';

i:=pos('"',s);inc(i);

while (i<=byte(s[0])) and (s[i]<>'"') do begin

ss:=ss+s[i];

inc(i);

end;

GetName:=ss;

end;

 

begin

clrscr;

if ParamCount=0 then begin

Writeln('Usage : asc2wrl <file[.asc]> [parameter1] [parameter2] ...');

Writeln('parameters:');

Writeln('/cameras:on');

Writeln('/cameras:off');

Writeln('/lights:on');

Writeln('/lights:off');

Writeln('/logo:on');

Writeln('/logo:off');

halt(1);

end;

FileName:=paramstr(1);

if Pos('.',FileName)>0 then FileName:=copy(FileName,1,Pos('.',FileName)-1);

FindFirst(FileName+'.asc', AnyFile, DirInfo);

if DosError<>0 then begin

Writeln('Could not find file: '+FileName+'.asc, please try again !');

halt(1);

end;

writeln('3ds to Vrml converter, Copyright (c) 1997 by Baboi Daniel.');

writeln;

writeln('Converting '+DirInfo.Name+' to '+FileName+'.wrl');

writeln('Processed lines : ');

cameras:=true;

lights:=true;

logo:=true;

for i:=2 to paramcount do begin

if (paramstr(i)='/cameras:on')or(paramstr(i)='/CAMERAS:ON') then cameras:=true

else if (paramstr(i)='/cameras:off')or(paramstr(i)='/CAMERAS:OFF') then cameras:=false

else if (paramstr(i)='/lights:on')or(paramstr(i)='/LIGHTS:ON') then lights:=true

else if (paramstr(i)='/lights:off')or(paramstr(i)='/LIGHTS:OFF')then lights:=false

else if (paramstr(i)='/logo:on')or(paramstr(i)='/LOGO:ON') then logo:=true

else if (paramstr(i)='/logo:off')or(paramstr(i)='/LOGO:OFF') then logo:=false;

end;

assign(asc,DirInfo.Name);

assign(wrl,FileName+'.wrl');

rewrite(wrl);

writeln(wrl,'#VRML V1.0 ascii');

writeln(wrl,'Separator {');

if logo then begin

writeln(wrl,' Info {');

writeln(wrl,' string "Created using the 3ds to Vrml converter done by Baboi Daniel"');

writeln(wrl,' }');

writeln(wrl,' Info {');

writeln(wrl,' string "You can reach converter'#39's author at: adir@alpha.comp-craiova.ro"');

writeln(wrl,' }');

end;

writeln(wrl,' Rotation { rotation 1 0 0 3.1415 }');

reset(asc);

Nobj:='';

LineNo:=1;

Nfaces:=1;

readln(asc,line);inc(LineNo);

while not eof(asc) do begin

if copy(line,1,13)='Named object:' then begin

Nobj:=GetName(line);

readln(asc,line);inc(LineNo);

end else

{begin Camera}

if (copy(line,1,6)='Camera') and (cameras) then begin

LA:='';

i:=pos('(',line);inc(i);

while (i<=byte(line[0])) and (line[i] in ['0'..'9','-','.']) do begin

LA:=LA+line[i];

inc(i);

end;

LensAngle:=(3.14142 * GetReal(LA))/180;

outL:=false;

repeat

readln(asc,line);inc(LineNo);

if copy(line,1,9)='Position:' then begin

X:=GetReal(copy(line,Pos('X:',line)+2,10));

Y:=GetReal(copy(line,Pos('Y:',line)+2,10));

Z:=GetReal(copy(line,Pos('Z:',line)+2,10));

end else

if copy(line,1,7)='Target:' then begin

SpotLtoX:=GetReal(copy(line,Pos('X:',line)+2,10));

SpotLtoY:=GetReal(copy(line,Pos('Y:',line)+2,10));

SpotLtoZ:=GetReal(copy(line,Pos('Z:',line)+2,10));

end else begin

outL:=true;

writeln(wrl,' DEF '+Nobj+' PerspectiveCamera {');

writeln(wrl,' position ',X:4:3,' ',Y:4:3,' ',Z:4:3);

writeln(wrl,' focalDistance ',sqrt(sqr(SpotLtoX-X)+sqr(SpotLtoY-Y)+sqr(Z)):6:5);

writeln(wrl,' orientation ',SpotLtoX+X:4:3,' ',SpotLtoY+Y:4:3,' ',SpotLtoZ+Z:4:3,' ',0);

writeln(wrl,' heightAngle ',LensAngle:4:5);

writeln(wrl,' }');

end;

until outL;

gotoxy(19,4);write(LineNo:5);

end

{begin Camera}

else

{begin LUMINI}

if (copy(line,1,12)='Direct light') and lights then begin

spotL:=false;

outL:=false;

repeat

readln(asc,line);inc(LineNo);

if copy(line,1,9)='Position:' then begin

LPosX:=GetReal(copy(line,Pos('X:',line)+2,10));

LPosY:=GetReal(copy(line,Pos('Y:',line)+2,10));

LPosZ:=GetReal(copy(line,Pos('Z:',line)+2,10));

end else

if copy(line,1,12)='Light color:' then begin

Rr:=GetReal(copy(line,Pos('Red=',line)+4,10));

Gg:=GetReal(copy(line,Pos('Green=',line)+6,10));

Bb:=GetReal(copy(line,Pos('Blue=',line)+5,10));

end else

if copy(line,1,13)='Spotlight to:' then begin

SpotLtoX:=GetReal(copy(line,Pos('X:',line)+2,10));

SpotLtoY:=GetReal(copy(line,Pos('Y:',line)+2,10));

SpotLtoZ:=GetReal(copy(line,Pos('Z:',line)+2,10));

spotL:=true;

end else

if (copy(line,1,13)='Hotspot size:')or

(copy(line,1,13)='Falloff size:') then begin

CutOffSpotLZ:=GetReal(copy(line,15,10));

spotL:=true;

end else begin

outL:=true;

if spotL then begin

writeln(wrl,' DEF '+Nobj+' SpotLight {');

writeln(wrl,' color ',Rr:4:3,' ',Gg:4:3,' ',Bb:4:3);

writeln(wrl,' location ',LPosX:4:3,' ',LPosY:4:3,' ',LPosZ:4:3);

writeln(wrl,' direction ',SpotLtoX:4:3,' ',SpotLtoY:4:3,' ',SpotLtoZ:4:3);

writeln(wrl,' cutOffAngle ',pi*CutOffSpotLZ/180:4:5);

writeln(wrl,' }');

end else begin

writeln(wrl,' DEF '+Nobj+' PointLight {');

writeln(wrl,' color ',Rr:4:3,' ',Gg:4:3,' ',Bb:4:3);

writeln(wrl,' location ',LPosX:4:3,' ',LPosY:4:3,' ',LPosZ:4:3);

writeln(wrl,' }');

end;

end;

until outL;

gotoxy(19,4);write(LineNo:5);

end

{end LUMINI}

else

{begin OBIECTE }

if copy(line,1,20)='Tri-mesh, Vertices: ' then begin

writeln(wrl,' DEF '+Nobj+' Separator{');

writeln(wrl,' Coordinate3{');

writeln(wrl,' point[');

Nvertex:=GetLong(copy(line,21,10));

Nfaces:=GetLong(copy(line,pos('Faces:',line)+6,15));

repeat

readln(asc,line);inc(LineNo);

until copy(line,1,12)='Vertex list:';

for i:=0 to Nvertex-2 do begin

readln(asc,line);inc(LineNo);

X:=GetReal(copy(line,Pos('X:',line)+2,14));

Y:=GetReal(copy(line,Pos('Y:',line)+2,14));

Z:=GetReal(copy(line,Pos('Z:',line)+2,14));

write(wrl,' ',X:4:3,' ');

write(wrl,Y:4:3,' ');

writeln(wrl,Z:4:3,',');

end;

readln(asc,line);inc(LineNo);

X:=GetReal(copy(line,Pos('X:',line)+2,15));

Y:=GetReal(copy(line,Pos('Y:',line)+2,15));

Z:=GetReal(copy(line,Pos('Z:',line)+2,15));

write(wrl,' ',X:4:3,' ');

write(wrl,Y:4:3,' ');

writeln(wrl,Z:4:3,' ]');

writeln(wrl,' }');

repeat

readln(asc,line);inc(LineNo);

until copy(line,1,10)='Face list:';

writeln(wrl,' IndexedFaceSet {');

writeln(wrl,' coordIndex[');

for i:=0 to Nfaces-2 do begin

repeat

readln(asc,line);inc(LineNo);

until copy(line,1,5)='Face ';

A:=GetLong(copy(line,Pos('A:',line)+2,10));

B:=GetLong(copy(line,Pos('B:',line)+2,10));

C:=GetLong(copy(line,Pos('C:',line)+2,10));

write(wrl,' ',A,',');

write(wrl,B,',');

writeln(wrl,C,',-1,');

end;

repeat

readln(asc,line);inc(LineNo);

until copy(line,1,5)='Face ';

A:=GetLong(copy(line,Pos('A:',line)+2,10));

B:=GetLong(copy(line,Pos('B:',line)+2,10));

C:=GetLong(copy(line,Pos('C:',line)+2,10));

write(wrl,' ',A,',');

write(wrl,B,',');

writeln(wrl,C,',-1 ]');

writeln(wrl,' }');

readln(asc,line);inc(LineNo);

gotoxy(19,4);write(LineNo:5);

writeln(wrl,' }');

end

{end OBIECTE }

else begin

readln(asc,line);inc(LineNo);

gotoxy(19,4);write(LineNo:5);

end;

end;

writeln(wrl,'}');

writeln;

writeln('Done !');

close(asc);

close(wrl);

end.

Go Back