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
11 changed files with 109 additions and 99 deletions
Showing only changes of commit 9d3765ff91 - Show all commits

View File

@ -80,6 +80,21 @@ pxr::VtValue CurvesData::get_data(pxr::TfToken const &key) const
return pxr::VtValue(); return pxr::VtValue();
} }
pxr::SdfPath CurvesData::material_id() const
{
if (!mat_data_) {
return pxr::SdfPath();
}
return mat_data_->prim_id;
}
void CurvesData::available_materials(Set<pxr::SdfPath> &paths) const
{
if (mat_data_ && !mat_data_->prim_id.IsEmpty()) {
paths.add(mat_data_->prim_id);
}
}
pxr::HdBasisCurvesTopology CurvesData::topology() const pxr::HdBasisCurvesTopology CurvesData::topology() const
{ {
return pxr::HdBasisCurvesTopology(pxr::HdTokens->linear, return pxr::HdBasisCurvesTopology(pxr::HdTokens->linear,
@ -111,18 +126,27 @@ pxr::HdPrimvarDescriptorVector CurvesData::primvar_descriptors(
return primvars; return primvars;
} }
pxr::SdfPath CurvesData::material_id() const void CurvesData::write_materials()
{ {
if (!mat_data_) { Object *object = (Object *)id;
return pxr::SdfPath(); Material *mat = nullptr;
} /* TODO: Using only first material. Add support for multimaterial. */
return mat_data_->prim_id; if (BKE_object_material_count_eval(object) > 0) {
mat = BKE_object_material_get_eval(object, 0);
} }
void CurvesData::available_materials(Set<pxr::SdfPath> &paths) const if (!mat) {
{ mat_data_ = nullptr;
if (mat_data_ && !mat_data_->prim_id.IsEmpty()) { return;
paths.add(mat_data_->prim_id); }
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();
} }
} }
@ -167,28 +191,4 @@ void CurvesData::write_uv_maps(Curves *curves)
} }
} }
void CurvesData::write_materials()
{
Object *object = (Object *)id;
Material *mat = nullptr;
/* TODO: Using only first material. Add support for multimaterial. */
if (BKE_object_material_count_eval(object) > 0) {
mat = BKE_object_material_get_eval(object, 0);
}
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

View File

@ -25,16 +25,18 @@ class CurvesData : public ObjectData {
void update() override; void update() override;
pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue get_data(pxr::TfToken const &key) const override;
pxr::SdfPath material_id() const override;
void available_materials(Set<pxr::SdfPath> &paths) const override;
pxr::HdBasisCurvesTopology topology() const; pxr::HdBasisCurvesTopology topology() const;
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const;
pxr::SdfPath material_id() const;
void available_materials(Set<pxr::SdfPath> &paths) const override; protected:
void write_materials() override;
private: private:
void write_curves(Curves *curves); void write_curves(Curves *curves);
void write_uv_maps(Curves *curves); void write_uv_maps(Curves *curves);
void write_materials();
pxr::VtIntArray curve_vertex_counts_; pxr::VtIntArray curve_vertex_counts_;
pxr::VtVec3fArray vertices_; pxr::VtVec3fArray vertices_;

View File

@ -55,7 +55,7 @@ class IdData {
level, \ level, \
"%s (%s): " msg, \ "%s (%s): " msg, \
prim_id.GetText(), \ prim_id.GetText(), \
id->name, \ id ? id->name : "", \
##__VA_ARGS__); ##__VA_ARGS__);
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -100,6 +100,24 @@ pxr::VtValue MeshData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key)
return get_data(key); return get_data(key);
} }
pxr::SdfPath MeshData::material_id(pxr::SdfPath const &id) const
{
const SubMesh &sm = submesh(id);
if (!sm.mat_data) {
return pxr::SdfPath();
}
return sm.mat_data->prim_id;
}
void MeshData::available_materials(Set<pxr::SdfPath> &paths) const
{
for (auto &sm : submeshes_) {
if (sm.mat_data && !sm.mat_data->prim_id.IsEmpty()) {
paths.add(sm.mat_data->prim_id);
}
}
}
pxr::HdMeshTopology MeshData::mesh_topology(pxr::SdfPath const &id) const pxr::HdMeshTopology MeshData::mesh_topology(pxr::SdfPath const &id) const
{ {
const SubMesh &sm = submesh(id); const SubMesh &sm = submesh(id);
@ -131,15 +149,6 @@ pxr::HdPrimvarDescriptorVector MeshData::primvar_descriptors(
return primvars; return primvars;
} }
pxr::SdfPath MeshData::material_id(pxr::SdfPath const &id) const
{
const SubMesh &sm = submesh(id);
if (!sm.mat_data) {
return pxr::SdfPath();
}
return sm.mat_data->prim_id;
}
pxr::HdCullStyle MeshData::cull_style(pxr::SdfPath const &id) const pxr::HdCullStyle MeshData::cull_style(pxr::SdfPath const &id) const
{ {
const SubMesh &sm = submesh(id); const SubMesh &sm = submesh(id);
@ -170,15 +179,6 @@ void MeshData::update_double_sided(MaterialData *mat_data)
} }
} }
void MeshData::available_materials(Set<pxr::SdfPath> &paths) const
{
for (auto &sm : submeshes_) {
if (sm.mat_data && !sm.mat_data->prim_id.IsEmpty()) {
paths.add(sm.mat_data->prim_id);
}
}
}
pxr::SdfPathVector MeshData::submesh_paths() const pxr::SdfPathVector MeshData::submesh_paths() const
{ {
pxr::SdfPathVector ret; pxr::SdfPathVector ret;
@ -188,6 +188,30 @@ pxr::SdfPathVector MeshData::submesh_paths() const
return ret; return ret;
} }
void MeshData::write_materials()
{
Object *object = (Object *)id;
for (int i = 0; i < submeshes_.size(); ++i) {
SubMesh &m = submeshes_[i];
Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1);
if (!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();
}
}
}
pxr::SdfPath MeshData::submesh_prim_id(int index) const pxr::SdfPath MeshData::submesh_prim_id(int index) const
{ {
char name[16]; char name[16];
@ -288,28 +312,4 @@ void MeshData::write_submeshes(Mesh *mesh)
} }
} }
void MeshData::write_materials()
{
Object *object = (Object *)id;
for (int i = 0; i < submeshes_.size(); ++i) {
SubMesh &m = submeshes_[i];
Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1);
if (!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();
}
}
}
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -34,21 +34,23 @@ class MeshData : public ObjectData {
pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue get_data(pxr::TfToken const &key) const override;
pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const override; pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const override;
pxr::SdfPath material_id(pxr::SdfPath const &id) const override;
void available_materials(Set<pxr::SdfPath> &paths) const override;
pxr::HdMeshTopology mesh_topology(pxr::SdfPath const &id) const; pxr::HdMeshTopology mesh_topology(pxr::SdfPath const &id) const;
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const;
pxr::SdfPath material_id(pxr::SdfPath const &id) const;
pxr::HdCullStyle cull_style(pxr::SdfPath const &id) const; pxr::HdCullStyle cull_style(pxr::SdfPath const &id) const;
bool double_sided(pxr::SdfPath const &id) const; bool double_sided(pxr::SdfPath const &id) const;
void update_double_sided(MaterialData *mat_data); void update_double_sided(MaterialData *mat_data);
void available_materials(Set<pxr::SdfPath> &paths) const override;
pxr::SdfPathVector submesh_paths() const; pxr::SdfPathVector submesh_paths() const;
protected:
void write_materials() override;
private: private:
pxr::SdfPath submesh_prim_id(int index) const; pxr::SdfPath submesh_prim_id(int index) const;
const SubMesh &submesh(pxr::SdfPath const &id) const; const SubMesh &submesh(pxr::SdfPath const &id) const;
void write_submeshes(Mesh *mesh); void write_submeshes(Mesh *mesh);
void write_materials();
pxr::VtVec3fArray vertices_; pxr::VtVec3fArray vertices_;
std::vector<SubMesh> submeshes_; std::vector<SubMesh> submeshes_;

View File

@ -84,6 +84,16 @@ pxr::VtValue ObjectData::get_data(pxr::SdfPath const &id, pxr::TfToken const &ke
return get_data(key); return get_data(key);
} }
pxr::SdfPath ObjectData::material_id() const
{
return pxr::SdfPath();
}
pxr::SdfPath ObjectData::material_id(pxr::SdfPath const &id) const
{
return material_id();
}
void ObjectData::available_materials(Set<pxr::SdfPath> &paths) const {} void ObjectData::available_materials(Set<pxr::SdfPath> &paths) const {}
void ObjectData::write_transform() void ObjectData::write_transform()

