Skip to content

Commit

Permalink
Revert slider OBJ export
Browse files Browse the repository at this point in the history
  • Loading branch information
ousnius committed Jan 24, 2016
1 parent c4ab8f5 commit 3e9f682
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 209 deletions.
179 changes: 5 additions & 174 deletions ObjFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ int ObjFile::AddGroup(const string& name, const vector<Vector3>& verts, const ve
return 0;
}


int ObjFile::AddGroup(const string& name, const vector<Vector3>& verts, const vector<Face>& faces, const vector<Vector2>& uvs) {
if (name.empty() || verts.empty())
return 1;

ObjData* newData = new ObjData();
newData->name = name;
newData->verts = verts;
newData->faces = faces;
newData->uvs = uvs;

data[name] = newData;
return 0;
}

int ObjFile::LoadForNif(const string &fileName) {
fstream base(fileName.c_str(), ios_base::in | ios_base::binary);
if (base.fail())
Expand Down Expand Up @@ -188,147 +173,6 @@ int ObjFile::LoadForNif(fstream &base) {
return 0;
}

int ObjFile::LoadVertOrderMap(const string &fileName, map<int, int>& outMap, vector<Face>& origFaces, vector<Vector2>& origUVs) {
fstream base(fileName.c_str(), ios_base::in | ios_base::binary);
if (base.fail())
return 1;

LoadVertOrderMap(base, outMap, origFaces, origUVs);
base.close();
return 0;
}

int ObjFile::LoadVertOrderMap(fstream &base, map<int, int>& outMap, vector<Face>& origFaces, vector<Vector2>& origUVs) {
ObjData* di = new ObjData();

Vector3 v;
Vector2 uv;
Vector2 uv2;
Triangle t;

string dump;
string curgrp;
string facept1;
string facept2;
string facept3;
string facept4;
int f[4];
int ft[4];
int nPoints = 0;
int v_idx[4];

vector<Vector3> verts;
vector<Vector2> uvs;
size_t pos;
map<int, vector<VertUV>> vertMap;
map<int, vector<VertUV>>::iterator savedVert;

bool gotface = false;

while (!base.eof()) {
if (!gotface)
base >> dump;
else
gotface = false;

if (dump.compare("v") == 0) {
base >> v.x >> v.y >> v.z;
verts.push_back(v);
}
else if (dump.compare("g") == 0 || dump.compare("o") == 0) {
base >> curgrp;

if (di->name != "") {
data[di->name] = di;
di = new ObjData;
}

di->name = curgrp;
objGroups.push_back(curgrp);
}
else if (dump.compare("vt") == 0) {
base >> uv.u >> uv.v;
uv.v = 1.0f - uv.v;
uvs.push_back(uv);
origUVs.push_back(uv);
}
else if (dump.compare("f") == 0) {
base >> facept1 >> facept2 >> facept3;
pos = facept1.find('/');
f[0] = atoi(facept1.c_str()) - 1;
ft[0] = atoi(facept1.substr(pos + 1).c_str()) - 1;
pos = facept2.find('/');
f[1] = atoi(facept2.c_str()) - 1;
ft[1] = atoi(facept2.substr(pos + 1).c_str()) - 1;
pos = facept3.find('/');
f[2] = atoi(facept3.c_str()) - 1;
ft[2] = atoi(facept3.substr(pos + 1).c_str()) - 1;
base >> facept4;

if (facept4 == "f" || facept4 == "g" || facept4 == "s" || facept4 == "#") {
gotface = true;
dump = facept4;
nPoints = 3;
}
else if (facept4.length() > 0) {
pos = facept4.find('/');
f[3] = atoi(facept4.c_str()) - 1;
ft[3] = atoi(facept4.substr(pos + 1).c_str()) - 1;
nPoints = 4;
}

if (f[0] == -1 || f[1] == -1 || f[2] == -1)
continue;

origFaces.emplace_back(nPoints, f, ft);

for (int i = 0; i < nPoints; i++) {
v_idx[i] = di->verts.size();
if ((savedVert = vertMap.find(f[i])) != vertMap.end()) {
for (int j = 0; j < savedVert->second.size(); j++) {
if (savedVert->second[j].uv == ft[i])
v_idx[i] = savedVert->second[j].v;
else if (uvs.size() > 0) {
uv = uvs[ft[i]];
uv2 = uvs[savedVert->second[j].uv];
if (fabs(uv.u - uv2.u) > uvDupThreshold) {
v_idx[i] = v_idx[i];
continue;
}
else if (fabs(uv.v - uv2.v) > uvDupThreshold) {
v_idx[i] = v_idx[i];
continue;
}
v_idx[i] = savedVert->second[j].v;
}
}
}

if (v_idx[i] == di->verts.size()) {
vertMap[f[i]].push_back(VertUV(v_idx[i], ft[i]));
outMap[f[i]] = di->verts.size();
di->verts.push_back(verts[f[i]]);
if (uvs.size() > 0) {
di->uvs.push_back(uvs[ft[i]]);
}
}
}
t.p1 = v_idx[0];
t.p2 = v_idx[1];
t.p3 = v_idx[2];
di->tris.push_back(t);
if (nPoints == 4) {
t.p1 = v_idx[0];
t.p2 = v_idx[2];
t.p3 = v_idx[3];
di->tris.push_back(t);
}
}
}

delete di;
return 0;
}

int ObjFile::Save(const string &fileName) {
ofstream file(fileName.c_str(), ios_base::binary);
Expand All @@ -353,24 +197,11 @@ int ObjFile::Save(const string &fileName) {
file << "vt " << d.second->uvs[i].u << " " << (1.0f - d.second->uvs[i].v) << endl;
file << endl;

if (d.second->faces.size() > 0) {
for (int i = 0; i < d.second->faces.size(); i++) {
file << "f " << d.second->faces[i].p1 + 1 << "/" << d.second->faces[i].uv1 + 1 << " "
<< d.second->faces[i].p2 + 1 << "/" << d.second->faces[i].uv2 + 1 << " "
<< d.second->faces[i].p3 + 1 << "/" << d.second->faces[i].uv3 + 1;
if (d.second->faces[i].nPoints == 4) {
file << " " << d.second->faces[i].p4 + 1 << "/" << d.second->faces[i].uv4 + 1;
}
file << endl;
}
}
else {
for (int i = 0; i < d.second->tris.size(); i++) {
file << "f " << d.second->tris[i].p1 + 1 << "/" << d.second->tris[i].p1 + 1 << " "
<< d.second->tris[i].p2 + 1 << "/" << d.second->tris[i].p2 + 1 << " "
<< d.second->tris[i].p3 + 1 << "/" << d.second->tris[i].p3 + 1
<< endl;
}
for (int i = 0; i < d.second->tris.size(); i++) {
file << "f " << d.second->tris[i].p1 + 1 << "/" << d.second->tris[i].p1 + 1 << " "
<< d.second->tris[i].p2 + 1 << "/" << d.second->tris[i].p2 + 1 << " "
<< d.second->tris[i].p3 + 1 << "/" << d.second->tris[i].p3 + 1
<< endl;
}
file << endl;
}
Expand Down
5 changes: 0 additions & 5 deletions ObjFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ struct ObjData {
vector<Vector3> verts;
vector<Triangle> tris;
vector<Vector2> uvs;
vector<Face> faces; // only for quad support during special output operations.
};

class ObjFile {
Expand All @@ -41,17 +40,13 @@ class ObjFile {
~ObjFile();

int AddGroup(const string& name, const vector<Vector3>& verts, const vector<Triangle>& tris, const vector<Vector2>& uvs);
int AddGroup(const string& name, const vector<Vector3>& verts, const vector<Face>& faces, const vector<Vector2>& uvs);

void SetScale(const Vector3& inScale) { scale = inScale; }
void SetOffset(const Vector3& inOffset) { offset = inOffset; }

int LoadForNif(const string& fileName);
int LoadForNif(fstream& base);

int LoadVertOrderMap(const string& fileName, map<int, int>& outMap, vector<Face>& origFaces, vector<Vector2>& origUVs);
int LoadVertOrderMap(fstream& base, map<int, int>& outMap, vector<Face>& origFaces, vector<Vector2>& origUVs);

int Save(const string& fileName);

bool CopyDataForGroup(const string& name, vector<Vector3>* v, vector<Triangle>* t, vector<Vector2>* uv);
Expand Down
18 changes: 2 additions & 16 deletions OutfitProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ int OutfitProject::SaveSliderOBJ(const string& sliderName, const string& shapeNa
vector<Triangle> tris;
const vector<Vector3>* verts = workNif.GetRawVertsForShape(shapeName);
workNif.GetTrisForShape(shapeName, &tris);
const vector<Vector2>* uvs = workNif.GetUvsForShape(shapeName);

vector<Vector3> outVerts = *verts;

Expand All @@ -740,24 +741,9 @@ int OutfitProject::SaveSliderOBJ(const string& sliderName, const string& shapeNa
else
morpher.ApplyResultToVerts(sliderName, target, &outVerts);


string mapfn = wxFileSelector("Choose vertex map source .obj", wxEmptyString, wxEmptyString, ".obj", "*.obj", wxFD_OPEN | wxFD_FILE_MUST_EXIST, owner);
if (mapfn.empty())
return 1;

map<int, int> orderMap;
vector<Face> origFaces;
vector<Vector2> origUvs;
ObjFile orderFile;
orderFile.LoadVertOrderMap(mapfn, orderMap, origFaces, origUvs);

vector<Vector3> swizzleverts(orderMap.size());
for (int i = 0; i < orderMap.size(); i++)
swizzleverts[i] = outVerts[orderMap[i]];

ObjFile obj;
obj.SetScale(Vector3(0.1f, 0.1f, 0.1f));
obj.AddGroup(shapeName, swizzleverts, origFaces, origUvs);
obj.AddGroup(shapeName, outVerts, tris, *uvs);
if (obj.Save(fileName))
return 1;

Expand Down
14 changes: 0 additions & 14 deletions OutfitStudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2559,7 +2559,6 @@ void OutfitStudio::OnSliderExportOBJ(wxCommandEvent& WXUNUSED(event)) {
}
}
else {
/*
string fn = wxFileSelector("Export .obj slider data", wxEmptyString, wxEmptyString, ".obj", "*.obj", wxFD_SAVE | wxFD_OVERWRITE_PROMPT, this);
if (fn.empty())
return;
Expand All @@ -2569,19 +2568,6 @@ void OutfitStudio::OnSliderExportOBJ(wxCommandEvent& WXUNUSED(event)) {
wxLogError("Failed to export OBJ file '%s'!", fn);
wxMessageBox("Failed to export OBJ file!", "Error", wxICON_ERROR);
}
*/
// TEMPORARY bulk slider export
string dir = wxDirSelector("Export .obj slider data to directory", wxEmptyString, wxDD_DIR_MUST_EXIST, wxDefaultPosition, this);
vector<string> sliderNames;
project->GetSliderList(sliderNames);

for (auto s : sliderNames) {
statusBar->SetStatusText("Exporting " + s);
string fn = dir;
fn += "\\" + s + ".obj";
project->SaveSliderOBJ(s, activeItem->shapeName, fn);

}
statusBar->SetStatusText("Ready!");
}

Expand Down

0 comments on commit 3e9f682

Please sign in to comment.