Fix GL_LINEAR_MIPMAP_LINEAR error

This commit is contained in:
tovjemam 2025-08-16 21:35:01 +02:00
parent d872eb0b55
commit 8fd8d5ec29
4 changed files with 57 additions and 25 deletions

View File

@ -3,9 +3,9 @@
## TODO ## TODO
Fix known issues: Fix known issues:
- [ ] Fix GL_LINEAR_MIPMAP_LINEAR error
- [ ] Use cached textures when loading meshes instead of always loading the textures again for each mesh - [ ] Use cached textures when loading meshes instead of always loading the textures again for each mesh
- [ ] Fix apparently incorrect light radius of lights visible through scaled portals - [ ] Fix apparently incorrect light radius of lights visible through scaled portals
- [x] Fix GL_LINEAR_MIPMAP_LINEAR error
Add new features: Add new features:
- [ ] Text and UI rendering - [ ] Text and UI rendering

View File

@ -675,7 +675,8 @@ void game::Sector::BakeLightmap()
GL_RGB, GL_RGB,
GL_RGB, GL_RGB,
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
GL_LINEAR true, // LINEAR
false // No mipmaps
); );
} }

View File

@ -4,28 +4,58 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h> #include <stb_image.h>
gfx::Texture::Texture(GLuint width, GLuint height, const void* data, GLint internalformat, GLenum format, GLenum type, GLenum filter) { static void GetGLFilterModes(bool linear, bool mipmaps, GLenum& filter_min, GLenum& filter_mag) {
glGenTextures(1, &m_id); if (linear)
{
filter_min = GL_LINEAR;
filter_mag = GL_LINEAR;
if (mipmaps)
{
filter_min = GL_LINEAR_MIPMAP_LINEAR;
}
}
else
{
filter_min = GL_NEAREST;
filter_mag = GL_NEAREST;
if (mipmaps)
{
// Mipmaps always linear
filter_min = GL_NEAREST_MIPMAP_LINEAR;
}
}
}
if (!m_id) gfx::Texture::Texture(GLuint width, GLuint height, const void* data, GLint internalformat, GLenum format, GLenum type, bool linear, bool mipmaps) {
throw std::runtime_error("Nelze vytvorit texturu!"); glGenTextures(1, &m_id);
glBindTexture(GL_TEXTURE_2D, m_id); if (!m_id)
throw std::runtime_error("Nelze vytvorit texturu!");
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, type, data); glBindTexture(GL_TEXTURE_2D, m_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, type, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glGenerateMipmap(GL_TEXTURE_2D); GLenum filter_min, filter_mag;
GetGLFilterModes(linear, mipmaps, filter_min, filter_mag);
glBindTexture(GL_TEXTURE_2D, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_min);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_mag);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if (mipmaps)
{
glGenerateMipmap(GL_TEXTURE_2D);
}
glBindTexture(GL_TEXTURE_2D, 0);
} }
gfx::Texture::~Texture() { gfx::Texture::~Texture() {
glDeleteTextures(1, &m_id); glDeleteTextures(1, &m_id);
} }
std::shared_ptr<gfx::Texture> gfx::Texture::LoadFromFile(const std::string& filename) std::shared_ptr<gfx::Texture> gfx::Texture::LoadFromFile(const std::string& filename)
@ -33,16 +63,17 @@ std::shared_ptr<gfx::Texture> gfx::Texture::LoadFromFile(const std::string& file
int width, height, channels; int width, height, channels;
unsigned char* data = stbi_load(filename.c_str(), &width, &height, &channels, 4); unsigned char* data = stbi_load(filename.c_str(), &width, &height, &channels, 4);
if (!data) { if (!data) {
throw std::runtime_error("Failed to load texture from file: " + filename); throw std::runtime_error("Failed to load texture from file: " + filename);
} }
std::shared_ptr<Texture> texture; std::shared_ptr<Texture> texture;
try { try {
texture = std::make_shared<Texture>(width, height, data, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, GL_LINEAR_MIPMAP_LINEAR); texture = std::make_shared<Texture>(width, height, data, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, true, true);
} catch (const std::exception& e) { }
stbi_image_free(data); catch (const std::exception& e) {
throw; // Rethrow the exception after freeing the data stbi_image_free(data);
throw; // Rethrow the exception after freeing the data
} }
stbi_image_free(data); stbi_image_free(data);

View File

@ -14,7 +14,7 @@ class Texture : public NonCopyableNonMovable
GLuint m_id; GLuint m_id;
public: public:
Texture(GLuint width, GLuint height, const void* data, GLint internalformat, GLenum format, GLenum type, GLenum filter = GL_NEAREST); Texture(GLuint width, GLuint height, const void* data, GLint internalformat, GLenum format, GLenum type, bool linear, bool mipmaps);
~Texture(); ~Texture();
GLuint GetId() const { return m_id; } GLuint GetId() const { return m_id; }