Fix GL_LINEAR_MIPMAP_LINEAR error
This commit is contained in:
parent
d872eb0b55
commit
8fd8d5ec29
@ -3,9 +3,9 @@
|
||||
## TODO
|
||||
|
||||
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
|
||||
- [ ] Fix apparently incorrect light radius of lights visible through scaled portals
|
||||
- [x] Fix GL_LINEAR_MIPMAP_LINEAR error
|
||||
|
||||
Add new features:
|
||||
- [ ] Text and UI rendering
|
||||
|
||||
@ -675,7 +675,8 @@ void game::Sector::BakeLightmap()
|
||||
GL_RGB,
|
||||
GL_RGB,
|
||||
GL_UNSIGNED_BYTE,
|
||||
GL_LINEAR
|
||||
true, // LINEAR
|
||||
false // No mipmaps
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@ -4,28 +4,58 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
gfx::Texture::Texture(GLuint width, GLuint height, const void* data, GLint internalformat, GLenum format, GLenum type, GLenum filter) {
|
||||
glGenTextures(1, &m_id);
|
||||
static void GetGLFilterModes(bool linear, bool mipmaps, GLenum& filter_min, GLenum& filter_mag) {
|
||||
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)
|
||||
throw std::runtime_error("Nelze vytvorit texturu!");
|
||||
gfx::Texture::Texture(GLuint width, GLuint height, const void* data, GLint internalformat, GLenum format, GLenum type, bool linear, bool mipmaps) {
|
||||
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);
|
||||
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);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, type, data);
|
||||
|
||||
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() {
|
||||
glDeleteTextures(1, &m_id);
|
||||
glDeleteTextures(1, &m_id);
|
||||
}
|
||||
|
||||
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;
|
||||
unsigned char* data = stbi_load(filename.c_str(), &width, &height, &channels, 4);
|
||||
|
||||
if (!data) {
|
||||
throw std::runtime_error("Failed to load texture from file: " + filename);
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
throw std::runtime_error("Failed to load texture from file: " + filename);
|
||||
}
|
||||
|
||||
std::shared_ptr<Texture> texture;
|
||||
try {
|
||||
texture = std::make_shared<Texture>(width, height, data, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, GL_LINEAR_MIPMAP_LINEAR);
|
||||
} catch (const std::exception& e) {
|
||||
stbi_image_free(data);
|
||||
throw; // Rethrow the exception after freeing the data
|
||||
try {
|
||||
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);
|
||||
throw; // Rethrow the exception after freeing the data
|
||||
}
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
@ -14,7 +14,7 @@ class Texture : public NonCopyableNonMovable
|
||||
GLuint m_id;
|
||||
|
||||
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();
|
||||
|
||||
GLuint GetId() const { return m_id; }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user