unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, GLMisc, GLScene, StdCtrls, GLWin32Viewer, GLObjects,
  ComCtrls, GLGeomObjects,Math, GLCadencer,Keyboard, GLNavigator,jpeg,
  Menus, GLTexture, GLTilePlane, GLVectorFileObjects,GLFile3DS,GraphicEx,
  inifiles, GLFileMD2, GLSkydome, GLShadowVolume;

type
  TForm1 = class(TForm)
    ListaObjetos: TListBox;
    GLScene1: TGLScene;
    Panel1: TPanel;
    Splitter1: TSplitter;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Splitter2: TSplitter;
    Splitter3: TSplitter;
    GLSceneViewer1: TGLSceneViewer;
    GLSceneViewer2: TGLSceneViewer;
    GLSceneViewer3: TGLSceneViewer;
    GLSceneViewer4: TGLSceneViewer;
    Splitter4: TSplitter;
    navega: TGLDummyCube;
    Efectos: TGLDummyCube;
    Actor: TGLDummyCube;
    Escenario: TGLDummyCube;
    StatusBar1: TStatusBar;
    Luz1: TGLLightSource;
    GLCone1: TGLCone;
    GLCadencer1: TGLCadencer;
    GLCamera4: TGLCamera;
    MainMenu1: TMainMenu;
    Archivo1: TMenuItem;
    Abrir1: TMenuItem;
    Guardar1: TMenuItem;
    N1: TMenuItem;
    Salir1: TMenuItem;
    SaveDialog1: TSaveDialog;
    OpenDialog1: TOpenDialog;
    PopupMenu1: TPopupMenu;
    Nuevoobjeto1: TMenuItem;
    Geometriabasica1: TMenuItem;
    Panel5: TPanel;
    Panel6: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Luces: TGLDummyCube;
    Editarobjeto1: TMenuItem;
    Borrarobjeto1: TMenuItem;
    Label3: TLabel;
    Label4: TLabel;
    N2: TMenuItem;
    Resetearescalado1: TMenuItem;
    Resetearposicion1: TMenuItem;
    Editarcolortextura1: TMenuItem;
    GLMaterialLibrary1: TGLMaterialLibrary;
    LibreriadeMateriales1: TMenuItem;
    Cargar1: TMenuItem;
    Abrir2: TMenuItem;
    Guardar2: TMenuItem;
    test1: TMenuItem;
    Rotar1: TMenuItem;
    X1: TMenuItem;
    Y1: TMenuItem;
    Z1: TMenuItem;
    Ficherodemalla1: TMenuItem;
    OpenMalla: TOpenDialog;
    personaje: TGLActor;
    OpenActor: TOpenDialog;
    CamActor: TGLCamera;
    GLDisk1: TGLDisk;
    GLSkyDome1: TGLSkyDome;
    Cap_Luz1: TMenuItem;
    GLCamera1: TGLCamera;
    GLCamera2: TGLCamera;
    GLCamera3: TGLCamera;
    col_personaje: TGLSphere;
    procedure GLSceneViewer3MouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
      WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
    procedure HandleKeys(const deltaTime: Double);
    procedure GLCadencer1Progress(Sender: TObject; const deltaTime,
      newTime: Double);
    procedure AjustarCamaras();
    procedure Guardar1Click(Sender: TObject);
    procedure CargaObjetosEscena();
    procedure Abrir1Click(Sender: TObject);
    procedure Geometriabasica1Click(Sender: TObject);
    procedure ListaObjetosClick(Sender: TObject);
    procedure Borrarobjeto1Click(Sender: TObject);
    procedure Editarobjeto1Click(Sender: TObject);
    procedure Editarcolortextura1Click(Sender: TObject);
    procedure Resetearescalado1Click(Sender: TObject);
    procedure Abrir2Click(Sender: TObject);
    procedure Cargar1Click(Sender: TObject);
    procedure X1Click(Sender: TObject);
    procedure Y1Click(Sender: TObject);
    procedure Z1Click(Sender: TObject);
    procedure Ficherodemalla1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure test1Click(Sender: TObject);
  private
    { Private declarations }
{    efectos: TGLBaseSceneObject;
    actor: TGLBaseSceneObject;
    escenario: TGLBaseSceneObject;

    camara1: TGLCamera;
    camara2: TGLCamera;
    camara3:TGLCamera; }
    camara4:TGLCamera;
    activo: TGLBaseSceneObject;
    lista_freeforms:TStrings;
    actor_file:string;
  public
    { Public declarations }
    mx : integer;
    my : integer;
    DistanciaaObjeto:Single;
  end;

