2026-10-04 17:46:01 +05:00
|
|
|
unit KM_Render;
|
|
|
|
|
{$I KaM_Remake.inc}
|
|
|
|
|
interface
|
|
|
|
|
uses
|
|
|
|
|
{$IFDEF WDC} Windows, {$ENDIF}
|
|
|
|
|
{$IFDEF Unix} LCLIntf, LCLType, OpenGLContext, {$ENDIF}
|
|
|
|
|
dglOpenGL,
|
2026-10-06 12:49:37 +05:00
|
|
|
KM_RenderTypes, KM_CommonTypes;
|
2026-10-04 17:46:01 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
const
|
|
|
|
|
TEX_FORMAT_SIZE: array [TKMTexFormat] of Byte = (2, 4, 1);
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
TKMRenderMode = (rm2D, rm3D);
|
|
|
|
|
|
|
|
|
|
//General OpenGL handling
|
|
|
|
|
TKMRender = class
|
|
|
|
|
private class var
|
|
|
|
|
fMaxViewportDim: Cardinal; // Max Viewport Dimensions, which could be used for FBO render
|
|
|
|
|
private
|
|
|
|
|
// Flag, True when FBO is inited. We don't need to init FBO for every run, since its only used in SaveMapToImageFile feature
|
|
|
|
|
fFBOInited: Boolean;
|
|
|
|
|
// Off-screen buffer, example from https://stackoverflow.com/questions/12157646/how-to-render-offscreen-on-opengl
|
|
|
|
|
fFBO: Cardinal;
|
|
|
|
|
fRenderBuf: Cardinal;
|
|
|
|
|
|
|
|
|
|
fScreenX, fScreenY: Word;
|
|
|
|
|
|
|
|
|
|
procedure InitFBO;
|
|
|
|
|
procedure SaveBufferToFile(aUseFBOBuffer: Boolean; var aWidth, aHeight: Integer; var aPixelData: TKMCardinalArray);
|
|
|
|
|
function GetMaxFBOSize: Cardinal;
|
|
|
|
|
public
|
2026-10-06 12:08:28 +05:00
|
|
|
constructor Create(aScreenX, aScreenY: Integer);
|
2026-10-04 17:46:01 +05:00
|
|
|
destructor Destroy; override;
|
|
|
|
|
|
|
|
|
|
procedure SetRenderMode(aRenderMode: TKMRenderMode); //Switch between 2D and 3D perspectives
|
|
|
|
|
|
|
|
|
|
class procedure GraphicsContextChanged(aAvailable: Boolean); static;
|
|
|
|
|
class function TextureBackendReady: Boolean; static;
|
|
|
|
|
class function GetMaxTexSize: Cardinal; static;
|
|
|
|
|
class function GetMaxViewportDim: Cardinal; static;
|
|
|
|
|
class function GenerateTextureCommon(aMinFilter, aMagFilter: TKMFilterType): Cardinal;
|
|
|
|
|
class function GenTexture(DestX, DestY: Word; const Data: Pointer; Mode: TKMTexFormat; aMinFilter, aMagFilter: TKMFilterType): Cardinal;
|
|
|
|
|
class procedure DeleteTexture(aTex: Cardinal);
|
|
|
|
|
class procedure UpdateTexture(aTexture: Cardinal; DestX, DestY: Word; Mode: TKMTexFormat; const Data: Pointer);
|
|
|
|
|
class procedure BindTexture(aTexId: Cardinal);
|
|
|
|
|
class procedure InvalidateTextureBindingCache; static;
|
|
|
|
|
|
|
|
|
|
class property MaxTextureSize: Cardinal read GetMaxTexSize;
|
|
|
|
|
class property MaxViewportDim: Cardinal read GetMaxViewportDim;
|
|
|
|
|
|
|
|
|
|
property MaxFBOSize: Cardinal read GetMaxFBOSize;
|
|
|
|
|
procedure Resize(aWidth, aHeight: Integer);
|
|
|
|
|
|
|
|
|
|
procedure ReadRenderedToScreenPixels(var aWidth, aHeight: Integer; var aPixelData: TKMCardinalArray);
|
|
|
|
|
procedure ReadRenderedToFBOPixels(var aWidth, aHeight: Integer; var aPixelData: TKMCardinalArray);
|
|
|
|
|
|
|
|
|
|
property ScreenX: Word read fScreenX;
|
|
|
|
|
property ScreenY: Word read fScreenY;
|
|
|
|
|
|
|
|
|
|
procedure BeginFrame;
|
|
|
|
|
procedure RenderBrightness(aValue: Byte);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
gRender: TKMRender;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
uses
|
|
|
|
|
SysUtils, Math,
|
|
|
|
|
KromOGLUtils, KromUtils,
|
|
|
|
|
KM_Log, KM_Defaults, KM_IoPNG, KM_CommonClasses,
|
|
|
|
|
KM_Render_Mobile;
|
|
|
|
|
|
|
|
|
|
const
|
|
|
|
|
// We want to use off-screen FBO buffer to render full map to the file,
|
|
|
|
|
// we are restricted with max file we are able to write via TBitmap or TJpegImage or TPNGImage atm
|
|
|
|
|
// could check https://github.com/graphics32 package to solve this issue with saving ultra large images
|
|
|
|
|
MAX_FBO_BUFFER_SIZE = CELL_SIZE_PX * (MAX_MAP_SIZE - 1); // (10200 = CELL_SIZE=40px)*255
|
|
|
|
|
|
|
|
|
|
{ TKMRender }
|
2026-10-06 12:08:28 +05:00
|
|
|
constructor TKMRender.Create(aScreenX, aScreenY: Integer);
|
2026-10-04 17:46:01 +05:00
|
|
|
begin
|
|
|
|
|
inherited Create;
|
|
|
|
|
|
|
|
|
|
gLog.AddTime('Init Render started');
|
|
|
|
|
|
|
|
|
|
fFBOInited := False;
|
|
|
|
|
if not fBlind then
|
|
|
|
|
begin
|
|
|
|
|
fMaxViewportDim := GetMaxViewportDim;
|
|
|
|
|
gLog.AddTime('GL_MAX_VIEWPORT_DIM = ' + IntToStr(fMaxViewportDim));
|
|
|
|
|
|
|
|
|
|
glClearColor(0, 0, 0, 0); //Background
|
|
|
|
|
glClearStencil(0);
|
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
|
|
|
|
glShadeModel(GL_SMOOTH); //Enables Smooth Color Shading
|
|
|
|
|
glPolygonMode(GL_FRONT, GL_FILL);
|
|
|
|
|
glEnable(GL_NORMALIZE);
|
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Set alpha mode
|
|
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
|
|
|
glEnable(GL_COLOR_MATERIAL); //Enable Materials
|
|
|
|
|
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
|
|
|
|
|
glDisable(GL_LIGHTING); //We don't need it
|
|
|
|
|
//glEnable(GL_CULL_FACE);
|
|
|
|
|
//glCullFace(GL_FRONT);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end;
|
2026-10-06 12:08:28 +05:00
|
|
|
Resize(aScreenX, aScreenY);
|
2026-10-04 17:46:01 +05:00
|
|
|
gLog.AddTime('Init Render Done');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
destructor TKMRender.Destroy;
|
|
|
|
|
begin
|
|
|
|
|
if not fBlind then
|
|
|
|
|
begin
|
|
|
|
|
if fFBOInited then
|
|
|
|
|
begin
|
|
|
|
|
glDeleteFramebuffers(1,@fFBO);
|
|
|
|
|
glDeleteRenderbuffers(1,@fRenderBuf);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
procedure TKMRender.InitFBO;
|
|
|
|
|
begin
|
|
|
|
|
if fFBOInited then Exit;
|
|
|
|
|
|
|
|
|
|
fFBOInited := True;
|
|
|
|
|
|
|
|
|
|
glGenFramebuffers(1, @fFBO);
|
|
|
|
|
glGenRenderbuffers(1, @fRenderBuf);
|
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, fRenderBuf);
|
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, GetMaxFBOSize, GetMaxFBOSize);
|
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fFBO);
|
|
|
|
|
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, fRenderBuf);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//We have to use this method EVERY time we want to bind texture. Otherwise collisions could happen
|
|
|
|
|
//
|
|
|
|
|
//Do not bind same texture again, it can drastically change render performance
|
|
|
|
|
//F.e. on an average Map (Cube 256x256) when full map is shown in viewport
|
|
|
|
|
//there are only ~10k new texture binds, when all other ~30k binds can be skipped
|
|
|
|
|
class procedure TKMRender.BindTexture(aTexId: Cardinal);
|
|
|
|
|
begin
|
|
|
|
|
KMRenderMobileBindTexture(aTexId);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class procedure TKMRender.InvalidateTextureBindingCache;
|
|
|
|
|
begin
|
|
|
|
|
KMRenderMobileInvalidateTextureBindingCache;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
procedure TKMRender.Resize(aWidth, aHeight: Integer);
|
|
|
|
|
begin
|
|
|
|
|
fScreenX := max(aWidth, 1);
|
|
|
|
|
fScreenY := max(aHeight, 1);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
procedure TKMRender.SetRenderMode(aRenderMode: TKMRenderMode);
|
|
|
|
|
begin
|
|
|
|
|
if fBlind then Exit;
|
|
|
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION); //Change Matrix Mode to Projection
|
|
|
|
|
glLoadIdentity; //Reset View
|
|
|
|
|
|
|
|
|
|
//In 2D mode we use Z-test to clip terrain shadows behind mountains
|
|
|
|
|
//1 unit for each tile strip. 512 means we can handle up to 512x512 maps
|
|
|
|
|
case aRenderMode of
|
|
|
|
|
rm2D: glOrtho(0, fScreenX, fScreenY, 0, -100, 100);
|
|
|
|
|
rm3D: gluPerspective(80, -fScreenX/fScreenY, 0.1, 5000);
|
|
|
|
|
end;
|
|
|
|
|
glMatrixMode(GL_MODELVIEW); //Return to the modelview matrix
|
|
|
|
|
glLoadIdentity; //Reset View
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class function TKMRender.GenerateTextureCommon(aMinFilter, aMagFilter: TKMFilterType): Cardinal;
|
|
|
|
|
begin
|
|
|
|
|
Result := KMRenderMobileCreateTexture(aMinFilter, aMagFilter);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Generate texture out of TCardinalArray
|
|
|
|
|
class function TKMRender.GenTexture(DestX, DestY: Word; const Data: Pointer; Mode: TKMTexFormat; aMinFilter, aMagFilter: TKMFilterType): Cardinal;
|
|
|
|
|
var
|
|
|
|
|
texture: Cardinal;
|
|
|
|
|
begin
|
|
|
|
|
texture := GenerateTextureCommon(aMinFilter, aMagFilter);
|
|
|
|
|
try
|
|
|
|
|
UpdateTexture(texture, DestX, DestY, Mode, Data);
|
|
|
|
|
Result := texture;
|
|
|
|
|
except
|
|
|
|
|
DeleteTexture(texture);
|
|
|
|
|
raise;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class procedure TKMRender.DeleteTexture(aTex: Cardinal);
|
|
|
|
|
begin
|
|
|
|
|
KMRenderMobileDeleteTexture(aTex);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function TKMRender.GetMaxFBOSize: Cardinal;
|
|
|
|
|
begin
|
|
|
|
|
Result := Min(fMaxViewportDim, MAX_FBO_BUFFER_SIZE);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class procedure TKMRender.GraphicsContextChanged(aAvailable: Boolean);
|
|
|
|
|
begin
|
|
|
|
|
if aAvailable then
|
|
|
|
|
KMRenderMobileInitializeCurrentContext
|
|
|
|
|
else
|
|
|
|
|
KMRenderMobileInvalidateCurrentContext;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class function TKMRender.TextureBackendReady: Boolean;
|
|
|
|
|
begin
|
|
|
|
|
Result := KMRenderMobileTextureBackendReady;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class function TKMRender.GetMaxTexSize: Cardinal;
|
|
|
|
|
begin
|
|
|
|
|
Result := KMRenderMobileMaxTextureSize;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class function TKMRender.GetMaxViewportDim: Cardinal;
|
|
|
|
|
begin
|
|
|
|
|
if fMaxViewportDim > 0 then Exit(fMaxViewportDim);
|
|
|
|
|
|
|
|
|
|
//Get max supported dimensions for viewport (used in off-screen render, FBO)
|
|
|
|
|
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, @fMaxViewportDim);
|
|
|
|
|
Result := fMaxViewportDim;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Update texture with TCardinalArray
|
|
|
|
|
class procedure TKMRender.UpdateTexture(aTexture: Cardinal; DestX, DestY: Word; Mode: TKMTexFormat; const Data: Pointer);
|
|
|
|
|
begin
|
|
|
|
|
KMRenderMobileUpdateTexture(aTexture, DestX, DestY, Mode, Data);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
procedure TKMRender.ReadRenderedToScreenPixels(var aWidth, aHeight: Integer; var aPixelData: TKMCardinalArray);
|
|
|
|
|
begin
|
|
|
|
|
SaveBufferToFile(False, aWidth, aHeight, aPixelData);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
procedure TKMRender.ReadRenderedToFBOPixels(var aWidth, aHeight: Integer; var aPixelData: TKMCardinalArray);
|
|
|
|
|
begin
|
|
|
|
|
SaveBufferToFile(True, aWidth, aHeight, aPixelData);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
procedure TKMRender.SaveBufferToFile(aUseFBOBuffer: Boolean; var aWidth, aHeight: Integer; var aPixelData: TKMCardinalArray);
|
|
|
|
|
{$IFDEF WDC}
|
|
|
|
|
var
|
|
|
|
|
I, K: Integer;
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
begin
|
|
|
|
|
{$IFDEF WDC}
|
|
|
|
|
aWidth := ScreenX;
|
|
|
|
|
aHeight := ScreenY;
|
|
|
|
|
|
|
|
|
|
if aUseFBOBuffer then
|
|
|
|
|
begin
|
|
|
|
|
// Read from offscreen buffer
|
|
|
|
|
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
|
|
|
|
// Bind to FBO framebuffer
|
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, fFBO);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
// Read from onscreen buffer
|
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
|
|
|
|
|
|
|
|
|
SetLength(aPixelData, aWidth * aHeight + 1);
|
|
|
|
|
glReadPixels(0, 0, aWidth, aHeight, GL_BGRA, GL_UNSIGNED_BYTE, @aPixelData[0]);
|
|
|
|
|
|
|
|
|
|
if aUseFBOBuffer then
|
|
|
|
|
// Return to onscreen rendering:
|
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
|
|
|
|
|
|
|
|
|
//Mirror verticaly
|
|
|
|
|
for i := 0 to (aHeight div 2) - 1 do
|
|
|
|
|
for K := 0 to aWidth - 1 do
|
|
|
|
|
SwapInt(aPixelData[i * aWidth + K], aPixelData[((aHeight - 1) - i) * aWidth + K]);
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
procedure TKMRender.BeginFrame;
|
|
|
|
|
begin
|
|
|
|
|
if fBlind then Exit;
|
|
|
|
|
|
|
|
|
|
if SAVE_MAP_TO_FBO_RENDER then
|
|
|
|
|
begin
|
|
|
|
|
if not fFBOInited then
|
|
|
|
|
// Init once
|
|
|
|
|
InitFBO // glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fFBO) is called while initialization
|
|
|
|
|
else
|
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fFBO);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
|
|
|
|
|
SetRenderMode(rm2D);
|
|
|
|
|
|
|
|
|
|
//RC.Activate for OSX
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Render highlight overlay to make whole picture look brighter (more saturated)
|
|
|
|
|
procedure TKMRender.RenderBrightness(aValue: Byte);
|
|
|
|
|
begin
|
|
|
|
|
if fBlind then Exit;
|
|
|
|
|
|
|
|
|
|
//There will be no change to image anyway
|
|
|
|
|
if aValue = 0 then Exit;
|
|
|
|
|
|
|
|
|
|
BindTexture(0); // We have to reset texture to default (0), because it could be bind to any other texture (atlas)
|
|
|
|
|
|
|
|
|
|
glLoadIdentity;
|
|
|
|
|
glBlendFunc(GL_DST_COLOR, GL_ONE);
|
|
|
|
|
glColor4f(aValue/20, aValue/20, aValue/20, aValue/20);
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
glkRect(0, 0, ScreenX, ScreenY);
|
|
|
|
|
glEnd;
|
|
|
|
|
|
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end.
|