Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2009-08-10 22:32:36 +0000
committerStijn Buys <ingar@osirion.org>2009-08-10 22:32:36 +0000
commitbd142a328328cdf0cbfbb59e4e0aa99dd51184b8 (patch)
treec2ed4e832e60b379723ea3f530c5b192bec0da61 /src/model/asefile.cc
parentbbd04914749b2e3918796fe331daf649b06163ea (diff)
initial texture support for ase models
Diffstat (limited to 'src/model/asefile.cc')
-rw-r--r--src/model/asefile.cc115
1 files changed, 105 insertions, 10 deletions
diff --git a/src/model/asefile.cc b/src/model/asefile.cc
index 7b574d7..f4f3342 100644
--- a/src/model/asefile.cc
+++ b/src/model/asefile.cc
@@ -33,6 +33,11 @@ ASEFile::ASEFile(std::string const &name)
ASEFile::~ASEFile()
{
+ for (VertexList::iterator it = ase_tvertexlist.begin(); it != ase_tvertexlist.end(); it++) {
+ delete (*it).second;
+ }
+ ase_tvertexlist.clear();
+
for (VertexList::iterator it = ase_vertexlist.begin(); it != ase_vertexlist.end(); it++) {
delete (*it).second;
}
@@ -126,7 +131,7 @@ bool ASEFile::read_mesh_face_list(std::istream &is)
line >> word;
if (word.compare("}") == 0) {
- con_debug << count << " mesh faces" << std::endl;
+ con_debug << " " << count << " mesh faces" << std::endl;
return true;
} else if ( word.compare("*MESH_FACE") == 0) {
@@ -160,6 +165,8 @@ bool ASEFile::read_mesh_normals(std::istream &is)
char data[1024];
memset(data, 0, sizeof(data));
+ size_t count = 0;
+
size_t index = 0;
size_t vertindex = 0;
float x, y, z;
@@ -172,6 +179,7 @@ bool ASEFile::read_mesh_normals(std::istream &is)
line >> firstword;
if (firstword.compare("}") == 0) {
+ con_debug << " " << count << " face normals" << std::endl;
return true;
} else if ( firstword.compare("*MESH_FACENORMAL") == 0) {
@@ -179,6 +187,7 @@ bool ASEFile::read_mesh_normals(std::istream &is)
it = ase_facelist.find(index);
if (it != ase_facelist.end()) {
(*it).second->normal().assign(x, y, z);
+ count++;
} else {
con_debug << " could not find face " << index << std::endl;
}
@@ -205,6 +214,76 @@ bool ASEFile::read_mesh_normals(std::istream &is)
return false;
}
+bool ASEFile::read_mesh_tvertex_list(std::istream &is)
+{
+ size_t count = 0;
+
+ char data[1024];
+ memset(data, 0, sizeof(data));
+
+ while (is.getline(data, sizeof(data) -1 )) {
+ std::istringstream line(data);
+
+ std::string firstword;
+ line >> firstword;
+
+ if (firstword.compare("}") == 0) {
+ con_debug << " " << count << " texture vertices" << std::endl;
+ return true;
+
+ } else if ( firstword.compare("*MESH_TVERT") == 0) {
+ size_t index;
+ float x, y, z;
+ if (line >> index >> x >> y >> z) {
+ math::Vector3f *v = new math::Vector3f(x, y, z);
+ ase_tvertexlist[index] = v;
+ count++;
+ }
+ }
+ }
+
+ return false;
+}
+
+bool ASEFile::read_mesh_tface_list(std::istream &is)
+{
+size_t count = 0;
+
+ char data[1024];
+ memset(data, 0, sizeof(data));
+
+ while (is.getline(data, sizeof(data) -1 )) {
+ std::istringstream line(data);
+
+ std::string firstword;
+ line >> firstword;
+
+ if (firstword.compare("}") == 0) {
+ con_debug << " " << count << " face texture coordinates" << std::endl;
+ return true;
+
+ } else if ( firstword.compare("*MESH_TFACE") == 0) {
+ size_t index, a, b, c;
+ if (line >> index >> a >> b >> c) {
+ Triangle *triangle = ase_facelist[index];
+ math::Vector3f *t0 = ase_tvertexlist[a];
+ math::Vector3f *t1 = ase_tvertexlist[b];
+ math::Vector3f *t2 = ase_tvertexlist[c];
+ if (triangle) {
+ if (t0)
+ triangle->t0().assign(t0->x, t0->y);
+ if (t1)
+ triangle->t1().assign(t1->x, t1->y);
+ if (t2)
+ triangle->t2().assign(t2->x, t2->y);
+ }
+ count++;
+ }
+ }
+ }
+
+ return false;
+}
bool ASEFile::read_mesh(std::istream &is)
{
@@ -238,6 +317,18 @@ bool ASEFile::read_mesh(std::istream &is)
read_mesh_normals(is);
}
+ } else if ((level == 1 ) && (word.compare("*MESH_TVERTLIST") == 0)) {
+ if ((line >> word) && (word.compare("{") == 0)) {
+ con_debug << " " << name() << " *MESH_TVERTLIST" << std::endl;
+ read_mesh_tvertex_list(is);
+ }
+
+ } else if ((level == 1 ) && (word.compare("*MESH_TFACELIST") == 0)) {
+ if ((line >> word) && (word.compare("{") == 0)) {
+ con_debug << " " << name() << " *MESH_TFACELIST" << std::endl;
+ read_mesh_tface_list(is);
+ }
+
} else {
do {
@@ -337,11 +428,12 @@ Model * ASEFile::load(const std::string &name)
Model *model = new Model(name);
// default material
- Material *material = Material::find("textures/common/entity");
+ Material *material = Material::find("models/" + name);
if (!material) {
- material = new Material("placeholder");
+ material = new Material("models/" + name);
Material::add(material);
- material->set_flags(Material::Primary);
+ material->set_flags(Material::Texture);
+ material->set_texture(material->name());
}
// a single fragment for all the model triangles
@@ -353,18 +445,21 @@ Model * ASEFile::load(const std::string &name)
// calculate model center
math::Vector3f center((asefile.ase_minbbox + asefile.ase_maxbbox) * 0.5f);
- model->model_minbbox = asefile.ase_minbbox - center;
- model->model_maxbbox = asefile.ase_maxbbox - center;
+ //const float scale = SCALE;
+ const float scale = 0.125f;
- model->model_radius = asefile.ase_minbbox.length();
+ // caculate bounding box
+ model->model_minbbox = (asefile.ase_minbbox - center) * scale;
+ model->model_maxbbox = (asefile.ase_maxbbox - center) * scale;
+ model->model_radius = model->model_maxbbox.length();
// load the model faces into the fragment
for (FaceList::iterator it = asefile.ase_facelist.begin(); it != asefile.ase_facelist.end(); it++) {
Triangle *triangle = (*it).second;
- fragment->add_vertex(triangle->v0() - center , triangle->n0(), false);
- fragment->add_vertex(triangle->v1() - center , triangle->n1(), false);
- fragment->add_vertex(triangle->v2() - center , triangle->n2(), false);
+ fragment->add_vertex((triangle->v0() - center) * scale , triangle->n0(), triangle->t0(), false);
+ fragment->add_vertex((triangle->v1() - center) * scale , triangle->n1(), triangle->t1(), false);
+ fragment->add_vertex((triangle->v2() - center) * scale , triangle->n2(), triangle->t2(), false);
model->model_tris_count++;
model->model_tris_detail_count++;