1499 lines · 61.4 KB · C++
RawDownload
////  FBXReader.cpp//  interface////  Created by Andrzej Kapolka on 9/18/13.//  Copyright (c) 2013 High Fidelity, Inc. All rights reserved.// #include <QBuffer>#include <QDataStream>#include <QIODevice>#include <QStringList>#include <QTextStream>#include <QtDebug>#include <QtEndian> #include <glm/gtc/quaternion.hpp>#include <glm/gtx/quaternion.hpp>#include <glm/gtx/transform.hpp> #include <OctalCode.h> #include <VoxelTree.h> #include "FBXReader.h"#include "Util.h" using namespace std; template<class T> QVariant readBinaryArray(QDataStream& in) {    quint32 arrayLength;    quint32 encoding;    quint32 compressedLength;        in >> arrayLength;    in >> encoding;    in >> compressedLength;        QVector<T> values;    const int DEFLATE_ENCODING = 1;    if (encoding == DEFLATE_ENCODING) {        // preface encoded data with uncompressed length        QByteArray compressed(sizeof(quint32) + compressedLength, 0);        *((quint32*)compressed.data()) = qToBigEndian<quint32>(arrayLength * sizeof(T));        in.readRawData(compressed.data() + sizeof(quint32), compressedLength);        QByteArray uncompressed = qUncompress(compressed);        QDataStream uncompressedIn(uncompressed);        uncompressedIn.setByteOrder(QDataStream::LittleEndian);        uncompressedIn.setVersion(QDataStream::Qt_4_5); // for single/double precision switch        for (int i = 0; i < arrayLength; i++) {            T value;            uncompressedIn >> value;            values.append(value);        }    } else {        for (int i = 0; i < arrayLength; i++) {            T value;            in >> value;            values.append(value);        }    }    return QVariant::fromValue(values);} QVariant parseBinaryFBXProperty(QDataStream& in) {    char ch;    in.device()->getChar(&ch);    switch (ch) {        case 'Y': {            qint16 value;            in >> value;            return QVariant::fromValue(value);           }        case 'C': {            bool value;            in >> value;            return QVariant::fromValue(value);        }        case 'I': {            qint32 value;            in >> value;            return QVariant::fromValue(value);        }        case 'F': {            float value;            in >> value;            return QVariant::fromValue(value);        }        case 'D': {            double value;            in >> value;            return QVariant::fromValue(value);        }        case 'L': {            qint64 value;            in >> value;            return QVariant::fromValue(value);        }        case 'f': {            return readBinaryArray<float>(in);        }        case 'd': {            return readBinaryArray<double>(in);        }        case 'l': {            return readBinaryArray<qint64>(in);        }        case 'i': {            return readBinaryArray<qint32>(in);        }        case 'b': {            return readBinaryArray<bool>(in);        }        case 'S':        case 'R': {            quint32 length;            in >> length;            return QVariant::fromValue(in.device()->read(length));        }        default:            throw QString("Unknown property type: ") + ch;    }} FBXNode parseBinaryFBXNode(QDataStream& in) {    quint32 endOffset;    quint32 propertyCount;    quint32 propertyListLength;    quint8 nameLength;        in >> endOffset;    in >> propertyCount;    in >> propertyListLength;    in >> nameLength;        FBXNode node;    const int MIN_VALID_OFFSET = 40;    if (endOffset < MIN_VALID_OFFSET || nameLength == 0) {        // use a null name to indicate a null node        return node;    }    node.name = in.device()->read(nameLength);        for (int i = 0; i < propertyCount; i++) {        node.properties.append(parseBinaryFBXProperty(in));        }        while (endOffset > in.device()->pos()) {        FBXNode child = parseBinaryFBXNode(in);        if (child.name.isNull()) {            return node;                    } else {            node.children.append(child);        }    }        return node;} class Tokenizer {public:        Tokenizer(QIODevice* device) : _device(device), _pushedBackToken(-1) { }        enum SpecialToken { DATUM_TOKEN = 0x100 };        int nextToken();    const QByteArray& getDatum() const { return _datum; }        void pushBackToken(int token) { _pushedBackToken = token; }    private:        QIODevice* _device;    QByteArray _datum;    int _pushedBackToken;}; int Tokenizer::nextToken() {    if (_pushedBackToken != -1) {        int token = _pushedBackToken;        _pushedBackToken = -1;        return token;    }     char ch;    while (_device->getChar(&ch)) {        if (QChar(ch).isSpace()) {            continue; // skip whitespace        }        switch (ch) {            case ';':                _device->readLine(); // skip the comment                break;                            case ':':            case '{':            case '}':            case ',':                return ch; // special punctuation                        case '\"':                _datum = "";                while (_device->getChar(&ch)) {                    if (ch == '\"') { // end on closing quote                        break;                    }                    if (ch == '\\') { // handle escaped quotes                        if (_device->getChar(&ch) && ch != '\"') {                            _datum.append('\\');                        }                    }                    _datum.append(ch);                }                return DATUM_TOKEN;                               default:                _datum = "";                _datum.append(ch);                while (_device->getChar(&ch)) {                    if (QChar(ch).isSpace() || ch == ';' || ch == ':' || ch == '{' || ch == '}' || ch == ',' || ch == '\"') {                        _device->ungetChar(ch); // read until we encounter a special character, then replace it                        break;                    }                    _datum.append(ch);                }                return DATUM_TOKEN;        }    }    return -1;} FBXNode parseTextFBXNode(Tokenizer& tokenizer) {    FBXNode node;        if (tokenizer.nextToken() != Tokenizer::DATUM_TOKEN) {        return node;    }    node.name = tokenizer.getDatum();        if (tokenizer.nextToken() != ':') {        return node;    }        int token;    bool expectingDatum = true;    while ((token = tokenizer.nextToken()) != -1) {        if (token == '{') {            for (FBXNode child = parseTextFBXNode(tokenizer); !child.name.isNull(); child = parseTextFBXNode(tokenizer)) {                node.children.append(child);            }            return node;        }        if (token == ',') {            expectingDatum = true;                    } else if (token == Tokenizer::DATUM_TOKEN && expectingDatum) {            node.properties.append(tokenizer.getDatum());            expectingDatum = false;                } else {            tokenizer.pushBackToken(token);            return node;        }    }        return node;} FBXNode parseFBX(QIODevice* device) {    // verify the prolog    const QByteArray BINARY_PROLOG = "Kaydara FBX Binary  ";    if (device->peek(BINARY_PROLOG.size()) != BINARY_PROLOG) {        // parse as a text file        FBXNode top;        Tokenizer tokenizer(device);        while (device->bytesAvailable()) {                FBXNode next = parseTextFBXNode(tokenizer);            if (next.name.isNull()) {                return top;                            } else {                top.children.append(next);            }        }        return top;    }    QDataStream in(device);    in.setByteOrder(QDataStream::LittleEndian);    in.setVersion(QDataStream::Qt_4_5); // for single/double precision switch        // see http://code.blender.org/index.php/2013/08/fbx-binary-file-format-specification/ for an explanation    // of the FBX binary format        // skip the rest of the header    const int HEADER_SIZE = 27;    in.skipRawData(HEADER_SIZE);        // parse the top-level node    FBXNode top;    while (device->bytesAvailable()) {        FBXNode next = parseBinaryFBXNode(in);        if (next.name.isNull()) {            return top;                    } else {            top.children.append(next);        }    }        return top;} QVariantHash parseMapping(QIODevice* device) {    QVariantHash properties;        QByteArray line;    while (!(line = device->readLine()).isEmpty()) {        if ((line = line.trimmed()).startsWith('#')) {            continue; // comment        }        QList<QByteArray> sections = line.split('=');        if (sections.size() < 2) {            continue;        }        QByteArray name = sections.at(0).trimmed();        if (sections.size() == 2) {            properties.insertMulti(name, sections.at(1).trimmed());                } else if (sections.size() == 3) {            QVariantHash heading = properties.value(name).toHash();            heading.insertMulti(sections.at(1).trimmed(), sections.at(2).trimmed());            properties.insert(name, heading);                    } else if (sections.size() >= 4) {            QVariantHash heading = properties.value(name).toHash();            QVariantList contents;            for (int i = 2; i < sections.size(); i++) {                contents.append(sections.at(i).trimmed());            }            heading.insertMulti(sections.at(1).trimmed(), contents);            properties.insert(name, heading);        }    }        return properties;} QVector<glm::vec3> createVec3Vector(const QVector<double>& doubleVector) {    QVector<glm::vec3> values;    for (const double* it = doubleVector.constData(), *end = it + doubleVector.size(); it != end; ) {        float x = *it++;        float y = *it++;        float z = *it++;        values.append(glm::vec3(x, y, z));    }    return values;} QVector<glm::vec2> createVec2Vector(const QVector<double>& doubleVector) {    QVector<glm::vec2> values;    for (const double* it = doubleVector.constData(), *end = it + doubleVector.size(); it != end; ) {        float s = *it++;        float t = *it++;        values.append(glm::vec2(s, -t));    }    return values;} glm::mat4 createMat4(const QVector<double>& doubleVector) {    return glm::mat4(doubleVector.at(0), doubleVector.at(1), doubleVector.at(2), doubleVector.at(3),        doubleVector.at(4), doubleVector.at(5), doubleVector.at(6), doubleVector.at(7),        doubleVector.at(8), doubleVector.at(9), doubleVector.at(10), doubleVector.at(11),        doubleVector.at(12), doubleVector.at(13), doubleVector.at(14), doubleVector.at(15));} QVector<int> getIntVector(const QVariantList& properties, int index) {    QVector<int> vector = properties.at(index).value<QVector<int> >();    if (!vector.isEmpty()) {        return vector;    }    for (; index < properties.size(); index++) {        vector.append(properties.at(index).toInt());    }    return vector;} QVector<double> getDoubleVector(const QVariantList& properties, int index) {    QVector<double> vector = properties.at(index).value<QVector<double> >();    if (!vector.isEmpty()) {        return vector;    }    for (; index < properties.size(); index++) {        vector.append(properties.at(index).toDouble());    }    return vector;} glm::vec3 getVec3(const QVariantList& properties, int index) {    return glm::vec3(properties.at(index).value<double>(), properties.at(index + 1).value<double>(),        properties.at(index + 2).value<double>());} glm::vec3 parseVec3(const QString& string) {    QStringList elements = string.split(',');    if (elements.isEmpty()) {        return glm::vec3();    }    glm::vec3 value;    for (int i = 0; i < 3; i++) {        // duplicate last value if there aren't three elements        value[i] = elements.at(min(i, elements.size() - 1)).trimmed().toFloat();    }    return value;} QString processID(const QString& id) {    // Blender (at least) prepends a type to the ID, so strip it out    int index = id.indexOf("::");    return (index == -1) ? id : id.mid(index + 2);} QString getID(const QVariantList& properties, int index = 0) {    return processID(properties.at(index).toString());} const char* FACESHIFT_BLENDSHAPES[] = {    "EyeBlink_L",    "EyeBlink_R",    "EyeSquint_L",    "EyeSquint_R",    "EyeDown_L",    "EyeDown_R",    "EyeIn_L",    "EyeIn_R",    "EyeOpen_L",    "EyeOpen_R",    "EyeOut_L",    "EyeOut_R",    "EyeUp_L",    "EyeUp_R",    "BrowsD_L",    "BrowsD_R",    "BrowsU_C",    "BrowsU_L",    "BrowsU_R",    "JawFwd",    "JawLeft",    "JawOpen",    "JawChew",    "JawRight",    "MouthLeft",    "MouthRight",    "MouthFrown_L",    "MouthFrown_R",    "MouthSmile_L",    "MouthSmile_R",    "MouthDimple_L",    "MouthDimple_R",    "LipsStretch_L",    "LipsStretch_R",    "LipsUpperClose",    "LipsLowerClose",    "LipsUpperUp",    "LipsLowerDown",    "LipsUpperOpen",    "LipsLowerOpen",    "LipsFunnel",    "LipsPucker",    "ChinLowerRaise",    "ChinUpperRaise",    "Sneer",    "Puff",    "CheekSquint_L",    "CheekSquint_R",    ""}; class FBXModel {public:    QString name;        int parentIndex;        glm::mat4 preTransform;    glm::quat preRotation;    glm::quat rotation;    glm::quat postRotation;    glm::mat4 postTransform;        glm::vec3 rotationMin;    glm::vec3 rotationMax;}; glm::mat4 getGlobalTransform(const QMultiHash<QString, QString>& parentMap,        const QHash<QString, FBXModel>& models, QString nodeID) {    glm::mat4 globalTransform;    while (!nodeID.isNull()) {        const FBXModel& model = models.value(nodeID);

Lines 1–500 of 1499.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t