Skip to content

Commit

Permalink
Merge branch 'master' into msvc-clang-unreachable-code
Browse files Browse the repository at this point in the history
  • Loading branch information
kimkulling authored Apr 4, 2023
2 parents 20b2f85 + 17d8e99 commit 1733d9f
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 88 deletions.
32 changes: 22 additions & 10 deletions code/AssetLib/FBX/FBXConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void FBXConverter::ConvertRootNode() {
mSceneOut->mRootNode->mName.Set(unique_name);

// root has ID 0
ConvertNodes(0L, mSceneOut->mRootNode, mSceneOut->mRootNode);
ConvertNodes(0L, mSceneOut->mRootNode, mSceneOut->mRootNode, aiMatrix4x4());
}

static std::string getAncestorBaseName(const aiNode *node) {
Expand Down Expand Up @@ -196,7 +196,7 @@ struct FBXConverter::PotentialNode {
/// todo: get bone from stack
/// todo: make map of aiBone* to aiNode*
/// then update convert clusters to the new format
void FBXConverter::ConvertNodes(uint64_t id, aiNode *parent, aiNode *root_node) {
void FBXConverter::ConvertNodes(uint64_t id, aiNode *parent, aiNode *root_node, const aiMatrix4x4 &globalTransform) {
const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(id, "Model");

std::vector<PotentialNode> nodes;
Expand Down Expand Up @@ -290,14 +290,15 @@ void FBXConverter::ConvertNodes(uint64_t id, aiNode *parent, aiNode *root_node)
}

// recursion call - child nodes
ConvertNodes(model->ID(), last_parent, root_node);
aiMatrix4x4 newGlobalMatrix = globalTransform * nodes_chain.front().mNode->mTransformation;
ConvertNodes(model->ID(), last_parent, root_node, newGlobalMatrix);

if (doc.Settings().readLights) {
ConvertLights(*model, node_name);
}

if (doc.Settings().readCameras) {
ConvertCameras(*model, node_name);
ConvertCameras(*model, node_name, newGlobalMatrix);
}

nodes.push_back(std::move(nodes_chain.front()));
Expand Down Expand Up @@ -327,12 +328,14 @@ void FBXConverter::ConvertLights(const Model &model, const std::string &orig_nam
}
}

void FBXConverter::ConvertCameras(const Model &model, const std::string &orig_name) {
void FBXConverter::ConvertCameras(const Model &model,
const std::string &orig_name,
const aiMatrix4x4 &transform) {
const std::vector<const NodeAttribute *> &node_attrs = model.GetAttributes();
for (const NodeAttribute *attr : node_attrs) {
const Camera *const cam = dynamic_cast<const Camera *>(attr);
if (cam) {
ConvertCamera(*cam, orig_name);
ConvertCamera(*cam, orig_name, transform);
}
}
}
Expand Down Expand Up @@ -413,17 +416,26 @@ void FBXConverter::ConvertLight(const Light &light, const std::string &orig_name
}
}

void FBXConverter::ConvertCamera(const Camera &cam, const std::string &orig_name) {
void FBXConverter::ConvertCamera(const Camera &cam,
const std::string &orig_name,
aiMatrix4x4 transform) {
cameras.push_back(new aiCamera());
aiCamera *const out_camera = cameras.back();

out_camera->mName.Set(orig_name);

out_camera->mAspect = cam.AspectWidth() / cam.AspectHeight();

aiVector3D pos = cam.Position();
out_camera->mLookAt = cam.InterestPosition();
out_camera->mUp = pos + cam.UpVector();
transform.Inverse();
pos *= transform;
out_camera->mLookAt *= transform;
out_camera->mUp *= transform;
out_camera->mLookAt -= pos;
out_camera->mUp -= pos;
out_camera->mPosition = aiVector3D(0.0f);
out_camera->mLookAt = aiVector3D(1.0f, 0.0f, 0.0f);
out_camera->mUp = aiVector3D(0.0f, 1.0f, 0.0f);

out_camera->mHorizontalFOV = AI_DEG_TO_RAD(cam.FieldOfView());

Expand Down Expand Up @@ -640,7 +652,7 @@ void FBXConverter::GetRotationMatrix(Model::RotOrder mode, const aiVector3D &rot
bool FBXConverter::NeedsComplexTransformationChain(const Model &model) {
const PropertyTable &props = model.Props();

const auto zero_epsilon = ai_epsilon;
const auto zero_epsilon = Math::getEpsilon<ai_real>();
const aiVector3D all_ones(1.0f, 1.0f, 1.0f);
for (size_t i = 0; i < TransformationComp_MAXIMUM; ++i) {
const TransformationComp comp = static_cast<TransformationComp>(i);
Expand Down
9 changes: 6 additions & 3 deletions code/AssetLib/FBX/FBXConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,22 @@ class FBXConverter {

// ------------------------------------------------------------------------------------------------
// collect and assign child nodes
void ConvertNodes(uint64_t id, aiNode *parent, aiNode *root_node);
void ConvertNodes(uint64_t id, aiNode *parent, aiNode *root_node,
const aiMatrix4x4 &globalTransform);

// ------------------------------------------------------------------------------------------------
void ConvertLights(const Model& model, const std::string &orig_name );

// ------------------------------------------------------------------------------------------------
void ConvertCameras(const Model& model, const std::string &orig_name );
void ConvertCameras(const Model& model, const std::string &orig_name,
const aiMatrix4x4 &transform);

// ------------------------------------------------------------------------------------------------
void ConvertLight( const Light& light, const std::string &orig_name );

// ------------------------------------------------------------------------------------------------
void ConvertCamera( const Camera& cam, const std::string &orig_name );
void ConvertCamera(const Camera& cam, const std::string &orig_name,
aiMatrix4x4 transform);

// ------------------------------------------------------------------------------------------------
void GetUniqueName( const std::string &name, std::string& uniqueName );
Expand Down
27 changes: 14 additions & 13 deletions code/AssetLib/glTF/glTFAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,21 +513,22 @@ struct Camera : public Object {
};

Type type;
struct Perspective {
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)
};

struct Ortographic {
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)
};
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;
struct Perspective perspective;
struct Ortographic ortographic;
};

Camera() = default;
Expand Down
2 changes: 1 addition & 1 deletion code/AssetLib/glTF2/glTF2Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ struct Accessor : public Object {
inline size_t GetMaxByteSize();

template <class T>
void ExtractData(T *&outData);
size_t ExtractData(T *&outData, const std::vector<unsigned int> *remappingIndices = nullptr);

void WriteData(size_t count, const void *src_buffer, size_t src_stride);
void WriteSparseValues(size_t count, const void *src_data, size_t src_dataStride);
Expand Down
36 changes: 25 additions & 11 deletions code/AssetLib/glTF2/glTF2Asset.inl
Original file line number Diff line number Diff line change
Expand Up @@ -962,14 +962,15 @@ inline size_t Accessor::GetMaxByteSize() {
}

template <class T>
void Accessor::ExtractData(T *&outData) {
size_t Accessor::ExtractData(T *&outData, const std::vector<unsigned int> *remappingIndices) {
uint8_t *data = GetPointer();
if (!data) {
throw DeadlyImportError("GLTF2: data is null when extracting data from ", getContextForErrorMessages(id, name));
}

const size_t usedCount = (remappingIndices != nullptr) ? remappingIndices->size() : count;
const size_t elemSize = GetElementSize();
const size_t totalSize = elemSize * count;
const size_t totalSize = elemSize * usedCount;

const size_t stride = GetStride();

Expand All @@ -980,18 +981,31 @@ void Accessor::ExtractData(T *&outData) {
}

const size_t maxSize = GetMaxByteSize();
if (count * stride > maxSize) {
throw DeadlyImportError("GLTF: count*stride ", (count * stride), " > maxSize ", maxSize, " in ", getContextForErrorMessages(id, name));
}

outData = new T[count];
if (stride == elemSize && targetElemSize == elemSize) {
memcpy(outData, data, totalSize);
} else {
for (size_t i = 0; i < count; ++i) {
memcpy(outData + i, data + i * stride, elemSize);
outData = new T[usedCount];

if (remappingIndices != nullptr) {
const unsigned int maxIndex = static_cast<unsigned int>(maxSize / stride - 1);
for (size_t i = 0; i < usedCount; ++i) {
size_t srcIdx = (*remappingIndices)[i];
if (srcIdx > maxIndex) {
throw DeadlyImportError("GLTF: index*stride ", (srcIdx * stride), " > maxSize ", maxSize, " in ", getContextForErrorMessages(id, name));
}
memcpy(outData + i, data + srcIdx * stride, elemSize);
}
} else { // non-indexed cases
if (usedCount * stride > maxSize) {
throw DeadlyImportError("GLTF: count*stride ", (usedCount * stride), " > maxSize ", maxSize, " in ", getContextForErrorMessages(id, name));
}
if (stride == elemSize && targetElemSize == elemSize) {
memcpy(outData, data, totalSize);
} else {
for (size_t i = 0; i < usedCount; ++i) {
memcpy(outData + i, data + i * stride, elemSize);
}
}
}
return usedCount;
}

inline void Accessor::WriteData(size_t _count, const void *src_buffer, size_t src_stride) {
Expand Down
Loading

0 comments on commit 1733d9f

Please sign in to comment.