Add buffer (bytestring) accessors to material colors - plus small fixes

This commit is contained in:
Thomas Krijnen
2024-02-28 11:12:17 +01:00
parent 5e7156021b
commit ce5b1530e3
+26 -2
View File
@@ -83,7 +83,7 @@
template <typename T>
std::pair<char const*, size_t> vector_to_buffer(const T& t) {
using V = typename std::remove_reference<decltype(t)>::type;
return { reinterpret_cast<const char*>(t.data()), t.size() * sizeof(V::value_type) };
return { reinterpret_cast<const char*>(t.data()), t.size() * sizeof(typename V::value_type) };
}
%}
@@ -321,6 +321,29 @@ struct ShapeRTTI : public boost::static_visitor<PyObject*>
return vector_to_buffer(self->normals());
}
PyObject* colors_buffer() const {
std::vector<double> clrs;
clrs.reserve(self->materials().size() * 4);
for (auto& m : self->materials()) {
if (m.diffuse) {
clrs.push_back(m.diffuse.ccomponents()[0]);
clrs.push_back(m.diffuse.ccomponents()[1]);
clrs.push_back(m.diffuse.ccomponents()[2]);
} else {
clrs.push_back(0.);
clrs.push_back(0.);
clrs.push_back(0.);
}
if (m.has_transparency()) {
clrs.push_back(1. - m.transparency);
} else {
clrs.push_back(1.);
}
}
auto p = vector_to_buffer(clrs);
return PyBytes_FromStringAndSize(p.first, p.second);
}
%pythoncode %{
# Hide the getters with read-only property implementations
id = property(id)
@@ -337,7 +360,8 @@ struct ShapeRTTI : public boost::static_visitor<PyObject*>
material_ids_buffer = property(material_ids_buffer)
item_ids_buffer = property(item_ids_buffer)
verts_buffer = property(verts_buffer)
normals = property(normals)
normals_buffer = property(normals_buffer)
colors_buffer = property(colors_buffer)
%}
};