Merge pull request #699 from otgerp/master

glTF exporter + improved the importer (and code refactor)
pull/719/head^2
Kim Kulling 2015-12-03 13:19:38 +01:00
commit a972397196
13 changed files with 3529 additions and 1716 deletions

View File

@ -359,6 +359,27 @@ public: // static utilities
IOStream* stream,
std::vector<char>& data);
// -------------------------------------------------------------------
/** Utility function to move a std::vector into a aiScene array
* @param vec The vector to be moved
* @param out The output pointer to the allocated array.
* @param numOut The output count of elements copied. */
template<typename T>
AI_FORCE_INLINE
static void CopyVector(
std::vector<T>& vec,
T*& out,
unsigned int& outLength)
{
outLength = vec.size();
if (outLength) {
out = new T[outLength];
std::swap_ranges(vec.begin(), vec.end(), out);
}
}
protected:
/** Error description in case there was one. */

View File

@ -563,12 +563,14 @@ ADD_ASSIMP_IMPORTER(X
)
ADD_ASSIMP_IMPORTER(GLTF
glTFAsset.h
glTFAsset.inl
glTFAssetWriter.h
glTFAssetWriter.inl
glTFImporter.cpp
glTFImporter.h
glTFUtil.cpp
glTFUtil.h
glTFFileData.h
glTFExporter.h
glTFExporter.cpp
)
@ -655,7 +657,6 @@ SET ( openddl_parser_SRCS
)
SOURCE_GROUP( openddl_parser FILES ${openddl_parser_SRCS})
INCLUDE_DIRECTORIES( "../contrib/rapidjson/include" )
# VC2010 fixes

View File

@ -139,9 +139,9 @@ Exporter::ExportFormatEntry gExporters[] =
#ifndef ASSIMP_BUILD_NO_GLTF_EXPORTER
Exporter::ExportFormatEntry( "gltf", "GL Transmission Format", "gltf", &ExportSceneGLTF,
aiProcess_JoinIdenticalVertices),
aiProcess_JoinIdenticalVertices | aiProcess_SortByPType),
Exporter::ExportFormatEntry( "glb", "GL Transmission Format (binary)", "glb", &ExportSceneGLB,
aiProcess_JoinIdenticalVertices),
aiProcess_JoinIdenticalVertices | aiProcess_SortByPType),
#endif
#ifndef ASSIMP_BUILD_NO_ASSBIN_EXPORTER

945
code/glTFAsset.h 100644
View File

