eus_assimp.cpp
Go to the documentation of this file.
00001 // assimp_devel
00002 #include <Importer.hpp>
00003 #include <Exporter.hpp>
00004 #include <postprocess.h>
00005 #include <scene.h>
00006 
00007 // convex decomposition
00008 #if COMPILE_CONVEX_DECOMPOSITION
00009 #include "NvConvexDecomposition.h"
00010 #endif
00011 
00012 //SDL image
00013 #if USE_SDL_IMAGE
00014 #include "SDL_image.h"
00015 #endif
00016 
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 #include <unistd.h>
00020 #include <string.h>
00021 #include <signal.h>
00022 #include <math.h>
00023 #include <time.h>
00024 #include <pthread.h>
00025 #include <setjmp.h>
00026 #include <errno.h>
00027 
00028 #include <list>
00029 #include <vector>
00030 #include <set>
00031 #include <string>
00032 #include <map>
00033 #include <sstream>
00034 #include <cstdio>
00035 #include <iostream>
00036 
00037 // for eus.h
00038 #define class   eus_class
00039 #define throw   eus_throw
00040 #define export  eus_export
00041 #define vector  eus_vector
00042 #define string  eus_string
00043 #define iostream eus_iostream
00044 #define complex  eus_complex
00045 
00046 #include "eus.h"
00047 extern "C" {
00048   pointer ___eus_assimp(register context *ctx, int n, pointer *argv, pointer env);
00049   void register_eus_assimp(){
00050     char modname[] = "___eus_assimp";
00051     return add_module_initializer(modname, (pointer (*)())___eus_assimp);}
00052 }
00053 
00054 #undef class
00055 #undef throw
00056 #undef export
00057 #undef vector
00058 #undef string
00059 #undef iostream
00060 #undef complex
00061 
00062 #define SIZE_T_MAX (std::numeric_limits<size_t>::max())
00063 
00064 static pointer K_VERTICES, K_NORMALS, K_INDICES, K_TEXCOORDS, K_TYPE, K_MATERIAL;
00065 static pointer K_LINES, K_TRIANGLES, K_QUADS, K_POLYGON, K_NAME;
00066 static pointer K_FILENAME, K_AMBIENT, K_DIFFUSE, K_SPECULAR, K_EMISSION, K_SHININESS, K_TRANSPARENCY;
00067 static pointer K_HEIGHT, K_WIDTH;
00068 
00069 static std::string primitiveType (unsigned int tp) {
00070  switch (tp) {
00071  case aiPrimitiveType_POINT:
00072    return "POINT";
00073    break;
00074  case aiPrimitiveType_LINE:
00075    return "LINE";
00076    break;
00077  case aiPrimitiveType_TRIANGLE:
00078    return "TRIANGLE";
00079    break;
00080  case aiPrimitiveType_POLYGON:
00081    return "POLYGON";
00082    break;
00083  }
00084  return "NO_TYPE";
00085 }
00086 
00087 static void printMesh(aiMesh *mesh, std::string prefix)
00088 {
00089   std::string name;
00090   name.assign(  mesh->mName.data, mesh->mName.length );
00091 
00092   std::cerr << ";;" << prefix << " Mesh name: " << name << std::endl;
00093 
00094   std::cerr << ";;" << prefix << "   Bones: " << mesh->mNumBones << std::endl;
00095   std::cerr << ";;" << prefix << "   Faces: " << mesh->mNumFaces << std::endl;
00096   std::cerr << ";;" << prefix << "   Vertices: " << mesh->mNumVertices << std::endl;
00097   std::cerr << ";;" << prefix << "   PType: " << primitiveType( mesh->mPrimitiveTypes ) << std::endl;
00098   std::cerr << ";;" << prefix << "   MaterialIndex: " << mesh->mMaterialIndex << std::endl;
00099 }
00100 
00101 static void printTransform(aiMatrix4x4 &transform, std::string prefix) {
00102   std::cerr << prefix << "  trans: " << transform.a1 << " " << transform.a2 << " " << transform.a3 << " " << transform.a4 << std::endl;
00103   std::cerr << prefix << "         " << transform.b1 << " " << transform.b2 << " " << transform.b3 << " " << transform.b4 << std::endl;
00104   std::cerr << prefix << "         " << transform.c1 << " " << transform.c2 << " " << transform.c3 << " " << transform.c4 << std::endl;
00105   std::cerr << prefix << "         " << transform.d1 << " " << transform.d2 << " " << transform.d3 << " " << transform.d4 << std::endl;
00106 }
00107 
00108 static void printNode(aiNode *node, std::string prefix)
00109 {
00110   std::string name;
00111   name.assign (node->mName.C_Str());
00112 
00113   fprintf (stderr,"%s Node name(%lX): %s", prefix.c_str(), (void *)node, name.c_str());
00114   std::cerr << std::endl;
00115   std::cerr << prefix << "    Meshes: " << node->mNumMeshes << " / ";
00116   for (unsigned int i = 0; i < node->mNumMeshes; i++ ) {
00117     std::cerr << " " << node->mMeshes[i];
00118   }
00119   std::cerr << std::endl;
00120   printTransform(node->mTransformation, prefix);
00121 
00122   std::cerr << prefix << "   Children: " << node->mNumChildren << std::endl;
00123   for (unsigned int i = 0; i < node->mNumChildren; i++ ) {
00124     printNode(node->mChildren[i], prefix + "  ");
00125   }
00126 }
00127 
00128 static void printMaterial(aiMaterial *material, unsigned int idx)
00129 {
00130   std::cerr << ";;  material: " << idx << std::endl;
00131 
00132   //float col[4];
00133   //aiReturn ret = material->Get(AI_MATKEY_COLOR_DIFFUSE, col, NULL);
00134   //std::cerr << "color : " << ret << " " << col[0] << " " << col[1] << " " << col[2] << " " << col[3];
00135 
00136   for ( unsigned int i = 0; i < material->mNumProperties; i++ ) {
00137     aiMaterialProperty *mp = material->mProperties[i];
00138     std::string kname;
00139     kname.assign(  mp->mKey.data, mp->mKey.length );
00140     std::cerr << ";;    " << kname << std::endl;
00141   }
00142 }
00143 
00144 static pointer store_mesh_info (register context *ctx, eusfloat_t base_scl,
00145                                 const aiScene *scene, const aiNode *node, const aiMesh *amesh,
00146                                 const aiMatrix4x4 &trans, int direc) {
00147   static int mesh_cntr = -1;
00148   pointer ver_mat, nom_mat, indices, tex_cds;
00149   eusfloat_t *ver_vec = NULL, *nom_vec = NULL, *tex_vec = NULL;
00150   numunion nu;
00151   pointer ret = NIL;
00152   int npc = 0;
00153 
00154   mesh_cntr++;
00155   // name
00156   {
00157     std::string nname, mname;
00158     nname.assign (node->mName.C_Str());
00159     mname.assign (amesh->mName.C_Str());
00160 
00161     if (nname.empty() && mname.empty()) {
00162       nname = "eusglvertices_" + mesh_cntr;
00163     } else {
00164       nname = nname + "_" + mname;
00165     }
00166     pointer lname = makestring ((char *)nname.c_str(), nname.length());
00167     vpush (lname);
00168     lname = rawcons (ctx, lname , NIL);
00169     vpop (); vpush (lname);
00170     lname = rawcons (ctx, K_NAME, lname);
00171     vpop (); vpush (lname);
00172     ret = rawcons (ctx, lname, ret);
00173     vpop ();
00174     vpush (ret); npc++;
00175   }
00176 
00177   // type
00178   {
00179     pointer ltype = NIL;
00180     switch (amesh->mPrimitiveTypes) {
00181     case aiPrimitiveType_LINE:
00182       ltype = K_LINES;
00183       break;
00184     case aiPrimitiveType_TRIANGLE:
00185       ltype = K_TRIANGLES;
00186       break;
00187     case aiPrimitiveType_POLYGON:
00188       ltype = K_POLYGON;
00189       break;
00190     }
00191     ltype = rawcons (ctx, ltype , NIL);
00192     vpush (ltype);
00193     ltype = rawcons (ctx, K_TYPE, ltype);
00194     vpop(); vpush (ltype);
00195     ret = rawcons (ctx, ltype, ret);
00196     vpop();
00197     vpush (ret); npc++;
00198   }
00199 
00200   // material
00201   {
00202     pointer lmaterial = NIL;
00203     aiMaterial *am = scene->mMaterials[amesh->mMaterialIndex];
00204 #if DEBUG
00205     std::cerr << ";; material properties: " << am->mNumProperties << std::endl;
00206 #endif
00207     aiReturn ar;
00208     aiString s;
00209     aiColor4D clr4d( 0.0, 0.0, 0.0, 0.0);
00210     float val;
00211     int lpc = 0;
00212     ar = am->Get (AI_MATKEY_NAME, s);
00213     if (ar == aiReturn_SUCCESS) {
00214 #if DEBUG
00215       std::string str; str.assign(s.C_Str());
00216       std::cerr << ";; material properties: name: " << str << std::endl;
00217 #endif
00218       pointer pelem;
00219       pointer eusstr = makestring (s.data, strlen(s.data));
00220       vpush (eusstr);//
00221       pelem = rawcons (ctx, eusstr, NIL);
00222       vpush (pelem);//
00223       pelem = rawcons (ctx, K_NAME, pelem);
00224       vpush (pelem);//
00225       lmaterial = rawcons (ctx, pelem, lmaterial);
00226       vpop(); vpop(); vpop();
00227       vpush (lmaterial); lpc++;
00228     }
00229     ar = am->Get (AI_MATKEY_COLOR_AMBIENT, clr4d);
00230     if (ar == aiReturn_SUCCESS) {
00231 #if DEBUG
00232       std::cerr << ";; material properties: AMBIENT: ";
00233       std::cerr << clr4d[0] << " " << clr4d[1] << " " << clr4d[2] << " " << clr4d[3] << std::endl;
00234 #endif
00235       pointer pelem = makefvector (4);
00236       pelem->c.fvec.fv[0] = clr4d[0]; pelem->c.fvec.fv[1] = clr4d[1];
00237       pelem->c.fvec.fv[2] = clr4d[2]; pelem->c.fvec.fv[3] = clr4d[3];
00238       vpush (pelem);//
00239       pelem = rawcons (ctx, pelem, NIL);
00240       vpush (pelem);//
00241       pelem = rawcons (ctx, K_AMBIENT, pelem);
00242       vpush (pelem);//
00243       lmaterial = rawcons (ctx, pelem, lmaterial);
00244       vpop(); vpop(); vpop();
00245       vpush (lmaterial); lpc++;
00246     }
00247     ar = am->Get (AI_MATKEY_COLOR_DIFFUSE, clr4d);
00248     if (ar == aiReturn_SUCCESS) {
00249 #if DEBUG
00250       std::cerr << ";; material properties: DIFFUSE: ";
00251       std::cerr << clr4d[0] << " " << clr4d[1] << " " << clr4d[2] << " " << clr4d[3] << std::endl;
00252 #endif
00253       pointer pelem = makefvector (4);
00254       pelem->c.fvec.fv[0] = clr4d[0]; pelem->c.fvec.fv[1] = clr4d[1];
00255       pelem->c.fvec.fv[2] = clr4d[2]; pelem->c.fvec.fv[3] = clr4d[3];
00256       vpush (pelem);//
00257       pelem = rawcons (ctx, pelem, NIL);
00258       vpush (pelem);//
00259       pelem = rawcons (ctx, K_DIFFUSE, pelem);
00260       vpush (pelem);//
00261       lmaterial = rawcons (ctx, pelem, lmaterial);
00262       vpop(); vpop(); vpop();
00263       vpush (lmaterial); lpc++;
00264     }
00265     ar = am->Get (AI_MATKEY_COLOR_SPECULAR, clr4d);
00266     if (ar == aiReturn_SUCCESS) {
00267 #if DEBUG
00268       std::cerr << ";; material properties: SPECULAR: ";
00269       std::cerr << clr4d[0] << " " << clr4d[1] << " " << clr4d[2] << " " << clr4d[3] << std::endl;
00270 #endif
00271       pointer pelem = makefvector (4);
00272       pelem->c.fvec.fv[0] = clr4d[0]; pelem->c.fvec.fv[1] = clr4d[1];
00273       pelem->c.fvec.fv[2] = clr4d[2]; pelem->c.fvec.fv[3] = clr4d[3];
00274       vpush (pelem);//
00275       pelem = rawcons (ctx, pelem, NIL);
00276       vpush (pelem);//
00277       pelem = rawcons (ctx, K_SPECULAR, pelem);
00278       vpush (pelem);//
00279       lmaterial = rawcons (ctx, pelem, lmaterial);
00280       vpop(); vpop(); vpop();
00281       vpush (lmaterial); lpc++;
00282     }
00283     ar = am->Get (AI_MATKEY_COLOR_EMISSIVE, clr4d);
00284     if (ar == aiReturn_SUCCESS) {
00285 #if DEBUG
00286       std::cerr << ";; material properties: EMISSIVE: ";
00287       std::cerr << clr4d[0] << " " << clr4d[1] << " " << clr4d[2] << " " << clr4d[3] << std::endl;
00288 #endif
00289       pointer pelem = makefvector (4);
00290       pelem->c.fvec.fv[0] = clr4d[0]; pelem->c.fvec.fv[1] = clr4d[1];
00291       pelem->c.fvec.fv[2] = clr4d[2]; pelem->c.fvec.fv[3] = clr4d[3];
00292       vpush (pelem);//
00293       pelem = rawcons (ctx, pelem, NIL);
00294       vpush (pelem);//
00295       pelem = rawcons (ctx, K_EMISSION, pelem);
00296       vpush (pelem);//
00297       lmaterial = rawcons (ctx, pelem, lmaterial);
00298       vpop(); vpop(); vpop();
00299       vpush (lmaterial); lpc++;
00300     }
00301     ar = am->Get (AI_MATKEY_SHININESS, val);
00302     if (ar == aiReturn_SUCCESS) {
00303 #if DEBUG
00304       std::cerr << ";; material properties: SHININESS: " << val << std::endl;
00305 #endif
00306       pointer pelem;
00307       pelem = rawcons (ctx, makeflt(val), NIL);
00308       vpush (pelem);//
00309       pelem = rawcons (ctx, K_SHININESS, pelem);
00310       vpush (pelem);//
00311       lmaterial = rawcons (ctx, pelem, lmaterial);
00312       vpop(); vpop();
00313       vpush (lmaterial); lpc++;
00314     }
00315 #if 0 // Do not use transparent??
00316     ar = am->Get (AI_MATKEY_COLOR_TRANSPARENT, clr4d);
00317     if (ar == aiReturn_SUCCESS) {
00318       std::cerr << ";; material properties: TRANSPARENT: ";
00319       std::cerr << clr4d[0] << " " << clr4d[1] << " " << clr4d[2] << " " << clr4d[3] << std::endl;
00320       pointer pelem = makefvector (4);
00321       pelem->c.fvec.fv[0] = clr4d[0]; pelem->c.fvec.fv[1] = clr4d[1];
00322       pelem->c.fvec.fv[2] = clr4d[2]; pelem->c.fvec.fv[3] = clr4d[3];
00323       vpush (pelem);//
00324       pelem = rawcons (ctx, pelem, NIL);
00325       vpush (pelem);//
00326       pelem = rawcons (ctx, K_TRANSPARENT, pelem);
00327       vpush (pelem);//
00328       lmaterial = rawcons (ctx, pelem, lmaterial);
00329       vpop(); vpop(); vpop();
00330       vpush (lmaterial); lpc++;
00331     }
00332 #endif
00333     for (unsigned int tex_count = 0; tex_count < am->GetTextureCount(aiTextureType_DIFFUSE); tex_count++) {
00334       aiString fpath; // filename
00335 #if DEBUG
00336       aiTextureMapping mapping;
00337       unsigned int uvindex;
00338       float blend;
00339       aiTextureOp op;
00340       aiTextureMapMode mapmode;
00341       ar = am->GetTexture(aiTextureType_DIFFUSE, tex_count, &fpath,
00342                           &mapping, &uvindex, &blend, &op, &mapmode);
00343 #else
00344       ar = am->GetTexture(aiTextureType_DIFFUSE, tex_count, &fpath);
00345 #endif
00346 
00347       if (ar == AI_SUCCESS) {
00348         pointer pelem;
00349         pointer eusstr = makestring (fpath.data, strlen(fpath.data));
00350         vpush (eusstr);//
00351         pelem = rawcons (ctx, eusstr, NIL);
00352         vpush (pelem);//
00353         pelem = rawcons (ctx, K_FILENAME, pelem);
00354         vpush (pelem);//
00355         lmaterial = rawcons (ctx, pelem, lmaterial);
00356         vpop(); vpop(); vpop();
00357         vpush (lmaterial); lpc++;
00358 #if DEBUG
00359         std::cerr << ";; material properties: Texture: ";
00360         std::string str; str.assign(fpath.C_Str());
00361         std::cerr << "file: " << str << std::endl;
00362         std::cerr << " / uv_index: " << uvindex  << std::endl;
00363         std::cerr << " / blend: " << blend  << std::endl;
00364         std::cerr << " / Mapping: " << mapping  << std::endl;
00365         std::cerr << " / Op: " << op  << std::endl;
00366         std::cerr << " / Mode: " << mapmode  << std::endl;
00367 #endif
00368       }
00369     }
00370 
00371     // finalize material
00372     pointer tmp = rawcons (ctx, lmaterial, NIL);
00373     vpush (tmp); lpc++;
00374     tmp = rawcons (ctx, K_MATERIAL, tmp);
00375     vpush (tmp); lpc++;
00376     ret = rawcons (ctx, tmp, ret);
00377     while(lpc-- > 0) vpop();
00378 
00379     vpush (ret); npc++;
00380   }
00381 
00382   if (!!amesh->mNormals) {
00383     nom_mat = makematrix (ctx, amesh->mNumVertices, 3); vpush (nom_mat); npc++;
00384     nom_vec = nom_mat->c.ary.entity->c.fvec.fv;
00385   }
00386   ver_mat = makematrix (ctx, amesh->mNumVertices, 3); vpush (ver_mat); npc++;
00387   ver_vec = ver_mat->c.ary.entity->c.fvec.fv;
00388 
00389   // count indices
00390   int icount = 0;
00391   {
00392     for (unsigned int f = 0; f < amesh->mNumFaces; f++) {
00393       icount += amesh->mFaces[f].mNumIndices;
00394     }
00395     indices = makevector (C_INTVECTOR, icount);
00396     vpush (indices); npc++;
00397   }
00398   // fill indices
00399   {
00400     eusinteger_t *vec = indices->c.ivec.iv;
00401     int icount = 0;
00402     for (unsigned int f = 0; f < amesh->mNumFaces; f++) {
00403       for(unsigned int p = 0; p < amesh->mFaces[f].mNumIndices; p++) {
00404         vec[icount++] = amesh->mFaces[f].mIndices[p];
00405       }
00406     }
00407   }
00408   // add indices to return list
00409   {
00410     pointer tmp;
00411     tmp = rawcons (ctx, indices, NIL);
00412     vpush (tmp);
00413     tmp = rawcons (ctx, K_INDICES, tmp);
00414     ret = rawcons (ctx, tmp, ret);
00415     vpop();
00416     vpush (ret); npc++;
00417   }
00418   // texcoords
00419   {
00420     int texcount = 0;
00421     if (amesh->HasTextureCoords(texcount)) {
00422       tex_cds = makefvector (2 * amesh->mNumVertices);
00423       tex_vec = tex_cds->c.fvec.fv;
00424       for (unsigned int v = 0; v < amesh->mNumVertices; v++) {
00425         *tex_vec++ = amesh->mTextureCoords[texcount][v].x;
00426         *tex_vec++ = (1.0 - amesh->mTextureCoords[texcount][v].y); // why swap horizontal line
00427       }
00428     }
00429     if (!!tex_vec) {
00430       pointer tmp;
00431       tmp = rawcons (ctx, tex_cds, NIL);
00432       vpush (tmp);
00433       tmp = rawcons (ctx, K_TEXCOORDS, tmp);
00434       ret = rawcons (ctx, tmp, ret);
00435       vpop();
00436       vpush (ret); npc++;
00437     }
00438   }
00439 
00440   // make vetices and normal matrix
00441   int vcount=0, ncount=0;
00442   for (unsigned int i = 0; i < amesh->mNumVertices; ++i) {
00443     aiQuaternion rot;
00444     aiVector3D   pos;
00445     trans.DecomposeNoScaling(rot, pos);
00446     aiVector3D tvec (amesh->mVertices[i]);
00447     tvec *= trans;
00448     tvec *= base_scl;
00449     switch (direc) {
00450     case 0: // X_UP
00451       ver_vec[vcount++] = - tvec.z;
00452       ver_vec[vcount++] = tvec.y;
00453       ver_vec[vcount++] = tvec.x;
00454       break;
00455     case 1: // Y_UP
00456       ver_vec[vcount++] = tvec.x;
00457       ver_vec[vcount++] = - tvec.z;
00458       ver_vec[vcount++] = tvec.y;
00459       break;
00460     case 2: // Z_UP
00461       ver_vec[vcount++] = tvec.x;
00462       ver_vec[vcount++] = tvec.y;
00463       ver_vec[vcount++] = tvec.z;
00464       break;
00465     case 3: // -X_UP
00466       ver_vec[vcount++] = tvec.z;
00467       ver_vec[vcount++] = tvec.y;
00468       ver_vec[vcount++] = - tvec.x;
00469       break;
00470     case 4: // -Y_UP
00471       ver_vec[vcount++] = tvec.x;
00472       ver_vec[vcount++] = tvec.z;
00473       ver_vec[vcount++] = - tvec.y;
00474       break;
00475     }
00476 
00477     if ( !!nom_vec ) {
00478       aiVector3D tnom = rot.Rotate (amesh->mNormals[i]);
00479       switch (direc) {
00480       case 0:
00481         nom_vec[ncount++] = - tnom.z;
00482         nom_vec[ncount++] = tnom.y;
00483         nom_vec[ncount++] = tnom.x;
00484         break;
00485       case 1:
00486         nom_vec[ncount++] = tnom.x;
00487         nom_vec[ncount++] = - tnom.z;
00488         nom_vec[ncount++] = tnom.y;
00489         break;
00490       case 2:
00491         nom_vec[ncount++] = tnom.x;
00492         nom_vec[ncount++] = tnom.y;
00493         nom_vec[ncount++] = tnom.z;
00494         break;
00495       case 3:
00496         nom_vec[ncount++] = tnom.z;
00497         nom_vec[ncount++] = tnom.y;
00498         nom_vec[ncount++] = - tnom.x;
00499         break;
00500       case 4:
00501         nom_vec[ncount++] = tnom.x;
00502         nom_vec[ncount++] = tnom.z;
00503         nom_vec[ncount++] = - tnom.y;
00504         break;
00505       }
00506     }
00507   }
00508   //
00509   if ( !!nom_vec ) {
00510     pointer tmp;
00511     tmp = rawcons (ctx, nom_mat , NIL);
00512     vpush (tmp);
00513     tmp = rawcons (ctx, K_NORMALS , tmp);
00514     ret = rawcons (ctx, tmp, ret);
00515     vpop();
00516     vpush (ret); npc++;
00517   }
00518   //
00519   {
00520     pointer tmp;
00521     tmp = rawcons (ctx, ver_mat , NIL);
00522     vpush (tmp);
00523     tmp = rawcons (ctx, K_VERTICES , tmp);
00524     ret = rawcons (ctx, tmp, ret);
00525     vpop();
00526     vpush (ret); npc++;
00527   }
00528 
00529   for (;npc > 0; npc--) vpop();
00530 
00531   return ret;
00532 }
00533 
00534 static void register_all_nodes (register context *ctx, eusfloat_t base_scl,
00535                                 const aiScene *scene, const aiNode *node,
00536                                 const aiMatrix4x4 &parent_world_trans, int direc,
00537                                 std::vector<pointer> &mesh_info) {
00538   aiMatrix4x4 node_world_trans = parent_world_trans * node->mTransformation;
00539 #if DEBUG
00540   fprintf (stderr, "node : %lX\n", (void *)node);
00541   printTransform (node_world_trans, "");
00542 #endif
00543   if (node->mNumMeshes > 0 && node->mMeshes != NULL) {
00544     for (unsigned int n = 0; n < node->mNumMeshes; n++) {
00545       aiMesh *am = scene->mMeshes[node->mMeshes[n]];
00546 #if DEBUG
00547       fprintf (stderr, " mesh: %d", node->mMeshes[n]);
00548       std::cerr << ";; mesh = " << (void *)am << std::endl;
00549 #endif
00550       pointer ret = store_mesh_info (ctx, base_scl, scene, node, am, node_world_trans, direc);
00551       vpush (ret);
00552       mesh_info.push_back (ret);
00553     }
00554   }
00555 #if DEBUG
00556   fprintf(stderr, "\n");
00557   std::cerr << " children: " <<  node->mNumChildren << std::endl;
00558 #endif
00559   for (unsigned int c = 0; c < node->mNumChildren; c++) {
00560     register_all_nodes (ctx, base_scl,
00561                         scene, node->mChildren[c],
00562                         node_world_trans, direc, mesh_info);
00563   }
00564 }
00565 
00566 pointer GET_MESHES(register context *ctx,int n,pointer *argv)
00567 {
00568   /* filename &optional scale options direction dumpfilename */
00569   eusfloat_t base_scl = 1.0;
00570   numunion nu;
00571 
00572   ckarg2(1, 5);
00573   if (!isstring(argv[0])) error (E_NOSTRING);
00574   if (n > 1) {
00575     base_scl = ckfltval (argv[1]);
00576     //fprintf(stderr, ";; scale = %f\n", base_scl);
00577   }
00578 
00579   Assimp::Importer importer;
00580 
00581   { // ignore UP_DIRECTION tag in collada
00582     bool existing;
00583     importer.SetPropertyBool(AI_CONFIG_IMPORT_COLLADA_IGNORE_UP_DIRECTION, true, &existing);
00584     if(existing) {
00585       fprintf(stderr, ";; OverWrite : Ignore UP_DIRECTION", existing);
00586     }
00587   }
00588   unsigned int post_proc = (aiProcess_Triangulate | aiProcess_SortByPType);
00589   bool recalc_normal = false;
00590   char *dumpfile = NULL;
00591   int direction = 2; // :X_UP 0, :Y_UP 1, :Z_UP 2
00592 
00593   if (n > 2) {
00594     post_proc = ckintval(argv[2]);
00595     //fprintf(stderr, ";; option = %X\n", post_proc);
00596     if((post_proc & aiProcess_GenNormals) ||
00597        (post_proc & aiProcess_GenSmoothNormals)) {
00598       recalc_normal = true;
00599       if((post_proc & aiProcess_GenNormals) &&
00600          (post_proc & aiProcess_GenSmoothNormals)) {
00601         post_proc &= ~aiProcess_GenNormals;
00602       }
00603     }
00604   }
00605   if (n > 3) {
00606     direction = ckintval(argv[3]);
00607   }
00608   if (n > 4) {
00609     if (argv[4] != NIL) {
00610       if (!isstring(argv[4])) error(E_NOSTRING);
00611       dumpfile = (char *)get_string(argv[4]);
00612     }
00613   }
00614   const aiScene* raw_scene = importer.ReadFile ((char *)get_string(argv[0]), 0);
00615 
00616   if (!raw_scene) {
00617     std::string str (importer.GetErrorString());
00618     std::cerr << ";; " << str << std::endl;
00619     return NIL;
00620   }
00621 #if DEBUG
00622   std::cerr << ";; Num meshes(raw): " << raw_scene->mNumMeshes << std::endl;
00623 #endif
00624   if (recalc_normal) {
00625 #if DEBUG
00626     std::cerr << ";; Recalc_normal" << std::endl;
00627 #endif
00628     for (unsigned int m = 0; m < raw_scene->mNumMeshes; m++) {
00629       aiMesh *a = raw_scene->mMeshes[m];
00630       if (!!a->mNormals) { a->mNormals = NULL; }
00631     }
00632   }
00633 
00634   const aiScene* scene = importer.ApplyPostProcessing (post_proc);
00635   if (!scene) {
00636     std::string str (importer.GetErrorString());
00637     std::cerr << ";; " << str << std::endl;
00638     return NIL;
00639   }
00640 #if DEBUG
00641   std::cerr << ";; Num meshes: " << scene->mNumMeshes << std::endl;
00642   // travarse nodes and store
00643   printNode (scene->mRootNode, ";;");
00644 #endif
00645 
00646   pointer lst = NIL;
00647   {
00648     aiMatrix4x4 world_trans;
00649     std::vector<pointer> mesh_info;
00650     register_all_nodes (ctx, base_scl, scene,
00651                         scene->mRootNode, world_trans, direction, mesh_info);
00652 #if DEBUG
00653     std::cerr << ";; mesh size: " << mesh_info.size() << std::endl;
00654 #endif
00655     for (std::vector<pointer>::reverse_iterator rit = mesh_info.rbegin();
00656          rit != mesh_info.rend(); rit++) {
00657       vpush(lst);
00658       lst = rawcons (ctx, *rit, lst);
00659       vpop(); // vpop(); // pop for mesh_info
00660     }
00661     vpush (lst);
00662   }
00663 
00664   if (!!dumpfile) {
00665     std::string outf, outext;
00666     outf.assign (dumpfile);
00667     {
00668       const std::string::size_type s = outf.find_last_of('.');
00669       if (s != std::string::npos) {
00670         outext = outf.substr (s+1);
00671       } else {
00672         std::cerr << ";; Can not find extention: " << outf << std::endl;
00673       }
00674     }
00675     Assimp::Exporter exporter;
00676     size_t outfi = SIZE_T_MAX;
00677     for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
00678       const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
00679       if (outext == e->fileExtension) {
00680         outfi = i; break;
00681       }
00682     }
00683     if (outfi == SIZE_T_MAX) {
00684       outext = "stl";
00685       for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
00686         const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
00687         if (outext == e->fileExtension) {
00688           outfi = i; break;
00689         }
00690       }
00691     }
00692     if (outfi == SIZE_T_MAX) outfi = 0;
00693 
00694     const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(outfi);
00695 #if DEBUG
00696     fprintf (stderr, ";; use file format: %s\n", e->id);
00697 #endif
00698     aiReturn ret = exporter.Export (scene, e->id, outf);
00699   }
00700 
00701   vpop(); // vpush (lst);
00702   return lst;
00703 }
00704 
00705 pointer DUMP_GL_VERTICES(register context *ctx,int n,pointer *argv)
00706 {
00707   /* filename type-list material-list vertices-list normals-list texcoords-list indices-list
00708      (scale) (gen_normal nil) (smooth_normal nil) (split_large_mesh nil)
00709      (optimize_mesh nil) (identical_vert nil) (fix_normal) (direction)
00710   */
00711   // FIXME: name_list
00712   ckarg2(7, 14);
00713   numunion nu;
00714   int direction = 2;
00715   char *dumpfile = NULL;
00716   if (!isstring(argv[0])) error(E_NOSTRING);
00717   dumpfile = (char *)get_string(argv[0]);
00718 
00719   int list_len = 0;
00720   {
00721     pointer a = argv[1];
00722     while (islist(a)) {list_len++; a=ccdr(a);}
00723   }
00724   if (list_len <= 0) return NIL;
00725   bool has_materials = false;
00726   pointer ltype, linfo, lvertices, lnormals, ltexcoords, lindices;
00727   pointer mtype, minfo, mvertices, mnormals, mtexcoords, mindices;
00728   eusfloat_t scale = 1.0;
00729 
00730   ltype = argv[1];
00731   linfo = argv[2];
00732   lvertices = argv[3];
00733   lnormals = argv[4];
00734   ltexcoords = argv[5];
00735   lindices = argv[6];
00736   if (n > 7) {
00737     scale = fltval(argv[7]);
00738   }
00739   if (n > 13) {
00740     direction = intval(argv[13]);
00741   }
00742 
00743   // assimp loader
00744   aiScene *pScene = (aiScene *) malloc(sizeof (aiScene));
00745   memset((void *)pScene, 0, sizeof (aiScene));
00746   pScene->mPrivate = malloc(sizeof (void *) * 2);
00747   //printf("scene = %lX\n", (void *)pScene);
00748 
00749   pScene->mNumMeshes = list_len;
00750   pScene->mMeshes = new aiMesh*[list_len];
00751 
00752   // allocate a single node
00753   pScene->mRootNode = new aiNode();
00754   pScene->mRootNode->mNumMeshes = list_len;
00755   pScene->mRootNode->mMeshes = new unsigned int[list_len];
00756   pScene->mRootNode->mName.Set("eus_glvertices");
00757 
00758   if (linfo != NIL) {
00759     pScene->mNumMaterials = list_len;
00760     pScene->mMaterials = new aiMaterial*[list_len];
00761     has_materials = true;
00762   } else {
00763     pScene->mNumMaterials = 1;
00764     pScene->mMaterials = new aiMaterial*[1];
00765     has_materials = false;
00766     // make default material
00767     aiMaterial* pcMat = new aiMaterial();
00768     aiString s; s.Set (AI_DEFAULT_MATERIAL_NAME);
00769     pcMat->AddProperty (&s, AI_MATKEY_NAME);
00770     aiColor4D clrDiffuse(0.8f, 0.8f, 0.8f, 1.0f);
00771     pcMat->AddProperty (&clrDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
00772     pcMat->AddProperty (&clrDiffuse, 1, AI_MATKEY_COLOR_SPECULAR);
00773     clrDiffuse = aiColor4D (0.2f, 0.2f, 0.2f, 1.0f);
00774     pcMat->AddProperty (&clrDiffuse, 1, AI_MATKEY_COLOR_AMBIENT);
00775 
00776     pScene->mMaterials[0] = pcMat;
00777   }
00778 
00779   for (int i = 0; i < list_len; i++) {
00780     mtype = ccar (ltype);
00781     minfo = ccar (linfo);
00782     mvertices = ccar (lvertices);
00783     mnormals = ccar (lnormals);
00784     mtexcoords = ccar (ltexcoords);
00785     mindices = ccar (lindices);
00786 
00787     ltype = ccdr (ltype);
00788     linfo = ccdr (linfo);
00789     lvertices = ccdr (lvertices);
00790     lnormals = ccdr (lnormals);
00791     ltexcoords = ccdr (ltexcoords);
00792     lindices = ccdr (lindices);
00793 
00794     pScene->mRootNode->mMeshes[i] = i;
00795     aiMesh* pMesh = pScene->mMeshes[i] = new aiMesh();
00796     // std::cerr << ";; mesh = " << (void *)pMesh << std::endl;
00797     if (!has_materials) {
00798       pMesh->mMaterialIndex = 0;
00799     } else {
00800       pMesh->mMaterialIndex = i;
00801       // material
00802       aiMaterial* pcMat = new aiMaterial();
00803       std::ostringstream oss;
00804       oss << "eusassimp_mat_" << i;
00805       aiString s; s.Set (oss.str());
00806       pcMat->AddProperty(&s, AI_MATKEY_NAME);
00807 
00808       for (int el = 0; el < 7; el++) {
00809         // (list :ambient :diffuse :specular :emission :shininess :transparency :filename)
00810         pointer melem = ccar (minfo);
00811         minfo = ccdr (minfo);
00812         aiColor4D clr4d (0.0f, 0.0f, 0.0f, 1.0f);
00813         switch (el) {
00814         case 0:
00815           if (melem != NIL) {
00816             for(int j = 0; j < intval(melem->c.fvec.length); j++) {
00817               clr4d[j] = melem->c.fvec.fv[j];
00818             }
00819             //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
00820             pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_AMBIENT);
00821           }
00822           break;
00823         case 1:
00824           if (melem != NIL) {
00825             for(int j = 0; j < intval(melem->c.fvec.length); j++) {
00826               clr4d[j] = melem->c.fvec.fv[j];
00827             }
00828             //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
00829             pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_DIFFUSE);
00830           }
00831           break;
00832         case 2:
00833           if (melem != NIL) {
00834             for(int j = 0; j < intval(melem->c.fvec.length); j++) {
00835               clr4d[j] = melem->c.fvec.fv[j];
00836             }
00837             //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
00838             pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_SPECULAR);
00839           }
00840           break;
00841         case 3:
00842           if (melem != NIL) {
00843             for(int j = 0; j < intval(melem->c.fvec.length); j++) {
00844               clr4d[j] = melem->c.fvec.fv[j];
00845             }
00846             //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
00847             pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_EMISSIVE);
00848           }
00849           break;
00850         case 4:
00851           if (melem != NIL) {
00852             float val = fltval (melem);
00853             pcMat->AddProperty(&val, 1, AI_MATKEY_SHININESS);
00854           }
00855           break;
00856         case 5:
00857           if (melem != NIL) {
00858             if (isnum (melem)) {
00859               float val = isflt (melem) ? fltval (melem) : (float)intval (melem);
00860               //printf("trans: %f\n", val);
00861               for(int j = 0; j < 4; j++) {
00862                 clr4d[j] = val;
00863               }
00864             } else {
00865               for(int j = 0; j < intval(melem->c.fvec.length); j++) {
00866                 clr4d[j] = melem->c.fvec.fv[j];
00867               }
00868             }
00869             //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
00870             pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_TRANSPARENT);
00871           }
00872           break;
00873         case 6: // texture
00874           if (melem != NIL) {
00875             if (isstring(melem)) {
00876               //printf(";; texture file: %s\n", melem->c.str.chars);
00877               aiString s; s.Set ((const char *)(melem->c.str.chars));
00878               pcMat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(0));
00879             }
00880           }
00881           break;
00882         }
00883       }
00884       pScene->mMaterials[i] = pcMat;
00885     }
00886     // vertices
00887     if (mvertices == NIL) { /* error */ }
00888     eusinteger_t size = intval (mvertices->c.ary.entity->c.fvec.length);
00889     eusfloat_t *fv = mvertices->c.ary.entity->c.fvec.fv;
00890     pMesh->mNumVertices = size / 3;
00891     pMesh->mVertices = new aiVector3D [pMesh->mNumVertices];
00892     for (unsigned int k = 0; k < pMesh->mNumVertices; k++) {
00893       pMesh->mVertices[k].x = scale * *fv++;
00894       pMesh->mVertices[k].y = scale * *fv++;
00895       pMesh->mVertices[k].z = scale * *fv++;
00896     }
00897     // normals
00898     if (mnormals != NIL) {
00899       eusinteger_t size = intval (mnormals->c.ary.entity->c.fvec.length);
00900       eusfloat_t *fv = mnormals->c.ary.entity->c.fvec.fv;
00901       if (size / 3 != pMesh->mNumVertices) { /* error */ }
00902       pMesh->mNormals  = new aiVector3D [pMesh->mNumVertices];
00903       for (unsigned int k = 0; k < pMesh->mNumVertices; k++) {
00904         pMesh->mNormals[k].x = *fv++;
00905         pMesh->mNormals[k].y = *fv++;
00906         pMesh->mNormals[k].z = *fv++;
00907       }
00908     }
00909     // texcoords
00910     if (mtexcoords != NIL) {
00911       printf(";; add texture coords\n");
00912       eusinteger_t size = intval (mtexcoords->c.fvec.length);
00913       eusfloat_t *fv = mtexcoords->c.fvec.fv;
00914       if (size / 2 != pMesh->mNumVertices) { /* error */ }
00915       // use just 1 texture
00916       pMesh->mTextureCoords[0] = new aiVector3D [pMesh->mNumVertices];
00917       for (unsigned int k = 0; k < pMesh->mNumVertices; k++) {
00918         pMesh->mTextureCoords[0][k].x = *fv++;
00919         pMesh->mTextureCoords[0][k].y = (1.0 - *fv++);
00920       }
00921     }
00922 
00923     unsigned int verts_per_face = intval(mtype);
00924     if (mindices != NIL) {
00925       if (verts_per_face != 4 && verts_per_face != 3) {
00926         /* variable face size */
00927         // not implemented yet
00928       } else {
00929         eusinteger_t *iv = mindices->c.ivec.iv;
00930         eusinteger_t idlen = intval (mindices->c.ivec.length);
00931         pMesh->mNumFaces = idlen / verts_per_face;//
00932 
00933         pMesh->mFaces = new aiFace[pMesh->mNumFaces];
00934         for (unsigned int f = 0; f < pMesh->mNumFaces; f++) {
00935           aiFace& face = pMesh->mFaces[f];
00936           face.mNumIndices = verts_per_face;
00937           face.mIndices = new unsigned int[verts_per_face];
00938           for (unsigned int o = 0; o < verts_per_face; o++) {
00939             face.mIndices[o] = *iv++;
00940           }
00941         }
00942       }
00943     } else {
00944       // add indices
00945       if (verts_per_face != 4 && verts_per_face != 3) { /* error */ }
00946       pMesh->mNumFaces = pMesh->mNumVertices / verts_per_face;
00947       pMesh->mFaces = new aiFace[pMesh->mNumFaces];
00948       for (unsigned int f = 0, p = 0; f < pMesh->mNumFaces; f++) {
00949         aiFace& face = pMesh->mFaces[f];
00950         face.mNumIndices = verts_per_face;
00951         face.mIndices = new unsigned int[verts_per_face];
00952         for (unsigned int o = 0; o < verts_per_face; o++, p++) {
00953           face.mIndices[o] = p;
00954         }
00955       }
00956     }
00957   }
00958 
00959   if (!!dumpfile) {
00960     std::string outf, outext;
00961     outf.assign (dumpfile);
00962     {
00963       const std::string::size_type s = outf.find_last_of('.');
00964       if (s != std::string::npos) {
00965         outext = outf.substr (s+1);
00966       } else {
00967         std::cerr << ";; Can not find extention: " << outf << std::endl;
00968       }
00969     }
00970 
00971     Assimp::Exporter exporter;
00972     size_t outfi = SIZE_T_MAX;
00973     for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
00974       const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
00975       if (outext == e->fileExtension) {
00976         outfi = i; break;
00977       }
00978     }
00979     if (outfi == SIZE_T_MAX) {
00980       outext = "stl";
00981       for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
00982         const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
00983         if (outext == e->fileExtension) {
00984           outfi = i; break;
00985         }
00986       }
00987     }
00988     if (outfi == SIZE_T_MAX) outfi = 0;
00989     const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(outfi);
00990     aiReturn ret = exporter.Export (pScene, e->id, outf);
00991   }
00992 
00993   return NIL;
00994 }
00995 
00996 pointer CONVEX_DECOMP_GL_VERTICES(register context *ctx, int n, pointer *argv)
00997 {
00998   /* vertices-list indices-list
00999      skinwidth decomposition-depth max-hull-vertices
01000      concavity-threshold merge-threshold volume-split-threshold */
01001 
01002   numunion nu;
01003   int verts_per_face = 3;
01004   pointer lvertices = argv[0];
01005   pointer lindices = argv[1];
01006   pointer ret = NIL;
01007 
01008 #if COMPILE_CONVEX_DECOMPOSITION
01009   ckarg2(2, 8);
01010 
01011   NxF32 skinWidth = 0;
01012   NxU32 decompositionDepth = 4;
01013   NxU32 maxHullVertices    = 64;
01014   NxF32 concavityThresholdPercent = 0.1f;
01015   NxF32 mergeThresholdPercent = 20;
01016   NxF32 volumeSplitThresholdPercent = 2;
01017 
01018   if (n > 1) {
01019     skinWidth = ckfltval(argv[2]);
01020   }
01021   if (n > 2) {
01022     decompositionDepth = ckintval(argv[3]);
01023   }
01024   if (n > 3) {
01025     maxHullVertices    = ckintval(argv[4]);
01026   }
01027   if (n > 4) {
01028     concavityThresholdPercent = ckfltval(argv[5]);
01029   }
01030   if (n > 5) {
01031     mergeThresholdPercent = ckfltval(argv[6]);
01032   }
01033   if (n > 6) {
01034     volumeSplitThresholdPercent = ckfltval(argv[7]);
01035   }
01036 
01037   int list_len = 0;
01038   {
01039     pointer a = lvertices;
01040     while (islist (a)) {list_len++; a=ccdr(a);}
01041   }
01042   //
01043   CONVEX_DECOMPOSITION::iConvexDecomposition *ic = CONVEX_DECOMPOSITION::createConvexDecomposition();
01044 
01045   // store all vertices
01046   for (int i = 0; i < list_len; i++) {
01047     pointer mvertices = ccar (lvertices);
01048     pointer mindices = ccar (lindices);
01049 
01050     lvertices = ccdr (lvertices);
01051     lindices = ccdr (lindices);
01052 
01053     if (mvertices == NIL) { /* error */ }
01054     eusinteger_t size = intval (mvertices->c.ary.entity->c.fvec.length);
01055     eusfloat_t *fv = mvertices->c.ary.entity->c.fvec.fv;
01056 
01057     if (mindices != NIL) {
01058       eusinteger_t *iv = mindices->c.ivec.iv;
01059       eusinteger_t idlen = intval (mindices->c.ivec.length);
01060       for (unsigned int f = 0; f < idlen / verts_per_face; f++) {
01061         eusinteger_t i1 = *iv++;
01062         eusinteger_t i2 = *iv++;
01063         eusinteger_t i3 = *iv++;
01064         NxF32 p1[3];
01065         NxF32 p2[3];
01066         NxF32 p3[3];
01067 
01068         p1[0] = fv[3*i1];
01069         p1[1] = fv[3*i1+1];
01070         p1[2] = fv[3*i1+2];
01071 
01072         p2[0] = fv[3*i2];
01073         p2[1] = fv[3*i2+1];
01074         p2[2] = fv[3*i2+2];
01075 
01076         p3[0] = fv[3*i3];
01077         p3[1] = fv[3*i3+1];
01078         p3[2] = fv[3*i3+2];
01079 
01080         ic->addTriangle (p1, p2, p3);
01081       }
01082     } else {
01083       int step = (3 * verts_per_face);
01084       for (unsigned int f = 0; f < size / step; f++) {
01085         NxF32 p1[3];
01086         NxF32 p2[3];
01087         NxF32 p3[3];
01088 
01089         p1[0] = fv[f * step + 0];
01090         p1[1] = fv[f * step + 1];
01091         p1[2] = fv[f * step + 2];
01092 
01093         p2[0] = fv[f * step + 3];
01094         p2[1] = fv[f * step + 4];
01095         p2[2] = fv[f * step + 5];
01096 
01097         p3[0] = fv[f * step + 6];
01098         p3[1] = fv[f * step + 7];
01099         p3[2] = fv[f * step + 8];
01100 
01101         ic->addTriangle (p1, p2, p3);
01102       }
01103     }
01104   }
01105 
01106   //NxF32 skinWidth = 0;
01107   //NxU32 decompositionDepth = 4;
01108   //NxU32 maxHullVertices    = 64;
01109   //NxF32 concavityThresholdPercent = 0.1f;
01110   //NxF32 mergeThresholdPercent = 20;
01111   //NxF32 volumeSplitThresholdPercent = 2;
01112   bool useInitialIslandGeneration = true;
01113   bool useIslandGeneration = false;
01114   bool useBackgroundThreads = false;
01115 
01116   ic->computeConvexDecomposition (skinWidth,
01117                                   decompositionDepth,
01118                                   maxHullVertices,
01119                                   concavityThresholdPercent,
01120                                   mergeThresholdPercent,
01121                                   volumeSplitThresholdPercent,
01122                                   useInitialIslandGeneration,
01123                                   useIslandGeneration,
01124                                   useBackgroundThreads);
01125 
01126   while ( !ic->isComputeComplete() ) {
01127 #if DEBUG
01128     fprintf (stderr, "Computing the convex decomposition in a background thread.\r\n");
01129 #endif
01130     sleep(1);
01131     // Sleep(1000);
01132   }
01133   // finish process
01134 
01135   NxU32 hullCount = ic->getHullCount();
01136 #if DEBUG
01137   fprintf (stderr, "Convex Decomposition produced %d hulls.\r\n", hullCount );
01138 #endif
01139   for (NxU32 i = 0; i < hullCount; i++) {
01140     //
01141     pointer tmpret = NIL;
01142     int npc = 0;
01143     // name
01144     {
01145       std::ostringstream oss;
01146       oss << "convex_" << i;
01147       pointer lname = makestring ((char *)oss.str().c_str(), oss.str().length());
01148       vpush (lname);
01149       lname = rawcons (ctx, lname , NIL);
01150       vpop (); vpush (lname);
01151       lname = rawcons (ctx, K_NAME, lname);
01152       vpop (); vpush (lname);
01153       tmpret = rawcons (ctx, lname, tmpret);
01154       vpop ();
01155       vpush (tmpret); npc++;
01156     }
01157     // type
01158     {
01159       pointer ltype = K_TRIANGLES;
01160       ltype = rawcons (ctx, ltype , NIL);
01161       vpush (ltype);
01162       ltype = rawcons (ctx, K_TYPE, ltype);
01163       vpop(); vpush (ltype);
01164       tmpret = rawcons (ctx, ltype, tmpret);
01165       vpop();
01166       vpush (tmpret); npc++;
01167     }
01168 
01169     CONVEX_DECOMPOSITION::ConvexHullResult result;
01170     ic->getConvexHullResult(i, result);
01171 
01172     pointer ver_mat = makematrix (ctx, result.mVcount, 3); vpush (ver_mat); npc++;
01173     eusfloat_t *ver_vec = ver_mat->c.ary.entity->c.fvec.fv;
01174 
01175     pointer indices = makevector (C_INTVECTOR, result.mTcount * 3);
01176     eusinteger_t *vec = indices->c.ivec.iv;
01177     vpush (indices); npc++;
01178 
01179     for (NxU32 i = 0; i < result.mVcount * 3; i++) {
01180       ver_vec[i] = result.mVertices[i];
01181     }
01182     for (NxU32 i = 0; i < result.mTcount * 3; i++) {
01183       vec[i] = result.mIndices[i];
01184     }
01185     // add indices to return list
01186     {
01187       pointer tmp;
01188       tmp = rawcons (ctx, indices, NIL);
01189       vpush (tmp);
01190       tmp = rawcons (ctx, K_INDICES, tmp);
01191       tmpret = rawcons (ctx, tmp, tmpret);
01192       vpop();
01193       vpush (tmpret); npc++;
01194     }
01195     //
01196     {
01197       pointer tmp;
01198       tmp = rawcons (ctx, ver_mat , NIL);
01199       vpush (tmp);
01200       tmp = rawcons (ctx, K_VERTICES , tmp);
01201       tmpret = rawcons (ctx, tmp, tmpret);
01202       vpop();
01203       vpush (tmpret); npc++;
01204     }
01205 
01206     vpush(ret); npc++;
01207     ret = rawcons (ctx, tmpret, ret);
01208     for (;npc > 0; npc--) vpop();
01209     vpush(ret);
01210   }
01211 
01212   CONVEX_DECOMPOSITION::releaseConvexDecomposition(ic);
01213   vpop(); // pop ret
01214 #else
01215   fprintf(stderr, ";;This program have not been compiled with convex decomposition");
01216 #endif
01217 
01218   return ret;
01219 }
01220 
01221 pointer ASSIMP_DESCRIBE(register context *ctx,int n,pointer *argv)
01222 {
01223   /* */
01224   ckarg(1);
01225   if (!isstring(argv[0])) error(E_NOSTRING);
01226 
01227   Assimp::Importer importer;
01228   const aiScene* scene = importer.ReadFile((char *)get_string(argv[0]),
01229                                            aiProcess_Triangulate            |
01230                                            aiProcess_JoinIdenticalVertices  |
01231                                            aiProcess_SortByPType);
01232 
01233   if (!scene) {
01234     std::string str( importer.GetErrorString() );
01235     std::cerr << ";; " << str << std::endl;
01236     //std::cerr << ";; file: " << get_string(argv[0]) << " not found" << std::endl;
01237     return NIL;
01238   }
01239 
01240   std::cerr << ";; scene->NumAnimations " <<  scene->mNumAnimations << std::endl;
01241   //aiAnimation ** mAnimations;
01242 
01243   std::cerr << ";; scene->mNumCameras " << scene->mNumCameras << std::endl;
01244   //aiCamera ** mCameras;
01245 
01246   std::cerr << ";; scene->mNumLights " << scene->mNumLights << std::endl;
01247   //aiLight ** mLights;
01248 
01249   std::cerr << ";; scene->mNumMaterials " << scene->mNumMaterials << std::endl;
01250   for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
01251     printMaterial(scene->mMaterials[i], i);
01252   }
01253   //aiMaterial ** mMaterials;
01254 
01255   std::cerr << ";; scene->mNumTextures " << scene->mNumTextures << std::endl;
01256   //aiTexture ** mTextures;
01257 
01258   std::cerr << ";; scene->mFlags " << scene->mFlags << std::endl;
01259 
01260   std::cerr << ";; scene->mNumMeshes " << scene->mNumMeshes << std::endl;
01261 
01262   //aiMesh ** scene->mMeshes;
01263   for ( unsigned int i = 0; i < scene->mNumMeshes; i++ ) {
01264     printMesh( scene->mMeshes[i] , "");
01265   }
01266 
01267   printNode( scene->mRootNode , "");
01268 
01269   return NIL;
01270 }
01271 
01272 #if USE_SDL_IMAGE
01273 pointer ASSIMP_LOAD_IMAGE(register context *ctx,int n,pointer *argv)
01274 {
01275   pointer ret = NIL;
01276   ckarg(1);
01277   if (!isstring(argv[0])) error (E_NOSTRING);
01278 
01279   SDL_Surface *sf = IMG_Load((char *)get_string(argv[0]));
01280   if (!!sf) {
01281     pointer ptr;
01282     int byteperpixel = sf->format->BytesPerPixel;
01283     pointer img_buffer = makestring ((char *)sf->pixels, sf->h * sf->w * byteperpixel);
01284 #if 0
01285     // should check RGB and RGBA ...
01286     if (!!(sf->format)) {
01287       printf("fmt:%0X, byte/pixel %d, MASK(RGBA): %08X %08X %08X %08X\n",
01288              //sf->format->format,
01289              sf->flags,
01290              sf->format->BytesPerPixel,
01291              sf->format->Rmask,
01292              sf->format->Gmask,
01293              sf->format->Bmask,
01294              sf->format->Amask);
01295     }
01296     printf("width: %d, height: %d, pitch: %d, pixels: %lX\n",
01297            sf->w,
01298            sf->h,
01299            sf->pitch,
01300            sf->pixels);
01301 #endif
01302     if (!!(sf->format)) {
01303       if (sf->format->BytesPerPixel == 3) {
01304         if ( sf->format->Rmask != 0x000000FF ||
01305              sf->format->Gmask != 0x0000FF00 ||
01306              sf->format->Bmask != 0x00FF0000 ) {
01307           int rm = 0;
01308           int gm = 0;
01309           int bm = 0;
01310           unsigned int mask;
01311 
01312           mask = 0x000000FF;
01313           for (int i = 0; i < 4; i++) {
01314             if ( mask == sf->format->Rmask ) {
01315               rm = i; break;
01316             }
01317             mask <<= 8;
01318           }
01319           mask = 0x000000FF;
01320           for (int i = 0; i < 4; i++) {
01321             if ( mask == sf->format->Gmask ) {
01322               gm = i; break;
01323             }
01324             mask <<= 8;
01325           }
01326           mask = 0x000000FF;
01327           for (int i = 0; i < 4; i++) {
01328             if ( mask == sf->format->Bmask ) {
01329               bm = i; break;
01330             }
01331             mask <<= 8;
01332           }
01333 #if 0
01334           fprintf(stderr, ";; fmt:%0X, byte/pixel %d, MASK(RGBA): %08X %08X %08X %08X -> %d %d %d\n",
01335              //sf->format->format,
01336                  sf->flags,
01337                  sf->format->BytesPerPixel,
01338                  sf->format->Rmask,
01339                  sf->format->Gmask,
01340                  sf->format->Bmask,
01341                  sf->format->Amask, rm, gm, bm);
01342           fprintf(stderr, ";; width: %d, height: %d, pitch: %d, pixels: %lX\n",
01343                  sf->w,
01344                  sf->h,
01345                  sf->pitch,
01346                  sf->pixels);
01347 #endif
01348           unsigned char *src_ptr = (unsigned char *)sf->pixels;
01349           unsigned char *dst_ptr = (unsigned char *)img_buffer->c.str.chars;
01350           for (int i = 0; i < sf->w * sf->h; i++) {
01351             dst_ptr[0] = src_ptr[rm];
01352             dst_ptr[1] = src_ptr[gm];
01353             dst_ptr[2] = src_ptr[bm];
01354             dst_ptr += 3; src_ptr += 3;
01355           }
01356         }
01357       }
01358       if (sf->format->BytesPerPixel == 4) {
01359         if ( sf->format->Rmask != 0x000000FF ||
01360              sf->format->Gmask != 0x0000FF00 ||
01361              sf->format->Bmask != 0x00FF0000 ||
01362              sf->format->Amask != 0xFF000000 ) {
01363           int am = 0;
01364           int rm = 0;
01365           int gm = 0;
01366           int bm = 0;
01367           unsigned int mask;
01368 
01369           mask = 0x000000FF;
01370           for (int i = 0; i < 4; i++) {
01371             if ( mask == sf->format->Rmask ) {
01372               rm = i; break;
01373             }
01374             mask <<= 8;
01375           }
01376           mask = 0x000000FF;
01377           for (int i = 0; i < 4; i++) {
01378             if ( mask == sf->format->Gmask ) {
01379               gm = i; break;
01380             }
01381             mask <<= 8;
01382           }
01383           mask = 0x000000FF;
01384           for (int i = 0; i < 4; i++) {
01385             if ( mask == sf->format->Bmask ) {
01386               bm = i; break;
01387             }
01388             mask <<= 8;
01389           }
01390           mask = 0x000000FF;
01391           for (int i = 0; i < 4; i++) {
01392             if ( mask == sf->format->Amask ) {
01393               am = i; break;
01394             }
01395             mask <<= 8;
01396           }
01397 #if 0
01398           fprintf(stderr, ";; fmt:%0X, byte/pixel %d, MASK(RGBA): %08X %08X %08X %08X -> %d %d %d %d\n",
01399              //sf->format->format,
01400                  sf->flags,
01401                  sf->format->BytesPerPixel,
01402                  sf->format->Rmask,
01403                  sf->format->Gmask,
01404                  sf->format->Bmask,
01405                  sf->format->Amask, rm, gm, bm, am);
01406           fprintf(stderr, ";; width: %d, height: %d, pitch: %d, pixels: %lX\n",
01407                  sf->w,
01408                  sf->h,
01409                  sf->pitch,
01410                  sf->pixels);
01411 #endif
01412           unsigned char *src_ptr = (unsigned char *)sf->pixels;
01413           unsigned char *dst_ptr = (unsigned char *)img_buffer->c.str.chars;
01414           for (int i = 0; i < sf->w * sf->h; i++) {
01415             dst_ptr[0] = src_ptr[rm];
01416             dst_ptr[1] = src_ptr[gm];
01417             dst_ptr[2] = src_ptr[bm];
01418             dst_ptr[3] = src_ptr[am];
01419             dst_ptr += 4; src_ptr += 4;
01420           }
01421         }
01422       }
01423     }
01424 
01425     vpush (img_buffer);
01426     ptr = rawcons (ctx, img_buffer, NIL);
01427     vpop(); vpush (ptr);
01428     ptr = rawcons (ctx, K_VERTICES, ptr);
01429     vpop(); vpush (ptr);
01430     ret = rawcons (ctx, ptr, ret);
01431     vpop ();
01432     vpush(ret);
01433 
01434     ptr = rawcons (ctx, makeint(byteperpixel), NIL);
01435     vpush(ptr);
01436     ptr = rawcons (ctx, K_TYPE, ptr);
01437     vpop(); vpush (ptr);
01438     ret = rawcons (ctx, ptr, ret);
01439     vpop (); vpop();
01440     vpush(ret);
01441 
01442     ptr = rawcons (ctx, makeint(sf->h) ,NIL);
01443     vpush(ptr);
01444     ptr = rawcons (ctx, K_HEIGHT, ptr);
01445     vpop(); vpush (ptr);
01446     ret = rawcons (ctx, ptr, ret);
01447     vpop (); vpop();
01448     vpush(ret);
01449 
01450     ptr = rawcons (ctx, makeint(sf->w) ,NIL);
01451     vpush(ptr);
01452     ptr = rawcons (ctx, K_WIDTH, ptr);
01453     vpop(); vpush (ptr);
01454     ret = rawcons (ctx, ptr, ret);
01455     vpop (); vpop();
01456     //vpush(ret);
01457   }
01458 
01459   IMG_Quit();
01460 
01461   return ret;
01462 }
01463 #endif
01464 
01465 pointer ___eus_assimp(register context *ctx, int n, pointer *argv, pointer env)
01466 {
01467   defun(ctx,"C-ASSIMP-GET-GLVERTICES", argv[0], (pointer (*)())GET_MESHES);
01468   defun(ctx,"C-ASSIMP-DUMP-GLVERTICES", argv[0], (pointer (*)())DUMP_GL_VERTICES);
01469   defun(ctx,"C-CONVEX-DECOMPOSITION-GLVERTICES", argv[0], (pointer (*)())CONVEX_DECOMP_GL_VERTICES);
01470   defun(ctx,"C-ASSIMP-DESCRIBE", argv[0], (pointer (*)())ASSIMP_DESCRIBE);
01471 #if USE_SDL_IMAGE
01472   defun(ctx,"C-ASSIMP-LOAD-IMAGE", argv[0], (pointer (*)())ASSIMP_LOAD_IMAGE);
01473 #endif
01474 
01475   K_VERTICES  = defkeyword(ctx, "VERTICES");
01476   K_NORMALS   = defkeyword(ctx, "NORMALS");
01477   K_INDICES   = defkeyword(ctx, "INDICES");
01478   K_TEXCOORDS = defkeyword(ctx, "TEXCOORDS");
01479   K_MATERIAL  = defkeyword(ctx, "MATERIAL");
01480   K_TYPE      = defkeyword(ctx, "TYPE");
01481   K_LINES     = defkeyword(ctx, "LINES");
01482   K_TRIANGLES = defkeyword(ctx, "TRIANGLES");
01483   K_QUADS     = defkeyword(ctx, "QUADS");
01484   K_POLYGON   = defkeyword(ctx, "POLYGON");
01485   K_NAME      = defkeyword(ctx, "NAME");
01486   // for material
01487   K_FILENAME     = defkeyword(ctx, "FILENAME");
01488   K_AMBIENT      = defkeyword(ctx, "AMBIENT");
01489   K_DIFFUSE      = defkeyword(ctx, "DIFFUSE");
01490   K_SPECULAR     = defkeyword(ctx, "SPECULAR");
01491   K_EMISSION     = defkeyword(ctx, "EMISSION");
01492   K_SHININESS    = defkeyword(ctx, "SHININESS");
01493   K_TRANSPARENCY = defkeyword(ctx, "TRANSPARENCY");
01494   // for image
01495   K_HEIGHT       = defkeyword(ctx, "HEIGHT");
01496   K_WIDTH        = defkeyword(ctx, "WIDTH");
01497   return 0;
01498 }


eus_assimp
Author(s): Yohei Kakiuchi
autogenerated on Mon Oct 6 2014 01:10:01