Files
3d-model-renderer/src/render/ModelNode.h
T
gs5579andDevin AI c3cb854875 Add GPU (hardware) skinning path with CPU fallback
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-28 05:59:55 +00:00

86 lines
2.9 KiB
C++

#pragma once
#include "model/Model.h"
#include "model/Poser.h"
#include <osg/Geometry>
#include <osg/MatrixTransform>
#include <osg/Texture2D>
#include <osg/Uniform>
#include <memory>
namespace mr {
enum class LightingModel { MmdToon = 0, Lambert = 1 };
// Scene graph for one model. Converts the model's Y-up space to OSG's Z-up world
// and re-uploads deformed vertices whenever the pose or morph weights change.
class ModelNode : public osg::MatrixTransform {
public:
explicit ModelNode(std::shared_ptr<Model> model);
const Model& model() const { return *model_; }
Poser& poser() { return poser_; }
void setShowSkeleton(bool show);
void setSelectedBone(int bone);
void setBackfaceCulling(bool enabled);
void setWireframe(bool enabled);
void setLightingModel(LightingModel lighting);
void setLightFollowsCamera(bool follow);
// Hardware falls back to Software when the GPU cannot do it.
void setSkinningMode(SkinningMode mode);
SkinningMode activeSkinningMode() const { return activeSkinning_; }
const osg::BoundingBox& posedBounds() const { return posedBounds_; }
// World-space bounding sphere of the rest pose.
osg::BoundingSphere restBound() const;
void updateDeformation();
private:
void buildMesh();
osg::StateSet* buildMaterialState(const Material& material, int order);
void preloadTextureImages();
osg::Texture2D* texture(int index, bool clamp);
void buildSkinning();
void uploadBoneMatrices();
void buildSkeleton();
void updateSkeleton();
std::shared_ptr<Model> model_;
Poser poser_;
osg::ref_ptr<osg::Group> meshGroup_;
osg::ref_ptr<osg::Vec3Array> positions_;
osg::ref_ptr<osg::Vec3Array> normals_;
std::vector<osg::ref_ptr<osg::Geometry>> geometries_;
std::vector<osg::ref_ptr<osg::Texture2D>> textures_;
std::vector<bool> textureTried_;
std::vector<osg::ref_ptr<osg::Image>> preloadedImages_;
osg::ref_ptr<osg::Texture2D> sharedToons_[10];
osg::ref_ptr<osg::Geode> skeletonGeode_;
osg::ref_ptr<osg::Geometry> skeletonLines_;
osg::ref_ptr<osg::Geometry> skeletonPoints_;
std::vector<int> skeletonBones_;
int selectedBone_ = -1;
bool skeletonDirty_ = true;
SkinningMode requestedSkinning_ = SkinningMode::Hardware;
SkinningMode activeSkinning_ = SkinningMode::Software;
osg::ref_ptr<osg::Image> boneImage_;
osg::ref_ptr<osg::Texture2D> boneTexture_;
osg::ref_ptr<osg::Uniform> skinningUniform_;
std::vector<osg::Vec3f> boneRestPositions_;
float boneReach_ = 0.f; // max distance of a vertex (incl. morphs) from its main bone
osg::BoundingBox unskinnedBounds_; // vertices without bones
osg::BoundingBox posedBounds_;
osg::ref_ptr<osg::Uniform> lightingUniform_;
osg::ref_ptr<osg::Uniform> lightFollowUniform_;
osg::ref_ptr<osg::Uniform> lightDirUniform_;
};
} // namespace mr