00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "lib3ds_impl.h"
00019
00020
00021 static void
00022 solid_bgnd_read(Lib3dsBackground *background, Lib3dsIo *io) {
00023 Lib3dsChunk c;
00024 uint16_t chunk;
00025 int have_lin = FALSE;
00026
00027 lib3ds_chunk_read_start(&c, CHK_SOLID_BGND, io);
00028
00029 while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
00030 switch (chunk) {
00031 case CHK_LIN_COLOR_F:
00032 lib3ds_io_read_rgb(io, background->solid_color);
00033 have_lin = TRUE;
00034 break;
00035
00036 case CHK_COLOR_F:
00037 lib3ds_io_read_rgb(io, background->solid_color);
00038 break;
00039
00040 default:
00041 lib3ds_chunk_unknown(chunk, io);
00042 }
00043 }
00044
00045 lib3ds_chunk_read_end(&c, io);
00046 }
00047
00048
00049 static void
00050 v_gradient_read(Lib3dsBackground *background, Lib3dsIo *io) {
00051 Lib3dsChunk c;
00052 uint16_t chunk;
00053 int index[2];
00054 float col[2][3][3];
00055 int have_lin = 0;
00056
00057 lib3ds_chunk_read_start(&c, CHK_V_GRADIENT, io);
00058
00059 background->gradient_percent = lib3ds_io_read_float(io);
00060 lib3ds_chunk_read_tell(&c, io);
00061
00062 index[0] = index[1] = 0;
00063 while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
00064 switch (chunk) {
00065 case CHK_COLOR_F:
00066 lib3ds_io_read_rgb(io, col[0][index[0]]);
00067 index[0]++;
00068 break;
00069
00070 case CHK_LIN_COLOR_F:
00071 lib3ds_io_read_rgb(io, col[1][index[1]]);
00072 index[1]++;
00073 have_lin = 1;
00074 break;
00075
00076 default:
00077 lib3ds_chunk_unknown(chunk, io);
00078 }
00079 }
00080 {
00081 int i;
00082 for (i = 0; i < 3; ++i) {
00083 background->gradient_top[i] = col[have_lin][0][i];
00084 background->gradient_middle[i] = col[have_lin][1][i];
00085 background->gradient_bottom[i] = col[have_lin][2][i];
00086 }
00087 }
00088 lib3ds_chunk_read_end(&c, io);
00089 }
00090
00091
00092 void
00093 lib3ds_background_read(Lib3dsBackground *background, Lib3dsIo *io) {
00094 Lib3dsChunk c;
00095
00096 lib3ds_chunk_read(&c, io);
00097 switch (c.chunk) {
00098 case CHK_BIT_MAP: {
00099 lib3ds_io_read_string(io, background->bitmap_name, 64);
00100 break;
00101 }
00102
00103 case CHK_SOLID_BGND: {
00104 lib3ds_chunk_read_reset(&c, io);
00105 solid_bgnd_read(background, io);
00106 break;
00107 }
00108
00109 case CHK_V_GRADIENT: {
00110 lib3ds_chunk_read_reset(&c, io);
00111 v_gradient_read(background, io);
00112 break;
00113 }
00114
00115 case CHK_USE_BIT_MAP: {
00116 background->use_bitmap = TRUE;
00117 break;
00118 }
00119
00120 case CHK_USE_SOLID_BGND: {
00121 background->use_solid = TRUE;
00122 break;
00123 }
00124
00125 case CHK_USE_V_GRADIENT: {
00126 background->use_gradient = TRUE;
00127 break;
00128 }
00129 }
00130 }
00131
00132
00133 static void
00134 colorf_write(float rgb[3], Lib3dsIo *io) {
00135 Lib3dsChunk c;
00136
00137 c.chunk = CHK_COLOR_F;
00138 c.size = 18;
00139 lib3ds_chunk_write(&c, io);
00140 lib3ds_io_write_rgb(io, rgb);
00141
00142 c.chunk = CHK_LIN_COLOR_F;
00143 c.size = 18;
00144 lib3ds_chunk_write(&c, io);
00145 lib3ds_io_write_rgb(io, rgb);
00146 }
00147
00148
00149 static int
00150 colorf_defined(float rgb[3]) {
00151 int i;
00152 for (i = 0; i < 3; ++i) {
00153 if (fabs(rgb[i]) > LIB3DS_EPSILON) {
00154 break;
00155 }
00156 }
00157 return(i < 3);
00158 }
00159
00160
00161 void
00162 lib3ds_background_write(Lib3dsBackground *background, Lib3dsIo *io) {
00163 if (strlen(background->bitmap_name)) {
00164 Lib3dsChunk c;
00165 c.chunk = CHK_BIT_MAP;
00166 c.size = 6 + 1 + (uint32_t)strlen(background->bitmap_name);
00167 lib3ds_chunk_write(&c, io);
00168 lib3ds_io_write_string(io, background->bitmap_name);
00169 }
00170
00171 if (colorf_defined(background->solid_color)) {
00172 Lib3dsChunk c;
00173 c.chunk = CHK_SOLID_BGND;
00174 c.size = 42;
00175 lib3ds_chunk_write(&c, io);
00176 colorf_write(background->solid_color, io);
00177 }
00178
00179 if (colorf_defined(background->gradient_top) ||
00180 colorf_defined(background->gradient_middle) ||
00181 colorf_defined(background->gradient_bottom)) {
00182 Lib3dsChunk c;
00183 c.chunk = CHK_V_GRADIENT;
00184 c.size = 118;
00185 lib3ds_chunk_write(&c, io);
00186 lib3ds_io_write_float(io, background->gradient_percent);
00187 colorf_write(background->gradient_top, io);
00188 colorf_write(background->gradient_middle, io);
00189 colorf_write(background->gradient_bottom, io);
00190 }
00191
00192 if (background->use_bitmap) {
00193 Lib3dsChunk c;
00194 c.chunk = CHK_USE_BIT_MAP;
00195 c.size = 6;
00196 lib3ds_chunk_write(&c, io);
00197 }
00198
00199 if (background->use_solid) {
00200 Lib3dsChunk c;
00201 c.chunk = CHK_USE_SOLID_BGND;
00202 c.size = 6;
00203 lib3ds_chunk_write(&c, io);
00204 }
00205
00206 if (background->use_gradient) {
00207 Lib3dsChunk c;
00208 c.chunk = CHK_USE_V_GRADIENT;
00209 c.size = 6;
00210 lib3ds_chunk_write(&c, io);
00211 }
00212 }
00213