1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/*
* 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: camera.c,v 1.12 2004/11/16 07:41:44 efalk Exp $
*/
#define LIB3DS_EXPORT
#include <lib3ds/camera.h>
#include <lib3ds/chunk.h>
#include <lib3ds/io.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <config.h>
#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif
/*!
* \defgroup camera Cameras
*
* \author J.E. Hoffmann <je-h@gmx.net>
*/
/*!
* Return a new Lib3dsCamera object.
*
* Object is initialized with the given name and fov=45. All other
* values are 0.
*
* \param name Name of this camera. Must not be NULL. Must be < 64 characters.
*
* \return Lib3dsCamera object or NULL on failure.
*
* \ingroup camera
*/
Lib3dsCamera*
lib3ds_camera_new(const char *name)
{
Lib3dsCamera *camera;
ASSERT(name);
ASSERT(strlen(name)<64);
camera=(Lib3dsCamera*)calloc(sizeof(Lib3dsCamera), 1);
if (!camera) {
return(0);
}
strcpy(camera->name, name);
camera->fov=45.0f;
return(camera);
}
/*!
* Free a Lib3dsCamera object and all of its resources.
*
* \param camera Lib3dsCamera object to be freed.
*
* \ingroup camera
*/
void
lib3ds_camera_free(Lib3dsCamera *camera)
{
memset(camera, 0, sizeof(Lib3dsCamera));
free(camera);
}
/*!
* Dump information about a Lib3dsCamera object to stdout.
*
* \param camera Object to be dumped.
*
* \see lib3ds_file_dump_cameras
*
* \ingroup camera
*/
void
lib3ds_camera_dump(Lib3dsCamera *camera)
{
ASSERT(camera);
printf(" name: %s\n", camera->name);
printf(" position: (%f, %f, %f)\n",
camera->position[0], camera->position[1], camera->position[2]);
printf(" target (%f, %f, %f)\n",
camera->target[0], camera->target[1], camera->target[2]);
printf(" roll: %f\n", camera->roll);
printf(" fov: %f\n", camera->fov);
printf(" see_cone: %s\n", camera->see_cone ? "yes" : "no");
printf(" near_range: %f\n", camera->near_range);
printf(" far_range: %f\n", camera->far_range);
printf("\n");
}
/*!
* Read a camera definition from a file.
*
* This function is called by lib3ds_file_read(), and you probably
* don't want to call it directly.
*
* \param camera A Lib3dsCamera to be filled in.
* \param io A Lib3dsIo object previously set up by the caller.
*
* \return LIB3DS_TRUE on success, LIB3DS_FALSE on failure.
*
* \see lib3ds_file_read
*
* \ingroup camera
*/
Lib3dsBool
lib3ds_camera_read(Lib3dsCamera *camera, Lib3dsIo *io)
{
Lib3dsChunk c;
Lib3dsWord chunk;
if (!lib3ds_chunk_read_start(&c, LIB3DS_N_CAMERA, io)) {
return(LIB3DS_FALSE);
}
{
int i;
for (i=0; i<3; ++i) {
camera->position[i]=lib3ds_io_read_float(io);
}
for (i=0; i<3; ++i) {
camera->target[i]=lib3ds_io_read_float(io);
}
}
camera->roll=lib3ds_io_read_float(io);
{
float s;
s=lib3ds_io_read_float(io);
if (fabs(s)<LIB3DS_EPSILON) {
camera->fov=45.0;
}
else {
camera->fov=2400.0f/s;
}
}
lib3ds_chunk_read_tell(&c, io);
while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
switch (chunk) {
case LIB3DS_CAM_SEE_CONE:
{
camera->see_cone=LIB3DS_TRUE;
}
break;
case LIB3DS_CAM_RANGES:
{
camera->near_range=lib3ds_io_read_float(io);
camera->far_range=lib3ds_io_read_float(io);
}
break;
default:
lib3ds_chunk_unknown(chunk);
}
}
lib3ds_chunk_read_end(&c, io);
return(LIB3DS_TRUE);
}
/*!
* 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.
*
* \return LIB3DS_TRUE on success, LIB3DS_FALSE on failure.
*
* \see lib3ds_file_write
*
* \ingroup camera
*/
Lib3dsBool
lib3ds_camera_write(Lib3dsCamera *camera, Lib3dsIo *io)
{
Lib3dsChunk c;
c.chunk=LIB3DS_N_CAMERA;
if (!lib3ds_chunk_write_start(&c,io)) {
return(LIB3DS_FALSE);
}
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=LIB3DS_CAM_SEE_CONE;
c.size=6;
lib3ds_chunk_write(&c, io);
}
{
Lib3dsChunk c;
c.chunk=LIB3DS_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);
}
if (!lib3ds_chunk_write_end(&c,io)) {
return(LIB3DS_FALSE);
}
return(LIB3DS_TRUE);
}
/*!
\typedef Lib3dsCamera
\ingroup camera
\sa _Lib3dsCamera
*/
|