@ -0,0 +1,945 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2015, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
/** @file glTFAsset.h
* Declares a glTF class to handle gltf/glb files
*
* glTF Extensions Support:
* KHR_binary_glTF: full
* KHR_materials_common: full
*/
#ifndef glTFAsset_H_INC
#define glTFAsset_H_INC
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/rapidjson.h>
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#ifdef ASSIMP_API
# include "boost/shared_ptr.hpp"
# include "DefaultIOSystem.h"
# include "ByteSwapper.h"
#else
# include <memory>
# define AI_SWAP4(p)
# define ai_assert
#endif
#if _MSC_VER > 1500 || (defined __GNUC___)
# define ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
# else
# define gltf_unordered_map map
#endif
#ifdef ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
# include <unordered_map>
# if _MSC_VER > 1600
# define gltf_unordered_map unordered_map
# else
# define gltf_unordered_map tr1::unordered_map
# endif
#endif
namespace glTF
{
#ifdef ASSIMP_API
using Assimp::IOStream;
using Assimp::IOSystem;
using boost::shared_ptr;
#else
using std::shared_ptr;
typedef std::runtime_error DeadlyImportError;
typedef std::runtime_error DeadlyExportError;
enum aiOrigin { aiOrigin_SET = 0, aiOrigin_CUR = 1, aiOrigin_END = 2 };
class IOSystem;
class IOStream
{
FILE* f;
public:
IOStream(FILE* file) : f(file) {}
~IOStream() { fclose(f); f = 0; }
size_t Read(void* b, size_t sz, size_t n) { return fread(b, sz, n, f); }
size_t Write(const void* b, size_t sz, size_t n) { return fwrite(b, sz, n, f); }
int Seek(size_t off, aiOrigin orig) { return fseek(f, off, int(orig)); }
size_t Tell() const { return ftell(f); }
size_t FileSize() {
long p = Tell(), len = (Seek(0, aiOrigin_END), Tell());
return size_t((Seek(p, aiOrigin_SET), len));
}
};
#endif
using rapidjson::Value;
using rapidjson::Document;
class Asset;
class AssetWriter;
struct BufferView; // here due to cross-reference
struct Texture;
struct Light;
// Vec/matrix types, as raw float arrays
typedef float (vec3)[3];
typedef float (vec4)[4];
typedef float (mat4)[16];
namespace Util
{
void EncodeBase64(const uint8_t* in, size_t inLength, std::string& out);
size_t DecodeBase64(const char* in, size_t inLength, uint8_t*& out);
inline size_t DecodeBase64(const char* in, uint8_t*& out)
{
return DecodeBase64(in, strlen(in), out);
}
struct DataURI
{
const char* mediaType;
const char* charset;
bool base64;
const char* data;
size_t dataLength;
};
//! Check if a uri is a data URI
inline bool ParseDataURI(const char* uri, size_t uriLen, DataURI& out);
}
//! Magic number for GLB files
#define AI_GLB_MAGIC_NUMBER "glTF"
#ifdef ASSIMP_API
#include "./../include/assimp/Compiler/pushpack1.h"
#endif
//! For the KHR_binary_glTF extension (binary .glb file)
//! 20-byte header (+ the JSON + a "body" data section)
struct GLB_Header
{
uint8_t magic[4]; //!< Magic number: "glTF"
uint32_t version; //!< Version number (always 1 as of the last update)
uint32_t length; //!< Total length of the Binary glTF, including header, scene, and body, in bytes
uint32_t sceneLength; //!< Length, in bytes, of the glTF scene
uint32_t sceneFormat; //!< Specifies the format of the glTF scene (see the SceneFormat enum)
} PACK_STRUCT;
#ifdef ASSIMP_API
#include "./../include/assimp/Compiler/poppack1.h"
#endif
//! Values for the GLB_Header::sceneFormat field
enum SceneFormat
{
SceneFormat_JSON = 0
};
//! Values for the mesh primitive modes
enum PrimitiveMode
{
PrimitiveMode_POINTS = 0,
PrimitiveMode_LINES = 1,
PrimitiveMode_LINE_LOOP = 2,
PrimitiveMode_LINE_STRIP = 3,
PrimitiveMode_TRIANGLES = 4,
PrimitiveMode_TRIANGLE_STRIP = 5,
PrimitiveMode_TRIANGLE_FAN = 6
};
//! Values for the Accessor::componentType field
enum ComponentType
{
ComponentType_BYTE = 5120,
ComponentType_UNSIGNED_BYTE = 5121,
ComponentType_SHORT = 5122,
ComponentType_UNSIGNED_SHORT = 5123,
ComponentType_FLOAT = 5126
};
inline size_t ComponentTypeSize(ComponentType t)
{
switch (t) {
case ComponentType_SHORT:
case ComponentType_UNSIGNED_SHORT:
return 2;
case ComponentType_FLOAT:
return 4;
//case Accessor::ComponentType_BYTE:
//case Accessor::ComponentType_UNSIGNED_BYTE:
default:
return 1;
}
}
//! Values for the BufferView::target field
enum BufferViewTarget
{
BufferViewTarget_ARRAY_BUFFER = 34962,
BufferViewTarget_ELEMENT_ARRAY_BUFFER = 34963
};
//! Values for the Texture::format and Texture::internalFormat fields
enum TextureFormat
{
TextureFormat_ALPHA = 6406,
TextureFormat_RGB = 6407,
TextureFormat_RGBA = 6408,
TextureFormat_LUMINANCE = 6409,
TextureFormat_LUMINANCE_ALPHA = 6410
};
//! Values for the Texture::target field
enum TextureTarget
{
TextureTarget_TEXTURE_2D = 3553
};
//! Values for the Texture::type field
enum TextureType
{
TextureType_UNSIGNED_BYTE = 5121,
TextureType_UNSIGNED_SHORT_5_6_5 = 33635,
TextureType_UNSIGNED_SHORT_4_4_4_4 = 32819,
TextureType_UNSIGNED_SHORT_5_5_5_1 = 32820
};
//! Values for the Accessor::type field (helper class)
class AttribType
{
public:
enum Value
{ SCALAR, VEC2, VEC3, VEC4, MAT2, MAT3, MAT4 };
private:
static const size_t NUM_VALUES = static_cast<size_t>(MAT4)+1;
struct Info
{ const char* name; unsigned int numComponents; };
template<int N> struct data
{ static const Info infos[NUM_VALUES]; };
public:
inline static Value FromString(const char* str)
{
for (size_t i = 0; i < NUM_VALUES; ++i) {
if (strcmp(data<0>::infos[i].name, str) == 0) {
return static_cast<Value>(i);
}
}
return SCALAR;
}
inline static const char* ToString(Value type)
{
return data<0>::infos[static_cast<size_t>(type)].name;
}
inline static unsigned int GetNumComponents(Value type)
{
return data<0>::infos[static_cast<size_t>(type)].numComponents;
}
};
// must match the order of the AttribTypeTraits::Value enum!
template<int N> const AttribType::Info
AttribType::data<N>::infos[AttribType::NUM_VALUES] = {
{ "SCALAR", 1 }, { "VEC2", 2 }, { "VEC3", 3 }, { "VEC4", 4 }, { "MAT2", 4 }, { "MAT3", 9 }, { "MAT4", 16 }
};
//! A reference to one top-level object, which is valid
//! until the Asset instance is destroyed
template<class T>
class Ref
{
std::vector<T*>* vector;
int index;
public:
Ref() : vector(0), index(0) {}
Ref(std::vector<T*>& vec, int idx) : vector(&vec), index(idx) {}
inline size_t GetIndex() const
{ return index; }
operator bool() const
{ return vector != 0; }
T* operator->()
{ return (*vector)[index]; }
T& operator*()
{ return *((*vector)[index]); }
};
//! Helper struct to represent values that might not be present
template<class T>
struct Nullable
{
T value;
bool isPresent;
Nullable() : isPresent(false) {}
Nullable(T& val) : value(val), isPresent(true) {}
};
//! Base classe for all glTF top-level objects
struct Object
{
std::string id; //!< The globally unique ID used to reference this object
std::string name; //!< The user-defined name of this object
//! Objects marked as special are not exported (used to emulate the binary body buffer)
virtual bool IsSpecial() const
{ return false; }
virtual ~Object() {}
};
//
// Classes for each glTF top-level object type
//
//! A typed view into a BufferView. A BufferView contains raw binary data.
//! An accessor provides a typed view into a BufferView or a subset of a BufferView
// !similar to how WebGL's vertexAttribPointer() defines an attribute in a buffer.
struct Accessor : public Object
{
Ref<BufferView> bufferView; //!< The ID of the bufferView. (required)
unsigned int byteOffset; //!< The offset relative to the start of the bufferView in bytes. (required)
unsigned int byteStride; //!< The stride, in bytes, between attributes referenced by this accessor. (default: 0)
ComponentType componentType; //!< The datatype of components in the attribute. (required)
unsigned int count; //!< The number of attributes referenced by this accessor. (required)
AttribType::Value type; //!< Specifies if the attribute is a scalar, vector, or matrix. (required)
//std::vector<float> max; //!< Maximum value of each component in this attribute.
//std::vector<float> min; //!< Minimum value of each component in this attribute.
unsigned int GetNumComponents();
unsigned int GetBytesPerComponent();
unsigned int GetElementSize();
inline uint8_t* GetPointer();
template<class T>
void ExtractData(T*& outData);
void WriteData(size_t count, const void* src_buffer, size_t src_stride);
//! Helper class to iterate the data
class Indexer
{
friend struct Accessor;
Accessor& accessor;
uint8_t* data;
size_t elemSize, stride;
Indexer(Accessor& acc);
public:
//! Accesses the i-th value as defined by the accessor
template<class T>
T GetValue(int i);
//! Accesses the i-th value as defined by the accessor
inline unsigned int GetUInt(int i)
{
return GetValue<unsigned int>(i);
}
};
inline Indexer GetIndexer()
{
return Indexer(*this);
}
Accessor() {}
void Read(Value& obj, Asset& r);
};
struct Animation : public Object
{
struct Channel
{
};
struct Target
{
};
struct Sampler
{
};
};
//! A buffer points to binary geometry, animation, or skins.
struct Buffer : public Object
{
public:
enum Type
{
Type_arraybuffer,
Type_text
};
//std::string uri; //!< The uri of the buffer. Can be a filepath, a data uri, etc. (required)
size_t byteLength; //!< The length of the buffer in bytes. (default: 0)
//std::string type; //!< XMLHttpRequest responseType (default: "arraybuffer")
Type type;
private:
shared_ptr<uint8_t> mData; //!< Pointer to the data
bool mIsSpecial; //!< Set to true for special cases (e.g. the body buffer)
public:
Buffer();
void Read(Value& obj, Asset& r);
void LoadFromStream(IOStream& stream, size_t length = 0, size_t baseOffset = 0);
size_t AppendData(uint8_t* data, size_t length);
void Grow(size_t amount);
uint8_t* GetPointer()
{ return mData.get(); }
void MarkAsSpecial()
{ mIsSpecial = true; }
bool IsSpecial() const
{ return mIsSpecial; }
};
//! A view into a buffer generally representing a subset of the buffer.
struct BufferView : public Object
{
Ref<Buffer> buffer; //! The ID of the buffer. (required)
size_t byteOffset; //! The offset into the buffer in bytes. (required)
size_t byteLength; //! The length of the bufferView in bytes. (default: 0)
BufferViewTarget target; //! The target that the WebGL buffer should be bound to.
BufferView() {}
void Read(Value& obj, Asset& r);
};
struct Camera : public Object
{
enum Type
{
Perspective,
Orthographic
};
Type type;
union
{
struct {
float aspectRatio; //!<The floating - point aspect ratio of the field of view. (0 = undefined = use the canvas one)
float yfov; //!<The floating - point vertical field of view in radians. (required)
float zfar; //!<The floating - point distance to the far clipping plane. (required)
float znear; //!< The floating - point distance to the near clipping plane. (required)
} perspective;
struct {
float xmag; //! The floating-point horizontal magnification of the view. (required)
float ymag; //! The floating-point vertical magnification of the view. (required)
float zfar; //! The floating-point distance to the far clipping plane. (required)
float znear; //! The floating-point distance to the near clipping plane. (required)
} ortographic;
};
Camera() {}
void Read(Value& obj, Asset& r);
};
//! Image data used to create a texture.
struct Image : public Object
{
std::string uri; //! The uri of the image, that can be a file path, a data URI, etc.. (required)
Ref<BufferView> bufferView;
std::string mimeType;
int width, height;
private:
uint8_t* mData;
size_t mDataLength;
public:
Image();
void Read(Value& obj, Asset& r);
inline bool HasData() const
{ return mDataLength > 0; }
inline size_t GetDataLength() const
{ return mDataLength; }
inline const uint8_t* GetData() const
{ return mData; }
inline uint8_t* StealData();
inline void SetData(uint8_t* data, size_t length, Asset& r);
};
//! Holds a material property that can be a texture or a color
struct TexProperty
{
Ref<Texture> texture;
vec4 color;
};
//! The material appearance of a primitive.
struct Material : public Object
{
//Ref<Sampler> source; //!< The ID of the technique.
//std::gltf_unordered_map<std::string, std::string> values; //!< A dictionary object of parameter values.
//! Techniques defined by KHR_materials_common
enum Technique
{
Technique_undefined = 0,
Technique_BLINN,
Technique_PHONG,
Technique_LAMBERT,
Technique_CONSTANT
};
TexProperty ambient;
TexProperty diffuse;
TexProperty specular;
TexProperty emission;
bool doubleSided;
bool transparent;
float transparency;
float shininess;
Technique technique;
Material() { SetDefaults(); }
void Read(Value& obj, Asset& r);
void SetDefaults();
};
//! A set of primitives to be rendered. A node can contain one or more meshes. A node's transform places the mesh in the scene.
struct Mesh : public Object
{
typedef std::vector< Ref<Accessor> > AccessorList;
struct Primitive
{
PrimitiveMode mode;
struct Attributes {
AccessorList position, normal, texcoord, color, joint, jointmatrix, weight;
} attributes;
Ref<Accessor> indices;
Ref<Material> material;
};
std::vector<Primitive> primitives;
Mesh() {}
void Read(Value& obj, Asset& r);
};
struct Node : public Object
{
std::vector< Ref<Node> > children;
std::vector< Ref<Mesh> > meshes;
Nullable<mat4> matrix;
Nullable<vec3> translation;
Nullable<vec4> rotation;
Nullable<vec3> scale;
Ref<Camera> camera;
Ref<Light> light;
Node() {}
void Read(Value& obj, Asset& r);
};
struct Program : public Object
{
Program() {}
void Read(Value& obj, Asset& r);
};
struct Sampler : public Object
{
Sampler() {}
void Read(Value& obj, Asset& r);
};
struct Scene : public Object
{
std::vector< Ref<Node> > nodes;
Scene() {}
void Read(Value& obj, Asset& r);
};
struct Shader : public Object
{
Shader() {}
void Read(Value& obj, Asset& r);
};
struct Skin : public Object
{
Skin() {}
void Read(Value& obj, Asset& r);
};
struct Technique : public Object
{
struct Parameters
{
};
struct States
{
};
struct Functions
{
};
Technique() {}
void Read(Value& obj, Asset& r);
};
//! A texture and its sampler.
struct Texture : public Object
{
//Ref<Sampler> source; //!< The ID of the sampler 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 internalFormat; //!< The texture's internal format. (default: TextureFormat_RGBA)
//TextureTarget target; //!< The target that the WebGL texture should be bound to. (default: TextureTarget_TEXTURE_2D)
//TextureType type; //!< Texel datatype. (default: TextureType_UNSIGNED_BYTE)
Texture() {}
void Read(Value& obj, Asset& r);
};
//! A light (from KHR_materials_common extension)
struct Light : public Object
{
enum Type
{
Type_undefined,
Type_ambient,
Type_directional,
Type_point,
Type_spot
};
Type type;
vec4 color;
float distance;
float constantAttenuation;
float linearAttenuation;
float quadraticAttenuation;
float falloffAngle;
float falloffExponent;
Light() {}
void Read(Value& obj, Asset& r);
void SetDefaults();
};
//! Base class for LazyDict that acts as an interface
class LazyDictBase
{
public:
virtual ~LazyDictBase() {}
virtual void AttachToDocument(Document& doc) = 0;
virtual void DetachFromDocument() = 0;
virtual void WriteObjects(AssetWriter& writer) = 0;
};
//! (Stub class that is specialized in glTFAssetWriter.h)
template<class T>
struct LazyDictWriter
{
static void Write(T& d, AssetWriter& w) {}
};
//! 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
template<class T>
class LazyDict : public LazyDictBase
{
friend class Asset;
friend class AssetWriter;
typedef typename std::gltf_unordered_map< std::string, size_t > Dict;
std::vector<T*> mObjs; //! The read objects
Dict mObjsById; //! The read objects accesible by id
const char* mDictId; //! ID of the dictionary object
const char* mExtId; //! ID of the extension defining the dictionary
Value* mDict; //! JSON dictionary object
Asset& mAsset; //! The asset instance
void AttachToDocument(Document& doc);
void DetachFromDocument();
void WriteObjects(AssetWriter& writer)
{ LazyDictWriter< LazyDict >::Write(*this, writer); }
Ref<T> Add(T* obj);
public:
LazyDict(Asset& asset, const char* dictId, const char* extId = 0);
~LazyDict();
Ref<T> Get(const char* id);
Ref<T> Get(size_t i);
Ref<T> Create(const char* id);
Ref<T> Create(const std::string& id)
{ return Create(id.c_str()); }
inline size_t Size() const
{ return mObjs.size(); }
inline T& operator[](size_t i)
{ return *mObjs[i]; }
};
struct AssetMetadata
{
std::string copyright; //!< A copyright message suitable for display to credit the content creator.
std::string generator; //!< Tool that generated this glTF model.Useful for debugging.
bool premultipliedAlpha; //!< Specifies if the shaders were generated with premultiplied alpha. (default: false)
struct {
std::string api; //!< Specifies the target rendering API (default: "WebGL")
std::string version; //!< Specifies the target rendering API (default: "1.0.3")
} profile; //!< Specifies the target rendering API and version, e.g., WebGL 1.0.3. (default: {})
int version; //!< The glTF format version (should be 1)
void Read(Document& doc);
};
//
// glTF Asset class
//
//! Root object for a glTF asset
class Asset
{
typedef std::gltf_unordered_map<std::string, int> IdMap;
template<class T>
friend class LazyDict;
friend struct Buffer; // To access OpenFile
friend class AssetWriter;
private:
IOSystem* mIOSystem;
std::string mCurrentAssetDir;
size_t mSceneLength;
size_t mBodyOffset, mBodyLength;
std::vector<LazyDictBase*> mDicts;
IdMap mUsedIds;
Ref<Buffer> mBodyBuffer;
Asset(Asset&);
Asset& operator=(const Asset&);
public:
//! Keeps info about the enabled extensions
struct Extensions
{
bool KHR_binary_glTF;
bool KHR_materials_common;
} extensionsUsed;
AssetMetadata asset;
// Dictionaries for each type of object
LazyDict<Accessor> accessors;
LazyDict<Animation> animations;
LazyDict<Buffer> buffers;
LazyDict<BufferView> bufferViews;
LazyDict<Camera> cameras;
LazyDict<Image> images;
LazyDict<Material> materials;
LazyDict<Mesh> meshes;
LazyDict<Node> nodes;
//LazyDict<Program> programs;
//LazyDict<Sampler> samplers;
LazyDict<Scene> scenes;
//LazyDict<Shader> shaders;
//LazyDict<Skin> skins;
//LazyDict<Technique> techniques;
LazyDict<Texture> textures;
LazyDict<Light> lights; // KHR_materials_common ext
Ref<Scene> scene;
public:
Asset(IOSystem* io = 0)
: mIOSystem(io)
, accessors (*this, "accessors")
, animations (*this, "animations")
, buffers (*this, "buffers")
, bufferViews (*this, "bufferViews")
, cameras (*this, "cameras")
, images (*this, "images")
, materials (*this, "materials")
, meshes (*this, "meshes")
, nodes (*this, "nodes")
//, programs (*this, "programs")
//, samplers (*this, "samplers")
, scenes (*this, "scenes")
//, shaders (*this, "shaders")
//, skins (*this, "skins")
//, techniques (*this, "techniques")
, textures (*this, "textures")
, lights (*this, "lights", "KHR_materials_common")
{
memset(&extensionsUsed, 0, sizeof(extensionsUsed));
memset(&asset, 0, sizeof(asset));
}
//! Main function
void Load(const std::string& file, bool isBinary = false);
//! Enables the "KHR_binary_glTF" extension on the asset
void SetAsBinary();
//! Search for an available name, starting from the given strings
std::string FindUniqueID(const std::string& str, const char* suffix);
Ref<Buffer> GetBodyBuffer()
{ return mBodyBuffer; }
private:
void ReadBinaryHeader(IOStream& stream);
void ReadExtensionsUsed(Document& doc);
IOStream* OpenFile(std::string path, const char* mode, bool absolute = false);
};
}
// Include the implementation of the methods
#include "glTFAsset.inl"
#endif