var
  Form1: TForm1;

implementation

uses Unit2, Unit3, Unit4;

{$R *.dfm}

procedure TForm1.GLSceneViewer3MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
   if ssLeft in Shift then
      GLCamera4.MoveAroundTarget(my-y, mx-x);
   mx:=x; my:=y;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

   CargaObjetosEscena();
   lista_freeforms := TStringList.Create;	

   activo := navega;
   DistanciaaObjeto := activo.DistanceTo(GLCamera4);
   AjustarCamaras();
   {
   DistanciaaObjeto := activo.DistanceTo(GLCamera1);
   GLCamera1.Position.x := -DistanciaaObjeto;
   GLCamera2.Position.y := DistanciaaObjeto;
   GLCamera3.Position.z := DistanciaaObjeto;
   }
   
   //activo.Add
   //GLSceneViewer3.Camera := camara4;
  // GlScene1.Cameras.Remove(GLCamera4,true);

end;

procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin

	//GLCamera1.AdjustDistanceToTarget(Power(1.1, WheelDelta/120));
	//GLCamera2.AdjustDistanceToTarget(Power(1.1, WheelDelta/120));
	//GLCamera3.AdjustDistanceToTarget(Power(1.1, WheelDelta/120));
	GLCamera4.AdjustDistanceToTarget(Power(1.1, WheelDelta/120));
  DistanciaaObjeto := activo.DistanceTo(GLCamera4);
  AjustarCamaras();
  //GLCamera4.AdjustDistanceToTarget(DistanciaaObjeto);
  {
  DistanciaaObjeto := DistanciaaObjeto + WheelDelta/(120*8);
  if DistanciaaObjeto >=0 then
     GLCamera5.Position.X := DistanciaaObjeto;
  DistanciaaObjeto := GLCamera5.Position.X; }
end;

procedure TForm1.AjustarCamaras();
begin
   GlCamera1.TargetObject := activo;
   GlCamera2.TargetObject := activo;
   GlCamera3.TargetObject := activo;
   GlCamera4.TargetObject := activo;

   GLCamera1.Position.x := activo.Position.X -DistanciaaObjeto;
   GLCamera1.Position.y := activo.Position.y;
   GLCamera1.Position.z := activo.Position.z;

   GLCamera2.Position.x := activo.Position.x;
   GLCamera2.Position.y := (activo.Position.y + DistanciaaObjeto)*(-1);
   GLCamera2.Position.z := activo.Position.z + 0.001;

   GLCamera3.Position.x := activo.Position.x;
   GLCamera3.Position.y := activo.Position.y;
//   if GLCamera3.Position.y < 0 then
//     GLCamera3.Position.y := 0.1;
   GLCamera3.Position.z := activo.Position.z +DistanciaaObjeto;

   statusbar1.Panels.Items[0].Text:='c1:'+
    Format('%f',[(GLCamera1.Position.x)])+':'+Format('%f',[(GLCamera1.Position.y)])+':'+Format('%f',[(GLCamera1.Position.z)]) + ' c2:'+
    Format('%f',[(GLCamera2.Position.x)])+':'+Format('%f',[(GLCamera2.Position.y)])+':'+Format('%f',[(GLCamera2.Position.z)]) +' c3:'+
    Format('%f',[(GLCamera3.Position.x)])+':'+Format('%f',[(GLCamera3.Position.y)])+':'+Format('%f',[(GLCamera3.Position.z)]);
end;

procedure TForm1.HandleKeys(const deltaTime: Double);
var
   moving : String;
   avance : Single;
   angulo : single;
