FmMaterialEditor.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit FmMaterialEditor;
  5. (* Editor window for a material (with preview) *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. Winapi.Windows,
  10. System.Classes,
  11. System.TypInfo,
  12. VCL.Forms,
  13. VCL.ComCtrls,
  14. VCL.StdCtrls,
  15. VCL.Controls,
  16. VCL.Buttons,
  17. GLS.SceneViewer,
  18. GLS.State,
  19. GLS.Material,
  20. GLS.Texture,
  21. FRTrackBarEdit,
  22. FRMaterialPreview,
  23. FRColorEditor,
  24. FRFaceEditor,
  25. FRTextureEdit;
  26. type
  27. TGLMaterialEditorForm = class(TForm)
  28. PageControl1: TPageControl;
  29. TSFront: TTabSheet;
  30. TSBack: TTabSheet;
  31. TSTexture: TTabSheet;
  32. FEFront: TRFaceEditor;
  33. FEBack: TRFaceEditor;
  34. GroupBox1: TGroupBox;
  35. MPPreview: TRMaterialPreview;
  36. BBOk: TBitBtn;
  37. BBCancel: TBitBtn;
  38. RTextureEdit: TRTextureEdit;
  39. CBBlending: TComboBox;
  40. Label1: TLabel;
  41. Label2: TLabel;
  42. CBPolygonMode: TComboBox;
  43. procedure OnMaterialChanged(Sender: TObject);
  44. public
  45. constructor Create(AOwner: TComponent); override;
  46. function Execute(AMaterial: TGLMaterial): Boolean;
  47. end;
  48. function GLMaterialEditorForm: TGLMaterialEditorForm;
  49. procedure ReleaseMaterialEditorForm;
  50. // ----------------------------------------------
  51. implementation
  52. // ----------------------------------------------
  53. {$R *.dfm}
  54. var
  55. vGLMaterialEditorForm: TGLMaterialEditorForm;
  56. function GLMaterialEditorForm: TGLMaterialEditorForm;
  57. begin
  58. if not Assigned(vGLMaterialEditorForm) then
  59. vGLMaterialEditorForm := TGLMaterialEditorForm.Create(nil);
  60. Result := vGLMaterialEditorForm;
  61. end;
  62. procedure ReleaseMaterialEditorForm;
  63. begin
  64. if Assigned(vGLMaterialEditorForm) then
  65. begin
  66. vGLMaterialEditorForm.Free;
  67. vGLMaterialEditorForm := nil;
  68. end;
  69. end;
  70. constructor TGLMaterialEditorForm.Create(AOwner: TComponent);
  71. var
  72. I: Integer;
  73. begin
  74. inherited;
  75. for I := 0 to Integer(High(TGLBlendingMode)) do
  76. CBBlending.Items.Add(GetEnumName(TypeInfo(TGLBlendingMode), I));
  77. for I := 0 to Integer(High(TGLPolygonMode)) do
  78. CBPolygonMode.Items.Add(GetEnumName(TypeInfo(TGLPolygonMode), I));
  79. FEFront.OnChange := OnMaterialChanged;
  80. FEBack.OnChange := OnMaterialChanged;
  81. RTextureEdit.OnChange := OnMaterialChanged;
  82. end;
  83. function TGLMaterialEditorForm.Execute(AMaterial: TGLMaterial): Boolean;
  84. begin
  85. with AMaterial.GetActualPrimaryMaterial do
  86. begin
  87. FEFront.FaceProperties := FrontProperties;
  88. FEBack.FaceProperties := BackProperties;
  89. RTextureEdit.Texture := Texture;
  90. CBPolygonMode.ItemIndex := Integer(PolygonMode);
  91. CBBlending.ItemIndex := Integer(BlendingMode);
  92. end;
  93. MPPreview.Material := AMaterial;
  94. Result := (ShowModal = mrOk);
  95. if Result then
  96. with AMaterial.GetActualPrimaryMaterial do
  97. begin
  98. FrontProperties := FEFront.FaceProperties;
  99. BackProperties := FEBack.FaceProperties;
  100. Texture := RTextureEdit.Texture;
  101. BlendingMode := TGLBlendingMode(CBBlending.ItemIndex);
  102. PolygonMode := TGLPolygonMode(CBPolygonMode.ItemIndex);
  103. end;
  104. end;
  105. procedure TGLMaterialEditorForm.OnMaterialChanged(Sender: TObject);
  106. begin
  107. with MPPreview do
  108. begin
  109. Material.FrontProperties := FEFront.FaceProperties;
  110. Material.BackProperties := FEBack.FaceProperties;
  111. Material.Texture := RTextureEdit.Texture;
  112. Material.BlendingMode := TGLBlendingMode(CBBlending.ItemIndex);
  113. Material.PolygonMode := TGLPolygonMode(CBPolygonMode.ItemIndex);
  114. Cube.Material := Material;
  115. Sphere.Material := Material;
  116. TeaPot.Material := Material;
  117. end;
  118. MPPreview.GLSceneViewer.Invalidate;
  119. end;
  120. // ------------------------------------------------------------------
  121. initialization
  122. // ------------------------------------------------------------------
  123. finalization
  124. ReleaseMaterialEditorForm;
  125. end.