Home | API | File List | Examples | Download

lib3ds_camera.c

00001 /*
00002     Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
00003     All rights reserved.
00004     
00005     This program is free  software: you can redistribute it and/or modify 
00006     it under the terms of the GNU Lesser General Public License as published 
00007     by the Free Software Foundation, either version 2.1 of the License, or 
00008     (at your option) any later version.
00009 
00010     Thisprogram  is  distributed in the hope that it will be useful, 
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
00013     GNU Lesser General Public License for more details.
00014     
00015     You should  have received a copy of the GNU Lesser General Public License
00016     along with  this program; If not, see <http://www.gnu.org/licenses/>. 
00017 */
00018 #include "lib3ds_impl.h"
00019 
00020 
00031 Lib3dsCamera*
00032 lib3ds_camera_new(const char *name) {
00033     Lib3dsCamera *camera;
00034 
00035     assert(name);
00036     assert(strlen(name) < 64);
00037 
00038     camera = (Lib3dsCamera*)calloc(sizeof(Lib3dsCamera), 1);
00039     if (!camera) {
00040         return(0);
00041     }
00042     strcpy(camera->name, name);
00043     camera->fov = 45.0f;
00044     return(camera);
00045 }
00046 
00047 
00053 void
00054 lib3ds_camera_free(Lib3dsCamera *camera) {
00055     memset(camera, 0, sizeof(Lib3dsCamera));
00056     free(camera);
00057 }
00058 
00059 
00071 void
00072 lib3ds_camera_read(Lib3dsCamera *camera, Lib3dsIo *io) {
00073     Lib3dsChunk c;
00074     uint16_t chunk;
00075 
00076     lib3ds_chunk_read_start(&c, CHK_N_CAMERA, io);
00077 
00078     {
00079         int i;
00080         for (i = 0; i < 3; ++i) {
00081             camera->position[i] = lib3ds_io_read_float(io);
00082         }
00083         for (i = 0; i < 3; ++i) {
00084             camera->target[i] = lib3ds_io_read_float(io);
00085         }
00086     }
00087     camera->roll = lib3ds_io_read_float(io);
00088     {
00089         float s;
00090         s = lib3ds_io_read_float(io);
00091         if (fabs(s) < LIB3DS_EPSILON) {
00092             camera->fov = 45.0;
00093         } else {
00094             camera->fov = 2400.0f / s;
00095         }
00096     }
00097     lib3ds_chunk_read_tell(&c, io);
00098 
00099     while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
00100         switch (chunk) {
00101             case CHK_CAM_SEE_CONE: {
00102                 camera->see_cone = TRUE;
00103             }
00104             break;
00105 
00106             case CHK_CAM_RANGES: {
00107                 camera->near_range = lib3ds_io_read_float(io);
00108                 camera->far_range = lib3ds_io_read_float(io);
00109             }
00110             break;
00111 
00112             default:
00113                 lib3ds_chunk_unknown(chunk, io);
00114         }
00115     }
00116 
00117     lib3ds_chunk_read_end(&c, io);
00118 }
00119 
00120 
00132 void
00133 lib3ds_camera_write(Lib3dsCamera *camera, Lib3dsIo *io) {
00134     Lib3dsChunk c;
00135 
00136     c.chunk = CHK_N_CAMERA;
00137     lib3ds_chunk_write_start(&c, io);
00138 
00139     lib3ds_io_write_vector(io, camera->position);
00140     lib3ds_io_write_vector(io, camera->target);
00141     lib3ds_io_write_float(io, camera->roll);
00142     if (fabs(camera->fov) < LIB3DS_EPSILON) {
00143         lib3ds_io_write_float(io, 2400.0f / 45.0f);
00144     } else {
00145         lib3ds_io_write_float(io, 2400.0f / camera->fov);
00146     }
00147 
00148     if (camera->see_cone) {
00149         Lib3dsChunk c;
00150         c.chunk = CHK_CAM_SEE_CONE;
00151         c.size = 6;
00152         lib3ds_chunk_write(&c, io);
00153     }
00154     {
00155         Lib3dsChunk c;
00156         c.chunk = CHK_CAM_RANGES;
00157         c.size = 14;
00158         lib3ds_chunk_write(&c, io);
00159         lib3ds_io_write_float(io, camera->near_range);
00160         lib3ds_io_write_float(io, camera->far_range);
00161     }
00162 
00163     lib3ds_chunk_write_end(&c, io);
00164 }
00165