implemented converting static meshes
This commit is contained in:
parent
32d207184a
commit
fb15ed80cb
|
@ -80,7 +80,9 @@
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="src\assimputils\assimpgeometry.cpp" />
|
||||||
<ClCompile Include="src\assimputils\assimpsceneinfo.cpp" />
|
<ClCompile Include="src\assimputils\assimpsceneinfo.cpp" />
|
||||||
|
<ClCompile Include="src\convert\meshutils.cpp" />
|
||||||
<ClCompile Include="src\convert\skeletalanimated.cpp" />
|
<ClCompile Include="src\convert\skeletalanimated.cpp" />
|
||||||
<ClCompile Include="src\convert\static.cpp" />
|
<ClCompile Include="src\convert\static.cpp" />
|
||||||
<ClCompile Include="src\main.cpp" />
|
<ClCompile Include="src\main.cpp" />
|
||||||
|
@ -88,8 +90,13 @@
|
||||||
<ClCompile Include="src\utils\fileutils.cpp" />
|
<ClCompile Include="src\utils\fileutils.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="src\assimputils\types.h" />
|
||||||
<ClInclude Include="src\assimputils\utils.h" />
|
<ClInclude Include="src\assimputils\utils.h" />
|
||||||
<ClInclude Include="src\convert\convert.h" />
|
<ClInclude Include="src\convert\convert.h" />
|
||||||
|
<ClInclude Include="src\convert\mesh.h" />
|
||||||
|
<ClInclude Include="src\convert\meshmaterial.h" />
|
||||||
|
<ClInclude Include="src\convert\meshtriangle.h" />
|
||||||
|
<ClInclude Include="src\convert\submesh.h" />
|
||||||
<ClInclude Include="src\nodetree\nodetree.h" />
|
<ClInclude Include="src\nodetree\nodetree.h" />
|
||||||
<ClInclude Include="src\utils\utils.h" />
|
<ClInclude Include="src\utils\utils.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -33,6 +33,12 @@
|
||||||
<ClCompile Include="src\nodetree\nodetree.cpp">
|
<ClCompile Include="src\nodetree\nodetree.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\convert\meshutils.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\assimputils\assimpgeometry.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="src\utils\utils.h">
|
<ClInclude Include="src\utils\utils.h">
|
||||||
|
@ -47,5 +53,20 @@
|
||||||
<ClInclude Include="src\nodetree\nodetree.h">
|
<ClInclude Include="src\nodetree\nodetree.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\convert\mesh.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\assimputils\types.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\convert\meshmaterial.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\convert\meshtriangle.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\convert\submesh.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
69
AssimpToMesh/src/assimputils/assimpgeometry.cpp
Normal file
69
AssimpToMesh/src/assimputils/assimpgeometry.cpp
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
// determine if a vertex is already contained in the bucket, if so matchingIndex will contain the index of the matching vertex
|
||||||
|
bool FindVertex(const aiVector3D &vertex, const AssimpVertices &bucket, unsigned int &matchingIndex)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < bucket.size(); ++i)
|
||||||
|
{
|
||||||
|
if (vertex == bucket[i])
|
||||||
|
{
|
||||||
|
matchingIndex = i;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CollectVerticesInMesh(
|
||||||
|
const aiMesh *mesh,
|
||||||
|
AssimpVertices &verticesBucket,
|
||||||
|
AssimpVertices &normalsBucket,
|
||||||
|
AssimpVertices &texCoordsBucket,
|
||||||
|
AssimpVertexIndices &indexMapping)
|
||||||
|
{
|
||||||
|
indexMapping.resize(mesh->mNumVertices);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < mesh->mNumVertices; ++i)
|
||||||
|
{
|
||||||
|
unsigned int matchingIndex = 0;
|
||||||
|
aiVector3D vertex = mesh->mVertices[i];
|
||||||
|
|
||||||
|
if (!FindVertex(vertex, verticesBucket, matchingIndex))
|
||||||
|
{
|
||||||
|
// vertex isn't in the bucket already, add it, and then record mapping between the mesh vertex index and the bucket vertex index
|
||||||
|
verticesBucket.push_back(vertex);
|
||||||
|
matchingIndex = verticesBucket.size() - 1;
|
||||||
|
|
||||||
|
// also copy the normal and tex coord into the appropriate buckets
|
||||||
|
// TODO: check if mesh has normals and/or tex coords first!
|
||||||
|
aiVector3D normal = mesh->mNormals[i];
|
||||||
|
normalsBucket.push_back(normal);
|
||||||
|
aiVector3D texCoord = mesh->mTextureCoords[0][i];
|
||||||
|
texCoordsBucket.push_back(texCoord);
|
||||||
|
}
|
||||||
|
|
||||||
|
// stating the obvious so when i come back here and read this, i know instantly
|
||||||
|
// what the fuck is going on (sad, I know):
|
||||||
|
// i = index into mesh's separate vertices list
|
||||||
|
// matchingIndex = index into bucket's vertices list
|
||||||
|
indexMapping[i] = matchingIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CollectAllMeshVertices(
|
||||||
|
const aiScene *scene,
|
||||||
|
AssimpVertices &verticesBucket,
|
||||||
|
AssimpVertices &normalsBucket,
|
||||||
|
AssimpVertices &texCoordsBucket,
|
||||||
|
AssimpVerticesMap &indexMapping)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < scene->mNumMeshes; ++i)
|
||||||
|
{
|
||||||
|
aiMesh *mesh = scene->mMeshes[i];
|
||||||
|
AssimpVertexIndices indexes;
|
||||||
|
CollectVerticesInMesh(mesh, verticesBucket, normalsBucket, texCoordsBucket, indexes);
|
||||||
|
|
||||||
|
indexMapping[i] = indexes;
|
||||||
|
}
|
||||||
|
}
|
13
AssimpToMesh/src/assimputils/types.h
Normal file
13
AssimpToMesh/src/assimputils/types.h
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#ifndef __ASSIMPUTILS_TYPES_H_INCLUDED__
|
||||||
|
#define __ASSIMPUTILS_TYPES_H_INCLUDED__
|
||||||
|
|
||||||
|
#include <aiTypes.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
typedef std::vector<aiVector3D> AssimpVertices;
|
||||||
|
|
||||||
|
typedef std::vector<unsigned int> AssimpVertexIndices;
|
||||||
|
typedef std::map<unsigned int, AssimpVertexIndices> AssimpVerticesMap;
|
||||||
|
|
||||||
|
#endif
|
|
@ -4,8 +4,35 @@
|
||||||
#include <assimp.hpp>
|
#include <assimp.hpp>
|
||||||
#include <aiScene.h>
|
#include <aiScene.h>
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
bool IsSceneAnimated(const aiScene *scene);
|
bool IsSceneAnimated(const aiScene *scene);
|
||||||
bool IsSceneSkeletal(const aiScene *scene);
|
bool IsSceneSkeletal(const aiScene *scene);
|
||||||
bool IsSceneStatic(const aiScene *scene);
|
bool IsSceneStatic(const aiScene *scene);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This will go through each mesh in the scene, and store all the vertices it
|
||||||
|
* finds into one common bucket, building up a mapping as it goes so that,
|
||||||
|
* we can easily determine what mesh index and vertex index as stored by ASSIMP
|
||||||
|
* corresponds to what vertex in the common bucket. That is, after this, given:
|
||||||
|
*
|
||||||
|
* aiScene *scene = ...
|
||||||
|
* aiMesh *mesh = scene->mMeshes[meshIndex];
|
||||||
|
* unsigned int bucketIndex = mesh->mVertices[3];
|
||||||
|
* ... and/or ...
|
||||||
|
* unsigned int bucketIndex = mesh->mFaces[10]->mIndices[0];
|
||||||
|
* aiVector3D vertex = bucket[bucketIndex];
|
||||||
|
*
|
||||||
|
* where "bucketIndex" is the index into one of the bucket lists
|
||||||
|
*/
|
||||||
|
void CollectAllMeshVertices(
|
||||||
|
const aiScene *scene,
|
||||||
|
AssimpVertices &verticesBucket,
|
||||||
|
AssimpVertices &normalsBucket,
|
||||||
|
AssimpVertices &texCoordsBucket,
|
||||||
|
AssimpVerticesMap &indexMapping);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
20
AssimpToMesh/src/convert/mesh.h
Normal file
20
AssimpToMesh/src/convert/mesh.h
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef __CONVERT_MESH_H_INCLUDED__
|
||||||
|
#define __CONVERT_MESH_H_INCLUDED__
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <aiTypes.h>
|
||||||
|
#include "../assimputils/types.h"
|
||||||
|
#include "meshmaterial.h"
|
||||||
|
#include "meshtriangle.h"
|
||||||
|
#include "submesh.h"
|
||||||
|
|
||||||
|
void WriteMeshHeader(FILE *fp);
|
||||||
|
void WriteVertices(const AssimpVertices &vertices, FILE *fp);
|
||||||
|
void WriteNormals(const AssimpVertices &normals, FILE *fp);
|
||||||
|
void WriteTexCoords(const AssimpVertices &texCoords, FILE *fp);
|
||||||
|
void WriteMaterials(const std::vector<MeshMaterial> &materials, FILE *fp);
|
||||||
|
void WriteTriangles(const std::vector<MeshTriangle> &triangles, FILE *fp);
|
||||||
|
void WriteSubMeshes(const std::vector<SubMesh> &subMeshes, FILE *fp);
|
||||||
|
|
||||||
|
#endif
|
63
AssimpToMesh/src/convert/meshmaterial.h
Normal file
63
AssimpToMesh/src/convert/meshmaterial.h
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
#ifndef __CONVERT_MESHMATERIAL_H_INCLUDED__
|
||||||
|
#define __CONVERT_MESHMATERIAL_H_INCLUDED__
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <aiMaterial.h>
|
||||||
|
|
||||||
|
struct MeshMaterial
|
||||||
|
{
|
||||||
|
MeshMaterial()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
MeshMaterial(const aiMaterial *source)
|
||||||
|
{
|
||||||
|
aiString name;
|
||||||
|
aiColor3D ambient;
|
||||||
|
aiColor3D diffuse;
|
||||||
|
aiColor3D specular;
|
||||||
|
aiColor3D emissive;
|
||||||
|
float shininess;
|
||||||
|
float opacity;
|
||||||
|
|
||||||
|
source->Get(AI_MATKEY_NAME, name);
|
||||||
|
source->Get(AI_MATKEY_COLOR_AMBIENT, ambient);
|
||||||
|
source->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse);
|
||||||
|
source->Get(AI_MATKEY_COLOR_SPECULAR, specular);
|
||||||
|
source->Get(AI_MATKEY_COLOR_EMISSIVE, emissive);
|
||||||
|
source->Get(AI_MATKEY_SHININESS, shininess);
|
||||||
|
source->Get(AI_MATKEY_OPACITY, opacity);
|
||||||
|
|
||||||
|
this->name = std::string(name.data, name.length);
|
||||||
|
|
||||||
|
this->ambient[0] = ambient.r;
|
||||||
|
this->ambient[1] = ambient.g;
|
||||||
|
this->ambient[2] = ambient.b;
|
||||||
|
|
||||||
|
this->diffuse[0] = diffuse.r;
|
||||||
|
this->diffuse[1] = diffuse.g;
|
||||||
|
this->diffuse[2] = diffuse.b;
|
||||||
|
|
||||||
|
this->specular[0] = specular.r;
|
||||||
|
this->specular[1] = specular.g;
|
||||||
|
this->specular[2] = specular.b;
|
||||||
|
|
||||||
|
this->emissive[0] = emissive.r;
|
||||||
|
this->emissive[1] = emissive.g;
|
||||||
|
this->emissive[2] = emissive.b;
|
||||||
|
|
||||||
|
this->shininess = shininess;
|
||||||
|
|
||||||
|
this->opacity = opacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string name;
|
||||||
|
float ambient[3];
|
||||||
|
float diffuse[3];
|
||||||
|
float specular[3];
|
||||||
|
float emissive[3];
|
||||||
|
float shininess;
|
||||||
|
float opacity;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
12
AssimpToMesh/src/convert/meshtriangle.h
Normal file
12
AssimpToMesh/src/convert/meshtriangle.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef __CONVERT_MESHTRIANGLE_H_INCLUDED__
|
||||||
|
#define __CONVERT_MESHTRIANGLE_H_INCLUDED__
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
struct MeshTriangle
|
||||||
|
{
|
||||||
|
uint32_t vertices[3];
|
||||||
|
uint32_t subMesh;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
194
AssimpToMesh/src/convert/meshutils.cpp
Normal file
194
AssimpToMesh/src/convert/meshutils.cpp
Normal file
|
@ -0,0 +1,194 @@
|
||||||
|
#include "mesh.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void WriteMeshHeader(FILE *fp)
|
||||||
|
{
|
||||||
|
fputs("MESH", fp);
|
||||||
|
uint8_t version = 1;
|
||||||
|
fwrite(&version, 1, 1, fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteVertices(const AssimpVertices &vertices, FILE *fp)
|
||||||
|
{
|
||||||
|
uint32_t count = vertices.size();
|
||||||
|
if (count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
uint32_t size =
|
||||||
|
(sizeof(float) * 3) * // x y, z
|
||||||
|
vertices.size()
|
||||||
|
+ 4; // chunk size
|
||||||
|
|
||||||
|
fputs("VTX", fp);
|
||||||
|
fwrite(&size, 4, 1, fp);
|
||||||
|
fwrite(&count, 4, 1, fp);
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < count; ++i)
|
||||||
|
{
|
||||||
|
fwrite(&vertices[i].x, sizeof(float), 1, fp);
|
||||||
|
fwrite(&vertices[i].y, sizeof(float), 1, fp);
|
||||||
|
fwrite(&vertices[i].z, sizeof(float), 1, fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteNormals(const AssimpVertices &normals, FILE *fp)
|
||||||
|
{
|
||||||
|
uint32_t count = normals.size();
|
||||||
|
if (count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
uint32_t size =
|
||||||
|
(sizeof(float) * 3) * // x y, z
|
||||||
|
normals.size()
|
||||||
|
+ 4; // chunk size
|
||||||
|
|
||||||
|
fputs("NRL", fp);
|
||||||
|
fwrite(&size, 4, 1, fp);
|
||||||
|
fwrite(&count, 4, 1, fp);
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < count; ++i)
|
||||||
|
{
|
||||||
|
fwrite(&normals[i].x, sizeof(float), 1, fp);
|
||||||
|
fwrite(&normals[i].y, sizeof(float), 1, fp);
|
||||||
|
fwrite(&normals[i].z, sizeof(float), 1, fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteTexCoords(const AssimpVertices &texCoords, FILE *fp)
|
||||||
|
{
|
||||||
|
uint32_t count = texCoords.size();
|
||||||
|
if (count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
uint32_t size =
|
||||||
|
(sizeof(float) * 2) * // u, v
|
||||||
|
texCoords.size()
|
||||||
|
+ 4; // count
|
||||||
|
|
||||||
|
fputs("TXT", fp);
|
||||||
|
fwrite(&size, 4, 1, fp);
|
||||||
|
fwrite(&count, 4, 1, fp);
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < count; ++i)
|
||||||
|
{
|
||||||
|
fwrite(&texCoords[i].x, sizeof(float), 1, fp);
|
||||||
|
fwrite(&texCoords[i].y, sizeof(float), 1, fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteMaterials(const std::vector<MeshMaterial> &materials, FILE *fp)
|
||||||
|
{
|
||||||
|
uint32_t count = materials.size();
|
||||||
|
if (count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
uint32_t size = 4 + // count
|
||||||
|
(
|
||||||
|
(
|
||||||
|
(sizeof(float) * 3) + // ambient
|
||||||
|
(sizeof(float) * 3) + // diffuse
|
||||||
|
(sizeof(float) * 3) + // specular
|
||||||
|
(sizeof(float) * 3) + // emissive
|
||||||
|
sizeof(float) + // shininess
|
||||||
|
sizeof(float) // opacity
|
||||||
|
) * count
|
||||||
|
);
|
||||||
|
|
||||||
|
// add up all the variable length string sizes for the material names
|
||||||
|
for (uint32_t i = 0; i < count; ++i)
|
||||||
|
size += materials[i].name.length() + 1; // include null terminator
|
||||||
|
|
||||||
|
fputs("MTL", fp);
|
||||||
|
fwrite(&size, 4, 1, fp);
|
||||||
|
fwrite(&count, 4, 1, fp);
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < count; ++i)
|
||||||
|
{
|
||||||
|
const MeshMaterial *m = &materials[i];
|
||||||
|
|
||||||
|
fwrite(m->name.c_str(), m->name.length(), 1, fp);
|
||||||
|
char ch = '\0';
|
||||||
|
fwrite(&ch, 1, 1, fp);
|
||||||
|
|
||||||
|
fwrite(&m->ambient[0], sizeof(float), 1, fp);
|
||||||
|
fwrite(&m->ambient[1], sizeof(float), 1, fp);
|
||||||
|
fwrite(&m->ambient[2], sizeof(float), 1, fp);
|
||||||
|
|
||||||
|
fwrite(&m->diffuse[0], sizeof(float), 1, fp);
|
||||||
|
fwrite(&m->diffuse[1], sizeof(float), 1, fp);
|
||||||
|
fwrite(&m->diffuse[2], sizeof(float), 1, fp);
|
||||||
|
|
||||||
|
fwrite(&m->specular[0], sizeof(float), 1, fp);
|
||||||
|
fwrite(&m->specular[1], sizeof(float), 1, fp);
|
||||||
|
fwrite(&m->specular[2], sizeof(float), 1, fp);
|
||||||
|
|
||||||
|
fwrite(&m->emissive[0], sizeof(float), 1, fp);
|
||||||
|
fwrite(&m->emissive[1], sizeof(float), 1, fp);
|
||||||
|
fwrite(&m->emissive[2], sizeof(float), 1, fp);
|
||||||
|
|
||||||
|
fwrite(&m->shininess, sizeof(float), 1, fp);
|
||||||
|
|
||||||
|
fwrite(&m->opacity, sizeof(float), 1, fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteTriangles(const std::vector<MeshTriangle> &triangles, FILE *fp)
|
||||||
|
{
|
||||||
|
uint32_t count = triangles.size();
|
||||||
|
if (count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
uint32_t size = 4; // count
|
||||||
|
size +=
|
||||||
|
(
|
||||||
|
(4 * 3) // vertex indices
|
||||||
|
+ 4 // material index
|
||||||
|
) * count;
|
||||||
|
|
||||||
|
fputs("TRI", fp);
|
||||||
|
fwrite(&size, 4, 1, fp);
|
||||||
|
fwrite(&count, 4, 1, fp);
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < count; ++i)
|
||||||
|
{
|
||||||
|
const MeshTriangle *t = &triangles[i];
|
||||||
|
|
||||||
|
fwrite(&t->vertices[0], 4, 1, fp);
|
||||||
|
fwrite(&t->vertices[1], 4, 1, fp);
|
||||||
|
fwrite(&t->vertices[2], 4, 1, fp);
|
||||||
|
|
||||||
|
fwrite(&t->subMesh, 4, 1, fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteSubMeshes(const std::vector<SubMesh> &subMeshes, FILE *fp)
|
||||||
|
{
|
||||||
|
uint32_t count = subMeshes.size();
|
||||||
|
if (count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
uint32_t size = 4 + // count
|
||||||
|
4 + // material index
|
||||||
|
4; // count of triangles
|
||||||
|
|
||||||
|
// add up all the variable length string sizes for the submesh names
|
||||||
|
for (uint32_t i = 0; i < count; ++i)
|
||||||
|
size += subMeshes[i].name.length() + 1; // include null terminator
|
||||||
|
|
||||||
|
fputs("GRP", fp);
|
||||||
|
fwrite(&size, 4, 1, fp);
|
||||||
|
fwrite(&count, 4, 1, fp);
|
||||||
|
for (uint32_t i = 0; i < count; ++i)
|
||||||
|
{
|
||||||
|
const SubMesh *m = &subMeshes[i];
|
||||||
|
|
||||||
|
fwrite(m->name.c_str(), m->name.length(), 1, fp);
|
||||||
|
char c = '\0';
|
||||||
|
fwrite(&c, 1, 1, fp);
|
||||||
|
|
||||||
|
fwrite(&m->material, 4, 1, fp);
|
||||||
|
fwrite(&m->numTriangles, 4, 1, fp);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,73 @@
|
||||||
#include "convert.h"
|
#include "convert.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
#include "mesh.h"
|
||||||
|
#include "../assimputils/utils.h"
|
||||||
|
|
||||||
void ConvertStatic(const std::string &outfile, const aiScene *scene)
|
void ConvertStatic(const std::string &outfile, const aiScene *scene)
|
||||||
{
|
{
|
||||||
|
FILE *fp = fopen(outfile.c_str(), "wb");
|
||||||
|
if (fp == NULL)
|
||||||
|
throw std::exception("Error creating output file.");
|
||||||
|
|
||||||
|
WriteMeshHeader(fp);
|
||||||
|
|
||||||
|
// basic mesh info
|
||||||
|
AssimpVertices vertices;
|
||||||
|
AssimpVertices normals;
|
||||||
|
AssimpVertices texCoords;
|
||||||
|
AssimpVerticesMap vertexIndicesMap;
|
||||||
|
CollectAllMeshVertices(scene, vertices, normals, texCoords, vertexIndicesMap);
|
||||||
|
WriteVertices(vertices, fp);
|
||||||
|
WriteNormals(normals, fp);
|
||||||
|
WriteTexCoords(texCoords, fp);
|
||||||
|
|
||||||
|
// materials
|
||||||
|
std::vector<MeshMaterial> materials;
|
||||||
|
for (unsigned int i = 0; i < scene->mNumMaterials; ++i)
|
||||||
|
{
|
||||||
|
MeshMaterial material = MeshMaterial(scene->mMaterials[i]);
|
||||||
|
materials.push_back(material);
|
||||||
|
}
|
||||||
|
WriteMaterials(materials, fp);
|
||||||
|
|
||||||
|
// triangles
|
||||||
|
std::vector<MeshTriangle> triangles;
|
||||||
|
for (unsigned int i = 0; i < scene->mNumMeshes; ++i)
|
||||||
|
{
|
||||||
|
aiMesh *mesh = scene->mMeshes[i];
|
||||||
|
for (unsigned int j = 0; j < mesh->mNumFaces; ++j)
|
||||||
|
{
|
||||||
|
aiFace *face = &mesh->mFaces[j];
|
||||||
|
|
||||||
|
MeshTriangle triangle;
|
||||||
|
triangle.vertices[0] = vertexIndicesMap[i][face->mIndices[0]];
|
||||||
|
triangle.vertices[1] = vertexIndicesMap[i][face->mIndices[1]];
|
||||||
|
triangle.vertices[2] = vertexIndicesMap[i][face->mIndices[2]];
|
||||||
|
triangle.subMesh = i;
|
||||||
|
|
||||||
|
triangles.push_back(triangle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WriteTriangles(triangles, fp);
|
||||||
|
|
||||||
|
// sub meshes
|
||||||
|
std::vector<SubMesh> subMeshes;
|
||||||
|
for (unsigned int i = 0; i < scene->mNumMeshes; ++i)
|
||||||
|
{
|
||||||
|
aiMesh *mesh = scene->mMeshes[i];
|
||||||
|
SubMesh subMesh;
|
||||||
|
|
||||||
|
subMesh.name = std::string(mesh->mName.data, mesh->mName.length);
|
||||||
|
subMesh.material = mesh->mMaterialIndex;
|
||||||
|
subMesh.numTriangles = mesh->mNumFaces;
|
||||||
|
|
||||||
|
subMeshes.push_back(subMesh);
|
||||||
|
}
|
||||||
|
WriteSubMeshes(subMeshes, fp);
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
14
AssimpToMesh/src/convert/submesh.h
Normal file
14
AssimpToMesh/src/convert/submesh.h
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#ifndef __CONVERT_SUBMESH_H_INCLUDED__
|
||||||
|
#define __CONVERT_SUBMESH_H_INCLUDED__
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct SubMesh
|
||||||
|
{
|
||||||
|
std::string name;
|
||||||
|
uint32_t material;
|
||||||
|
uint32_t numTriangles;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Reference in a new issue