C:/jkyprian/devel/lib3ds/lib3ds/camera.c

00001 /*
00002  * The 3D Studio File Format Library
00003  * Copyright (C) 1996-2007 by Jan Eric Kyprianidis <www.kyprianidis.com>
00004  * All rights reserved.
00005  *
00006  * This program is  free  software;  you can redistribute it and/or modify it
00007  * under the terms of the  GNU Lesser General Public License  as published by 
00008  * the  Free Software Foundation;  either version 2.1 of the License,  or (at 
00009  * your option) any later version.
00010  *
00011  * This  program  is  distributed in  the  hope that it will  be useful,  but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00013  * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public  
00014  * License for more details.
00015  *
00016  * You should  have received  a copy of the GNU Lesser General Public License
00017  * along with  this program;  if not, write to the  Free Software Foundation,
00018  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *
00020  * $Id: camera.c,v 1.17 2007/06/20 17:04:08 jeh Exp $
00021  */
00022 #include <lib3ds/camera.h>
00023 #include <lib3ds/chunk.h>
00024 #include <lib3ds/io.h>
00025 #include <stdlib.h>
00026 #include <math.h>
00027 #include <string.h>
00028 
00029 
00047 Lib3dsCamera*
00048 lib3ds_camera_new(const char *name)
00049 {
00050   Lib3dsCamera *camera;
00051 
00052   ASSERT(name);
00053   ASSERT(strlen(name)<64);
00054   
00055   camera=(Lib3dsCamera*)calloc(sizeof(Lib3dsCamera), 1);
00056   if (!camera) {
00057     return(0);
00058   }
00059   strcpy(camera->name, name);
00060   camera->fov=45.0f;
00061   return(camera);
00062 }
00063 
00064 
00072 void
00073 lib3ds_camera_free(Lib3dsCamera *camera)
00074 {
00075   memset(camera, 0, sizeof(Lib3dsCamera));
00076   free(camera);
00077 }
00078 
00079 
00089 void
00090 lib3ds_camera_dump(Lib3dsCamera *camera)
00091 {
00092   ASSERT(camera);
00093   printf("  name:       %s\n", camera->name);
00094   printf("  position:   (%f, %f, %f)\n", 
00095     camera->position[0], camera->position[1], camera->position[2]);
00096   printf("  target      (%f, %f, %f)\n", 
00097     camera->target[0], camera->target[1], camera->target[2]);
00098   printf("  roll:       %f\n", camera->roll);
00099   printf("  fov:        %f\n", camera->fov);
00100   printf("  see_cone:   %s\n", camera->see_cone ? "yes" : "no");
00101   printf("  near_range: %f\n", camera->near_range);
00102   printf("  far_range:  %f\n", camera->far_range);
00103   printf("\n");
00104 }
00105 
00106 
00122 Lib3dsBool
00123 lib3ds_camera_read(Lib3dsCamera *camera, Lib3dsIo *io)
00124 {
00125   Lib3dsChunk c;
00126   Lib3dsWord chunk;
00127 
00128   if (!lib3ds_chunk_read_start(&c, LIB3DS_N_CAMERA, io)) {
00129     return(LIB3DS_FALSE);
00130   }
00131   {
00132     int i;
00133     for (i=0; i<3; ++i) {
00134       camera->position[i]=lib3ds_io_read_float(io);
00135     }
00136     for (i=0; i<3; ++i) {
00137       camera->target[i]=lib3ds_io_read_float(io);
00138     }
00139   }
00140   camera->roll=lib3ds_io_read_float(io);
00141   {
00142     float s;
00143     s=lib3ds_io_read_float(io);
00144     if (fabs(s)<LIB3DS_EPSILON) {
00145       camera->fov=45.0;
00146     }
00147     else {
00148       camera->fov=2400.0f/s;
00149     }
00150   }
00151   lib3ds_chunk_read_tell(&c, io);
00152   
00153   while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
00154     switch (chunk) {
00155       case LIB3DS_CAM_SEE_CONE:
00156         {
00157           camera->see_cone=LIB3DS_TRUE;
00158         }
00159         break;
00160       case LIB3DS_CAM_RANGES:
00161         {
00162           camera->near_range=lib3ds_io_read_float(io);
00163           camera->far_range=lib3ds_io_read_float(io);
00164         }
00165         break;
00166       default:
00167         lib3ds_chunk_unknown(chunk);
00168     }
00169   }
00170   
00171   lib3ds_chunk_read_end(&c, io);
00172   return(LIB3DS_TRUE);
00173 }
00174 
00175 
00191 Lib3dsBool
00192 lib3ds_camera_write(Lib3dsCamera *camera, Lib3dsIo *io)
00193 {
00194   Lib3dsChunk c;
00195 
00196   c.chunk=LIB3DS_N_CAMERA;
00197   if (!lib3ds_chunk_write_start(&c,io)) {
00198     return(LIB3DS_FALSE);
00199   }
00200 
00201   lib3ds_io_write_vector(io, camera->position);
00202   lib3ds_io_write_vector(io, camera->target);
00203   lib3ds_io_write_float(io, camera->roll);
00204   if (fabs(camera->fov)<LIB3DS_EPSILON) {
00205     lib3ds_io_write_float(io, 2400.0f/45.0f);
00206   }
00207   else {
00208     lib3ds_io_write_float(io, 2400.0f/camera->fov);
00209   }
00210 
00211   if (camera->see_cone) {
00212     Lib3dsChunk c;
00213     c.chunk=LIB3DS_CAM_SEE_CONE;
00214     c.size=6;
00215     lib3ds_chunk_write(&c, io);
00216   }
00217   {
00218     Lib3dsChunk c;
00219     c.chunk=LIB3DS_CAM_RANGES;
00220     c.size=14;
00221     lib3ds_chunk_write(&c, io);
00222     lib3ds_io_write_float(io, camera->near_range);
00223     lib3ds_io_write_float(io, camera->far_range);
00224   }
00225 
00226   if (!lib3ds_chunk_write_end(&c,io)) {
00227     return(LIB3DS_FALSE);
00228   }
00229   return(LIB3DS_TRUE);
00230 }
00231 

Hosted by
SourceForge.net Logo
Generated at Wed Jun 20 18:51:36 2007 by Doxygen 1.5.2