Logo Search packages:      
Sourcecode: view3ds version File versions

document.cpp

/*
 * The 3D Studio File Format Library
 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
 * All rights reserved.
 *
 * This program is  free  software;  you can redistribute it and/or modify it
 * under the terms of the  GNU Lesser General Public License  as published by 
 * the  Free Software Foundation;  either version 2.1 of the License,  or (at 
 * your option) any later version.
 *
 * This  program  is  distributed in  the  hope that it will  be useful,  but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public  
 * License for more details.
 *
 * You should  have received  a copy of the GNU Lesser General Public License
 * along with  this program;  if not, write to the  Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: document.cpp,v 1.4 2001/01/15 10:56:12 jeh Exp $
 */
#include "document.h"
#include <lib3ds/file.h>
#include <lib3ds/camera.h>
#include <lib3ds/mesh.h>
#include <qtimer.h>
#include <cmath>


/*!
 *  
 */
Document::Document()
{
  d_file=0;
  d_timer=new QTimer(this);
  connect(d_timer, SIGNAL(timeout()), this, SLOT(timerSlot()));
}


/*!
 *  
 */
Document::~Document()
{
  if (d_file) {
    lib3ds_file_free(d_file);
    d_file=0;
  }
}


/*!
 *  
 */
bool 
Document::open(QString filename)
{
  if (d_file) {
    lib3ds_file_free(d_file);
    d_file=0;
  }
  d_file=lib3ds_file_load((const char*)filename);
  if (!d_file) {
    puts("***ERROR*** Loading 3DS file failed.");
    return(false);
  }
  if (!d_file->cameras) {
    if (d_file->meshes) {
      Lib3dsMesh *mesh = d_file->meshes;
      Lib3dsVector min, max, mp;
      lib3ds_mesh_bounding_box(mesh, min, max);
      while(mesh = mesh->next)
      {
        Lib3dsVector mmin, mmax;
        lib3ds_mesh_bounding_box(mesh, mmin, mmax);
        for(int i=0; i < 3; ++i)
        {
          if (mmin[i] < min[i]) min[i] = mmin[i];
          if (mmax[i] > max[i]) max[i] = mmax[i];
        }
      }
      mp[0] = (min[0] + max[0])/2.;
      mp[1] = (min[1] + max[1])/2.;
      mp[2] = (min[2] + max[2])/2.;
      d_file->cameras = lib3ds_camera_new("default");
      d_file->cameras->position[0] = mp[0];
      d_file->cameras->position[1] = mp[1];
      d_file->cameras->position[2] = min[2] - (max[1]-mp[1])/tan(15*M_PI/180.);
      d_file->cameras->target[0] = mp[0];
      d_file->cameras->target[1] = mp[1];
      d_file->cameras->target[2] = mp[2];
      d_file->cameras->near_range = min[2] - fabs(0.1*min[2]);
      d_file->cameras->far_range = max[2] + fabs(0.1*max[2]);
      d_file->cameras->fov = 30;
    }
    else
      return false;
  }
  d_camera=d_file->cameras->name;

  lib3ds_file_eval(d_file,d_file->current_frame);
  emit documentChanged();
  return(true);
}


/*!
 *  
 */
bool 
Document::save(QString filename)
{
  /* FIXME: */
  ASSERT(false);
  return(false);
}


/*!
 *  
 */
void 
Document::begin()
{
  d_file->current_frame=0;
  emit documentChanged();
}


/*!
 *  
 */
void 
Document::prev()
{
  --d_file->current_frame;
  if (d_file->current_frame<0) {
    d_file->current_frame=0;
  }
  emit documentChanged();
}


/*!
 *  
 */
void 
Document::play()
{
  d_timer->start(0,true);
}


/*!
 *  
 */
void 
Document::stop()
{
  d_timer->stop();
}


/*!
 *  
 */
void 
Document::next()
{
  ++d_file->current_frame;
  if (d_file->current_frame>d_file->frames) {
    d_file->current_frame=d_file->frames;
  }
  emit documentChanged();
}


/*!
 *  
 */
void 
Document::end()
{
  d_file->current_frame=d_file->frames;
  emit documentChanged();
}


/*!
 *  
 */
Lib3dsFile* 
Document::file()
{
  return(d_file);
}


/*!
 *  
 */
QString
Document::camera()
{
  return(d_camera);
}


/*!
 *  
 */
void 
Document::setCurrent(int current)
{
  if (!d_file) {
    return;
  }

  d_file->current_frame=current;
  if (d_file->current_frame>d_file->frames) {
    d_file->current_frame=d_file->frames;
  }
  lib3ds_file_eval(d_file, d_file->current_frame);
  emit documentChanged();
}


/*!
 *  
 */
void 
Document::setCamera(const QString& name)
{
  if (d_camera!=name) {
    d_camera=name;
    emit documentChanged();
  }
}


/*!
 *  
 */
void 
Document::timerSlot()
{
  ASSERT(d_timer);
  if (!d_file) {
    return;
  }

  ++d_file->current_frame;
  if (d_file->current_frame>d_file->frames) {
    d_file->current_frame=0;
  }
  lib3ds_file_eval(d_file, d_file->current_frame);

  emit documentChanged();
  d_timer->start(0,true);
}






Generated by  Doxygen 1.6.0   Back to index