Files
3d-model-renderer/src/render/ModelNode.h
T
gs5579andDevin AI 3154e4fab9 Add Look panel: edge outlines, shadow mapping, anime cel and soft studio shading, rim light
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-12-01 06:08:50 +00:00

91 lines
3.1 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, AnimeCel = 2, SoftStudio = 3 };
// 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);
// MMD edge lines for materials that have them; width multiplies every material's edge size.
void setShowOutlines(bool show);
void setOutlineWidth(float width);
bool hasOutlines() const;
// 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 buildEdges(osg::Vec2Array* uvs);
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::Geode> edgeGroup_;
osg::ref_ptr<osg::Uniform> edgeWidthUniform_;
bool showOutlines_ = true, wireframe_ = false;
};
} // namespace mr