begin
   avance:=0.004;
   angulo := 0.1;
   if IsKeyDown(VK_SHIFT) then
   begin
       avance := avance *10;
       angulo := angulo *8;
   end;
   // This function uses asynchronous keyboard check (see Keyboard.pas)
  // if IsKeyDown(VK_ESCAPE) then Close;

   // Move Actor in the scene

   // if nothing specified, we are standing

   // are we advaning/backpedaling ?
   if IsKeyDown(VK_UP) then begin
     if IsKeyDown(VK_CONTROL) then
     begin
         activo.Position.y := activo.Position.y -avance;
         GLCamera4.Position.y := GLCamera4.Position.y -avance;

     end else
     if IsKeyDown(VK_MENU) then
     begin
        activo.Pitch(-angulo);
     end else
     begin
      activo.Position.z := activo.Position.z -avance;
      GLCamera4.Position.z := GLCamera4.Position.z -avance;
     end;
      //GLNavigator1.MoveForward(cWalkStep*boost);
   end;
   if IsKeyDown(VK_DOWN) then begin
     if IsKeyDown(VK_CONTROL) then
     begin
        activo.Position.y := activo.Position.y +avance;
        GLCamera4.Position.y := GLCamera4.Position.y +avance;

     end else
     if IsKeyDown(VK_MENU) then
     begin
        activo.Pitch(angulo);
     end else
     begin
       activo.Position.z := activo.Position.z +avance;
       GLCamera4.Position.z := GLCamera4.Position.z +avance;

      //GLNavigator1.MoveForward(-cWalkStep*boost);
     end;
   end;

   // slightly more complex, depending on CTRL key, we either turn or strafe
   if IsKeyDown(VK_LEFT) then begin
      if IsKeyDown(VK_CONTROL) then
          //GLNavigator1.StrafeHorizontal(-cStrafeStep*boost)
          activo.Turn(angulo)
      else
      if IsKeyDown(VK_MENU) then
      begin
       activo.Roll(-angulo);
      end else
      begin
         //GLNavigator1.TurnHorizontal(-cRotAngle*boost);
       activo.Position.X := activo.Position.X +avance;
       GLCamera4.Position.x := GLCamera4.Position.x +avance;

      end;
   end;
   if IsKeyDown(VK_RIGHT) then begin
      if IsKeyDown(VK_CONTROL) then
          //GLNavigator1.StrafeHorizontal(cStrafeStep*boost)
         activo.Turn(-angulo)
      else
      if IsKeyDown(VK_MENU) then
      begin
       activo.Roll(angulo);
      end else
      begin
          //GLNavigator1.TurnHorizontal(cRotAngle*boost);
        activo.Position.X := activo.Position.X -avance;
       GLCamera4.Position.x := GLCamera4.Position.x -avance;
      end;
   end;

   if IsKeyDown('+') or IsKeyDown(VK_ADD) then
   begin
        activo.Scale.x := activo.Scale.x +(avance /6);
        activo.Scale.y := activo.Scale.y +(avance/6);
        activo.Scale.z := activo.Scale.z +(avance/6);
   end;
   if IsKeyDown('-') or IsKeyDown(VK_SUBTRACT)  then
   begin
        activo.Scale.x := activo.Scale.x -(avance/6);
        activo.Scale.y := activo.Scale.y -(avance/6);
        activo.Scale.z := activo.Scale.z -(avance/6);
   end ;


   AjustarCamaras();

end;

procedure TForm1.GLCadencer1Progress(Sender: TObject; const deltaTime,
  newTime: Double);
begin
   HandleKeys(deltaTime);
   GLSceneViewer1.Invalidate;
end;

procedure TForm1.Guardar1Click(Sender: TObject);
var
  nombre, path:string;
  material_file: string;
  IniFile : TIniFile;
  objeto :TGLBaseSceneObject;
  a : integer;
