Use cached textures when loading meshes instead of always loading the textures again for each mesh
This commit is contained in:
parent
8fd8d5ec29
commit
102bc3f2b0
@ -3,9 +3,9 @@
|
||||
## TODO
|
||||
|
||||
Fix known issues:
|
||||
- [ ] 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
|
||||
- [x] Use cached textures when loading meshes instead of always loading the textures again for each mesh
|
||||
|
||||
Add new features:
|
||||
- [ ] Text and UI rendering
|
||||
|
||||
@ -5,6 +5,8 @@
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
std::map<std::string, std::weak_ptr<gfx::Texture>> assets::Mesh::s_texture_cache;
|
||||
|
||||
assets::Mesh::Mesh(std::span<MeshVertex> verts, std::span<MeshTriangle> tris, std::span<MeshMaterial> materials) :
|
||||
va_(gfx::VA_POSITION | gfx::VA_NORMAL | gfx::VA_UV | gfx::VA_LIGHTMAP_UV, gfx::VF_CREATE_EBO)
|
||||
{
|
||||
@ -110,7 +112,7 @@ std::shared_ptr<assets::Mesh> assets::Mesh::LoadFromFile(const std::string& file
|
||||
if (!material.name.empty())
|
||||
{
|
||||
try {
|
||||
material.texture = gfx::Texture::LoadFromFile("data/" + material.name + ".png");
|
||||
material.texture = LoadTexture("data/" + material.name + ".png");
|
||||
} catch (const std::exception& e) {
|
||||
throw std::runtime_error("Failed to load texture for material '" + material.name + "': " + e.what());
|
||||
}
|
||||
@ -127,3 +129,20 @@ std::shared_ptr<assets::Mesh> assets::Mesh::LoadFromFile(const std::string& file
|
||||
}
|
||||
return mesh;
|
||||
}
|
||||
|
||||
std::shared_ptr<gfx::Texture> assets::Mesh::LoadTexture(const std::string& filename)
|
||||
{
|
||||
auto it = s_texture_cache.find(filename);
|
||||
if (it != s_texture_cache.end())
|
||||
{
|
||||
if (auto texture = it->second.lock())
|
||||
{
|
||||
return texture; // Return cached texture
|
||||
}
|
||||
}
|
||||
|
||||
auto texture = gfx::Texture::LoadFromFile(filename);
|
||||
s_texture_cache[filename] = texture; // Cache the texture
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <span>
|
||||
#include <map>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "gfx/vertex_array.hpp"
|
||||
@ -59,5 +60,9 @@ namespace assets
|
||||
std::span<const MeshMaterial> GetMaterials() const { return materials_; }
|
||||
|
||||
static std::shared_ptr<Mesh> LoadFromFile(const std::string& filename, bool load_collision);
|
||||
|
||||
private:
|
||||
static std::map<std::string, std::weak_ptr<gfx::Texture>> s_texture_cache;
|
||||
static std::shared_ptr<gfx::Texture> LoadTexture(const std::string& filename);
|
||||
};
|
||||
}
|
||||
|
||||
@ -60,6 +60,8 @@ gfx::Texture::~Texture() {
|
||||
|
||||
std::shared_ptr<gfx::Texture> gfx::Texture::LoadFromFile(const std::string& filename)
|
||||
{
|
||||
printf("Loading texture from file: %s\n", filename.c_str());
|
||||
|
||||
int width, height, channels;
|
||||
unsigned char* data = stbi_load(filename.c_str(), &width, &height, &channels, 4);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user