View File

@ -31,6 +31,8 @@ class ObjectData : public IdData {
using IdData::get_data; using IdData::get_data;
virtual pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const; virtual pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const;
virtual pxr::SdfPath material_id() const;
virtual pxr::SdfPath material_id(pxr::SdfPath const &id) const;
virtual void available_materials(Set<pxr::SdfPath> &paths) const; virtual void available_materials(Set<pxr::SdfPath> &paths) const;
pxr::GfMatrix4d transform; pxr::GfMatrix4d transform;

View File

@ -121,11 +121,6 @@ pxr::VtValue VolumeData::get_data(pxr::TfToken const &key) const
return pxr::VtValue(); return pxr::VtValue();
} }
pxr::HdVolumeFieldDescriptorVector VolumeData::field_descriptors() const
{
return field_descriptors_;
}
pxr::SdfPath VolumeData::material_id() const pxr::SdfPath VolumeData::material_id() const
{ {
if (!mat_data_) { if (!mat_data_) {
@ -141,6 +136,11 @@ void VolumeData::available_materials(Set<pxr::SdfPath> &paths) const
} }
} }
pxr::HdVolumeFieldDescriptorVector VolumeData::field_descriptors() const
{
return field_descriptors_;
}
void VolumeData::write_materials() void VolumeData::write_materials()
{ {
Object *object = (Object *)id; Object *object = (Object *)id;

View File

@ -20,14 +20,15 @@ class VolumeData : public ObjectData {
void update() override; void update() override;
pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue get_data(pxr::TfToken const &key) const override;
pxr::SdfPath material_id() const override;
pxr::HdVolumeFieldDescriptorVector field_descriptors() const;
pxr::SdfPath material_id() const;
void available_materials(Set<pxr::SdfPath> &paths) const override; void available_materials(Set<pxr::SdfPath> &paths) const override;
private: pxr::HdVolumeFieldDescriptorVector field_descriptors() const;
void write_materials();
protected:
void write_materials() override;
private:
std::string filepath_; std::string filepath_;
pxr::HdVolumeFieldDescriptorVector field_descriptors_; pxr::HdVolumeFieldDescriptorVector field_descriptors_;
MaterialData *mat_data_ = nullptr; MaterialData *mat_data_ = nullptr;

View File

@ -134,12 +134,6 @@ void WorldData::init()
write_transform(); write_transform();
} }
void WorldData::insert()
{
ID_LOG(1, "");
scene_delegate_->GetRenderIndex().InsertSprim(prim_type_, scene_delegate_, prim_id);
}
void WorldData::update() void WorldData::update()
{ {
ID_LOG(1, ""); ID_LOG(1, "");

View File

@ -23,7 +23,6 @@ class WorldData : public LightData {
WorldData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id); WorldData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id);
void init() override; void init() override;
void insert() override;
void update() override; void update() override;
protected: protected: