static void
rib_camera(FILE *o, Lib3dsFile *f, Lib3dsMatrix M)
{
  Lib3dsNode *c;
  Lib3dsNode *t;
  const char *name=camera;

  ASSERT(f);
  if (!name) {
    if (f->cameras) {
      name=f->cameras->name;
    }
  }
  if (!name) {
    fprintf(stderr, "***ERROR*** No camera found!\n");
    return;
  }
  c=lib3ds_file_node_by_name(f, name, LIB3DS_CAMERA_NODE);
  t=lib3ds_file_node_by_name(f, name, LIB3DS_TARGET_NODE);
  if (!c || !t) {
    fprintf(stderr, "***ERROR*** Invalid camera/target!\n");
    return;
  }

  lib3ds_matrix_camera(M, c->data.camera.pos, t->data.target.pos, c->data.camera.roll);
  rib_concat_transform(o, M);
  fprintf(o, "Camera \"perspective\" \"float fov\" [%f]\n", c->data.camera.fov);
}
static void
create_node(Lib3dsFile *f, Lib3dsNode *node, FILE *o)
{
  Lib3dsMesh *mesh;
  
  if ((node->type==LIB3DS_OBJECT_NODE) && (strcmp(node->name,"$$$DUMMY")!=0)) {
    mesh=lib3ds_file_mesh_by_name(f, node->name);
    ASSERT(mesh);
    if (mesh) {
      Lib3dsObjectData *d=&node->data.object;

      fprintf(o, "\n\n##\n## Object: %s\n##\n", node->name);
      fprintf(o, "AttributeBegin\n");

      {
        Lib3dsMatrix N,M,X;
        lib3ds_matrix_copy(N, node->matrix);
        lib3ds_matrix_translate_xyz(N, -d->pivot[0], -d->pivot[1], -d->pivot[2]);
        lib3ds_matrix_copy(M, mesh->matrix);
        lib3ds_matrix_inv(M);
        lib3ds_matrix_mul(X,N,M);
        rib_concat_transform(o, X);
      }
      {
        unsigned p;
	int i, j;
        Lib3dsVector *normalL=malloc(3*sizeof(Lib3dsVector)*mesh->faces);
        lib3ds_mesh_calculate_normals(mesh, normalL);
	Lib3dsMaterial *lastmat = NULL;

	int nalloc = 256, nfaces = 0;
	float *P = (float *)malloc(nalloc*9*sizeof(float));
	float *N = (float *)malloc(nalloc*9*sizeof(float));
	float *uv = (float *)malloc(nalloc*6*sizeof(float));

        for (p=0; p<mesh->faces; ++p) {
          Lib3dsFace *face=&mesh->faceL[p];
          Lib3dsMaterial *mat=lib3ds_file_material_by_name(f, face->material);
          if (mat && mat != lastmat) {
	      char *Kdmap = NULL, *Omap = NULL;

	      flushpp(o, P, N, uv, nfaces);
	      nfaces = 0;
	      lastmat = mat;

	      if (mat->texture1_map.name[0]) {
		  Kdmap = GetTexture(o, mat->texture1_map.name, mat->texture1_mask.name, 0);
		  char *scale = malloc(strlen(Kdmap)+20);
		  strcpy(scale, Kdmap);
		  strcat(scale, "-scale");
		  fprintf(o, "Texture \"%s\" \"color\" \"scale\" \"texture tex1\" \"%s\" "
			  "\"color tex2\" [.8 .8 .8]\n", scale, Kdmap);
		  Kdmap = scale;
	      }
	      if (mat->opacity_map.name[0])
		  Omap = GetTexture(o, mat->opacity_map.name, mat->opacity_mask.name, 0);

	      char *bumpScale = NULL;
	      if (mat->bump_map.name[0]) {
		  char *Bmap = GetTexture(o, mat->bump_map.name, mat->bump_mask.name, 1);
		  bumpScale = malloc(strlen(Bmap)+20);
		  strcpy(bumpScale, Bmap);
		  strcat(bumpScale, "-scale");
		  fprintf(o, "Texture \"%s\" \"float\" \"scale\" \"texture tex1\" \"%s\" "
			  "\"float tex2\" [.05]\n", bumpScale, Bmap);
	      }

	      fprintf(o, "Material \"uber\" ");
	      if (Kdmap) fprintf(o, "\"texture Kd\" \"%s\" ", Kdmap);
	      else       fprintf(o, "\"color Kd\" [%f %f %f] ",
				 mat->diffuse[0], mat->diffuse[1], mat->diffuse[2]);
	      if (Omap) fprintf(o, "\"texture opacity\" \"%s\" ", Omap);
	      else      fprintf(o, "\"color opacity\" [%f %f %f] ",
				1.f - mat->transparency,
				1.f - mat->transparency,
				1.f - mat->transparency);
	      fprintf(o, "\"color Ks\" [%f %f %f] "
		      "\"float roughness\" [%f] ",
		      mat->specular[0]*mat->shin_strength,
		      mat->specular[1]*mat->shin_strength,
		      mat->specular[2]*mat->shin_strength,
		      mat->shininess
		      );
//CO	      dumptex(o, "tex2", &mat->texture2_map); 
//CO	      dumptex(o, "tex2mask", &mat->texture2_mask);
//CO	      dumptex(o, "specular", &mat->specular_map); 
//CO	      dumptex(o, "specularmask", &mat->specular_mask);
//CO	      dumptex(o, "shininess", &mat->shininess_map); 
//CO	      dumptex(o, "shininessmask", &mat->shininess_mask);
//CO	      dumptex(o, "reflection", &mat->reflection_map); 
//CO	      dumptex(o, "reflectionmask", &mat->reflection_mask);
	      fprintf(o, "\n");

	      if (bumpScale)
		  fprintf(o, "\"float bumpmap\" \"%s\"\n", bumpScale);
          }
	  if (nfaces+1 == nalloc) {
	      nalloc *= 2;
	      P = (float *)realloc(P, nalloc*9*sizeof(float));
	      N = (float *)realloc(N, nalloc*9*sizeof(float));
	      uv = (float *)realloc(uv, nalloc*6*sizeof(float));
	  }

	  for (i = 0; i < 3; ++i) {
	      for (j = 0; j < 3; ++j) {
		  P[9*nfaces+3*i+j] = mesh->pointL[face->points[i]].pos[j];
		  N[9*nfaces+3*i+j] = normalL[3*p+i][j];
		  if (j != 2 && mesh->texelL) 
		      uv[6*nfaces+2*i+j] = mesh->texelL[face->points[i]][j];
	      }
	      if (!mesh->texelL) {
		  uv[6*nfaces+0] = 0;
		  uv[6*nfaces+1] = 0;
		  uv[6*nfaces+2] = 0;
		  uv[6*nfaces+3] = 1;
		  uv[6*nfaces+4] = 1;
		  uv[6*nfaces+5] = 0;
	      }
	  }
	  ++nfaces;
	}

	flushpp(o, P, N, uv, nfaces);
	free(P);
	free(N);
	free(uv);
        free(normalL);
      }
      nTextures = 0;
      fprintf(o, "AttributeEnd\n");
    }
  }
  {
    Lib3dsNode *n;
    for (n=node->childs; n; n=n->next) {
      create_node(f,n,o);
    }
  }
}
Esempio n. 3
0
static void
create_node(Lib3dsFile *f, Lib3dsNode *node, FILE *o)
{
  Lib3dsMesh *mesh;
  
  if ((node->type==LIB3DS_OBJECT_NODE) && (strcmp(node->name,"$$$DUMMY")!=0)) {
    mesh=lib3ds_file_mesh_by_name(f, node->name);
    ASSERT(mesh);
    if (mesh) {
      Lib3dsObjectData *d=&node->data.object;
      
      fprintf(o, "AttributeBegin\n");
      fprintf(o, "Surface \"matte\" \"Kd\" [0.75]\n");
      fprintf(o, "Color 1 1 1\n");

      {
        Lib3dsMatrix N,M,X;
        lib3ds_matrix_copy(N, node->matrix);
        lib3ds_matrix_translate_xyz(N, -d->pivot[0], -d->pivot[1], -d->pivot[2]);
        lib3ds_matrix_copy(M, mesh->matrix);
        lib3ds_matrix_inv(M);
        lib3ds_matrix_mul(X,N,M);
        rib_concat_transform(o, X);
      }
      {
        unsigned p;
        Lib3dsVector *normalL=malloc(3*sizeof(Lib3dsVector)*mesh->faces);
        lib3ds_mesh_calculate_normals(mesh, normalL);
        
        for (p=0; p<mesh->faces; ++p) {
          Lib3dsFace *face=&mesh->faceL[p];
          Lib3dsMaterial *mat=lib3ds_file_material_by_name(f, face->material);
          if (mat) {
            fprintf(o, "Color [%f %f %f]\n",
              mat->diffuse[0],
              mat->diffuse[1],
              mat->diffuse[2]
            );
            fprintf(o,
              "Surface "
              "\"lib3dsmaterial\" "
              "\"color specularcolor\" [%f %f %f] "
              "\"float shininess \" [%f] "
              "\"float shin_stength \" [%f] "
              "\n",
              mat->specular[0],
              mat->specular[1],
              mat->specular[2],
              mat->shininess,
              mat->shin_strength
            );
          }
          fprintf(o, "Polygon \"P\" [%f %f %f %f %f %f %f %f %f] ",
            mesh->pointL[face->points[0]].pos[0],
            mesh->pointL[face->points[0]].pos[1],
            mesh->pointL[face->points[0]].pos[2],
            mesh->pointL[face->points[1]].pos[0],
            mesh->pointL[face->points[1]].pos[1],
            mesh->pointL[face->points[1]].pos[2],
            mesh->pointL[face->points[2]].pos[0],
            mesh->pointL[face->points[2]].pos[1],
            mesh->pointL[face->points[2]].pos[2] 
          );

          fprintf(o, "\"N\" [%f %f %f %f %f %f %f %f %f] ",
            normalL[3*p+0][0],
            normalL[3*p+0][1],
            normalL[3*p+0][2],
            normalL[3*p+1][0],
            normalL[3*p+1][1],
            normalL[3*p+1][2],
            normalL[3*p+2][0],
            normalL[3*p+2][1],
            normalL[3*p+2][2]
          );
        }

        free(normalL);
      }
      fprintf(o, "AttributeEnd\n");
    }
  }
  {
    Lib3dsNode *n;
    for (n=node->childs; n; n=n->next) {
      create_node(f,n,o);
    }
  }
}