当前位置: 首页 > news >正文

【OSG学习笔记】Day 16: 骨骼动画与蒙皮(osgAnimation)

在这里插入图片描述

骨骼动画基础

骨骼动画是 3D 计算机图形中常用的技术,它通过以下两个主要组件实现角色动画。

  • 骨骼系统 (Skeleton):由层级结构的骨头组成,类似于人体骨骼
  • 蒙皮 (Mesh Skinning):将模型网格顶点绑定到骨骼上,使骨骼移动时带动网格变形

在 OSG 中,这些功能主要由osgAnimation库提供支持。

当然可以!以下是 Day 16:骨骼动画与蒙皮(osgAnimation) 中的“核心知识点”部分,以标准 Markdown (.md) 格式呈现:


核心知识点

类名作用
osgAnimation::AnimationManagerBase动画管理器接口,定义了动画更新的基本行为
osgAnimation::BasicAnimationManager基础动画管理器实现类,用于管理和播放多个动画
osgAnimation::RigGeometry蒙皮网格数据结构,支持骨骼影响顶点的变形计算
osgAnimation::AnimationPathCallback动画路径回调,可用于控制骨骼或模型沿特定路径运动
osgDB::readNodeFile()加载模型文件(支持 OSGT 等格式),自动解析动画和骨骼信息

OSGT 格式

格式骨骼支持动画保留OSG 兼容性
.osgt✅ 完整✅ 完整⭐⭐⭐⭐⭐(原生支持,推荐使用)
.dae✅ 完整✅ 完整⭐⭐⭐⭐(需 Collada 插件)
.fbx❌ 不支持❌ 不支持不可直接读取
  • .osgt:OSG 的二进制序列化格式,支持完整的骨骼与动画数据,加载速度快,兼容性最好。
  • .dae(Collada):开放的 XML 格式,广泛用于三维模型交换,支持骨骼和动画,但需要 OSG 的 osgdb_collada 插件。
  • .fbx:Autodesk 的私有格式,功能强大,但 OpenSceneGraph 默认不支持 FBX,需要借助第三方插件(如 OpenSceneGraph-FBX)才能加载。

实战

实战1

通过代码自动生成动画效果。

animation.cpp

