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

Revision 10853, 5.9 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
21static void
22solid_bgnd_read(Lib3dsBackground *background, Lib3dsIo *io) {
23    Lib3dsChunk c;
24    uint16_t chunk;
25    int have_lin = FALSE;
26
27    lib3ds_chunk_read_start(&c, CHK_SOLID_BGND, io);
28
29    while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
30        switch (chunk) {
31            case CHK_LIN_COLOR_F:
32                lib3ds_io_read_rgb(io, background->solid_color);
33                have_lin = TRUE;
34                break;
35
36            case CHK_COLOR_F:
37                lib3ds_io_read_rgb(io, background->solid_color);
38                break;
39
40            default:
41                lib3ds_chunk_unknown(chunk, io);
42        }
43    }
44
45    lib3ds_chunk_read_end(&c, io);
46}
47
48
49static void
50v_gradient_read(Lib3dsBackground *background, Lib3dsIo *io) {
51    Lib3dsChunk c;
52    uint16_t chunk;
53    int index[2];
54    float col[2][3][3];
55    int have_lin = 0;
56
57    lib3ds_chunk_read_start(&c, CHK_V_GRADIENT, io);
58
59    background->gradient_percent = lib3ds_io_read_float(io);
60    lib3ds_chunk_read_tell(&c, io);
61
62    index[0] = index[1] = 0;
63    while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
64        switch (chunk) {
65            case CHK_COLOR_F:
66                lib3ds_io_read_rgb(io, col[0][index[0]]);
67                index[0]++;
68                break;
69
70            case CHK_LIN_COLOR_F:
71                lib3ds_io_read_rgb(io, col[1][index[1]]);
72                index[1]++;
73                have_lin = 1;
74                break;
75
76            default:
77                lib3ds_chunk_unknown(chunk, io);
78        }
79    }
80    {
81        int i;
82        for (i = 0; i < 3; ++i) {
83            background->gradient_top[i] = col[have_lin][0][i];
84            background->gradient_middle[i] = col[have_lin][1][i];
85            background->gradient_bottom[i] = col[have_lin][2][i];
86        }
87    }
88    lib3ds_chunk_read_end(&c, io);
89}
90
91
92void
93lib3ds_background_read(Lib3dsBackground *background, Lib3dsIo *io) {
94    Lib3dsChunk c;
95
96    lib3ds_chunk_read(&c, io);
97    switch (c.chunk) {
98        case CHK_BIT_MAP: {
99            lib3ds_io_read_string(io, background->bitmap_name, 64);
100            break;
101        }
102
103        case CHK_SOLID_BGND: {
104            lib3ds_chunk_read_reset(&c, io);
105            solid_bgnd_read(background, io);
106            break;
107        }
108
109        case CHK_V_GRADIENT: {
110            lib3ds_chunk_read_reset(&c, io);
111            v_gradient_read(background, io);
112            break;
113        }
114
115        case CHK_USE_BIT_MAP: {
116            background->use_bitmap = TRUE;
117            break;
118        }
119
120        case CHK_USE_SOLID_BGND: {
121            background->use_solid = TRUE;
122            break;
123        }
124
125        case CHK_USE_V_GRADIENT: {
126            background->use_gradient = TRUE;
127            break;
128        }
129    }
130}
131
132
133static void
134colorf_write(float rgb[3], Lib3dsIo *io) {
135    Lib3dsChunk c;
136
137    c.chunk = CHK_COLOR_F;
138    c.size = 18;
139    lib3ds_chunk_write(&c, io);
140    lib3ds_io_write_rgb(io, rgb);
141
142    c.chunk = CHK_LIN_COLOR_F;
143    c.size = 18;
144    lib3ds_chunk_write(&c, io);
145    lib3ds_io_write_rgb(io, rgb);
146}
147
148
149static int
150colorf_defined(float rgb[3]) {
151    int i;
152    for (i = 0; i < 3; ++i) {
153        if (fabs(rgb[i]) > LIB3DS_EPSILON) {
154            break;
155        }
156    }
157    return(i < 3);
158}
159
160
161void
162lib3ds_background_write(Lib3dsBackground *background, Lib3dsIo *io) {
163    if (strlen(background->bitmap_name)) { /*---- LIB3DS_BIT_MAP ----*/
164        Lib3dsChunk c;
165        c.chunk = CHK_BIT_MAP;
166        c.size = 6 + 1 + (uint32_t)strlen(background->bitmap_name);
167        lib3ds_chunk_write(&c, io);
168        lib3ds_io_write_string(io, background->bitmap_name);
169    }
170
171    if (colorf_defined(background->solid_color)) { /*---- LIB3DS_SOLID_BGND ----*/
172        Lib3dsChunk c;
173        c.chunk = CHK_SOLID_BGND;
174        c.size = 42;
175        lib3ds_chunk_write(&c, io);
176        colorf_write(background->solid_color, io);
177    }
178
179    if (colorf_defined(background->gradient_top) ||
180        colorf_defined(background->gradient_middle) ||
181        colorf_defined(background->gradient_bottom)) { /*---- LIB3DS_V_GRADIENT ----*/
182        Lib3dsChunk c;
183        c.chunk = CHK_V_GRADIENT;
184        c.size = 118;
185        lib3ds_chunk_write(&c, io);
186        lib3ds_io_write_float(io, background->gradient_percent);
187        colorf_write(background->gradient_top, io);
188        colorf_write(background->gradient_middle, io);
189        colorf_write(background->gradient_bottom, io);
190    }
191
192    if (background->use_bitmap) { /*---- LIB3DS_USE_BIT_MAP ----*/
193        Lib3dsChunk c;
194        c.chunk = CHK_USE_BIT_MAP;
195        c.size = 6;
196        lib3ds_chunk_write(&c, io);
197    }
198
199    if (background->use_solid) { /*---- LIB3DS_USE_SOLID_BGND ----*/
200        Lib3dsChunk c;
201        c.chunk = CHK_USE_SOLID_BGND;
202        c.size = 6;
203        lib3ds_chunk_write(&c, io);
204    }
205
206    if (background->use_gradient) { /*---- LIB3DS_USE_V_GRADIENT ----*/
207        Lib3dsChunk c;
208        c.chunk = CHK_USE_V_GRADIENT;
209        c.size = 6;
210        lib3ds_chunk_write(&c, io);
211    }
212}
Note: See TracBrowser for help on using the browser.