Copy texture coordinates from OBJ into flex. Fixes #2464.

PiperOrigin-RevId: 732109960
Change-Id: I506b3540ab5d489ce8f10d0c473919611ccb654a
This commit is contained in:
Alessio Quaglino
2025-02-28 06:03:11 -08:00
committed by Copybara-Service
parent 06e217df7e
commit 7651cb1b33
19 changed files with 117 additions and 15 deletions
+6
View File
@@ -413,6 +413,7 @@ bool mjCFlexcomp::Make(mjsBody* body, char* error, int error_sz) {
mjs_setString(pf->name, name.c_str());
mjs_setInt(pf->elem, element.data(), element.size());
mjs_setFloat(pf->texcoord, texcoord.data(), texcoord.size());
mjs_setInt(pf->facetexcoord, facetexcoord.data(), facetexcoord.size());
if (!centered) {
mjs_setDouble(pf->vert, point.data(), point.size());
}
@@ -1055,6 +1056,11 @@ bool mjCFlexcomp::MakeMesh(mjCModel* model, char* error, int error_sz) {
point[i] = (double) mesh.Vert(i);
}
if (mesh.HasTexcoord()) {
texcoord = mesh.Texcoord();
facetexcoord = mesh.FaceTexcoord();
}
// copy faces or create 3D mesh
if (def.spec.flex->dim == 2) {
element = mesh.Face();
+1
View File
@@ -106,6 +106,7 @@ class mjCFlexcomp {
std::vector<bool> used; // is point used by any element (false: skip)
std::vector<int> element; // flex elements
std::vector<float> texcoord; // vertex texture coordinates
std::vector<int> facetexcoord; // face texture coordinates (OBJ only)
// plugin support
std::string plugin_name;
+10 -1
View File
@@ -3184,6 +3184,7 @@ void mjCFlex::PointToLocal() {
spec.vert = &spec_vert_;
spec.node = &spec_node_;
spec.texcoord = &spec_texcoord_;
spec.facetexcoord = &spec_facetexcoord_;
spec.elem = &spec_elem_;
spec.info = &info;
material = nullptr;
@@ -3192,6 +3193,7 @@ void mjCFlex::PointToLocal() {
vert = nullptr;
node = nullptr;
texcoord = nullptr;
facetexcoord = nullptr;
elem = nullptr;
}
@@ -3217,6 +3219,7 @@ void mjCFlex::CopyFromSpec() {
vert_ = spec_vert_;
node_ = spec_node_;
texcoord_ = spec_texcoord_;
facetexcoord_ = spec_facetexcoord_;
elem_ = spec_elem_;
// clear precompiled asset. TODO: use asset cache
@@ -3320,10 +3323,16 @@ void mjCFlex::Compile(const mjVFS* vfs) {
}
// check texcoord
if (!texcoord_.empty() && texcoord_.size()!=2*nvert) {
if (!texcoord_.empty() && texcoord_.size()!=2*nvert && facetexcoord_.empty()) {
throw mjCError(this, "two texture coordinates per vertex expected");
}
// no facetexcoord: copy from faces
if (facetexcoord_.empty() && !texcoord_.empty()) {
facetexcoord_.assign(3*nelem, 0);
memcpy(facetexcoord_.data(), elem_.data(), 3*nelem*sizeof(int));
}
// resolve material name
mjCBase* pmat = model->FindObject(mjOBJ_MATERIAL, material_);
if (pmat) {
+3
View File
@@ -2911,10 +2911,13 @@ void mjCModel::CopyObjects(mjModel* m) {
}
if (pfl->texcoord_.empty()) {
m->flex_texcoordadr[i] = -1;
memcpy(m->flex_elemtexcoord + elemdata_adr, pfl->elem_.data(), pfl->elem_.size()*sizeof(int));
} else {
m->flex_texcoordadr[i] = texcoord_adr;
memcpy(m->flex_texcoord + 2*texcoord_adr,
pfl->texcoord_.data(), pfl->texcoord_.size()*sizeof(float));
memcpy(m->flex_elemtexcoord + elemdata_adr, pfl->facetexcoord_.data(),
pfl->facetexcoord_.size()*sizeof(int));
}
m->flex_elemnum[i] = pfl->nelem;
memcpy(m->flex_elem + elemdata_adr, pfl->elem_.data(), pfl->elem_.size()*sizeof(int));
+5
View File
@@ -776,6 +776,7 @@ class mjCFlex_ : public mjCBase {
std::vector<double> node_; // node positions
std::vector<int> elem_; // element vertex ids
std::vector<float> texcoord_; // vertex texture coordinates
std::vector<int> facetexcoord_; // face texture coordinates (OBJ only)
std::string material_; // name of material used for rendering
std::string spec_material_;
@@ -785,6 +786,7 @@ class mjCFlex_ : public mjCBase {
std::vector<double> spec_node_;
std::vector<int> spec_elem_;
std::vector<float> spec_texcoord_;
std::vector<int> spec_facetexcoord_;
};
class mjCFlex: public mjCFlex_, private mjsFlex {
@@ -815,6 +817,7 @@ class mjCFlex: public mjCFlex_, private mjsFlex {
const std::vector<double>& get_elemaabb() const { return elemaabb_; }
const std::vector<int>& get_elem() const { return elem_; }
const std::vector<float>& get_texcoord() const { return texcoord_; }
const std::vector<int>& get_facetexcoord() const { return facetexcoord_; }
const std::vector<std::string>& get_nodebody() const { return nodebody_; }
bool HasTexcoord() const; // texcoord not null
@@ -927,6 +930,8 @@ class mjCMesh: public mjCMesh_, private mjsMesh {
float Vert(int i) const { return vert_[i]; }
const std::vector<float>& UserVert() const { return spec_vert_; }
const std::vector<float>& UserNormal() const { return spec_normal_; }
const std::vector<float>& Texcoord() const { return texcoord_; }
const std::vector<int>& FaceTexcoord() const { return facetexcoord_; }
const std::vector<float>& UserTexcoord() const { return spec_texcoord_; }
const std::vector<int>& Face() const { return face_; }
const std::vector<int>& UserFace() const { return spec_face_; }