upgrade to Filament 1.21.0
This commit is contained in:
@@ -1,122 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef GLTFIO_DEPENDENCY_GRAPH_H
|
||||
#define GLTFIO_DEPENDENCY_GRAPH_H
|
||||
|
||||
#include <utils/Entity.h>
|
||||
|
||||
#include <tsl/robin_map.h>
|
||||
#include <tsl/robin_set.h>
|
||||
|
||||
#include <queue>
|
||||
#include <string>
|
||||
|
||||
namespace filament {
|
||||
class MaterialInstance;
|
||||
class Texture;
|
||||
}
|
||||
|
||||
namespace gltfio {
|
||||
|
||||
/**
|
||||
* Internal graph that enables FilamentAsset to discover "ready-to-render" entities by tracking
|
||||
* the loading status of Texture objects that each entity depends on.
|
||||
*
|
||||
* Renderables connect to a set of material instances, which in turn connect to a set of parameter
|
||||
* names, which in turn connect to a set of texture objects. These relationships are not easily
|
||||
* inspectable using the Filament API or ECS.
|
||||
*
|
||||
* One graph corresponds to a single glTF asset. The graph only contains weak references, it does
|
||||
* not have ownership over any Filament objects. Here's an example:
|
||||
*
|
||||
* Entity Entity Entity Entity
|
||||
* | / \ | /
|
||||
* | / \ | /
|
||||
* Material Material Material
|
||||
* / | \ |
|
||||
* / | \ |
|
||||
* Param Param Param Param
|
||||
* \ / | |
|
||||
* \ / | |
|
||||
* Texture Texture Texture
|
||||
*
|
||||
* Note that the left-most entity in the above graph has no textures, so it becomes ready as soon as
|
||||
* finalize is called.
|
||||
*/
|
||||
class DependencyGraph {
|
||||
public:
|
||||
using Material = filament::MaterialInstance;
|
||||
using Entity = utils::Entity;
|
||||
|
||||
// Pops up to "count" ready-to-render entities off the queue.
|
||||
// If "result" is non-null, returns the number of written items.
|
||||
// If "result" is null, returns the number of available entities.
|
||||
size_t popRenderables(Entity* result, size_t count) noexcept;
|
||||
|
||||
// These are called during the initial asset loader phase.
|
||||
void addEdge(Entity entity, Material* material);
|
||||
void addEdge(Material* material, const char* parameter);
|
||||
|
||||
// This is called at the end of the initial asset loading phase.
|
||||
// Makes a guarantee that no new material nodes or parameter nodes will be added to the graph.
|
||||
void finalize();
|
||||
|
||||
// This can be called after finalization to allow for dynamic addition of entities.
|
||||
// It is slower than finalize() because it checks the readiness of existing materials.
|
||||
void refinalize();
|
||||
|
||||
// These are called after textures have created and decoded.
|
||||
void addEdge(filament::Texture* texture, Material* material, const char* parameter);
|
||||
void markAsReady(filament::Texture* texture);
|
||||
|
||||
private:
|
||||
struct TextureNode {
|
||||
filament::Texture* texture;
|
||||
bool ready;
|
||||
};
|
||||
|
||||
struct MaterialNode {
|
||||
tsl::robin_map<std::string, TextureNode*> params;
|
||||
};
|
||||
|
||||
struct EntityNode {
|
||||
tsl::robin_set<Material*> materials;
|
||||
size_t numReadyMaterials = 0;
|
||||
};
|
||||
|
||||
void checkReadiness(Material* material);
|
||||
void markAsReady(Material* material);
|
||||
TextureNode* getStatus(filament::Texture* texture);
|
||||
|
||||
// The following maps contain the directed edges in the graph.
|
||||
tsl::robin_map<Entity, EntityNode> mEntityToMaterial;
|
||||
tsl::robin_map<Material*, tsl::robin_set<Entity>> mMaterialToEntity;
|
||||
tsl::robin_map<Material*, MaterialNode> mMaterialToTexture;
|
||||
tsl::robin_map<filament::Texture*, tsl::robin_set<Material*>> mTextureToMaterial;
|
||||
|
||||
// Each texture (and its readiness flag) can be referenced from multiple nodes, so we own
|
||||
// a collection of wrapper objects in the following map. This uses std::unique_ptr to allow
|
||||
// nodes to refer to a texture wrapper using a stable weak pointer.
|
||||
tsl::robin_map<filament::Texture*, std::unique_ptr<TextureNode>> mTextureNodes;
|
||||
|
||||
std::queue<Entity> mReadyRenderables;
|
||||
bool mFinalized = false;
|
||||
};
|
||||
|
||||
} // namespace gltfio
|
||||
|
||||
#endif // GLTFIO_DEPENDENCY_GRAPH_H
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef GLTFIO_DRACO_CACHE_H
|
||||
#define GLTFIO_DRACO_CACHE_H
|
||||
|
||||
#include <cgltf.h>
|
||||
|
||||
#include <tsl/robin_map.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#ifndef GLTFIO_DRACO_SUPPORTED
|
||||
#define GLTFIO_DRACO_SUPPORTED 0
|
||||
#endif
|
||||
|
||||
namespace gltfio {
|
||||
|
||||
class DracoMesh;
|
||||
|
||||
// Manages a set of Draco meshes that can be looked up using cgltf_buffer_view.
|
||||
//
|
||||
// The cache key is the buffer view that holds the compressed data. This allows the loader to
|
||||
// avoid duplicated work when a single Draco mesh is referenced from multiple primitives.
|
||||
class DracoCache {
|
||||
public:
|
||||
DracoMesh* findOrCreateMesh(const cgltf_buffer_view* key);
|
||||
private:
|
||||
tsl::robin_map<const cgltf_buffer_view*, std::unique_ptr<DracoMesh>> mCache;
|
||||
};
|
||||
|
||||
// Decodes a Draco mesh upon construction and retains the results.
|
||||
//
|
||||
// The DracoMesh API leverages cgltf accessor structs in a way that bears explanation. These are
|
||||
// read / write parameters that tell the decoder where to write the decoded data, and what format
|
||||
// is desired. The buffer_view in the accessor should be null unless decompressed data is already
|
||||
// loaded. This tells the decoder that it should create a buffer_view and a buffer. The buffer
|
||||
// view, the buffer, and the buffer's data are all automatically freed when DracoMesh is destroyed.
|
||||
//
|
||||
// Note that in the gltfio architecture, the AssetLoader has the job of constructing VertexBuffer
|
||||
// objects while the ResourceLoader has the job of populating them asychronously. This means that
|
||||
// our Draco decoder relies on the accessor fields being 100% correct. If we had to be robust
|
||||
// against faulty accessor information, we would need to replace the VertexBuffer object that was
|
||||
// created in the AssetLoader, which would be a messy process.
|
||||
class DracoMesh {
|
||||
public:
|
||||
static DracoMesh* decode(const uint8_t* compressedData, size_t compressedSize);
|
||||
bool getFaceIndices(cgltf_accessor* destination) const;
|
||||
bool getVertexAttributes(uint32_t attributeId, cgltf_accessor* destination) const;
|
||||
~DracoMesh();
|
||||
private:
|
||||
DracoMesh(struct DracoMeshDetails* details);
|
||||
std::unique_ptr<struct DracoMeshDetails> mDetails;
|
||||
};
|
||||
|
||||
} // namespace gltfio
|
||||
|
||||
#endif // GLTFIO_DRACO_CACHE_H
|
||||
@@ -1,289 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef GLTFIO_FFILAMENTASSET_H
|
||||
#define GLTFIO_FFILAMENTASSET_H
|
||||
|
||||
#include <gltfio/FilamentAsset.h>
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/IndexBuffer.h>
|
||||
#include <filament/MaterialInstance.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/TextureSampler.h>
|
||||
#include <filament/TransformManager.h>
|
||||
#include <filament/VertexBuffer.h>
|
||||
|
||||
#include <gltfio/MaterialProvider.h>
|
||||
|
||||
#include <math/mat4.h>
|
||||
|
||||
#include <utils/FixedCapacityVector.h>
|
||||
#include <utils/CString.h>
|
||||
#include <utils/Entity.h>
|
||||
|
||||
#include <cgltf.h>
|
||||
|
||||
#include "upcast.h"
|
||||
#include "DependencyGraph.h"
|
||||
#include "DracoCache.h"
|
||||
#include "FFilamentInstance.h"
|
||||
|
||||
#include <tsl/robin_map.h>
|
||||
#include <tsl/htrie_map.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define GLTFIO_VERBOSE 0
|
||||
#define GLTFIO_WARN(msg)
|
||||
#else
|
||||
#define GLTFIO_VERBOSE 1
|
||||
#define GLTFIO_WARN(msg) slog.w << msg << io::endl
|
||||
#endif
|
||||
|
||||
namespace utils {
|
||||
class NameComponentManager;
|
||||
class EntityManager;
|
||||
}
|
||||
|
||||
namespace gltfio {
|
||||
|
||||
class Animator;
|
||||
class Wireframe;
|
||||
class MorphHelper;
|
||||
|
||||
// Encapsulates VertexBuffer::setBufferAt() or IndexBuffer::setBuffer().
|
||||
struct BufferSlot {
|
||||
const cgltf_accessor* accessor;
|
||||
cgltf_attribute_type attribute;
|
||||
int bufferIndex; // for vertex buffers only
|
||||
filament::VertexBuffer* vertexBuffer;
|
||||
filament::IndexBuffer* indexBuffer;
|
||||
};
|
||||
|
||||
// Encapsulates a connection between Texture and MaterialInstance.
|
||||
struct TextureSlot {
|
||||
const cgltf_texture* texture;
|
||||
filament::MaterialInstance* materialInstance;
|
||||
const char* materialParameter;
|
||||
filament::TextureSampler sampler;
|
||||
bool srgb;
|
||||
};
|
||||
|
||||
// MeshCache
|
||||
// ---------
|
||||
// If a given glTF mesh is referenced by multiple glTF nodes, then it generates a separate Filament
|
||||
// renderable for each of those nodes. All renderables generated by a given mesh share a common set
|
||||
// of VertexBuffer and IndexBuffer objects. To achieve the sharing behavior, the loader maintains a
|
||||
// small cache. The cache keys are glTF mesh definitions and the cache entries are lists of
|
||||
// primitives, where a "primitive" is a reference to a Filament VertexBuffer and IndexBuffer.
|
||||
struct Primitive {
|
||||
filament::VertexBuffer* vertices = nullptr;
|
||||
filament::IndexBuffer* indices = nullptr;
|
||||
filament::Aabb aabb; // object-space bounding box
|
||||
UvMap uvmap; // mapping from each glTF UV set to either UV0 or UV1 (8 bytes)
|
||||
};
|
||||
using MeshCache = tsl::robin_map<const cgltf_mesh*, std::vector<Primitive>>;
|
||||
|
||||
// MatInstanceCache
|
||||
// ----------------
|
||||
// Each glTF material definition corresponds to a single filament::MaterialInstance, which are
|
||||
// temporarily cached during loading. The filament::Material objects that are used to create instances are
|
||||
// cached in MaterialProvider. If a given glTF material is referenced by multiple glTF meshes, then
|
||||
// their corresponding filament primitives will share the same Filament MaterialInstance and UvMap.
|
||||
// The UvMap is a mapping from each texcoord slot in glTF to one of Filament's 2 texcoord sets.
|
||||
struct MaterialEntry {
|
||||
filament::MaterialInstance* instance;
|
||||
UvMap uvmap;
|
||||
};
|
||||
using MatInstanceCache = tsl::robin_map<intptr_t, MaterialEntry>;
|
||||
|
||||
struct FFilamentAsset : public FilamentAsset {
|
||||
FFilamentAsset(filament::Engine* engine, utils::NameComponentManager* names,
|
||||
utils::EntityManager* entityManager, const cgltf_data* srcAsset) :
|
||||
mEngine(engine), mNameManager(names), mEntityManager(entityManager) {
|
||||
mSourceAsset.reset(new SourceAsset {(cgltf_data*)srcAsset});
|
||||
}
|
||||
|
||||
~FFilamentAsset();
|
||||
|
||||
size_t getEntityCount() const noexcept {
|
||||
return mEntities.size();
|
||||
}
|
||||
|
||||
const utils::Entity* getEntities() const noexcept {
|
||||
return mEntities.empty() ? nullptr : mEntities.data();
|
||||
}
|
||||
|
||||
const utils::Entity* getLightEntities() const noexcept {
|
||||
return mLightEntities.empty() ? nullptr : mLightEntities.data();
|
||||
}
|
||||
|
||||
size_t getLightEntityCount() const noexcept {
|
||||
return mLightEntities.size();
|
||||
}
|
||||
|
||||
const utils::Entity* getCameraEntities() const noexcept {
|
||||
return mCameraEntities.empty() ? nullptr : mCameraEntities.data();
|
||||
}
|
||||
|
||||
size_t getCameraEntityCount() const noexcept {
|
||||
return mCameraEntities.size();
|
||||
}
|
||||
|
||||
utils::Entity getRoot() const noexcept {
|
||||
return mRoot;
|
||||
}
|
||||
|
||||
size_t popRenderables(utils::Entity* entities, size_t count) noexcept {
|
||||
return mDependencyGraph.popRenderables(entities, count);
|
||||
}
|
||||
|
||||
size_t getMaterialInstanceCount() const noexcept {
|
||||
return mMaterialInstances.size();
|
||||
}
|
||||
|
||||
const filament::MaterialInstance* const* getMaterialInstances() const noexcept {
|
||||
return mMaterialInstances.data();
|
||||
}
|
||||
|
||||
filament::MaterialInstance* const* getMaterialInstances() noexcept {
|
||||
return mMaterialInstances.data();
|
||||
}
|
||||
|
||||
size_t getResourceUriCount() const noexcept {
|
||||
return mResourceUris.size();
|
||||
}
|
||||
|
||||
const char* const* getResourceUris() const noexcept {
|
||||
return mResourceUris.data();
|
||||
}
|
||||
|
||||
filament::Aabb getBoundingBox() const noexcept {
|
||||
return mBoundingBox;
|
||||
}
|
||||
|
||||
const char* getName(utils::Entity entity) const noexcept;
|
||||
|
||||
const char* getExtras(utils::Entity entity) const noexcept;
|
||||
|
||||
utils::Entity getFirstEntityByName(const char* name) noexcept;
|
||||
|
||||
size_t getEntitiesByName(const char* name, utils::Entity* entities,
|
||||
size_t maxCount) const noexcept;
|
||||
|
||||
size_t getEntitiesByPrefix(const char* prefix, utils::Entity* entities,
|
||||
size_t maxCount) const noexcept;
|
||||
|
||||
Animator* getAnimator() noexcept;
|
||||
|
||||
void setMorphWeights(utils::Entity entity , const float* weights, size_t count) noexcept;
|
||||
|
||||
int getMorphTargetCount(utils::Entity entity) noexcept;
|
||||
|
||||
utils::Entity getWireframe() noexcept;
|
||||
|
||||
filament::Engine* getEngine() const noexcept {
|
||||
return mEngine;
|
||||
}
|
||||
|
||||
void releaseSourceData() noexcept;
|
||||
|
||||
const void* getSourceAsset() const noexcept {
|
||||
return mSourceAsset.get() ? mSourceAsset->hierarchy : nullptr;
|
||||
}
|
||||
|
||||
FilamentInstance** getAssetInstances() noexcept {
|
||||
return (FilamentInstance**) mInstances.data();
|
||||
}
|
||||
|
||||
size_t getAssetInstanceCount() const noexcept {
|
||||
return mInstances.size();
|
||||
}
|
||||
|
||||
void takeOwnership(filament::Texture* texture) {
|
||||
mTextures.push_back(texture);
|
||||
}
|
||||
|
||||
void bindTexture(const TextureSlot& tb, filament::Texture* texture) {
|
||||
tb.materialInstance->setParameter(tb.materialParameter, texture, tb.sampler);
|
||||
mDependencyGraph.addEdge(texture, tb.materialInstance, tb.materialParameter);
|
||||
}
|
||||
|
||||
bool isInstanced() const {
|
||||
return mInstances.size() > 0;
|
||||
}
|
||||
|
||||
filament::Engine* mEngine;
|
||||
utils::NameComponentManager* mNameManager;
|
||||
utils::EntityManager* mEntityManager;
|
||||
std::vector<utils::Entity> mEntities;
|
||||
std::vector<utils::Entity> mLightEntities;
|
||||
std::vector<utils::Entity> mCameraEntities;
|
||||
std::vector<filament::MaterialInstance*> mMaterialInstances;
|
||||
std::vector<filament::VertexBuffer*> mVertexBuffers;
|
||||
std::vector<filament::BufferObject*> mBufferObjects;
|
||||
std::vector<filament::IndexBuffer*> mIndexBuffers;
|
||||
std::vector<filament::Texture*> mTextures;
|
||||
filament::Aabb mBoundingBox;
|
||||
utils::Entity mRoot;
|
||||
std::vector<FFilamentInstance*> mInstances;
|
||||
SkinVector mSkins; // unused for instanced assets
|
||||
Animator* mAnimator = nullptr;
|
||||
MorphHelper* mMorpher = nullptr;
|
||||
Wireframe* mWireframe = nullptr;
|
||||
bool mResourcesLoaded = false;
|
||||
DependencyGraph mDependencyGraph;
|
||||
tsl::htrie_map<char, std::vector<utils::Entity>> mNameToEntity;
|
||||
tsl::robin_map<utils::Entity, utils::CString> mNodeExtras;
|
||||
utils::CString mAssetExtras;
|
||||
|
||||
// Sentinels for situations where ResourceLoader needs to generate data.
|
||||
const cgltf_accessor mGenerateNormals = {};
|
||||
const cgltf_accessor mGenerateTangents = {};
|
||||
|
||||
// Encapsulates reference-counted source data, which includes the cgltf hierachy
|
||||
// and potentially also includes buffer data that can be uploaded to the GPU.
|
||||
struct SourceAsset {
|
||||
~SourceAsset() { cgltf_free(hierarchy); }
|
||||
cgltf_data* hierarchy;
|
||||
DracoCache dracoCache;
|
||||
utils::FixedCapacityVector<uint8_t> glbData;
|
||||
};
|
||||
|
||||
// We used shared ownership for the raw cgltf data in order to permit ResourceLoader to
|
||||
// complete various asynchronous work (e.g. uploading buffers to the GPU) even after the asset
|
||||
// or ResourceLoader have been destroyed.
|
||||
using SourceHandle = std::shared_ptr<SourceAsset>;
|
||||
SourceHandle mSourceAsset;
|
||||
|
||||
// Transient source data that can freed via releaseSourceData:
|
||||
std::vector<BufferSlot> mBufferSlots;
|
||||
std::vector<TextureSlot> mTextureSlots;
|
||||
std::vector<const char*> mResourceUris;
|
||||
NodeMap mNodeMap; // unused for instanced assets
|
||||
std::vector<std::pair<const cgltf_primitive*, filament::VertexBuffer*> > mPrimitives;
|
||||
MatInstanceCache mMatInstanceCache;
|
||||
MeshCache mMeshCache;
|
||||
};
|
||||
|
||||
FILAMENT_UPCAST(FilamentAsset)
|
||||
|
||||
} // namespace gltfio
|
||||
|
||||
#endif // GLTFIO_FFILAMENTASSET_H
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef GLTFIO_FFILAMENTINSTANCE_H
|
||||
#define GLTFIO_FFILAMENTINSTANCE_H
|
||||
|
||||
#include <gltfio/FilamentInstance.h>
|
||||
|
||||
#include <utils/Entity.h>
|
||||
|
||||
#include <math/mat4.h>
|
||||
|
||||
#include <tsl/robin_map.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "upcast.h"
|
||||
|
||||
struct cgltf_node;
|
||||
|
||||
namespace gltfio {
|
||||
|
||||
struct FFilamentAsset;
|
||||
class Animator;
|
||||
|
||||
struct Skin {
|
||||
std::string name;
|
||||
std::vector<filament::math::mat4f> inverseBindMatrices;
|
||||
std::vector<utils::Entity> joints;
|
||||
std::vector<utils::Entity> targets;
|
||||
};
|
||||
|
||||
using SkinVector = std::vector<Skin>;
|
||||
using NodeMap = tsl::robin_map<const cgltf_node*, utils::Entity>;
|
||||
|
||||
struct FFilamentInstance : public FilamentInstance {
|
||||
std::vector<utils::Entity> entities;
|
||||
utils::Entity root;
|
||||
Animator* animator;
|
||||
FFilamentAsset* owner;
|
||||
SkinVector skins;
|
||||
NodeMap nodeMap;
|
||||
Animator* getAnimator() noexcept;
|
||||
};
|
||||
|
||||
FILAMENT_UPCAST(FilamentInstance)
|
||||
|
||||
} // namespace gltfio
|
||||
|
||||
#endif // GLTFIO_FFILAMENTINSTANCE_H
|
||||
@@ -41,13 +41,9 @@
|
||||
#include <camutils/Manipulator.h>
|
||||
|
||||
#include <utils/NameComponentManager.h>
|
||||
#include <utils/JobSystem.h>
|
||||
|
||||
#include "math.h"
|
||||
|
||||
#include "FFilamentInstance.h"
|
||||
#include "FFilamentAsset.h"
|
||||
|
||||
#include <math/mat4.h>
|
||||
#include <math/quat.h>
|
||||
#include <math/scalar.h>
|
||||
@@ -61,12 +57,6 @@
|
||||
|
||||
#include "Log.h"
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
#include <android/asset_manager_jni.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include <android/log.h>
|
||||
#include <android/native_activity.h>
|
||||
|
||||
using namespace filament;
|
||||
using namespace filament::math;
|
||||
using namespace gltfio;
|
||||
@@ -84,18 +74,6 @@ namespace filament
|
||||
class LightManager;
|
||||
}
|
||||
|
||||
namespace gltfio
|
||||
{
|
||||
MaterialProvider *createGPUMorphShaderLoader(
|
||||
const void *opaqueData,
|
||||
uint64_t opaqueDataSize,
|
||||
const void *fadeData,
|
||||
uint64_t fadeDataSize,
|
||||
Engine *engine);
|
||||
void decomposeMatrix(const filament::math::mat4f &mat, filament::math::float3 *translation,
|
||||
filament::math::quatf *rotation, filament::math::float3 *scale);
|
||||
}
|
||||
|
||||
namespace polyvox
|
||||
{
|
||||
|
||||
@@ -136,14 +114,10 @@ namespace polyvox
|
||||
|
||||
FilamentViewer::FilamentViewer(
|
||||
void *layer,
|
||||
const char *opaqueShaderPath,
|
||||
const char *fadeShaderPath,
|
||||
LoadResource loadResource,
|
||||
FreeResource freeResource) : _layer(layer),
|
||||
_loadResource(loadResource),
|
||||
_freeResource(freeResource),
|
||||
opaqueShaderResources(nullptr, 0, 0),
|
||||
fadeShaderResources(nullptr, 0, 0),
|
||||
_assetBuffer(nullptr, 0, 0)
|
||||
{
|
||||
_engine = Engine::create(Engine::Backend::OPENGL);
|
||||
@@ -205,7 +179,7 @@ namespace polyvox
|
||||
void FilamentViewer::createSwapChain(void *surface)
|
||||
{
|
||||
_swapChain = _engine->createSwapChain(surface);
|
||||
// Log("swapchain created.");
|
||||
Log("swapchain created.");
|
||||
}
|
||||
|
||||
void FilamentViewer::destroySwapChain()
|
||||
@@ -214,8 +188,8 @@ namespace polyvox
|
||||
{
|
||||
_engine->destroy(_swapChain);
|
||||
_swapChain = nullptr;
|
||||
Log("Swapchain destroyed.");
|
||||
}
|
||||
// Log("swapchain destroyed.");
|
||||
}
|
||||
|
||||
void FilamentViewer::applyWeights(float *weights, int count)
|
||||
@@ -223,8 +197,10 @@ namespace polyvox
|
||||
|
||||
for (size_t i = 0, c = _asset->getEntityCount(); i != c; ++i)
|
||||
{
|
||||
_asset->setMorphWeights(
|
||||
_asset->getEntities()[i],
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
auto inst = rm.getInstance(_asset->getEntities()[i]);
|
||||
rm.setMorphWeights(
|
||||
inst,
|
||||
weights,
|
||||
count);
|
||||
}
|
||||
@@ -240,6 +216,7 @@ namespace polyvox
|
||||
for (size_t i = 0; i < resourceUriCount; i++)
|
||||
{
|
||||
string uri = relativeResourcePath + string(resourceUris[i]);
|
||||
Log("Creating resource buffer for resource at %s",uri.c_str());
|
||||
ResourceBuffer buf = _loadResource(uri.c_str());
|
||||
|
||||
// using FunctionCallback = std::function<void(void*, unsigned int, void *)>;
|
||||
@@ -273,8 +250,6 @@ namespace polyvox
|
||||
{
|
||||
Log("Releasing source data");
|
||||
_asset->releaseSourceData();
|
||||
// _freeResource(opaqueShaderResources);
|
||||
// _freeResource(fadeShaderResources);
|
||||
}
|
||||
|
||||
void FilamentViewer::loadGlb(const char *const uri)
|
||||
@@ -376,100 +351,99 @@ namespace polyvox
|
||||
///
|
||||
bool FilamentViewer::setCamera(const char *cameraName)
|
||||
{
|
||||
FFilamentAsset *asset = (FFilamentAsset *)_asset;
|
||||
|
||||
gltfio::NodeMap &sourceNodes = asset->isInstanced() ? asset->mInstances[0]->nodeMap
|
||||
: asset->mNodeMap;
|
||||
Log("Setting camera to node %s", cameraName);
|
||||
for (auto pair : sourceNodes)
|
||||
{
|
||||
cgltf_node const *node = pair.first;
|
||||
// gltfio::NodeMap &sourceNodes = _asset->isInstanced() ? asset->mInstances[0]->nodeMap
|
||||
// : asset->mNodeMap;
|
||||
// Log("Setting camera to node %s", cameraName);
|
||||
// for (auto pair : sourceNodes)
|
||||
// {
|
||||
// cgltf_node const *node = pair.first;
|
||||
|
||||
if (strcmp(cameraName, node->name) != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// if (strcmp(cameraName, node->name) != 0)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
Log("Node %s : Matrix : %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f Translation : %03f %03f %03f Rotation %03f %03f %03f %03f Scale %03f %03f %03f",
|
||||
node->name,
|
||||
node->matrix[0],
|
||||
node->matrix[1],
|
||||
node->matrix[2],
|
||||
node->matrix[3],
|
||||
node->matrix[4],
|
||||
node->matrix[5],
|
||||
node->matrix[6],
|
||||
node->matrix[7],
|
||||
node->matrix[8],
|
||||
node->matrix[9],
|
||||
node->matrix[10],
|
||||
node->matrix[11],
|
||||
node->matrix[12],
|
||||
node->matrix[13],
|
||||
node->matrix[14],
|
||||
node->matrix[15],
|
||||
node->translation[0],
|
||||
node->translation[1],
|
||||
node->translation[2],
|
||||
node->rotation[0],
|
||||
node->rotation[1],
|
||||
node->rotation[2],
|
||||
node->rotation[3],
|
||||
node->scale[0],
|
||||
node->scale[1],
|
||||
node->scale[2]
|
||||
);
|
||||
mat4f t = mat4f::translation(float3 { node->translation[0],node->translation[1],node->translation[2] });
|
||||
mat4f r { quatf { node->rotation[3], node->rotation[0], node->rotation[1], node->rotation[2] } };
|
||||
mat4f transform = t * r;
|
||||
// Log("Node %s : Matrix : %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f Translation : %03f %03f %03f Rotation %03f %03f %03f %03f Scale %03f %03f %03f",
|
||||
// node->name,
|
||||
// node->matrix[0],
|
||||
// node->matrix[1],
|
||||
// node->matrix[2],
|
||||
// node->matrix[3],
|
||||
// node->matrix[4],
|
||||
// node->matrix[5],
|
||||
// node->matrix[6],
|
||||
// node->matrix[7],
|
||||
// node->matrix[8],
|
||||
// node->matrix[9],
|
||||
// node->matrix[10],
|
||||
// node->matrix[11],
|
||||
// node->matrix[12],
|
||||
// node->matrix[13],
|
||||
// node->matrix[14],
|
||||
// node->matrix[15],
|
||||
// node->translation[0],
|
||||
// node->translation[1],
|
||||
// node->translation[2],
|
||||
// node->rotation[0],
|
||||
// node->rotation[1],
|
||||
// node->rotation[2],
|
||||
// node->rotation[3],
|
||||
// node->scale[0],
|
||||
// node->scale[1],
|
||||
// node->scale[2]
|
||||
// );
|
||||
// mat4f t = mat4f::translation(float3 { node->translation[0],node->translation[1],node->translation[2] });
|
||||
// mat4f r { quatf { node->rotation[3], node->rotation[0], node->rotation[1], node->rotation[2] } };
|
||||
// mat4f transform = t * r;
|
||||
|
||||
if (!node->camera)
|
||||
{
|
||||
cgltf_node* leaf = node->children[0];
|
||||
// if (!node->camera)
|
||||
// {
|
||||
// cgltf_node* leaf = node->children[0];
|
||||
|
||||
Log("Child 1 trans : %03f %03f %03f rot : %03f %03f %03f %03f ", leaf->translation[0], leaf->translation[1],leaf->translation[2], leaf->rotation[0],leaf->rotation[1],leaf->rotation[2],leaf->rotation[3]);
|
||||
// Log("Child 1 trans : %03f %03f %03f rot : %03f %03f %03f %03f ", leaf->translation[0], leaf->translation[1],leaf->translation[2], leaf->rotation[0],leaf->rotation[1],leaf->rotation[2],leaf->rotation[3]);
|
||||
|
||||
if (!leaf->camera) {
|
||||
leaf = leaf->children[0];
|
||||
Log("Child 2 %03f %03f %03f %03f %03f %03f %03f ", leaf->translation[0], leaf->translation[1],leaf->translation[2], leaf->rotation[0],leaf->rotation[1],leaf->rotation[2],leaf->rotation[3]);
|
||||
if (!leaf->camera) {
|
||||
Log("Could not find GLTF camera under node or its ssecond or third child nodes.");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
// if (!leaf->camera) {
|
||||
// leaf = leaf->children[0];
|
||||
// Log("Child 2 %03f %03f %03f %03f %03f %03f %03f ", leaf->translation[0], leaf->translation[1],leaf->translation[2], leaf->rotation[0],leaf->rotation[1],leaf->rotation[2],leaf->rotation[3]);
|
||||
// if (!leaf->camera) {
|
||||
// Log("Could not find GLTF camera under node or its ssecond or third child nodes.");
|
||||
// exit(-1);
|
||||
// }
|
||||
// }
|
||||
|
||||
Log("Using rotation from leaf node.");
|
||||
// Log("Using rotation from leaf node.");
|
||||
|
||||
mat4f child_rot { quatf { leaf->rotation[3], leaf->rotation[0], leaf->rotation[1], leaf->rotation[2] } };
|
||||
// mat4f child_rot { quatf { leaf->rotation[3], leaf->rotation[0], leaf->rotation[1], leaf->rotation[2] } };
|
||||
|
||||
transform *= child_rot;
|
||||
}
|
||||
// transform *= child_rot;
|
||||
// }
|
||||
|
||||
Entity cameraEntity = EntityManager::get().create();
|
||||
Camera *cam = _engine->createCamera(cameraEntity);
|
||||
// Entity cameraEntity = EntityManager::get().create();
|
||||
// Camera *cam = _engine->createCamera(cameraEntity);
|
||||
|
||||
const Viewport &vp = _view->getViewport();
|
||||
// const Viewport &vp = _view->getViewport();
|
||||
|
||||
const double aspect = (double)vp.width / vp.height;
|
||||
// const double aspect = (double)vp.width / vp.height;
|
||||
|
||||
// todo - pull focal length from gltf node
|
||||
// // todo - pull focal length from gltf node
|
||||
|
||||
cam->setLensProjection(_cameraFocalLength, aspect, kNearPlane, kFarPlane);
|
||||
// cam->setLensProjection(_cameraFocalLength, aspect, kNearPlane, kFarPlane);
|
||||
|
||||
if (!cam)
|
||||
{
|
||||
Log("Couldn't create camera");
|
||||
}
|
||||
else
|
||||
{
|
||||
_engine->getTransformManager().setTransform(
|
||||
_engine->getTransformManager().getInstance(cameraEntity), transform
|
||||
);
|
||||
// if (!cam)
|
||||
// {
|
||||
// Log("Couldn't create camera");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// _engine->getTransformManager().setTransform(
|
||||
// _engine->getTransformManager().getInstance(cameraEntity), transform
|
||||
// );
|
||||
|
||||
_view->setCamera(cam);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// _view->setCamera(cam);
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -492,34 +466,12 @@ namespace polyvox
|
||||
|
||||
StringList FilamentViewer::getTargetNames(const char *meshName)
|
||||
{
|
||||
FFilamentAsset *asset = (FFilamentAsset *)_asset;
|
||||
NodeMap &sourceNodes = asset->isInstanced() ? asset->mInstances[0]->nodeMap : asset->mNodeMap;
|
||||
|
||||
if (sourceNodes.empty())
|
||||
{
|
||||
Log("Asset source nodes empty?");
|
||||
return StringList(nullptr, 0);
|
||||
}
|
||||
Log("Fetching morph target names for mesh %s", meshName);
|
||||
|
||||
for (auto pair : sourceNodes)
|
||||
{
|
||||
cgltf_node const *node = pair.first;
|
||||
cgltf_mesh const *mesh = node->mesh;
|
||||
|
||||
if (mesh)
|
||||
{
|
||||
Log("Mesh : %s ", mesh->name);
|
||||
if (strcmp(meshName, mesh->name) == 0)
|
||||
{
|
||||
return StringList((const char **)mesh->target_names, (int)mesh->target_names_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
// int count = asset->getMorphTargetCountAt()
|
||||
// asset->getMorphTargetNameAt()
|
||||
return StringList(nullptr, 0);
|
||||
}
|
||||
|
||||
void FilamentViewer::loadSkybox(const char *const skyboxPath, const char *const iblPath, AAssetManager *am)
|
||||
void FilamentViewer::loadSkybox(const char *const skyboxPath, const char *const iblPath)
|
||||
{
|
||||
|
||||
ResourceBuffer skyboxBuffer = _loadResource(skyboxPath);
|
||||
@@ -633,7 +585,8 @@ namespace polyvox
|
||||
|
||||
const double aspect = (double)width / height;
|
||||
_mainCamera->setLensProjection(_cameraFocalLength, aspect, kNearPlane, kFarPlane);
|
||||
Log("Set viewport to %d %d", _width, _height);
|
||||
|
||||
Log("Set viewport to width: %d height: %d scaleFactor : %f", width, height, contentScaleFactor);
|
||||
}
|
||||
|
||||
void FilamentViewer::animateWeights(float *data, int numWeights, int numFrames, float frameRate)
|
||||
@@ -700,74 +653,3 @@ namespace polyvox
|
||||
|
||||
}
|
||||
|
||||
|
||||
// //
|
||||
// //if(morphAnimationBuffer.frameIndex >= morphAnimationBuffer.numFrames) {
|
||||
// // this.morphAnimationBuffer = null;
|
||||
// // return;
|
||||
// //}
|
||||
// //
|
||||
// //if(morphAnimationBuffer.frameIndex == -1) {
|
||||
// // applyWeights(morphAnimationBuffer->frameData, morphAnimationBuffer->numWeights);
|
||||
// // morphAnimationBuffer->frameIndex++;
|
||||
// // morphAnimationBuffer->lastTime = std::chrono::high_resolution_clock::now();
|
||||
// //} else {
|
||||
// // duration dur = std::chrono::high_resolution_clock::now() - morphAnimationBuffer->lastTime;
|
||||
// // float msElapsed = dur.count();
|
||||
// // if(msElapsed > morphAnimationBuffer->frameLength) {
|
||||
// // frameIndex++;
|
||||
// // applyWeights(frameData + (frameIndex * numWeights), numWeights);
|
||||
// // morphAnimationBuffer->lastTime = std::chrono::high_resolution_clock::now();
|
||||
// // }
|
||||
// //}
|
||||
|
||||
|
||||
// void FilamentViewer::createMorpher(const char* meshName, int* primitives, int numPrimitives) {
|
||||
// // morphHelper = new gltfio::GPUMorphHelper((FFilamentAsset*)_asset, meshName, primitives, numPrimitives);
|
||||
// // morphHelper = new gltfio::CPUMorpher(((FFilamentAsset)*_asset, (FFilamentInstance*)_asset));
|
||||
// }
|
||||
|
||||
// void FilamentViewer::animateBones() {
|
||||
// }
|
||||
// Entity entity = _asset->getFirstEntityByName("CC_Base_JawRoot");
|
||||
// if(!entity) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// TransformManager& transformManager = _engine->getTransformManager();
|
||||
|
||||
// TransformManager::Instance node = transformManager.getInstance( entity);
|
||||
|
||||
// mat4f xform = transformManager.getTransform(node);
|
||||
// float3 scale;
|
||||
// quatf rotation;
|
||||
// float3 translation;
|
||||
// decomposeMatrix(xform, &translation, &rotation, &scale);
|
||||
|
||||
// // const quatf srcQuat { weights[0] * 0.9238,0,weights[0] * 0.3826, 0 };
|
||||
// // float3 { scale[0] * (1.0f - weights[0]), scale[1] * (1.0f - weights[1]), scale[2] * (1.0f - weights[2]) }
|
||||
// // xform = composeMatrix(translation + float3 { weights[0], weights[1], weights[2] }, rotation, scale );
|
||||
// transformManager.setTransform(node, xform);
|
||||
|
||||
// }
|
||||
|
||||
// void FilamentViewer::updateAnimation(AnimationBuffer animation, std::function<void(int)> callback) {
|
||||
// if(morphAnimationBuffer.frameIndex >= animation.numFrames) {
|
||||
// this.animation = null;
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if(animation.frameIndex == -1) {
|
||||
// animation->frameIndex++;
|
||||
// animation->lastTime = std::chrono::high_resolution_clock::now();
|
||||
// callback(); // applyWeights(morphAnimationBuffer->frameData, morphAnimationBuffer->numWeights);
|
||||
// } else {
|
||||
// duration dur = std::chrono::high_resolution_clock::now() - morphAnimationBuffer->lastTime;
|
||||
// float msElapsed = dur.count();
|
||||
// if(msElapsed > animation->frameLength) {
|
||||
// animation->frameIndex++;
|
||||
// animation->lastTime = std::chrono::high_resolution_clock::now();
|
||||
// callback(); // applyWeights(frameData + (frameIndex * numWeights), numWeights);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -30,12 +30,6 @@
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
#include <android/asset_manager_jni.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include <android/log.h>
|
||||
#include <android/native_activity.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace filament;
|
||||
using namespace filament::math;
|
||||
@@ -102,22 +96,21 @@ namespace polyvox {
|
||||
|
||||
class FilamentViewer {
|
||||
public:
|
||||
FilamentViewer(void* layer, const char* opaqueShaderPath, const char* fadeShaderPath, LoadResource loadResource, FreeResource freeResource);
|
||||
FilamentViewer(void* layer, LoadResource loadResource, FreeResource freeResource);
|
||||
~FilamentViewer();
|
||||
void loadGlb(const char* const uri);
|
||||
void loadGltf(const char* const uri, const char* relativeResourcePath);
|
||||
void loadSkybox(const char* const skyboxUri, const char* const iblUri, AAssetManager* am);
|
||||
void loadSkybox(const char* const skyboxUri, const char* const iblUri);
|
||||
|
||||
void updateViewportAndCameraProjection(int height, int width, float scaleFactor);
|
||||
void render();
|
||||
// void createMorpher(const char* meshName, int* primitives, int numPrimitives);
|
||||
void releaseSourceAssets();
|
||||
StringList getTargetNames(const char* meshName);
|
||||
unique_ptr<vector<string>> getAnimationNames();
|
||||
Manipulator<float>* manipulator;
|
||||
void applyWeights(float* weights, int count);
|
||||
void animateWeights(float* data, int numWeights, int length, float frameRate);
|
||||
// void animateBones();
|
||||
|
||||
void playAnimation(int index, bool loop);
|
||||
bool setCamera(const char* nodeName);
|
||||
void destroySwapChain();
|
||||
@@ -136,9 +129,6 @@ namespace polyvox {
|
||||
LoadResource _loadResource;
|
||||
FreeResource _freeResource;
|
||||
|
||||
ResourceBuffer opaqueShaderResources;
|
||||
ResourceBuffer fadeShaderResources;
|
||||
|
||||
Scene* _scene;
|
||||
View* _view;
|
||||
Engine* _engine;
|
||||
|
||||
@@ -1,273 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef GLTFIO_GLTFENUMS_H
|
||||
#define GLTFIO_GLTFENUMS_H
|
||||
|
||||
#include <filament/IndexBuffer.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
#include <filament/TextureSampler.h>
|
||||
#include <filament/VertexBuffer.h>
|
||||
|
||||
#include <cgltf.h>
|
||||
|
||||
#define GL_NEAREST 0x2600
|
||||
#define GL_LINEAR 0x2601
|
||||
#define GL_NEAREST_MIPMAP_NEAREST 0x2700
|
||||
#define GL_LINEAR_MIPMAP_NEAREST 0x2701
|
||||
#define GL_NEAREST_MIPMAP_LINEAR 0x2702
|
||||
#define GL_LINEAR_MIPMAP_LINEAR 0x2703
|
||||
#define GL_REPEAT 0x2901
|
||||
#define GL_MIRRORED_REPEAT 0x8370
|
||||
#define GL_CLAMP_TO_EDGE 0x812F
|
||||
|
||||
inline filament::TextureSampler::WrapMode getWrapMode(cgltf_int wrap) {
|
||||
switch (wrap) {
|
||||
case GL_REPEAT:
|
||||
return filament::TextureSampler::WrapMode::REPEAT;
|
||||
case GL_MIRRORED_REPEAT:
|
||||
return filament::TextureSampler::WrapMode::MIRRORED_REPEAT;
|
||||
case GL_CLAMP_TO_EDGE:
|
||||
return filament::TextureSampler::WrapMode::CLAMP_TO_EDGE;
|
||||
}
|
||||
return filament::TextureSampler::WrapMode::REPEAT;
|
||||
}
|
||||
|
||||
inline filament::TextureSampler::MinFilter getMinFilter(cgltf_int minFilter) {
|
||||
switch (minFilter) {
|
||||
case GL_NEAREST:
|
||||
return filament::TextureSampler::MinFilter::NEAREST;
|
||||
case GL_LINEAR:
|
||||
return filament::TextureSampler::MinFilter::LINEAR;
|
||||
case GL_NEAREST_MIPMAP_NEAREST:
|
||||
return filament::TextureSampler::MinFilter::NEAREST_MIPMAP_NEAREST;
|
||||
case GL_LINEAR_MIPMAP_NEAREST:
|
||||
return filament::TextureSampler::MinFilter::LINEAR_MIPMAP_NEAREST;
|
||||
case GL_NEAREST_MIPMAP_LINEAR:
|
||||
return filament::TextureSampler::MinFilter::NEAREST_MIPMAP_LINEAR;
|
||||
case GL_LINEAR_MIPMAP_LINEAR:
|
||||
return filament::TextureSampler::MinFilter::LINEAR_MIPMAP_LINEAR;
|
||||
}
|
||||
return filament::TextureSampler::MinFilter::LINEAR_MIPMAP_LINEAR;
|
||||
}
|
||||
|
||||
inline filament::TextureSampler::MagFilter getMagFilter(cgltf_int magFilter) {
|
||||
switch (magFilter) {
|
||||
case GL_NEAREST:
|
||||
return filament::TextureSampler::MagFilter::NEAREST;
|
||||
case GL_LINEAR:
|
||||
return filament::TextureSampler::MagFilter::LINEAR;
|
||||
}
|
||||
return filament::TextureSampler::MagFilter::LINEAR;
|
||||
}
|
||||
|
||||
inline bool getVertexAttrType(cgltf_attribute_type atype, filament::VertexAttribute* attrType) {
|
||||
switch (atype) {
|
||||
case cgltf_attribute_type_position:
|
||||
*attrType = filament::VertexAttribute::POSITION;
|
||||
return true;
|
||||
case cgltf_attribute_type_texcoord:
|
||||
*attrType = filament::VertexAttribute::UV0;
|
||||
return true;
|
||||
case cgltf_attribute_type_color:
|
||||
*attrType = filament::VertexAttribute::COLOR;
|
||||
return true;
|
||||
case cgltf_attribute_type_joints:
|
||||
*attrType = filament::VertexAttribute::BONE_INDICES;
|
||||
return true;
|
||||
case cgltf_attribute_type_weights:
|
||||
*attrType = filament::VertexAttribute::BONE_WEIGHTS;
|
||||
return true;
|
||||
case cgltf_attribute_type_normal:
|
||||
case cgltf_attribute_type_tangent:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool getIndexType(cgltf_component_type ctype, filament::IndexBuffer::IndexType* itype) {
|
||||
switch (ctype) {
|
||||
case cgltf_component_type_r_8u:
|
||||
case cgltf_component_type_r_16u:
|
||||
*itype = filament::IndexBuffer::IndexType::USHORT;
|
||||
return true;
|
||||
case cgltf_component_type_r_32u:
|
||||
*itype = filament::IndexBuffer::IndexType::UINT;
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool getPrimitiveType(cgltf_primitive_type in,
|
||||
filament::RenderableManager::PrimitiveType* out) {
|
||||
switch (in) {
|
||||
case cgltf_primitive_type_points:
|
||||
*out = filament::RenderableManager::PrimitiveType::POINTS;
|
||||
return true;
|
||||
case cgltf_primitive_type_lines:
|
||||
*out = filament::RenderableManager::PrimitiveType::LINES;
|
||||
return true;
|
||||
case cgltf_primitive_type_line_strip:
|
||||
*out = filament::RenderableManager::PrimitiveType::LINE_STRIP;
|
||||
return true;
|
||||
case cgltf_primitive_type_triangles:
|
||||
*out = filament::RenderableManager::PrimitiveType::TRIANGLES;
|
||||
return true;
|
||||
case cgltf_primitive_type_triangle_strip:
|
||||
*out = filament::RenderableManager::PrimitiveType::TRIANGLE_STRIP;
|
||||
return true;
|
||||
case cgltf_primitive_type_line_loop:
|
||||
case cgltf_primitive_type_triangle_fan:
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// This converts a cgltf component type into a Filament Attribute type.
|
||||
//
|
||||
// This function has two out parameters. One result is a safe "permitted type" which we know is
|
||||
// universally accepted across GPU's and backends, but may require conversion (see Transcoder). The
|
||||
// other result is the "actual type" which requires no conversion.
|
||||
//
|
||||
// Returns false if the given component type is invalid.
|
||||
inline bool getElementType(cgltf_type type, cgltf_component_type ctype,
|
||||
filament::VertexBuffer::AttributeType* permitType,
|
||||
filament::VertexBuffer::AttributeType* actualType) {
|
||||
switch (type) {
|
||||
case cgltf_type_scalar:
|
||||
switch (ctype) {
|
||||
case cgltf_component_type_r_8:
|
||||
*permitType = filament::VertexBuffer::AttributeType::BYTE;
|
||||
*actualType = filament::VertexBuffer::AttributeType::BYTE;
|
||||
return true;
|
||||
case cgltf_component_type_r_8u:
|
||||
*permitType = filament::VertexBuffer::AttributeType::UBYTE;
|
||||
*actualType = filament::VertexBuffer::AttributeType::UBYTE;
|
||||
return true;
|
||||
case cgltf_component_type_r_16:
|
||||
*permitType = filament::VertexBuffer::AttributeType::SHORT;
|
||||
*actualType = filament::VertexBuffer::AttributeType::SHORT;
|
||||
return true;
|
||||
case cgltf_component_type_r_16u:
|
||||
*permitType = filament::VertexBuffer::AttributeType::USHORT;
|
||||
*actualType = filament::VertexBuffer::AttributeType::USHORT;
|
||||
return true;
|
||||
case cgltf_component_type_r_32u:
|
||||
*permitType = filament::VertexBuffer::AttributeType::UINT;
|
||||
*actualType = filament::VertexBuffer::AttributeType::UINT;
|
||||
return true;
|
||||
case cgltf_component_type_r_32f:
|
||||
*permitType = filament::VertexBuffer::AttributeType::FLOAT;
|
||||
*actualType = filament::VertexBuffer::AttributeType::FLOAT;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case cgltf_type_vec2:
|
||||
switch (ctype) {
|
||||
case cgltf_component_type_r_8:
|
||||
*permitType = filament::VertexBuffer::AttributeType::BYTE2;
|
||||
*actualType = filament::VertexBuffer::AttributeType::BYTE2;
|
||||
return true;
|
||||
case cgltf_component_type_r_8u:
|
||||
*permitType = filament::VertexBuffer::AttributeType::UBYTE2;
|
||||
*actualType = filament::VertexBuffer::AttributeType::UBYTE2;
|
||||
return true;
|
||||
case cgltf_component_type_r_16:
|
||||
*permitType = filament::VertexBuffer::AttributeType::SHORT2;
|
||||
*actualType = filament::VertexBuffer::AttributeType::SHORT2;
|
||||
return true;
|
||||
case cgltf_component_type_r_16u:
|
||||
*permitType = filament::VertexBuffer::AttributeType::USHORT2;
|
||||
*actualType = filament::VertexBuffer::AttributeType::USHORT2;
|
||||
return true;
|
||||
case cgltf_component_type_r_32f:
|
||||
*permitType = filament::VertexBuffer::AttributeType::FLOAT2;
|
||||
*actualType = filament::VertexBuffer::AttributeType::FLOAT2;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case cgltf_type_vec3:
|
||||
switch (ctype) {
|
||||
case cgltf_component_type_r_8:
|
||||
*permitType = filament::VertexBuffer::AttributeType::FLOAT3;
|
||||
*actualType = filament::VertexBuffer::AttributeType::BYTE3;
|
||||
return true;
|
||||
case cgltf_component_type_r_8u:
|
||||
*permitType = filament::VertexBuffer::AttributeType::FLOAT3;
|
||||
*actualType = filament::VertexBuffer::AttributeType::UBYTE3;
|
||||
return true;
|
||||
case cgltf_component_type_r_16:
|
||||
*permitType = filament::VertexBuffer::AttributeType::FLOAT3;
|
||||
*actualType = filament::VertexBuffer::AttributeType::SHORT3;
|
||||
return true;
|
||||
case cgltf_component_type_r_16u:
|
||||
*permitType = filament::VertexBuffer::AttributeType::FLOAT3;
|
||||
*actualType = filament::VertexBuffer::AttributeType::USHORT3;
|
||||
return true;
|
||||
case cgltf_component_type_r_32f:
|
||||
*permitType = filament::VertexBuffer::AttributeType::FLOAT3;
|
||||
*actualType = filament::VertexBuffer::AttributeType::FLOAT3;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case cgltf_type_vec4:
|
||||
switch (ctype) {
|
||||
case cgltf_component_type_r_8:
|
||||
*permitType = filament::VertexBuffer::AttributeType::BYTE4;
|
||||
*actualType = filament::VertexBuffer::AttributeType::BYTE4;
|
||||
return true;
|
||||
case cgltf_component_type_r_8u:
|
||||
*permitType = filament::VertexBuffer::AttributeType::UBYTE4;
|
||||
*actualType = filament::VertexBuffer::AttributeType::UBYTE4;
|
||||
return true;
|
||||
case cgltf_component_type_r_16:
|
||||
*permitType = filament::VertexBuffer::AttributeType::SHORT4;
|
||||
*actualType = filament::VertexBuffer::AttributeType::SHORT4;
|
||||
return true;
|
||||
case cgltf_component_type_r_16u:
|
||||
*permitType = filament::VertexBuffer::AttributeType::USHORT4;
|
||||
*actualType = filament::VertexBuffer::AttributeType::USHORT4;
|
||||
return true;
|
||||
case cgltf_component_type_r_32f:
|
||||
*permitType = filament::VertexBuffer::AttributeType::FLOAT4;
|
||||
*actualType = filament::VertexBuffer::AttributeType::FLOAT4;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool requiresConversion(cgltf_type type, cgltf_component_type ctype) {
|
||||
filament::VertexBuffer::AttributeType permitted;
|
||||
filament::VertexBuffer::AttributeType actual;
|
||||
bool supported = getElementType(type, ctype, &permitted, &actual);
|
||||
return supported && permitted != actual;
|
||||
}
|
||||
|
||||
#endif // GLTFIO_GLTFENUMS_H
|
||||
@@ -16,10 +16,10 @@ void Log(const char *fmt, ...) {
|
||||
#elif defined __OBJC__
|
||||
NSString *format = [[NSString alloc] initWithUTF8String:fmt];
|
||||
NSLogv(format, args);
|
||||
[format release];
|
||||
#else
|
||||
printf(fmt, args);
|
||||
vprintf(fmt, args);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2021 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef GLTFIO_MORPHHELPER_H
|
||||
#define GLTFIO_MORPHHELPER_H
|
||||
|
||||
#include "FFilamentAsset.h"
|
||||
#include "FFilamentInstance.h"
|
||||
|
||||
#include <filament/MorphTargetBuffer.h>
|
||||
|
||||
#include <math/vec4.h>
|
||||
|
||||
#include <tsl/robin_map.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
struct cgltf_node;
|
||||
struct cgltf_mesh;
|
||||
struct cgltf_primitive;
|
||||
|
||||
namespace gltfio {
|
||||
|
||||
/**
|
||||
* Internal class that partitions lists of morph weights and maintains a cache of BufferObject
|
||||
* instances. This allows gltfio to support up to 255 morph targets.
|
||||
*
|
||||
* Each partition is associated with an unordered set of 4 (or fewer) morph target indices, which
|
||||
* we call the "primary indices" for that time slice.
|
||||
*
|
||||
* Animator has ownership over a single instance of MorphHelper, thus it is 1:1 with FilamentAsset.
|
||||
*/
|
||||
class MorphHelper {
|
||||
public:
|
||||
using Entity = utils::Entity;
|
||||
MorphHelper(FFilamentAsset* asset, FFilamentInstance* inst);
|
||||
~MorphHelper();
|
||||
|
||||
void setWeights(Entity entity, float const* weights, int count) noexcept;
|
||||
int getTargetCount(Entity entity) const noexcept;
|
||||
|
||||
private:
|
||||
struct GltfPrimitive {
|
||||
filament::MorphTargetBuffer* targets;
|
||||
};
|
||||
|
||||
struct TableEntry {
|
||||
std::vector<GltfPrimitive> primitives; // TODO: flatten this?
|
||||
};
|
||||
|
||||
void addPrimitive(cgltf_mesh const* mesh, int primitiveIndex, Entity entity);
|
||||
|
||||
tsl::robin_map<Entity, TableEntry> mMorphTable;
|
||||
const FFilamentAsset* mAsset;
|
||||
const FFilamentInstance* mInstance;
|
||||
};
|
||||
|
||||
} // namespace gltfio
|
||||
|
||||
#endif // GLTFIO_MORPHHELPER_H
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2021 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <cgltf.h>
|
||||
|
||||
#include <math/vec4.h>
|
||||
|
||||
namespace filament { class VertexBuffer; }
|
||||
|
||||
namespace gltfio {
|
||||
|
||||
/**
|
||||
* Internal helper that examines a cgltf primitive and generates data suitable for Filament's
|
||||
* TANGENTS attribute. This has been designed to be run as a JobSystem job, but clients are not
|
||||
* required to do so.
|
||||
*/
|
||||
struct TangentsJob {
|
||||
static constexpr int kMorphTargetUnused = -1;
|
||||
|
||||
// The inputs to the procedure. The prim is owned by the client, which should ensure that it
|
||||
// stays alive for the duration of the procedure.
|
||||
struct InputParams {
|
||||
const cgltf_primitive* prim;
|
||||
const int morphTargetIndex = kMorphTargetUnused;
|
||||
};
|
||||
|
||||
// The context of the procedure. These fields are not used by the procedure but are provided as
|
||||
// a convenience to clients. You can think of this as a scratch space for clients.
|
||||
struct Context {
|
||||
filament::VertexBuffer* const vb;
|
||||
const uint8_t slot;
|
||||
};
|
||||
|
||||
// The outputs of the procedure. The results array gets malloc'd by the procedure, so clients
|
||||
// should remember to free it.
|
||||
struct OutputParams {
|
||||
cgltf_size vertexCount;
|
||||
filament::math::short4* results;
|
||||
};
|
||||
|
||||
// Clients might want to track the jobs in an array, so the arguments are bundled into a struct.
|
||||
struct Params {
|
||||
InputParams in;
|
||||
Context context;
|
||||
OutputParams out;
|
||||
};
|
||||
|
||||
// Performs tangents generation synchronously. This can be invoked from inside a job if desired.
|
||||
// The parameters structure is owned by the client.
|
||||
static void run(Params* params);
|
||||
};
|
||||
|
||||
} // namespace gltfio
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef GLTFIO_WIREFRAME_H
|
||||
#define GLTFIO_WIREFRAME_H
|
||||
|
||||
#include <filament/IndexBuffer.h>
|
||||
#include <filament/VertexBuffer.h>
|
||||
|
||||
#include <utils/Entity.h>
|
||||
|
||||
namespace gltfio {
|
||||
|
||||
struct FFilamentAsset;
|
||||
|
||||
struct Wireframe {
|
||||
Wireframe(FFilamentAsset* asset);
|
||||
~Wireframe();
|
||||
const FFilamentAsset* mAsset;
|
||||
utils::Entity mEntity;
|
||||
filament::VertexBuffer* mVertexBuffer;
|
||||
filament::IndexBuffer* mIndexBuffer;
|
||||
};
|
||||
|
||||
} // namsepace gltfio
|
||||
|
||||
#endif // GLTFIO_WIREFRAME_H
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2015 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TNT_FILAMENT_UPCAST_H
|
||||
#define TNT_FILAMENT_UPCAST_H
|
||||
|
||||
/*
|
||||
* Generates functions to safely upcast a pointer Bar* to FBar*
|
||||
* FILAMENT_UPCAST() should be included in the header file
|
||||
* declaring FBar, e.g.:
|
||||
*
|
||||
* #include <Bar.h>
|
||||
*
|
||||
* class FBar : public Bar {
|
||||
* };
|
||||
*
|
||||
* FILAMENT_UPCAST(Bar)
|
||||
*
|
||||
*/
|
||||
|
||||
#define FILAMENT_UPCAST(CLASS) \
|
||||
inline F##CLASS& upcast(CLASS& that) noexcept { \
|
||||
return static_cast<F##CLASS &>(that); \
|
||||
} \
|
||||
inline const F##CLASS& upcast(const CLASS& that) noexcept { \
|
||||
return static_cast<const F##CLASS &>(that); \
|
||||
} \
|
||||
inline F##CLASS* upcast(CLASS* that) noexcept { \
|
||||
return static_cast<F##CLASS *>(that); \
|
||||
} \
|
||||
inline F##CLASS const* upcast(CLASS const* that) noexcept { \
|
||||
return static_cast<F##CLASS const *>(that); \
|
||||
}
|
||||
|
||||
#endif // TNT_FILAMENT_UPCAST_H
|
||||
Reference in New Issue
Block a user