Search code examples
delphi-7glscene

Can i change Emition Color to a GLHudSprite object in GLScene?


i use this function to get the material

function AddMaterial(aMatLib: TGlMaterialLibrary; aFileName, aMaterialName: string):TGlLibMaterial;
begin
  result := aMatLib.Materials.Add;
  with result do
  begin
    with Material do
     begin
       MaterialOptions := [moIgnoreFog, moNoLighting];
       Texture.Disabled := false;
       BlendingMode := bmTransparency;
       Texture.TextureMode := tmModulate;
       with FrontProperties do
        begin
          Ambient.SetColor(1, 1, 1, 1);
          Diffuse.SetColor(1, 1, 1, 1);
          Emission.SetColor(1, 1, 1, 1);
          Specular.SetColor(1, 1, 1, 1);
        end;
      Texture.ImageClassName := 'TGLCompositeImage';
      if ExtractFileExt(aFileName) = '' then
        TGLCompositeImage(Texture.Image).LoadFromFile(aFileName + '.png')
      else
        TGLCompositeImage(Texture.Image).LoadFromFile(aFileName);
      //TGLCompositeImage(Texture.Image).LoadFromFile(aFileName);
   end;
    Name := aMaterialName;
  end;
end;

second i add an onmouse move procedure

first the const vector colors

const
  OnMoveInObjects_Color: TColorVector = (0.243, 0.243, 0.243, 1.000);
  OnOutObjects_Color: TColorVector = (0.000, 0.000, 0.000, 1.000);

create the object

 fsExit:= TGLHUDSprite.CreateAsChild(MainForm.Dummy_mainmenu);
 fsExit.Material.MaterialLibrary:= MatLib;
 fsExit.Material.LibMaterialName:= 'bexit';
 fsExit.SetSquareSize(50);
 fsExit.Position.X:= CenterX;
 fsExit.Position.Y:= 30;
 fsExit.Visible:= true;

and then the procedure

procedure Check_Mouse_UpPlayer(x,y:Integer);
 var
  sTVol: FLOAT;
begin
 if MainForm.IsMouseOverImage(fsExit,x,y) then
   begin
     fsExit.Material.FrontProperties.Emission.Color:= OnMoveInObjects_Color;
     if IsKeyDown(VK_LBUTTON) then
       begin
        fade_blur:= true;
        ShowExit;
      end;
    end
  else
   fsExit.Material.FrontProperties.Emission.Color:= OnOutObjects_Color;
end;

and here is the ismouseoverImage function...

function TMainForm.IsMouseOverImage(const AButton: TGLHudSprite; const X, Y: Integer):Boolean;
begin
  Result := (X >= AButton.Position.X - AButton.Width / 2) and (X <= AButton.Position.X + AButton.Width / 2) and
        (Y >= AButton.Position.Y - AButton.Height / 2) and (Y <= AButton.Position.Y + AButton.Height / 2);
end;

Now when the mouse is over the image the material change the emition color but i dont get results... (i don't see anything).

What i am doing wrong...?

Thank you...


Solution

  • I put together a sample showcasing what I tried to explain. The only procedure it contains assigns a different material to a HUDSprite on mouse over and deselects it on mouse out.

        procedure TForm1.GLSceneViewer1MouseMove(Sender: TObject;
      Shift: TShiftState; X, Y: Integer);
    begin
       if (X >= GLHUDSprite1.Position.X - GLHUDSprite1.Width * 0.5) and (X <= GLHUDSprite1.Position.X + GLHUDSprite1.Width * 0.5) and (Y >= GLHUDSprite1.Position.Y - GLHUDSprite1.Height * 0.5) and (Y <= GLHUDSprite1.Position.Y + GLHUDSprite1.Height * 0.5) then
       begin
          GLHUDSprite1.Material.LibMaterialName := 'Selected';
       end
       else
          GLHUDSprite1.Material.LibMaterialName := 'Unselected';
      GLSceneViewer1.Invalidate;
    end;
    

    This is all you need, I'm attaching the sample including all the files. Please download the project files from here: TestHUDSprite