1215
code/glTFAsset.inl 100644

File diff suppressed because it is too large Load Diff

View File

@ -37,49 +37,53 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
#ifndef AI_GLTFUTIL_H_INC
#define AI_GLTFUTIL_H_INC
//#include "StreamReader.h"
//#include "MemoryIOWrapper.h"
#include "StringComparison.h"
/** @file glTFWriter.h
* Declares a class to write gltf/glb files
*
* glTF Extensions Support:
* KHR_binary_glTF: full
* KHR_materials_common: full
*/
#ifndef glTFAssetWriter_H_INC
#define glTFAssetWriter_H_INC
#if _MSC_VER > 1500 || (defined __GNUC___)
# define ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
# else
# define gltf_unordered_map map
# define gltf_unordered_multimap multimap
#endif
#include "glTFAsset.h"
#ifdef ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
# include <unordered_map>
# if _MSC_VER > 1600
# define gltf_unordered_map unordered_map
# define gltf_unordered_multimap unordered_multimap
# else
# define gltf_unordered_map tr1::unordered_map
# define gltf_unordered_multimap tr1::unordered_multimap
# endif
#endif
namespace glTF
{
namespace Assimp {
namespace glTF {
using rapidjson::MemoryPoolAllocator;
//
// Misc
//
class AssetWriter
{
template<class T>
friend struct LazyDictWriter;
std::size_t DecodeBase64(const char* in, uint8_t*& out);
std::size_t DecodeBase64(const char* in, std::size_t inLength, uint8_t*& out);
private:
void EncodeBase64(const uint8_t* in, std::size_t inLength, std::string& out);
void WriteBinaryData(IOStream* outfile, size_t sceneLength);
void WriteMetadata();
void WriteExtensionsUsed();
bool IsDataURI(const char* uri);
template<class T>
void WriteObjects(LazyDict<T>& d);
public:
Document mDoc;
Asset& mAsset;
MemoryPoolAllocator<>& mAl;
AssetWriter(Asset& asset);
void WriteFile(const char* path);
};
}
}
// Include the implementation of the methods
#include "glTFAssetWriter.inl"
#endif // AI_GLTFUTIL_H_INC
#endif

View File

@ -0,0 +1,497 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2015, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <rapidjson/prettywriter.h>
namespace glTF {
using rapidjson::StringBuffer;
using rapidjson::PrettyWriter;
using rapidjson::Writer;
using rapidjson::StringRef;
using rapidjson::StringRef;
namespace {
template<size_t N>
inline Value& MakeValue(Value& val, float(&r)[N], MemoryPoolAllocator<>& al) {
val.SetArray();
val.Reserve(N, al);
for (int i = 0; i < N; ++i) {
val.PushBack(r[i], al);
}
return val;
};
template<class T>
inline void AddRefsVector(Value& obj, const char* fieldId, std::vector< Ref<T> >& v, MemoryPoolAllocator<>& al) {
if (v.empty()) return;
Value lst;
lst.SetArray();
lst.Reserve(v.size(), al);
for (size_t i = 0; i < v.size(); ++i) {
lst.PushBack(StringRef(v[i]->id), al);
}
obj.AddMember(StringRef(fieldId), lst, al);
};
}
inline void Write(Value& obj, Accessor& a, AssetWriter& w)
{
obj.AddMember("bufferView", Value(a.bufferView->id, w.mAl).Move(), w.mAl);
obj.AddMember("byteOffset", a.byteOffset, w.mAl);
obj.AddMember("byteStride", a.byteStride, w.mAl);
obj.AddMember("componentType", int(a.componentType), w.mAl);
obj.AddMember("count", a.count, w.mAl);
obj.AddMember("type", StringRef(AttribType::ToString(a.type)), w.mAl);
}
inline void Write(Value& obj, Animation& a, AssetWriter& w)
{
}
inline void Write(Value& obj, Buffer& b, AssetWriter& w)
{
std::string dataURI = "data:application/octet-stream;base64,";
Util::EncodeBase64(b.GetPointer(), b.byteLength, dataURI);
const char* type;
switch (b.type) {
case Buffer::Type_text:
type = "text"; break;
default:
type = "arraybuffer";
}
obj.AddMember("byteLength", b.byteLength, w.mAl);
obj.AddMember("type", StringRef(type), w.mAl);
obj.AddMember("uri", Value(dataURI, w.mAl).Move(), w.mAl);
}
inline void Write(Value& obj, BufferView& bv, AssetWriter& w)
{
obj.AddMember("buffer", Value(bv.buffer->id, w.mAl).Move(), w.mAl);
obj.AddMember("byteOffset", bv.byteOffset, w.mAl);
obj.AddMember("byteLength", bv.byteLength, w.mAl);
obj.AddMember("target", int(bv.target), w.mAl);
}
inline void Write(Value& obj, Camera& c, AssetWriter& w)
{
}
inline void Write(Value& obj, Image& img, AssetWriter& w)
{
std::string uri;
if (w.mAsset.extensionsUsed.KHR_binary_glTF && img.bufferView) {
Value exts, ext;
exts.SetObject();
ext.SetObject();
ext.AddMember("bufferView", StringRef(img.bufferView->id), w.mAl);
if (!img.mimeType.empty())
ext.AddMember("mimeType", StringRef(img.mimeType), w.mAl);
exts.AddMember("KHR_binary_glTF", ext, w.mAl);
obj.AddMember("extensions", exts, w.mAl);
return;
}
else if (img.HasData()) {
uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType);
uri += ";base64,";
Util::EncodeBase64(img.GetData(), img.GetDataLength(), uri);
}
else {
uri = img.uri;
}
obj.AddMember("uri", Value(uri, w.mAl).Move(), w.mAl);
}
namespace {
inline void WriteColorOrTex(Value& obj, TexProperty& prop, const char* propName, MemoryPoolAllocator<>& al)
{
if (prop.texture)
obj.AddMember(StringRef(propName), Value(prop.texture->id, al).Move(), al);
else {
Value col;
obj.AddMember(StringRef(propName), MakeValue(col, prop.color, al), al);
}
}
}
inline void Write(Value& obj, Material& m, AssetWriter& w)
{
Value v;
v.SetObject();
{
WriteColorOrTex(v, m.ambient, "ambient", w.mAl);
WriteColorOrTex(v, m.diffuse, "diffuse", w.mAl);
WriteColorOrTex(v, m.specular, "specular", w.mAl);
WriteColorOrTex(v, m.emission, "emission", w.mAl);
v.AddMember("shininess", m.shininess, w.mAl);
}
obj.AddMember("values", v, w.mAl);
}
namespace {
inline void WriteAttrs(AssetWriter& w, Value& attrs, Mesh::AccessorList& lst,
const char* semantic, bool forceNumber = false)
{
if (lst.empty()) return;
if (lst.size() == 1 && !forceNumber) {
attrs.AddMember(StringRef(semantic), Value(lst[0]->id, w.mAl).Move(), w.mAl);
}
else {
for (size_t i = 0; i < lst.size(); ++i) {
char buffer[32];
sprintf(buffer, "%s_%d", semantic, int(i));
attrs.AddMember(Value(buffer, w.mAl).Move(), Value(lst[i]->id, w.mAl).Move(), w.mAl);
}
}
}
}
inline void Write(Value& obj, Mesh& m, AssetWriter& w)
{
Value primitives;
primitives.SetArray();
primitives.Reserve(m.primitives.size(), w.mAl);
for (size_t i = 0; i < m.primitives.size(); ++i) {
Mesh::Primitive& p = m.primitives[i];
Value prim;
prim.SetObject();
{
prim.AddMember("mode", Value(int(p.mode)).Move(), w.mAl);
if (p.material)
prim.AddMember("material", p.material->id, w.mAl);
if (p.indices)
prim.AddMember("indices", Value(p.indices->id, w.mAl).Move(), w.mAl);
Value attrs;
attrs.SetObject();
{
WriteAttrs(w, attrs, p.attributes.position, "POSITION");
WriteAttrs(w, attrs, p.attributes.normal, "NORMAL");
WriteAttrs(w, attrs, p.attributes.texcoord, "TEXCOORD", true);
WriteAttrs(w, attrs, p.attributes.color, "COLOR");
WriteAttrs(w, attrs, p.attributes.joint, "JOINT");
WriteAttrs(w, attrs, p.attributes.jointmatrix, "JOINTMATRIX");
WriteAttrs(w, attrs, p.attributes.weight, "WEIGHT");
}
prim.AddMember("attributes", attrs, w.mAl);
}
primitives.PushBack(prim, w.mAl);
}
obj.AddMember("primitives", primitives, w.mAl);
}
inline void Write(Value& obj, Node& n, AssetWriter& w)
{
if (n.matrix.isPresent) {
Value val;
obj.AddMember("matrix", MakeValue(val, n.matrix.value, w.mAl).Move(), w.mAl);
}
if (n.translation.isPresent) {
Value val;
obj.AddMember("translation", MakeValue(val, n.translation.value, w.mAl).Move(), w.mAl);
}
if (n.scale.isPresent) {
Value val;
obj.AddMember("scale", MakeValue(val, n.scale.value, w.mAl).Move(), w.mAl);
}
if (n.rotation.isPresent) {
Value val;
obj.AddMember("rotation", MakeValue(val, n.rotation.value, w.mAl).Move(), w.mAl);
}
AddRefsVector(obj, "children", n.children, w.mAl);
AddRefsVector(obj, "meshes", n.meshes, w.mAl);
}
inline void Write(Value& obj, Program& b, AssetWriter& w)
{
}
inline void Write(Value& obj, Sampler& b, AssetWriter& w)
{
}
inline void Write(Value& scene, Scene& s, AssetWriter& w)
{
AddRefsVector(scene, "nodes", s.nodes, w.mAl);
}
inline void Write(Value& obj, Shader& b, AssetWriter& w)
{
}
inline void Write(Value& obj, Skin& b, AssetWriter& w)
{
}
inline void Write(Value& obj, Technique& b, AssetWriter& w)
{
}
inline void Write(Value& obj, Texture& tex, AssetWriter& w)
{
if (tex.source) {
obj.AddMember("source", Value(tex.source->id, w.mAl).Move(), w.mAl);
}
}
inline void Write(Value& obj, Light& b, AssetWriter& w)
{
}
AssetWriter::AssetWriter(Asset& a)
: mDoc()
, mAsset(a)
, mAl(mDoc.GetAllocator())
{
mDoc.SetObject();
WriteMetadata();
WriteExtensionsUsed();
// Dump the contents of the dictionaries
for (size_t i = 0; i < a.mDicts.size(); ++i) {
a.mDicts[i]->WriteObjects(*this);
}
// Add the target scene field
if (mAsset.scene) {
mDoc.AddMember("scene", StringRef(mAsset.scene->id), mAl);
}
}
void AssetWriter::WriteFile(const char* path)
{
bool isBinary = mAsset.extensionsUsed.KHR_binary_glTF;
boost::scoped_ptr<IOStream> outfile
(mAsset.OpenFile(path, isBinary ? "wb" : "wt", true));
if (outfile == 0) {
throw DeadlyExportError("Could not open output file: " + std::string(path));
}
if (isBinary) {
// we will write the header later, skip its size
outfile->Seek(sizeof(GLB_Header), aiOrigin_SET);
}
StringBuffer docBuffer;
bool pretty = true;
if (!isBinary && pretty) {
PrettyWriter<StringBuffer> writer(docBuffer);
mDoc.Accept(writer);
}
else {
Writer<StringBuffer> writer(docBuffer);
mDoc.Accept(writer);
}
if (outfile->Write(docBuffer.GetString(), docBuffer.GetSize(), 1) != 1) {
throw DeadlyExportError("Failed to write scene data!");
}
if (isBinary) {
WriteBinaryData(outfile.get(), docBuffer.GetSize());
}
}
void AssetWriter::WriteBinaryData(IOStream* outfile, size_t sceneLength)
{
//
// write the body data
//
size_t bodyLength = 0;
if (Ref<Buffer> b = mAsset.GetBodyBuffer()) {
bodyLength = b->byteLength;
if (bodyLength > 0) {
size_t bodyOffset = sizeof(GLB_Header) + sceneLength;
bodyOffset = (bodyOffset + 3) & ~3; // Round up to next multiple of 4
outfile->Seek(bodyOffset, aiOrigin_SET);
if (outfile->Write(b->GetPointer(), b->byteLength, 1) != 1) {
throw DeadlyExportError("Failed to write body data!");
}
}
}
//
// write the header
//
GLB_Header header;
memcpy(header.magic, AI_GLB_MAGIC_NUMBER, sizeof(header.magic));
header.version = 1;
AI_SWAP4(header.version);
header.length = sizeof(header) + sceneLength + bodyLength;
AI_SWAP4(header.length);
header.sceneLength = sceneLength;
AI_SWAP4(header.sceneLength);
header.sceneFormat = SceneFormat_JSON;
AI_SWAP4(header.sceneFormat);
outfile->Seek(0, aiOrigin_SET);
if (outfile->Write(&header, 1, sizeof(header)) != sizeof(header)) {
throw DeadlyExportError("Failed to write the header!");
}
}
void AssetWriter::WriteMetadata()
{
Value asset;
asset.SetObject();
{
asset.AddMember("version", mAsset.asset.version, mAl);
asset.AddMember("generator", Value(mAsset.asset.generator, mAl).Move(), mAl);
}
mDoc.AddMember("asset", asset, mAl);
}
void AssetWriter::WriteExtensionsUsed()
{
Value exts;
exts.SetArray();
{
if (false)
exts.PushBack(StringRef("KHR_binary_glTF"), mAl);
if (false)
exts.PushBack(StringRef("KHR_materials_common"), mAl);
}
if (!exts.Empty())
mDoc.AddMember("extensionsUsed", exts, mAl);
}
template<class T>
void AssetWriter::WriteObjects(LazyDict<T>& d)
{
if (d.mObjs.empty()) return;
Value* container = &mDoc;
if (d.mExtId) {
Value* exts = FindObject(mDoc, "extensions");
if (!exts) {
mDoc.AddMember("extensions", Value().SetObject().Move(), mDoc.GetAllocator());
exts = FindObject(mDoc, "extensions");
}
if (!(container = FindObject(*exts, d.mExtId))) {
exts->AddMember(StringRef(d.mExtId), Value().SetObject().Move(), mDoc.GetAllocator());
container = FindObject(*exts, d.mExtId);
}
}
Value* dict;
if (!(dict = FindObject(*container, d.mDictId))) {
container->AddMember(StringRef(d.mDictId), Value().SetObject().Move(), mDoc.GetAllocator());
dict = FindObject(*container, d.mDictId);
}
for (size_t i = 0; i < d.mObjs.size(); ++i) {
if (d.mObjs[i]->IsSpecial()) continue;
Value obj;
obj.SetObject();
if (!d.mObjs[i]->name.empty()) {
obj.AddMember("name", StringRef(d.mObjs[i]->name.c_str()), mAl);
}
Write(obj, *d.mObjs[i], *this);
dict->AddMember(StringRef(d.mObjs[i]->id), obj, mAl);
}
}
template<class T>
struct LazyDictWriter< LazyDict<T> >
{
static void Write(LazyDict<T>& d, AssetWriter& w)
{
w.WriteObjects(d);
}
};
}