begin
    if (savedialog1.Execute) then
    begin
       if ExtractFileExt(SaveDialog1.FileName) = ''then
          nombre := SaveDialog1.FileName + '.scn'
       else
          nombre := SaveDialog1.FileName;
          
       if fileexists(nombre) then
         if (messagedlg('El fichero ya esxiste, ¿desea sobreescribirlo?',mtConfirmation, [mbYes, mbNo], 0)=mrNo) then
           exit;
       activo.ShowAxes := false;
       GlScene1.SaveToFile(ChangeFileExt(nombre,'.gls'));
       material_file := ChangeFileExt(nombre,'.glm');
       GlMaterialLibrary1.SaveToFile(material_file);
       IniFile := TIniFile.Create(ChangeFileExt(nombre,'.scn'));
       IniFile.WriteString('General', 'File',ChangeFileExt(ExtractFileName(nombre),'.gls'));
       IniFile.WriteString('General', 'Materials', ExtractFileName(material_file));

       //fichero actores
       IniFile.WriteString('General', 'Actor_File', ExtractFileName(actor_file));
       if (actor_file <> '') then
           CopyFile(PChar(actor_file),PChar(path+ExtractFileName(actor_file)),true);
       if (FileExists(ChangeFileExt(actor_file,'.jpg'))) then
           CopyFile(PChar(ChangeFileExt(actor_file,'.jpg')),PChar(path+ExtractFileName(ChangeFileExt(actor_file,'.jpg'))),true);


       //Numero de mallas
       //IniFile.WriteInteger('General', 'Number_of_Mallas', );

       path := ExtractFilePath(nombre);
       for a := 0 to lista_freeforms.Count-1 do
       begin
           objeto := TGLFreeForm(lista_freeforms.objects[a]);
           nombre := lista_freeforms.Strings[a];

           if not FileExists(path+ExtractFileName(nombre)) then
              CopyFile(PChar(nombre),PChar(path+ExtractFileName(nombre)),true);
           IniFile.WriteString('Malla'+inttostr(a),'File',ExtractFileName(nombre));
           IniFile.WriteString('Malla'+inttostr(a), 'Name',objeto.Name);
       end;
       IniFile.WriteInteger('General', 'Number_of_Mallas', lista_freeforms.Count);
       IniFile.Free;
    end;

end;

procedure TForm1.CargaObjetosEscena();
var
   efectos, actor, escenario,luces: TGLBaseSceneObject;
   a:integer;
   name:string;
begin
   //Selecciona navega como el objeto activo
   activo := navega;
   label2.Caption := 'navega';

   //Posiciona las camaras de las vistas en sus posiciones iniciales
   {
   GLCamera1.Position.x := activo.Position.x -3;
   GLCamera1.Position.y := activo.Position.y;
   GLCamera1.Position.z := activo.Position.z;
   DistanciaaObjeto := activo.DistanceTo(GLCamera1);
   GLCamera1.Position.x := -DistanciaaObjeto;
   GLCamera2.Position.y := DistanciaaObjeto;
   GLCamera3.Position.z := DistanciaaObjeto;
    }
   //Caga la lista de objetos
   ListaObjetos.Items.Clear;
   ListaObjetos.Items.Add('navega');
   efectos := glscene1.Objects.FindChild('Efectos',false);
   actor := glscene1.Objects.FindChild('Actor',false);
   escenario := glscene1.Objects.FindChild('Escenario',false);
   luces := glscene1.Objects.FindChild('Luces',false);
   for a := 0 to efectos.Count-1 do
      ListaObjetos.Items.Add(efectos.Children[a].Name);
   {
   for a := 0 to actor.Count-1 do
   begin
      name := actor.Children[a].Name;
      if (name <> 'CamActor') then
        ListaObjetos.Items.Add(Name);
   end;
   }
   name := actor.Name;
   ListaObjetos.Items.Add(name);
   for a := 0 to escenario.Count-1 do
      ListaObjetos.Items.Add(escenario.Children[a].Name);
   for a := 0 to luces.Count-1 do
      ListaObjetos.Items.Add(luces.Children[a].Name);
end;

procedure TForm1.Abrir1Click(Sender: TObject);
var
  //MatFile:string;
  i:integer;
  ScnFile  : TIniFile;
  FileGLS, FileGLM,FileActor,MallaFile,MallaName:string;
  NumMallas: integer;
  malla : TGLFreeForm;
