Maya Converter Created v 0.1

I made this!

I have developed simple file that converts .obj file format exported from Maya to format that can be in easy way imported to my framework, processed and displayed! current version allows to import whole object with texture! This simple but really flexible script allows me to import any model exported to .Obj to the framework/engine.. how cool is that?!

some code below:

loading file and counts
[cc lang=”cpp”]
ifstream fin;
char input;

// counts.
vertexCount = 0;
textureCount = 0;
normalCount = 0;
faceCount = 0;

// open the file.
fin.open(filename);

// was successful in opening the file?
if(fin.fail() == true)
{
return false;
}

// beginning of the vertices section.
fin.get(input);
while(input != ‘v’)
{
// remainder of the line.
while(input != ‘\n’)
{
fin.get(input);
}

// start reading next line.
fin.get(input);
}

// vertices, texture coord, and normals counts
while(input == ‘v’)
{
fin.get(input);

if(input == ‘ ‘) { vertexCount++; }
if(input == ‘t’) { textureCount++; }
if(input == ‘n’) { normalCount++; }

// the line.
while(input != ‘\n’)
{
fin.get(input);
}

fin.get(input);
}

// number faces.
while(!fin.eof())
{
if(input == ‘f’) { faceCount++; }

// the line.
while(input != ‘\n’)
{
fin.get(input);
}

fin.get(input);
}

// Close the file.
fin.close();
[/cc]

now file processor:
[cc lang=”cpp”]
VertexType *vertices, *texcoords, *normals;
FaceType *faces;
ifstream fin;
int vertexIndex, texcoordIndex, normalIndex, faceIndex, vIndex, tIndex, nIndex;
char input, input2;
ofstream fout;

// Init the data structures.
vertices = new VertexType[vertexCount];
if(!vertices)
{
return false;
}

texcoords = new VertexType[textureCount];
if(!texcoords)
{
return false;
}

normals = new VertexType[normalCount];
if(!normals)
{
return false;
}

faces = new FaceType[faceCount];
if(!faces)
{
return false;
}

// the indexes.
vertexIndex = 0;
texcoordIndex = 0;
normalIndex = 0;
faceIndex = 0;

// open the file.
fin.open(filename);

// opened?
if(fin.fail() == true)
{
return false;
}

// parse of the vertices section.
fin.get(input);
while(input != ‘v’)
{
// the line.
while(input != ‘\n’)
{
fin.get(input);
}

// read the next line.
fin.get(input);
}

// Read vertices, texture coordinates, and normals into the data structures.
// + convert to left hand coordinate system.
while(input == ‘v’)
{
fin.get(input);

// read in the vertices.
if(input == ‘ ‘)
{
fin >> vertices[vertexIndex].x >> vertices[vertexIndex].y >> vertices[vertexIndex].z;

// Invert the Z vertex to left hand system.
vertices[vertexIndex].z = vertices[vertexIndex].z * -1.0f;

vertexIndex++;
}

// Read texture coords.
if(input == ‘t’)
{
fin >> texcoords[texcoordIndex].x >> texcoords[texcoordIndex].y;

// change the V texture coordinates to left hand system.
texcoords[texcoordIndex].y = 1.0f – texcoords[texcoordIndex].y;

texcoordIndex++;
}

// read normals.
if(input == ‘n’)
{
fin >> normals[normalIndex].x >> normals[normalIndex].y >> normals[normalIndex].z;

// normal to left hand system.
normals[normalIndex].z = normals[normalIndex].z * -1.0f;

normalIndex++;
}

// read line.
while(input != ‘\n’)
{
fin.get(input);
}

// next line.
fin.get(input);
}

//number of face indexes.
while(!fin.eof())
{
if(input == ‘f’)
{
// Read backwards – to a left hand system.
fin >> faces[faceIndex].vIndex3 >> input2 >> faces[faceIndex].tIndex3 >> input2 >> faces[faceIndex].nIndex3
>> faces[faceIndex].vIndex2 >> input2 >> faces[faceIndex].tIndex2 >> input2 >> faces[faceIndex].nIndex2
>> faces[faceIndex].vIndex1 >> input2 >> faces[faceIndex].tIndex1 >> input2 >> faces[faceIndex].nIndex1;
faceIndex++;
}

// the line.
while(input != ‘\n’)
{
fin.get(input);
}

// next line.
fin.get(input);
}

// close.
fin.close();

// output file.
fout.open(“model.txt”);

// header.
fout << "Vertex Count: " << (faceCount * 3) << endl; fout << endl; fout << "Data:" << endl; fout << endl; for(int i=0; i

Leave a Reply

Your email address will not be published. Required fields are marked *