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->id) {
00680         outfi = i; break;
00681       } else if (outext == e->fileExtension) {
00682         outfi = i; break;
00683       }
00684     }
00685     if (outfi == SIZE_T_MAX) {
00686       outext = "stl";
00687       for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
00688         const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
00689         if (outext == e->fileExtension) {
00690           outfi = i; break;
00691         }
00692       }
00693     }
00694     if (outfi == SIZE_T_MAX) outfi = 0;
00695 
00696     const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(outfi);
00697 #if DEBUG
00698     fprintf (stderr, ";; use file format: %s\n", e->id);
00699 #endif
00700     aiReturn ret = exporter.Export (scene, e->id, outf);
00701   }
00702 
00703   vpop(); // vpush (lst);
00704   return lst;
00705 }
00706 
00707 pointer DUMP_GL_VERTICES(register context *ctx,int n,pointer *argv)
00708 {
00709   /* filename type-list material-list vertices-list normals-list texcoords-list indices-list
00710      (scale) (gen_normal nil) (smooth_normal nil) (split_large_mesh nil)
00711      (optimize_mesh nil) (identical_vert nil) (fix_normal) (direction)
00712   */
00713   // FIXME: name_list
00714   ckarg2(7, 14);
00715   numunion nu;
00716   int direction = 2;
00717   char *dumpfile = NULL;
00718   if (!isstring(argv[0])) error(E_NOSTRING);
00719   dumpfile = (char *)get_string(argv[0]);
00720 
00721   int list_len = 0;
00722   {
00723     pointer a = argv[1];
00724     while (islist(a)) {list_len++; a=ccdr(a);}
00725   }
00726   if (list_len <= 0) return NIL;
00727   bool has_materials = false;
00728   pointer ltype, linfo, lvertices, lnormals, ltexcoords, lindices;
00729   pointer mtype, minfo, mvertices, mnormals, mtexcoords, mindices;
00730   eusfloat_t scale = 1.0;
00731 
00732   ltype = argv[1];
00733   linfo = argv[2];
00734   lvertices = argv[3];
00735   lnormals = argv[4];
00736   ltexcoords = argv[5];
00737   lindices = argv[6];
00738   if (n > 7) {
00739     scale = fltval(argv[7]);
00740   }
00741   if (n > 13) {
00742     direction = intval(argv[13]);
00743   }
00744 
00745   // assimp loader
00746   aiScene *pScene = (aiScene *) malloc(sizeof (aiScene));
00747   memset((void *)pScene, 0, sizeof (aiScene));
00748   pScene->mPrivate = malloc(sizeof (void *) * 2);
00749   //printf("scene = %lX\n", (void *)pScene);
00750 
00751   pScene->mNumMeshes = list_len;
00752   pScene->mMeshes = new aiMesh*[list_len];
00753 
00754   // allocate a single node
00755   pScene->mRootNode = new aiNode();
00756   pScene->mRootNode->mNumMeshes = list_len;
00757   pScene->mRootNode->mMeshes = new unsigned int[list_len];
00758   pScene->mRootNode->mName.Set("eus_glvertices");
00759 
00760   if (linfo != NIL) {
00761     pScene->mNumMaterials = list_len;
00762     pScene->mMaterials = new aiMaterial*[list_len];
00763     has_materials = true;
00764   } else {
00765     pScene->mNumMaterials = 1;
00766     pScene->mMaterials = new aiMaterial*[1];
00767     has_materials = false;
00768     // make default material
00769     aiMaterial* pcMat = new aiMaterial();
00770     aiString s; s.Set (AI_DEFAULT_MATERIAL_NAME);
00771     pcMat->AddProperty (&s, AI_MATKEY_NAME);
00772     aiColor4D clrDiffuse(0.8f, 0.8f, 0.8f, 1.0f);
00773     pcMat->AddProperty (&clrDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
00774     pcMat->AddProperty (&clrDiffuse, 1, AI_MATKEY_COLOR_SPECULAR);
00775     clrDiffuse = aiColor4D (0.2f, 0.2f, 0.2f, 1.0f);
00776     pcMat->AddProperty (&clrDiffuse, 1, AI_MATKEY_COLOR_AMBIENT);
00777 
00778     pScene->mMaterials[0] = pcMat;
00779   }
00780 
00781   for (int i = 0; i < list_len; i++) {
00782     mtype = ccar (ltype);
00783     minfo = ccar (linfo);
00784     mvertices = ccar (lvertices);
00785     mnormals = ccar (lnormals);
00786     mtexcoords = ccar (ltexcoords);
00787     mindices = ccar (lindices);
00788 
00789     ltype = ccdr (ltype);
00790     linfo = ccdr (linfo);
00791     lvertices = ccdr (lvertices);
00792     lnormals = ccdr (lnormals);
00793     ltexcoords = ccdr (ltexcoords);
00794     lindices = ccdr (lindices);
00795 
00796     pScene->mRootNode->mMeshes[i] = i;
00797     aiMesh* pMesh = pScene->mMeshes[i] = new aiMesh();
00798     // std::cerr << ";; mesh = " << (void *)pMesh << std::endl;
00799     if (!has_materials) {
00800       pMesh->mMaterialIndex = 0;
00801     } else {
00802       pMesh->mMaterialIndex = i;
00803       // material
00804       aiMaterial* pcMat = new aiMaterial();
00805       std::ostringstream oss;
00806       oss << "eusassimp_mat_" << i;
00807       aiString s; s.Set (oss.str());
00808       pcMat->AddProperty(&s, AI_MATKEY_NAME);
00809 
00810       for (int el = 0; el < 7; el++) {
00811         // (list :ambient :diffuse :specular :emission :shininess :transparency :filename)
00812         pointer melem = ccar (minfo);
00813         minfo = ccdr (minfo);
00814         aiColor4D clr4d (0.0f, 0.0f, 0.0f, 1.0f);
00815         switch (el) {
00816         case 0:
00817           if (melem != NIL) {
00818             for(int j = 0; j < intval(melem->c.fvec.length); j++) {
00819               clr4d[j] = melem->c.fvec.fv[j];
00820             }
00821             //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
00822             pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_AMBIENT);
00823           }
00824           break;
00825         case 1:
00826           if (melem != NIL) {
00827             for(int j = 0; j < intval(melem->c.fvec.length); j++) {
00828               clr4d[j] = melem->c.fvec.fv[j];
00829             }
00830             //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
00831             pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_DIFFUSE);
00832           }
00833           break;
00834         case 2:
00835           if (melem != NIL) {
00836             for(int j = 0; j < intval(melem->c.fvec.length); j++) {
00837               clr4d[j] = melem->c.fvec.fv[j];
00838             }
00839             //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
00840             pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_SPECULAR);
00841           }
00842           break;
00843         case 3:
00844           if (melem != NIL) {
00845             for(int j = 0; j < intval(melem->c.fvec.length); j++) {
00846               clr4d[j] = melem->c.fvec.fv[j];
00847             }
00848             //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
00849             pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_EMISSIVE);
00850           }
00851           break;
00852         case 4:
00853           if (melem != NIL) {
00854             float val = fltval (melem);
00855             pcMat->AddProperty(&val, 1, AI_MATKEY_SHININESS);
00856           }
00857           break;
00858         case 5:
00859           if (melem != NIL) {
00860             if (isnum (melem)) {
00861               float val = isflt (melem) ? fltval (melem) : (float)intval (melem);
00862               //printf("trans: %f\n", val);
00863               for(int j = 0; j < 4; j++) {
00864                 clr4d[j] = val;
00865               }
00866             } else {
00867               for(int j = 0; j < intval(melem->c.fvec.length); j++) {
00868                 clr4d[j] = melem->c.fvec.fv[j];
00869               }
00870             }
00871             //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
00872             pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_TRANSPARENT);
00873           }
00874           break;
00875         case 6: // texture
00876           if (melem != NIL) {
00877             if (isstring(melem)) {
00878               //printf(";; texture file: %s\n", melem->c.str.chars);
00879               aiString s; s.Set ((const char *)(melem->c.str.chars));
00880               pcMat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(0));
00881             }
00882           }
00883           break;
00884         }
00885       }
00886       pScene->mMaterials[i] = pcMat;
00887     }
00888     // vertices
00889     if (mvertices == NIL) { /* error */ }
00890     eusinteger_t size = intval (mvertices->c.ary.entity->c.fvec.length);
00891     eusfloat_t *fv = mvertices->c.ary.entity->c.fvec.fv;
00892     pMesh->mNumVertices = size / 3;
00893     pMesh->mVertices = new aiVector3D [pMesh->mNumVertices];
00894     for (unsigned int k = 0; k < pMesh->mNumVertices; k++) {
00895       pMesh->mVertices[k].x = scale * *fv++;
00896       pMesh->mVertices[k].y = scale * *fv++;
00897       pMesh->mVertices[k].z = scale * *fv++;
00898     }
00899     // normals
00900     if (mnormals != NIL) {
00901       eusinteger_t size = intval (mnormals->c.ary.entity->c.fvec.length);
00902       eusfloat_t *fv = mnormals->c.ary.entity->c.fvec.fv;
00903       if (size / 3 != pMesh->mNumVertices) { /* error */ }
00904       pMesh->mNormals  = new aiVector3D [pMesh->mNumVertices];
00905       for (unsigned int k = 0; k < pMesh->mNumVertices; k++) {
00906         pMesh->mNormals[k].x = *fv++;
00907         pMesh->mNormals[k].y = *fv++;
00908         pMesh->mNormals[k].z = *fv++;
00909       }
00910     }
00911     // texcoords
00912     if (mtexcoords != NIL) {
00913       printf(";; add texture coords\n");
00914       eusinteger_t size = intval (mtexcoords->c.fvec.length);
00915       eusfloat_t *fv = mtexcoords->c.fvec.fv;
00916       if (size / 2 != pMesh->mNumVertices) { /* error */ }
00917       // use just 1 texture
00918       pMesh->mTextureCoords[0] = new aiVector3D [pMesh->mNumVertices];
00919       for (unsigned int k = 0; k < pMesh->mNumVertices; k++) {
00920         pMesh->mTextureCoords[0][k].x = *fv++;
00921         pMesh->mTextureCoords[0][k].y = (1.0 - *fv++);
00922       }
00923     }
00924 
00925     unsigned int verts_per_face = intval(mtype);
00926     if (mindices != NIL) {
00927       if (verts_per_face != 4 && verts_per_face != 3) {
00928         /* variable face size */
00929         // not implemented yet
00930       } else {
00931         eusinteger_t *iv = mindices->c.ivec.iv;
00932         eusinteger_t idlen = intval (mindices->c.ivec.length);
00933         pMesh->mNumFaces = idlen / verts_per_face;//
00934 
00935         pMesh->mFaces = new aiFace[pMesh->mNumFaces];
00936         for (unsigned int f = 0; f < pMesh->mNumFaces; f++) {
00937           aiFace& face = pMesh->mFaces[f];
00938           face.mNumIndices = verts_per_face;
00939           face.mIndices = new unsigned int[verts_per_face];
00940           for (unsigned int o = 0; o < verts_per_face; o++) {
00941             face.mIndices[o] = *iv++;
00942           }
00943         }
00944       }
00945     } else {
00946       // add indices
00947       if (verts_per_face != 4 && verts_per_face != 3) { /* error */ }
00948       pMesh->mNumFaces = pMesh->mNumVertices / verts_per_face;
00949       pMesh->mFaces = new aiFace[pMesh->mNumFaces];
00950       for (unsigned int f = 0, p = 0; f < pMesh->mNumFaces; f++) {
00951         aiFace& face = pMesh->mFaces[f];
00952         face.mNumIndices = verts_per_face;
00953         face.mIndices = new unsigned int[verts_per_face];
00954         for (unsigned int o = 0; o < verts_per_face; o++, p++) {
00955           face.mIndices[o] = p;
00956         }
00957       }
00958     }
00959   }
00960 
00961   if (!!dumpfile) {
00962     std::string outf, outext;
00963     outf.assign (dumpfile);
00964     {
00965       const std::string::size_type s = outf.find_last_of('.');
00966       if (s != std::string::npos) {
00967         outext = outf.substr (s+1);
00968       } else {
00969         std::cerr << ";; Can not find extention: " << outf << std::endl;
00970       }
00971     }
00972 
00973     Assimp::Exporter exporter;
00974     size_t outfi = SIZE_T_MAX;
00975     for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
00976       const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
00977       if (outext == e->id) {
00978         outfi = i; break;
00979       } else if (outext == e->fileExtension) {
00980         outfi = i; break;
00981       }
00982     }
00983     if (outfi == SIZE_T_MAX) {
00984       outext = "stl";
00985       for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
00986         const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
00987         if (outext == e->fileExtension) {
00988           outfi = i; break;
00989         }
00990       }
00991     }
00992     if (outfi == SIZE_T_MAX) outfi = 0;
00993     const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(outfi);
00994     aiReturn ret = exporter.Export (pScene, e->id, outf);
00995   }
00996 
00997   return NIL;
00998 }
00999 
01000 pointer CONVEX_DECOMP_GL_VERTICES(register context *ctx, int n, pointer *argv)
01001 {
01002   /* vertices-list indices-list
01003      skinwidth decomposition-depth max-hull-vertices
01004      concavity-threshold merge-threshold volume-split-threshold */
01005 
01006   numunion nu;
01007   int verts_per_face = 3;
01008   pointer lvertices = argv[0];
01009   pointer lindices = argv[1];
01010   pointer ret = NIL;
01011 
01012 #if COMPILE_CONVEX_DECOMPOSITION
01013   ckarg2(2, 8);
01014 
01015   NxF32 skinWidth = 0;
01016   NxU32 decompositionDepth = 4;
01017   NxU32 maxHullVertices    = 64;
01018   NxF32 concavityThresholdPercent = 0.1f;
01019   NxF32 mergeThresholdPercent = 20;
01020   NxF32 volumeSplitThresholdPercent = 2;
01021 
01022   if (n > 1) {
01023     skinWidth = ckfltval(argv[2]);
01024   }
01025   if (n > 2) {
01026     decompositionDepth = ckintval(argv[3]);
01027   }
01028   if (n > 3) {
01029     maxHullVertices    = ckintval(argv[4]);
01030   }
01031   if (n > 4) {
01032     concavityThresholdPercent = ckfltval(argv[5]);
01033   }
01034   if (n > 5) {
01035     mergeThresholdPercent = ckfltval(argv[6]);
01036   }
01037   if (n > 6) {
01038     volumeSplitThresholdPercent = ckfltval(argv[7]);
01039   }
01040 
01041   int list_len = 0;
01042   {
01043     pointer a = lvertices;
01044     while (islist (a)) {list_len++; a=ccdr(a);}
01045   }
01046   //
01047   CONVEX_DECOMPOSITION::iConvexDecomposition *ic = CONVEX_DECOMPOSITION::createConvexDecomposition();
01048 
01049   // store all vertices
01050   for (int i = 0; i < list_len; i++) {
01051     pointer mvertices = ccar (lvertices);
01052     pointer mindices = ccar (lindices);
01053 
01054     lvertices = ccdr (lvertices);
01055     lindices = ccdr (lindices);
01056 
01057     if (mvertices == NIL) { /* error */ }
01058     eusinteger_t size = intval (mvertices->c.ary.entity->c.fvec.length);
01059     eusfloat_t *fv = mvertices->c.ary.entity->c.fvec.fv;
01060 
01061     if (mindices != NIL) {
01062       eusinteger_t *iv = mindices->c.ivec.iv;
01063       eusinteger_t idlen = intval (mindices->c.ivec.length);
01064       for (unsigned int f = 0; f < idlen / verts_per_face; f++) {
01065         eusinteger_t i1 = *iv++;
01066         eusinteger_t i2 = *iv++;
01067         eusinteger_t i3 = *iv++;
01068         NxF32 p1[3];
01069         NxF32 p2[3];
01070         NxF32 p3[3];
01071 
01072         p1[0] = fv[3*i1];
01073         p1[1] = fv[3*i1+1];
01074         p1[2] = fv[3*i1+2];
01075 
01076         p2[0] = fv[3*i2];
01077         p2[1] = fv[3*i2+1];
01078         p2[2] = fv[3*i2+2];
01079 
01080         p3[0] = fv[3*i3];
01081         p3[1] = fv[3*i3+1];
01082         p3[2] = fv[3*i3+2];
01083 
01084         ic->addTriangle (p1, p2, p3);
01085       }
01086     } else {
01087       int step = (3 * verts_per_face);
01088       for (unsigned int f = 0; f < size / step; f++) {
01089         NxF32 p1[3];
01090         NxF32 p2[3];
01091         NxF32 p3[3];
01092 
01093         p1[0] = fv[f * step + 0];
01094         p1[1] = fv[f * step + 1];
01095         p1[2] = fv[f * step + 2];
01096 
01097         p2[0] = fv[f * step + 3];
01098         p2[1] = fv[f * step + 4];
01099         p2[2] = fv[f * step + 5];
01100 
01101         p3[0] = fv[f * step + 6];
01102         p3[1] = fv[f * step + 7];
01103         p3[2] = fv[f * step + 8];
01104 
01105         ic->addTriangle (p1, p2, p3);
01106       }
01107     }
01108   }
01109 
01110   //NxF32 skinWidth = 0;
01111   //NxU32 decompositionDepth = 4;
01112   //NxU32 maxHullVertices    = 64;
01113   //NxF32 concavityThresholdPercent = 0.1f;
01114   //NxF32 mergeThresholdPercent = 20;
01115   //NxF32 volumeSplitThresholdPercent = 2;
01116   bool useInitialIslandGeneration = true;
01117   bool useIslandGeneration = false;
01118   bool useBackgroundThreads = false;
01119 
01120   ic->computeConvexDecomposition (skinWidth,
01121                                   decompositionDepth,
01122                                   maxHullVertices,
01123                                   concavityThresholdPercent,
01124                                   mergeThresholdPercent,
01125                                   volumeSplitThresholdPercent,
01126                                   useInitialIslandGeneration,
01127                                   useIslandGeneration,
01128                                   useBackgroundThreads);
01129 
01130   while ( !ic->isComputeComplete() ) {
01131 #if DEBUG
01132     fprintf (stderr, "Computing the convex decomposition in a background thread.\r\n");
01133 #endif
01134     sleep(1);
01135     // Sleep(1000);
01136   }
01137   // finish process
01138 
01139   NxU32 hullCount = ic->getHullCount();
01140 #if DEBUG
01141   fprintf (stderr, "Convex Decomposition produced %d hulls.\r\n", hullCount );
01142 #endif
01143   for (NxU32 i = 0; i < hullCount; i++) {
01144     //
01145     pointer tmpret = NIL;
01146     int npc = 0;
01147     // name
01148     {
01149       std::ostringstream oss;
01150       oss << "convex_" << i;
01151       pointer lname = makestring ((char *)oss.str().c_str(), oss.str().length());
01152       vpush (lname);
01153       lname = rawcons (ctx, lname , NIL);
01154       vpop (); vpush (lname);
01155       lname = rawcons (ctx, K_NAME, lname);
01156       vpop (); vpush (lname);
01157       tmpret = rawcons (ctx, lname, tmpret);
01158       vpop ();
01159       vpush (tmpret); npc++;
01160     }
01161     // type
01162     {
01163       pointer ltype = K_TRIANGLES;
01164       ltype = rawcons (ctx, ltype , NIL);
01165       vpush (ltype);
01166       ltype = rawcons (ctx, K_TYPE, ltype);
01167       vpop(); vpush (ltype);
01168       tmpret = rawcons (ctx, ltype, tmpret);
01169       vpop();
01170       vpush (tmpret); npc++;
01171     }
01172 
01173     CONVEX_DECOMPOSITION::ConvexHullResult result;
01174     ic->getConvexHullResult(i, result);
01175 
01176     pointer ver_mat = makematrix (ctx, result.mVcount, 3); vpush (ver_mat); npc++;
01177     eusfloat_t *ver_vec = ver_mat->c.ary.entity->c.fvec.fv;
01178 
01179     pointer indices = makevector (C_INTVECTOR, result.mTcount * 3);
01180     eusinteger_t *vec = indices->c.ivec.iv;
01181     vpush (indices); npc++;
01182 
01183     for (NxU32 i = 0; i < result.mVcount * 3; i++) {
01184       ver_vec[i] = result.mVertices[i];
01185     }
01186     for (NxU32 i = 0; i < result.mTcount * 3; i++) {
01187       vec[i] = result.mIndices[i];
01188     }
01189     // add indices to return list
01190     {
01191       pointer tmp;
01192       tmp = rawcons (ctx, indices, NIL);
01193       vpush (tmp);
01194       tmp = rawcons (ctx, K_INDICES, tmp);
01195       tmpret = rawcons (ctx, tmp, tmpret);
01196       vpop();
01197       vpush (tmpret); npc++;
01198     }
01199     //
01200     {
01201       pointer tmp;
01202       tmp = rawcons (ctx, ver_mat , NIL);
01203       vpush (tmp);
01204       tmp = rawcons (ctx, K_VERTICES , tmp);
01205       tmpret = rawcons (ctx, tmp, tmpret);
01206       vpop();
01207       vpush (tmpret); npc++;
01208     }
01209 
01210     vpush(ret); npc++;
01211     ret = rawcons (ctx, tmpret, ret);
01212     for (;npc > 0; npc--) vpop();
01213     vpush(ret);
01214   }
01215 
01216   CONVEX_DECOMPOSITION::releaseConvexDecomposition(ic);
01217   vpop(); // pop ret
01218 #else
01219   fprintf(stderr, ";;This program have not been compiled with convex decomposition");
01220 #endif
01221 
01222   return ret;
01223 }
01224 
01225 pointer ASSIMP_DESCRIBE(register context *ctx,int n,pointer *argv)
01226 {
01227   /* */
01228   ckarg(1);
01229   if (!isstring(argv[0])) error(E_NOSTRING);
01230 
01231   Assimp::Importer importer;
01232   const aiScene* scene = importer.ReadFile((char *)get_string(argv[0]),
01233                                            aiProcess_Triangulate            |
01234                                            aiProcess_JoinIdenticalVertices  |
01235                                            aiProcess_SortByPType);
01236 
01237   if (!scene) {
01238     std::string str( importer.GetErrorString() );
01239     std::cerr << ";; " << str << std::endl;
01240     //std::cerr << ";; file: " << get_string(argv[0]) << " not found" << std::endl;
01241     return NIL;
01242   }
01243 
01244   std::cerr << ";; scene->NumAnimations " <<  scene->mNumAnimations << std::endl;
01245   //aiAnimation ** mAnimations;
01246 
01247   std::cerr << ";; scene->mNumCameras " << scene->mNumCameras << std::endl;
01248   //aiCamera ** mCameras;
01249 
01250   std::cerr << ";; scene->mNumLights " << scene->mNumLights << std::endl;
01251   //aiLight ** mLights;
01252 
01253   std::cerr << ";; scene->mNumMaterials " << scene->mNumMaterials << std::endl;
01254   for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
01255     printMaterial(scene->mMaterials[i], i);
01256   }
01257   //aiMaterial ** mMaterials;
01258 
01259   std::cerr << ";; scene->mNumTextures " << scene->mNumTextures << std::endl;
01260   //aiTexture ** mTextures;
01261 
01262   std::cerr << ";; scene->mFlags " << scene->mFlags << std::endl;
01263 
01264   std::cerr << ";; scene->mNumMeshes " << scene->mNumMeshes << std::endl;
01265 
01266   //aiMesh ** scene->mMeshes;
01267   for ( unsigned int i = 0; i < scene->mNumMeshes; i++ ) {
01268     printMesh( scene->mMeshes[i] , "");
01269   }
01270 
01271   printNode( scene->mRootNode , "");
01272 
01273   return NIL;
01274 }
01275 
01276 #if USE_SDL_IMAGE
01277 pointer ASSIMP_LOAD_IMAGE(register context *ctx,int n,pointer *argv)
01278 {
01279   pointer ret = NIL;
01280   ckarg(1);
01281   if (!isstring(argv[0])) error (E_NOSTRING);
01282 
01283   SDL_Surface *sf = IMG_Load((char *)get_string(argv[0]));
01284   if (!!sf) {
01285     pointer ptr;
01286     int byteperpixel = sf->format->BytesPerPixel;
01287     pointer img_buffer = makestring ((char *)sf->pixels, sf->h * sf->w * byteperpixel);
01288 #if 0
01289     // should check RGB and RGBA ...
01290     if (!!(sf->format)) {
01291       printf("fmt:%0X, byte/pixel %d, MASK(RGBA): %08X %08X %08X %08X\n",
01292              //sf->format->format,
01293              sf->flags,
01294              sf->format->BytesPerPixel,
01295              sf->format->Rmask,
01296              sf->format->Gmask,
01297              sf->format->Bmask,
01298              sf->format->Amask);
01299     }
01300     printf("width: %d, height: %d, pitch: %d, pixels: %lX\n",
01301            sf->w,
01302            sf->h,
01303            sf->pitch,
01304            sf->pixels);
01305 #endif
01306     if (!!(sf->format)) {
01307       if (sf->format->BytesPerPixel == 3) {
01308         if ( sf->format->Rmask != 0x000000FF ||
01309              sf->format->Gmask != 0x0000FF00 ||
01310              sf->format->Bmask != 0x00FF0000 ) {
01311           int rm = 0;
01312           int gm = 0;
01313           int bm = 0;
01314           unsigned int mask;
01315 
01316           mask = 0x000000FF;
01317           for (int i = 0; i < 4; i++) {
01318             if ( mask == sf->format->Rmask ) {
01319               rm = i; break;
01320             }
01321             mask <<= 8;
01322           }
01323           mask = 0x000000FF;
01324           for (int i = 0; i < 4; i++) {
01325             if ( mask == sf->format->Gmask ) {
01326               gm = i; break;
01327             }
01328             mask <<= 8;
01329           }
01330           mask = 0x000000FF;
01331           for (int i = 0; i < 4; i++) {
01332             if ( mask == sf->format->Bmask ) {
01333               bm = i; break;
01334             }
01335             mask <<= 8;
01336           }
01337 #if 0
01338           fprintf(stderr, ";; fmt:%0X, byte/pixel %d, MASK(RGBA): %08X %08X %08X %08X -> %d %d %d\n",
01339              //sf->format->format,
01340                  sf->flags,
01341                  sf->format->BytesPerPixel,
01342                  sf->format->Rmask,
01343                  sf->format->Gmask,
01344                  sf->format->Bmask,
01345                  sf->format->Amask, rm, gm, bm);
01346           fprintf(stderr, ";; width: %d, height: %d, pitch: %d, pixels: %lX\n",
01347                  sf->w,
01348                  sf->h,
01349                  sf->pitch,
01350                  sf->pixels);
01351 #endif
01352           unsigned char *src_ptr = (unsigned char *)sf->pixels;
01353           unsigned char *dst_ptr = (unsigned char *)img_buffer->c.str.chars;
01354           for (int i = 0; i < sf->w * sf->h; i++) {
01355             dst_ptr[0] = src_ptr[rm];
01356             dst_ptr[1] = src_ptr[gm];
01357             dst_ptr[2] = src_ptr[bm];
01358             dst_ptr += 3; src_ptr += 3;
01359           }
01360         }
01361       }
01362       if (sf->format->BytesPerPixel == 4) {
01363         if ( sf->format->Rmask != 0x000000FF ||
01364              sf->format->Gmask != 0x0000FF00 ||
01365              sf->format->Bmask != 0x00FF0000 ||
01366              sf->format->Amask != 0xFF000000 ) {
01367           int am = 0;
01368           int rm = 0;
01369           int gm = 0;
01370           int bm = 0;
01371           unsigned int mask;
01372 
01373           mask = 0x000000FF;
01374           for (int i = 0; i < 4; i++) {
01375             if ( mask == sf->format->Rmask ) {
01376               rm = i; break;
01377             }
01378             mask <<= 8;
01379           }
01380           mask = 0x000000FF;
01381           for (int i = 0; i < 4; i++) {
01382             if ( mask == sf->format->Gmask ) {
01383               gm = i; break;
01384             }
01385             mask <<= 8;
01386           }
01387           mask = 0x000000FF;
01388           for (int i = 0; i < 4; i++) {
01389             if ( mask == sf->format->Bmask ) {
01390               bm = i; break;
01391             }
01392             mask <<= 8;
01393           }
01394           mask = 0x000000FF;
01395           for (int i = 0; i < 4; i++) {
01396             if ( mask == sf->format->Amask ) {
01397               am = i; break;
01398             }
01399             mask <<= 8;
01400           }
01401 #if 0
01402           fprintf(stderr, ";; fmt:%0X, byte/pixel %d, MASK(RGBA): %08X %08X %08X %08X -> %d %d %d %d\n",
01403              //sf->format->format,
01404                  sf->flags,
01405                  sf->format->BytesPerPixel,
01406                  sf->format->Rmask,
01407                  sf->format->Gmask,
01408                  sf->format->Bmask,
01409                  sf->format->Amask, rm, gm, bm, am);
01410           fprintf(stderr, ";; width: %d, height: %d, pitch: %d, pixels: %lX\n",
01411                  sf->w,
01412                  sf->h,
01413                  sf->pitch,
01414                  sf->pixels);
01415 #endif
01416           unsigned char *src_ptr = (unsigned char *)sf->pixels;
01417           unsigned char *dst_ptr = (unsigned char *)img_buffer->c.str.chars;
01418           for (int i = 0; i < sf->w * sf->h; i++) {
01419             dst_ptr[0] = src_ptr[rm];
01420             dst_ptr[1] = src_ptr[gm];
01421             dst_ptr[2] = src_ptr[bm];
01422             dst_ptr[3] = src_ptr[am];
01423             dst_ptr += 4; src_ptr += 4;
01424           }
01425         }
01426       }
01427     }
01428 
01429     vpush (img_buffer);
01430     ptr = rawcons (ctx, img_buffer, NIL);
01431     vpop(); vpush (ptr);
01432     ptr = rawcons (ctx, K_VERTICES, ptr);
01433     vpop(); vpush (ptr);
01434     ret = rawcons (ctx, ptr, ret);
01435     vpop ();
01436     vpush(ret);
01437 
01438     ptr = rawcons (ctx, makeint(byteperpixel), NIL);
01439     vpush(ptr);
01440     ptr = rawcons (ctx, K_TYPE, ptr);
01441     vpop(); vpush (ptr);
01442     ret = rawcons (ctx, ptr, ret);
01443     vpop (); vpop();
01444     vpush(ret);
01445 
01446     ptr = rawcons (ctx, makeint(sf->h) ,NIL);
01447     vpush(ptr);
01448     ptr = rawcons (ctx, K_HEIGHT, ptr);
01449     vpop(); vpush (ptr);
01450     ret = rawcons (ctx, ptr, ret);
01451     vpop (); vpop();
01452     vpush(ret);
01453 
01454     ptr = rawcons (ctx, makeint(sf->w) ,NIL);
01455     vpush(ptr);
01456     ptr = rawcons (ctx, K_WIDTH, ptr);
01457     vpop(); vpush (ptr);
01458     ret = rawcons (ctx, ptr, ret);
01459     vpop (); vpop();
01460     //vpush(ret);
01461   }
01462 
01463   IMG_Quit();
01464 
01465   return ret;
01466 }
01467 #endif
01468 
01469 pointer ___eus_assimp(register context *ctx, int n, pointer *argv, pointer env)
01470 {
01471   defun(ctx,"C-ASSIMP-GET-GLVERTICES", argv[0], (pointer (*)())GET_MESHES, NULL);
01472   defun(ctx,"C-ASSIMP-DUMP-GLVERTICES", argv[0], (pointer (*)())DUMP_GL_VERTICES, NULL);
01473   defun(ctx,"C-CONVEX-DECOMPOSITION-GLVERTICES", argv[0], (pointer (*)())CONVEX_DECOMP_GL_VERTICES, NULL);
01474   defun(ctx,"C-ASSIMP-DESCRIBE", argv[0], (pointer (*)())ASSIMP_DESCRIBE, NULL);
01475 #if USE_SDL_IMAGE
01476   defun(ctx,"C-ASSIMP-LOAD-IMAGE", argv[0], (pointer (*)())ASSIMP_LOAD_IMAGE, NULL);
01477 #endif
01478 
01479   K_VERTICES  = defkeyword(ctx, "VERTICES");
01480   K_NORMALS   = defkeyword(ctx, "NORMALS");
01481   K_INDICES   = defkeyword(ctx, "INDICES");
01482   K_TEXCOORDS = defkeyword(ctx, "TEXCOORDS");
01483   K_MATERIAL  = defkeyword(ctx, "MATERIAL");
01484   K_TYPE      = defkeyword(ctx, "TYPE");
01485   K_LINES     = defkeyword(ctx, "LINES");
01486   K_TRIANGLES = defkeyword(ctx, "TRIANGLES");
01487   K_QUADS     = defkeyword(ctx, "QUADS");
01488   K_POLYGON   = defkeyword(ctx, "POLYGON");
01489   K_NAME      = defkeyword(ctx, "NAME");
01490   // for material
01491   K_FILENAME     = defkeyword(ctx, "FILENAME");
01492   K_AMBIENT      = defkeyword(ctx, "AMBIENT");
01493   K_DIFFUSE      = defkeyword(ctx, "DIFFUSE");
01494   K_SPECULAR     = defkeyword(ctx, "SPECULAR");
01495   K_EMISSION     = defkeyword(ctx, "EMISSION");
01496   K_SHININESS    = defkeyword(ctx, "SHININESS");
01497   K_TRANSPARENCY = defkeyword(ctx, "TRANSPARENCY");
01498   // for image
01499   K_HEIGHT       = defkeyword(ctx, "HEIGHT");
01500   K_WIDTH        = defkeyword(ctx, "WIDTH");
01501   return 0;
01502 }


eus_assimp
Author(s): Yohei Kakiuchi
autogenerated on Sat Jun 8 2019 20:57:44