Ignore:
Timestamp:
Dec 22, 2016, 8:49:19 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Updated BGRABitmap package.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GraphicTest/Packages/bgrabitmap/face3d.inc

    r472 r494  
    88       ColorOverride: boolean;
    99       TexCoordOverride: boolean;
     10       ActualColor: TBGRAPixel;
     11       ActualTexCoord: TPointF;
    1012     end;
    1113
     
    1618    FVertices: packed array of TBGRAFaceVertexDescription;
    1719    FVertexCount: integer;
    18     FTexture: IBGRAScanner;
     20    FTexture, FActualTexture: IBGRAScanner;
    1921    FMaterial: IBGRAMaterial3D;
     22    FActualMaterial: TBGRAMaterial3D;
    2023    FMaterialName: string;
    2124    FParentTexture: boolean;
     
    3033    function GetVertexDescription(AIndex : integer): PBGRAFaceVertexDescription;
    3134    procedure SetCustomFlags(AValue: DWord);
     35    procedure ComputeActualVertexColor(AIndex: integer);
     36    procedure ComputeActualTexCoord(AMinIndex, AMaxIndex: integer);
     37    procedure UpdateTexture;
    3238  public
    3339    function GetObject3D: IBGRAObject3D;
    3440    constructor Create(AObject3D: IBGRAObject3D; AVertices: array of IBGRAVertex3D);
    3541    destructor Destroy; override;
     42    procedure ComputeVertexColors;
     43    procedure UpdateMaterial;
     44    procedure FlipFace;
    3645    function AddVertex(AVertex: IBGRAVertex3D): integer;
    3746    function GetParentTexture: boolean;
     
    8998    property LightThroughFactorOverride: boolean read GetLightThroughFactorOverride write SetLightThroughFactorOverride;
    9099    property Material: IBGRAMaterial3D read GetMaterial write SetMaterial;
     100    property ActualMaterial: TBGRAMaterial3D read FActualMaterial;
     101    property ActualTexture: IBGRAScanner read FActualTexture;
    91102    property VertexDescription[AIndex : integer]: PBGRAFaceVertexDescription read GetVertexDescription;
    92103    property CustomFlags: DWord read GetCustomFlags write SetCustomFlags;
     
    114125begin
    115126  FCustomFlags:= AValue;
     127end;
     128
     129procedure TBGRAFace3D.ComputeActualVertexColor(AIndex: integer);
     130begin
     131  with FVertices[AIndex] do
     132  begin
     133    if ColorOverride then
     134      ActualColor := Color
     135    else
     136    if Vertex.ParentColor then
     137      ActualColor := FObject3D.Color
     138    else
     139      ActualColor := Vertex.Color;
     140  end;
     141end;
     142
     143procedure TBGRAFace3D.ComputeActualTexCoord(AMinIndex, AMaxIndex: integer);
     144var
     145  i: Integer;
     146  zoom: TPointF;
     147  m: IBGRAMaterial3D;
     148begin
     149  m := ActualMaterial;
     150  if m <> nil then zoom := m.TextureZoom
     151  else zoom := PointF(1,1);
     152  for i := AMinIndex to AMaxIndex do
     153    with FVertices[i] do
     154    begin
     155      if TexCoordOverride then
     156        ActualTexCoord := TexCoord
     157      else
     158        ActualTexCoord := Vertex.TexCoord;
     159      ActualTexCoord.x *= zoom.x;
     160      ActualTexCoord.y *= zoom.y;
     161    end;
     162end;
     163
     164procedure TBGRAFace3D.UpdateTexture;
     165begin
     166  if FParentTexture then
     167  begin
     168    FActualTexture := nil;
     169    if FActualMaterial <> nil then
     170      FActualTexture := FActualMaterial.GetTexture;
     171    if FActualTexture = nil then
     172      FActualTexture := FObject3D.Texture
     173  end
     174  else
     175    FActualTexture := FTexture;
    116176end;
    117177
     
    131191  i: Integer;
    132192begin
    133   SetLength(FVertices, length(AVertices));
    134   for i:= 0 to high(AVertices) do
    135     AddVertex(AVertices[i]);
    136193  FObject3D := AObject3D;
    137194  FBiface := false;
     
    139196  FLightThroughFactor:= 0;
    140197  FLightThroughFactorOverride:= false;
     198
     199  UpdateMaterial;
     200
     201  SetLength(FVertices, length(AVertices));
     202  for i:= 0 to high(AVertices) do
     203    AddVertex(AVertices[i]);
    141204end;
    142205
    143206destructor TBGRAFace3D.Destroy;
    144207begin
     208  FMaterial := nil;
    145209  fillchar(FTexture,sizeof(FTexture),0);
     210  fillchar(FActualTexture,sizeof(FActualTexture),0);
    146211  inherited Destroy;
     212end;
     213
     214procedure TBGRAFace3D.ComputeVertexColors;
     215var
     216  i: Integer;
     217begin
     218  for i := 0 to FVertexCount-1 do
     219    ComputeActualVertexColor(i);
     220end;
     221
     222procedure TBGRAFace3D.UpdateMaterial;
     223begin
     224  if Material <> nil then
     225    FActualMaterial := TBGRAMaterial3D(Material.GetAsObject)
     226  else if FObject3D.Material <> nil then
     227    FActualMaterial := TBGRAMaterial3D(FObject3D.Material.GetAsObject)
     228  else if TBGRAScene3D(FObject3D.Scene).DefaultMaterial <> nil then
     229    FActualMaterial := TBGRAMaterial3D(TBGRAScene3D(FObject3D.Scene).DefaultMaterial.GetAsObject);
     230
     231  UpdateTexture;
     232
     233  ComputeActualTexCoord(0,FVertexCount-1);
     234end;
     235
     236procedure TBGRAFace3D.FlipFace;
     237var i: integer;
     238  temp: TBGRAFaceVertexDescription;
     239begin
     240  for i := 0 to (VertexCount div 2)-1 do
     241  begin
     242    temp := FVertices[i];
     243    FVertices[i] := FVertices[VertexCount-1-i];
     244    FVertices[VertexCount-1-i] := temp;
     245  end;
    147246end;
    148247
     
    161260    Normal := nil;
    162261  end;
     262  ComputeActualVertexColor(result);
     263  ComputeActualTexCoord(result,result);
    163264  inc(FVertexCount);
    164265end;
     
    186287    raise Exception.Create('Index out of bounds');
    187288  FVertices[AIndex].Vertex := AValue;
     289  ComputeActualVertexColor(AIndex);
    188290end;
    189291
     
    192294  if (AIndex < 0) or (AIndex >= FVertexCount) then
    193295    raise Exception.Create('Index out of bounds');
    194   result := FVertices[AIndex].Color;
     296  result := FVertices[AIndex].ActualColor;
    195297end;
    196298
     
    220322begin
    221323  FParentTexture := AValue;
     324  UpdateTexture;
    222325end;
    223326
     
    226329  FTexture := AValue;
    227330  FParentTexture := false;
     331  UpdateTexture;
    228332end;
    229333
     
    245349    ColorOverride := true;
    246350  end;
     351  ComputeActualVertexColor(AIndex);
    247352end;
    248353
     
    253358    raise Exception.Create('Index out of bounds');
    254359  FVertices[AIndex].ColorOverride := AValue;
     360  ComputeActualVertexColor(AIndex);
    255361end;
    256362
     
    275381  FVertices[AIndex].TexCoord := AValue;
    276382  FVertices[AIndex].TexCoordOverride := true;
     383  ComputeActualTexCoord(AIndex, AIndex);
    277384end;
    278385
     
    374481procedure TBGRAFace3D.SetMaterial(const AValue: IBGRAMaterial3D);
    375482begin
    376   FMaterial := AValue;
     483  if AValue <> FMaterial then
     484  begin
     485    FMaterial := AValue;
     486    UpdateMaterial;
     487  end;
    377488end;
    378489
     
    382493  begin
    383494    FMaterialName := AValue;
    384     FObject3D.Scene.UseMaterial(FMaterialName, self);
     495    TBGRAScene3D(FObject3D.Scene).UseMaterial(FMaterialName, self);
    385496  end;
    386497end;
Note: See TracChangeset for help on using the changeset viewer.