#include <osgViewer/Viewer>
#include <osg/Group>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <osgAnimation/BasicAnimationManager>
#include <osgAnimation/UpdateMatrixTransform>
#include <osgAnimation/Animation>
#include <osgAnimation/Channel>
#include <osgGA/TrackballManipulator>
#include <osg/Material>
#include <osgDB/Registry>
#include <osg/Notify>
#include <iostream>// 创建骨骼节点
osg::MatrixTransform* createBone(const std::string& name, float length, const osg::Vec4& color) {osg::ref_ptr<osg::MatrixTransform> bone = new osg::MatrixTransform;bone->setName(name);// 创建骨骼可视化(圆柱体)osg::ref_ptr<osg::Cylinder> cylinder = new osg::Cylinder(osg::Vec3(0, 0, length/2), 0.1f, length);osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(cylinder);// 设置颜色osg::ref_ptr<osg::Material> material = new osg::Material;material->setDiffuse(osg::Material::FRONT, osg::Vec4(color));drawable->getOrCreateStateSet()->setAttributeAndModes(material.get());// 添加关节球osg::ref_ptr<osg::Sphere> jointSphere = new osg::Sphere(osg::Vec3(0, 0, 0), 0.15f);osg::ref_ptr<osg::ShapeDrawable> jointDrawable = new osg::ShapeDrawable(jointSphere);jointDrawable->setColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));osg::ref_ptr<osg::Geode> geode = new osg::Geode;geode->addDrawable(drawable.get());geode->addDrawable(jointDrawable.get());bone->addChild(geode.get());return bone.release();
}// 在main函数中添加临时测试动画
class SimpleRotationCallback : public osg::NodeCallback {
public:virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {static double start = osg::Timer::instance()->time_s();double t = osg::Timer::instance()->time_s() - start;float angle = sin(t) * 1.0f; // 摆动幅度osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(node);if (mt) {mt->setMatrix(osg::Matrix::rotate(angle, osg::Vec3(0,1,0)));}traverse(node, nv);}
};// 创建动画
osgAnimation::Animation* createArmAnimation() {osg::ref_ptr<osgAnimation::Animation> animation = new osgAnimation::Animation;animation->setName("ArmAnimation");// 创建肩关节动画通道osg::ref_ptr<osgAnimation::QuatSphericalLinearChannel> shoulderChannel = new osgAnimation::QuatSphericalLinearChannel;shoulderChannel->setName("rotation");shoulderChannel->setTargetName("Shoulder");osgAnimation::QuatKeyframeContainer* shoulderKeyframes = shoulderChannel->getOrCreateSampler()->getOrCreateKeyframeContainer();shoulderKeyframes->push_back(osgAnimation::QuatKeyframe(0.0, osg::Quat(0, osg::Vec3(0,1,0))));shoulderKeyframes->push_back(osgAnimation::QuatKeyframe(2.0, osg::Quat(osg::PI/4, osg::Vec3(0,1,0))));shoulderKeyframes->push_back(osgAnimation::QuatKeyframe(4.0, osg::Quat(-osg::PI/4, osg::Vec3(0,1,0))));shoulderKeyframes->push_back(osgAnimation::QuatKeyframe(6.0, osg::Quat(0, osg::Vec3(0,1,0))));// 创建肘关节动画通道osg::ref_ptr<osgAnimation::QuatSphericalLinearChannel> elbowChannel = new osgAnimation::QuatSphericalLinearChannel;elbowChannel->setName("rotation");elbowChannel->setTargetName("Elbow");osgAnimation::QuatKeyframeContainer* elbowKeyframes = elbowChannel->getOrCreateSampler()->getOrCreateKeyframeContainer();elbowKeyframes->push_back(osgAnimation::QuatKeyframe(0.0, osg::Quat(0, osg::Vec3(0,1,0))));elbowKeyframes->push_back(osgAnimation::QuatKeyframe(1.0, osg::Quat(osg::PI/2, osg::Vec3(0,1,0))));elbowKeyframes->push_back(osgAnimation::QuatKeyframe(2.0, osg::Quat(0, osg::Vec3(0,1,0))));elbowKeyframes->push_back(osgAnimation::QuatKeyframe(3.0, osg::Quat(osg::PI/2, osg::Vec3(0,1,0))));elbowKeyframes->push_back(osgAnimation::QuatKeyframe(4.0, osg::Quat(0, osg::Vec3(0,1,0))));elbowKeyframes->push_back(osgAnimation::QuatKeyframe(6.0, osg::Quat(0, osg::Vec3(0,1,0))));// 创建腕关节动画通道osg::ref_ptr<osgAnimation::QuatSphericalLinearChannel> wristChannel = new osgAnimation::QuatSphericalLinearChannel;wristChannel->setName("rotation");wristChannel->setTargetName("Wrist");osgAnimation::QuatKeyframeContainer* wristKeyframes = wristChannel->getOrCreateSampler()->getOrCreateKeyframeContainer();wristKeyframes->push_back(osgAnimation::QuatKeyframe(0.0, osg::Quat(0, osg::Vec3(1,0,0))));wristKeyframes->push_back(osgAnimation::QuatKeyframe(1.5, osg::Quat(osg::PI/3, osg::Vec3(1,0,0))));wristKeyframes->push_back(osgAnimation::QuatKeyframe(3.0, osg::Quat(-osg::PI/3, osg::Vec3(1,0,0))));wristKeyframes->push_back(osgAnimation::QuatKeyframe(4.5, osg::Quat(osg::PI/3, osg::Vec3(1,0,0))));wristKeyframes->push_back(osgAnimation::QuatKeyframe(6.0, osg::Quat(0, osg::Vec3(1,0,0))));animation->addChannel(shoulderChannel.get());animation->addChannel(elbowChannel.get());animation->addChannel(wristChannel.get());animation->setPlayMode(osgAnimation::Animation::LOOP);animation->setDuration(6.0);return animation.release();
}// 创建手臂模型
osg::Group* createArm() {osg::ref_ptr<osg::Group> root = new osg::Group;// 创建骨骼层级结构osg::ref_ptr<osg::MatrixTransform> shoulder = createBone("Shoulder", 2.0f, osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));osg::ref_ptr<osg::MatrixTransform> elbow = createBone("Elbow", 1.5f, osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));osg::ref_ptr<osg::MatrixTransform> wrist = createBone("Wrist", 1.0f, osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));// 设置骨骼初始位置shoulder->setMatrix(osg::Matrix::translate(0, 0, 0));elbow->setMatrix(osg::Matrix::translate(0, 0, 2.0f));wrist->setMatrix(osg::Matrix::translate(0, 0, 1.5f));// 构建层级关系shoulder->addChild(elbow);elbow->addChild(wrist);// 添加动画更新回调shoulder->setUpdateCallback(new SimpleRotationCallback);elbow->setUpdateCallback(new osgAnimation::UpdateMatrixTransform("Elbow"));wrist->setUpdateCallback(new osgAnimation::UpdateMatrixTransform("Wrist"));// 创建手爪模型osg::ref_ptr<osg::Geode> handGeode = new osg::Geode;osg::ref_ptr<osg::Box> leftFinger = new osg::Box(osg::Vec3(-0.2f, 0, 0.5f), 0.1f, 0.1f, 1.0f);osg::ref_ptr<osg::Box> rightFinger = new osg::Box(osg::Vec3(0.2f, 0, 0.5f), 0.1f, 0.1f, 1.0f);osg::ref_ptr<osg::ShapeDrawable> leftDrawable = new osg::ShapeDrawable(leftFinger);osg::ref_ptr<osg::ShapeDrawable> rightDrawable = new osg::ShapeDrawable(rightFinger);leftDrawable->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f));rightDrawable->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f));handGeode->addDrawable(leftDrawable);handGeode->addDrawable(rightDrawable);wrist->addChild(handGeode);root->addChild(shoulder);// 创建底座osg::ref_ptr<osg::Geode> baseGeode = new osg::Geode;osg::ref_ptr<osg::Cylinder> baseCylinder = new osg::Cylinder(osg::Vec3(0,0,-0.5f), 1.0f, 0.5f);osg::ref_ptr<osg::ShapeDrawable> baseDrawable = new osg::ShapeDrawable(baseCylinder);baseDrawable->setColor(osg::Vec4(0.5f, 0.5f, 0.5f, 1.0f));baseGeode->addDrawable(baseDrawable);root->addChild(baseGeode);return root.release();
}int main() {// 设置调试输出级别osg::setNotifyLevel(osg::NOTICE);// 创建ViewerosgViewer::Viewer viewer;// 创建主场景组osg::ref_ptr<osg::Group> root = new osg::Group;// 添加手臂模型root->addChild(createArm());// 创建动画管理器osg::ref_ptr<osgAnimation::BasicAnimationManager> animManager = new osgAnimation::BasicAnimationManager;// 将动画管理器作为场景图的更新回调root->setUpdateCallback(animManager);// 添加动画osgAnimation::Animation* anim = createArmAnimation();animManager->registerAnimation(anim);animManager->playAnimation(anim);// 输出调试信息std::cout << "动画管理器状态: " << (animManager.valid() ? "有效" : "无效") << std::endl;std::cout << "已注册动画数量: " << animManager->getAnimationList().size() << std::endl;if (animManager->getAnimationList().size() > 0) {std::cout << "正在播放动画: " << animManager->getAnimationList()[0]->getName() << std::endl;}viewer.setSceneData(root);viewer.setCameraManipulator(new osgGA::TrackballManipulator);// 设置初始视角viewer.getCameraManipulator()->setHomePosition(osg::Vec3(0, -10, 5), // 眼睛位置osg::Vec3(0, 0, 2),   // 中心位置osg::Vec3(0, 0, 1)    // 上方向);viewer.home(); // 应用初始视角设置return viewer.run();
}
运行效果

在这里插入图片描述

加载osgt文件

animationOsgt.cpp

#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>
#include <osgAnimation/BasicAnimationManager>int main() {// 1. 加载模型osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("../dumptruck.osgt");if (!model) return 1;// 2. 获取动画管理器osgAnimation::BasicAnimationManager* animManager = dynamic_cast<osgAnimation::BasicAnimationManager*>(model->getUpdateCallback());if (animManager && !animManager->getAnimationList().empty()) {// 3. 正确播放动画(两种解决方案):if (animManager) {const osgAnimation::AnimationList& animList = animManager->getAnimationList();if (!animList.empty()) {// 打印所有动画信息for (const auto& anim : animList) {std::cout << "Found animation: " << anim->getName() << " (" << anim->getDuration() << "s)\n";}// 播放第一个动画animManager->playAnimation(animList[0]);} else {std::cerr << "Warning: No animations found in the model" << std::endl;}} else {std::cerr << "Error: No AnimationManager found" << std::endl;}}// 4. 设置查看器osgViewer::Viewer viewer;viewer.setSceneData(model);viewer.setCameraManipulator(new osgGA::TrackballManipulator());return viewer.run();
}
运行效果

在这里插入图片描述

本章可能会和自己的osg版本有关系,会有一些报错。耐心解决。_

在这里插入图片描述

相关文章:

  • day27-shell编程(自动化)
  • 食养有方:进行性核上性麻痹患者的健康饮食指南
  • Java线程同步技术深度解析与实践
  • 【2025年6月8日】Claude 4 国内使用全攻略
  • LLMs基础学习(八)强化学习专题(1)
  • android binder(四)binder驱动详解2
  • MyBatis中include标签用法详解
  • ARM SMMUv3 STE表和CD表数据格式分析(三)
  • 利用Pandas AI完成Excel大模型的结合实现自然语言问数
  • AtCoder Beginner Contest 409
  • OD 算法题 B卷【正整数到Excel编号之间的转换】
  • BERT
  • 自然语言处理——文本表示
  • NLP学习路线图(三十四): 命名实体识别(NER)
  • Mac M芯片 RAG 极简流程 安装 ragflow + LM studio
  • 嵌入式面试提纲
  • 网约车平台(预约打车)
  • 【Elasticsearch】映射:详解 _source store 字段
  • 【医疗电子技术】新型医疗电子和医学人工智能发展现状和趋势
  • 大数据赋能行业智能化升级:从数据价值到战略落地的全景透视
  • 美国网站建设公司哪家好/资源网站优化排名优化
  • 自己有网站怎么做点卡?/西安关键词快速排名
  • wordpress 删除gravatar/余姚关键词优化公司
  • 网络托管/谷歌seo排名
  • 建设银行网站如何下载u盾/可以直接进入网站的正能量
  • 微信手机官方网站/百度识图在线网页版