Esempio n. 1
0
/*!
 * Write a camera definition to a file.
 *
 * This function is called by lib3ds_file_write(), and you probably
 * don't want to call it directly.
 *
 * \param camera A Lib3dsCamera to be written.
 * \param io A Lib3dsIo object previously set up by the caller.
 *
 * \see lib3ds_file_write
 */
void
lib3ds_camera_write(Lib3dsCamera *camera, Lib3dsIo *io) {
    Lib3dsChunk c;

    c.chunk = CHK_N_CAMERA;
    lib3ds_chunk_write_start(&c, io);

    lib3ds_io_write_vector(io, camera->position);
    lib3ds_io_write_vector(io, camera->target);
    lib3ds_io_write_float(io, camera->roll);
    if (fabs(camera->fov) < LIB3DS_EPSILON) {
        lib3ds_io_write_float(io, 2400.0f / 45.0f);
    } else {
        lib3ds_io_write_float(io, 2400.0f / camera->fov);
    }

    if (camera->see_cone) {
        Lib3dsChunk c;
        c.chunk = CHK_CAM_SEE_CONE;
        c.size = 6;
        lib3ds_chunk_write(&c, io);
    }
    {
        Lib3dsChunk c;
        c.chunk = CHK_CAM_RANGES;
        c.size = 14;
        lib3ds_chunk_write(&c, io);
        lib3ds_io_write_float(io, camera->near_range);
        lib3ds_io_write_float(io, camera->far_range);
    }

    lib3ds_chunk_write_end(&c, io);
}
Esempio n. 2
0
static void
point_array_write(Lib3dsMesh *mesh, Lib3dsIo *io) {
    Lib3dsChunk c;
    int i;

    c.chunk = CHK_POINT_ARRAY;
    c.size = 8 + 12 * mesh->nvertices;
    lib3ds_chunk_write(&c, io);

    lib3ds_io_write_word(io, (uint16_t) mesh->nvertices);

    if (lib3ds_matrix_det(mesh->matrix) >= 0.0f) {
        for (i = 0; i < mesh->nvertices; ++i) {
            lib3ds_io_write_vector(io, mesh->vertices[i]);
        }
    } else {
        /* Flip X coordinate of vertices if mesh matrix
           has negative determinant */
        float inv_matrix[4][4], M[4][4];
        float tmp[3];

        lib3ds_matrix_copy(inv_matrix, mesh->matrix);
        lib3ds_matrix_inv(inv_matrix);
        lib3ds_matrix_copy(M, mesh->matrix);
        lib3ds_matrix_scale(M, -1.0f, 1.0f, 1.0f);
        lib3ds_matrix_mult(M, M, inv_matrix);

        for (i = 0; i < mesh->nvertices; ++i) {
            lib3ds_vector_transform(tmp, M, mesh->vertices[i]);
            lib3ds_io_write_vector(io, tmp);
        }
    }
}
void
lib3ds_track_write(Lib3dsTrack *track, Lib3dsIo *io) {
    int i;

    lib3ds_io_write_word(io, (uint16_t)track->flags);
    lib3ds_io_write_dword(io, 0);
    lib3ds_io_write_dword(io, 0);
    lib3ds_io_write_dword(io, track->nkeys);

    switch (track->type) {
        case LIB3DS_TRACK_BOOL:
            for (i = 0; i < track->nkeys; ++i) {
                lib3ds_io_write_intd(io, track->keys[i].frame);
                tcb_write(&track->keys[i], io);
            }
            break;

        case LIB3DS_TRACK_FLOAT:
            for (i = 0; i < track->nkeys; ++i) {
                lib3ds_io_write_intd(io, track->keys[i].frame);
                tcb_write(&track->keys[i], io);
                lib3ds_io_write_float(io, track->keys[i].value[0]);
            }
            break;

        case LIB3DS_TRACK_VECTOR:
            for (i = 0; i < track->nkeys; ++i) {
                lib3ds_io_write_intd(io, track->keys[i].frame);
                tcb_write(&track->keys[i], io);
                lib3ds_io_write_vector(io, track->keys[i].value);
            }
            break;

        case LIB3DS_TRACK_QUAT:
            for (i = 0; i < track->nkeys; ++i) {
                lib3ds_io_write_intd(io, track->keys[i].frame);
                tcb_write(&track->keys[i], io);
                lib3ds_io_write_float(io, track->keys[i].value[3]);
                lib3ds_io_write_vector(io, track->keys[i].value);
            }
            break;

        /*case LIB3DS_TRACK_MORPH:
            for (i = 0; i < track->nkeys; ++i) {
                lib3ds_io_write_intd(io, track->keys[i].frame);
                tcb_write(&track->keys[i].tcb, io);
                lib3ds_io_write_string(io, track->keys[i].data.m.name);
            }
            break;*/
    }
}
Esempio n. 4
0
static Lib3dsBool
point_array_write(Lib3dsMesh *mesh, Lib3dsIo *io)
{
  Lib3dsChunk c;
  unsigned i;

  if (!mesh->points || !mesh->pointL) {
    return(LIB3DS_TRUE);
  }
  ASSERT(mesh->points<0x10000);
  c.chunk=LIB3DS_POINT_ARRAY;
  c.size=8+12*mesh->points;
  lib3ds_chunk_write(&c, io);
  
  lib3ds_io_write_word(io, (Lib3dsWord)mesh->points);

  if (lib3ds_matrix_det(mesh->matrix) >= 0.0f) {
    for (i=0; i<mesh->points; ++i) {
      lib3ds_io_write_vector(io, mesh->pointL[i].pos);
    }
  }
  else {
    /* Flip X coordinate of vertices if mesh matrix 
       has negative determinant */
    Lib3dsMatrix inv_matrix, M;
    Lib3dsVector tmp;

    lib3ds_matrix_copy(inv_matrix, mesh->matrix);
    lib3ds_matrix_inv(inv_matrix);
    lib3ds_matrix_copy(M, mesh->matrix);
    lib3ds_matrix_scale_xyz(M, -1.0f, 1.0f, 1.0f);
    lib3ds_matrix_mult(M, inv_matrix);

    for (i=0; i<mesh->points; ++i) {
      lib3ds_vector_transform(tmp, M, mesh->pointL[i].pos);
      lib3ds_io_write_vector(io, tmp);
    }
  }

  return(LIB3DS_TRUE);
}
Esempio n. 5
0
static Lib3dsBool
point_array_write(Lib3dsMesh *mesh, Lib3dsIo *io)
{
  Lib3dsChunk c;
  unsigned i;

  if (!mesh->points || !mesh->pointL) {
    return(LIB3DS_TRUE);
  }
  ASSERT(mesh->points<0x10000);
  c.chunk=LIB3DS_POINT_ARRAY;
  c.size=8+12*mesh->points;
  lib3ds_chunk_write(&c, io);
  
  lib3ds_io_write_word(io, (Lib3dsWord)mesh->points);
  for (i=0; i<mesh->points; ++i) {
    lib3ds_io_write_vector(io, mesh->pointL[i].pos);
  }
  return(LIB3DS_TRUE);
}
Esempio n. 6
0
/*!
 * \ingroup tracks 
 */
