root/OpenSceneGraph/trunk/src/osgPlugins/3ds/camera.cpp @ 10076

Revision 10076, 4.3 kB (checked in by robert, 6 years ago)

From Neil Hughes, converted across to use istream for reading data from file to enable reading .3ds files over http (use OSG's libcurl plugin).

From Robert Osfield, ammendments of the above to better support reading of files from local directories.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[8]1/*
2 * The 3D Studio File Format Library
3 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
4 * All rights reserved.
5 *
6 * This program is  free  software;  you can redistribute it and/or modify it
[151]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
[8]9 * your option) any later version.
10 *
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
[151]13 * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public 
[8]14 * License for more details.
15 *
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.
19 *
20 * $Id$
21 */
22#define LIB3DS_EXPORT
[1563]23#include "camera.h"
24#include "chunk.h"
25#include "readwrite.h"
[8]26#include <stdlib.h>
27#include <math.h>
28#include <string.h>
[1563]29#include "config.h"
[8]30#ifdef WITH_DMALLOC
31#include <dmalloc.h>
32#endif
33
[151]34
[8]35/*!
36 * \defgroup camera Cameras
37 *
38 * \author J.E. Hoffmann <je-h@gmx.net>
39 */
40
[151]41
[8]42/*!
43 * \ingroup camera
44 */
45Lib3dsCamera*
46lib3ds_camera_new(const char *name)
47{
[151]48  Lib3dsCamera *camera;
[8]49
[151]50  ASSERT(name);
51  ASSERT(strlen(name)<64);
52 
53  camera=(Lib3dsCamera*)calloc(sizeof(Lib3dsCamera), 1);
54  if (!camera) {
55    return(0);
56  }
57  strcpy(camera->name, name);
58  camera->fov=45.0f;
59  return(camera);
60}
[8]61
[151]62
63/*!
64 * \ingroup camera
65 */
66void
67lib3ds_camera_free(Lib3dsCamera *camera)
68{
69  memset(camera, 0, sizeof(Lib3dsCamera));
70  free(camera);
[8]71}
72
73
74/*!
75 * \ingroup camera
76 */
77void
[151]78lib3ds_camera_dump(Lib3dsCamera *camera)
[8]79{
[151]80  ASSERT(camera);
81  printf("  name:       %s\n", camera->name);
82  printf("  position:   (%f, %f, %f)\n",
83    camera->position[0], camera->position[1], camera->position[2]);
84  printf("  target      (%f, %f, %f)\n",
85    camera->target[0], camera->target[1], camera->target[2]);
86  printf("  roll:       %f\n", camera->roll);
87  printf("  fov:        %f\n", camera->fov);
88  printf("  see_cone:   %s\n", camera->see_cone ? "yes" : "no");
89  printf("  near_range: %f\n", camera->near_range);
90  printf("  far_range:  %f\n", camera->near_range);
91  printf("\n");
[8]92}
93
94
95/*!
96 * \ingroup camera
97 */
98Lib3dsBool
[10076]99lib3ds_camera_read(Lib3dsCamera *camera, iostream *strm)
[8]100{
[151]101  Lib3dsChunk c;
102  Lib3dsWord chunk;
[8]103
[10076]104  if (!lib3ds_chunk_read_start(&c, LIB3DS_N_CAMERA, strm)) {
[151]105    return(LIB3DS_FALSE);
106  }
107  {
108    int i;
109    for (i=0; i<3; ++i) {
[10076]110      camera->position[i]=lib3ds_float_read(strm);
[8]111    }
[151]112    for (i=0; i<3; ++i) {
[10076]113      camera->target[i]=lib3ds_float_read(strm);
[8]114    }
[151]115  }
[10076]116  camera->roll=lib3ds_float_read(strm);
[151]117  {
118    float s;
[10076]119    s=lib3ds_float_read(strm);
[151]120    if (fabs(s)<LIB3DS_EPSILON) {
121      camera->fov=45.0;
122    }
123    else {
124      camera->fov=2400.0f/s;
125    }
126  }
[10076]127  lib3ds_chunk_read_tell(&c, strm);
[151]128 
[10076]129  while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) {
[151]130    switch (chunk) {
131      case LIB3DS_CAM_SEE_CONE:
[8]132        {
[151]133          camera->see_cone=LIB3DS_TRUE;
[8]134        }
[151]135        break;
136      case LIB3DS_CAM_RANGES:
[8]137        {
[10076]138          camera->near_range=lib3ds_float_read(strm);
139          camera->far_range=lib3ds_float_read(strm);
[8]140        }
[151]141        break;
142      default:
143        lib3ds_chunk_unknown(chunk);
[8]144    }
[151]145  }
146 
[10076]147  lib3ds_chunk_read_end(&c, strm);
[151]148  return(LIB3DS_TRUE);
[8]149}
150
151
152/*!
153 * \ingroup camera
154 */
155Lib3dsBool
[10076]156lib3ds_camera_write(Lib3dsCamera *camera, iostream *strm)
[8]157{
[151]158  Lib3dsChunk c;
[8]159
[151]160  c.chunk=LIB3DS_N_CAMERA;
[10076]161  if (!lib3ds_chunk_write_start(&c,strm)) {
[151]162    return(LIB3DS_FALSE);
163  }
[8]164
[10076]165  lib3ds_vector_write(camera->position, strm);
166  lib3ds_vector_write(camera->target, strm);
167  lib3ds_float_write(camera->roll, strm);
[151]168  if (fabs(camera->fov)<LIB3DS_EPSILON) {
[10076]169    lib3ds_float_write(2400.0f/45.0f, strm);
[151]170  }
171  else {
[10076]172    lib3ds_float_write(2400.0f/camera->fov, strm);
[151]173  }
[8]174
[151]175  if (camera->see_cone) {
176    Lib3dsChunk c;
177    c.chunk=LIB3DS_CAM_SEE_CONE;
178    c.size=6;
[10076]179    lib3ds_chunk_write(&c, strm);
[151]180  }
181  {
182    Lib3dsChunk c;
183    c.chunk=LIB3DS_CAM_RANGES;
184    c.size=14;
[10076]185    lib3ds_chunk_write(&c, strm);
186    lib3ds_float_write(camera->near_range, strm);
187    lib3ds_float_write(camera->far_range, strm);
[151]188  }
[8]189
[10076]190  if (!lib3ds_chunk_write_end(&c,strm)) {
[151]191    return(LIB3DS_FALSE);
192  }
193  return(LIB3DS_TRUE);
[8]194}
195
196
197/*!
198
199\typedef Lib3dsCamera
200  \ingroup camera
201  \sa _Lib3dsCamera
202
203*/
Note: See TracBrowser for help on using the browser.