eus_assimp.cpp
Go to the documentation of this file.
1 // assimp_devel
2 #include <Importer.hpp>
3 #include <Exporter.hpp>
4 #include <postprocess.h>
5 #include <scene.h>
6 
7 // convex decomposition
8 #if COMPILE_CONVEX_DECOMPOSITION
9 #include "NvConvexDecomposition.h"
10 #endif
11 
12 //SDL image
13 #if USE_SDL_IMAGE
14 #include "SDL_image.h"
15 #endif
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <signal.h>
22 #include <math.h>
23 #include <time.h>
24 #include <pthread.h>
25 #include <setjmp.h>
26 #include <errno.h>
27 
28 #include <list>
29 #include <vector>
30 #include <set>
31 #include <string>
32 #include <map>
33 #include <sstream>
34 #include <cstdio>
35 #include <iostream>
36 
37 // for eus.h
38 #define class eus_class
39 #define throw eus_throw
40 #define export eus_export
41 #define vector eus_vector
42 #define string eus_string
43 #define iostream eus_iostream
44 #define complex eus_complex
45 
46 #include "eus.h"
47 extern "C" {
48  pointer ___eus_assimp(register context *ctx, int n, pointer *argv, pointer env);
50  char modname[] = "___eus_assimp";
51  return add_module_initializer(modname, (pointer (*)())___eus_assimp);}
52 }
53 
54 #undef class
55 #undef throw
56 #undef export
57 #undef vector
58 #undef string
59 #undef iostream
60 #undef complex
61 
62 #define SIZE_T_MAX (std::numeric_limits<size_t>::max())
63 
68 
69 static std::string primitiveType (unsigned int tp) {
70  switch (tp) {
71  case aiPrimitiveType_POINT:
72  return "POINT";
73  break;
74  case aiPrimitiveType_LINE:
75  return "LINE";
76  break;
77  case aiPrimitiveType_TRIANGLE:
78  return "TRIANGLE";
79  break;
80  case aiPrimitiveType_POLYGON:
81  return "POLYGON";
82  break;
83  }
84  return "NO_TYPE";
85 }
86 
87 static void printMesh(aiMesh *mesh, std::string prefix)
88 {
89  std::string name;
90  name.assign( mesh->mName.data, mesh->mName.length );
91 
92  std::cerr << ";;" << prefix << " Mesh name: " << name << std::endl;
93 
94  std::cerr << ";;" << prefix << " Bones: " << mesh->mNumBones << std::endl;
95  std::cerr << ";;" << prefix << " Faces: " << mesh->mNumFaces << std::endl;
96  std::cerr << ";;" << prefix << " Vertices: " << mesh->mNumVertices << std::endl;
97  std::cerr << ";;" << prefix << " PType: " << primitiveType( mesh->mPrimitiveTypes ) << std::endl;
98  std::cerr << ";;" << prefix << " MaterialIndex: " << mesh->mMaterialIndex << std::endl;
99 }
100 
101 static void printTransform(aiMatrix4x4 &transform, std::string prefix) {
102  std::cerr << prefix << " trans: " << transform.a1 << " " << transform.a2 << " " << transform.a3 << " " << transform.a4 << std::endl;
103  std::cerr << prefix << " " << transform.b1 << " " << transform.b2 << " " << transform.b3 << " " << transform.b4 << std::endl;
104  std::cerr << prefix << " " << transform.c1 << " " << transform.c2 << " " << transform.c3 << " " << transform.c4 << std::endl;
105  std::cerr << prefix << " " << transform.d1 << " " << transform.d2 << " " << transform.d3 << " " << transform.d4 << std::endl;
106 }
107 
108 static void printNode(aiNode *node, std::string prefix)
109 {
110  std::string name;
111  name.assign (node->mName.C_Str());
112 
113  fprintf (stderr,"%s Node name(%lX): %s", prefix.c_str(), (void *)node, name.c_str());
114  std::cerr << std::endl;
115  std::cerr << prefix << " Meshes: " << node->mNumMeshes << " / ";
116  for (unsigned int i = 0; i < node->mNumMeshes; i++ ) {
117  std::cerr << " " << node->mMeshes[i];
118  }
119  std::cerr << std::endl;
120  printTransform(node->mTransformation, prefix);
121 
122  std::cerr << prefix << " Children: " << node->mNumChildren << std::endl;
123  for (unsigned int i = 0; i < node->mNumChildren; i++ ) {
124  printNode(node->mChildren[i], prefix + " ");
125  }
126 }
127 
128 static void printMaterial(aiMaterial *material, unsigned int idx)
129 {
130  std::cerr << ";; material: " << idx << std::endl;
131 
132  //float col[4];
133  //aiReturn ret = material->Get(AI_MATKEY_COLOR_DIFFUSE, col, NULL);
134  //std::cerr << "color : " << ret << " " << col[0] << " " << col[1] << " " << col[2] << " " << col[3];
135 
136  for ( unsigned int i = 0; i < material->mNumProperties; i++ ) {
137  aiMaterialProperty *mp = material->mProperties[i];
138  std::string kname;
139  kname.assign( mp->mKey.data, mp->mKey.length );
140  std::cerr << ";; " << kname << std::endl;
141  }
142 }
143 
144 static pointer store_mesh_info (register context *ctx, eusfloat_t base_scl,
145  const aiScene *scene, const aiNode *node, const aiMesh *amesh,
146  const aiMatrix4x4 &trans, int direc) {
147  static int mesh_cntr = -1;
148  pointer ver_mat, nom_mat, indices, tex_cds;
149  eusfloat_t *ver_vec = NULL, *nom_vec = NULL, *tex_vec = NULL;
150  numunion nu;
151  pointer ret = NIL;
152  int npc = 0;
153 
154  mesh_cntr++;
155  // name
156  {
157  std::string nname, mname;
158  nname.assign (node->mName.C_Str());
159  mname.assign (amesh->mName.C_Str());
160 
161  if (nname.empty() && mname.empty()) {
162  nname = "eusglvertices_" + mesh_cntr;
163  } else {
164  nname = nname + "_" + mname;
165  }
166  pointer lname = makestring ((char *)nname.c_str(), nname.length());
167  vpush (lname);
168  lname = rawcons (ctx, lname , NIL);
169  vpop (); vpush (lname);
170  lname = rawcons (ctx, K_NAME, lname);
171  vpop (); vpush (lname);
172  ret = rawcons (ctx, lname, ret);
173  vpop ();
174  vpush (ret); npc++;
175  }
176 
177  // type
178  {
179  pointer ltype = NIL;
180  switch (amesh->mPrimitiveTypes) {
181  case aiPrimitiveType_LINE:
182  ltype = K_LINES;
183  break;
184  case aiPrimitiveType_TRIANGLE:
185  ltype = K_TRIANGLES;
186  break;
187  case aiPrimitiveType_POLYGON:
188  ltype = K_POLYGON;
189  break;
190  }
191  ltype = rawcons (ctx, ltype , NIL);
192  vpush (ltype);
193  ltype = rawcons (ctx, K_TYPE, ltype);
194  vpop(); vpush (ltype);
195  ret = rawcons (ctx, ltype, ret);
196  vpop();
197  vpush (ret); npc++;
198  }
199 
200  // material
201  {
202  pointer lmaterial = NIL;
203  aiMaterial *am = scene->mMaterials[amesh->mMaterialIndex];
204 #if DEBUG
205  std::cerr << ";; material properties: " << am->mNumProperties << std::endl;
206 #endif
207  aiReturn ar;
208  aiString s;
209  aiColor4D clr4d( 0.0, 0.0, 0.0, 0.0);
210  float val;
211  int lpc = 0;
212  ar = am->Get (AI_MATKEY_NAME, s);
213  if (ar == aiReturn_SUCCESS) {
214 #if DEBUG
215  std::string str; str.assign(s.C_Str());
216  std::cerr << ";; material properties: name: " << str << std::endl;
217 #endif
218  pointer pelem;
219  pointer eusstr = makestring (s.data, strlen(s.data));
220  vpush (eusstr);//
221  pelem = rawcons (ctx, eusstr, NIL);
222  vpush (pelem);//
223  pelem = rawcons (ctx, K_NAME, pelem);
224  vpush (pelem);//
225  lmaterial = rawcons (ctx, pelem, lmaterial);
226  vpop(); vpop(); vpop();
227  vpush (lmaterial); lpc++;
228  }
229  ar = am->Get (AI_MATKEY_COLOR_AMBIENT, clr4d);
230  if (ar == aiReturn_SUCCESS) {
231 #if DEBUG
232  std::cerr << ";; material properties: AMBIENT: ";
233  std::cerr << clr4d[0] << " " << clr4d[1] << " " << clr4d[2] << " " << clr4d[3] << std::endl;
234 #endif
235  pointer pelem = makefvector (4);
236  pelem->c.fvec.fv[0] = clr4d[0]; pelem->c.fvec.fv[1] = clr4d[1];
237  pelem->c.fvec.fv[2] = clr4d[2]; pelem->c.fvec.fv[3] = clr4d[3];
238  vpush (pelem);//
239  pelem = rawcons (ctx, pelem, NIL);
240  vpush (pelem);//
241  pelem = rawcons (ctx, K_AMBIENT, pelem);
242  vpush (pelem);//
243  lmaterial = rawcons (ctx, pelem, lmaterial);
244  vpop(); vpop(); vpop();
245  vpush (lmaterial); lpc++;
246  }
247  ar = am->Get (AI_MATKEY_COLOR_DIFFUSE, clr4d);
248  if (ar == aiReturn_SUCCESS) {
249 #if DEBUG
250  std::cerr << ";; material properties: DIFFUSE: ";
251  std::cerr << clr4d[0] << " " << clr4d[1] << " " << clr4d[2] << " " << clr4d[3] << std::endl;
252 #endif
253  pointer pelem = makefvector (4);
254  pelem->c.fvec.fv[0] = clr4d[0]; pelem->c.fvec.fv[1] = clr4d[1];
255  pelem->c.fvec.fv[2] = clr4d[2]; pelem->c.fvec.fv[3] = clr4d[3];
256  vpush (pelem);//
257  pelem = rawcons (ctx, pelem, NIL);
258  vpush (pelem);//
259  pelem = rawcons (ctx, K_DIFFUSE, pelem);
260  vpush (pelem);//
261  lmaterial = rawcons (ctx, pelem, lmaterial);
262  vpop(); vpop(); vpop();
263  vpush (lmaterial); lpc++;
264  }
265  ar = am->Get (AI_MATKEY_COLOR_SPECULAR, clr4d);
266  if (ar == aiReturn_SUCCESS) {
267 #if DEBUG
268  std::cerr << ";; material properties: SPECULAR: ";
269  std::cerr << clr4d[0] << " " << clr4d[1] << " " << clr4d[2] << " " << clr4d[3] << std::endl;
270 #endif
271  pointer pelem = makefvector (4);
272  pelem->c.fvec.fv[0] = clr4d[0]; pelem->c.fvec.fv[1] = clr4d[1];
273  pelem->c.fvec.fv[2] = clr4d[2]; pelem->c.fvec.fv[3] = clr4d[3];
274  vpush (pelem);//
275  pelem = rawcons (ctx, pelem, NIL);
276  vpush (pelem);//
277  pelem = rawcons (ctx, K_SPECULAR, pelem);
278  vpush (pelem);//
279  lmaterial = rawcons (ctx, pelem, lmaterial);
280  vpop(); vpop(); vpop();
281  vpush (lmaterial); lpc++;
282  }
283  ar = am->Get (AI_MATKEY_COLOR_EMISSIVE, clr4d);
284  if (ar == aiReturn_SUCCESS) {
285 #if DEBUG
286  std::cerr << ";; material properties: EMISSIVE: ";
287  std::cerr << clr4d[0] << " " << clr4d[1] << " " << clr4d[2] << " " << clr4d[3] << std::endl;
288 #endif
289  pointer pelem = makefvector (4);
290  pelem->c.fvec.fv[0] = clr4d[0]; pelem->c.fvec.fv[1] = clr4d[1];
291  pelem->c.fvec.fv[2] = clr4d[2]; pelem->c.fvec.fv[3] = clr4d[3];
292  vpush (pelem);//
293  pelem = rawcons (ctx, pelem, NIL);
294  vpush (pelem);//
295  pelem = rawcons (ctx, K_EMISSION, pelem);
296  vpush (pelem);//
297  lmaterial = rawcons (ctx, pelem, lmaterial);
298  vpop(); vpop(); vpop();
299  vpush (lmaterial); lpc++;
300  }
301  ar = am->Get (AI_MATKEY_SHININESS, val);
302  if (ar == aiReturn_SUCCESS) {
303 #if DEBUG
304  std::cerr << ";; material properties: SHININESS: " << val << std::endl;
305 #endif
306  pointer pelem;
307  pelem = rawcons (ctx, makeflt(val), NIL);
308  vpush (pelem);//
309  pelem = rawcons (ctx, K_SHININESS, pelem);
310  vpush (pelem);//
311  lmaterial = rawcons (ctx, pelem, lmaterial);
312  vpop(); vpop();
313  vpush (lmaterial); lpc++;
314  }
315 #if 0 // Do not use transparent??
316  ar = am->Get (AI_MATKEY_COLOR_TRANSPARENT, clr4d);
317  if (ar == aiReturn_SUCCESS) {
318  std::cerr << ";; material properties: TRANSPARENT: ";
319  std::cerr << clr4d[0] << " " << clr4d[1] << " " << clr4d[2] << " " << clr4d[3] << std::endl;
320  pointer pelem = makefvector (4);
321  pelem->c.fvec.fv[0] = clr4d[0]; pelem->c.fvec.fv[1] = clr4d[1];
322  pelem->c.fvec.fv[2] = clr4d[2]; pelem->c.fvec.fv[3] = clr4d[3];
323  vpush (pelem);//
324  pelem = rawcons (ctx, pelem, NIL);
325  vpush (pelem);//
326  pelem = rawcons (ctx, K_TRANSPARENT, pelem);
327  vpush (pelem);//
328  lmaterial = rawcons (ctx, pelem, lmaterial);
329  vpop(); vpop(); vpop();
330  vpush (lmaterial); lpc++;
331  }
332 #endif
333  for (unsigned int tex_count = 0; tex_count < am->GetTextureCount(aiTextureType_DIFFUSE); tex_count++) {
334  aiString fpath; // filename
335 #if DEBUG
336  aiTextureMapping mapping;
337  unsigned int uvindex;
338  float blend;
339  aiTextureOp op;
340  aiTextureMapMode mapmode;
341  ar = am->GetTexture(aiTextureType_DIFFUSE, tex_count, &fpath,
342  &mapping, &uvindex, &blend, &op, &mapmode);
343 #else
344  ar = am->GetTexture(aiTextureType_DIFFUSE, tex_count, &fpath);
345 #endif
346 
347  if (ar == AI_SUCCESS) {
348  pointer pelem;
349  pointer eusstr = makestring (fpath.data, strlen(fpath.data));
350  vpush (eusstr);//
351  pelem = rawcons (ctx, eusstr, NIL);
352  vpush (pelem);//
353  pelem = rawcons (ctx, K_FILENAME, pelem);
354  vpush (pelem);//
355  lmaterial = rawcons (ctx, pelem, lmaterial);
356  vpop(); vpop(); vpop();
357  vpush (lmaterial); lpc++;
358 #if DEBUG
359  std::cerr << ";; material properties: Texture: ";
360  std::string str; str.assign(fpath.C_Str());
361  std::cerr << "file: " << str << std::endl;
362  std::cerr << " / uv_index: " << uvindex << std::endl;
363  std::cerr << " / blend: " << blend << std::endl;
364  std::cerr << " / Mapping: " << mapping << std::endl;
365  std::cerr << " / Op: " << op << std::endl;
366  std::cerr << " / Mode: " << mapmode << std::endl;
367 #endif
368  }
369  }
370 
371  // finalize material
372  pointer tmp = rawcons (ctx, lmaterial, NIL);
373  vpush (tmp); lpc++;
374  tmp = rawcons (ctx, K_MATERIAL, tmp);
375  vpush (tmp); lpc++;
376  ret = rawcons (ctx, tmp, ret);
377  while(lpc-- > 0) vpop();
378 
379  vpush (ret); npc++;
380  }
381 
382  if (!!amesh->mNormals) {
383  nom_mat = makematrix (ctx, amesh->mNumVertices, 3); vpush (nom_mat); npc++;
384  nom_vec = nom_mat->c.ary.entity->c.fvec.fv;
385  }
386  ver_mat = makematrix (ctx, amesh->mNumVertices, 3); vpush (ver_mat); npc++;
387  ver_vec = ver_mat->c.ary.entity->c.fvec.fv;
388 
389  // count indices
390  int icount = 0;
391  {
392  for (unsigned int f = 0; f < amesh->mNumFaces; f++) {
393  icount += amesh->mFaces[f].mNumIndices;
394  }
395  indices = makevector (C_INTVECTOR, icount);
396  vpush (indices); npc++;
397  }
398  // fill indices
399  {
400  eusinteger_t *vec = indices->c.ivec.iv;
401  int icount = 0;
402  for (unsigned int f = 0; f < amesh->mNumFaces; f++) {
403  for(unsigned int p = 0; p < amesh->mFaces[f].mNumIndices; p++) {
404  vec[icount++] = amesh->mFaces[f].mIndices[p];
405  }
406  }
407  }
408  // add indices to return list
409  {
410  pointer tmp;
411  tmp = rawcons (ctx, indices, NIL);
412  vpush (tmp);
413  tmp = rawcons (ctx, K_INDICES, tmp);
414  ret = rawcons (ctx, tmp, ret);
415  vpop();
416  vpush (ret); npc++;
417  }
418  // texcoords
419  {
420  int texcount = 0;
421  if (amesh->HasTextureCoords(texcount)) {
422  tex_cds = makefvector (2 * amesh->mNumVertices);
423  tex_vec = tex_cds->c.fvec.fv;
424  for (unsigned int v = 0; v < amesh->mNumVertices; v++) {
425  *tex_vec++ = amesh->mTextureCoords[texcount][v].x;
426  *tex_vec++ = (1.0 - amesh->mTextureCoords[texcount][v].y); // why swap horizontal line
427  }
428  }
429  if (!!tex_vec) {
430  pointer tmp;
431  tmp = rawcons (ctx, tex_cds, NIL);
432  vpush (tmp);
433  tmp = rawcons (ctx, K_TEXCOORDS, tmp);
434  ret = rawcons (ctx, tmp, ret);
435  vpop();
436  vpush (ret); npc++;
437  }
438  }
439 
440  // make vetices and normal matrix
441  int vcount=0, ncount=0;
442  for (unsigned int i = 0; i < amesh->mNumVertices; ++i) {
443  aiQuaternion rot;
444  aiVector3D pos;
445  trans.DecomposeNoScaling(rot, pos);
446  aiVector3D tvec (amesh->mVertices[i]);
447  tvec *= trans;
448  tvec *= base_scl;
449  switch (direc) {
450  case 0: // X_UP
451  ver_vec[vcount++] = - tvec.z;
452  ver_vec[vcount++] = tvec.y;
453  ver_vec[vcount++] = tvec.x;
454  break;
455  case 1: // Y_UP
456  ver_vec[vcount++] = tvec.x;
457  ver_vec[vcount++] = - tvec.z;
458  ver_vec[vcount++] = tvec.y;
459  break;
460  case 2: // Z_UP
461  ver_vec[vcount++] = tvec.x;
462  ver_vec[vcount++] = tvec.y;
463  ver_vec[vcount++] = tvec.z;
464  break;
465  case 3: // -X_UP
466  ver_vec[vcount++] = tvec.z;
467  ver_vec[vcount++] = tvec.y;
468  ver_vec[vcount++] = - tvec.x;
469  break;
470  case 4: // -Y_UP
471  ver_vec[vcount++] = tvec.x;
472  ver_vec[vcount++] = tvec.z;
473  ver_vec[vcount++] = - tvec.y;
474  break;
475  }
476 
477  if ( !!nom_vec ) {
478  aiVector3D tnom = rot.Rotate (amesh->mNormals[i]);
479  switch (direc) {
480  case 0:
481  nom_vec[ncount++] = - tnom.z;
482  nom_vec[ncount++] = tnom.y;
483  nom_vec[ncount++] = tnom.x;
484  break;
485  case 1:
486  nom_vec[ncount++] = tnom.x;
487  nom_vec[ncount++] = - tnom.z;
488  nom_vec[ncount++] = tnom.y;
489  break;
490  case 2:
491  nom_vec[ncount++] = tnom.x;
492  nom_vec[ncount++] = tnom.y;
493  nom_vec[ncount++] = tnom.z;
494  break;
495  case 3:
496  nom_vec[ncount++] = tnom.z;
497  nom_vec[ncount++] = tnom.y;
498  nom_vec[ncount++] = - tnom.x;
499  break;
500  case 4:
501  nom_vec[ncount++] = tnom.x;
502  nom_vec[ncount++] = tnom.z;
503  nom_vec[ncount++] = - tnom.y;
504  break;
505  }
506  }
507  }
508  //
509  if ( !!nom_vec ) {
510  pointer tmp;
511  tmp = rawcons (ctx, nom_mat , NIL);
512  vpush (tmp);
513  tmp = rawcons (ctx, K_NORMALS , tmp);
514  ret = rawcons (ctx, tmp, ret);
515  vpop();
516  vpush (ret); npc++;
517  }
518  //
519  {
520  pointer tmp;
521  tmp = rawcons (ctx, ver_mat , NIL);
522  vpush (tmp);
523  tmp = rawcons (ctx, K_VERTICES , tmp);
524  ret = rawcons (ctx, tmp, ret);
525  vpop();
526  vpush (ret); npc++;
527  }
528 
529  for (;npc > 0; npc--) vpop();
530 
531  return ret;
532 }
533 
534 static void register_all_nodes (register context *ctx, eusfloat_t base_scl,
535  const aiScene *scene, const aiNode *node,
536  const aiMatrix4x4 &parent_world_trans, int direc,
537  std::vector<pointer> &mesh_info) {
538  aiMatrix4x4 node_world_trans = parent_world_trans * node->mTransformation;
539 #if DEBUG
540  fprintf (stderr, "node : %lX\n", (void *)node);
541  printTransform (node_world_trans, "");
542 #endif
543  if (node->mNumMeshes > 0 && node->mMeshes != NULL) {
544  for (unsigned int n = 0; n < node->mNumMeshes; n++) {
545  aiMesh *am = scene->mMeshes[node->mMeshes[n]];
546 #if DEBUG
547  fprintf (stderr, " mesh: %d", node->mMeshes[n]);
548  std::cerr << ";; mesh = " << (void *)am << std::endl;
549 #endif
550  pointer ret = store_mesh_info (ctx, base_scl, scene, node, am, node_world_trans, direc);
551  vpush (ret);
552  mesh_info.push_back (ret);
553  }
554  }
555 #if DEBUG
556  fprintf(stderr, "\n");
557  std::cerr << " children: " << node->mNumChildren << std::endl;
558 #endif
559  for (unsigned int c = 0; c < node->mNumChildren; c++) {
560  register_all_nodes (ctx, base_scl,
561  scene, node->mChildren[c],
562  node_world_trans, direc, mesh_info);
563  }
564 }
565 
566 pointer GET_MESHES(register context *ctx,int n,pointer *argv)
567 {
568  /* filename &optional scale options direction dumpfilename */
569  eusfloat_t base_scl = 1.0;
570  numunion nu;
571 
572  ckarg2(1, 5);
573  if (!isstring(argv[0])) error (E_NOSTRING);
574  if (n > 1) {
575  base_scl = ckfltval (argv[1]);
576  //fprintf(stderr, ";; scale = %f\n", base_scl);
577  }
578 
579  Assimp::Importer importer;
580 
581  { // ignore UP_DIRECTION tag in collada
582  bool existing;
583  importer.SetPropertyBool(AI_CONFIG_IMPORT_COLLADA_IGNORE_UP_DIRECTION, true, &existing);
584  if(existing) {
585  fprintf(stderr, ";; OverWrite : Ignore UP_DIRECTION", existing);
586  }
587  }
588  unsigned int post_proc = (aiProcess_Triangulate | aiProcess_SortByPType);
589  bool recalc_normal = false;
590  char *dumpfile = NULL;
591  int direction = 2; // :X_UP 0, :Y_UP 1, :Z_UP 2
592 
593  if (n > 2) {
594  post_proc = ckintval(argv[2]);
595  //fprintf(stderr, ";; option = %X\n", post_proc);
596  if((post_proc & aiProcess_GenNormals) ||
597  (post_proc & aiProcess_GenSmoothNormals)) {
598  recalc_normal = true;
599  if((post_proc & aiProcess_GenNormals) &&
600  (post_proc & aiProcess_GenSmoothNormals)) {
601  post_proc &= ~aiProcess_GenNormals;
602  }
603  }
604  }
605  if (n > 3) {
606  direction = ckintval(argv[3]);
607  }
608  if (n > 4) {
609  if (argv[4] != NIL) {
610  if (!isstring(argv[4])) error(E_NOSTRING);
611  dumpfile = (char *)get_string(argv[4]);
612  }
613  }
614  const aiScene* raw_scene = importer.ReadFile ((char *)get_string(argv[0]), 0);
615 
616  if (!raw_scene) {
617  std::string str (importer.GetErrorString());
618  std::cerr << ";; " << str << std::endl;
619  return NIL;
620  }
621 #if DEBUG
622  std::cerr << ";; Num meshes(raw): " << raw_scene->mNumMeshes << std::endl;
623 #endif
624  if (recalc_normal) {
625 #if DEBUG
626  std::cerr << ";; Recalc_normal" << std::endl;
627 #endif
628  for (unsigned int m = 0; m < raw_scene->mNumMeshes; m++) {
629  aiMesh *a = raw_scene->mMeshes[m];
630  if (!!a->mNormals) { a->mNormals = NULL; }
631  }
632  }
633 
634  const aiScene* scene = importer.ApplyPostProcessing (post_proc);
635  if (!scene) {
636  std::string str (importer.GetErrorString());
637  std::cerr << ";; " << str << std::endl;
638  return NIL;
639  }
640 #if DEBUG
641  std::cerr << ";; Num meshes: " << scene->mNumMeshes << std::endl;
642  // travarse nodes and store
643  printNode (scene->mRootNode, ";;");
644 #endif
645 
646  pointer lst = NIL;
647  {
648  aiMatrix4x4 world_trans;
649  std::vector<pointer> mesh_info;
650  register_all_nodes (ctx, base_scl, scene,
651  scene->mRootNode, world_trans, direction, mesh_info);
652 #if DEBUG
653  std::cerr << ";; mesh size: " << mesh_info.size() << std::endl;
654 #endif
655  for (std::vector<pointer>::reverse_iterator rit = mesh_info.rbegin();
656  rit != mesh_info.rend(); rit++) {
657  vpush(lst);
658  lst = rawcons (ctx, *rit, lst);
659  vpop(); // vpop(); // pop for mesh_info
660  }
661  vpush (lst);
662  }
663 
664  if (!!dumpfile) {
665  std::string outf, outext;
666  outf.assign (dumpfile);
667  {
668  const std::string::size_type s = outf.find_last_of('.');
669  if (s != std::string::npos) {
670  outext = outf.substr (s+1);
671  } else {
672  std::cerr << ";; Can not find extention: " << outf << std::endl;
673  }
674  }
675  Assimp::Exporter exporter;
676  size_t outfi = SIZE_T_MAX;
677  for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
678  const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
679  if (outext == e->id) {
680  outfi = i; break;
681  } else if (outext == e->fileExtension) {
682  outfi = i; break;
683  }
684  }
685  if (outfi == SIZE_T_MAX) {
686  outext = "stl";
687  for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
688  const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
689  if (outext == e->fileExtension) {
690  outfi = i; break;
691  }
692  }
693  }
694  if (outfi == SIZE_T_MAX) outfi = 0;
695 
696  const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(outfi);
697 #if DEBUG
698  fprintf (stderr, ";; use file format: %s\n", e->id);
699 #endif
700  aiReturn ret = exporter.Export (scene, e->id, outf);
701  }
702 
703  vpop(); // vpush (lst);
704  return lst;
705 }
706 
707 pointer DUMP_GL_VERTICES(register context *ctx,int n,pointer *argv)
708 {
709  /* filename type-list material-list vertices-list normals-list texcoords-list indices-list
710  (scale) (gen_normal nil) (smooth_normal nil) (split_large_mesh nil)
711  (optimize_mesh nil) (identical_vert nil) (fix_normal) (direction)
712  */
713  // FIXME: name_list
714  ckarg2(7, 14);
715  numunion nu;
716  int direction = 2;
717  char *dumpfile = NULL;
718  if (!isstring(argv[0])) error(E_NOSTRING);
719  dumpfile = (char *)get_string(argv[0]);
720 
721  int list_len = 0;
722  {
723  pointer a = argv[1];
724  while (islist(a)) {list_len++; a=ccdr(a);}
725  }
726  if (list_len <= 0) return NIL;
727  bool has_materials = false;
728  pointer ltype, linfo, lvertices, lnormals, ltexcoords, lindices;
729  pointer mtype, minfo, mvertices, mnormals, mtexcoords, mindices;
730  eusfloat_t scale = 1.0;
731 
732  ltype = argv[1];
733  linfo = argv[2];
734  lvertices = argv[3];
735  lnormals = argv[4];
736  ltexcoords = argv[5];
737  lindices = argv[6];
738  if (n > 7) {
739  scale = fltval(argv[7]);
740  }
741  if (n > 13) {
742  direction = intval(argv[13]);
743  }
744 
745  // assimp loader
746  aiScene *pScene = (aiScene *) malloc(sizeof (aiScene));
747  memset((void *)pScene, 0, sizeof (aiScene));
748  pScene->mPrivate = malloc(sizeof (void *) * 2);
749  //printf("scene = %lX\n", (void *)pScene);
750 
751  pScene->mNumMeshes = list_len;
752  pScene->mMeshes = new aiMesh*[list_len];
753 
754  // allocate a single node
755  pScene->mRootNode = new aiNode();
756  pScene->mRootNode->mNumMeshes = list_len;
757  pScene->mRootNode->mMeshes = new unsigned int[list_len];
758  pScene->mRootNode->mName.Set("eus_glvertices");
759 
760  if (linfo != NIL) {
761  pScene->mNumMaterials = list_len;
762  pScene->mMaterials = new aiMaterial*[list_len];
763  has_materials = true;
764  } else {
765  pScene->mNumMaterials = 1;
766  pScene->mMaterials = new aiMaterial*[1];
767  has_materials = false;
768  // make default material
769  aiMaterial* pcMat = new aiMaterial();
770  aiString s; s.Set (AI_DEFAULT_MATERIAL_NAME);
771  pcMat->AddProperty (&s, AI_MATKEY_NAME);
772  aiColor4D clrDiffuse(0.8f, 0.8f, 0.8f, 1.0f);
773  pcMat->AddProperty (&clrDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
774  pcMat->AddProperty (&clrDiffuse, 1, AI_MATKEY_COLOR_SPECULAR);
775  clrDiffuse = aiColor4D (0.2f, 0.2f, 0.2f, 1.0f);
776  pcMat->AddProperty (&clrDiffuse, 1, AI_MATKEY_COLOR_AMBIENT);
777 
778  pScene->mMaterials[0] = pcMat;
779  }
780 
781  for (int i = 0; i < list_len; i++) {
782  mtype = ccar (ltype);
783  minfo = ccar (linfo);
784  mvertices = ccar (lvertices);
785  mnormals = ccar (lnormals);
786  mtexcoords = ccar (ltexcoords);
787  mindices = ccar (lindices);
788 
789  ltype = ccdr (ltype);
790  linfo = ccdr (linfo);
791  lvertices = ccdr (lvertices);
792  lnormals = ccdr (lnormals);
793  ltexcoords = ccdr (ltexcoords);
794  lindices = ccdr (lindices);
795 
796  pScene->mRootNode->mMeshes[i] = i;
797  aiMesh* pMesh = pScene->mMeshes[i] = new aiMesh();
798  // std::cerr << ";; mesh = " << (void *)pMesh << std::endl;
799  if (!has_materials) {
800  pMesh->mMaterialIndex = 0;
801  } else {
802  pMesh->mMaterialIndex = i;
803  // material
804  aiMaterial* pcMat = new aiMaterial();
805  std::ostringstream oss;
806  oss << "eusassimp_mat_" << i;
807  aiString s; s.Set (oss.str());
808  pcMat->AddProperty(&s, AI_MATKEY_NAME);
809 
810  for (int el = 0; el < 7; el++) {
811  // (list :ambient :diffuse :specular :emission :shininess :transparency :filename)
812  pointer melem = ccar (minfo);
813  minfo = ccdr (minfo);
814  aiColor4D clr4d (0.0f, 0.0f, 0.0f, 1.0f);
815  switch (el) {
816  case 0:
817  if (melem != NIL) {
818  for(int j = 0; j < intval(melem->c.fvec.length); j++) {
819  clr4d[j] = melem->c.fvec.fv[j];
820  }
821  //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
822  pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_AMBIENT);
823  }
824  break;
825  case 1:
826  if (melem != NIL) {
827  for(int j = 0; j < intval(melem->c.fvec.length); j++) {
828  clr4d[j] = melem->c.fvec.fv[j];
829  }
830  //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
831  pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_DIFFUSE);
832  }
833  break;
834  case 2:
835  if (melem != NIL) {
836  for(int j = 0; j < intval(melem->c.fvec.length); j++) {
837  clr4d[j] = melem->c.fvec.fv[j];
838  }
839  //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
840  pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_SPECULAR);
841  }
842  break;
843  case 3:
844  if (melem != NIL) {
845  for(int j = 0; j < intval(melem->c.fvec.length); j++) {
846  clr4d[j] = melem->c.fvec.fv[j];
847  }
848  //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
849  pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_EMISSIVE);
850  }
851  break;
852  case 4:
853  if (melem != NIL) {
854  float val = fltval (melem);
855  pcMat->AddProperty(&val, 1, AI_MATKEY_SHININESS);
856  }
857  break;
858  case 5:
859  if (melem != NIL) {
860  if (isnum (melem)) {
861  float val = isflt (melem) ? fltval (melem) : (float)intval (melem);
862  //printf("trans: %f\n", val);
863  for(int j = 0; j < 4; j++) {
864  clr4d[j] = val;
865  }
866  } else {
867  for(int j = 0; j < intval(melem->c.fvec.length); j++) {
868  clr4d[j] = melem->c.fvec.fv[j];
869  }
870  }
871  //printf("%d: %f %f %f %f\n", el, clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
872  pcMat->AddProperty(&clr4d, 1, AI_MATKEY_COLOR_TRANSPARENT);
873  }
874  break;
875  case 6: // texture
876  if (melem != NIL) {
877  if (isstring(melem)) {
878  //printf(";; texture file: %s\n", melem->c.str.chars);
879  aiString s; s.Set ((const char *)(melem->c.str.chars));
880  pcMat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(0));
881  }
882  }
883  break;
884  }
885  }
886  pScene->mMaterials[i] = pcMat;
887  }
888  // vertices
889  if (mvertices == NIL) { /* error */ }
890  eusinteger_t size = intval (mvertices->c.ary.entity->c.fvec.length);
891  eusfloat_t *fv = mvertices->c.ary.entity->c.fvec.fv;
892  pMesh->mNumVertices = size / 3;
893  pMesh->mVertices = new aiVector3D [pMesh->mNumVertices];
894  for (unsigned int k = 0; k < pMesh->mNumVertices; k++) {
895  pMesh->mVertices[k].x = scale * *fv++;
896  pMesh->mVertices[k].y = scale * *fv++;
897  pMesh->mVertices[k].z = scale * *fv++;
898  }
899  // normals
900  if (mnormals != NIL) {
901  eusinteger_t size = intval (mnormals->c.ary.entity->c.fvec.length);
902  eusfloat_t *fv = mnormals->c.ary.entity->c.fvec.fv;
903  if (size / 3 != pMesh->mNumVertices) { /* error */ }
904  pMesh->mNormals = new aiVector3D [pMesh->mNumVertices];
905  for (unsigned int k = 0; k < pMesh->mNumVertices; k++) {
906  pMesh->mNormals[k].x = *fv++;
907  pMesh->mNormals[k].y = *fv++;
908  pMesh->mNormals[k].z = *fv++;
909  }
910  }
911  // texcoords
912  if (mtexcoords != NIL) {
913  printf(";; add texture coords\n");
914  eusinteger_t size = intval (mtexcoords->c.fvec.length);
915  eusfloat_t *fv = mtexcoords->c.fvec.fv;
916  if (size / 2 != pMesh->mNumVertices) { /* error */ }
917  // use just 1 texture
918  pMesh->mTextureCoords[0] = new aiVector3D [pMesh->mNumVertices];
919  for (unsigned int k = 0; k < pMesh->mNumVertices; k++) {
920  pMesh->mTextureCoords[0][k].x = *fv++;
921  pMesh->mTextureCoords[0][k].y = (1.0 - *fv++);
922  }
923  }
924 
925  unsigned int verts_per_face = intval(mtype);
926  if (mindices != NIL) {
927  if (verts_per_face != 4 && verts_per_face != 3) {
928  /* variable face size */
929  // not implemented yet
930  } else {
931  eusinteger_t *iv = mindices->c.ivec.iv;
932  eusinteger_t idlen = intval (mindices->c.ivec.length);
933  pMesh->mNumFaces = idlen / verts_per_face;//
934 
935  pMesh->mFaces = new aiFace[pMesh->mNumFaces];
936  for (unsigned int f = 0; f < pMesh->mNumFaces; f++) {
937  aiFace& face = pMesh->mFaces[f];
938  face.mNumIndices = verts_per_face;
939  face.mIndices = new unsigned int[verts_per_face];
940  for (unsigned int o = 0; o < verts_per_face; o++) {
941  face.mIndices[o] = *iv++;
942  }
943  }
944  }
945  } else {
946  // add indices
947  if (verts_per_face != 4 && verts_per_face != 3) { /* error */ }
948  pMesh->mNumFaces = pMesh->mNumVertices / verts_per_face;
949  pMesh->mFaces = new aiFace[pMesh->mNumFaces];
950  for (unsigned int f = 0, p = 0; f < pMesh->mNumFaces; f++) {
951  aiFace& face = pMesh->mFaces[f];
952  face.mNumIndices = verts_per_face;
953  face.mIndices = new unsigned int[verts_per_face];
954  for (unsigned int o = 0; o < verts_per_face; o++, p++) {
955  face.mIndices[o] = p;
956  }
957  }
958  }
959  }
960 
961  if (!!dumpfile) {
962  std::string outf, outext;
963  outf.assign (dumpfile);
964  {
965  const std::string::size_type s = outf.find_last_of('.');
966  if (s != std::string::npos) {
967  outext = outf.substr (s+1);
968  } else {
969  std::cerr << ";; Can not find extention: " << outf << std::endl;
970  }
971  }
972 
973  Assimp::Exporter exporter;
974  size_t outfi = SIZE_T_MAX;
975  for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
976  const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
977  if (outext == e->id) {
978  outfi = i; break;
979  } else if (outext == e->fileExtension) {
980  outfi = i; break;
981  }
982  }
983  if (outfi == SIZE_T_MAX) {
984  outext = "stl";
985  for(size_t i = 0, end = exporter.GetExportFormatCount(); i < end; ++i) {
986  const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(i);
987  if (outext == e->fileExtension) {
988  outfi = i; break;
989  }
990  }
991  }
992  if (outfi == SIZE_T_MAX) outfi = 0;
993  const aiExportFormatDesc* const e = exporter.GetExportFormatDescription(outfi);
994  aiReturn ret = exporter.Export (pScene, e->id, outf);
995  }
996 
997  return NIL;
998 }
999 
1000 pointer CONVEX_DECOMP_GL_VERTICES(register context *ctx, int n, pointer *argv)
1001 {
1002  /* vertices-list indices-list
1003  skinwidth decomposition-depth max-hull-vertices
1004  concavity-threshold merge-threshold volume-split-threshold */
1005 
1006  numunion nu;
1007  int verts_per_face = 3;
1008  pointer lvertices = argv[0];
1009  pointer lindices = argv[1];
1010  pointer ret = NIL;
1011 
1012 #if COMPILE_CONVEX_DECOMPOSITION
1013  ckarg2(2, 8);
1014 
1015  NxF32 skinWidth = 0;
1016  NxU32 decompositionDepth = 4;
1017  NxU32 maxHullVertices = 64;
1018  NxF32 concavityThresholdPercent = 0.1f;
1019  NxF32 mergeThresholdPercent = 20;
1020  NxF32 volumeSplitThresholdPercent = 2;
1021 
1022  if (n > 1) {
1023  skinWidth = ckfltval(argv[2]);
1024  }
1025  if (n > 2) {
1026  decompositionDepth = ckintval(argv[3]);
1027  }
1028  if (n > 3) {
1029  maxHullVertices = ckintval(argv[4]);
1030  }
1031  if (n > 4) {
1032  concavityThresholdPercent = ckfltval(argv[5]);
1033  }
1034  if (n > 5) {
1035  mergeThresholdPercent = ckfltval(argv[6]);
1036  }
1037  if (n > 6) {
1038  volumeSplitThresholdPercent = ckfltval(argv[7]);
1039  }
1040 
1041  int list_len = 0;
1042  {
1043  pointer a = lvertices;
1044  while (islist (a)) {list_len++; a=ccdr(a);}
1045  }
1046  //
1047  CONVEX_DECOMPOSITION::iConvexDecomposition *ic = CONVEX_DECOMPOSITION::createConvexDecomposition();
1048 
1049  // store all vertices
1050  for (int i = 0; i < list_len; i++) {
1051  pointer mvertices = ccar (lvertices);
1052  pointer mindices = ccar (lindices);
1053 
1054  lvertices = ccdr (lvertices);
1055  lindices = ccdr (lindices);
1056 
1057  if (mvertices == NIL) { /* error */ }
1058  eusinteger_t size = intval (mvertices->c.ary.entity->c.fvec.length);
1059  eusfloat_t *fv = mvertices->c.ary.entity->c.fvec.fv;
1060 
1061  if (mindices != NIL) {
1062  eusinteger_t *iv = mindices->c.ivec.iv;
1063  eusinteger_t idlen = intval (mindices->c.ivec.length);
1064  for (unsigned int f = 0; f < idlen / verts_per_face; f++) {
1065  eusinteger_t i1 = *iv++;
1066  eusinteger_t i2 = *iv++;
1067  eusinteger_t i3 = *iv++;
1068  NxF32 p1[3];
1069  NxF32 p2[3];
1070  NxF32 p3[3];
1071 
1072  p1[0] = fv[3*i1];
1073  p1[1] = fv[3*i1+1];
1074  p1[2] = fv[3*i1+2];
1075 
1076  p2[0] = fv[3*i2];
1077  p2[1] = fv[3*i2+1];
1078  p2[2] = fv[3*i2+2];
1079 
1080  p3[0] = fv[3*i3];
1081  p3[1] = fv[3*i3+1];
1082  p3[2] = fv[3*i3+2];
1083 
1084  ic->addTriangle (p1, p2, p3);
1085  }
1086  } else {
1087  int step = (3 * verts_per_face);
1088  for (unsigned int f = 0; f < size / step; f++) {
1089  NxF32 p1[3];
1090  NxF32 p2[3];
1091  NxF32 p3[3];
1092 
1093  p1[0] = fv[f * step + 0];
1094  p1[1] = fv[f * step + 1];
1095  p1[2] = fv[f * step + 2];
1096 
1097  p2[0] = fv[f * step + 3];
1098  p2[1] = fv[f * step + 4];
1099  p2[2] = fv[f * step + 5];
1100 
1101  p3[0] = fv[f * step + 6];
1102  p3[1] = fv[f * step + 7];
1103  p3[2] = fv[f * step + 8];
1104 
1105  ic->addTriangle (p1, p2, p3);
1106  }
1107  }
1108  }
1109 
1110  //NxF32 skinWidth = 0;
1111  //NxU32 decompositionDepth = 4;
1112  //NxU32 maxHullVertices = 64;
1113  //NxF32 concavityThresholdPercent = 0.1f;
1114  //NxF32 mergeThresholdPercent = 20;
1115  //NxF32 volumeSplitThresholdPercent = 2;
1116  bool useInitialIslandGeneration = true;
1117  bool useIslandGeneration = false;
1118  bool useBackgroundThreads = false;
1119 
1120  ic->computeConvexDecomposition (skinWidth,
1121  decompositionDepth,
1122  maxHullVertices,
1123  concavityThresholdPercent,
1124  mergeThresholdPercent,
1125  volumeSplitThresholdPercent,
1126  useInitialIslandGeneration,
1127  useIslandGeneration,
1128  useBackgroundThreads);
1129 
1130  while ( !ic->isComputeComplete() ) {
1131 #if DEBUG
1132  fprintf (stderr, "Computing the convex decomposition in a background thread.\r\n");
1133 #endif
1134  sleep(1);
1135  // Sleep(1000);
1136  }
1137  // finish process
1138 
1139  NxU32 hullCount = ic->getHullCount();
1140 #if DEBUG
1141  fprintf (stderr, "Convex Decomposition produced %d hulls.\r\n", hullCount );
1142 #endif
1143  for (NxU32 i = 0; i < hullCount; i++) {
1144  //
1145  pointer tmpret = NIL;
1146  int npc = 0;
1147  // name
1148  {
1149  std::ostringstream oss;
1150  oss << "convex_" << i;
1151  pointer lname = makestring ((char *)oss.str().c_str(), oss.str().length());
1152  vpush (lname);
1153  lname = rawcons (ctx, lname , NIL);
1154  vpop (); vpush (lname);
1155  lname = rawcons (ctx, K_NAME, lname);
1156  vpop (); vpush (lname);
1157  tmpret = rawcons (ctx, lname, tmpret);
1158  vpop ();
1159  vpush (tmpret); npc++;
1160  }
1161  // type
1162  {
1163  pointer ltype = K_TRIANGLES;
1164  ltype = rawcons (ctx, ltype , NIL);
1165  vpush (ltype);
1166  ltype = rawcons (ctx, K_TYPE, ltype);
1167  vpop(); vpush (ltype);
1168  tmpret = rawcons (ctx, ltype, tmpret);
1169  vpop();
1170  vpush (tmpret); npc++;
1171  }
1172 
1173  CONVEX_DECOMPOSITION::ConvexHullResult result;
1174  ic->getConvexHullResult(i, result);
1175 
1176  pointer ver_mat = makematrix (ctx, result.mVcount, 3); vpush (ver_mat); npc++;
1177  eusfloat_t *ver_vec = ver_mat->c.ary.entity->c.fvec.fv;
1178 
1179  pointer indices = makevector (C_INTVECTOR, result.mTcount * 3);
1180  eusinteger_t *vec = indices->c.ivec.iv;
1181  vpush (indices); npc++;
1182 
1183  for (NxU32 i = 0; i < result.mVcount * 3; i++) {
1184  ver_vec[i] = result.mVertices[i];
1185  }
1186  for (NxU32 i = 0; i < result.mTcount * 3; i++) {
1187  vec[i] = result.mIndices[i];
1188  }
1189  // add indices to return list
1190  {
1191  pointer tmp;
1192  tmp = rawcons (ctx, indices, NIL);
1193  vpush (tmp);
1194  tmp = rawcons (ctx, K_INDICES, tmp);
1195  tmpret = rawcons (ctx, tmp, tmpret);
1196  vpop();
1197  vpush (tmpret); npc++;
1198  }
1199  //
1200  {
1201  pointer tmp;
1202  tmp = rawcons (ctx, ver_mat , NIL);
1203  vpush (tmp);
1204  tmp = rawcons (ctx, K_VERTICES , tmp);
1205  tmpret = rawcons (ctx, tmp, tmpret);
1206  vpop();
1207  vpush (tmpret); npc++;
1208  }
1209 
1210  vpush(ret); npc++;
1211  ret = rawcons (ctx, tmpret, ret);
1212  for (;npc > 0; npc--) vpop();
1213  vpush(ret);
1214  }
1215 
1216  CONVEX_DECOMPOSITION::releaseConvexDecomposition(ic);
1217  vpop(); // pop ret
1218 #else
1219  fprintf(stderr, ";;This program have not been compiled with convex decomposition");
1220 #endif
1221 
1222  return ret;
1223 }
1224 
1225 pointer ASSIMP_DESCRIBE(register context *ctx,int n,pointer *argv)
1226 {
1227  /* */
1228  ckarg(1);
1229  if (!isstring(argv[0])) error(E_NOSTRING);
1230 
1231  Assimp::Importer importer;
1232  const aiScene* scene = importer.ReadFile((char *)get_string(argv[0]),
1233  aiProcess_Triangulate |
1234  aiProcess_JoinIdenticalVertices |
1235  aiProcess_SortByPType);
1236 
1237  if (!scene) {
1238  std::string str( importer.GetErrorString() );
1239  std::cerr << ";; " << str << std::endl;
1240  //std::cerr << ";; file: " << get_string(argv[0]) << " not found" << std::endl;
1241  return NIL;
1242  }
1243 
1244  std::cerr << ";; scene->NumAnimations " << scene->mNumAnimations << std::endl;
1245  //aiAnimation ** mAnimations;
1246 
1247  std::cerr << ";; scene->mNumCameras " << scene->mNumCameras << std::endl;
1248  //aiCamera ** mCameras;
1249 
1250  std::cerr << ";; scene->mNumLights " << scene->mNumLights << std::endl;
1251  //aiLight ** mLights;
1252 
1253  std::cerr << ";; scene->mNumMaterials " << scene->mNumMaterials << std::endl;
1254  for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
1255  printMaterial(scene->mMaterials[i], i);
1256  }
1257  //aiMaterial ** mMaterials;
1258 
1259  std::cerr << ";; scene->mNumTextures " << scene->mNumTextures << std::endl;
1260  //aiTexture ** mTextures;
1261 
1262  std::cerr << ";; scene->mFlags " << scene->mFlags << std::endl;
1263 
1264  std::cerr << ";; scene->mNumMeshes " << scene->mNumMeshes << std::endl;
1265 
1266  //aiMesh ** scene->mMeshes;
1267  for ( unsigned int i = 0; i < scene->mNumMeshes; i++ ) {
1268  printMesh( scene->mMeshes[i] , "");
1269  }
1270 
1271  printNode( scene->mRootNode , "");
1272 
1273  return NIL;
1274 }
1275 
1276 #if USE_SDL_IMAGE
1277 pointer ASSIMP_LOAD_IMAGE(register context *ctx,int n,pointer *argv)
1278 {
1279  pointer ret = NIL;
1280  ckarg(1);
1281  if (!isstring(argv[0])) error (E_NOSTRING);
1282 
1283  SDL_Surface *sf = IMG_Load((char *)get_string(argv[0]));
1284  if (!!sf) {
1285  pointer ptr;
1286  int byteperpixel = sf->format->BytesPerPixel;
1287  pointer img_buffer = makestring ((char *)sf->pixels, sf->h * sf->w * byteperpixel);
1288 #if 0
1289  // should check RGB and RGBA ...
1290  if (!!(sf->format)) {
1291  printf("fmt:%0X, byte/pixel %d, MASK(RGBA): %08X %08X %08X %08X\n",
1292  //sf->format->format,
1293  sf->flags,
1294  sf->format->BytesPerPixel,
1295  sf->format->Rmask,
1296  sf->format->Gmask,
1297  sf->format->Bmask,
1298  sf->format->Amask);
1299  }
1300  printf("width: %d, height: %d, pitch: %d, pixels: %lX\n",
1301  sf->w,
1302  sf->h,
1303  sf->pitch,
1304  sf->pixels);
1305 #endif
1306  if (!!(sf->format)) {
1307  if (sf->format->BytesPerPixel == 3) {
1308  if ( sf->format->Rmask != 0x000000FF ||
1309  sf->format->Gmask != 0x0000FF00 ||
1310  sf->format->Bmask != 0x00FF0000 ) {
1311  int rm = 0;
1312  int gm = 0;
1313  int bm = 0;
1314  unsigned int mask;
1315 
1316  mask = 0x000000FF;
1317  for (int i = 0; i < 4; i++) {
1318  if ( mask == sf->format->Rmask ) {
1319  rm = i; break;
1320  }
1321  mask <<= 8;
1322  }
1323  mask = 0x000000FF;
1324  for (int i = 0; i < 4; i++) {
1325  if ( mask == sf->format->Gmask ) {
1326  gm = i; break;
1327  }
1328  mask <<= 8;
1329  }
1330  mask = 0x000000FF;
1331  for (int i = 0; i < 4; i++) {
1332  if ( mask == sf->format->Bmask ) {
1333  bm = i; break;
1334  }
1335  mask <<= 8;
1336  }
1337 #if 0
1338  fprintf(stderr, ";; fmt:%0X, byte/pixel %d, MASK(RGBA): %08X %08X %08X %08X -> %d %d %d\n",
1339  //sf->format->format,
1340  sf->flags,
1341  sf->format->BytesPerPixel,
1342  sf->format->Rmask,
1343  sf->format->Gmask,
1344  sf->format->Bmask,
1345  sf->format->Amask, rm, gm, bm);
1346  fprintf(stderr, ";; width: %d, height: %d, pitch: %d, pixels: %lX\n",
1347  sf->w,
1348  sf->h,
1349  sf->pitch,
1350  sf->pixels);
1351 #endif
1352  unsigned char *src_ptr = (unsigned char *)sf->pixels;
1353  unsigned char *dst_ptr = (unsigned char *)img_buffer->c.str.chars;
1354  for (int i = 0; i < sf->w * sf->h; i++) {
1355  dst_ptr[0] = src_ptr[rm];
1356  dst_ptr[1] = src_ptr[gm];
1357  dst_ptr[2] = src_ptr[bm];
1358  dst_ptr += 3; src_ptr += 3;
1359  }
1360  }
1361  }
1362  if (sf->format->BytesPerPixel == 4) {
1363  if ( sf->format->Rmask != 0x000000FF ||
1364  sf->format->Gmask != 0x0000FF00 ||
1365  sf->format->Bmask != 0x00FF0000 ||
1366  sf->format->Amask != 0xFF000000 ) {
1367  int am = 0;
1368  int rm = 0;
1369  int gm = 0;
1370  int bm = 0;
1371  unsigned int mask;
1372 
1373  mask = 0x000000FF;
1374  for (int i = 0; i < 4; i++) {
1375  if ( mask == sf->format->Rmask ) {
1376  rm = i; break;
1377  }
1378  mask <<= 8;
1379  }
1380  mask = 0x000000FF;
1381  for (int i = 0; i < 4; i++) {
1382  if ( mask == sf->format->Gmask ) {
1383  gm = i; break;
1384  }
1385  mask <<= 8;
1386  }
1387  mask = 0x000000FF;
1388  for (int i = 0; i < 4; i++) {
1389  if ( mask == sf->format->Bmask ) {
1390  bm = i; break;
1391  }
1392  mask <<= 8;
1393  }
1394  mask = 0x000000FF;
1395  for (int i = 0; i < 4; i++) {
1396  if ( mask == sf->format->Amask ) {
1397  am = i; break;
1398  }
1399  mask <<= 8;
1400  }
1401 #if 0
1402  fprintf(stderr, ";; fmt:%0X, byte/pixel %d, MASK(RGBA): %08X %08X %08X %08X -> %d %d %d %d\n",
1403  //sf->format->format,
1404  sf->flags,
1405  sf->format->BytesPerPixel,
1406  sf->format->Rmask,
1407  sf->format->Gmask,
1408  sf->format->Bmask,
1409  sf->format->Amask, rm, gm, bm, am);
1410  fprintf(stderr, ";; width: %d, height: %d, pitch: %d, pixels: %lX\n",
1411  sf->w,
1412  sf->h,
1413  sf->pitch,
1414  sf->pixels);
1415 #endif
1416  unsigned char *src_ptr = (unsigned char *)sf->pixels;
1417  unsigned char *dst_ptr = (unsigned char *)img_buffer->c.str.chars;
1418  for (int i = 0; i < sf->w * sf->h; i++) {
1419  dst_ptr[0] = src_ptr[rm];
1420  dst_ptr[1] = src_ptr[gm];
1421  dst_ptr[2] = src_ptr[bm];
1422  dst_ptr[3] = src_ptr[am];
1423  dst_ptr += 4; src_ptr += 4;
1424  }
1425  }
1426  }
1427  }
1428 
1429  vpush (img_buffer);
1430  ptr = rawcons (ctx, img_buffer, NIL);
1431  vpop(); vpush (ptr);
1432  ptr = rawcons (ctx, K_VERTICES, ptr);
1433  vpop(); vpush (ptr);
1434  ret = rawcons (ctx, ptr, ret);
1435  vpop ();
1436  vpush(ret);
1437 
1438  ptr = rawcons (ctx, makeint(byteperpixel), NIL);
1439  vpush(ptr);
1440  ptr = rawcons (ctx, K_TYPE, ptr);
1441  vpop(); vpush (ptr);
1442  ret = rawcons (ctx, ptr, ret);
1443  vpop (); vpop();
1444  vpush(ret);
1445 
1446  ptr = rawcons (ctx, makeint(sf->h) ,NIL);
1447  vpush(ptr);
1448  ptr = rawcons (ctx, K_HEIGHT, ptr);
1449  vpop(); vpush (ptr);
1450  ret = rawcons (ctx, ptr, ret);
1451  vpop (); vpop();
1452  vpush(ret);
1453 
1454  ptr = rawcons (ctx, makeint(sf->w) ,NIL);
1455  vpush(ptr);
1456  ptr = rawcons (ctx, K_WIDTH, ptr);
1457  vpop(); vpush (ptr);
1458  ret = rawcons (ctx, ptr, ret);
1459  vpop (); vpop();
1460  //vpush(ret);
1461  }
1462 
1463  IMG_Quit();
1464 
1465  return ret;
1466 }
1467 #endif
1468 
1469 pointer ___eus_assimp(register context *ctx, int n, pointer *argv, pointer env)
1470 {
1471  defun(ctx,"C-ASSIMP-GET-GLVERTICES", argv[0], (pointer (*)())GET_MESHES, NULL);
1472  defun(ctx,"C-ASSIMP-DUMP-GLVERTICES", argv[0], (pointer (*)())DUMP_GL_VERTICES, NULL);
1473  defun(ctx,"C-CONVEX-DECOMPOSITION-GLVERTICES", argv[0], (pointer (*)())CONVEX_DECOMP_GL_VERTICES, NULL);
1474  defun(ctx,"C-ASSIMP-DESCRIBE", argv[0], (pointer (*)())ASSIMP_DESCRIBE, NULL);
1475 #if USE_SDL_IMAGE
1476  defun(ctx,"C-ASSIMP-LOAD-IMAGE", argv[0], (pointer (*)())ASSIMP_LOAD_IMAGE, NULL);
1477 #endif
1478 
1479  K_VERTICES = defkeyword(ctx, "VERTICES");
1480  K_NORMALS = defkeyword(ctx, "NORMALS");
1481  K_INDICES = defkeyword(ctx, "INDICES");
1482  K_TEXCOORDS = defkeyword(ctx, "TEXCOORDS");
1483  K_MATERIAL = defkeyword(ctx, "MATERIAL");
1484  K_TYPE = defkeyword(ctx, "TYPE");
1485  K_LINES = defkeyword(ctx, "LINES");
1486  K_TRIANGLES = defkeyword(ctx, "TRIANGLES");
1487  K_QUADS = defkeyword(ctx, "QUADS");
1488  K_POLYGON = defkeyword(ctx, "POLYGON");
1489  K_NAME = defkeyword(ctx, "NAME");
1490  // for material
1491  K_FILENAME = defkeyword(ctx, "FILENAME");
1492  K_AMBIENT = defkeyword(ctx, "AMBIENT");
1493  K_DIFFUSE = defkeyword(ctx, "DIFFUSE");
1494  K_SPECULAR = defkeyword(ctx, "SPECULAR");
1495  K_EMISSION = defkeyword(ctx, "EMISSION");
1496  K_SHININESS = defkeyword(ctx, "SHININESS");
1497  K_TRANSPARENCY = defkeyword(ctx, "TRANSPARENCY");
1498  // for image
1499  K_HEIGHT = defkeyword(ctx, "HEIGHT");
1500  K_WIDTH = defkeyword(ctx, "WIDTH");
1501  return 0;
1502 }
f
pointer rawcons(context *, pointer, pointer)
static pointer K_NAME
Definition: eus_assimp.cpp:65
pointer makeint(eusinteger_t v)
static pointer store_mesh_info(register context *ctx, eusfloat_t base_scl, const aiScene *scene, const aiNode *node, const aiMesh *amesh, const aiMatrix4x4 &trans, int direc)
Definition: eus_assimp.cpp:144
static pointer K_SHININESS
Definition: eus_assimp.cpp:66
static pointer K_POLYGON
Definition: eus_assimp.cpp:65
memset(serverAddr, 0, sizeof(*serverAddr))
static pointer K_TRIANGLES
Definition: eus_assimp.cpp:65
static pointer K_QUADS
Definition: eus_assimp.cpp:65
pointer GET_MESHES(register context *ctx, int n, pointer *argv)
Definition: eus_assimp.cpp:566
struct string str
pointer length
end
byte chars[1]
void register_eus_assimp()
Definition: eus_assimp.cpp:49
static pointer K_INDICES
Definition: eus_assimp.cpp:64
pointer ASSIMP_DESCRIBE(register context *ctx, int n, pointer *argv)
float fv[1]
GLfloat n[6][3]
static void register_all_nodes(register context *ctx, eusfloat_t base_scl, const aiScene *scene, const aiNode *node, const aiMatrix4x4 &parent_world_trans, int direc, std::vector< pointer > &mesh_info)
Definition: eus_assimp.cpp:534
struct arrayheader ary
pointer defkeyword(context *, char *)
pointer makefvector(int)
static pointer K_NORMALS
Definition: eus_assimp.cpp:64
static pointer K_HEIGHT
Definition: eus_assimp.cpp:67
static pointer K_SPECULAR
Definition: eus_assimp.cpp:66
pointer makevector(pointer, int)
pointer DUMP_GL_VERTICES(register context *ctx, int n, pointer *argv)
Definition: eus_assimp.cpp:707
ckarg(2)
void add_module_initializer(char *, pointer(*)())
float ckfltval()
struct intvector ivec
pointer CONVEX_DECOMP_GL_VERTICES(register context *ctx, int n, pointer *argv)
pointer length
union cell::cellunion c
ROS_INFO ROS_ERROR int pointer * argv
pointer entity
result
E_NOSTRING
static pointer K_MATERIAL
Definition: eus_assimp.cpp:64
static void printMesh(aiMesh *mesh, std::string prefix)
Definition: eus_assimp.cpp:87
static pointer K_LINES
Definition: eus_assimp.cpp:65
static pointer K_DIFFUSE
Definition: eus_assimp.cpp:66
face(linetab, int mi, int m, int r, int n)
short s
pointer ___eus_assimp(register context *ctx, int n, pointer *argv, pointer env)
pointer error(enum errorcode ec,...) pointer error(va_alist) va_dcl
long eusinteger_t
static void printMaterial(aiMaterial *material, unsigned int idx)
Definition: eus_assimp.cpp:128
byte * get_string()
pointer defun(context *, char *, pointer, pointer(*)(), char *)
pointer makematrix(context *, int, int)
static pointer K_AMBIENT
Definition: eus_assimp.cpp:66
FILE * outf
static std::string primitiveType(unsigned int tp)
Definition: eus_assimp.cpp:69
static void printTransform(aiMatrix4x4 &transform, std::string prefix)
Definition: eus_assimp.cpp:101
unsigned int step
float fltval()
static pointer K_EMISSION
Definition: eus_assimp.cpp:66
long iv[1]
#define string
Definition: eus_assimp.cpp:42
pointer makestring(char *, int)
static pointer K_TYPE
Definition: eus_assimp.cpp:64
#define NULL
pointer C_INTVECTOR
static pointer K_TEXCOORDS
Definition: eus_assimp.cpp:64
GLfloat v[8][3]
static void printNode(aiNode *node, std::string prefix)
Definition: eus_assimp.cpp:108
static pointer K_FILENAME
Definition: eus_assimp.cpp:66
static pointer K_TRANSPARENCY
Definition: eus_assimp.cpp:66
eusinteger_t intval(pointer p)
#define SIZE_T_MAX
Definition: eus_assimp.cpp:62
static pointer K_WIDTH
Definition: eus_assimp.cpp:67
static pointer K_VERTICES
Definition: eus_assimp.cpp:64
double eusfloat_t
pointer NIL
char a[26]
struct floatvector fvec
pointer makeflt()


eus_assimp
Author(s): Yohei Kakiuchi
autogenerated on Mon May 4 2020 03:39:31