Lib3dsBool
lib3ds_lin3_track_write(Lib3dsLin3Track *track, Lib3dsIo *io)
{
  Lib3dsLin3Key *k;
  Lib3dsDword num=0;
  for (k=track->keyL; k; k=k->next) {
    ++num;
  }
  lib3ds_io_write_word(io, (Lib3dsWord)track->flags);
  lib3ds_io_write_dword(io, 0);
  lib3ds_io_write_dword(io, 0);
  lib3ds_io_write_dword(io, num);

  for (k=track->keyL; k; k=k->next) {
    if (!lib3ds_tcb_write(&k->tcb,io)) {
      return(LIB3DS_FALSE);
    }
    lib3ds_io_write_vector(io, k->value);
  }
  return(LIB3DS_TRUE);
}
Esempio n. 7
0
/*!
 * \ingroup tracks 
 */
Lib3dsBool
lib3ds_quat_track_write(Lib3dsQuatTrack *track, Lib3dsIo *io)
{
  Lib3dsQuatKey *k;
  Lib3dsDword num=0;
  for (k=track->keyL; k; k=k->next) {
    ++num;
  }
  lib3ds_io_write_word(io, (Lib3dsWord)track->flags);
  lib3ds_io_write_dword(io, 0);
  lib3ds_io_write_dword(io, 0);
  lib3ds_io_write_dword(io, num);

  for (k=track->keyL; k; k=k->next) {
    if (!lib3ds_tcb_write(&k->tcb,io)) {
      return(LIB3DS_FALSE);
    }
    lib3ds_io_write_float(io, k->angle);
    lib3ds_io_write_vector(io, k->axis);
  }
  return(LIB3DS_TRUE);
}
Esempio n. 8
0
void
lib3ds_mesh_write(Lib3dsFile *file, Lib3dsMesh *mesh, Lib3dsIo *io) {
    Lib3dsChunk c;

    c.chunk = CHK_N_TRI_OBJECT;
    lib3ds_chunk_write_start(&c, io);

    point_array_write(mesh, io);
    texco_array_write(mesh, io);

    if (mesh->map_type != LIB3DS_MAP_NONE) {   /*---- LIB3DS_MESH_TEXTURE_INFO ----*/
        Lib3dsChunk c;
        int i, j;

        c.chunk = CHK_MESH_TEXTURE_INFO;
        c.size = 92;
        lib3ds_chunk_write(&c, io);

        lib3ds_io_write_word(io, (uint16_t)mesh->map_type);

        for (i = 0; i < 2; ++i) {
            lib3ds_io_write_float(io, mesh->map_tile[i]);
        }
        lib3ds_io_write_vector(io, mesh->map_pos);
        lib3ds_io_write_float(io, mesh->map_scale);

        for (i = 0; i < 4; i++) {
            for (j = 0; j < 3; j++) {
                lib3ds_io_write_float(io, mesh->map_matrix[i][j]);
            }
        }
        for (i = 0; i < 2; ++i) {
            lib3ds_io_write_float(io, mesh->map_planar_size[i]);
        }
        lib3ds_io_write_float(io, mesh->map_cylinder_height);
    }

    flag_array_write(mesh, io);

    {
        /*---- LIB3DS_MESH_MATRIX ----*/
        Lib3dsChunk c;
        int i, j;

        c.chunk = CHK_MESH_MATRIX;
        c.size = 54;
        lib3ds_chunk_write(&c, io);
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 3; j++) {
                lib3ds_io_write_float(io, mesh->matrix[i][j]);
            }
        }
    }

    if (mesh->color) {   /*---- LIB3DS_MESH_COLOR ----*/
        Lib3dsChunk c;

        c.chunk = CHK_MESH_COLOR;
        c.size = 7;
        lib3ds_chunk_write(&c, io);
        lib3ds_io_write_byte(io, (uint8_t)mesh->color);
    }

    face_array_write(file, mesh, io);

    lib3ds_chunk_write_end(&c, io);
}
Esempio n. 9
0
File: node.c Progetto: GYengera/mrpt
/*!
 * \ingroup node
 */
