forked from jrbedard/3d-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mayaObjConverter.cpp
executable file
·175 lines (127 loc) · 5.12 KB
/
mayaObjConverter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright(c) 2005-2006. All Rights Reserved
// By Jean-René Bédard (https://github.com/jrbedard/3d-converter)
#include "StdAfx.h"
#include "objFile.h"
#include "mayaFile.h"
#include "mayaObjConverter.h"
CMayaObjConverter::CMayaObjConverter():
m_pMayaFile(NULL),
m_pOptions(NULL)
{
}
CMayaObjConverter::~CMayaObjConverter()
{
}
bool CMayaObjConverter::ParseOptions(const std::string& optionString)
{
m_pOptions = new CMayaObjOptions();
CToObjConverter::ParseOptions(optionString, m_pOptions);
return true;
}
// Convert a Maya file into an OBJ file
bool CMayaObjConverter::Convert(CFile* pOtherFile, CFile* pObjFile)
{
// Initialize MAYA -> OBJ
m_pMayaFile = InitConversion<CMayaFile>(pOtherFile, pObjFile);
OBJ_ASSERT(m_pMayaFile && m_pOptions);
if(!m_pMayaFile || !m_pOptions)
return false;
MSG_INFO("Converting Maya file format into OBJ file format...");
// Get the Obj group vector
CObjFile::GroupVector& groups = m_pObjFile->GetGroupVector();
// Get the Maya mesh vector
CMayaFile::MeshVector& meshes = m_pMayaFile->GetMeshVector();
CMayaFile::MeshVector::iterator meshIt;
CMayaFile::MeshVector::const_iterator meshEnd = meshes.end();
for(meshIt = meshes.begin(); meshIt != meshEnd; ++meshIt) // for each Maya mesh
{
std::string meshName(meshIt->GetMeshName());
// verify that it is a valid mesh
if(meshIt->GetPolyFaceVector().size() <= 0 )
continue;
MSG_DEBUG(meshName << "...");
// a group corresponds to a Maya mesh node
CObjFile::CGroup group(meshName);
// "Append" Vertex positions
{
Vector3DVector& mayaVertexVector = meshIt->GetVertexPositionVector();
Vector3DVector& objVertexVector = m_pObjFile->GetVertexPositionVector();
objVertexVector.reserve(mayaVertexVector.size());
objVertexVector.insert(objVertexVector.end(), mayaVertexVector.begin(), mayaVertexVector.end()); // Append new Vertices
}
// "Append" Texture coordinates
{
Vector3DVector& mayaUvVector = meshIt->GetTextureCoordVector();
Vector3DVector& objUvVector = m_pObjFile->GetTextureCoordVector();
objUvVector.reserve(mayaUvVector.size());
objUvVector.insert(objUvVector.end(), mayaUvVector.begin(), mayaUvVector.end()); // Append new UVs
}
// "Append" Normals
{
Vector3DVector& mayaNormalVector = meshIt->GetNormalVector();
Vector3DVector& objNormalVector = m_pObjFile->GetNormalVector();
objNormalVector.reserve(mayaNormalVector.size());
objNormalVector.insert(objNormalVector.end(), mayaNormalVector.begin(), mayaNormalVector.end()); // Append new Normals
}
// Faces
{
CMayaFile::CMesh::PolyFaceVector& polyFaces = meshIt->GetPolyFaceVector();
CMayaFile::CMesh::PolyFaceVector::iterator polyFaceIt;
CMayaFile::CMesh::PolyFaceVector::const_iterator polyFaceEnd = polyFaces.end();
group.GetFaceVector().reserve(polyFaces.size()); // reserve face vector
for(polyFaceIt = polyFaces.begin(); polyFaceIt != polyFaceEnd; ++polyFaceIt) // for each Maya face
{
CMayaFile::CMesh::CPolyFace::CFace mayaFace = polyFaceIt->GetFace();
CMayaFile::CMesh::CPolyFace::CMu mayaMu;
if(polyFaceIt->HasTextureCoordinate())
mayaMu = polyFaceIt->GetMu();
CObjFile::CFace objFace;
uint edgeCount = mayaFace.EdgeCount();
uint debugFace = polyFaceIt - polyFaces.begin();
int vIndex = 0;
std::pair< bool, uint > uvIndex;
std::pair< bool, uint > normalIndex;
for(uint v=0; v < edgeCount; ++v ) // for each vertex in maya polyface
{
vIndex = 0;
uvIndex = std::make_pair(false, 0);
normalIndex = std::make_pair(false, 0);
OBJ_ASSERT(v < mayaFace.GetEdgeVector().size());
int edgeIndex = mayaFace.GetEdgeVector()[v];
if(edgeIndex >= 0) // edge positive direction : take the first vertex of the edge
{
OBJ_ASSERT(edgeIndex < (int)meshIt->GetEdgeVector().size());
vIndex = (int)meshIt->GetEdgeVector()[edgeIndex][0] + 1; // OBJ is 1-based index
}
else // edge negative direction : make the edge index positive, decrement 1 and take the second vertex of the edge
{
OBJ_ASSERT(abs(edgeIndex)-1 <= (int)meshIt->GetEdgeVector().size()-1);
vIndex = (int)meshIt->GetEdgeVector()[abs(edgeIndex)-1][1] + 1; // OBJ is 1-based index
}
// UVs
if(polyFaceIt->HasTextureCoordinate())
uvIndex = std::make_pair(true, mayaMu.GetUvVector()[v] + 1); // absolute UV index, OBJ is 1-based index
// Normals
if(polyFaceIt->HasNormal())
{
OBJ_ASSERT(v < mayaFace.GetNormalVector().size());
normalIndex = std::make_pair(true, mayaFace.GetNormalVector()[v] + 1); // OBJ is 1-based index
}
// Add vertex to OBJ face
objFace.AddVertex( CObjFile::CVertex(vIndex, uvIndex, normalIndex) );
}
group.AddFace(objFace); // Add face to OBJ group
}
}
m_pObjFile->AddGroup(group); // Add group to OBJ file
}
// Materials
{
CMtlFile* pMtlFile = m_pObjFile->GetMaterialFile();
OBJ_ASSERT(pMtlFile);
if(!pMtlFile)
return true;
// Retreive and set material properties here
}
return true;
}