Refactor code after PR #57 #67

Merged
Bogdan Nagirniak merged 9 commits from hydra-object_data-refactor into hydra-render 2023-07-20 22:34:34 +02:00
7 changed files with 30 additions and 54 deletions
Showing only changes of commit 83403c15a4 - Show all commits

View File

@ -470,7 +470,6 @@ void BlenderSceneDelegate::update_collection()
} }
else { else {
obj_data = objects_.lookup_or_add(id, ObjectData::create(this, object, id)).get(); obj_data = objects_.lookup_or_add(id, ObjectData::create(this, object, id)).get();
obj_data->init();
obj_data->insert(); obj_data->insert();
} }
} }

View File

@ -28,10 +28,7 @@ extern struct CLG_LogRef *LOG_RENDER_HYDRA_SCENE;
class Engine; class Engine;
class BlenderSceneDelegate : public pxr::HdSceneDelegate { class BlenderSceneDelegate : public pxr::HdSceneDelegate {
friend ObjectData; /* has access to instances */ friend ObjectData; /* has access to materials */
friend CurvesData; /* has access to materials */
friend VolumeData; /* has access to materials */
friend MeshData; /* has access to materials */
friend MaterialData; /* has access to objects and instancers */ friend MaterialData; /* has access to objects and instancers */
public: public:

View File

@ -134,20 +134,7 @@ void CurvesData::write_materials()
if (BKE_object_material_count_eval(object) > 0) { if (BKE_object_material_count_eval(object) > 0) {
mat = BKE_object_material_get_eval(object, 0); mat = BKE_object_material_get_eval(object, 0);
} }
mat_data_ = get_or_create_material(mat);
if (!mat) {
mat_data_ = nullptr;
return;
}
pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat);
mat_data_ = scene_delegate_->material_data(p_id);
if (!mat_data_) {
scene_delegate_->materials_.add_new(
p_id, std::make_unique<MaterialData>(scene_delegate_, mat, p_id));
mat_data_ = scene_delegate_->material_data(p_id);
mat_data_->init();
mat_data_->insert();
}
} }
void CurvesData::write_curves(Curves *curves) void CurvesData::write_curves(Curves *curves)

View File

@ -195,21 +195,7 @@ void MeshData::write_materials()
for (int i = 0; i < submeshes_.size(); ++i) { for (int i = 0; i < submeshes_.size(); ++i) {
SubMesh &m = submeshes_[i]; SubMesh &m = submeshes_[i];
Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1); Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1);
if (!mat) { m.mat_data = get_or_create_material(mat);
m.mat_data = nullptr;
continue;
}
pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat);
m.mat_data = scene_delegate_->material_data(p_id);
if (!m.mat_data) {
m.mat_data = scene_delegate_->materials_
.lookup_or_add(p_id,
std::make_unique<MaterialData>(scene_delegate_, mat, p_id))
.get();
m.mat_data->init();
m.mat_data->insert();
}
} }
} }

View File

@ -23,7 +23,7 @@ std::unique_ptr<ObjectData> ObjectData::create(BlenderSceneDelegate *scene_deleg
Object *object, Object *object,
pxr::SdfPath const &prim_id) pxr::SdfPath const &prim_id)
{ {
std::unique_ptr<ObjectData> data; std::unique_ptr<ObjectData> obj_data;
switch (object->type) { switch (object->type) {
case OB_MESH: case OB_MESH:
@ -31,22 +31,23 @@ std::unique_ptr<ObjectData> ObjectData::create(BlenderSceneDelegate *scene_deleg
case OB_FONT: case OB_FONT:
case OB_CURVES_LEGACY: case OB_CURVES_LEGACY:
case OB_MBALL: case OB_MBALL:
data = std::make_unique<MeshData>(scene_delegate, object, prim_id); obj_data = std::make_unique<MeshData>(scene_delegate, object, prim_id);
break; break;
case OB_CURVES: case OB_CURVES:
data = std::make_unique<CurvesData>(scene_delegate, object, prim_id); obj_data = std::make_unique<CurvesData>(scene_delegate, object, prim_id);
break; break;
case OB_LAMP: case OB_LAMP:
data = std::make_unique<LightData>(scene_delegate, object, prim_id); obj_data = std::make_unique<LightData>(scene_delegate, object, prim_id);
break; break;
case OB_VOLUME: case OB_VOLUME:
data = std::make_unique<VolumeData>(scene_delegate, object, prim_id); obj_data = std::make_unique<VolumeData>(scene_delegate, object, prim_id);
break; break;
default: default:
BLI_assert_unreachable(); BLI_assert_unreachable();
break; break;
} }
return data; obj_data->init();
return obj_data;
} }
bool ObjectData::is_supported(Object *object) bool ObjectData::is_supported(Object *object)
@ -103,6 +104,24 @@ void ObjectData::write_transform()
void ObjectData::write_materials() {} void ObjectData::write_materials() {}
MaterialData *ObjectData::get_or_create_material(Material *mat)
{
if (!mat) {
return nullptr;
}
pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat);
MaterialData *mat_data = scene_delegate_->material_data(p_id);
if (!mat_data) {
scene_delegate_->materials_.add_new(
p_id, std::make_unique<MaterialData>(scene_delegate_, mat, p_id));
mat_data = scene_delegate_->material_data(p_id);
mat_data->init();
mat_data->insert();
}
return mat_data;
}
pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]) pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4])
{ {
pxr::GfMatrix4d ret; pxr::GfMatrix4d ret;

View File

@ -41,6 +41,7 @@ class ObjectData : public IdData {
protected: protected:
virtual void write_transform(); virtual void write_transform();
virtual void write_materials(); virtual void write_materials();
MaterialData *get_or_create_material(Material *mat);
}; };
using ObjectDataMap = Map<pxr::SdfPath, std::unique_ptr<ObjectData>>; using ObjectDataMap = Map<pxr::SdfPath, std::unique_ptr<ObjectData>>;

View File

@ -149,20 +149,7 @@ void VolumeData::write_materials()
if (BKE_object_material_count_eval(object) > 0) { if (BKE_object_material_count_eval(object) > 0) {
mat = BKE_object_material_get_eval(object, 0); mat = BKE_object_material_get_eval(object, 0);
} }
mat_data_ = get_or_create_material(mat);
if (!mat) {
mat_data_ = nullptr;
return;
}
pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat);
mat_data_ = scene_delegate_->material_data(p_id);
if (!mat_data_) {
scene_delegate_->materials_.add_new(
p_id, std::make_unique<MaterialData>(scene_delegate_, mat, p_id));
mat_data_ = scene_delegate_->material_data(p_id);
mat_data_->init();
mat_data_->insert();
}
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra