2 * The 3D Studio File Format Library
3 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * $Id: camera.c,v 1.12 2004/11/16 07:41:44 efalk Exp $
23 #include <lib3ds/camera.h>
24 #include <lib3ds/chunk.h>
25 #include <lib3ds/io.h>
35 * \defgroup camera Cameras
37 * \author J.E. Hoffmann <je-h@gmx.net>
42 * Return a new Lib3dsCamera object.
44 * Object is initialized with the given name and fov=45. All other
47 * \param name Name of this camera. Must not be NULL. Must be < 64 characters.
49 * \return Lib3dsCamera object or NULL on failure.
54 lib3ds_camera_new(const char *name)
59 ASSERT(strlen(name)<64);
61 camera=(Lib3dsCamera*)calloc(sizeof(Lib3dsCamera), 1);
65 strcpy(camera->name, name);
72 * Free a Lib3dsCamera object and all of its resources.
74 * \param camera Lib3dsCamera object to be freed.
79 lib3ds_camera_free(Lib3dsCamera *camera)
81 memset(camera, 0, sizeof(Lib3dsCamera));
87 * Dump information about a Lib3dsCamera object to stdout.
89 * \param camera Object to be dumped.
91 * \see lib3ds_file_dump_cameras
96 lib3ds_camera_dump(Lib3dsCamera *camera)
99 printf(" name: %s\n", camera->name);
100 printf(" position: (%f, %f, %f)\n",
101 camera->position[0], camera->position[1], camera->position[2]);
102 printf(" target (%f, %f, %f)\n",
103 camera->target[0], camera->target[1], camera->target[2]);
104 printf(" roll: %f\n", camera->roll);
105 printf(" fov: %f\n", camera->fov);
106 printf(" see_cone: %s\n", camera->see_cone ? "yes" : "no");
107 printf(" near_range: %f\n", camera->near_range);
108 printf(" far_range: %f\n", camera->far_range);
114 * Read a camera definition from a file.
116 * This function is called by lib3ds_file_read(), and you probably
117 * don't want to call it directly.
119 * \param camera A Lib3dsCamera to be filled in.
120 * \param io A Lib3dsIo object previously set up by the caller.
122 * \return LIB3DS_TRUE on success, LIB3DS_FALSE on failure.
124 * \see lib3ds_file_read
129 lib3ds_camera_read(Lib3dsCamera *camera, Lib3dsIo *io)
134 if (!lib3ds_chunk_read_start(&c, LIB3DS_N_CAMERA, io)) {
135 return(LIB3DS_FALSE);
139 for (i=0; i<3; ++i) {
140 camera->position[i]=lib3ds_io_read_float(io);
142 for (i=0; i<3; ++i) {
143 camera->target[i]=lib3ds_io_read_float(io);
146 camera->roll=lib3ds_io_read_float(io);
149 s=lib3ds_io_read_float(io);
150 if (fabs(s)<LIB3DS_EPSILON) {
154 camera->fov=2400.0f/s;
157 lib3ds_chunk_read_tell(&c, io);
159 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
161 case LIB3DS_CAM_SEE_CONE:
163 camera->see_cone=LIB3DS_TRUE;
166 case LIB3DS_CAM_RANGES:
168 camera->near_range=lib3ds_io_read_float(io);
169 camera->far_range=lib3ds_io_read_float(io);
173 lib3ds_chunk_unknown(chunk);
177 lib3ds_chunk_read_end(&c, io);
183 * Write a camera definition to a file.
185 * This function is called by lib3ds_file_write(), and you probably
186 * don't want to call it directly.
188 * \param camera A Lib3dsCamera to be written.
189 * \param io A Lib3dsIo object previously set up by the caller.
191 * \return LIB3DS_TRUE on success, LIB3DS_FALSE on failure.
193 * \see lib3ds_file_write
198 lib3ds_camera_write(Lib3dsCamera *camera, Lib3dsIo *io)
202 c.chunk=LIB3DS_N_CAMERA;
203 if (!lib3ds_chunk_write_start(&c,io)) {
204 return(LIB3DS_FALSE);
207 lib3ds_io_write_vector(io, camera->position);
208 lib3ds_io_write_vector(io, camera->target);
209 lib3ds_io_write_float(io, camera->roll);
210 if (fabs(camera->fov)<LIB3DS_EPSILON) {
211 lib3ds_io_write_float(io, 2400.0f/45.0f);
214 lib3ds_io_write_float(io, 2400.0f/camera->fov);
217 if (camera->see_cone) {
219 c.chunk=LIB3DS_CAM_SEE_CONE;
221 lib3ds_chunk_write(&c, io);
225 c.chunk=LIB3DS_CAM_RANGES;
227 lib3ds_chunk_write(&c, io);
228 lib3ds_io_write_float(io, camera->near_range);
229 lib3ds_io_write_float(io, camera->far_range);
232 if (!lib3ds_chunk_write_end(&c,io)) {
233 return(LIB3DS_FALSE);
241 \typedef Lib3dsCamera