2009-12-20 22:05:15 +01:00
|
|
|
(*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License, Version 1.0 only
|
|
|
|
* (the "License"). You may not use this file except in compliance
|
|
|
|
* with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at
|
|
|
|
* http://www.opensource.org/licenses/cddl1.php.
|
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at
|
|
|
|
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
|
|
|
* add the following below this CDDL HEADER, with the fields enclosed
|
|
|
|
* by brackets "[]" replaced with your own identifying * information:
|
|
|
|
* Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Portions Copyright 2009 Andreas Schneider
|
|
|
|
*)
|
|
|
|
unit ULightManager;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, Imaging, ImagingTypes, ImagingClasses, ImagingCanvases,
|
2009-12-22 09:36:35 +01:00
|
|
|
ImagingOpenGL, GL, GLu, GLext, fgl, ULandscape, UWorldItem, UCacheManager;
|
2009-12-20 22:05:15 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
|
2009-12-21 00:13:26 +01:00
|
|
|
TCalculateOffset = procedure(AX, AY: Integer; out DrawX, DrawY: Integer) of object;
|
|
|
|
|
|
|
|
{ TLightMaterial }
|
|
|
|
|
2009-12-22 09:36:35 +01:00
|
|
|
TLightMaterial = class(TSimpleMaterial)
|
2009-12-21 00:13:26 +01:00
|
|
|
constructor Create(AGraphic: TBaseImage);
|
|
|
|
destructor Destroy; override;
|
|
|
|
protected
|
|
|
|
FCanvas: TFastARGB32Canvas;
|
|
|
|
public
|
|
|
|
property Canvas: TFastARGB32Canvas read FCanvas;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TLightCache = specialize TCacheManager<TLightMaterial>;
|
|
|
|
|
|
|
|
TLightManager = class;
|
2009-12-20 22:05:15 +01:00
|
|
|
|
|
|
|
{ TLightSource }
|
|
|
|
|
|
|
|
TLightSource = class
|
2009-12-21 00:13:26 +01:00
|
|
|
constructor Create(AManager: TLightManager; AWorldItem: TWorldItem);
|
|
|
|
destructor Destroy; override;
|
2009-12-20 22:05:15 +01:00
|
|
|
protected
|
|
|
|
FX: Integer;
|
|
|
|
FY: Integer;
|
2009-12-21 13:41:18 +01:00
|
|
|
FZ: SmallInt;
|
2009-12-21 00:13:26 +01:00
|
|
|
FMaterial: TLightMaterial;
|
2009-12-20 22:05:15 +01:00
|
|
|
public
|
|
|
|
property X: Integer read FX;
|
|
|
|
property Y: Integer read FY;
|
2009-12-21 13:41:18 +01:00
|
|
|
property Z: SmallInt read FZ;
|
2009-12-21 00:13:26 +01:00
|
|
|
property Material: TLightMaterial read FMaterial;
|
2009-12-20 22:05:15 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
TLightSources = specialize TFPGObjectList<TLightSource>;
|
|
|
|
|
|
|
|
{ TLightManager }
|
|
|
|
|
|
|
|
TLightManager = class
|
2009-12-21 00:55:19 +01:00
|
|
|
constructor Create(ACalculateOffset: TCalculateOffset);
|
2009-12-20 22:05:15 +01:00
|
|
|
destructor Destroy; override;
|
|
|
|
protected
|
|
|
|
FLightSources: TLightSources;
|
|
|
|
FOverlay: TSingleImage;
|
|
|
|
FOverlayTexture: GLuint;
|
2009-12-21 13:41:18 +01:00
|
|
|
FLightLevel: Byte;
|
2009-12-20 22:05:15 +01:00
|
|
|
FValid: Boolean;
|
|
|
|
FCalculateOffset: TCalculateOffset;
|
2009-12-21 00:13:26 +01:00
|
|
|
FLightCache: TLightCache;
|
2009-12-22 09:36:35 +01:00
|
|
|
FUseFBO: Boolean;
|
|
|
|
FInitialized: Boolean;
|
2009-12-21 00:13:26 +01:00
|
|
|
function GetLight(AID: Integer): TLightMaterial;
|
2009-12-21 13:41:18 +01:00
|
|
|
procedure SetLightLevel(AValue: Byte);
|
2009-12-21 00:13:26 +01:00
|
|
|
procedure UpdateOverlay(AScreenRect: TRect);
|
2009-12-20 22:05:15 +01:00
|
|
|
public
|
2009-12-21 13:41:18 +01:00
|
|
|
property LightLevel: Byte read FLightLevel write SetLightLevel;
|
2009-12-22 09:36:35 +01:00
|
|
|
procedure InitGL;
|
2009-12-20 22:05:15 +01:00
|
|
|
procedure UpdateLightMap(ALeft, AWidth, ATop, AHeight: Integer;
|
|
|
|
AScreenBuffer: TScreenBuffer);
|
2009-12-21 00:13:26 +01:00
|
|
|
procedure Draw(AScreenRect: TRect);
|
2009-12-20 22:05:15 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2009-12-21 22:26:52 +01:00
|
|
|
UGameResources, UTiledata, UStatics, UMap, ULight, Logging;
|
2009-12-20 22:05:15 +01:00
|
|
|
|
|
|
|
{ TLightManager }
|
|
|
|
|
2009-12-21 00:55:19 +01:00
|
|
|
constructor TLightManager.Create(ACalculateOffset: TCalculateOffset);
|
2009-12-20 22:05:15 +01:00
|
|
|
begin
|
|
|
|
FCalculateOffset := ACalculateOffset;
|
|
|
|
FLightSources := TLightSources.Create(True);
|
2009-12-21 13:41:18 +01:00
|
|
|
FLightLevel := 0;
|
2009-12-21 00:13:26 +01:00
|
|
|
FLightCache := TLightCache.Create(32);
|
2009-12-22 09:36:35 +01:00
|
|
|
FInitialized := False;
|
2009-12-20 22:05:15 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TLightManager.Destroy;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FLightSources);
|
|
|
|
FreeAndNil(FOverlay);
|
2009-12-21 00:13:26 +01:00
|
|
|
FreeAndNil(FLightCache);
|
2009-12-20 22:05:15 +01:00
|
|
|
glDeleteTextures(1, @FOverlayTexture);
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2009-12-21 00:13:26 +01:00
|
|
|
function TLightManager.GetLight(AID: Integer): TLightMaterial;
|
|
|
|
var
|
|
|
|
light: TLight;
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
if not FLightCache.QueryID(AID, Result) then
|
|
|
|
begin
|
|
|
|
if ResMan.Lights.Exists(AID) then
|
|
|
|
begin
|
|
|
|
light := ResMan.Lights.GetLight(AID);
|
|
|
|
Result := TLightMaterial.Create(light.Graphic);
|
|
|
|
FLightCache.StoreID(AID, Result);
|
|
|
|
light.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2009-12-21 13:41:18 +01:00
|
|
|
procedure TLightManager.SetLightLevel(AValue: Byte);
|
|
|
|
begin
|
|
|
|
FLightLevel := AValue;
|
|
|
|
FValid := False;
|
|
|
|
end;
|
|
|
|
|
2009-12-21 00:13:26 +01:00
|
|
|
procedure TLightManager.UpdateOverlay(AScreenRect: TRect);
|
2009-12-20 22:05:15 +01:00
|
|
|
var
|
2009-12-22 11:49:48 +01:00
|
|
|
canvas: TFastARGB32Canvas;
|
2009-12-20 22:05:15 +01:00
|
|
|
color: TColor32Rec;
|
2009-12-21 00:13:26 +01:00
|
|
|
i: Integer;
|
|
|
|
lightMaterial: TLightMaterial;
|
2009-12-22 09:36:35 +01:00
|
|
|
colorGL: GLclampf;
|
|
|
|
fbo: GLuint;
|
2009-12-20 22:05:15 +01:00
|
|
|
begin
|
|
|
|
glDeleteTextures(1, @FOverlayTexture);
|
2009-12-22 09:36:35 +01:00
|
|
|
if FUseFBO then
|
|
|
|
begin
|
|
|
|
glGenTextures(1, @FOverlayTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, FOverlayTexture);
|
|
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, AScreenRect.Right,
|
|
|
|
AScreenRect.Bottom, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
glGenFramebuffersEXT(1, @fbo);
|
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
|
|
|
|
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
|
|
|
|
GL_TEXTURE_2D, FOverlayTexture, 0);
|
|
|
|
|
|
|
|
colorGL :=(32 - lightLevel) / 32;
|
|
|
|
glClearColor(colorGL, colorGL, colorGL, 1);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
glBlendFunc(GL_ONE, GL_ONE);
|
|
|
|
for i := 0 to FLightSources.Count - 1 do
|
|
|
|
begin
|
|
|
|
lightMaterial := FLightSources[i].Material;
|
|
|
|
if lightMaterial <> nil then
|
|
|
|
begin
|
|
|
|
glBindTexture(GL_TEXTURE_2D, lightMaterial.Texture);
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glTexCoord2i(0, 0);
|
|
|
|
glVertex2i(FLightSources[i].FX - lightMaterial.RealWidth div 2,
|
|
|
|
FLightSources[i].FY - lightMaterial.RealHeight div 2);
|
|
|
|
glTexCoord2i(0, 1);
|
|
|
|
glVertex2i(FLightSources[i].FX - lightMaterial.RealWidth div 2,
|
|
|
|
FLightSources[i].FY - lightMaterial.RealHeight div 2 +
|
|
|
|
lightMaterial.Height);
|
|
|
|
glTexCoord2i(1, 1);
|
|
|
|
glVertex2i(FLightSources[i].FX - lightMaterial.RealWidth div 2 +
|
|
|
|
lightMaterial.Width, FLightSources[i].FY -
|
|
|
|
lightMaterial.RealHeight div 2 + lightMaterial.Height);
|
|
|
|
glTexCoord2i(1, 0);
|
|
|
|
glVertex2i(FLightSources[i].FX - lightMaterial.RealWidth div 2 +
|
|
|
|
lightMaterial.Width,
|
|
|
|
FLightSources[i].FY - lightMaterial.RealHeight div 2);
|
|
|
|
glEnd;
|
|
|
|
end;
|
|
|
|
end;
|
2009-12-20 22:05:15 +01:00
|
|
|
|
2009-12-22 09:36:35 +01:00
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
|
|
|
glDeleteFramebuffersEXT(1, @fbo);
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
FOverlay.Free;
|
2009-12-20 22:05:15 +01:00
|
|
|
|
2009-12-22 09:36:35 +01:00
|
|
|
color.A := $FF;
|
|
|
|
color.R := ((32 - FLightLevel) * 255) div 32;
|
|
|
|
color.G := color.R;
|
|
|
|
color.B := color.R;
|
2009-12-20 22:05:15 +01:00
|
|
|
|
2009-12-22 09:36:35 +01:00
|
|
|
FOverlay := TSingleImage.CreateFromParams(AScreenRect.Right,
|
|
|
|
AScreenRect.Bottom, ifA8R8G8B8);
|
|
|
|
canvas := TFastARGB32Canvas.CreateForImage(FOverlay);
|
|
|
|
try
|
|
|
|
canvas.FillColor32 := color.Color;
|
|
|
|
canvas.FillRect(AScreenRect);
|
|
|
|
|
|
|
|
for i := 0 to FLightSources.Count - 1 do
|
2009-12-21 22:26:52 +01:00
|
|
|
begin
|
2009-12-22 09:36:35 +01:00
|
|
|
lightMaterial := FLightSources[i].Material;
|
|
|
|
if lightMaterial <> nil then
|
|
|
|
begin
|
|
|
|
lightMaterial.Canvas.DrawAdd(lightMaterial.Canvas.ClipRect, canvas,
|
|
|
|
FLightSources[i].FX - lightMaterial.RealWidth div 2,
|
|
|
|
FLightSources[i].FY - lightMaterial.RealHeight div 2);
|
|
|
|
end;
|
2009-12-21 22:26:52 +01:00
|
|
|
end;
|
2009-12-22 09:36:35 +01:00
|
|
|
finally
|
|
|
|
canvas.Free;
|
2009-12-21 00:13:26 +01:00
|
|
|
end;
|
2009-12-20 22:05:15 +01:00
|
|
|
|
2009-12-22 09:36:35 +01:00
|
|
|
FOverlayTexture := CreateGLTextureFromImage(FOverlay.ImageDataPointer^);
|
|
|
|
end;
|
2009-12-20 22:05:15 +01:00
|
|
|
|
|
|
|
FValid := True;
|
|
|
|
end;
|
|
|
|
|
2009-12-22 09:36:35 +01:00
|
|
|
procedure TLightManager.InitGL;
|
|
|
|
begin
|
|
|
|
FUseFBO := Load_GL_EXT_framebuffer_object;
|
|
|
|
end;
|
|
|
|
|
2009-12-20 22:05:15 +01:00
|
|
|
procedure TLightManager.UpdateLightMap(ALeft, AWidth, ATop, AHeight: Integer;
|
|
|
|
AScreenBuffer: TScreenBuffer);
|
|
|
|
var
|
|
|
|
blockInfo: PBlockInfo;
|
2009-12-21 22:26:52 +01:00
|
|
|
lights: TWorldItemList;
|
|
|
|
i, x, y, tileID: Integer;
|
|
|
|
tileData: TTiledata;
|
2009-12-22 13:51:17 +01:00
|
|
|
tileMap: array of array of TWorldItem;
|
2009-12-20 22:05:15 +01:00
|
|
|
begin
|
|
|
|
//Logger.EnterMethod([lcClient, lcDebug], 'UpdateLightMap');
|
|
|
|
FLightSources.Clear;
|
|
|
|
{Logger.Send([lcClient, lcDebug], 'AWidth', AWidth);
|
|
|
|
Logger.Send([lcClient, lcDebug], 'AHeight', AHeight);}
|
2009-12-21 22:26:52 +01:00
|
|
|
lights := TWorldItemList.Create(False);
|
2009-12-22 13:51:17 +01:00
|
|
|
SetLength(tileMap, AWidth, AHeight);
|
|
|
|
for x := 0 to AWidth - 1 do
|
|
|
|
for y := 0 to AHeight - 1 do
|
|
|
|
tileMap[x,y] := nil;
|
|
|
|
|
2009-12-20 22:05:15 +01:00
|
|
|
blockInfo := nil;
|
|
|
|
while AScreenBuffer.Iterate(blockInfo) do
|
|
|
|
begin
|
|
|
|
if blockInfo^.State = ssNormal then
|
|
|
|
begin
|
2009-12-21 22:26:52 +01:00
|
|
|
if blockInfo^.Item is TStaticItem then
|
|
|
|
tileID := blockInfo^.Item.TileID + $4000
|
|
|
|
else
|
|
|
|
tileID := blockInfo^.Item.TileID;
|
|
|
|
tileData := ResMan.Tiledata.TileData[tileID];
|
2009-12-20 22:05:15 +01:00
|
|
|
|
2009-12-21 22:26:52 +01:00
|
|
|
if tdfLightSource in tileData.Flags then
|
2009-12-22 13:51:17 +01:00
|
|
|
lights.Add(blockInfo^.Item)
|
|
|
|
else
|
|
|
|
tileMap[blockInfo^.Item.X - ALeft, blockInfo^.Item.Y - ATop] :=
|
|
|
|
blockInfo^.Item;
|
2009-12-21 22:26:52 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
for i := 0 to lights.Count - 1 do
|
2009-12-22 13:51:17 +01:00
|
|
|
begin
|
|
|
|
x := lights[i].X + 1 - ALeft;
|
|
|
|
y := lights[i].Y + 1 - ATop;
|
|
|
|
if (x = AWidth) or (y = AHeight) or (tileMap[x,y] = nil) or
|
|
|
|
(tileMap[x,y].Z < lights[i].Z + 5) then
|
|
|
|
FLightSources.Add(TLightSource.Create(Self, lights[i]));
|
|
|
|
end;
|
2009-12-21 22:26:52 +01:00
|
|
|
|
2009-12-22 09:36:35 +01:00
|
|
|
lights.Free;
|
|
|
|
|
2009-12-20 22:05:15 +01:00
|
|
|
FValid := False;
|
|
|
|
//Logger.ExitMethod([lcClient, lcDebug], 'UpdateLightMap');
|
|
|
|
end;
|
|
|
|
|
2009-12-21 00:13:26 +01:00
|
|
|
procedure TLightManager.Draw(AScreenRect: TRect);
|
2009-12-20 22:05:15 +01:00
|
|
|
begin
|
2009-12-22 09:36:35 +01:00
|
|
|
if not FInitialized then
|
|
|
|
InitGL;
|
|
|
|
|
2009-12-20 22:05:15 +01:00
|
|
|
if not FValid then
|
2009-12-21 00:13:26 +01:00
|
|
|
UpdateOverlay(AScreenRect);
|
2009-12-20 22:05:15 +01:00
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, FOverlayTexture);
|
|
|
|
glBlendFunc(GL_ZERO, GL_SRC_COLOR);
|
|
|
|
glBegin(GL_QUADS);
|
2009-12-22 09:36:35 +01:00
|
|
|
if FUseFBO then
|
|
|
|
begin
|
|
|
|
glTexCoord2i(0, 1);
|
|
|
|
glVertex2i(AScreenRect.Left, AScreenRect.Top);
|
|
|
|
glTexCoord2i(0, 0);
|
|
|
|
glVertex2i(AScreenRect.Left, AScreenRect.Bottom);
|
|
|
|
glTexCoord2i(1, 0);
|
|
|
|
glVertex2i(AScreenRect.Right, AScreenRect.Bottom);
|
|
|
|
glTexCoord2i(1, 1);
|
|
|
|
glVertex2i(AScreenRect.Right, AScreenRect.Top);
|
|
|
|
end else
|
|
|
|
begin
|
2009-12-21 22:26:52 +01:00
|
|
|
glTexCoord2i(0, 0);
|
|
|
|
glVertex2i(AScreenRect.Left, AScreenRect.Top);
|
|
|
|
glTexCoord2i(0, 1);
|
|
|
|
glVertex2i(AScreenRect.Left, AScreenRect.Bottom);
|
|
|
|
glTexCoord2i(1, 1);
|
|
|
|
glVertex2i(AScreenRect.Right, AScreenRect.Bottom);
|
|
|
|
glTexCoord2i(1, 0);
|
|
|
|
glVertex2i(AScreenRect.Right, AScreenRect.Top);
|
2009-12-22 09:36:35 +01:00
|
|
|
end;
|
2009-12-20 22:05:15 +01:00
|
|
|
glEnd;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TLightSource }
|
|
|
|
|
2009-12-21 00:13:26 +01:00
|
|
|
constructor TLightSource.Create(AManager: TLightManager; AWorldItem: TWorldItem);
|
|
|
|
var
|
|
|
|
lightID: Byte;
|
|
|
|
begin
|
|
|
|
lightID := ResMan.Tiledata.StaticTiles[AWorldItem.TileID].Quality;
|
|
|
|
FMaterial := AManager.GetLight(lightID);
|
|
|
|
if FMaterial <> nil then
|
|
|
|
begin
|
|
|
|
AManager.FCalculateOffset(AWorldItem.X, AWorldItem.Y, FX, FY);
|
|
|
|
FZ := AWorldItem.Z * 4;
|
2009-12-21 00:55:19 +01:00
|
|
|
FY := FY + 22 - FZ;
|
2009-12-21 00:13:26 +01:00
|
|
|
FMaterial.AddRef;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TLightSource.Destroy;
|
|
|
|
begin
|
|
|
|
if FMaterial <> nil then
|
|
|
|
FMaterial.DelRef;
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TLightMaterial }
|
|
|
|
|
|
|
|
constructor TLightMaterial.Create(AGraphic: TBaseImage);
|
|
|
|
begin
|
2009-12-22 09:36:35 +01:00
|
|
|
inherited Create(AGraphic);
|
2009-12-21 00:13:26 +01:00
|
|
|
FCanvas := TFastARGB32Canvas.CreateForImage(FGraphic);
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TLightMaterial.Destroy;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FCanvas);
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2009-12-20 22:05:15 +01:00
|
|
|
end.
|
|
|
|
|