笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。
CSDN视频网址:http://edu.csdn.net/lecturer/144
继续系列博客的分享,接下来分享的是关于骨骼权重的代码,这也是继 Cocos2d-x图形学渲染系列二十九,下面介绍关于骨骼权重的处理,类定义代码如下:
#ifdef _MSC_VER #pragma once #endif //_MSC_VER #ifndef FBXCONV_READERS_UTIL_H #define FBXCONV_READERS_UTIL_H #include <vector> #include <algorithm> #include <assert.h> namespace fbxconv { namespace readers { // 索引加权重对于顶点混合计算 struct BlendWeight { float weight; int index; BlendWeight() : weight(0.f), index(-1) {} BlendWeight(float w, int i) : weight(w), index(i) {} inlinebooloperator<(constBlendWeight& rhs) const { returnweight< rhs.weight; } inlinebooloperator>(constBlendWeight& rhs) const { returnweight> rhs.weight; } inlinebooloperator==(constBlendWeight& rhs) const { returnweight == rhs.weight; } }; // 索引组对于顶点混合 struct BlendBones { int *bones; unsignedint capacity; BlendBones(constunsignedint&capacity = 2) : capacity(capacity) { bones = newint[capacity]; memset(bones, -1, capacity * sizeof(int)); } BlendBones(constBlendBones&rhs) : capacity(rhs.capacity) { bones = newint[capacity]; memcpy(bones, rhs.bones, capacity * sizeof(int)); } ~BlendBones() { deletebones; } inlinebool has(constint&bone) const { for (unsignedint i = 0; i <capacity; i++) if (bones[i] == bone) returntrue; returnfalse; } inlineunsignedint size() const { for (unsignedint i = 0; i <capacity; i++) if (bones[i] <0) return i; returncapacity; } inlineunsignedint available() const { returncapacity - size(); } inlineint cost(conststd::vector<std::vector<BlendWeight>*>&rhs) const { int result = 0; for (std::vector<std::vector<BlendWeight>*>::const_iterator itr = rhs.begin(); itr != rhs.end(); ++itr) for (std::vector<BlendWeight>::const_iterator jtr = (*itr)->begin(); jtr != (*itr)->end(); ++jtr) if (!has((*jtr).index)) result++; return (result > (int)available()) ? -1 : result; } inlinevoid sort() { std::sort(bones, bones + size()); } inlineint idx(constint&bone) const { for (unsignedint i = 0; i <capacity; i++) if (bones[i] == bone) return i; return -1; } inlineint add(constint&v) { for (unsignedint i = 0; i <capacity; i++) { if (bones[i] == v) return i; elseif (bones[i] <0) { bones[i] = v; return i; } } return -1; } inlinebool add(conststd::vector<std::vector<BlendWeight>*>&rhs) { for (std::vector<std::vector<BlendWeight>*>::const_iterator itr = rhs.begin(); itr != rhs.end(); ++itr) for (std::vector<BlendWeight>::const_iterator jtr = (*itr)->begin(); jtr != (*itr)->end(); ++jtr) if (add((*jtr).index)<0) returnfalse; returntrue; } inlineintoperator[](constunsignedint idx) const { return idx <capacity ? bones[idx] : -1; } inlineBlendBones&operator=(constBlendBones&rhs) { if (&rhs == this) return *this; if (capacity != rhs.capacity) { delete[] bones; bones = newint[capacity = rhs.capacity]; } memcpy(bones, rhs.bones, capacity * sizeof(int)); return *this; } }; // 索引集合对于顶点混合 struct BlendBonesCollection { std::vector<BlendBones> bones; unsignedint bonesCapacity; BlendBonesCollection(constunsignedint&bonesCapacity) : bonesCapacity(bonesCapacity) { } BlendBonesCollection(constBlendBonesCollection&rhs) : bonesCapacity(rhs.bonesCapacity) { bones.insert(bones.begin(), rhs.bones.begin(), rhs.bones.end()); } inlineBlendBonesCollection&operator=(constBlendBonesCollection&rhs) { if (&rhs == this) return (*this); bones = rhs.bones; bonesCapacity = rhs.bonesCapacity; return (*this); } inlineunsignedint size() const { return (unsignedint)bones.size(); } inlineBlendBones&operator[](constunsignedint&idx) { returnbones[idx]; } inlineunsignedint add(conststd::vector<std::vector<BlendWeight>*>&rhs) { int cost = (int)bonesCapacity, idx = -1, n = bones.size(); for (int i = 0; i < n; i++) { constint c = bones[i].cost(rhs); if (c >= 0&& c < cost) { cost = c; idx = i; } } if (idx <0) { bones.push_back(BlendBones(bonesCapacity)); idx = n; } returnbones[idx].add(rhs) ? idx : -1; } inlinevoid sortBones() { for (std::vector<BlendBones>::iterator itr = bones.begin(); itr != bones.end(); ++itr) (*itr).sort(); } }; // 骨骼动画信息 struct AnimInfo { float start; float stop; float framerate; bool translate; bool rotate; bool scale; AnimInfo() : start(FLT_MAX), stop(-1.f), framerate(0.f), translate(false), rotate(false), scale(false) {} inlineAnimInfo&operator+=(constAnimInfo& rhs) { start = std::min(rhs.start, start); stop = std::max(rhs.stop, stop); framerate = std::max(rhs.framerate, framerate); translate = translate || rhs.translate; rotate = rotate || rhs.rotate; scale = scale || rhs.scale; return *this; } }; } } #endif
至此整个加密文件核心就给读者介绍完了,下图是整个工程的示意图如下:
下个系列告诉读者如何实现文件加密的具体实现,首先需要将上面的工程编译成可执行文件,本书实现的案例是基于mac系统的,windows系统类似。
作者:jxw167 发表于2017/2/3 10:06:30 原文链接
阅读:101 评论:0 查看评论