Cleanup in texture structures and enums, added wrap mode to TextureCreateParams and fixed wrap mode in foreground texture (lens flare effect)

dev
Tomasz Kapuściński 2022-05-08 18:23:37 +02:00
parent a25ce2d5df
commit 56e0c915b5
13 changed files with 97 additions and 176 deletions

View File

@ -103,7 +103,7 @@ void CSettings::SaveSettings()
GetConfigFile().SetFloatProperty("Setup", "ShadowColor", engine->GetShadowColor());
GetConfigFile().SetFloatProperty("Setup", "ShadowRange", engine->GetShadowRange());
GetConfigFile().SetIntProperty("Setup", "MSAA", engine->GetMultiSample());
GetConfigFile().SetIntProperty("Setup", "FilterMode", engine->GetTextureFilterMode());
GetConfigFile().SetIntProperty("Setup", "FilterMode", static_cast<int>(engine->GetTextureFilterMode()));
GetConfigFile().SetBoolProperty("Setup", "ShadowMapping", engine->GetShadowMapping());
GetConfigFile().SetBoolProperty("Setup", "ShadowMappingQuality", engine->GetShadowMappingQuality());
GetConfigFile().SetIntProperty("Setup", "ShadowMappingResolution",
@ -265,7 +265,7 @@ void CSettings::LoadSettings()
engine->SetMultiSample(iValue);
if (GetConfigFile().GetIntProperty("Setup", "FilterMode", iValue))
engine->SetTextureFilterMode(static_cast<Gfx::TexFilter>(iValue));
engine->SetTextureFilterMode(static_cast<Gfx::TextureFilter>(iValue));
if (GetConfigFile().GetBoolProperty("Setup", "ShadowMapping", bValue))
engine->SetShadowMapping(bValue);

View File

@ -328,7 +328,7 @@ public:
//! Creates a depth texture with specific dimensions and depth
virtual Texture CreateDepthTexture(int width, int height, int depth) = 0;
//! Updates a part of texture from raw image data
virtual void UpdateTexture(const Texture& texture, const glm::ivec2& offset, ImageData* data, TexImgFormat format) = 0;
virtual void UpdateTexture(const Texture& texture, const glm::ivec2& offset, ImageData* data, TextureFormat format) = 0;
//! Deletes a given texture, freeing it from video memory
virtual void DestroyTexture(const Texture &texture) = 0;
//! Deletes all textures created so far

View File

@ -24,9 +24,6 @@
#pragma once
#include "graphics/core/color.h"
#include <glm/glm.hpp>
@ -36,10 +33,10 @@ namespace Gfx
/**
* \enum TexImgFormat
* \enum TextureFormat
* \brief Format of image data
*/
enum class TexImgFormat : unsigned char
enum class TextureFormat : unsigned char
{
//! Try to determine automatically (may not work)
AUTO,
@ -54,95 +51,31 @@ enum class TexImgFormat : unsigned char
};
/**
* \enum TexFilter
* \enum TextureFilter
* \brief General texture filtering mode
*
* Corresponds to typical options in game graphics settings.
*/
enum TexFilter
enum class TextureFilter : unsigned char
{
TEX_FILTER_NEAREST,
TEX_FILTER_BILINEAR,
TEX_FILTER_TRILINEAR
};
/**
* \enum TexMinFilter
* \brief Texture minification filter
*
* Corresponds to OpenGL modes but should translate to DirectX too.
*/
enum TexMinFilter
{
TEX_MIN_FILTER_NEAREST,
TEX_MIN_FILTER_LINEAR,
TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST,
TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST,
TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR,
TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR
};
/**
* \enum TexMagFilter
* \brief Texture magnification filter
*/
enum TexMagFilter
{
TEX_MAG_FILTER_NEAREST,
TEX_MAG_FILTER_LINEAR
//! Nearest-neighbor filtering
NEAREST,
//! Linear filtering
BILINEAR,
//! Linear filtering with mipmapping
TRILINEAR,
};
/**
* \enum TexWrapMode
* \brief Wrapping mode for texture coords
*/
enum TexWrapMode
enum class TextureWrapMode : unsigned char
{
TEX_WRAP_CLAMP,
TEX_WRAP_CLAMP_TO_BORDER,
TEX_WRAP_REPEAT
};
/**
* \enum TexMixOperation
* \brief Multitexture mixing operation
*/
enum TexMixOperation
{
//! Default operation on default params (modulate on computed & texture)
TEX_MIX_OPER_DEFAULT,
//! = Arg1
TEX_MIX_OPER_REPLACE,
//! = Arg1 * Arg2
TEX_MIX_OPER_MODULATE,
//! = Arg1 + Arg2
TEX_MIX_OPER_ADD,
//! = Arg1 - Arg2
TEX_MIX_OPER_SUBTRACT
};
/**
* \enum TexMixArgument
* \brief Multitexture mixing argument
*/
enum TexMixArgument
{
//! Color from current texture
TEX_MIX_ARG_TEXTURE,
//! Color from texture unit 0
TEX_MIX_ARG_TEXTURE_0,
//! Color from texture unit 1
TEX_MIX_ARG_TEXTURE_1,
//! Color from texture unit 2
TEX_MIX_ARG_TEXTURE_2,
//! Color from texture unit 3
TEX_MIX_ARG_TEXTURE_3,
//! Color computed by previous texture unit (current in DirectX; previous in OpenGL)
TEX_MIX_ARG_COMPUTED_COLOR,
//! (Source) color of textured fragment (diffuse in DirectX; primary color in OpenGL)
TEX_MIX_ARG_SRC_COLOR,
//! Constant color (texture factor in DirectX; texture env color in OpenGL)
TEX_MIX_ARG_FACTOR
//! UVs are clamped to edges
CLAMP,
//! UVs are repeated
REPEAT,
};
/**
@ -157,52 +90,13 @@ struct TextureCreateParams
//! Whether to generate mipmaps
bool mipmap = false;
//! Format of source image data
TexImgFormat format = TexImgFormat::RGB;
TextureFormat format = TextureFormat::RGB;
//! General texture filtering mode
TexFilter filter = TEX_FILTER_NEAREST;
TextureFilter filter = TextureFilter::NEAREST;
//! Wrap mode for texture coordinates
TextureWrapMode wrap = TextureWrapMode::REPEAT;
//! Pad the image to nearest power of 2 dimensions
bool padToNearestPowerOfTwo = false;
//! Loads the default values
void LoadDefault()
{
*this = TextureCreateParams();
}
};
/**
* \struct TextureStageParams
* \brief Parameters for a texture unit
*
* These params define the behavior of texturing units (stages).
* They can be changed freely and are features of graphics engine, not any particular texture.
*/
struct TextureStageParams
{
//! Mixing operation done on color values
TexMixOperation colorOperation = TEX_MIX_OPER_DEFAULT;
//! 1st argument of color operations
TexMixArgument colorArg1 = TEX_MIX_ARG_COMPUTED_COLOR;
//! 2nd argument of color operations
TexMixArgument colorArg2 = TEX_MIX_ARG_TEXTURE;
//! Mixing operation done on alpha values
TexMixOperation alphaOperation = TEX_MIX_OPER_DEFAULT;
//! 1st argument of alpha operations
TexMixArgument alphaArg1 = TEX_MIX_ARG_COMPUTED_COLOR;
//! 2nd argument of alpha operations
TexMixArgument alphaArg2 = TEX_MIX_ARG_TEXTURE;
//! Wrap mode for 1st tex coord
TexWrapMode wrapS = TEX_WRAP_REPEAT;
//! Wrap mode for 2nd tex coord
TexWrapMode wrapT = TEX_WRAP_REPEAT;
//! Constant color factor (for TEX_MIX_ARG_FACTOR)
Color factor;
//! Loads the default values
void LoadDefault()
{
*this = TextureStageParams();
}
};
/**
@ -217,9 +111,9 @@ struct Texture
//! ID of the texture in graphics engine; 0 = invalid texture
unsigned int id = 0;
//! Size of texture
glm::ivec2 size;
glm::ivec2 size = { 0, 0 };
//! Original size of texture (as loaded from image)
glm::ivec2 originalSize;
glm::ivec2 originalSize = { 0, 0 };
//! Whether the texture has alpha channel
bool alpha = false;

View File

@ -276,11 +276,11 @@ CEngine::CEngine(CApplication *app, CSystemUtils* systemUtils)
m_shadowColor = 0.5f;
m_defaultTexParams.format = TexImgFormat::AUTO;
m_defaultTexParams.filter = TEX_FILTER_BILINEAR;
m_defaultTexParams.format = TextureFormat::AUTO;
m_defaultTexParams.filter = TextureFilter::BILINEAR;
m_terrainTexParams.format = TexImgFormat::AUTO;
m_terrainTexParams.filter = TEX_FILTER_BILINEAR;
m_terrainTexParams.format = TextureFormat::AUTO;
m_terrainTexParams.filter = TextureFilter::BILINEAR;
// Compute bias matrix for shadow mapping
glm::mat4 temp1, temp2;
@ -418,8 +418,8 @@ bool CEngine::Create()
Math::LoadOrthoProjectionMatrix(m_matProjInterface, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
TextureCreateParams params;
params.format = TexImgFormat::AUTO;
params.filter = TEX_FILTER_NEAREST;
params.format = TextureFormat::AUTO;
params.filter = TextureFilter::NEAREST;
params.mipmap = false;
m_miceTexture = LoadTexture("textures/interface/mouse.png", params);
@ -1943,8 +1943,14 @@ bool CEngine::LoadAllTextures()
else
m_backgroundTex.SetInvalid();
if (! m_foregroundName.empty())
m_foregroundTex = LoadTexture(m_foregroundName);
if (!m_foregroundName.empty())
{
TextureCreateParams params = m_defaultTexParams;
params.wrap = TextureWrapMode::CLAMP;
params.filter = TextureFilter::BILINEAR;
params.mipmap = false;
m_foregroundTex = LoadTexture(m_foregroundName, params);
}
else
m_foregroundTex.SetInvalid();
@ -2347,8 +2353,14 @@ void CEngine::SetForegroundName(const std::string& name)
m_foregroundName = name;
if (! m_foregroundName.empty() && !m_foregroundTex.Valid())
m_foregroundTex = LoadTexture(m_foregroundName);
if (!m_foregroundName.empty() && !m_foregroundTex.Valid())
{
TextureCreateParams params;
params.wrap = TextureWrapMode::CLAMP;
params.filter = TextureFilter::BILINEAR;
params.mipmap = false;
m_foregroundTex = LoadTexture(m_foregroundName, params);
}
}
void CEngine::SetOverFront(bool front)
@ -2399,16 +2411,16 @@ float CEngine::GetClippingDistance()
return m_clippingDistance;
}
void CEngine::SetTextureFilterMode(TexFilter value)
void CEngine::SetTextureFilterMode(TextureFilter value)
{
if(m_defaultTexParams.filter == value && m_terrainTexParams.filter == value) return;
m_defaultTexParams.filter = m_terrainTexParams.filter = value;
m_defaultTexParams.mipmap = m_terrainTexParams.mipmap = (value == TEX_FILTER_TRILINEAR);
m_defaultTexParams.mipmap = m_terrainTexParams.mipmap = (value == TextureFilter::TRILINEAR);
ReloadAllTextures();
}
TexFilter CEngine::GetTextureFilterMode()
TextureFilter CEngine::GetTextureFilterMode()
{
return m_terrainTexParams.filter;
}
@ -3177,8 +3189,8 @@ void CEngine::Capture3DScene()
image.surface = SDL_CreateRGBSurfaceFrom(blured.get(), newWidth, newHeight, 32, 0, 0, 0, 0, 0xFF000000);
TextureCreateParams params;
params.filter = TEX_FILTER_BILINEAR;
params.format = TexImgFormat::RGBA;
params.filter = TextureFilter::BILINEAR;
params.format = TextureFormat::RGBA;
params.mipmap = false;
m_capturedWorldTexture = m_device->CreateTexture(&image, params);

View File

@ -850,8 +850,8 @@ public:
//@{
//! Management the texture filter mode
// NOTE: This is an user configuration setting
void SetTextureFilterMode(TexFilter value);
TexFilter GetTextureFilterMode();
void SetTextureFilterMode(TextureFilter value);
TextureFilter GetTextureFilterMode();
//@}
//@{

View File

@ -1364,7 +1364,7 @@ CharTexture CText::CreateCharTexture(UTF8Char ch, CachedFont* font)
Texture tex;
tex.id = texture.id;
m_device->UpdateTexture(tex, texture.charPos, &imageData, TexImgFormat::RGBA);
m_device->UpdateTexture(tex, texture.charPos, &imageData, TextureFormat::RGBA);
imageData.surface = nullptr;
@ -1402,8 +1402,8 @@ FontTexture CText::CreateFontTexture(const glm::ivec2& tileSize)
data.surface = textureSurface;
TextureCreateParams createParams;
createParams.format = TexImgFormat::RGBA;
createParams.filter = TEX_FILTER_NEAREST;
createParams.format = TextureFormat::RGBA;
createParams.filter = TextureFilter::NEAREST;
createParams.mipmap = false;
Texture tex = m_device->CreateTexture(&data, createParams);

View File

@ -356,15 +356,15 @@ Texture CGL33Device::CreateTexture(ImageData *data, const TextureCreateParams &p
switch (params.filter)
{
case TEX_FILTER_NEAREST:
case TextureFilter::NEAREST:
minF = GL_NEAREST;
magF = GL_NEAREST;
break;
case TEX_FILTER_BILINEAR:
case TextureFilter::BILINEAR:
minF = GL_LINEAR;
magF = GL_LINEAR;
break;
case TEX_FILTER_TRILINEAR:
case TextureFilter::TRILINEAR:
minF = GL_LINEAR_MIPMAP_LINEAR;
magF = GL_LINEAR;
mipmapLevel = CEngine::GetInstance().GetTextureMipmapLevel();
@ -374,6 +374,21 @@ Texture CGL33Device::CreateTexture(ImageData *data, const TextureCreateParams &p
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minF);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magF);
GLint wrap = GL_REPEAT;
switch (params.wrap)
{
case TextureWrapMode::REPEAT:
wrap = GL_REPEAT;
break;
case TextureWrapMode::CLAMP:
wrap = GL_CLAMP;
break;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
// Set mipmap level and automatic mipmap generation if neccesary
if (params.mipmap)
{
@ -457,7 +472,7 @@ Texture CGL33Device::CreateDepthTexture(int width, int height, int depth)
return result;
}
void CGL33Device::UpdateTexture(const Texture& texture, const glm::ivec2& offset, ImageData* data, TexImgFormat format)
void CGL33Device::UpdateTexture(const Texture& texture, const glm::ivec2& offset, ImageData* data, TextureFormat format)
{
if (texture.id == 0) return;

View File

@ -110,7 +110,7 @@ public:
Texture CreateTexture(CImage *image, const TextureCreateParams &params) override;
Texture CreateTexture(ImageData *data, const TextureCreateParams &params) override;
Texture CreateDepthTexture(int width, int height, int depth) override;
void UpdateTexture(const Texture& texture, const glm::ivec2& offset, ImageData* data, TexImgFormat format) override;
void UpdateTexture(const Texture& texture, const glm::ivec2& offset, ImageData* data, TextureFormat format) override;
void DestroyTexture(const Texture &texture) override;
void DestroyAllTextures() override;

View File

@ -522,7 +522,7 @@ std::unique_ptr<CFrameBufferPixels> GetGLFrameBufferPixels(const glm::ivec2& siz
return pixels;
}
PreparedTextureData PrepareTextureData(ImageData* imageData, TexImgFormat format)
PreparedTextureData PrepareTextureData(ImageData* imageData, TextureFormat format)
{
PreparedTextureData texData;
@ -530,27 +530,27 @@ PreparedTextureData PrepareTextureData(ImageData* imageData, TexImgFormat format
texData.sourceFormat = 0;
if (format == TexImgFormat::RGB)
if (format == TextureFormat::RGB)
{
texData.sourceFormat = GL_RGB;
texData.alpha = false;
}
else if (format == TexImgFormat::BGR)
else if (format == TextureFormat::BGR)
{
texData.sourceFormat = GL_BGR;
texData.alpha = false;
}
else if (format == TexImgFormat::RGBA)
else if (format == TextureFormat::RGBA)
{
texData.sourceFormat = GL_RGBA;
texData.alpha = true;
}
else if (format == TexImgFormat::BGRA)
else if (format == TextureFormat::BGRA)
{
texData.sourceFormat = GL_BGRA;
texData.alpha = true;
}
else if (format == TexImgFormat::AUTO)
else if (format == TextureFormat::AUTO)
{
if (imageData->surface->format->BytesPerPixel == 4)
{

View File

@ -43,7 +43,7 @@ class CFrameBufferPixels;
struct DeviceConfig;
enum class PrimitiveType : unsigned char;
enum class Type : unsigned char;
enum class TexImgFormat : unsigned char;
enum class TextureFormat : unsigned char;
bool InitializeGLEW();
@ -99,7 +99,7 @@ struct PreparedTextureData
bool alpha = false;
};
PreparedTextureData PrepareTextureData(ImageData* imageData, TexImgFormat format);
PreparedTextureData PrepareTextureData(ImageData* imageData, TextureFormat format);
std::unique_ptr<CFrameBufferPixels> GetGLFrameBufferPixels(const glm::ivec2& size);

View File

@ -1186,8 +1186,8 @@ void CEdit::DrawImage(const glm::vec2& pos, std::string name, float width,
//m_engine->SetState(Gfx::ENG_RSTATE_NORMAL);
Gfx::TextureCreateParams params;
params.format = Gfx::TexImgFormat::AUTO;
params.filter = Gfx::TEX_FILTER_BILINEAR;
params.format = Gfx::TextureFormat::AUTO;
params.filter = Gfx::TextureFilter::BILINEAR;
params.padToNearestPowerOfTwo = true;
Gfx::Texture tex = m_engine->LoadTexture(PrepareImageFilename(name), params);

View File

@ -126,8 +126,8 @@ void CImage::Draw()
if ( m_filename[0] != 0 ) // displays an image?
{
Gfx::TextureCreateParams params;
params.format = Gfx::TexImgFormat::AUTO;
params.filter = Gfx::TEX_FILTER_BILINEAR;
params.format = Gfx::TextureFormat::AUTO;
params.filter = Gfx::TextureFilter::BILINEAR;
params.padToNearestPowerOfTwo = true;
Gfx::Texture tex = m_engine->LoadTexture(m_filename, params);
renderer->SetTexture(tex);

View File

@ -186,9 +186,9 @@ void CScreenSetupGraphics::CreateInterface()
pes = pw->CreateEnumSlider(pos, ddim, 0, EVENT_INTERFACE_TEXTURE_FILTER);
pes->SetState(STATE_SHADOW);
pes->SetPossibleValues({
{ Gfx::TEX_FILTER_NEAREST, "Nearest" },
{ Gfx::TEX_FILTER_BILINEAR, "Bilinear" },
{ Gfx::TEX_FILTER_TRILINEAR, "Trilinear" }
{ static_cast<int>(Gfx::TextureFilter::NEAREST), "Nearest" },
{ static_cast<int>(Gfx::TextureFilter::BILINEAR), "Bilinear" },
{ static_cast<int>(Gfx::TextureFilter::TRILINEAR), "Trilinear" }
});
pos.y += ddim.y/2;
pos.x += 0.005f;
@ -418,13 +418,13 @@ void CScreenSetupGraphics::UpdateSetupButtons()
pes = static_cast<CEnumSlider*>(pw->SearchControl(EVENT_INTERFACE_TEXTURE_FILTER));
if ( pes != nullptr )
{
pes->SetVisibleValue(m_engine->GetTextureFilterMode());
pes->SetVisibleValue(static_cast<int>(m_engine->GetTextureFilterMode()));
}
pes = static_cast<CEnumSlider*>(pw->SearchControl(EVENT_INTERFACE_TEXTURE_MIPMAP));
if ( pes != nullptr )
{
pes->SetState(STATE_ENABLE, m_engine->GetTextureFilterMode() == Gfx::TEX_FILTER_TRILINEAR);
pes->SetState(STATE_ENABLE, m_engine->GetTextureFilterMode() == Gfx::TextureFilter::TRILINEAR);
pes->SetVisibleValue(m_engine->GetTextureMipmapLevel());
}
@ -471,7 +471,7 @@ void CScreenSetupGraphics::ChangeSetupButtons()
if ( pes != nullptr )
{
int valueIndex = pes->GetVisibleValueIndex();
m_engine->SetTextureFilterMode(static_cast<Gfx::TexFilter>(valueIndex));
m_engine->SetTextureFilterMode(static_cast<Gfx::TextureFilter>(valueIndex));
}
pes = static_cast<CEnumSlider*>(pw->SearchControl(EVENT_INTERFACE_TEXTURE_MIPMAP));
@ -543,9 +543,9 @@ void CScreenSetupGraphics::ChangeSetupQuality(int quality)
if ( quality == 0 ) m_engine->SetTextureAnisotropyLevel(2);
if ( quality > 0 ) m_engine->SetTextureAnisotropyLevel(8);
if ( quality < 0 ) { m_engine->SetTextureFilterMode(Gfx::TEX_FILTER_BILINEAR); }
if ( quality == 0 ) { m_engine->SetTextureFilterMode(Gfx::TEX_FILTER_TRILINEAR); m_engine->SetTextureMipmapLevel(4); m_engine->SetTextureAnisotropyLevel(4); }
if ( quality > 0 ) { m_engine->SetTextureFilterMode(Gfx::TEX_FILTER_TRILINEAR); m_engine->SetTextureMipmapLevel(8); m_engine->SetTextureAnisotropyLevel(8); }
if ( quality < 0 ) { m_engine->SetTextureFilterMode(Gfx::TextureFilter::BILINEAR); }
if ( quality == 0 ) { m_engine->SetTextureFilterMode(Gfx::TextureFilter::TRILINEAR); m_engine->SetTextureMipmapLevel(4); m_engine->SetTextureAnisotropyLevel(4); }
if ( quality > 0 ) { m_engine->SetTextureFilterMode(Gfx::TextureFilter::TRILINEAR); m_engine->SetTextureMipmapLevel(8); m_engine->SetTextureAnisotropyLevel(8); }
if ( quality < 0 ) { m_engine->SetShadowMapping(false); m_engine->SetShadowMappingQuality(false); }
else { m_engine->SetShadowMapping(true); m_engine->SetShadowMappingQuality(true); m_engine->SetShadowMappingOffscreen(true); }