unit udrzwi;




interface

uses GLScene, StdCtrls, GLObjects,glmesh,meshutils,  SysUtils,
GLVectorFileObjects,vectortypes,vectorlists,gltexture,umklogger;

type XYZ=record x,y,z:real; end;


type tdrzwi=class
public
zrodlo:tglfreeform;
baza:tglbasesceneobject;
l1,l2,r1,r2:tgldummycube;
l1m,l2m,r1m,r2m:tglfreeform;
l1s,l2s,r1s,r2s:tglfreeform;
//druk:tmemo;
L,P:boolean;
sp,alfa,max,min,kier:real;
st:byte;
typ:byte;
szyba:tglmaterial;
szybki:tgldummycube;
log:tmklogger;
constructor create;
procedure zrob(s:string);

function minx(s:string):XYZ;
function znazwy(s:string):integer;
function maxx(s:string):XYZ;

procedure otworz;
procedure zamknij;
procedure odwr;
function sprawdz(s:string):integer;

procedure rusz(dt:double);
end;

implementation

constructor tdrzwi.create;
begin
        sp:=35;
        max:=0;
        min:=-170;
        alfa:=0;
        st:=0;
        L:=false;
        P:=false;
        typ:=1;
      //  log:=tmklogger.create('drzwi.txt');
      //  log.czysc;

end;


function tdrzwi.znazwy(s:string):integer;
var i,ret:integer;
begin
ret:=-1;
    //    log.wriln('==='+s+' '+inttostr(zrodlo.meshobjects.count-1));
        for i:=0 to zrodlo.meshobjects.count-1 do begin
     //           log.wriln(inttostr(i)+':'+zrodlo.MeshObjects[i].Name+':');
                if zrodlo.meshobjects[i].name=s then ret:=i;
                end;
    //    log.wriln('---'+s+' '+inttostr(ret));
        znazwy:=ret;
end;

function tdrzwi.minx(s:string):XYZ;
var
i,j:integer;
tm:XYZ;
begin
        tm.z:=100000000;tm.x:=100000000;tm.y:=100000000;
        for i:=0 to zrodlo.meshobjects.count-1 do begin
                if zrodlo.meshobjects[i].name=s then
                for j:=0 to zrodlo.meshobjects[i].vertices.count-1 do begin
                        if tm.y>zrodlo.meshobjects[i].vertices[j][1] then begin
                                tm.x:=zrodlo.meshobjects[i].vertices[j][0];
                                tm.y:=zrodlo.meshobjects[i].vertices[j][1];
                                tm.z:=zrodlo.meshobjects[i].vertices[j][2];
                               // druk.lines.add(format('%f %f %f',[tm.x, tm.y, tm.z]));
                        end;

                end;

        end;
        minx:=tm;
end;


function tdrzwi.maxx(s:string):XYZ;
var
i,j:integer;
tm:XYZ;
begin
        tm.z:=-100000000;tm.x:=-100000000;tm.y:=-100000000;
        for i:=0 to zrodlo.meshobjects.count-1 do begin
                if zrodlo.meshobjects[i].name=s then
                for j:=0 to zrodlo.meshobjects[i].vertices.count-1 do begin
                        if tm.y<zrodlo.meshobjects[i].vertices[j][1] then begin
                                tm.x:=zrodlo.meshobjects[i].vertices[j][0];
                                tm.y:=zrodlo.meshobjects[i].vertices[j][1];
                                tm.z:=zrodlo.meshobjects[i].vertices[j][2];
                                //druk.lines.add(format('%f %f %f',[tm.x, tm.y, tm.z]));
                        end;

                end;

        end;
        maxx:=tm;
end;

procedure tdrzwi.otworz;
begin
kier:=1;
st:=2;
end;

procedure tdrzwi.zamknij;
begin
kier:=-1;
st:=1;
end;

procedure tdrzwi.odwr;
begin
if st<2 then otworz else zamknij;
end;

procedure tdrzwi.rusz(dt:double);
begin
alfa:=alfa+kier*sp*dt;
if typ=1 then begin
if alfa>85 then begin kier:=0; alfa:=85; st:=4; end;
if alfa<0 then begin kier:=0; alfa:=0; st:=0; end;
        if L then begin
        l1.rollangle:=alfa;
        l2.rollangle:=-2*alfa;
        end;
        if P then begin
        r1.rollangle:=-alfa;
        r2.rollangle:=2*alfa;
        end;
end;
if typ=2 then begin
        if alfa>165 then begin kier:=0; alfa:=165; st:=4; end;
        if alfa<0 then begin kier:=0; alfa:=0; st:=0; end;
        if L then begin
        l1.rollangle:=-alfa;
        l2.rollangle:=alfa;
        end;
        if P then begin
        r1.rollangle:=alfa;
        r2.rollangle:=-alfa;
        end;
