内容简介:Cocos2d-x 3.x 图形学渲染系列三十
笔者介绍: 姜雪伟 , 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系统类似。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- CubeEngine v0.3B 更新,图形渲染引擎
- TensorFlow也可以做图形渲染了:当神经网络遇上计算机图形学
- Basemark推出Rocksolid图形渲染解决方案
- Ogre 1.12.3 发布,三维图形渲染引擎
- Rust 移动端跨平台复杂图形渲染项目开发系列总结(目录)
- Cocos2d-x 3.x 图形学渲染系列总结
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。