From 656407d4bcc9419011375f19ad756d9ab7254f89 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Wed, 22 Feb 2023 19:28:25 +0300 Subject: [PATCH] gl: implement texture filtering --- src/game/renders/opengl/r_common.pas | 5 ++++- src/game/renders/opengl/r_draw.pas | 30 ++++++++++++++++++++++++-- src/game/renders/opengl/r_map.pas | 1 + src/game/renders/opengl/r_textures.pas | 6 ++++++ src/nogl/noGL.pas | 1 + 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/game/renders/opengl/r_common.pas b/src/game/renders/opengl/r_common.pas index 42102bb..774b352 100644 --- a/src/game/renders/opengl/r_common.pas +++ b/src/game/renders/opengl/r_common.pas @@ -366,14 +366,17 @@ implementation end; procedure r_Common_DrawBackgroundImage (img: TGLTexture); - var fw, w, h: LongInt; + var fw, w, h: LongInt; OldFilter: Boolean; begin if img <> nil then begin img := BackgroundTexture.id; + OldFilter := img.filter; + r_Draw_SetFilter(img, gTextureFilter); if img.width = img.height then fw := img.width * 4 div 3 else fw := img.width; // fix aspect 4:3 r_Common_CalcAspect(fw, img.height, gScreenWidth, gScreenHeight, false, w, h); r_Draw_Texture(img, gScreenWidth div 2 - w div 2, 0, w, h, false, 255, 255, 255, 255, false); + r_Draw_SetFilter(img, OldFilter); end end; diff --git a/src/game/renders/opengl/r_draw.pas b/src/game/renders/opengl/r_draw.pas index f77b347..4f28d50 100644 --- a/src/game/renders/opengl/r_draw.pas +++ b/src/game/renders/opengl/r_draw.pas @@ -22,6 +22,8 @@ interface r_textures ; + procedure r_Draw_SetFilter (img: TGLTexture; enable: Boolean); + procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean); procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean); procedure r_Draw_TextureRepeatRotate (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer); @@ -129,7 +131,13 @@ implementation r_Draw_Rect(x, y, x + w, y + h, 0, 255, 0, 255); end; - procedure DrawTile (tile: TGLAtlasNode; x, y, w, h: Integer; flip: Boolean; rr, gg, bb, aa: Byte; blend: Boolean); + procedure r_Draw_SetFilter (img: TGLTexture; enable: Boolean); + begin + ASSERT(img <> nil); + img.filter := enable; + end; + + procedure DrawTile (tile: TGLAtlasNode; x, y, w, h: Integer; flip: Boolean; rr, gg, bb, aa: Byte; blend, filter: Boolean); var nw, nh, ax, bx, ay, by: GLfloat; l, t, r, b: Integer; begin if tile = nil then @@ -146,6 +154,24 @@ implementation by := (tile.b + 1) / nh; l := x; t := y; r := x + w; b := y + h; r_Textures_GL_Bind(tile.id); + if filter <> tile.base.filter then + begin + if filter then + begin + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + end + else + begin + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + end; + tile.base.filter := filter; + end; r_Draw_SetColor(rr, gg, bb, aa); r_Draw_EnableTexture2D(true); if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -201,7 +227,7 @@ implementation repeat n := img.GetTile(i, j); ASSERT(n <> nil); - DrawTile(n, 0, 0, n.width, n.height, flip, r, g, b, a, blend); + DrawTile(n, 0, 0, n.width, n.height, flip, r, g, b, a, blend, img.filter); glTranslatef(n.width, 0, 0); i := i + step; until i = last; diff --git a/src/game/renders/opengl/r_map.pas b/src/game/renders/opengl/r_map.pas index ee1c8b3..eba8140 100644 --- a/src/game/renders/opengl/r_map.pas +++ b/src/game/renders/opengl/r_map.pas @@ -1711,6 +1711,7 @@ implementation if gDrawBackGround and (SkyTexture <> nil) then begin r_Map_CalcSkyParallax(cx, cy, w, h, SkyTexture.width, SkyTexture.height, gMapInfo.Width, gMapInfo.Height, sx, sy, sw, sh); + r_Draw_SetFilter(SkyTexture, gTextureFilter); r_Draw_Texture(SkyTexture, sx, sy, sw, sh, false, 255, 255, 255, 255, false); end; diff --git a/src/game/renders/opengl/r_textures.pas b/src/game/renders/opengl/r_textures.pas index 56d2656..ae9d02a 100644 --- a/src/game/renders/opengl/r_textures.pas +++ b/src/game/renders/opengl/r_textures.pas @@ -47,6 +47,7 @@ interface TGLAtlas = class (TAtlas) private mID: GLuint; + mFilter: Boolean; public constructor Create (ww, hh: Integer; id: GLuint); @@ -56,6 +57,7 @@ interface function Alloc (ww, hh: Integer): TGLAtlasNode; overload; property id: GLuint read mID write mID default 0; + property filter: Boolean read mFilter write mFilter; end; TGLTexture = class @@ -65,6 +67,7 @@ interface mCols: Integer; mTile: array of TGLAtlasNode; mHints: TGLHintsSet; + mFilter: Boolean; public destructor Destroy; override; @@ -78,6 +81,7 @@ interface property cols: Integer read mCols; property lines: Integer read GetLines; property hints: TGLHintsSet read mHints; + property filter: Boolean read mFilter write mFilter; end; TGLMultiTexture = class @@ -206,6 +210,7 @@ implementation ASSERT(hh > 0); inherited Create(ww, hh); self.mID := id; + self.mFilter := false; end; destructor TGLAtlas.Destroy; @@ -378,6 +383,7 @@ implementation t.mCols := cols; // t.mLines := lines; t.mHints := hints; + t.mFilter := false; SetLength(t.mTile, cols * lines); for y := 0 to lines - 1 do begin diff --git a/src/nogl/noGL.pas b/src/nogl/noGL.pas index fe9601c..5ee98a2 100644 --- a/src/nogl/noGL.pas +++ b/src/nogl/noGL.pas @@ -70,6 +70,7 @@ interface GL_TEXTURE_WRAP_S = $2802; GL_TEXTURE_WRAP_T = $2803; GL_REPEAT = $2901; + GL_CLAMP_TO_EDGE = $812F; GL_TEXTURE_MIN_FILTER = $2801; GL_TEXTURE_MAG_FILTER = $2800; GL_RGBA = $1908; -- 2.29.2