end;
         if L then begin
        l1s.absoluteposition:=l1m.absoluteposition;
        l1s.AbsoluteDirection:=l1m.AbsoluteDirection;
        l1s.AbsoluteUp:=l1m.Absoluteup;
        l2s.absoluteposition:=l2m.absoluteposition;
        l2s.AbsoluteDirection:=l2m.AbsoluteDirection;
        l2s.AbsoluteUp:=l2m.Absoluteup;
        end;


         if P then begin
        r1s.absoluteposition:=r1m.absoluteposition;
        r1s.AbsoluteDirection:=r1m.AbsoluteDirection;
        r1s.AbsoluteUp:=r1m.Absoluteup;
        r2s.absoluteposition:=r2m.absoluteposition;
        r2s.AbsoluteDirection:=r2m.AbsoluteDirection;
        r2s.AbsoluteUp:=r2m.Absoluteup;
        end;

end;

procedure tdrzwi.zrob(s:string);
var ofs:XYZ;
ve:tvector3f;
 lObj:TMeshObject;
  t1, n1 : TAffineVectorList;

begin

if (znazwy(s+'l1_1')>=0) and
   (znazwy(s+'l2_1')>=0) then L:=true;
if (znazwy(s+'r1_1')>=0) and
   (znazwy(s+'r2_1')>=0) then P:=true;

if L then begin
        l1:=tgldummycube.Create(baza.AddNewChild(tgldummycube));  l1.parent:=baza;
        l2:=tgldummycube.Create(l1.AddNewChild(tgldummycube));  l2.parent:=l1;
        l1m:=tglfreeform.Create(l1.addnewchild(tglfreeform));l1m.Parent:=l1;
        l2m:=tglfreeform.Create(l2.addnewchild(tglfreeform)); l2m.Parent:=l2;
        l1s:=tglfreeform.Create(l1.addnewchild(tglfreeform)); l1s.Parent:=szybki;
        l2s:=tglfreeform.Create(l2.addnewchild(tglfreeform)); l2s.Parent:=szybki;
        l1m.MaterialLibrary:=zrodlo.MaterialLibrary;
        l2m.MaterialLibrary:=zrodlo.MaterialLibrary;
        try
        l2m.Material:=zrodlo.MaterialLibrary.LibMaterialByName(zrodlo.MeshObjects[znazwy(s+'l2_1')].FaceGroups[0].materialname).material;
        l1m.Material:=zrodlo.MaterialLibrary.LibMaterialByName(zrodlo.MeshObjects[znazwy(s+'l1_1')].FaceGroups[0].materialname).material;
        except
        end;

        l1s.Material:=szyba;
        l2s.Material:=szyba;


       //lewa czesc
        ofs:=minx(s+'l1_1');

      //  l1m.material.name:=zrodlo.materialname;

        lObj := TMeshObject.CreateOwned(l1m.MeshObjects);
        t1   := TAffineVectorList.create;
        n1   := TAffineVectorList.create;

       lObj.Vertices:= zrodlo.MeshObjects[znazwy(s+'l1_1')].ExtractTriangles(t1,n1);
        lobj.Normals:=n1;
        lobj.TexCoords:=t1;

        ve[0]:=-ofs.x;
        ve[1]:=-ofs.y;
        ve[2]:=0;
        l1.Position.x:=ofs.x;
        l1.Position.y:=ofs.y;
        l1.Position.z:=0;



        lobj.Vertices.Translate(ve);


//szyba lewa

if znazwy(s+'l1_2')>=0 then begin
        lObj := TMeshObject.CreateOwned(l1s.MeshObjects);
        t1   := TAffineVectorList.create;
        n1   := TAffineVectorList.create;
        lObj.Vertices := zrodlo.MeshObjects[znazwy(s+'l1_2')].ExtractTriangles(t1,n1);
        lobj.Normals:=n1;
        lobj.TexCoords:=t1;
        ve[0]:=-ofs.x;
        ve[1]:=-ofs.y;
        ve[2]:=0;
        lobj.Vertices.Translate(ve);
        zrodlo.MeshObjects[znazwy(s+'l1_2')].Free;
end;

//prawa
        ofs:=maxx(s+'l1_1');
        lObj := TMeshObject.CreateOwned(l2m.MeshObjects);
        t1   := TAffineVectorList.create;
        n1   := TAffineVectorList.create;
        lObj.Vertices := zrodlo.MeshObjects[znazwy(s+'l2_1')].ExtractTriangles(t1,n1);
        lobj.Normals:=n1;
        lobj.TexCoords:=t1;
        ve[0]:=-ofs.x;
        ve[1]:=-ofs.y;
        ve[2]:=0;
        l2.Position.x:=ofs.x-l1.position.x;
        l2.Position.y:=ofs.y-l1.position.y;
        l2.Position.z:=0;

        lobj.Vertices.Translate(ve);

//szyba prawa
if znazwy(s+'l2_2')>=0 then begin
        lObj := TMeshObject.CreateOwned(l2s.MeshObjects);
        t1   := TAffineVectorList.create;
        n1   := TAffineVectorList.create;
        lObj.Vertices := zrodlo.MeshObjects[znazwy(s+'l2_2')].ExtractTriangles(t1,n1);
        lobj.Normals:=n1;
        lobj.TexCoords:=t1;
        ve[0]:=-ofs.x;
        ve[1]:=-ofs.y;
        ve[2]:=0;
        lobj.Vertices.Translate(ve);
        zrodlo.MeshObjects[znazwy(s+'l2_2')].Free;
