00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "voidmeshimporter.h"
00023 #include <zeitgeist/logserver/logserver.h>
00024 #include <zeitgeist/fileserver/fileserver.h>
00025 #include <kerosin/sceneserver/helper/NVMeshMender.h>
00026
00027 using namespace zeitgeist;
00028 using namespace oxygen;
00029 using namespace std;
00030 using namespace boost;
00031
00032 VoidMeshImporter::VoidMeshImporter()
00033 {
00034 }
00035
00036 VoidMeshImporter::~VoidMeshImporter()
00037 {
00038 }
00039
00040 shared_ptr<TriMesh> VoidMeshImporter::ImportMesh
00041 (const string& name, const ParameterList& parameter)
00042 {
00043
00044 shared_ptr<FileServer> fileServer = shared_static_cast<FileServer>
00045 (GetCore()->Get("/sys/server/file"));
00046
00047 if (fileServer.get() == 0)
00048 {
00049 GetLog()->Error()
00050 << "(VoidMeshImporter) ERROR: cannot find FileServer\n";
00051
00052 return shared_ptr<TriMesh>();
00053 }
00054
00055 shared_ptr<salt::RFile> file = fileServer->Open(name);
00056
00057 if (file.get() == 0)
00058 {
00059 GetLog()->Error()
00060 << "(VoidMeshImporter) ERROR: cannot open file '" << name << "'\n";
00061 return shared_ptr<TriMesh>();
00062 }
00063
00064 NVMeshMender meshmender;
00065 NVMeshMender::VAVector input, output;
00066 NVMeshMender::VertexAttribute att;
00067
00068 char buffer[1024];
00069 int temp;
00070 int i;
00071
00072 GetLog()->Normal() << "(VoidMeshImporter) Loading " << name << "\n";
00073 file->Gets(buffer, 1024);
00074 sscanf(buffer, "TotalTriCount: %d", &temp);
00075 GetLog()->Normal() << "(VoidMeshImporter) TotalTriCount: "
00076 << temp << "\n";
00077
00078 int vertexCount;
00079 file->Gets(buffer, 1024);
00080 sscanf(buffer, "VertexCount: %d", &vertexCount);
00081 GetLog()->Normal() << "(VoidMeshImporter) VertexCount: "
00082 << vertexCount << "\n";
00083
00084 int meshCount;
00085 file->Gets(buffer, 1024);
00086 sscanf(buffer, "MeshCount: %d", &meshCount);
00087 GetLog()->Normal() << "(VoidMeshImporter) MeshCount: "
00088 << meshCount << "\n";
00089 file->Gets(buffer, 1024);
00090
00091
00092 NVMeshMender::VertexAttribute position;
00093 position.Name_ = "position";
00094
00095 NVMeshMender::VertexAttribute tex0;
00096 tex0.Name_ = "tex0";
00097
00098 for (int v = 0; v < vertexCount; ++v)
00099 {
00100 float x, y, z;
00101
00102 file->Gets(buffer, 1024);
00103 sscanf(buffer, "%f %f %f", &x, &y, &z);
00104 position.floatVector_.push_back(x);
00105 position.floatVector_.push_back(y);
00106 position.floatVector_.push_back(z);
00107
00108 file->Gets(buffer, 1024);
00109 sscanf(buffer, "%f %f", &x, &y);
00110 tex0.floatVector_.push_back(x);
00111 tex0.floatVector_.push_back(y);
00112 tex0.floatVector_.push_back(1);
00113 }
00114
00115 input.push_back(position);
00116 input.push_back(tex0);
00117
00118 file->Gets(buffer, 1024);
00119
00120
00121 NVMeshMender::VertexAttribute accumulatedIndices;
00122 vector<NVMeshMender::VertexAttribute> indices;
00123 indices.resize(meshCount);
00124
00125 NVMeshMender::VertexAttribute material;
00126 material.Name_ = "material";
00127 material.intVector_.resize(position.floatVector_.size());
00128
00129 accumulatedIndices.Name_ = "indices";
00130 for (int m = 0; m < meshCount; ++m)
00131 {
00132 int matId = 0;
00133
00134 file->Gets(buffer, 1024);
00135
00136 file->Gets(buffer, 1024);
00137 sscanf(buffer, " Material: %d", &matId);
00138
00139
00140 int faceCount;
00141 file->Gets(buffer, 1024);
00142 sscanf(buffer, " FaceCount: %d", &faceCount);
00143
00144
00145 file->Gets(buffer, 1024);
00146
00147 indices[m].Name_ = "indices";
00148 for(int f = 0; f < faceCount; ++f)
00149 {
00150 int a, b, c;
00151 file->Gets(buffer, 1024);
00152 sscanf(buffer, " %d %d %d", &a, &b, &c);
00153 indices[m].intVector_.push_back(a);
00154 material.intVector_[3*a] = matId;
00155 accumulatedIndices.intVector_.push_back(a);
00156 indices[m].intVector_.push_back(b);
00157 material.intVector_[3*b] = matId;
00158 accumulatedIndices.intVector_.push_back(b);
00159 indices[m].intVector_.push_back(c);
00160 material.intVector_[3*c] = matId;
00161 accumulatedIndices.intVector_.push_back(c);
00162 }
00163 file->Gets(buffer, 1024);
00164 }
00165
00166 input.push_back(accumulatedIndices);
00167 input.push_back(material);
00168
00169
00170 vector<string> materials;
00171 while (! file->Eof())
00172 {
00173 if (file->Gets(buffer, 1024)>0 && buffer[0] == 'M')
00174 {
00175 char matName[256];
00176 int dummy;
00177 sscanf(buffer, "Material %d: %s", &dummy, matName);
00178 GetLog()->Normal() << "(VoidMeshImporter) Material '"
00179 << matName << "'\n";
00180 materials.push_back(matName);
00181 }
00182 }
00183
00184 file->Close();
00185
00186
00187 att.Name_ = "position"; output.push_back(att);
00188 att.Name_ = "tex0"; output.push_back(att);
00189 att.Name_ = "normal"; output.push_back(att);
00190 att.Name_ = "indices"; output.push_back(att);
00191 att.Name_ = "material"; output.push_back(att);
00192
00193
00194 shared_ptr<TriMesh> triMesh(new TriMesh());
00195
00196 meshmender.Munge(input, output, 3.141592654f/3.0f, 0,
00197 NVMeshMender::FixTangents, NVMeshMender::DontFixCylindricalTexGen,
00198 NVMeshMender::WeightNormalsByFaceSize);
00199
00200 NVMeshMender::VertexAttribute outIndices;
00201 NVMeshMender::VertexAttribute outMaterial;
00202
00203 for (i=0; i < (int)output.size(); ++i)
00204 {
00205 string b = output[i].Name_;
00206
00207 if (output[i].Name_ == "position")
00208 {
00209 vertexCount = output[i].floatVector_.size()/3;
00210 shared_array<float> pos(new float[vertexCount * 3]);
00211 for (int j = 0; j < (vertexCount*3); ++j)
00212 {
00213 pos[j] = output[i].floatVector_[j];
00214 }
00215
00216 triMesh->SetPos(pos,vertexCount);
00217 }
00218
00219 if (output[i].Name_ == "tex0")
00220 {
00221 int size = output[i].floatVector_.size()/3;
00222 shared_array<float> texCoords(new float[size * 3]);
00223
00224 for (int j = 0; j < (size*3); ++j)
00225 {
00226 texCoords[j] = output[i].floatVector_[j];
00227 }
00228
00229 triMesh->SetTexCoords(texCoords);
00230 }
00231
00232 if (output[i].Name_ == "normal")
00233 {
00234 int size = output[i].floatVector_.size()/3;
00235 shared_array<float> normal(new float[size * 3]);
00236
00237 for (int j = 0; j < (size * 3); ++j)
00238 {
00239 normal[j] = output[i].floatVector_[j];
00240 }
00241
00242 triMesh->SetNormals(normal);
00243 }
00244
00245 if (output[i].Name_ == "indices")
00246 {
00247 outIndices = output[i];
00248 }
00249
00250 if (output[i].Name_ == "material")
00251 {
00252 outMaterial = output[i];
00253 }
00254 }
00255
00256 std::vector<int> faceCount;
00257 faceCount.resize(meshCount);
00258
00259 for (i = 0; i<meshCount; ++i)
00260 {
00261 faceCount[i] = 0;
00262 }
00263
00264 int totalFaceCount = outIndices.intVector_.size()/3;
00265 for (i = 0; i < totalFaceCount; ++i)
00266 {
00267 faceCount[outMaterial.intVector_[outIndices.intVector_[3*i]*3]]++;
00268 }
00269
00270 for (i = 0; i<meshCount; ++i)
00271 {
00272 shared_ptr<IndexBuffer> indeces(new IndexBuffer());
00273 indeces->EnsureFit(faceCount[i] * 3);
00274 unsigned int* faces = indeces->GetIndex().get();
00275
00276 for (int f = 0; f < totalFaceCount; ++f)
00277 {
00278 if (outMaterial.intVector_[outIndices.intVector_[3*f]*3]==i)
00279 {
00280
00281 int triBase = 3*f;
00282 indeces->Cache(outIndices.intVector_[triBase]);
00283 indeces->Cache(outIndices.intVector_[triBase+1]);
00284 indeces->Cache(outIndices.intVector_[triBase+2]);
00285 }
00286 }
00287
00288 triMesh->AddFace(indeces, materials[i]);
00289 }
00290
00291 return triMesh;
00292 }