root/OpenSceneGraph/trunk/src/osgPlugins/3ds/lib3ds/lib3ds_camera.c @ 10853

Revision 10853, 4.4 kB (checked in by robert, 5 years ago)

From Sukender,
"Here is our freshly baked 3DS reader/writer (named 'v0.5' to differentiate from previous one). Changes are against trunk rev. 10819.
Short changelog (from rev 10819):
- Added 3DS writer
- Sync'd with latest lib3DS
- Added options, especially "flattenMatrixTransforms" to get the "old" behaviour (else the reader correctly maps to OSG the transforms from the 3DS file).

What should be done:
- Check with pivot points, with and without "flattenMatrixTransforms" option.
- We ran tests on it, but we can never be 100% sure there is no bug. Testing from the community would of course be helpful."

RevLine 
[10853]1/*
2    Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
3    All rights reserved.
4   
5    This program is free  software: you can redistribute it and/or modify
6    it under the terms of the GNU Lesser General Public License as published
7    by the Free Software Foundation, either version 2.1 of the License, or
8    (at your option) any later version.
9
10    Thisprogram  is  distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    GNU Lesser General Public License for more details.
14   
15    You should  have received a copy of the GNU Lesser General Public License
16    along with  this program; If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "lib3ds_impl.h"
19
20
21/*!
22 * Return a new Lib3dsCamera object.
23 *
24 * Object is initialized with the given name and fov=45.  All other
25 * values are 0.
26 *
27 * \param name Name of this camera.  Must not be NULL.  Must be < 64 characters.
28 *
29 * \return Lib3dsCamera object or NULL on failure.
30 */
31Lib3dsCamera*
32lib3ds_camera_new(const char *name) {
33    Lib3dsCamera *camera;
34
35    assert(name);
36    assert(strlen(name) < 64);
37
38    camera = (Lib3dsCamera*)calloc(sizeof(Lib3dsCamera), 1);
39    if (!camera) {
40        return(0);
41    }
42    strcpy(camera->name, name);
43    camera->fov = 45.0f;
44    return(camera);
45}
46
47
48/*!
49 * Free a Lib3dsCamera object and all of its resources.
50 *
51 * \param camera Lib3dsCamera object to be freed.
52 */
53void
54lib3ds_camera_free(Lib3dsCamera *camera) {
55    memset(camera, 0, sizeof(Lib3dsCamera));
56    free(camera);
57}
58
59
60/*!
61 * Read a camera definition from a file.
62 *
63 * This function is called by lib3ds_file_read(), and you probably
64 * don't want to call it directly.
65 *
66 * \param camera A Lib3dsCamera to be filled in.
67 * \param io A Lib3dsIo object previously set up by the caller.
68 *
69 * \see lib3ds_file_read
70 */
71void
72lib3ds_camera_read(Lib3dsCamera *camera, Lib3dsIo *io) {
73    Lib3dsChunk c;
74    uint16_t chunk;
75
76    lib3ds_chunk_read_start(&c, CHK_N_CAMERA, io);
77
78    {
79        int i;
80        for (i = 0; i < 3; ++i) {
81            camera->position[i] = lib3ds_io_read_float(io);
82        }
83        for (i = 0; i < 3; ++i) {
84            camera->target[i] = lib3ds_io_read_float(io);
85        }
86    }
87    camera->roll = lib3ds_io_read_float(io);
88    {
89        float s;
90        s = lib3ds_io_read_float(io);
91        if (fabs(s) < LIB3DS_EPSILON) {
92            camera->fov = 45.0;
93        } else {
94            camera->fov = 2400.0f / s;
95        }
96    }
97    lib3ds_chunk_read_tell(&c, io);
98
99    while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
100        switch (chunk) {
101            case CHK_CAM_SEE_CONE: {
102                camera->see_cone = TRUE;
103            }
104            break;
105
106            case CHK_CAM_RANGES: {
107                camera->near_range = lib3ds_io_read_float(io);
108                camera->far_range = lib3ds_io_read_float(io);
109            }
110            break;
111
112            default:
113                lib3ds_chunk_unknown(chunk, io);
114        }
115    }
116
117    lib3ds_chunk_read_end(&c, io);
118}
119
120
121/*!
122 * Write a camera definition to a file.
123 *
124 * This function is called by lib3ds_file_write(), and you probably
125 * don't want to call it directly.
126 *
127 * \param camera A Lib3dsCamera to be written.
128 * \param io A Lib3dsIo object previously set up by the caller.
129 *
130 * \see lib3ds_file_write
131 */
132void
133lib3ds_camera_write(Lib3dsCamera *camera, Lib3dsIo *io) {
134    Lib3dsChunk c;
135
136    c.chunk = CHK_N_CAMERA;
137    lib3ds_chunk_write_start(&c, io);
138
139    lib3ds_io_write_vector(io, camera->position);
140    lib3ds_io_write_vector(io, camera->target);
141    lib3ds_io_write_float(io, camera->roll);
142    if (fabs(camera->fov) < LIB3DS_EPSILON) {
143        lib3ds_io_write_float(io, 2400.0f / 45.0f);
144    } else {
145        lib3ds_io_write_float(io, 2400.0f / camera->fov);
146    }
147
148    if (camera->see_cone) {
149        Lib3dsChunk c;
150        c.chunk = CHK_CAM_SEE_CONE;
151        c.size = 6;
152        lib3ds_chunk_write(&c, io);
153    }
154    {
155        Lib3dsChunk c;
156        c.chunk = CHK_CAM_RANGES;
157        c.size = 14;
158        lib3ds_chunk_write(&c, io);
159        lib3ds_io_write_float(io, camera->near_range);
160        lib3ds_io_write_float(io, camera->far_range);
161    }
162
163    lib3ds_chunk_write_end(&c, io);
164}
Note: See TracBrowser for help on using the browser.