end;




        zrodlo.MeshObjects[znazwy(s+'l1_1')].Free;
        zrodlo.MeshObjects[znazwy(s+'l2_1')].Free;

end;
if P then begin
        r1:=tgldummycube.Create(baza.AddNewChild(tgldummycube));  r1.parent:=baza;
        r2:=tgldummycube.Create(r1.AddNewChild(tgldummycube));  r2.parent:=r1;
        r1m:=tglfreeform.Create(r1.addnewchild(tglfreeform)); r1m.Parent:=r1;
        r2m:=tglfreeform.Create(r2.addnewchild(tglfreeform)); r2m.Parent:=r2;
        r1s:=tglfreeform.Create(r1.addnewchild(tglfreeform)); r1s.Parent:=szybki;
        r2s:=tglfreeform.Create(r2.addnewchild(tglfreeform)); r2s.Parent:=szybki;
        r1m.MaterialLibrary:=zrodlo.MaterialLibrary;
        r2m.MaterialLibrary:=zrodlo.MaterialLibrary;
        try
        r2m.Material:=zrodlo.MaterialLibrary.LibMaterialByName(zrodlo.MeshObjects[znazwy(s+'r2_1')].FaceGroups[0].materialname).material;
        r1m.Material:=zrodlo.MaterialLibrary.LibMaterialByName(zrodlo.MeshObjects[znazwy(s+'r1_1')].FaceGroups[0].materialname).material;
        except
        end;

        r1s.Material:=szyba;
        r2s.Material:=szyba;

        ofs:=maxx(s+'r1_1');
        lObj := TMeshObject.CreateOwned(r1m.MeshObjects);
        t1   := TAffineVectorList.create;
        n1   := TAffineVectorList.create;
        lObj.Vertices := zrodlo.MeshObjects[znazwy(s+'r1_1')].ExtractTriangles(t1,n1);
        lobj.Normals:=n1;
        lobj.TexCoords:=t1;
        ve[0]:=-ofs.x;
        ve[1]:=-ofs.y;
        ve[2]:=0;
        r1.Position.x:=ofs.x;
        r1.Position.y:=ofs.y;
        r1.Position.z:=0;
        lobj.Vertices.Translate(ve);

 //szyba lewa

if znazwy(s+'r1_2')>=0 then begin
        lObj := TMeshObject.CreateOwned(r1s.MeshObjects);
        t1   := TAffineVectorList.create;
        n1   := TAffineVectorList.create;
        lObj.Vertices := zrodlo.MeshObjects[znazwy(s+'r1_2')].ExtractTriangles(t1,n1);
        lobj.Normals:=n1;
        lobj.TexCoords:=t1;
        ve[0]:=-ofs.x;
        ve[1]:=-ofs.y;
        ve[2]:=0;
        lobj.Vertices.Translate(ve);
        zrodlo.MeshObjects[znazwy(s+'r1_2')].Free;
 end;


 //prawa
        ofs:=minx(s+'r1_1');

        lObj := TMeshObject.CreateOwned(r2m.MeshObjects);
        t1   := TAffineVectorList.create;
        n1   := TAffineVectorList.create;
        lObj.Vertices := zrodlo.MeshObjects[znazwy(s+'r2_1')].ExtractTriangles(t1,n1);
        lobj.Normals:=n1;
        lobj.TexCoords:=t1;
        ve[0]:=-ofs.x;
        ve[1]:=-ofs.y;
        ve[2]:=0;
        r2.Position.x:=ofs.x-r1.position.x;
        r2.Position.y:=ofs.y-r1.position.y;
        r2.Position.z:=0;

        lobj.Vertices.Translate(ve);

//szyba prawa

if znazwy(s+'r2_2')>=0 then begin
        lObj := TMeshObject.CreateOwned(r2s.MeshObjects);
        t1   := TAffineVectorList.create;
        n1   := TAffineVectorList.create;
        lObj.Vertices := zrodlo.MeshObjects[znazwy(s+'r2_2')].ExtractTriangles(t1,n1);
        lobj.Normals:=n1;
        lobj.TexCoords:=t1;
        ve[0]:=-ofs.x;
        ve[1]:=-ofs.y;
        ve[2]:=0;
        lobj.Vertices.Translate(ve);
        zrodlo.MeshObjects[znazwy(s+'r2_2')].Free;
end;




        zrodlo.MeshObjects[znazwy(s+'r1_1')].Free;
        zrodlo.MeshObjects[znazwy(s+'r2_1')].Free;


end;

end;

function tdrzwi.sprawdz(s:string):integer;
var ret:integer;
begin
ret:=-1;
if (znazwy(s+'l1_1')>=0) and
   (znazwy(s+'l2_1')>=0) then ret:=1;
if (znazwy(s+'r1_1')>=0) and
   (znazwy(s+'r2_1')>=0) then ret:=2;
   sprawdz:=ret;
end;

end.
