Added ability to read texture coordinates from 3DS files.
parent
d8b97ddc2f
commit
fee71fd05f
|
@ -7,14 +7,19 @@
|
|||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
|
||||
#include <iostream.h>
|
||||
#include "gl.h"
|
||||
#include "3dsmesh.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
#if 0
|
||||
static TriangleList* convertTriangleMesh(M3DTriangleMesh& mesh,
|
||||
const M3DScene& scene);
|
||||
#endif
|
||||
static VertexList* convertToVertexList(M3DTriangleMesh& mesh,
|
||||
const M3DScene& scene);
|
||||
|
||||
Mesh3DS::Mesh3DS(const M3DScene& scene)
|
||||
{
|
||||
|
@ -28,8 +33,8 @@ Mesh3DS::Mesh3DS(const M3DScene& scene)
|
|||
M3DTriangleMesh* mesh = model->getTriMesh(j);
|
||||
if (mesh != NULL)
|
||||
{
|
||||
triLists.insert(triLists.end(),
|
||||
convertTriangleMesh(*mesh, scene));
|
||||
vertexLists.insert(vertexLists.end(),
|
||||
convertToVertexList(*mesh, scene));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +44,7 @@ Mesh3DS::Mesh3DS(const M3DScene& scene)
|
|||
|
||||
Mesh3DS::~Mesh3DS()
|
||||
{
|
||||
for (TriListVec::iterator i = triLists.begin(); i != triLists.end(); i++)
|
||||
for (VertexListVec::iterator i = vertexLists.begin(); i != vertexLists.end(); i++)
|
||||
if (*i != NULL)
|
||||
delete *i;
|
||||
}
|
||||
|
@ -47,7 +52,7 @@ Mesh3DS::~Mesh3DS()
|
|||
|
||||
void Mesh3DS::render()
|
||||
{
|
||||
for (TriListVec::iterator i = triLists.begin(); i != triLists.end(); i++)
|
||||
for (VertexListVec::iterator i = vertexLists.begin(); i != vertexLists.end(); i++)
|
||||
(*i)->render();
|
||||
}
|
||||
|
||||
|
@ -56,7 +61,7 @@ void Mesh3DS::normalize()
|
|||
{
|
||||
AxisAlignedBox bbox;
|
||||
|
||||
for (TriListVec::iterator i = triLists.begin(); i != triLists.end(); i++)
|
||||
for (VertexListVec::iterator i = vertexLists.begin(); i != vertexLists.end(); i++)
|
||||
bbox.include((*i)->getBoundingBox());
|
||||
|
||||
Point3f center = bbox.getCenter();
|
||||
|
@ -69,20 +74,25 @@ void Mesh3DS::normalize()
|
|||
|
||||
printf("Normalize: %f\n", maxExtent);
|
||||
|
||||
for (i = triLists.begin(); i != triLists.end(); i++)
|
||||
for (i = vertexLists.begin(); i != vertexLists.end(); i++)
|
||||
(*i)->transform(Point3f(0, 0, 0) - center, 1.0f / maxExtent);
|
||||
}
|
||||
|
||||
|
||||
static TriangleList* convertTriangleMesh(M3DTriangleMesh& mesh,
|
||||
static VertexList* convertToVertexList(M3DTriangleMesh& mesh,
|
||||
const M3DScene& scene)
|
||||
{
|
||||
TriangleList* tl = new TriangleList();
|
||||
int nFaces = mesh.getFaceCount();
|
||||
int nVertices = mesh.getVertexCount();
|
||||
int nTexCoords = mesh.getTexCoordCount();
|
||||
bool smooth = (mesh.getSmoothingGroupCount() == nFaces);
|
||||
int i;
|
||||
|
||||
uint32 parts = VertexList::VertexNormal;
|
||||
if (nTexCoords == nVertices)
|
||||
parts |= VertexList::TexCoord0;
|
||||
VertexList* vl = new VertexList(parts);
|
||||
|
||||
Vec3f* faceNormals = new Vec3f[nFaces];
|
||||
Vec3f* vertexNormals = new Vec3f[nFaces * 3];
|
||||
int* faceCounts = new int[nVertices];
|
||||
|
@ -196,12 +206,21 @@ static TriangleList* convertTriangleMesh(M3DTriangleMesh& mesh,
|
|||
// build the triangle list
|
||||
for (i = 0; i < nFaces; i++)
|
||||
{
|
||||
uint16 v0, v1, v2;
|
||||
mesh.getFace(i, v0, v1, v2);
|
||||
uint16 triVert[3];
|
||||
mesh.getFace(i, triVert[0], triVert[1], triVert[2]);
|
||||
|
||||
tl->addTriangle(mesh.getVertex(v0), vertexNormals[i * 3],
|
||||
mesh.getVertex(v1), vertexNormals[i * 3 + 1],
|
||||
mesh.getVertex(v2), vertexNormals[i * 3 + 2]);
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
VertexList::Vertex v;
|
||||
v.point = mesh.getVertex(triVert[j]);
|
||||
v.normal = vertexNormals[i * 3 + j];
|
||||
if ((parts & VertexList::TexCoord0) != 0)
|
||||
{
|
||||
v.texCoords[0] = mesh.getTexCoord(triVert[j]);
|
||||
cout << "vt " << v.texCoords[0].x << ',' << v.texCoords[0].y << '\n';
|
||||
}
|
||||
vl->addVertex(v);
|
||||
}
|
||||
}
|
||||
|
||||
// set the color
|
||||
|
@ -209,7 +228,6 @@ static TriangleList* convertTriangleMesh(M3DTriangleMesh& mesh,
|
|||
string materialName = mesh.getMaterialName();
|
||||
if (materialName.length() > 0)
|
||||
{
|
||||
tl->setColorMode(1);
|
||||
int nMaterials = scene.getMaterialCount();
|
||||
for (i = 0; i < nMaterials; i++)
|
||||
{
|
||||
|
@ -217,7 +235,7 @@ static TriangleList* convertTriangleMesh(M3DTriangleMesh& mesh,
|
|||
if (materialName == material->getName())
|
||||
{
|
||||
M3DColor diffuse = material->getDiffuseColor();
|
||||
tl->setColor(Vec3f(diffuse.red, diffuse.green, diffuse.blue));
|
||||
vl->setDiffuseColor(Color(diffuse.red, diffuse.green, diffuse.blue));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -241,5 +259,5 @@ static TriangleList* convertTriangleMesh(M3DTriangleMesh& mesh,
|
|||
delete[] vertexFaces;
|
||||
}
|
||||
|
||||
return tl;
|
||||
return vl;
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
|
||||
#include <vector>
|
||||
#include "mesh.h"
|
||||
#include "trilist.h"
|
||||
#include "vertexlist.h"
|
||||
#include "3dsmodel.h"
|
||||
|
||||
|
||||
|
@ -26,8 +26,8 @@ class Mesh3DS : public Mesh
|
|||
void normalize();
|
||||
|
||||
private:
|
||||
typedef std::vector<TriangleList*> TriListVec;
|
||||
TriListVec triLists;
|
||||
typedef std::vector<VertexList*> VertexListVec;
|
||||
VertexListVec vertexLists;
|
||||
};
|
||||
|
||||
#endif // _3DSMESH_H_
|
||||
|
|
|
@ -105,6 +105,21 @@ void M3DTriangleMesh::addVertex(Point3f p)
|
|||
points.insert(points.end(), p);
|
||||
}
|
||||
|
||||
Point2f M3DTriangleMesh::getTexCoord(uint16 n) const
|
||||
{
|
||||
return texCoords[n];
|
||||
}
|
||||
|
||||
uint16 M3DTriangleMesh::getTexCoordCount() const
|
||||
{
|
||||
return (uint16) (texCoords.size());
|
||||
}
|
||||
|
||||
void M3DTriangleMesh::addTexCoord(Point2f p)
|
||||
{
|
||||
texCoords.insert(texCoords.end(), p);
|
||||
}
|
||||
|
||||
void M3DTriangleMesh::getFace(uint16 n, uint16& v0, uint16& v1, uint16& v2) const
|
||||
{
|
||||
int m = (int) n * 3;
|
||||
|
|
|
@ -62,6 +62,10 @@ class M3DTriangleMesh
|
|||
uint16 getVertexCount() const;
|
||||
void addVertex(Point3f);
|
||||
|
||||
Point2f getTexCoord(uint16) const;
|
||||
uint16 getTexCoordCount() const;
|
||||
void addTexCoord(Point2f);
|
||||
|
||||
void getFace(uint16, uint16&, uint16&, uint16&) const;
|
||||
uint16 getFaceCount() const;
|
||||
void addFace(uint16, uint16, uint16);
|
||||
|
@ -75,6 +79,7 @@ class M3DTriangleMesh
|
|||
|
||||
private:
|
||||
std::vector<Point3f> points;
|
||||
std::vector<Point2f> texCoords;
|
||||
std::vector<uint16> faces;
|
||||
std::vector<uint32> smoothingGroups;
|
||||
Mat4f matrix;
|
||||
|
|
|
@ -276,10 +276,12 @@ Mat4f readMeshMatrix(ifstream& in, int nBytes)
|
|||
float m31 = readFloat(in);
|
||||
float m32 = readFloat(in);
|
||||
|
||||
#if 0
|
||||
cout << m00 << " " << m01 << " " << m02 << '\n';
|
||||
cout << m10 << " " << m11 << " " << m12 << '\n';
|
||||
cout << m20 << " " << m21 << " " << m22 << '\n';
|
||||
cout << m30 << " " << m31 << " " << m32 << '\n';
|
||||
#endif
|
||||
|
||||
return Mat4f(Vec4f(m00, m01, m02, 0),
|
||||
Vec4f(m10, m11, m12, 0),
|
||||
|
@ -311,6 +313,19 @@ void readPointArray(ifstream& in, M3DTriangleMesh* triMesh)
|
|||
}
|
||||
|
||||
|
||||
void readTextureCoordArray(ifstream& in, M3DTriangleMesh* triMesh)
|
||||
{
|
||||
uint16 nPoints = readUshort(in);
|
||||
|
||||
for (int i = 0; i < (int) nPoints; i++)
|
||||
{
|
||||
float u = readFloat(in);
|
||||
float v = readFloat(in);
|
||||
triMesh->addTexCoord(Point2f(u, v));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool processFaceArrayChunk(ifstream& in,
|
||||
unsigned short chunkType,
|
||||
int contentSize,
|
||||
|
@ -390,6 +405,11 @@ bool processTriMeshChunk(ifstream& in,
|
|||
readPointArray(in, triMesh);
|
||||
return true;
|
||||
}
|
||||
else if (chunkType == M3DCHUNK_MESH_TEXTURE_COORDS)
|
||||
{
|
||||
readTextureCoordArray(in, triMesh);
|
||||
return true;
|
||||
}
|
||||
else if (chunkType == M3DCHUNK_FACE_ARRAY)
|
||||
{
|
||||
readFaceArray(in, triMesh, contentSize);
|
||||
|
@ -585,7 +605,7 @@ M3DScene* Read3DSFile(ifstream& in)
|
|||
}
|
||||
|
||||
|
||||
M3DScene* Read3DSFile(string filename)
|
||||
M3DScene* Read3DSFile(const string& filename)
|
||||
{
|
||||
ifstream in(filename.c_str(), ios::in | ios::binary);
|
||||
if (!in.good())
|
||||
|
@ -602,23 +622,6 @@ M3DScene* Read3DSFile(string filename)
|
|||
}
|
||||
|
||||
|
||||
M3DScene* Read3DSFile(char* filename)
|
||||
{
|
||||
ifstream in(filename, ios::in | ios::binary);
|
||||
if (!in.good())
|
||||
{
|
||||
cerr << "Error opening " << filename << '\n';
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
M3DScene* scene = Read3DSFile(in);
|
||||
in.close();
|
||||
return scene;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
#include "3dsmodel.h"
|
||||
|
||||
M3DScene* Read3DSFile(std::ifstream& in);
|
||||
M3DScene* Read3DSFile(std::string filename);
|
||||
M3DScene* Read3DSFile(char* filename);
|
||||
M3DScene* Read3DSFile(const std::string& filename);
|
||||
// M3DScene* Read3DSFile(char* filename);
|
||||
|
||||
#endif // _3DSREAD_H_
|
||||
|
|
Loading…
Reference in New Issue