begin
   //glscene1.Objects.DeleteChildren;
    if (opendialog1.Execute) then
    begin
       ScnFile := TIniFile.Create(OpenDialog1.FileName);
       FileGLS := ScnFile.ReadString('General','File','');
       FileGLM := ScnFile.ReadString('General','Materials','');
       FileActor := ScnFile.ReadString('General','Actor_File','');
       NumMallas := ScnFile.ReadInteger('General','Number_of_Mallas',0);


       //MatFile := ChangeFileExt(opendialog1.FileName,'.glm');
       {
       if (not FileExists(MatFile)) then
       begin
         showmessage('Fichero '+FileGLM+' no encontrado.');
        // exit;
       end;
       }
       glmateriallibrary1.LoadFromFile(FileGLM);
       // Pone todoas las texturas como tmDecal, ya que esto no se guarda
       //espero se guarde en nuevas versiones
       for i := 0 to glmateriallibrary1.Materials.Count-1 do
         glmateriallibrary1.Materials.Items[i].Material.Texture.TextureMode := tmModulate;
       //
       glscene1.LoadFromFile(FileGLS);

     //Carga las mallas
     for i := 0 to  NumMallas-1 do
     begin
         MallaFile := ScnFile.ReadString('Malla'+IntToStr(i),'File','');
         MallaName := ScnFile.ReadString('Malla'+IntToStr(i),'Name','');
         malla := TGLFreeForm(GLScene1.Objects.FindChild(MallaName,false));
         malla.LoadFromFile(MallaFile);
         //TGLFreeForm(GLScene1.Objects.FindChild(MallaName,false)).Scale.SetVector(0.03, 0.03, 0.03, 0);
         lista_freeforms.AddObject(MallaFile,malla);
     end;
     
     //Carga del personaje
     actor_file := FileActor;
     if FileActor <> '' then
        TGLActor(GLScene1.Objects.FindChild('personaje',false)).LoadFromFile(FileActor);


       CargaObjetosEscena();
    end;
end;

procedure TForm1.Geometriabasica1Click(Sender: TObject);
begin
    FormGeoBasic.showmodal;
end;

procedure TForm1.ListaObjetosClick(Sender: TObject);
var
   ObjIndice:Integer;
   objeto:TGLBaseSceneObject;
   NombreObjeto:string;
begin
    ObjIndice := ListaObjetos.ItemIndex;
    if (ObjIndice = -1) then
       exit; //No hay objetos seleccionados
    NombreObjeto := ListaObjetos.Items.Strings[ObjIndice];
    objeto := glscene1.Objects.FindChild(NombreObjeto,false);
    if objeto <> nil then
    begin
        activo.ShowAxes := false;
        activo := objeto;
        activo.ShowAxes := true;
        label2.Caption := NombreObjeto;
        AjustarCamaras();
    end;
    if objeto is TGLCube then label4.Caption:='Cubo';
    if objeto is TGLSphere then label4.Caption:='Esfera';
    if objeto is TGLCone then label4.Caption:='Cono';
    if objeto is TGLCylinder then label4.Caption:='Cilindro';
    if objeto is TGLPlane then label4.Caption:='Plano';
    if objeto is TGLLightSource then label4.Caption:='Luz';

    if NombreObjeto = 'navega' then label4.Caption:='Navegador';

end;

procedure TForm1.Borrarobjeto1Click(Sender: TObject);
var
   ObjIndice:Integer;
   objeto:TGLBaseSceneObject;
   NombreObjeto:string;
begin
    ObjIndice := ListaObjetos.ItemIndex;
    if (ObjIndice = -1) then
       exit; //No hay objetos seleccionados
    NombreObjeto := ListaObjetos.Items.Strings[ObjIndice];
    if (NombreObjeto='navega') then
    begin
       ShowMessage('El objeto navega no puede ser borrado');
       exit;
    end;
    if (NombreObjeto='Actor') then
    begin
       ShowMessage('El objeto personaje no puede ser borrado');
       exit;
    end;
    if (messagedlg('¿Desea borrar el objeto '+NombreObjeto+'?',mtConfirmation, [mbYes, mbNo], 0)=mrNo) then
       exit;
    objeto := glscene1.Objects.FindChild(NombreObjeto,false);
    if objeto = activo  then
    begin
        activo.ShowAxes := false;
        activo := navega;
        activo.ShowAxes := true;
        label2.Caption := NombreObjeto;
        AjustarCamaras();
    end;
    objeto :=  efectos.FindChild(NombreObjeto,true);
    if objeto <> nil then begin
      efectos.Remove(objeto,false);
      objeto.Destroy;      
    end;
    objeto :=  escenario.FindChild(NombreObjeto,true);
    if objeto <> nil then begin
      if objeto is TGLFreeForm then
      begin
          lista_freeforms.Delete(lista_freeforms.IndexOfObject(objeto));
      end;
      escenario.Remove(objeto,false);
      objeto.Destroy;
    end;
    objeto :=  luces.FindChild(NombreObjeto,true);
    if objeto <> nil then begin
      luces.Remove(objeto,false);
      objeto.Destroy;
    end;
    objeto :=  actor.FindChild(NombreObjeto,true);
    if objeto <> nil then begin
      actor.Remove(objeto,false);
      objeto.Destroy;
    end;
    objeto := nil;

    ListaObjetos.Items.Delete(ObjIndice);

end;

procedure TForm1.Editarobjeto1Click(Sender: TObject);
var
   ObjIndice:Integer;
   objeto:TGLBaseSceneObject;
   NombreObjeto:string;
begin
    ObjIndice := ListaObjetos.ItemIndex;
    if (ObjIndice = -1) then
       exit; //No hay objetos seleccionados
    NombreObjeto := ListaObjetos.Items.Strings[ObjIndice];
    objeto := glscene1.Objects.FindChild(NombreObjeto,false);
    if (objeto is TGLCube) then
    begin
       PropObj.cubo := TGLCube(objeto);
       PropObj.showmodal;
    end;
    if (objeto is TGLCone) then
    begin
       PropObj.cono := TGLCone(objeto);
       PropObj.showmodal;
    end;
    if (objeto is TGLPlane) then
    begin
       PropObj.plano := TGLPlane(objeto);
       PropObj.showmodal;
    end;
    if (objeto is TGLCylinder) then
    begin
       PropObj.cilindro := TGLCylinder(objeto);
       PropObj.showmodal;
    end;
    if (objeto is TGLSphere) then
    begin
       PropObj.esfera := TGLSphere(objeto);
       PropObj.showmodal;
    end;
end;

procedure TForm1.Editarcolortextura1Click(Sender: TObject);
var
   ObjIndice:Integer;
   objeto:TGLBaseSceneObject;
   NombreObjeto:string;   
begin
    ObjIndice := ListaObjetos.ItemIndex;
    if (ObjIndice = -1) then
       exit; //No hay objetos seleccionados
    NombreObjeto := ListaObjetos.Items.Strings[ObjIndice];
    objeto := glscene1.Objects.FindChild(NombreObjeto,false);

    materialform.MatDirecto := true;
    if (objeto is TGLCube) then
    begin
      materialform.MaterialAct := TGLCube(objeto).Material;
      materialform.showmodal;
    end;
    if (objeto is TGLCone) then
    begin
      materialform.MaterialAct := TGLCone(objeto).Material;
      materialform.showmodal;
    end;
    if (objeto is TGLSphere) then
    begin
      materialform.MaterialAct := TGLSphere(objeto).Material;
      materialform.showmodal;
    end;
    if (objeto is TGLCylinder) then
    begin
      materialform.MaterialAct := TGLCylinder(objeto).Material;
      materialform.showmodal;
    end;
    if (objeto is TGLPlane) then
    begin
      materialform.MaterialAct := TGLPlane(objeto).Material;
      materialform.showmodal;
    end;
end;

procedure TForm1.Resetearescalado1Click(Sender: TObject);
var
   obj: TGLSceneObject ;
begin
    obj := TGLSceneObject(activo);
    obj.Material := GLMaterialLibrary1.Materials.Items[0].Material;
end;

procedure TForm1.Abrir2Click(Sender: TObject);
begin
   materialform.MatDirecto := false;
   materialform.showmodal;
end;

procedure TForm1.Cargar1Click(Sender: TObject);
var
  MatFile:string;
begin
   //glscene1.Objects.DeleteChildren;
    if (opendialog1.Execute) then
    begin
       MatFile := ChangeFileExt(opendialog1.FileName,'.glm');
       if (not FileExists(MatFile)) then
       begin
         showmessage('Fichero '+MatFile+' no encontrado.');
         exit;
       end;
       glmateriallibrary1.LoadFromFile(MatFile);
       
       //glscene1.LoadFromFile(opendialog1.FileName);
       //CargaObjetosEscena();
    end;

