Asset loading for Android (#97)
This commit is contained in:
parent
0966d21dc2
commit
6ee70824e5
3 changed files with 81 additions and 10 deletions
|
|
@ -32,6 +32,10 @@
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
|
#if defined(__ANDROID__)
|
||||||
|
#include <android/asset_manager.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace vkMeshLoader
|
namespace vkMeshLoader
|
||||||
{
|
{
|
||||||
typedef enum VertexLayout {
|
typedef enum VertexLayout {
|
||||||
|
|
@ -208,6 +212,10 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
#if defined(__ANDROID__)
|
||||||
|
AAssetManager* assetManager = nullptr;
|
||||||
|
#endif
|
||||||
|
|
||||||
std::vector<MeshEntry> m_Entries;
|
std::vector<MeshEntry> m_Entries;
|
||||||
|
|
||||||
struct Dimension
|
struct Dimension
|
||||||
|
|
@ -246,25 +254,43 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loads the mesh with some default flags
|
// Loads the mesh with some default flags
|
||||||
bool LoadMesh(const std::string& Filename)
|
bool LoadMesh(const std::string& filename)
|
||||||
{
|
{
|
||||||
int flags = aiProcess_FlipWindingOrder | aiProcess_Triangulate | aiProcess_PreTransformVertices | aiProcess_CalcTangentSpace | aiProcess_GenSmoothNormals;
|
int flags = aiProcess_FlipWindingOrder | aiProcess_Triangulate | aiProcess_PreTransformVertices | aiProcess_CalcTangentSpace | aiProcess_GenSmoothNormals;
|
||||||
|
|
||||||
return LoadMesh(Filename, flags);
|
return LoadMesh(filename, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the mesh with custom flags
|
// Load the mesh with custom flags
|
||||||
bool LoadMesh(const std::string& Filename, int flags)
|
bool LoadMesh(const std::string& filename, int flags)
|
||||||
{
|
{
|
||||||
pScene = Importer.ReadFile(Filename.c_str(), flags);
|
#if defined(__ANDROID__)
|
||||||
|
// Meshes are stored inside the apk on Android (compressed)
|
||||||
|
// So they need to be loaded via the asset manager
|
||||||
|
|
||||||
|
AAsset* asset = AAssetManager_open(assetManager, filename.c_str(), AASSET_MODE_STREAMING);
|
||||||
|
assert(asset);
|
||||||
|
size_t size = AAsset_getLength(asset);
|
||||||
|
assert(size > 0);
|
||||||
|
|
||||||
|
void *meshData = malloc(size);
|
||||||
|
AAsset_read(asset, meshData, size);
|
||||||
|
AAsset_close(asset);
|
||||||
|
|
||||||
|
pScene = Importer.ReadFileFromMemory(meshData, size, flags);
|
||||||
|
|
||||||
|
free(meshData);
|
||||||
|
#else
|
||||||
|
pScene = Importer.ReadFile(filename.c_str(), flags);
|
||||||
|
#endif
|
||||||
|
|
||||||
if (pScene)
|
if (pScene)
|
||||||
{
|
{
|
||||||
return InitFromScene(pScene, Filename);
|
return InitFromScene(pScene, filename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Error parsing '%s': '%s'\n", Filename.c_str(), Importer.GetErrorString());
|
printf("Error parsing '%s': '%s'\n", filename.c_str(), Importer.GetErrorString());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -431,17 +431,35 @@ namespace vkTools
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load a cubemap texture (single file)
|
// Load a cubemap texture (single file)
|
||||||
void loadCubemap(const char* filename, VkFormat format, VulkanTexture *texture)
|
void loadCubemap(std::string filename, VkFormat format, VulkanTexture *texture)
|
||||||
{
|
{
|
||||||
VkFormatProperties formatProperties;
|
#if defined(__ANDROID__)
|
||||||
|
assert(assetManager != nullptr);
|
||||||
|
|
||||||
|
// Textures are stored inside the apk on Android (compressed)
|
||||||
|
// So they need to be loaded via the asset manager
|
||||||
|
AAsset* asset = AAssetManager_open(assetManager, filename.c_str(), AASSET_MODE_STREAMING);
|
||||||
|
assert(asset);
|
||||||
|
size_t size = AAsset_getLength(asset);
|
||||||
|
assert(size > 0);
|
||||||
|
|
||||||
|
void *textureData = malloc(size);
|
||||||
|
AAsset_read(asset, textureData, size);
|
||||||
|
AAsset_close(asset);
|
||||||
|
|
||||||
|
gli::textureCube texCube(gli::load((const char*)textureData, size));
|
||||||
|
|
||||||
|
free(textureData);
|
||||||
|
#else
|
||||||
gli::textureCube texCube(gli::load(filename));
|
gli::textureCube texCube(gli::load(filename));
|
||||||
|
#endif
|
||||||
assert(!texCube.empty());
|
assert(!texCube.empty());
|
||||||
|
|
||||||
texture->width = (uint32_t)texCube[0].dimensions().x;
|
texture->width = (uint32_t)texCube[0].dimensions().x;
|
||||||
texture->height = (uint32_t)texCube[0].dimensions().y;
|
texture->height = (uint32_t)texCube[0].dimensions().y;
|
||||||
|
|
||||||
// Get device properites for the requested texture format
|
// Get device properites for the requested texture format
|
||||||
|
VkFormatProperties formatProperties;
|
||||||
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProperties);
|
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProperties);
|
||||||
|
|
||||||
VkImageCreateInfo imageCreateInfo = vkTools::initializers::imageCreateInfo();
|
VkImageCreateInfo imageCreateInfo = vkTools::initializers::imageCreateInfo();
|
||||||
|
|
@ -630,11 +648,29 @@ namespace vkTools
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load an array texture (single file)
|
// Load an array texture (single file)
|
||||||
void loadTextureArray(const char* filename, VkFormat format, VulkanTexture *texture)
|
void loadTextureArray(std::string filename, VkFormat format, VulkanTexture *texture)
|
||||||
{
|
{
|
||||||
VkFormatProperties formatProperties;
|
#if defined(__ANDROID__)
|
||||||
|
assert(assetManager != nullptr);
|
||||||
|
|
||||||
|
// Textures are stored inside the apk on Android (compressed)
|
||||||
|
// So they need to be loaded via the asset manager
|
||||||
|
AAsset* asset = AAssetManager_open(assetManager, filename.c_str(), AASSET_MODE_STREAMING);
|
||||||
|
assert(asset);
|
||||||
|
size_t size = AAsset_getLength(asset);
|
||||||
|
assert(size > 0);
|
||||||
|
|
||||||
|
void *textureData = malloc(size);
|
||||||
|
AAsset_read(asset, textureData, size);
|
||||||
|
AAsset_close(asset);
|
||||||
|
|
||||||
|
gli::texture2DArray tex2DArray(gli::load((const char*)textureData, size));
|
||||||
|
|
||||||
|
free(textureData);
|
||||||
|
#else
|
||||||
gli::texture2DArray tex2DArray(gli::load(filename));
|
gli::texture2DArray tex2DArray(gli::load(filename));
|
||||||
|
#endif
|
||||||
|
|
||||||
assert(!tex2DArray.empty());
|
assert(!tex2DArray.empty());
|
||||||
|
|
||||||
texture->width = tex2DArray.dimensions().x;
|
texture->width = tex2DArray.dimensions().x;
|
||||||
|
|
@ -642,6 +678,7 @@ namespace vkTools
|
||||||
texture->layerCount = tex2DArray.layers();
|
texture->layerCount = tex2DArray.layers();
|
||||||
|
|
||||||
// Get device properites for the requested texture format
|
// Get device properites for the requested texture format
|
||||||
|
VkFormatProperties formatProperties;
|
||||||
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProperties);
|
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProperties);
|
||||||
|
|
||||||
VkImageCreateInfo imageCreateInfo = vkTools::initializers::imageCreateInfo();
|
VkImageCreateInfo imageCreateInfo = vkTools::initializers::imageCreateInfo();
|
||||||
|
|
|
||||||
|
|
@ -219,6 +219,9 @@ void VulkanExampleBase::prepare()
|
||||||
createSetupCommandBuffer();
|
createSetupCommandBuffer();
|
||||||
// Create a simple texture loader class
|
// Create a simple texture loader class
|
||||||
textureLoader = new vkTools::VulkanTextureLoader(physicalDevice, device, queue, cmdPool);
|
textureLoader = new vkTools::VulkanTextureLoader(physicalDevice, device, queue, cmdPool);
|
||||||
|
#if defined(__ANDROID__)
|
||||||
|
textureLoader->assetManager = androidApp->activity->assetManager;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
VkPipelineShaderStageCreateInfo VulkanExampleBase::loadShader(std::string fileName, VkShaderStageFlagBits stage)
|
VkPipelineShaderStageCreateInfo VulkanExampleBase::loadShader(std::string fileName, VkShaderStageFlagBits stage)
|
||||||
|
|
@ -291,6 +294,11 @@ void VulkanExampleBase::loadMesh(
|
||||||
float scale)
|
float scale)
|
||||||
{
|
{
|
||||||
VulkanMeshLoader *mesh = new VulkanMeshLoader();
|
VulkanMeshLoader *mesh = new VulkanMeshLoader();
|
||||||
|
|
||||||
|
#if defined(__ANDROID__)
|
||||||
|
mesh->assetManager = androidApp->activity->assetManager;
|
||||||
|
#endif
|
||||||
|
|
||||||
mesh->LoadMesh(filename);
|
mesh->LoadMesh(filename);
|
||||||
assert(mesh->m_Entries.size() > 0);
|
assert(mesh->m_Entries.size() > 0);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue