Browse Source

Asset: Implement assimp texture loading

master
Riyyi 6 months ago
parent
commit
79b4f1c3d1
  1. 30
      src/inferno/asset/texture.cpp
  2. 3
      src/inferno/asset/texture.h

30
src/inferno/asset/texture.cpp

@ -8,6 +8,7 @@
#include <cstdint> // uint8_t, uint32_t #include <cstdint> // uint8_t, uint32_t
#include <memory> // std::shared_ptr #include <memory> // std::shared_ptr
#include "assimp/texture.h"
#include "glad/glad.h" #include "glad/glad.h"
#include "ruc/meta/assert.h" #include "ruc/meta/assert.h"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
@ -56,6 +57,32 @@ std::shared_ptr<Texture2D> Texture2D::create(std::string_view path)
return result; return result;
} }
std::shared_ptr<Texture2D> Texture2D::create(aiTexture* texture)
{
auto result = std::shared_ptr<Texture2D>(new Texture2D(texture->mFilename.C_Str()));
int width = static_cast<int>(texture->mWidth);
int height = static_cast<int>(texture->mHeight);
int channels = 0;
unsigned char* data = reinterpret_cast<unsigned char*>(texture->pcData);
bool isCompressed = height == 0; // height 0 is compression, byte length stored in width variable
if (isCompressed) {
stbi_set_flip_vertically_on_load(0);
data = stbi_load_from_memory(data, width, &width, &height, &channels, STBI_default);
}
else {
channels = 4; // ARGB888
// TODO: imprement format hints? `archFormatHint'
VERIFY_NOT_REACHED();
}
result->init(width, height, channels);
result->create(data);
return result;
}
void Texture2D::bind(uint32_t unit) const void Texture2D::bind(uint32_t unit) const
{ {
// Set active unit // Set active unit
@ -198,6 +225,3 @@ void TextureCubemap::create()
} }
} // namespace Inferno } // namespace Inferno
// FIXME
// auto texture = (type == Texture::TwoDimensional) ? Texture2D::create(path) : TextureCubemap::create(path);

3
src/inferno/asset/texture.h

@ -12,6 +12,8 @@
#include "inferno/asset/asset-manager.h" #include "inferno/asset/asset-manager.h"
struct aiTexture;
namespace Inferno { namespace Inferno {
class Texture2D; class Texture2D;
@ -63,6 +65,7 @@ public:
// Factory function // Factory function
static std::shared_ptr<Texture2D> create(std::string_view path); static std::shared_ptr<Texture2D> create(std::string_view path);
static std::shared_ptr<Texture2D> create(aiTexture* texture);
virtual void bind(uint32_t unit = 0) const override; virtual void bind(uint32_t unit = 0) const override;
virtual void unbind() const override; virtual void unbind() const override;

Loading…
Cancel
Save