View File

@ -56,20 +56,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/foreach.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/stringbuffer.h>
#include "glTFFileData.h"
#include "glTFUtil.h"
#include "glTFAssetWriter.h"
using namespace rapidjson;
using namespace Assimp;
using namespace Assimp::glTF;
using namespace glTF;
namespace Assimp {
@ -93,307 +87,279 @@ namespace Assimp {
class glTFSceneExporter
{
typedef std::gltf_unordered_map<std::string, int> IdMap;
Document& mDoc;
MemoryPoolAllocator<>& mAl;
const aiScene* mScene;
std::string mRootNodeId;
std::vector<std::string> mMeshIds;
IdMap mUsedIds;
public:
glTFSceneExporter(Document& doc, const aiScene* pScene)
: mDoc(doc)
, mAl(doc.GetAllocator())
, mScene(pScene)
{
doc.SetObject();
for (unsigned int i = 0; i < pScene->mNumAnimations; ++i) {
}
for (unsigned int i = 0; i < pScene->mNumCameras; ++i) {
}
for (unsigned int i = 0; i < pScene->mNumLights; ++i) {
}
for (unsigned int i = 0; i < pScene->mNumLights; ++i) {
}
for (unsigned int i = 0; i < pScene->mNumMaterials; ++i) {
}
AddMeshes();
for (unsigned int i = 0; i < pScene->mNumTextures; ++i) {
}
AddNodes();
CreateScene();
}
inline void Pushf(Value& val, float f)
{
val.PushBack(Value(f).Move(), mAl);
}
inline void SetMatrix(Value& v, const aiMatrix4x4& m)
{
v.SetArray();
v.Reserve(16, mAl);
Pushf(v, m.a1); Pushf(v, m.b1); Pushf(v, m.c1); Pushf(v, m.d1);
Pushf(v, m.a2); Pushf(v, m.b2); Pushf(v, m.c2); Pushf(v, m.d2);
Pushf(v, m.a3); Pushf(v, m.b3); Pushf(v, m.c3); Pushf(v, m.d3);
Pushf(v, m.a4); Pushf(v, m.b4); Pushf(v, m.c4); Pushf(v, m.d4);
}
void AddMeshes()
{
if (mScene->mNumMeshes == 0) return;
Value meshes;
meshes.SetObject();
mMeshIds.reserve(mScene->mNumMeshes);
for (unsigned int i = 0; i < mScene->mNumMeshes; ++i) {
aiMesh* m = mScene->mMeshes[i];
std::string meshId = FindID(m->mName, "mesh");
mMeshIds.push_back(meshId);
Value mesh;
mesh.SetObject();
{
Value primitives;
primitives.SetObject();
mesh.AddMember("primitives", primitives, mAl);
}
meshes.AddMember(StringRef(mMeshIds.back()), mesh, mAl);
}
mDoc.AddMember("meshes", meshes, mAl);
}
void AddNodes()
{
if (!mScene->mRootNode) return;
Value nodes;
nodes.SetObject();
mRootNodeId = AddNode(nodes, mScene->mRootNode);
mDoc.AddMember("nodes", nodes, mAl);
}
std::string AddNode(Value& nodes, const aiNode* n)
{
std::string nodeId = FindID(n->mName, "node");
Value node;
node.SetObject();
if (!n->mTransformation.IsIdentity()) {
Value matrix;
SetMatrix(matrix, n->mTransformation);
node.AddMember("matrix", matrix, mAl);
}
if (n->mNumMeshes > 0) {
Value meshes;
meshes.SetArray();
for (unsigned int i = 0; i < n->mNumMeshes; ++i) {
meshes.PushBack(StringRef(mMeshIds[n->mMeshes[i]]), mAl);
}
node.AddMember("meshes", meshes, mAl);
}
if (n->mNumChildren > 0) {
Value children;
children.SetArray();
for (unsigned int i = 0; i < n->mNumChildren; ++i) {
std::string id = AddNode(nodes, n->mChildren[i]);
children.PushBack(Value(id, mAl).Move(), mAl);
}
node.AddMember("children", children, mAl);
}
nodes.AddMember(Value(nodeId, mAl).Move(), node, mAl);
return nodeId;
}
void CreateScene()
{
const char* sceneName = "defaultScene";
mDoc.AddMember("scene", Value(sceneName, mAl).Move(), mAl);
Value scenes;
scenes.SetObject();
{
Value scene;
scene.SetObject();
{
Value nodes;
nodes.SetArray();
if (!mRootNodeId.empty()) {
nodes.PushBack(StringRef(mRootNodeId), mAl);
}
scene.AddMember("nodes", nodes, mAl);
}
scenes.AddMember(Value(sceneName, mAl).Move(), scene, mAl);
}
mDoc.AddMember("scenes", scenes, mAl);
}
std::string FindID(const aiString& str, const char* suffix)
{
std::string id = str.C_Str();
IdMap::iterator it;
do {
if (!id.empty()) {
it = mUsedIds.find(id);
if (it == mUsedIds.end()) break;
id += "-";
}
id += suffix;
it = mUsedIds.find(id);
if (it == mUsedIds.end()) break;
char buffer[256];
int offset = sprintf(buffer, "%s-", id.c_str());
for (int i = 0; it == mUsedIds.end(); ++i) {
ASSIMP_itoa10(buffer + offset, sizeof(buffer), i);
id = buffer;
it = mUsedIds.find(id);
}
} while (false); // fake loop to allow using "break"
mUsedIds[id] = true;
return id;
}
};
glTFExporter::glTFExporter(const char* filename, IOSystem* pIOSystem, const aiScene* pScene,
const ExportProperties* pProperties, bool isBinary)
: mFilename(filename)
, mIOSystem(pIOSystem)
, mScene(pScene)
, mProperties(pProperties)
, mIsBinary(isBinary)
{
boost::scoped_ptr<IOStream> outfile(pIOSystem->Open(mFilename, "wt"));
if (outfile == 0) {
throw DeadlyExportError("Could not open output file: " + std::string(mFilename));
}
boost::scoped_ptr<Asset> asset(new glTF::Asset(pIOSystem));
mAsset = asset.get();
if (isBinary) {
// we will write the header later, skip its size
outfile->Seek(sizeof(GLB_Header), aiOrigin_SET);
asset->SetAsBinary();
}
ExportMetadata();
Document doc;
StringBuffer docBuffer;
{
glTFSceneExporter exportScene(doc, mScene);
//for (unsigned int i = 0; i < pScene->mNumAnimations; ++i) {}
bool pretty = true;
if (!isBinary && pretty) {
PrettyWriter<StringBuffer> writer(docBuffer);
doc.Accept(writer);
}
else {
Writer<StringBuffer> writer(docBuffer);
doc.Accept(writer);
}
//for (unsigned int i = 0; i < pScene->mNumCameras; ++i) {}
//for (unsigned int i = 0; i < pScene->mNumLights; ++i) {}
ExportMaterials();
ExportMeshes();
//for (unsigned int i = 0; i < pScene->mNumTextures; ++i) {}
if (mScene->mRootNode) {
ExportNode(mScene->mRootNode);
}
if (outfile->Write(docBuffer.GetString(), docBuffer.GetSize(), 1) != 1) {
throw DeadlyExportError("Failed to write scene data!");
}
ExportScene();
if (isBinary) {
WriteBinaryData(outfile.get(), docBuffer.GetSize());
}
glTF::AssetWriter writer(*mAsset);
writer.WriteFile(filename);
}
void glTFExporter::WriteBinaryData(IOStream* outfile, std::size_t sceneLength)
static void CopyValue(const aiMatrix4x4& v, glTF::mat4& o)
{
o[ 0] = v.a1; o[ 1] = v.b1; o[ 2] = v.c1; o[ 3] = v.d1;
o[ 4] = v.a2; o[ 5] = v.b2; o[ 6] = v.c2; o[ 7] = v.d2;
o[ 8] = v.a3; o[ 9] = v.b3; o[10] = v.c3; o[11] = v.d3;
o[12] = v.a4; o[13] = v.b4; o[14] = v.c4; o[15] = v.d4;
}
inline Ref<Accessor> ExportData(Asset& a, std::string& meshName, Ref<Buffer>& buffer,
unsigned int count, void* data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, bool isIndices = false)
{
//
// write the body data
//
if (!count || !data) return Ref<Accessor>();
if (!mBodyData.empty()) {
std::size_t bodyOffset = sizeof(GLB_Header) + sceneLength;
bodyOffset = (bodyOffset + 3) & ~3; // Round up to next multiple of 4
unsigned int numCompsIn = AttribType::GetNumComponents(typeIn);
unsigned int numCompsOut = AttribType::GetNumComponents(typeOut);
unsigned int bytesPerComp = ComponentTypeSize(compType);
outfile->Seek(bodyOffset, aiOrigin_SET);
size_t offset = buffer->byteLength;
size_t length = count * numCompsOut * bytesPerComp;
buffer->Grow(length);
if (outfile->Write(&mBodyData[0], mBodyData.size(), 1) != 1) {
throw DeadlyExportError("Failed to write body data!");
// bufferView
Ref<BufferView> bv = a.bufferViews.Create(a.FindUniqueID(meshName, "view"));
bv->buffer = buffer;
bv->byteOffset = 0;
bv->byteLength = length; //! The target that the WebGL buffer should be bound to.
bv->target = isIndices ? BufferViewTarget_ELEMENT_ARRAY_BUFFER : BufferViewTarget_ARRAY_BUFFER;
// accessor
Ref<Accessor> acc = a.accessors.Create(a.FindUniqueID(meshName, "accessor"));
acc->bufferView = bv;
acc->byteOffset = offset;
acc->byteStride = 0;
acc->componentType = compType;
acc->count = count;
acc->type = typeOut;
// copy the data
acc->WriteData(count, data, numCompsIn*bytesPerComp);
return acc;
}
namespace {
void GetMatScalar(const aiMaterial* mat, float& val, const char* propName, int type, int idx) {
if (mat->Get(propName, type, idx, val) == AI_SUCCESS) {}
}
}
void glTFExporter::GetMatColorOrTex(const aiMaterial* mat, glTF::TexProperty& prop, const char* propName, int type, int idx, aiTextureType tt)
{
aiString tex;
aiColor4D col;
if (mat->GetTextureCount(tt) > 0) {
if (mat->Get(AI_MATKEY_TEXTURE(tt, 0), tex) == AI_SUCCESS) {
std::string path = tex.C_Str();
if (path.size() > 0) {
if (path[0] != '*') {
std::map<std::string, size_t>::iterator it = mTexturesByPath.find(path);
if (it != mTexturesByPath.end()) {
prop.texture = mAsset->textures.Get(it->second);
}
}
if (!prop.texture) {
std::string texId = mAsset->FindUniqueID("", "texture");
prop.texture = mAsset->textures.Create(texId);
mTexturesByPath[path] = prop.texture.GetIndex();
std::string imgId = mAsset->FindUniqueID("", "image");
prop.texture->source = mAsset->images.Create(imgId);
if (path[0] == '*') { // embedded
aiTexture* tex = mScene->mTextures[atoi(&path[1])];
uint8_t* data = reinterpret_cast<uint8_t*>(tex->pcData);
prop.texture->source->SetData(data, tex->mWidth, *mAsset);
if (tex->achFormatHint[0]) {
std::string mimeType = "image/";
mimeType += (memcmp(tex->achFormatHint, "jpg", 3) == 0) ? "jpeg" : tex->achFormatHint;
prop.texture->source->mimeType = mimeType;
}
}
else {
prop.texture->source->uri = path;
}
}
}
}
}
//
// write the header
//
outfile->Seek(0, aiOrigin_SET);
GLB_Header header;
memcpy(header.magic, AI_GLB_MAGIC_NUMBER, sizeof(header.magic));
header.version = 1;
AI_SWAP4(header.version);
header.length = sizeof(header) + sceneLength + mBodyData.size();
AI_SWAP4(header.length);
header.sceneLength = sceneLength;
AI_SWAP4(header.sceneLength);
header.sceneFormat = SceneFormat_JSON;
AI_SWAP4(header.sceneFormat);
if (outfile->Write(&header, sizeof(header), 1) != 1) {
throw DeadlyExportError("Failed to write the header!");
if (mat->Get(propName, type, idx, col) == AI_SUCCESS) {
prop.color[0] = col.r; prop.color[1] = col.g; prop.color[2] = col.b; prop.color[3] = col.a;
}
}
void glTFExporter::ExportMaterials()
{
aiString aiName;
for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
const aiMaterial* mat = mScene->mMaterials[i];
std::string name;
if (mat->Get(AI_MATKEY_NAME, aiName) == AI_SUCCESS) {
name = aiName.C_Str();
}
name = mAsset->FindUniqueID(name, "material");
Ref<Material> m = mAsset->materials.Create(name);
GetMatColorOrTex(mat, m->ambient, AI_MATKEY_COLOR_AMBIENT, aiTextureType_AMBIENT);
GetMatColorOrTex(mat, m->diffuse, AI_MATKEY_COLOR_DIFFUSE, aiTextureType_DIFFUSE);
GetMatColorOrTex(mat, m->specular, AI_MATKEY_COLOR_SPECULAR, aiTextureType_SPECULAR);
GetMatColorOrTex(mat, m->emission, AI_MATKEY_COLOR_EMISSIVE, aiTextureType_EMISSIVE);
GetMatScalar(mat, m->shininess, AI_MATKEY_SHININESS);
}
}
void glTFExporter::ExportMeshes()
{
for (unsigned int i = 0; i < mScene->mNumMeshes; ++i) {
const aiMesh* aim = mScene->mMeshes[i];
std::string meshId = mAsset->FindUniqueID(aim->mName.C_Str(), "mesh");
Ref<Mesh> m = mAsset->meshes.Create(meshId);
m->primitives.resize(1);
Mesh::Primitive& p = m->primitives.back();
p.material = mAsset->materials.Get(aim->mMaterialIndex);
std::string bufferId = mAsset->FindUniqueID(meshId, "buffer");
Ref<Buffer> b = mAsset->GetBodyBuffer();
if (!b) {
b = mAsset->buffers.Create(bufferId);
}
Ref<Accessor> v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
if (v) p.attributes.position.push_back(v);
Ref<Accessor> n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
if (n) p.attributes.normal.push_back(n);
for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
if (aim->mNumUVComponents[i] > 0) {
AttribType::Value type = (aim->mNumUVComponents[i] == 2) ? AttribType::VEC2 : AttribType::VEC3;
Ref<Accessor> tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, true);
if (tc) p.attributes.texcoord.push_back(tc);
}
}
if (aim->mNumFaces > 0) {
unsigned int nIndicesPerFace = aim->mFaces[0].mNumIndices;
std::vector<uint16_t> indices;
indices.resize(aim->mNumFaces * nIndicesPerFace);
for (size_t i = 0; i < aim->mNumFaces; ++i) {
for (size_t j = 0; j < nIndicesPerFace; ++j) {
indices[i*nIndicesPerFace + j] = uint16_t(aim->mFaces[i].mIndices[j]);
}
}
p.indices = ExportData(*mAsset, meshId, b, indices.size(), &indices[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_UNSIGNED_SHORT);
}
switch (aim->mPrimitiveTypes) {
case aiPrimitiveType_POLYGON:
p.mode = PrimitiveMode_TRIANGLES; break; // TODO implement this
case aiPrimitiveType_LINE:
p.mode = PrimitiveMode_LINES; break;
case aiPrimitiveType_POINT:
p.mode = PrimitiveMode_POINTS; break;
default: // aiPrimitiveType_TRIANGLE
p.mode = PrimitiveMode_TRIANGLES;
}
}
}
size_t glTFExporter::ExportNode(const aiNode* n)
{
Ref<Node> node = mAsset->nodes.Create(mAsset->FindUniqueID(n->mName.C_Str(), "node"));
if (!n->mTransformation.IsIdentity()) {
node->matrix.isPresent = true;
CopyValue(n->mTransformation, node->matrix.value);
}
for (unsigned int i = 0; i < n->mNumMeshes; ++i) {
node->meshes.push_back(mAsset->meshes.Get(n->mMeshes[i]));
}
for (unsigned int i = 0; i < n->mNumChildren; ++i) {
size_t idx = ExportNode(n->mChildren[i]);
node->children.push_back(mAsset->nodes.Get(idx));
}
return node.GetIndex();
}
void glTFExporter::ExportScene()
{
const char* sceneName = "defaultScene";
Ref<Scene> scene = mAsset->scenes.Create(sceneName);
// root node will be the first one exported (idx 0)
if (mAsset->nodes.Size() > 0) {
scene->nodes.push_back(mAsset->nodes.Get(size_t(0)));
}
// set as the default scene
mAsset->scene = scene;
}
void glTFExporter::ExportMetadata()
{
glTF::AssetMetadata& asset = mAsset->asset;
asset.version = 1;
char buffer[256];
sprintf(buffer, "Open Asset Import Library (assimp v%d.%d.%d)",
aiGetVersionMajor(), aiGetVersionMinor(), aiGetVersionRevision());
asset.generator = buffer;
}

View File

@ -45,11 +45,24 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define AI_GLTFEXPORTER_H_INC
#include <assimp/types.h>
#include <assimp/material.h>
#include <sstream>
#include <vector>
#include <map>
#include "boost/scoped_ptr.hpp"
struct aiScene;
struct aiNode;
struct aiMaterial;
namespace glTF
{
class Asset;
struct TexProperty;
}
namespace Assimp
{
@ -57,7 +70,6 @@ namespace Assimp
class IOStream;
class ExportProperties;
// ------------------------------------------------------------------------------------------------
/** Helper class to export a given scene to an glTF file. */
// ------------------------------------------------------------------------------------------------
@ -74,12 +86,21 @@ namespace Assimp
IOSystem* mIOSystem;
const aiScene* mScene;
const ExportProperties* mProperties;
bool mIsBinary;
std::map<std::string, size_t> mTexturesByPath;
glTF::Asset* mAsset;
std::vector<unsigned char> mBodyData;
void WriteBinaryData(IOStream* outfile, std::size_t sceneLength);
void GetMatColorOrTex(const aiMaterial* mat, glTF::TexProperty& prop, const char* propName, int type, int idx, aiTextureType tt);
void ExportMetadata();
void ExportMaterials();
void ExportMeshes();
size_t ExportNode(const aiNode* node);
void ExportScene();
};
}

View File

@ -1,122 +0,0 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2015, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
#ifndef AI_GLTFFILEDATA_H_INC
#define AI_GLTFFILEDATA_H_INC
#include <stdint.h>
namespace Assimp {
namespace glTF {
//! Magic number for GLB files
#define AI_GLB_MAGIC_NUMBER "glTF"
#include "./../include/assimp/Compiler/pushpack1.h"
// KHR_binary_glTF (binary .glb file)
// 20-byte header (+ the JSON + a "body" data section)
struct GLB_Header
{
//! Magic number: "glTF"
unsigned char magic[4]; // "glTF"
//! Version number (always 1 as of the last update)
uint32_t version;
//! Total length of the Binary glTF, including header, scene, and body, in bytes
uint32_t length;
//! Length, in bytes, of the glTF scene
uint32_t sceneLength;
//! Specifies the format of the glTF scene (see the SceneFormat enum)
uint32_t sceneFormat;
} PACK_STRUCT;
#include "./../include/assimp/Compiler/poppack1.h"
//! Values for the GLB_Header::sceneFormat field
enum SceneFormat
{
SceneFormat_JSON = 0
};
//! Values for the mesh primitive modes
enum PrimitiveMode
{
PrimitiveMode_POINTS = 0,
PrimitiveMode_LINES = 1,
PrimitiveMode_LINE_LOOP = 2,
PrimitiveMode_LINE_STRIP = 3,
PrimitiveMode_TRIANGLES = 4,
PrimitiveMode_TRIANGLE_STRIP = 5,
PrimitiveMode_TRIANGLE_FAN = 6
};
//! Values for the accessors component type field
enum ComponentType
{
ComponentType_BYTE = 5120,
ComponentType_UNSIGNED_BYTE = 5121,
ComponentType_SHORT = 5122,
ComponentType_UNSIGNED_SHORT = 5123,
ComponentType_FLOAT = 5126
};
//! Will hold the enabled extensions
struct Extensions
{
bool KHR_binary_glTF;
};
} // end namespaces
}
#endif // AI_GLTFFILEDATA_H_INC

File diff suppressed because it is too large Load Diff

View File

@ -41,16 +41,23 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define AI_GLTFIMPORTER_H_INC
#include "BaseImporter.h"
#include "LogAux.h"
#include "DefaultIOSystem.h"
struct aiNode;
namespace glTF
{
class Asset;
}
namespace Assimp {
/**
* Load the glTF format.
* https://github.com/KhronosGroup/glTF/tree/master/specification
*/
class glTFImporter : public BaseImporter, public LogFunctions<glTFImporter> {
class glTFImporter : public BaseImporter{
public:
glTFImporter();
virtual ~glTFImporter();
@ -61,10 +68,20 @@ protected:
virtual void InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler );
private:
void ReadBinaryHeader(IOStream& stream);
std::size_t mSceneLength;
std::size_t mBodyOffset, mBodyLength;
std::vector<unsigned int> meshOffsets;
std::vector<int> embeddedTexIdxs;
aiScene* mScene;
void ImportEmbeddedTextures(glTF::Asset& a);
void ImportMaterials(glTF::Asset& a);
void ImportMeshes(glTF::Asset& a);
void ImportCameras(glTF::Asset& a);
void ImportLights(glTF::Asset& a);
void ImportNodes(glTF::Asset& a);
};
} // Namespace assimp

View File

@ -1,164 +0,0 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2015, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
#include "glTFUtil.h"
using namespace Assimp;
using namespace Assimp::glTF;
bool Assimp::glTF::IsDataURI(const char* uri)
{
return strncmp(uri, "data:", 5) == 0;
}
static const uint8_t tableDecodeBase64[128] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00
};
static inline char EncodeCharBase64(uint8_t b)
{
return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="[b];
}
static inline uint8_t DecodeCharBase64(char c)
{
return tableDecodeBase64[c]; // TODO faster with lookup table or ifs?
/*if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
return c == '+' ? 62 : 63;*/
}
std::size_t Assimp::glTF::DecodeBase64(
const char* in, uint8_t*& out)
{
return DecodeBase64(in, strlen(in), out);
}
std::size_t Assimp::glTF::DecodeBase64(
const char* in, std::size_t inLength, uint8_t*& out)
{
ai_assert(dataLen % 4 == 0);
if (inLength < 4) {
out = 0;
return 0;
}
int nEquals = int(in[inLength - 1] == '=') +
int(in[inLength - 2] == '=');
std::size_t outLength = (inLength * 3) / 4 - nEquals;
out = new uint8_t[outLength];
memset(out, 0, outLength);
std::size_t j = 0;
for (std::size_t i = 0; i < inLength; i += 4) {
uint8_t b0 = DecodeCharBase64(in[i]);
uint8_t b1 = DecodeCharBase64(in[i + 1]);
uint8_t b2 = DecodeCharBase64(in[i + 2]);
uint8_t b3 = DecodeCharBase64(in[i + 3]);
out[j++] = (uint8_t)((b0 << 2) | (b1 >> 4));
out[j++] = (uint8_t)((b1 << 4) | (b2 >> 2));
out[j++] = (uint8_t)((b2 << 6) | b3);
}
return outLength;
}
void Assimp::glTF::EncodeBase64(
const uint8_t* in, std::size_t inLength,
std::string& out)
{
std::size_t outLength = ((inLength + 2) / 3) * 4;
out.resize(outLength);
std::size_t j = 0;
for (std::size_t i = 0; i < inLength; i += 3) {
uint8_t b = (in[i] & 0xFC) >> 2;
out[j++] = EncodeCharBase64(b);
b = (in[i] & 0x03) << 4;
if (i + 1 < inLength) {
b |= (in[i + 1] & 0xF0) >> 4;
out[j++] = EncodeCharBase64(b);
b = (in[i + 1] & 0x0F) << 2;
if (i + 2 < inLength) {
b |= (in[i + 2] & 0xC0) >> 6;
out[j++] = EncodeCharBase64(b);
b = in[i + 2] & 0x3F;
out[j++] = EncodeCharBase64(b);
}
else {
out[j++] = EncodeCharBase64(b);
out[j++] = '=';
}
}
else {
out[j++] = EncodeCharBase64(b);
out[j++] = '=';
out[j++] = '=';
}
}
}