end;

procedure TForm1.X1Click(Sender: TObject);
var
   ObjIndice:Integer;
   objeto:TGLBaseSceneObject;
   NombreObjeto:string;
   valor : single;
begin
    ObjIndice := ListaObjetos.ItemIndex;
    if (ObjIndice = -1) then
       exit; //No hay objetos seleccionados
    NombreObjeto := ListaObjetos.Items.Strings[ObjIndice];
    objeto := glscene1.Objects.FindChild(NombreObjeto,false);
    try
       objeto.RollAngle :=strtofloat(inputbox('Rotar en eje x', 'Angulo de rotacion',floattostr(objeto.RollAngle)));
    except
       exit;
    end;

end;

procedure TForm1.Y1Click(Sender: TObject);
var
   ObjIndice:Integer;
   objeto:TGLBaseSceneObject;
   NombreObjeto:string;
   valor : single;
begin
    ObjIndice := ListaObjetos.ItemIndex;
    if (ObjIndice = -1) then
       exit; //No hay objetos seleccionados
    NombreObjeto := ListaObjetos.Items.Strings[ObjIndice];
    objeto := glscene1.Objects.FindChild(NombreObjeto,false);
    try
       objeto.TurnAngle := strtofloat(inputbox('Rotar en eje y', 'Angulo de rotacion',floattostr(objeto.TurnAngle)));

    except
       exit;
    end;

end;

procedure TForm1.Z1Click(Sender: TObject);
var
   ObjIndice:Integer;
   objeto:TGLBaseSceneObject;
   NombreObjeto:string;
   valor : single;
begin
    ObjIndice := ListaObjetos.ItemIndex;
    if (ObjIndice = -1) then
       exit; //No hay objetos seleccionados
    NombreObjeto := ListaObjetos.Items.Strings[ObjIndice];
    objeto := glscene1.Objects.FindChild(NombreObjeto,false);
    try
       objeto.PitchAngle := strtofloat(inputbox('Rotar en eje z', 'Angulo de rotacion',floattostr(objeto.PitchAngle)));

    except
       exit;
    end;

end;

procedure TForm1.Ficherodemalla1Click(Sender: TObject);
var
    malla:TGLFreeForm;
    nombre :string;
begin
   if (openmalla.Execute) then
   begin
      nombre := InputBox('Nombre del Objeto','Nombre del objeto:','');
      while ((nombre='')or(form1.ListaObjetos.Items.IndexOf(nombre)<>-1)) do
         //Ya existe un objeto con ese nombre
          nombre := InputBox('Ya existe un objeto con ese nombre','Ya existe un objeto con ese nombre, escriba otro nombre:','');
       if nombre = '' then exit;

       malla:=TGLFreeForm.Create(nil);
       malla.MaterialLibrary := glmateriallibrary1;
       malla.LoadFromFile(openmalla.FileName);
       malla.Position.SetPoint(0,0,0);
       malla.Scale.SetVector(0.02, 0.02, 0.02, 0);
       malla.Up.SetVector(0,0.0001,1,0);
       malla.Name :=nombre;
       lista_freeforms.AddObject(openmalla.FileName,malla);
       escenario.AddChild(malla);
       ListaObjetos.Items.Add(nombre);
       ListaObjetos.ItemIndex := ListaObjetos.Items.Count-1;
       ListaObjetosClick(nil);
   end;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
    lista_freeforms.Free;
end;

procedure TForm1.test1Click(Sender: TObject);
begin
      if openactor.Execute then
      begin
          personaje.LoadFromFile(openactor.FileName);
          personaje.Scale.SetVector(0.03, 0.03, 0.03, 0);
          actor_file := openactor.FileName;
          if (FileExists(ChangeFileExt(actor_file,'.jpg'))) then
          begin
              personaje.Material.Texture.Image.LoadFromFile(ChangeFileExt(actor_file,'.jpg'));
              personaje.Material.Texture.Disabled := false;
          end;

      end;
end;

end.
