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

Revision 10853, 3.7 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/*#define LIB3DS_CHUNK_DEBUG*/
22/*#define LIB3DS_CHUNK_WARNING*/
23
24
25/*!
26 * Reads a 3d-Studio chunk header from a little endian file stream.
27 *
28 * \param c  The chunk to store the data.
29 * \param io The file stream.
30 *
31 * \return   True on success, False otherwise.
32 */
33void
34lib3ds_chunk_read(Lib3dsChunk *c, Lib3dsIo *io) {
35    assert(c);
36    assert(io);
37    c->cur = lib3ds_io_tell(io);
38    c->chunk = lib3ds_io_read_word(io);
39    c->size = lib3ds_io_read_dword(io);
40    c->end = c->cur + c->size;
41    c->cur += 6;
42    if (c->size < 6) {
43        lib3ds_io_log(io, LIB3DS_LOG_ERROR, "Invalid chunk header.");
44    }
45
46}
47
48
49void
50lib3ds_chunk_read_start(Lib3dsChunk *c, uint16_t chunk, Lib3dsIo *io) {
51    assert(c);
52    assert(io);
53    lib3ds_chunk_read(c, io);
54    if ((chunk != 0) && (c->chunk != chunk)) {
55        lib3ds_io_log(io, LIB3DS_LOG_ERROR, "Unexpected chunk found.");
56    }
57    ((Lib3dsIoImpl*)io->impl)->log_indent++;
58}
59
60
61void
62lib3ds_chunk_read_tell(Lib3dsChunk *c, Lib3dsIo *io) {
63    c->cur = lib3ds_io_tell(io);
64}
65
66
67uint16_t
68lib3ds_chunk_read_next(Lib3dsChunk *c, Lib3dsIo *io) {
69    Lib3dsChunk d;
70
71    if (c->cur >= c->end) {
72        assert(c->cur == c->end);
73        return 0;
74    }
75
76    lib3ds_io_seek(io, (long)c->cur, LIB3DS_SEEK_SET);
77    d.chunk = lib3ds_io_read_word(io);
78    d.size = lib3ds_io_read_dword(io);
79    c->cur += d.size;
80
81    if (io->log_func) {
82        lib3ds_io_log(io, LIB3DS_LOG_INFO, "%s (0x%X) size=%lu", lib3ds_chunk_name(d.chunk), d.chunk, d.size);
83    }
84
85    return d.chunk;
86}
87
88
89void
90lib3ds_chunk_read_reset(Lib3dsChunk *c, Lib3dsIo *io) {
91    lib3ds_io_seek(io, -6, LIB3DS_SEEK_CUR);
92}
93
94
95void
96lib3ds_chunk_read_end(Lib3dsChunk *c, Lib3dsIo *io) {
97    ((Lib3dsIoImpl*)io->impl)->log_indent--;
98    lib3ds_io_seek(io, c->end, LIB3DS_SEEK_SET);
99}
100
101
102/*!
103 * Writes a 3d-Studio chunk header into a little endian file stream.
104 *
105 * \param c  The chunk to be written.
106 * \param io The file stream.
107 *
108 * \return   True on success, False otherwise.
109 */
110void
111lib3ds_chunk_write(Lib3dsChunk *c, Lib3dsIo *io) {
112    assert(c);
113    lib3ds_io_write_word(io, c->chunk);
114    lib3ds_io_write_dword(io, c->size);
115}
116
117
118void
119lib3ds_chunk_write_start(Lib3dsChunk *c, Lib3dsIo *io) {
120    assert(c);
121    c->size = 0;
122    c->cur = lib3ds_io_tell(io);
123    lib3ds_io_write_word(io, c->chunk);
124    lib3ds_io_write_dword(io, c->size);
125}
126
127
128void
129lib3ds_chunk_write_end(Lib3dsChunk *c, Lib3dsIo *io) {
130    assert(c);
131    c->size = lib3ds_io_tell(io) - c->cur;
132    lib3ds_io_seek(io, c->cur + 2, LIB3DS_SEEK_SET);
133    lib3ds_io_write_dword(io, c->size);
134    c->cur += c->size;
135    lib3ds_io_seek(io, c->cur, LIB3DS_SEEK_SET);
136}
137
138
139void
140lib3ds_chunk_unknown(uint16_t chunk, Lib3dsIo *io) {
141    if (io->log_func) {
142        lib3ds_io_log(io, LIB3DS_LOG_WARN, "Unknown Chunk: %s (0x%X)", lib3ds_chunk_name(chunk), chunk);
143    }
144}
145
Note: See TracBrowser for help on using the browser.