Lib3dsBool
lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, Lib3dsIo *io)
{
  Lib3dsChunk c;

  switch (node->type) {
    case LIB3DS_AMBIENT_NODE:
      c.chunk=LIB3DS_AMBIENT_NODE_TAG;
      break;
    case LIB3DS_OBJECT_NODE:
      c.chunk=LIB3DS_OBJECT_NODE_TAG;
      break;
    case LIB3DS_CAMERA_NODE:
      c.chunk=LIB3DS_CAMERA_NODE_TAG;
      break;
    case LIB3DS_TARGET_NODE:
      c.chunk=LIB3DS_TARGET_NODE_TAG;
      break;
    case LIB3DS_LIGHT_NODE:
      if (lib3ds_file_node_by_name(file, node->name, LIB3DS_SPOT_NODE)) {
        c.chunk=LIB3DS_SPOTLIGHT_NODE_TAG;
      }
      else {
        c.chunk=LIB3DS_LIGHT_NODE_TAG;
      }
      break;
    case LIB3DS_SPOT_NODE:
      c.chunk=LIB3DS_L_TARGET_NODE_TAG;
      break;
    default:
      return(LIB3DS_FALSE);
  }
  if (!lib3ds_chunk_write_start(&c,io)) {
    return(LIB3DS_FALSE);
  }

  { /*---- LIB3DS_NODE_ID ----*/
    Lib3dsChunk c;
    c.chunk=LIB3DS_NODE_ID;
    c.size=8;
    lib3ds_chunk_write(&c,io);
    lib3ds_io_write_intw(io, node->node_id);
  }

  { /*---- LIB3DS_NODE_HDR ----*/
    Lib3dsChunk c;
    c.chunk=LIB3DS_NODE_HDR;
    c.size=6+ 1+(Lib3dsDword)strlen(node->name) +2+2+2;
    lib3ds_chunk_write(&c,io);
    lib3ds_io_write_string(io, node->name);
    lib3ds_io_write_word(io, node->flags1);
    lib3ds_io_write_word(io, node->flags2);
    lib3ds_io_write_word(io, node->parent_id);
  }

  switch (c.chunk) {
    case LIB3DS_AMBIENT_NODE_TAG:
      { /*---- LIB3DS_COL_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_COL_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin3_track_write(&node->data.ambient.col_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      break;
    case LIB3DS_OBJECT_NODE_TAG:
      { /*---- LIB3DS_PIVOT ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_PIVOT;
        c.size=18;
        lib3ds_chunk_write(&c,io);
        lib3ds_io_write_vector(io, node->data.object.pivot);
      }
      { /*---- LIB3DS_INSTANCE_NAME ----*/
        Lib3dsChunk c;
        const char *name;
        if (strlen(node->data.object.instance)) {
          name=node->data.object.instance;

          c.chunk=LIB3DS_INSTANCE_NAME;
          c.size=6+1+(Lib3dsDword)strlen(name);
          lib3ds_chunk_write(&c,io);
          lib3ds_io_write_string(io, name);
        }
      }
      {
        int i;
        for (i=0; i<3; ++i) {
          if ((fabs(node->data.object.bbox_min[i])>LIB3DS_EPSILON) ||
            (fabs(node->data.object.bbox_max[i])>LIB3DS_EPSILON)) {
            break;
          }
        }
        
        if (i<3) { /*---- LIB3DS_BOUNDBOX ----*/
          Lib3dsChunk c;
          c.chunk=LIB3DS_BOUNDBOX;
          c.size=30;
          lib3ds_chunk_write(&c,io);
          lib3ds_io_write_vector(io, node->data.object.bbox_min);
          lib3ds_io_write_vector(io, node->data.object.bbox_max);
        }
      }
      { /*---- LIB3DS_POS_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_POS_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin3_track_write(&node->data.object.pos_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      { /*---- LIB3DS_ROT_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_ROT_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_quat_track_write(&node->data.object.rot_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      { /*---- LIB3DS_SCL_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_SCL_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin3_track_write(&node->data.object.scl_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      if (node->data.object.hide_track.keyL) { /*---- LIB3DS_HIDE_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_HIDE_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_bool_track_write(&node->data.object.hide_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      if (fabs(node->data.object.morph_smooth)>LIB3DS_EPSILON){ /*---- LIB3DS_MORPH_SMOOTH ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_MORPH_SMOOTH;
        c.size=10;
        lib3ds_chunk_write(&c,io);
        lib3ds_io_write_float(io, node->data.object.morph_smooth);
      }
      break;
    case LIB3DS_CAMERA_NODE_TAG:
      { /*---- LIB3DS_POS_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_POS_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin3_track_write(&node->data.camera.pos_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      { /*---- LIB3DS_FOV_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_FOV_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin1_track_write(&node->data.camera.fov_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      { /*---- LIB3DS_ROLL_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_ROLL_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin1_track_write(&node->data.camera.roll_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      break;
    case LIB3DS_TARGET_NODE_TAG:
      { /*---- LIB3DS_POS_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_POS_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin3_track_write(&node->data.target.pos_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      break;
    case LIB3DS_LIGHT_NODE_TAG:
      { /*---- LIB3DS_POS_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_POS_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin3_track_write(&node->data.light.pos_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      { /*---- LIB3DS_COL_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_COL_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin3_track_write(&node->data.light.col_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      break;
    case LIB3DS_SPOTLIGHT_NODE_TAG:
      { /*---- LIB3DS_POS_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_POS_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin3_track_write(&node->data.light.pos_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      { /*---- LIB3DS_COL_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_COL_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin3_track_write(&node->data.light.col_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      { /*---- LIB3DS_HOT_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_HOT_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin1_track_write(&node->data.light.hotspot_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      { /*---- LIB3DS_FALL_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_FALL_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin1_track_write(&node->data.light.falloff_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      { /*---- LIB3DS_ROLL_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_ROLL_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin1_track_write(&node->data.light.roll_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      break;
    case LIB3DS_L_TARGET_NODE_TAG:
      { /*---- LIB3DS_POS_TRACK_TAG ----*/
        Lib3dsChunk c;
        c.chunk=LIB3DS_POS_TRACK_TAG;
        if (!lib3ds_chunk_write_start(&c,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_lin3_track_write(&node->data.spot.pos_track,io)) {
          return(LIB3DS_FALSE);
        }
        if (!lib3ds_chunk_write_end(&c,io)) {
          return(LIB3DS_FALSE);
        }
      }
      break;
    default:
      return(LIB3DS_FALSE);
  }

  if (!lib3ds_chunk_write_end(&c,io)) {
    return(LIB3DS_FALSE);
  }
  return(LIB3DS_TRUE);
}
Esempio n. 10
0
void
lib3ds_node_write(Lib3dsNode *node, uint16_t node_id, uint16_t parent_id, Lib3dsIo *io) {
    Lib3dsChunk c;

    switch (node->type) {
        case LIB3DS_NODE_AMBIENT_COLOR:
            c.chunk = CHK_AMBIENT_NODE_TAG;
            break;

        case LIB3DS_NODE_MESH_INSTANCE:
            c.chunk = CHK_OBJECT_NODE_TAG;
            break;

        case LIB3DS_NODE_CAMERA:
            c.chunk = CHK_CAMERA_NODE_TAG;
            break;

        case LIB3DS_NODE_CAMERA_TARGET:
            c.chunk = CHK_TARGET_NODE_TAG;
            break;

        case LIB3DS_NODE_OMNILIGHT:
            c.chunk = CHK_LIGHT_NODE_TAG;
            break;

        case LIB3DS_NODE_SPOTLIGHT:
            c.chunk = CHK_SPOTLIGHT_NODE_TAG;
            break;

        case LIB3DS_NODE_SPOTLIGHT_TARGET:
            c.chunk = CHK_L_TARGET_NODE_TAG;
            break;

        default:
            assert(0);
            return;
    }

    lib3ds_chunk_write_start(&c, io);

    { /*---- CHK_NODE_ID ----*/
        Lib3dsChunk c;
        c.chunk = CHK_NODE_ID;
        c.size = 8;
        lib3ds_chunk_write(&c, io);
        lib3ds_io_write_intw(io, node_id);
    }

    { /*---- CHK_NODE_HDR ----*/
        Lib3dsChunk c;
        c.chunk = CHK_NODE_HDR;
        c.size = 6 + 1 + (uint32_t)strlen(node->name) + 2 + 2 + 2;
        lib3ds_chunk_write(&c, io);
        lib3ds_io_write_string(io, node->name);
        lib3ds_io_write_word(io, node->flags & 0xffff);
        lib3ds_io_write_word(io, (node->flags >> 16) & 0xffff);
        lib3ds_io_write_word(io, parent_id);
    }

    switch (c.chunk) {
        case CHK_AMBIENT_NODE_TAG: {
            Lib3dsAmbientColorNode *n = (Lib3dsAmbientColorNode*)node;
            if (n->color_track.nkeys) { /*---- CHK_COL_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_COL_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->color_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            break;
        }

        case CHK_OBJECT_NODE_TAG: {
            Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*)node;
            { /*---- CHK_PIVOT ----*/
                Lib3dsChunk c;
                c.chunk = CHK_PIVOT;
                c.size = 18;
                lib3ds_chunk_write(&c, io);
                lib3ds_io_write_vector(io, n->pivot);
            }

            { /*---- CHK_INSTANCE_NAME ----*/
                Lib3dsChunk c;
                const char *name;
                if (strlen(n->instance_name)) {
                    name = n->instance_name;

                    c.chunk = CHK_INSTANCE_NAME;
                    c.size = 6 + 1 + (uint32_t)strlen(name);
                    lib3ds_chunk_write(&c, io);
                    lib3ds_io_write_string(io, name);
                }
            }
            {
                int i;
                for (i = 0; i < 3; ++i) {
                    if ((fabs(n->bbox_min[i]) > LIB3DS_EPSILON) ||
                        (fabs(n->bbox_max[i]) > LIB3DS_EPSILON)) {
                        break;
                    }
                }

                if (i < 3) { /*---- CHK_BOUNDBOX ----*/
                    Lib3dsChunk c;
                    c.chunk = CHK_BOUNDBOX;
                    c.size = 30;
                    lib3ds_chunk_write(&c, io);
                    lib3ds_io_write_vector(io, n->bbox_min);
                    lib3ds_io_write_vector(io, n->bbox_max);
                }
            }

            if (n->pos_track.nkeys) { /*---- CHK_POS_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_POS_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->pos_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (n->rot_track.nkeys) { /*---- CHK_ROT_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_ROT_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->rot_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (n->scl_track.nkeys) { /*---- LIB3DS_SCL_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_SCL_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->scl_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (n->hide_track.nkeys) { /*---- CHK_HIDE_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_HIDE_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->hide_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (fabs(n->morph_smooth) > LIB3DS_EPSILON) { /*---- CHK_MORPH_SMOOTH ----*/
                Lib3dsChunk c;
                c.chunk = CHK_MORPH_SMOOTH;
                c.size = 10;
                lib3ds_chunk_write(&c, io);
                lib3ds_io_write_float(io, n->morph_smooth);
            }
            break;
        }

        case CHK_CAMERA_NODE_TAG: {
            Lib3dsCameraNode *n = (Lib3dsCameraNode*)node;
            if (n->pos_track.nkeys) { /*---- CHK_POS_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_POS_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->pos_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (n->fov_track.nkeys) { /*---- CHK_FOV_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_FOV_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->fov_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (n->roll_track.nkeys) { /*---- CHK_ROLL_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_ROLL_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->roll_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            break;
        }

        case CHK_TARGET_NODE_TAG: {
            Lib3dsTargetNode *n = (Lib3dsTargetNode*)node;
            if (n->pos_track.nkeys) { /*---- CHK_POS_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_POS_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->pos_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            break;
        }

        case CHK_LIGHT_NODE_TAG: {
            Lib3dsOmnilightNode *n = (Lib3dsOmnilightNode*)node;
            if (n->pos_track.nkeys) { /*---- CHK_POS_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_POS_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->pos_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (n->color_track.nkeys) { /*---- CHK_COL_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_COL_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->color_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            break;
        }

        case CHK_SPOTLIGHT_NODE_TAG: {
            Lib3dsSpotlightNode *n = (Lib3dsSpotlightNode*)node;
            if (n->pos_track.nkeys) { /*---- CHK_POS_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_POS_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->pos_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (n->color_track.nkeys) { /*---- CHK_COL_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_COL_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->color_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (n->hotspot_track.nkeys) { /*---- CHK_HOT_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_HOT_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->hotspot_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (n->falloff_track.nkeys) { /*---- CHK_FALL_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_FALL_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->falloff_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            if (n->roll_track.nkeys) { /*---- CHK_ROLL_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_ROLL_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->roll_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            break;
        }

        case CHK_L_TARGET_NODE_TAG: {
            Lib3dsTargetNode *n = (Lib3dsTargetNode*)node;
            if (n->pos_track.nkeys) { /*---- CHK_POS_TRACK_TAG ----*/
                Lib3dsChunk c;
                c.chunk = CHK_POS_TRACK_TAG;
                lib3ds_chunk_write_start(&c, io);
                lib3ds_track_write(&n->pos_track, io);
                lib3ds_chunk_write_end(&c, io);
            }
            break;
        }

        default:
            break;
    }

    lib3ds_chunk_write_end(&c, io);
}
Esempio n. 11
0
static Lib3dsBool
mdata_write(Lib3dsFile *file, Lib3dsIo *io)
{
  Lib3dsChunk c;

  c.chunk=LIB3DS_MDATA;
  if (!lib3ds_chunk_write_start(&c,io)) {
    return(LIB3DS_FALSE);
  }

  { /*---- LIB3DS_MESH_VERSION ----*/
    Lib3dsChunk c;
    c.chunk=LIB3DS_MESH_VERSION;
    c.size=10;
    lib3ds_chunk_write(&c,io);
    lib3ds_io_write_intd(io, file->mesh_version);
  }
  { /*---- LIB3DS_MASTER_SCALE ----*/
    Lib3dsChunk c;
    c.chunk=LIB3DS_MASTER_SCALE;
    c.size=10;
    lib3ds_chunk_write(&c,io);
    lib3ds_io_write_float(io, file->master_scale);
  }
  { /*---- LIB3DS_O_CONSTS ----*/
    int i;
    for (i=0; i<3; ++i) {
      if (fabs(file->construction_plane[i])>LIB3DS_EPSILON) {
        break;
      }
    }
    if (i<3) {
      Lib3dsChunk c;
      c.chunk=LIB3DS_O_CONSTS;
      c.size=18;
      lib3ds_chunk_write(&c,io);
      lib3ds_io_write_vector(io, file->construction_plane);
    }
  }
  
  { /*---- LIB3DS_AMBIENT_LIGHT ----*/
    int i;
    for (i=0; i<3; ++i) {
      if (fabs(file->ambient[i])>LIB3DS_EPSILON) {
        break;
      }
    }
    if (i<3) {
      Lib3dsChunk c;
      c.chunk=LIB3DS_AMBIENT_LIGHT;
      c.size=42;
      lib3ds_chunk_write(&c,io);
      colorf_write(file->ambient,io);
    }
  }
  lib3ds_background_write(&file->background, io);
  lib3ds_atmosphere_write(&file->atmosphere, io);
  lib3ds_shadow_write(&file->shadow, io);
  lib3ds_viewport_write(&file->viewport, io);
  {
    Lib3dsMaterial *p;
    for (p=file->materials; p!=0; p=p->next) {
      if (!lib3ds_material_write(p,io)) {
        return(LIB3DS_FALSE);
      }
    }
  }
  {
    Lib3dsCamera *p;
    Lib3dsChunk c;
    
    for (p=file->cameras; p!=0; p=p->next) {
      c.chunk=LIB3DS_NAMED_OBJECT;
      if (!lib3ds_chunk_write_start(&c,io)) {
        return(LIB3DS_FALSE);
      }
      lib3ds_io_write_string(io, p->name);
      lib3ds_camera_write(p,io);
      object_flags_write(p->object_flags,io);
      if (!lib3ds_chunk_write_end(&c,io)) {
        return(LIB3DS_FALSE);
      }
    }
  }
  {
    Lib3dsLight *p;
    Lib3dsChunk c;
    
    for (p=file->lights; p!=0; p=p->next) {
      c.chunk=LIB3DS_NAMED_OBJECT;
      if (!lib3ds_chunk_write_start(&c,io)) {
        return(LIB3DS_FALSE);
      }
      lib3ds_io_write_string(io,p->name);
      lib3ds_light_write(p,io);
      object_flags_write(p->object_flags,io);
      if (!lib3ds_chunk_write_end(&c,io)) {
        return(LIB3DS_FALSE);
      }
    }
  }
  {
    Lib3dsMesh *p;
    Lib3dsChunk c;
    
    for (p=file->meshes; p!=0; p=p->next) {
      c.chunk=LIB3DS_NAMED_OBJECT;
      if (!lib3ds_chunk_write_start(&c,io)) {
        return(LIB3DS_FALSE);
      }
      lib3ds_io_write_string(io, p->name);
      lib3ds_mesh_write(p,io);
      object_flags_write(p->object_flags,io);
      if (!lib3ds_chunk_write_end(&c,io)) {
        return(LIB3DS_FALSE);
      }
    }
  }

  if (!lib3ds_chunk_write_end(&c,io)) {
    return(LIB3DS_FALSE);
  }
  return(LIB3DS_TRUE);
}
Esempio n. 12
0
static void
mdata_write(Lib3dsFile *file, Lib3dsIo *io) {
    Lib3dsChunk c;

    c.chunk = CHK_MDATA;
    lib3ds_chunk_write_start(&c, io);

    { /*---- LIB3DS_MESH_VERSION ----*/
        Lib3dsChunk c;
        c.chunk = CHK_MESH_VERSION;
        c.size = 10;
        lib3ds_chunk_write(&c, io);
        lib3ds_io_write_intd(io, file->mesh_version);
    }
    { /*---- LIB3DS_MASTER_SCALE ----*/
        Lib3dsChunk c;
        c.chunk = CHK_MASTER_SCALE;
        c.size = 10;
        lib3ds_chunk_write(&c, io);
        lib3ds_io_write_float(io, file->master_scale);
    }
    { /*---- LIB3DS_O_CONSTS ----*/
        int i;
        for (i = 0; i < 3; ++i) {
            if (fabs(file->construction_plane[i]) > LIB3DS_EPSILON) {
                break;
            }
        }
        if (i < 3) {
            Lib3dsChunk c;
            c.chunk = CHK_O_CONSTS;
            c.size = 18;
            lib3ds_chunk_write(&c, io);
            lib3ds_io_write_vector(io, file->construction_plane);
        }
    }

    { /*---- LIB3DS_AMBIENT_LIGHT ----*/
        int i;
        for (i = 0; i < 3; ++i) {
            if (fabs(file->ambient[i]) > LIB3DS_EPSILON) {
                break;
            }
        }
        if (i < 3) {
            Lib3dsChunk c;
            c.chunk = CHK_AMBIENT_LIGHT;
            c.size = 42;
            lib3ds_chunk_write(&c, io);
            colorf_write(file->ambient, io);
        }
    }
    lib3ds_background_write(&file->background, io);
    lib3ds_atmosphere_write(&file->atmosphere, io);
    lib3ds_shadow_write(&file->shadow, io);
    lib3ds_viewport_write(&file->viewport, io);
    {
        int i;
        for (i = 0; i < file->nmaterials; ++i) {
            lib3ds_material_write(file->materials[i], io);
        }
    }
    {
        Lib3dsChunk c;
        int i;

        for (i = 0; i < file->ncameras; ++i) {
            c.chunk = CHK_NAMED_OBJECT;
            lib3ds_chunk_write_start(&c, io);
            lib3ds_io_write_string(io, file->cameras[i]->name);
            lib3ds_camera_write(file->cameras[i], io);
            object_flags_write(file->cameras[i]->object_flags, io);
            lib3ds_chunk_write_end(&c, io);
        }
    }
    {
        Lib3dsChunk c;
        int i;

        for (i = 0; i < file->nlights; ++i) {
            c.chunk = CHK_NAMED_OBJECT;
            lib3ds_chunk_write_start(&c, io);
            lib3ds_io_write_string(io, file->lights[i]->name);
            lib3ds_light_write(file->lights[i], io);
            object_flags_write(file->lights[i]->object_flags, io);
            lib3ds_chunk_write_end(&c, io);
        }
    }
    {
        Lib3dsChunk c;
        int i;

        for (i = 0; i < file->nmeshes; ++i) {
            c.chunk = CHK_NAMED_OBJECT;
            lib3ds_chunk_write_start(&c, io);
            lib3ds_io_write_string(io, file->meshes[i]->name);
            lib3ds_mesh_write(file, file->meshes[i], io);
            object_flags_write(file->meshes[i]->object_flags, io);
            lib3ds_chunk_write_end(&c, io);
        }
    }

    lib3ds_chunk_write_end(&c, io);
}
Esempio n. 13
0
File: light.c Progetto: DYFeng/mrpt
/*!
 * \ingroup light
 */
Lib3dsBool
lib3ds_light_write(Lib3dsLight *light, Lib3dsIo *io)
{
  Lib3dsChunk c;

  c.chunk=LIB3DS_N_DIRECT_LIGHT;
  if (!lib3ds_chunk_write_start(&c,io)) {
    return(LIB3DS_FALSE);
  }
  lib3ds_io_write_vector(io, light->position);
  { /*---- LIB3DS_COLOR_F ----*/
    Lib3dsChunk c;
    c.chunk=LIB3DS_COLOR_F;
    c.size=18;
    lib3ds_chunk_write(&c, io);
    lib3ds_io_write_rgb(io, light->color);
  }
  if (light->off) { /*---- LIB3DS_DL_OFF ----*/
    Lib3dsChunk c;
    c.chunk=LIB3DS_DL_OFF;
    c.size=6;
    lib3ds_chunk_write(&c, io);
  }
  { /*---- LIB3DS_DL_OUTER_RANGE ----*/
    Lib3dsChunk c;
    c.chunk=LIB3DS_DL_OUTER_RANGE;
    c.size=10;
    lib3ds_chunk_write(&c, io);
    lib3ds_io_write_float(io, light->outer_range);
  }
  { /*---- LIB3DS_DL_INNER_RANGE ----*/
    Lib3dsChunk c;
    c.chunk=LIB3DS_DL_INNER_RANGE;
    c.size=10;
    lib3ds_chunk_write(&c, io);
    lib3ds_io_write_float(io, light->inner_range);
  }
  { /*---- LIB3DS_DL_MULTIPLIER ----*/
    Lib3dsChunk c;
    c.chunk=LIB3DS_DL_MULTIPLIER;
    c.size=10;
    lib3ds_chunk_write(&c, io);
    lib3ds_io_write_float(io, light->multiplier);
  }
  if (light->attenuation) { /*---- LIB3DS_DL_ATTENUATE ----*/
    Lib3dsChunk c;
    c.chunk=LIB3DS_DL_ATTENUATE;
    c.size=6;
    lib3ds_chunk_write(&c, io);
  }

  if (light->spot_light) {
    Lib3dsChunk c;

    c.chunk=LIB3DS_DL_SPOTLIGHT;
    if (!lib3ds_chunk_write_start(&c,io)) {
      return(LIB3DS_FALSE);
    }
    lib3ds_io_write_vector(io, light->spot);
    lib3ds_io_write_float(io, light->hot_spot);
    lib3ds_io_write_float(io, light->fall_off);
    
    { /*---- LIB3DS_DL_SPOT_ROLL ----*/
      Lib3dsChunk c;
      c.chunk=LIB3DS_DL_SPOT_ROLL;
      c.size=10;
      lib3ds_chunk_write(&c, io);
      lib3ds_io_write_float(io, light->roll);
    }
    if (light->shadowed) { /*---- LIB3DS_DL_SHADOWED ----*/
      Lib3dsChunk c;
      c.chunk=LIB3DS_DL_SHADOWED;
      c.size=6;
      lib3ds_chunk_write(&c, io);
    }
    if ((fabs(light->shadow_bias)>LIB3DS_EPSILON) ||
      (fabs(light->shadow_filter)>LIB3DS_EPSILON) ||
      (light->shadow_size!=0)) { /*---- LIB3DS_DL_LOCAL_SHADOW2 ----*/
      Lib3dsChunk c;
      c.chunk=LIB3DS_DL_LOCAL_SHADOW2;
      c.size=16;
      lib3ds_chunk_write(&c, io);
      lib3ds_io_write_float(io, light->shadow_bias);
      lib3ds_io_write_float(io, light->shadow_filter);
      lib3ds_io_write_intw(io, light->shadow_size);
    }
    if (light->see_cone) { /*---- LIB3DS_DL_SEE_CONE ----*/
      Lib3dsChunk c;
      c.chunk=LIB3DS_DL_SEE_CONE;
      c.size=6;
      lib3ds_chunk_write(&c, io);
    }
    if (light->rectangular_spot) { /*---- LIB3DS_DL_SPOT_RECTANGULAR ----*/
      Lib3dsChunk c;
      c.chunk=LIB3DS_DL_SPOT_RECTANGULAR;
      c.size=6;
      lib3ds_chunk_write(&c, io);
    }
    if (fabs(light->spot_aspect)>LIB3DS_EPSILON) { /*---- LIB3DS_DL_SPOT_ASPECT ----*/
      Lib3dsChunk c;
      c.chunk=LIB3DS_DL_SPOT_ASPECT;
      c.size=10;
      lib3ds_chunk_write(&c, io);
      lib3ds_io_write_float(io, light->spot_aspect);
    }
    if (light->use_projector) { /*---- LIB3DS_DL_SPOT_PROJECTOR ----*/
      Lib3dsChunk c;
      c.chunk=LIB3DS_DL_SPOT_PROJECTOR;
      c.size=10;
      lib3ds_chunk_write(&c, io);
      lib3ds_io_write_string(io, light->projector);
    }
    if (light->spot_overshoot) { /*---- LIB3DS_DL_SPOT_OVERSHOOT ----*/
      Lib3dsChunk c;
      c.chunk=LIB3DS_DL_SPOT_OVERSHOOT;
      c.size=6;
      lib3ds_chunk_write(&c, io);
    }
    if (fabs(light->ray_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_DL_RAY_BIAS ----*/
      Lib3dsChunk c;
      c.chunk=LIB3DS_DL_RAY_BIAS;
      c.size=10;
      lib3ds_chunk_write(&c, io);
      lib3ds_io_write_float(io, light->ray_bias);
    }
    if (light->ray_shadows) { /*---- LIB3DS_DL_RAYSHAD ----*/
      Lib3dsChunk c;
      c.chunk=LIB3DS_DL_RAYSHAD;
      c.size=6;
      lib3ds_chunk_write(&c, io);
    }
    if (!lib3ds_chunk_write_end(&c,io)) {
      return(LIB3DS_FALSE);
    }
  }
  if (!lib3ds_chunk_write_end(&c,io)) {
    return(LIB3DS_FALSE);
  }
  return(LIB3DS_TRUE);
}
Esempio n. 14
0
/*!
 * \ingroup viewport 
 */
Lib3dsBool
lib3ds_viewport_write(Lib3dsViewport *viewport, Lib3dsIo *io)
{
  if (viewport->layout.views) {
    Lib3dsChunk c;
    unsigned i;

    c.chunk=LIB3DS_VIEWPORT_LAYOUT;
    if (!lib3ds_chunk_write_start(&c,io)) {
      return(LIB3DS_FALSE);
    }

    lib3ds_io_write_word(io, viewport->layout.style);
    lib3ds_io_write_intw(io, viewport->layout.active);
    lib3ds_io_write_intw(io, 0);
    lib3ds_io_write_intw(io, viewport->layout.swap);
    lib3ds_io_write_intw(io, 0);
    lib3ds_io_write_intw(io, viewport->layout.swap_prior);
    lib3ds_io_write_intw(io, viewport->layout.swap_view);
    
    {
      Lib3dsChunk c;
      c.chunk=LIB3DS_VIEWPORT_SIZE;
      c.size=14;
      lib3ds_chunk_write(&c,io);
      lib3ds_io_write_intw(io, viewport->layout.position[0]);
      lib3ds_io_write_intw(io, viewport->layout.position[1]);
      lib3ds_io_write_intw(io, viewport->layout.size[0]);
      lib3ds_io_write_intw(io, viewport->layout.size[1]);
    }

    for (i=0; i<viewport->layout.views; ++i) {
      Lib3dsChunk c;
      c.chunk=LIB3DS_VIEWPORT_DATA_3;
      c.size=55;
      lib3ds_chunk_write(&c,io);

      lib3ds_io_write_intw(io, 0);
      lib3ds_io_write_word(io, viewport->layout.viewL[i].axis_lock);
      lib3ds_io_write_intw(io, viewport->layout.viewL[i].position[0]);
      lib3ds_io_write_intw(io, viewport->layout.viewL[i].position[1]);
      lib3ds_io_write_intw(io, viewport->layout.viewL[i].size[0]);
      lib3ds_io_write_intw(io, viewport->layout.viewL[i].size[1]);
      lib3ds_io_write_word(io, viewport->layout.viewL[i].type);
      lib3ds_io_write_float(io, viewport->layout.viewL[i].zoom);
      lib3ds_io_write_vector(io, viewport->layout.viewL[i].center);
      lib3ds_io_write_float(io, viewport->layout.viewL[i].horiz_angle);
      lib3ds_io_write_float(io, viewport->layout.viewL[i].vert_angle);
      lib3ds_io_write(io, viewport->layout.viewL[i].camera,11);
    }

    if (!lib3ds_chunk_write_end(&c,io)) {
      return(LIB3DS_FALSE);
    }
  }

  if (viewport->default_view.type) {
    Lib3dsChunk c;

    c.chunk=LIB3DS_DEFAULT_VIEW;
    if (!lib3ds_chunk_write_start(&c,io)) {
      return(LIB3DS_FALSE);
    }

    switch (viewport->default_view.type) {
      case LIB3DS_VIEW_TYPE_TOP:
        {
          Lib3dsChunk c;
          c.chunk=LIB3DS_VIEW_TOP;
          c.size=22;
          lib3ds_chunk_write(&c,io);
          lib3ds_io_write_vector(io, viewport->default_view.position);
          lib3ds_io_write_float(io, viewport->default_view.width);
        }
        break;
      case LIB3DS_VIEW_TYPE_BOTTOM:
        {
          Lib3dsChunk c;
          c.chunk=LIB3DS_VIEW_BOTTOM;
          c.size=22;
          lib3ds_chunk_write(&c,io);
          lib3ds_io_write_vector(io, viewport->default_view.position);
          lib3ds_io_write_float(io, viewport->default_view.width);
        }
        break;
      case LIB3DS_VIEW_TYPE_LEFT:
        {
          Lib3dsChunk c;
          c.chunk=LIB3DS_VIEW_LEFT;
          c.size=22;
          lib3ds_chunk_write(&c,io);
          lib3ds_io_write_vector(io, viewport->default_view.position);
          lib3ds_io_write_float(io, viewport->default_view.width);
        }
        break;
      case LIB3DS_VIEW_TYPE_RIGHT:
        {
          Lib3dsChunk c;
          c.chunk=LIB3DS_VIEW_RIGHT;
          c.size=22;
          lib3ds_chunk_write(&c,io);
          lib3ds_io_write_vector(io, viewport->default_view.position);
          lib3ds_io_write_float(io, viewport->default_view.width);
        }
        break;
      case LIB3DS_VIEW_TYPE_FRONT:
        {
          Lib3dsChunk c;
          c.chunk=LIB3DS_VIEW_FRONT;
          c.size=22;
          lib3ds_chunk_write(&c,io);
          lib3ds_io_write_vector(io, viewport->default_view.position);
          lib3ds_io_write_float(io, viewport->default_view.width);
        }
        break;
      case LIB3DS_VIEW_TYPE_BACK:
        {
          Lib3dsChunk c;
          c.chunk=LIB3DS_VIEW_BACK;
          c.size=22;
          lib3ds_chunk_write(&c,io);
          lib3ds_io_write_vector(io, viewport->default_view.position);
          lib3ds_io_write_float(io, viewport->default_view.width);
        }
        break;
      case LIB3DS_VIEW_TYPE_USER:
        {
          Lib3dsChunk c;
          c.chunk=LIB3DS_VIEW_USER;
          c.size=34;
          lib3ds_chunk_write(&c,io);
          lib3ds_io_write_vector(io, viewport->default_view.position);
          lib3ds_io_write_float(io, viewport->default_view.width);
          lib3ds_io_write_float(io, viewport->default_view.horiz_angle);
          lib3ds_io_write_float(io, viewport->default_view.vert_angle);
          lib3ds_io_write_float(io, viewport->default_view.roll_angle);
        }
        break;
      case LIB3DS_VIEW_TYPE_CAMERA:
        {
          Lib3dsChunk c;
          c.chunk=LIB3DS_VIEW_CAMERA;
          c.size=17;
          lib3ds_chunk_write(&c, io);
          lib3ds_io_write(io, viewport->default_view.camera, 11);
        }
        break;
    }

    if (!lib3ds_chunk_write_end(&c, io)) {
      return(LIB3DS_FALSE);
    }
  }
  return(LIB3DS_TRUE);
}