Skip to content

Instantly share code, notes, and snippets.

@marty1885
Created January 7, 2018 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marty1885/fea00d06520af2ea928bfad1ac95f476 to your computer and use it in GitHub Desktop.
Save marty1885/fea00d06520af2ea928bfad1ac95f476 to your computer and use it in GitHub Desktop.
#include <assimp/Importer.hpp> // C++ importer interface
#include <assimp/scene.h> // Output data structure
#include <assimp/postprocess.h> // Post processing flags
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class MaterialInfo
{
public:
string name;
};
std::ostream &operator<<(std::ostream &os, aiColor3D const &col)
{
return os << col[0] << " " << col[1] << " " << col[2];
}
int main(int argc, char** argv)
{
if(argc != 3)
{
cout << "Usage : model2pbrt <inputModel> <output>" << endl;
return 0;
}
const string outFileName = argv[2];
const string inFileName = argv[1];
fstream file;
file.open(outFileName, ios::out);
if(!file)
{
cout << "Error : Cannot open/create file " << outFileName << endl;
return 1;
}
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile( inFileName.c_str(),
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType |
aiProcess_GenSmoothNormals);
if(scene == NULL)
{
cout << "Faild to load model " << inFileName << "." << endl;
return 0;
}
//Output material data
vector<MaterialInfo> materials;
for(int i=0 ;i<scene->mNumMaterials;i++)
{
const aiMaterial* material = scene->mMaterials[i];
aiString name;
aiString texturePath;
string textureName;
bool textured = false;
if(material->Get(AI_MATKEY_NAME, name) != AI_SUCCESS)
{
cout << "Error : Material must be named" << endl;
return 0;
}
if(material->GetTextureCount(aiTextureType_DIFFUSE) > 0)
{
if(material->GetTexture(aiTextureType_DIFFUSE, 0
, &texturePath, NULL, NULL, NULL, NULL, NULL)
== AI_SUCCESS)
{
textured = true;
textureName = string(name.C_Str())+string("_texture_Kd");
file << "Texture \"" << textureName << "\" "
<< "\"color\" \"imagemap\" \"string filename\" "
<< "\"" << texturePath.C_Str() << "\"" << endl;
}
}
aiColor3D diffuseColor(1,1,1);
material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor);
file << "MakeNamedMaterial " << "\"" << name.C_Str() << "\" ";
file << "\"string type\" \"matte\" "
<< "\"rgb Kd\" [" << diffuseColor << "] ";
if(textured)
file << "\"texture Kd\" " << "\"" << textureName << "\"";
MaterialInfo materialInfo;
materialInfo.name = name.C_Str();
materials.push_back(materialInfo);
file << endl;
}
//Output the mesh data
int meshCount = scene->mNumMeshes;
for(int i=0;i<meshCount;i++)
{
aiMesh* mesh = scene->mMeshes[i];
int meshFaces = mesh->mNumFaces;
int verticesNum = mesh->mNumVertices;
int materialIndex = mesh->mMaterialIndex;
file << "NamedMaterial \"" << materials[materialIndex].name << "\"" << endl;
file << "Shape \"trianglemesh\" " << endl;
file << "\t\"point P\" " << "[";
for(int j=0;j<verticesNum;j++)
{
for(int k=0;k<3;k++)
file << mesh->mVertices[j][k] << " ";
}
file << "]" << endl;
file << "\t\"integer indices\" " << "[";
for(int j=0;j<meshFaces;j++)
{
for(int k=0;k<3;k++)
file << mesh->mFaces[j].mIndices[k] << " ";
}
file << "]" << endl;
if(mesh->HasNormals() == true)
{
file << "\t\"normal N\" " << "[";
for(int j=0;j<verticesNum;j++)
{
for(int k=0;k<3;k++)
file << mesh->mNormals[j][k] << " ";
}
file << "]" << endl;
}
if(mesh->HasTextureCoords(0))
{
file << "\t\"float uv\" " << "[";
for(int j=0;j<verticesNum;j++)
{
for(int k=0;k<2;k++)
file << mesh->mTextureCoords[0][j][k] << " ";
}
file << "]" << endl;
}
}
file.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment