included samplers dictionary for textures

pull/1015/head
Angelo Scandaliato 2016-09-30 17:23:38 -07:00
parent 7dd2e7011d
commit 0619232aef
5 changed files with 135 additions and 16 deletions

View File

@ -236,6 +236,32 @@ namespace glTF
BufferViewTarget_ELEMENT_ARRAY_BUFFER = 34963 BufferViewTarget_ELEMENT_ARRAY_BUFFER = 34963
}; };
//! Values for the Sampler::magFilter field
enum SamplerMagFilter
{
SamplerMagFilter_Nearest = 9728,
SamplerMagFilter_Linear = 9729
};
//! Values for the Sampler::minFilter field
enum SamplerMinFilter
{
SamplerMinFilter_Nearest = 9728,
SamplerMinFilter_Linear = 9729,
SamplerMinFilter_Nearest_Mipmap_Nearest = 9984,
SamplerMinFilter_Linear_Mipmap_Nearest = 9985,
SamplerMinFilter_Nearest_Mipmap_Linear = 9986,
SamplerMinFilter_Linear_Mipmap_Linear = 9987
};
//! Values for the Sampler::wrapS and Sampler::wrapT field
enum SamplerWrap
{
SamplerWrap_Clamp_To_Edge = 33071,
SamplerWrap_Mirrored_Repeat = 33648,
SamplerWrap_Repeat = 10497
};
//! Values for the Texture::format and Texture::internalFormat fields //! Values for the Texture::format and Texture::internalFormat fields
enum TextureFormat enum TextureFormat
{ {
@ -812,8 +838,14 @@ namespace glTF
struct Sampler : public Object struct Sampler : public Object
{ {
SamplerMagFilter magFilter; //!< The texture magnification filter. (required)
SamplerMinFilter minFilter; //!< The texture minification filter. (required)
SamplerWrap wrapS; //!< The texture wrapping in the S direction. (required)
SamplerWrap wrapT; //!< The texture wrapping in the T direction. (required)
Sampler() {} Sampler() {}
void Read(Value& obj, Asset& r); void Read(Value& obj, Asset& r);
void SetDefaults();
}; };
struct Scene : public Object struct Scene : public Object
@ -860,8 +892,8 @@ namespace glTF
//! A texture and its sampler. //! A texture and its sampler.
struct Texture : public Object struct Texture : public Object
{ {
//Ref<Sampler> source; //!< The ID of the sampler used by this texture. (required) Ref<Sampler> sampler; //!< The ID of the sampler used by this texture. (required)
Ref<Image> source; //!< The ID of the image used by this texture. (required) Ref<Image> source; //!< The ID of the image used by this texture. (required)
//TextureFormat format; //!< The texture's format. (default: TextureFormat_RGBA) //TextureFormat format; //!< The texture's format. (default: TextureFormat_RGBA)
//TextureFormat internalFormat; //!< The texture's internal format. (default: TextureFormat_RGBA) //TextureFormat internalFormat; //!< The texture's internal format. (default: TextureFormat_RGBA)
@ -921,7 +953,7 @@ namespace glTF
//! (Implemented in glTFAssetWriter.h) //! (Implemented in glTFAssetWriter.h)
template<class T> template<class T>
void WriteLazyDict(LazyDict<T>& d, AssetWriter& w); void WriteLazyDict(LazyDict<T>& d, AssetWriter& w);
//! Manages lazy loading of the glTF top-level objects, and keeps a reference to them by ID //! Manages lazy loading of the glTF top-level objects, and keeps a reference to them by ID
//! It is the owner the loaded objects, so when it is destroyed it also deletes them //! It is the owner the loaded objects, so when it is destroyed it also deletes them
@ -983,7 +1015,7 @@ namespace glTF
int version; //!< The glTF format version (should be 1) int version; //!< The glTF format version (should be 1)
void Read(Document& doc); void Read(Document& doc);
AssetMetadata() AssetMetadata()
: premultipliedAlpha(false) : premultipliedAlpha(false)
, version(0) , version(0)
@ -1049,7 +1081,7 @@ namespace glTF
LazyDict<Mesh> meshes; LazyDict<Mesh> meshes;
LazyDict<Node> nodes; LazyDict<Node> nodes;
//LazyDict<Program> programs; //LazyDict<Program> programs;
//LazyDict<Sampler> samplers; LazyDict<Sampler> samplers;
LazyDict<Scene> scenes; LazyDict<Scene> scenes;
//LazyDict<Shader> shaders; //LazyDict<Shader> shaders;
//LazyDict<Skin> skins; //LazyDict<Skin> skins;
@ -1074,7 +1106,7 @@ namespace glTF
, meshes (*this, "meshes") , meshes (*this, "meshes")
, nodes (*this, "nodes") , nodes (*this, "nodes")
//, programs (*this, "programs") //, programs (*this, "programs")
//, samplers (*this, "samplers") , samplers (*this, "samplers")
, scenes (*this, "scenes") , scenes (*this, "scenes")
//, shaders (*this, "shaders") //, shaders (*this, "shaders")
//, skins (*this, "skins") //, skins (*this, "skins")

View File

@ -53,7 +53,7 @@ using namespace Assimp;
namespace glTF { namespace glTF {
namespace { namespace {
// //
// JSON Value reading helpers // JSON Value reading helpers
// //
@ -62,7 +62,7 @@ namespace {
struct ReadHelper { static bool Read(Value& val, T& out) { struct ReadHelper { static bool Read(Value& val, T& out) {
return val.IsInt() ? out = static_cast<T>(val.GetInt()), true : false; return val.IsInt() ? out = static_cast<T>(val.GetInt()), true : false;
}}; }};
template<> struct ReadHelper<bool> { static bool Read(Value& val, bool& out) { template<> struct ReadHelper<bool> { static bool Read(Value& val, bool& out) {
return val.IsBool() ? out = val.GetBool(), true : false; return val.IsBool() ? out = val.GetBool(), true : false;
}}; }};
@ -70,7 +70,7 @@ namespace {
template<> struct ReadHelper<float> { static bool Read(Value& val, float& out) { template<> struct ReadHelper<float> { static bool Read(Value& val, float& out) {
return val.IsNumber() ? out = static_cast<float>(val.GetDouble()), true : false; return val.IsNumber() ? out = static_cast<float>(val.GetDouble()), true : false;
}}; }};
template<unsigned int N> struct ReadHelper<float[N]> { static bool Read(Value& val, float (&out)[N]) { template<unsigned int N> struct ReadHelper<float[N]> { static bool Read(Value& val, float (&out)[N]) {
if (!val.IsArray() || val.Size() != N) return false; if (!val.IsArray() || val.Size() != N) return false;
for (unsigned int i = 0; i < N; ++i) { for (unsigned int i = 0; i < N; ++i) {
@ -312,7 +312,7 @@ inline void Buffer::Read(Value& obj, Asset& r)
if (file) { if (file) {
bool ok = LoadFromStream(*file, byteLength); bool ok = LoadFromStream(*file, byteLength);
delete file; delete file;
if (!ok) if (!ok)
throw DeadlyImportError("GLTF: error while reading referenced file \"" + std::string(uri) + "\"" ); throw DeadlyImportError("GLTF: error while reading referenced file \"" + std::string(uri) + "\"" );
} }
@ -439,7 +439,7 @@ inline void BufferView::Read(Value& obj, Asset& r)
if (bufferId) { if (bufferId) {
buffer = r.buffers.Get(bufferId); buffer = r.buffers.Get(bufferId);
} }
byteOffset = MemberOrDefault(obj, "byteOffset", 0u); byteOffset = MemberOrDefault(obj, "byteOffset", 0u);
byteLength = MemberOrDefault(obj, "byteLength", 0u); byteLength = MemberOrDefault(obj, "byteLength", 0u);
} }
@ -599,7 +599,7 @@ inline Image::Image()
inline void Image::Read(Value& obj, Asset& r) inline void Image::Read(Value& obj, Asset& r)
{ {
// Check for extensions first (to detect binary embedded data) // Check for extensions first (to detect binary embedded data)
if (Value* extensions = FindObject(obj, "extensions")) { if (Value* extensions = FindObject(obj, "extensions")) {
if (r.extensionsUsed.KHR_binary_glTF) { if (r.extensionsUsed.KHR_binary_glTF) {
if (Value* ext = FindObject(*extensions, "KHR_binary_glTF")) { if (Value* ext = FindObject(*extensions, "KHR_binary_glTF")) {
@ -665,12 +665,35 @@ inline void Image::SetData(uint8_t* data, size_t length, Asset& r)
} }
} }
inline void Sampler::Read(Value& obj, Asset& r)
{
SetDefaults();
ReadMember(obj, "magFilter", magFilter);
ReadMember(obj, "minFilter", minFilter);
ReadMember(obj, "wrapS", wrapS);
ReadMember(obj, "wrapT", wrapT);
}
inline void Sampler::SetDefaults()
{
magFilter = SamplerMagFilter_Linear;
minFilter = SamplerMinFilter_Linear;
wrapS = SamplerWrap_Repeat;
wrapT = SamplerWrap_Repeat;
}
inline void Texture::Read(Value& obj, Asset& r) inline void Texture::Read(Value& obj, Asset& r)
{ {
const char* sourcestr; const char* sourcestr;
if (ReadMember(obj, "source", sourcestr)) { if (ReadMember(obj, "source", sourcestr)) {
source = r.images.Get(sourcestr); source = r.images.Get(sourcestr);
} }
const char* samplerstr;
if (ReadMember(obj, "sampler", samplerstr)) {
sampler = r.samplers.Get(samplerstr);
}
} }
namespace { namespace {
@ -1131,7 +1154,7 @@ inline void Node::Read(Value& obj, Asset& r)
} }
} }
if (Value* matrix = FindArray(obj, "matrix")) { if (Value* matrix = FindArray(obj, "matrix")) {
ReadValue(*matrix, this->matrix); ReadValue(*matrix, this->matrix);
} }
@ -1407,13 +1430,13 @@ inline std::string Asset::FindUniqueID(const std::string& str, const char* suffi
id = buffer; id = buffer;
it = mUsedIds.find(id); it = mUsedIds.find(id);
} }
return id; return id;
} }
namespace Util { namespace Util {
inline inline
bool ParseDataURI(const char* const_uri, size_t uriLen, DataURI& out) { bool ParseDataURI(const char* const_uri, size_t uriLen, DataURI& out) {
if ( NULL == const_uri ) { if ( NULL == const_uri ) {
return false; return false;

View File

@ -333,7 +333,18 @@ namespace glTF {
inline void Write(Value& obj, Sampler& b, AssetWriter& w) inline void Write(Value& obj, Sampler& b, AssetWriter& w)
{ {
if (b.wrapS) {
obj.AddMember("wrapS", b.wrapS, w.mAl);
}
if (b.wrapT) {
obj.AddMember("wrapT", b.wrapT, w.mAl);
}
if (b.magFilter) {
obj.AddMember("magFilter", b.magFilter, w.mAl);
}
if (b.minFilter) {
obj.AddMember("minFilter", b.minFilter, w.mAl);
}
} }
inline void Write(Value& scene, Scene& s, AssetWriter& w) inline void Write(Value& scene, Scene& s, AssetWriter& w)
@ -361,6 +372,9 @@ namespace glTF {
if (tex.source) { if (tex.source) {
obj.AddMember("source", Value(tex.source->id, w.mAl).Move(), w.mAl); obj.AddMember("source", Value(tex.source->id, w.mAl).Move(), w.mAl);
} }
if (tex.sampler) {
obj.AddMember("sampler", Value(tex.sampler->id, w.mAl).Move(), w.mAl);
}
} }
inline void Write(Value& obj, Light& b, AssetWriter& w) inline void Write(Value& obj, Light& b, AssetWriter& w)

View File

@ -235,6 +235,52 @@ namespace {
} }
} }
void glTFExporter::GetTexSampler(const aiMaterial* mat, glTF::TexProperty& prop)
{
std::string samplerId = mAsset->FindUniqueID("", "sampler");
prop.texture->sampler = mAsset->samplers.Create(samplerId);
aiTextureMapMode mapU, mapV;
aiGetMaterialInteger(mat,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0),(int*)&mapU);
aiGetMaterialInteger(mat,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0),(int*)&mapV);
switch (mapU) {
case aiTextureMapMode_Wrap:
prop.texture->sampler->wrapS = SamplerWrap_Repeat;
break;
case aiTextureMapMode_Clamp:
prop.texture->sampler->wrapS = SamplerWrap_Clamp_To_Edge;
break;
case aiTextureMapMode_Mirror:
prop.texture->sampler->wrapS = SamplerWrap_Mirrored_Repeat;
break;
case aiTextureMapMode_Decal:
default:
prop.texture->sampler->wrapS = SamplerWrap_Repeat;
break;
};
switch (mapV) {
case aiTextureMapMode_Wrap:
prop.texture->sampler->wrapT = SamplerWrap_Repeat;
break;
case aiTextureMapMode_Clamp:
prop.texture->sampler->wrapT = SamplerWrap_Clamp_To_Edge;
break;
case aiTextureMapMode_Mirror:
prop.texture->sampler->wrapT = SamplerWrap_Mirrored_Repeat;
break;
case aiTextureMapMode_Decal:
default:
prop.texture->sampler->wrapT = SamplerWrap_Repeat;
break;
};
// Hard coded Texture filtering options because I do not know where to find them in the aiMaterial.
prop.texture->sampler->magFilter = SamplerMagFilter_Linear;
prop.texture->sampler->minFilter = SamplerMinFilter_Linear;
}
void glTFExporter::GetMatColorOrTex(const aiMaterial* mat, glTF::TexProperty& prop, const char* propName, int type, int idx, aiTextureType tt) void glTFExporter::GetMatColorOrTex(const aiMaterial* mat, glTF::TexProperty& prop, const char* propName, int type, int idx, aiTextureType tt)
{ {
aiString tex; aiString tex;
@ -274,6 +320,8 @@ void glTFExporter::GetMatColorOrTex(const aiMaterial* mat, glTF::TexProperty& pr
else { else {
prop.texture->source->uri = path; prop.texture->source->uri = path;
} }
GetTexSampler(mat, prop);
} }
} }
} }
@ -284,6 +332,7 @@ void glTFExporter::GetMatColorOrTex(const aiMaterial* mat, glTF::TexProperty& pr
} }
} }
void glTFExporter::ExportMaterials() void glTFExporter::ExportMaterials()
{ {
aiString aiName; aiString aiName;

View File

@ -94,6 +94,7 @@ namespace Assimp
void WriteBinaryData(IOStream* outfile, std::size_t sceneLength); void WriteBinaryData(IOStream* outfile, std::size_t sceneLength);
void GetTexSampler(const aiMaterial* mat, glTF::TexProperty& prop);
void GetMatColorOrTex(const aiMaterial* mat, glTF::TexProperty& prop, const char* propName, int type, int idx, aiTextureType tt); void GetMatColorOrTex(const aiMaterial* mat, glTF::TexProperty& prop, const char* propName, int type, int idx, aiTextureType tt);
void ExportMetadata(); void ExportMetadata();
void ExportMaterials(); void ExportMaterials();