unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, StdCtrls, GLObjects, GLScene, GLMisc,
  GLWin32Viewer, Spin,GLTexture,GLGeomObjects, ExtDlgs;

type
  TMaterialForm = class(TForm)
    ColorDialog1: TColorDialog;
    col1_1: TPanel;
    ListBox1: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Label4: TLabel;
    col1_2: TPanel;
    col1_3: TPanel;
    col1_4: TPanel;
    GLScene1: TGLScene;
    GLSceneViewer1: TGLSceneViewer;
    GLCamera1: TGLCamera;
    GLLightSource1: TGLLightSource;
    GLPlane1: TGLPlane;
    GLCube1: TGLCube;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    col2_4: TPanel;
    col2_3: TPanel;
    col2_2: TPanel;
    col2_1: TPanel;
    TabSheet3: TTabSheet;
    TrackBar1: TTrackBar;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    TrackBar2: TTrackBar;
    Label12: TLabel;
    Label13: TLabel;
    ComboBox1: TComboBox;
    alpha1_1: TSpinEdit;
    alpha1_2: TSpinEdit;
    alpha1_3: TSpinEdit;
    alpha1_4: TSpinEdit;
    Label14: TLabel;
    Label15: TLabel;
    alpha2_1: TSpinEdit;
    alpha2_2: TSpinEdit;
    alpha2_3: TSpinEdit;
    alpha2_4: TSpinEdit;
    Button1: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Label16: TLabel;
    Label17: TLabel;
    Bevel1: TBevel;
    labelmat: TLabel;
    Button6: TButton;
    Label18: TLabel;
    CheckBox1: TCheckBox;
    OpenPictureDialog1: TOpenPictureDialog;
    Label19: TLabel;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    Label20: TLabel;
    Label21: TLabel;
    procedure col1_1DblClick(Sender: TObject);
    procedure ActualizaMaterial();
    procedure col1_2DblClick(Sender: TObject);
    procedure col1_3DblClick(Sender: TObject);
    procedure col1_4DblClick(Sender: TObject);
    procedure col2_1DblClick(Sender: TObject);
    procedure col2_2DblClick(Sender: TObject);
    procedure col2_3DblClick(Sender: TObject);
    procedure col2_4DblClick(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure TrackBar2Change(Sender: TObject);
    procedure alpha1_1Change(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ActualizarValores();
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ListBox1DblClick(Sender: TObject);
    procedure ActualizaListMat();
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
    MaterialAct: TGLMaterial;
    MaterialList: TGlLibMaterials;
    MaterSelected: string;
    MatDirecto: boolean; //si es true se usa el material directamente sobre el objeto
  end;

var
  MaterialForm: TMaterialForm;

implementation

uses Unit1;

{$R *.dfm}

procedure TMaterialForm.col1_1DblClick(Sender: TObject);
begin
   colordialog1.Color :=  col1_1.Color;
   if colordialog1.Execute then
      col1_1.Color := colordialog1.Color;
   ActualizaMaterial;
end;

procedure TMaterialForm.ActualizaMaterial();
var
    fundido:TBlendingMode;
    imagealpha: TGLTextureImageAlpha;
    textmode:TGLTextureMode;
begin
       fundido := bmOpaque;
       case combobox1.ItemIndex of
          0: fundido := bmOpaque;
          1: fundido := bmTransparency;
          2: fundido := bmAdditive;
       end;
       glcube1.Material.BlendingMode := fundido;

       imagealpha:=tiaDefault;
       case combobox2.ItemIndex of
           0: imagealpha:=tiaDefault;
           1: imagealpha:=tiaAlphaFromIntensity;
           2: imagealpha:=tiaSuperBlackTransparent;
           3: imagealpha:=tiaLuminance;
           4: imagealpha:=tiaLuminanceSqrt;
           5: imagealpha:=tiaOpaque;
           6: imagealpha:=tiaTopLeftPointColorTransparent;
           7: imagealpha:=tiaInverseLuminance;
           8: imagealpha:=tiaInverseLuminanceSqrt;
       end;
       glcube1.Material.Texture.ImageAlpha := imagealpha;

       textmode :=tmDecal;
       case combobox3.ItemIndex of
           0: textmode:=tmDecal;
           1: textmode:=tmModulate;
           2: textmode:=tmBlend;
           3: textmode:=tmReplace;
       end;
       glcube1.Material.Texture.TextureMode := textmode;

       glcube1.Material.FrontProperties.Ambient.AsWinColor := col1_1.color;
       glcube1.Material.FrontProperties.Ambient.Alpha := alpha1_1.Value/1000;

       glcube1.Material.FrontProperties.Diffuse.AsWinColor := col1_2.color;
       glcube1.Material.FrontProperties.Diffuse.Alpha := alpha1_2.Value /1000;

       glcube1.Material.FrontProperties.Emission.AsWinColor := col1_3.color;
       glcube1.Material.FrontProperties.Emission.Alpha := alpha1_3.Value/1000;

       glcube1.Material.FrontProperties.Specular.AsWinColor := col1_4.color;
       glcube1.Material.FrontProperties.Specular.Alpha := alpha1_4.Value/1000;

       glcube1.Material.FrontProperties.Shininess := trackbar2.Position;

       glcube1.Material.BackProperties.Ambient.AsWinColor := col2_1.color;
       glcube1.Material.BackProperties.Ambient.Alpha := alpha2_1.Value/1000;

       glcube1.Material.BackProperties.Diffuse.AsWinColor := col2_2.color;
       glcube1.Material.BackProperties.Diffuse.Alpha := alpha2_2.Value/1000;

       glcube1.Material.BackProperties.Emission.AsWinColor := col2_3.color;
       glcube1.Material.BackProperties.Emission.Alpha := alpha2_3.Value/1000;

       glcube1.Material.BackProperties.Specular.AsWinColor := col2_4.color;
       glcube1.Material.BackProperties.Specular.Alpha := alpha2_4.Value/1000;

       glcube1.Material.BackProperties.Shininess := trackbar1.Position;

       MaterialAct := glcube1.Material;
       MaterialAct.Texture.ImageBrightness :=1;
       MaterialAct.Texture.ImageGamma := 1;
      

end;

procedure TMaterialForm.ActualizarValores();
begin
    case MaterialAct.BlendingMode of
        bmOpaque : combobox1.ItemIndex := 0;
        bmTransparency : combobox1.ItemIndex := 1;
        bmAdditive : combobox1.ItemIndex := 2;
    end;

   case MaterialAct.Texture.TextureMode of
        tmDecal: combobox3.ItemIndex := 0 ;
        tmModulate: combobox3.ItemIndex := 1 ;
        tmBlend: combobox3.ItemIndex :=2;
        tmReplace: combobox3.ItemIndex :=3;
   end;
   case MaterialAct.Texture.ImageAlpha of
        tiaDefault: combobox2.ItemIndex:=0;
        tiaAlphaFromIntensity: combobox2.ItemIndex:=1;
        tiaSuperBlackTransparent: combobox2.ItemIndex:=2;
        tiaLuminance: combobox2.ItemIndex:=3;
        tiaLuminanceSqrt: combobox2.ItemIndex:=4;
        tiaOpaque: combobox2.ItemIndex:=5;
        tiaTopLeftPointColorTransparent: combobox2.ItemIndex:=6;
        tiaInverseLuminance: combobox2.ItemIndex:=7;
        tiaInverseLuminanceSqrt: combobox2.ItemIndex:=8;
    end;


   //Textura
   checkbox1.Checked := not materialact.Texture.Disabled;
   //edit1.Text := materialact.Texture.Image.GetNamePath;
   label19.Caption := '';

       col1_1.color := MaterialAct.FrontProperties.Ambient.AsWinColor;
       alpha1_1.Value := Trunc(MaterialAct.FrontProperties.Ambient.Alpha *1000);

       col1_2.color := MaterialAct.FrontProperties.Diffuse.AsWinColor;
       alpha1_2.Value := Trunc(MaterialAct.FrontProperties.Diffuse.Alpha * 1000);

       col1_3.color := MaterialAct.FrontProperties.Emission.AsWinColor;
       alpha1_3.Value := Trunc(MaterialAct.FrontProperties.Emission.Alpha * 1000);

       col1_4.color := MaterialAct.FrontProperties.Specular.AsWinColor;
       alpha1_4.Value := Trunc(MaterialAct.FrontProperties.Specular.Alpha * 1000);

       trackbar2.Position := MaterialAct.FrontProperties.Shininess;

       col2_1.color := MaterialAct.BackProperties.Ambient.AsWinColor;
       alpha2_1.Value := Trunc(MaterialAct.BackProperties.Ambient.Alpha * 1000);

       col2_2.color := MaterialAct.BackProperties.Diffuse.AsWinColor;
       alpha2_2.Value := Trunc(MaterialAct.BackProperties.Diffuse.Alpha * 1000);

       col2_3.color := MaterialAct.BackProperties.Emission.AsWinColor;
       alpha2_3.Value := Trunc(MaterialAct.BackProperties.Emission.Alpha * 1000);

       col2_4.color := MaterialAct.BackProperties.Specular.AsWinColor;
       alpha2_4.Value := Trunc(MaterialAct.BackProperties.Specular.Alpha * 1000);

       trackbar1.Position := MaterialAct.BackProperties.Shininess;

       glcube1.Material := MaterialAct;
end;

procedure TMaterialForm.col1_2DblClick(Sender: TObject);
begin
   colordialog1.Color :=  col1_2.Color;
   if colordialog1.Execute then
      col1_2.Color := colordialog1.Color;
   ActualizaMaterial;
end;

procedure TMaterialForm.col1_3DblClick(Sender: TObject);
begin
   colordialog1.Color :=  col1_3.Color;
   if colordialog1.Execute then
      col1_3.Color := colordialog1.Color;
   ActualizaMaterial;
end;

procedure TMaterialForm.col1_4DblClick(Sender: TObject);
begin
   colordialog1.Color :=  col1_4.Color;
   if colordialog1.Execute then
      col1_4.Color := colordialog1.Color;
   ActualizaMaterial;
end;

procedure TMaterialForm.col2_1DblClick(Sender: TObject);
begin
   colordialog1.Color :=  col2_1.Color;
   if colordialog1.Execute then
      col2_1.Color := colordialog1.Color;
   ActualizaMaterial;
end;

procedure TMaterialForm.col2_2DblClick(Sender: TObject);
begin
   colordialog1.Color :=  col2_2.Color;
   if colordialog1.Execute then
      col2_2.Color := colordialog1.Color;
   ActualizaMaterial;
end;

procedure TMaterialForm.col2_3DblClick(Sender: TObject);
begin
   colordialog1.Color :=  col2_3.Color;
   if colordialog1.Execute then
      col2_3.Color := colordialog1.Color;
   ActualizaMaterial;
end;

procedure TMaterialForm.col2_4DblClick(Sender: TObject);
begin
   colordialog1.Color :=  col2_4.Color;
   if colordialog1.Execute then
      col2_4.Color := colordialog1.Color;
   ActualizaMaterial;
end;

procedure TMaterialForm.TrackBar1Change(Sender: TObject);
begin
  label10.Caption := inttostr(trackbar1.Position);
  ActualizaMaterial;
end;

procedure TMaterialForm.TrackBar2Change(Sender: TObject);
begin
  label12.Caption := inttostr(trackbar2.Position);
  ActualizaMaterial;
end;

procedure TMaterialForm.alpha1_1Change(Sender: TObject);
begin
    ActualizaMaterial;
end;

procedure TMaterialForm.ComboBox1Change(Sender: TObject);
begin

    ActualizaMaterial;
end;

procedure TMaterialForm.FormCreate(Sender: TObject);
begin
   MatDirecto := false;
  //MaterialAct := TGLMaterial.Create(nil);
end;

procedure TMaterialForm.FormShow(Sender: TObject);
var
  a :integer;
begin
   materiallist := form1.GLMaterialLibrary1.Materials;
   listbox1.Clear;

   if (MatDirecto) then
   begin
       button3.Enabled := false;
       button4.Enabled := false;
       button5.Enabled := false;
       {
       with form1 do
         MaterialAct := GLScene1.Objects.FindChild(ListaObjetos.Items.Strings[Listaobjetos.itemindex],false)
       end;}

   end else
   begin
       button3.Enabled := true;
       button4.Enabled := true;
       button5.Enabled := true;
     if materiallist.Count >0 then
       MaterialAct := Materiallist.Items[0].Material
     else
       MaterialAct := TGLMaterial.Create(nil);

     for a := 0 to materiallist.Count-1 do
        listbox1.Items.Add(Materiallist.Items[a].Name);
     listbox1.ItemIndex := 0;
     if listbox1.ItemIndex >-1 then
     begin
       labelmat.Caption :=  listbox1.Items.Strings[listbox1.ItemIndex];
     end else
       labelmat.Caption :=  '';

     materselected := labelmat.Caption;

   end;

   ActualizarValores();

end;

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


    if (objeto is TGLCube) then
    begin
      TGLCube(objeto).Material := MaterialAct;

      //TGLCube(objeto).Material.LibMaterialName := MaterSelected;
    end;
    if (objeto is TGLCone) then
    begin
      TGLCone(objeto).Material := MaterialAct;
    end;
    if (objeto is TGLSphere) then
    begin
      TGLSphere(objeto).Material := MaterialAct;
    end;
    if (objeto is TGLCylinder) then
    begin
     TGLCylinder(objeto).Material := MaterialAct;
    end;
    if (objeto is TGLPlane) then
    begin
      TGLPlane(objeto).Material := MaterialAct;
    end;
    close;
end;

procedure TMaterialForm.ListBox1DblClick(Sender: TObject);
begin
    ActualizaListMat ;

end;

procedure TMaterialForm.ActualizaListMat();
var
   ObjIndice:Integer;
   objeto:TGLBaseSceneObject;
   NombreObjeto:string;
begin
    ObjIndice := listbox1.ItemIndex;
    if (ObjIndice = -1) then
       exit; //No hay objetos seleccionados
    MaterialAct := materiallist.Items[ObjIndice].Material;
    labelmat.Caption :=  listbox1.Items.Strings[ObjIndice];
    materselected := labelmat.Caption;
    ActualizarValores;

end;

procedure TMaterialForm.Button3Click(Sender: TObject);
var
  nombre : string;
  material : TGLMaterial;
  matlib : TGLLibMaterial;
  a:integer;
begin
   nombre := Inputbox('Nombre del material','Introduzca el nombre del material:','');
   while (listbox1.Items.IndexOf(nombre)>-1) do
      nombre := Inputbox('Nombre del material','El material ya existe, introduzca el nombre del material:','');
//   material := TGLMaterial.Create(materialact);
   if nombre = '' then exit;
   matlib := form1.GLMaterialLibrary1.Materials.Add;
   matlib.Name := nombre;
   material := matlib.Material;
   material := materialact;
   matlib.Material := materialact;
                     
   //Actualiza lista
   listbox1.Clear;
   for a := 0 to materiallist.Count-1 do
      listbox1.Items.Add(Materiallist.Items[a].Name);
   listbox1.ItemIndex := materiallist.Count-1;
   labelmat.Caption :=  listbox1.Items.Strings[listbox1.Count-1];
   materselected := labelmat.Caption;

end;

procedure TMaterialForm.Button5Click(Sender: TObject);
var
  nombre : string;
  material : TGLMaterial;
  matlib : TGLLibMaterial;
begin
//    matlib := form1.GLMaterialLibrary1.Materials.Items[listbox1.itemindex];
//    matlib.Material := materialact;
    form1.GLMaterialLibrary1.Materials.Items[listbox1.itemindex].Material := materialact;
end;

procedure TMaterialForm.Button4Click(Sender: TObject);
var
  i,a :integer;
begin
    i := listbox1.ItemIndex;
    if (i = -1) then
       exit; //No hay objetos seleccionados
    if (messagedlg('¿Estas seguro de que deseas eliminar '+listbox1.Items.Strings[i]+' ?',mtConfirmation, [mbYes, mbNo],0) = mrNo )	 then exit;

    materiallist.Delete(i);
   //Actualiza lista
   listbox1.Clear;
   for a := 0 to materiallist.Count-1 do
      listbox1.Items.Add(Materiallist.Items[a].Name);
   if materiallist.Count > 0 then
   begin
     listbox1.ItemIndex := materiallist.Count-1;
     labelmat.Caption :=  listbox1.Items.Strings[listbox1.Count-1];
   end else
     labelmat.Caption := '';

   materselected := labelmat.Caption;
end;

procedure TMaterialForm.Button6Click(Sender: TObject);
begin
  if openpicturedialog1.Execute then
  begin
     materialact.Texture.Image.LoadFromFile(openpicturedialog1.FileName);
     label19.Caption := openpicturedialog1.FileName;
     glcube1.Material := materialact;
  end;

end;

procedure TMaterialForm.CheckBox1Click(Sender: TObject);
begin
   materialact.Texture.Disabled := not checkbox1.Checked;
   if fileexists(label19.Caption) then
     materialact.Texture.Image.LoadFromFile(label19.Caption);
   glcube1.Material := materialact;
end;

end.
