From 78372a5a629634ed9a7e45da4a18da6cef7457dc Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 31 Oct 2001 18:33:27 +0000 Subject: [PATCH] Updated the lib3ds plugin with the new 1.1 version of lib3ds. --- src/osgPlugins/lib3ds/Makefile | 2 +- src/osgPlugins/lib3ds/ReaderWriter3DS.cpp | 4 +- src/osgPlugins/lib3ds/atmosphere.cpp | 451 ++--- src/osgPlugins/lib3ds/background.cpp | 327 +-- src/osgPlugins/lib3ds/camera.cpp | 224 +- src/osgPlugins/lib3ds/camera.h | 1 + src/osgPlugins/lib3ds/chunk.cpp | 245 +-- src/osgPlugins/lib3ds/chunk.h | 1 + src/osgPlugins/lib3ds/ease.cpp | 61 +- src/osgPlugins/lib3ds/file.cpp | 2248 ++++++++++----------- src/osgPlugins/lib3ds/file.h | 14 +- src/osgPlugins/lib3ds/lib3ds_float.cpp | 21 +- src/osgPlugins/lib3ds/light.cpp | 627 +++--- src/osgPlugins/lib3ds/light.h | 1 + src/osgPlugins/lib3ds/material.cpp | 1736 ++++++++-------- src/osgPlugins/lib3ds/matrix.cpp | 728 ++++--- src/osgPlugins/lib3ds/mesh.cpp | 1392 ++++++------- src/osgPlugins/lib3ds/mesh.h | 13 + src/osgPlugins/lib3ds/node.cpp | 1726 ++++++++-------- src/osgPlugins/lib3ds/node.h | 4 +- src/osgPlugins/lib3ds/quat.cpp | 338 ++-- src/osgPlugins/lib3ds/readwrite.cpp | 404 ++-- src/osgPlugins/lib3ds/shadow.cpp | 208 +- src/osgPlugins/lib3ds/shadow.h | 2 +- src/osgPlugins/lib3ds/tcb.cpp | 181 +- src/osgPlugins/lib3ds/tracks.cpp | 1951 ++++++++---------- src/osgPlugins/lib3ds/types.h | 20 +- src/osgPlugins/lib3ds/vector.cpp | 184 +- src/osgPlugins/lib3ds/vector.h | 2 + src/osgPlugins/lib3ds/viewport.cpp | 608 +++--- 30 files changed, 6700 insertions(+), 7024 deletions(-) diff --git a/src/osgPlugins/lib3ds/Makefile b/src/osgPlugins/lib3ds/Makefile index bf5fbe99a..19051523f 100644 --- a/src/osgPlugins/lib3ds/Makefile +++ b/src/osgPlugins/lib3ds/Makefile @@ -28,7 +28,7 @@ LIB = ../../../lib/osgPlugins/osgdb_3ds.so TARGET_LOADER_FILES = osgPlugins/osgdb_3ds.so LIBS = -C++FLAGS += -I. -I../../../include +C++FLAGS += -I. -I../../../include LDFLAGS += -L../../../lib include ../../../Make/makerules diff --git a/src/osgPlugins/lib3ds/ReaderWriter3DS.cpp b/src/osgPlugins/lib3ds/ReaderWriter3DS.cpp index 807fec8d2..927007827 100644 --- a/src/osgPlugins/lib3ds/ReaderWriter3DS.cpp +++ b/src/osgPlugins/lib3ds/ReaderWriter3DS.cpp @@ -62,7 +62,7 @@ ReaderWriter3DS::ReaderWriter3DS() osgDB::ReaderWriter::ReadResult ReaderWriter3DS::readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) { - Lib3dsFile *f = lib3ds_open(fileName.c_str()); + Lib3dsFile *f = lib3ds_file_load(fileName.c_str()); if (f==NULL) return ReadResult::FILE_NOT_HANDLED; _directory = osgDB::getFilePath(fileName); @@ -151,7 +151,7 @@ osgDB::ReaderWriter::ReadResult ReaderWriter3DS::readNode(const std::string& fil } - lib3ds_close(f); + lib3ds_file_free(f); return group; } diff --git a/src/osgPlugins/lib3ds/atmosphere.cpp b/src/osgPlugins/lib3ds/atmosphere.cpp index b4586bd6a..bee7d3d62 100644 --- a/src/osgPlugins/lib3ds/atmosphere.cpp +++ b/src/osgPlugins/lib3ds/atmosphere.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -24,128 +24,120 @@ #include #include + /*! * \defgroup atmosphere Atmosphere Settings * * \author J.E. Hoffmann */ + static Lib3dsBool fog_read(Lib3dsFog *fog, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_FOG, f)) - { - return(LIB3DS_FALSE); - } - fog->near_plane=lib3ds_float_read(f); - fog->near_density=lib3ds_float_read(f); - fog->far_plane=lib3ds_float_read(f); - fog->far_density=lib3ds_float_read(f); - lib3ds_chunk_read_tell(&c, f); - - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + if (!lib3ds_chunk_read_start(&c, LIB3DS_FOG, f)) { + return(LIB3DS_FALSE); + } + fog->near_plane=lib3ds_float_read(f); + fog->near_density=lib3ds_float_read(f); + fog->far_plane=lib3ds_float_read(f); + fog->far_density=lib3ds_float_read(f); + lib3ds_chunk_read_tell(&c, f); + + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_LIN_COLOR_F: { - case LIB3DS_LIN_COLOR_F: - { - int i; - for (i=0; i<3; ++i) - { - fog->col[i]=lib3ds_float_read(f); - } - } - break; - case LIB3DS_COLOR_F: - break; - case LIB3DS_FOG_BGND: - { - fog->fog_background=LIB3DS_TRUE; - } - break; - default: - lib3ds_chunk_unknown(chunk); + int i; + for (i=0; i<3; ++i) { + fog->col[i]=lib3ds_float_read(f); + } } + break; + case LIB3DS_COLOR_F: + break; + case LIB3DS_FOG_BGND: + { + fog->fog_background=LIB3DS_TRUE; + } + break; + default: + lib3ds_chunk_unknown(chunk); } - - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool layer_fog_read(Lib3dsLayerFog *fog, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; - //Lib3dsBool have_lin=LIB3DS_FALSE; + Lib3dsChunk c; + Lib3dsWord chunk; + Lib3dsBool have_lin=LIB3DS_FALSE; - if (!lib3ds_chunk_read_start(&c, LIB3DS_LAYER_FOG, f)) - { - return(LIB3DS_FALSE); + if (!lib3ds_chunk_read_start(&c, LIB3DS_LAYER_FOG, f)) { + return(LIB3DS_FALSE); + } + fog->near_y=lib3ds_float_read(f); + fog->far_y=lib3ds_float_read(f); + fog->density=lib3ds_float_read(f); + fog->flags=lib3ds_dword_read(f); + lib3ds_chunk_read_tell(&c, f); + + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_LIN_COLOR_F: + lib3ds_rgb_read(fog->col,f); + have_lin=LIB3DS_TRUE; + break; + case LIB3DS_COLOR_F: + lib3ds_rgb_read(fog->col,f); + break; + default: + lib3ds_chunk_unknown(chunk); } - fog->near_y=lib3ds_float_read(f); - fog->far_y=lib3ds_float_read(f); - fog->density=lib3ds_float_read(f); - fog->flags=lib3ds_dword_read(f); - lib3ds_chunk_read_tell(&c, f); - - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) - { - case LIB3DS_LIN_COLOR_F: - lib3ds_rgb_read(fog->col,f); - //have_lin=LIB3DS_TRUE; - break; - case LIB3DS_COLOR_F: - lib3ds_rgb_read(fog->col,f); - break; - default: - lib3ds_chunk_unknown(chunk); - } - } - - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool distance_cue_read(Lib3dsDistanceCue *cue, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_DISTANCE_CUE, f)) - { - return(LIB3DS_FALSE); - } - cue->near_plane=lib3ds_float_read(f); - cue->near_dimming=lib3ds_float_read(f); - cue->far_plane=lib3ds_float_read(f); - cue->far_dimming=lib3ds_float_read(f); - lib3ds_chunk_read_tell(&c, f); - - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + if (!lib3ds_chunk_read_start(&c, LIB3DS_DISTANCE_CUE, f)) { + return(LIB3DS_FALSE); + } + cue->near_plane=lib3ds_float_read(f); + cue->near_dimming=lib3ds_float_read(f); + cue->far_plane=lib3ds_float_read(f); + cue->far_dimming=lib3ds_float_read(f); + lib3ds_chunk_read_tell(&c, f); + + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_DCUE_BGND: { - case LIB3DS_DCUE_BGND: - { - cue->cue_background=LIB3DS_TRUE; - } - break; - default: - lib3ds_chunk_unknown(chunk); + cue->cue_background=LIB3DS_TRUE; } + break; + default: + lib3ds_chunk_unknown(chunk); } - - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } @@ -155,60 +147,55 @@ distance_cue_read(Lib3dsDistanceCue *cue, FILE *f) Lib3dsBool lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, FILE *f) { - Lib3dsChunk c; + Lib3dsChunk c; - if (!lib3ds_chunk_read(&c, f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_read(&c, f)) { + return(LIB3DS_FALSE); + } + + switch (c.chunk) { + case LIB3DS_FOG: + { + lib3ds_chunk_read_reset(&c, f); + if (!fog_read(&atmosphere->fog, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_LAYER_FOG: + { + lib3ds_chunk_read_reset(&c, f); + if (!layer_fog_read(&atmosphere->layer_fog, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_DISTANCE_CUE: + { + lib3ds_chunk_read_reset(&c, f); + if (!distance_cue_read(&atmosphere->dist_cue, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_USE_FOG: + { + atmosphere->fog.use=LIB3DS_TRUE; + } + break; + case LIB3DS_USE_LAYER_FOG: + { + atmosphere->fog.use=LIB3DS_TRUE; + } + break; + case LIB3DS_USE_DISTANCE_CUE: + { + atmosphere->dist_cue.use=LIB3DS_TRUE; + } + break; + } - switch (c.chunk) - { - case LIB3DS_FOG: - { - lib3ds_chunk_read_reset(&c, f); - if (!fog_read(&atmosphere->fog, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_LAYER_FOG: - { - lib3ds_chunk_read_reset(&c, f); - if (!layer_fog_read(&atmosphere->layer_fog, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_DISTANCE_CUE: - { - lib3ds_chunk_read_reset(&c, f); - if (!distance_cue_read(&atmosphere->dist_cue, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_USE_FOG: - { - atmosphere->fog.use=LIB3DS_TRUE; - } - break; - case LIB3DS_USE_LAYER_FOG: - { - atmosphere->fog.use=LIB3DS_TRUE; - } - break; - case LIB3DS_USE_DISTANCE_CUE: - { - atmosphere->dist_cue.use=LIB3DS_TRUE; - } - break; - } - - return(LIB3DS_TRUE); + return(LIB3DS_TRUE); } @@ -218,100 +205,95 @@ lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, FILE *f) Lib3dsBool lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, FILE *f) { - { /*---- LIB3DS_FOG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_FOG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - lib3ds_float_write(atmosphere->fog.near_plane,f); - lib3ds_float_write(atmosphere->fog.near_density,f); - lib3ds_float_write(atmosphere->fog.far_plane,f); - lib3ds_float_write(atmosphere->fog.far_density,f); - { - Lib3dsChunk c; - c.chunk=LIB3DS_FOG_BGND; - c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(atmosphere->fog.col,f); - } - if (atmosphere->fog.fog_background) - { - Lib3dsChunk c; - c.chunk=LIB3DS_COLOR_F; - c.size=6; - lib3ds_chunk_write(&c,f); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (atmosphere->fog.use) { /*---- LIB3DS_FOG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_FOG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_LAYER_FOG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_LAYER_FOG; - c.size=40; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(atmosphere->layer_fog.near_y,f); - lib3ds_float_write(atmosphere->layer_fog.far_y,f); - lib3ds_float_write(atmosphere->layer_fog.near_y,f); - lib3ds_dword_write(atmosphere->layer_fog.flags,f); - { - Lib3dsChunk c; - c.chunk=LIB3DS_COLOR_F; - c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(atmosphere->fog.col,f); - } - } - { /*---- LIB3DS_DISTANCE_CUE ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_DISTANCE_CUE; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - lib3ds_float_write(atmosphere->dist_cue.near_plane,f); - lib3ds_float_write(atmosphere->dist_cue.near_dimming,f); - lib3ds_float_write(atmosphere->dist_cue.far_plane,f); - lib3ds_float_write(atmosphere->dist_cue.far_dimming,f); - if (atmosphere->dist_cue.cue_background) - { - Lib3dsChunk c; - c.chunk=LIB3DS_DCUE_BGND; - c.size=6; - lib3ds_chunk_write(&c,f); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } - } - if (atmosphere->fog.use) /*---- LIB3DS_USE_FOG ----*/ + lib3ds_float_write(atmosphere->fog.near_plane,f); + lib3ds_float_write(atmosphere->fog.near_density,f); + lib3ds_float_write(atmosphere->fog.far_plane,f); + lib3ds_float_write(atmosphere->fog.far_density,f); { - Lib3dsChunk c; - c.chunk=LIB3DS_USE_FOG; - c.size=6; - lib3ds_chunk_write(&c,f); + Lib3dsChunk c; + c.chunk=LIB3DS_COLOR_F; + c.size=18; + lib3ds_chunk_write(&c,f); + lib3ds_rgb_write(atmosphere->fog.col,f); } - /*---- LIB3DS_USE_LAYER_FOG ----*/ - if (atmosphere->layer_fog.use) - { - Lib3dsChunk c; - c.chunk=LIB3DS_USE_LAYER_FOG; - c.size=6; - lib3ds_chunk_write(&c,f); + if (atmosphere->fog.fog_background) { + Lib3dsChunk c; + c.chunk=LIB3DS_FOG_BGND; + c.size=6; + lib3ds_chunk_write(&c,f); } - if (atmosphere->dist_cue.use)/*---- LIB3DS_USE_DISTANCE_CUE ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_USE_V_GRADIENT; - c.size=6; - lib3ds_chunk_write(&c,f); + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); } + } - return(LIB3DS_TRUE); + if (atmosphere->layer_fog.use) { /*---- LIB3DS_LAYER_FOG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_LAYER_FOG; + c.size=40; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(atmosphere->layer_fog.near_y,f); + lib3ds_float_write(atmosphere->layer_fog.far_y,f); + lib3ds_float_write(atmosphere->layer_fog.near_y,f); + lib3ds_dword_write(atmosphere->layer_fog.flags,f); + { + Lib3dsChunk c; + c.chunk=LIB3DS_COLOR_F; + c.size=18; + lib3ds_chunk_write(&c,f); + lib3ds_rgb_write(atmosphere->fog.col,f); + } + } + + if (atmosphere->dist_cue.use) { /*---- LIB3DS_DISTANCE_CUE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DISTANCE_CUE; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + lib3ds_float_write(atmosphere->dist_cue.near_plane,f); + lib3ds_float_write(atmosphere->dist_cue.near_dimming,f); + lib3ds_float_write(atmosphere->dist_cue.far_plane,f); + lib3ds_float_write(atmosphere->dist_cue.far_dimming,f); + if (atmosphere->dist_cue.cue_background) { + Lib3dsChunk c; + c.chunk=LIB3DS_DCUE_BGND; + c.size=6; + lib3ds_chunk_write(&c,f); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + + if (atmosphere->fog.use) { /*---- LIB3DS_USE_FOG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_USE_FOG; + c.size=6; + lib3ds_chunk_write(&c,f); + } + + if (atmosphere->layer_fog.use) { /*---- LIB3DS_USE_LAYER_FOG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_USE_LAYER_FOG; + c.size=6; + lib3ds_chunk_write(&c,f); + } + + if (atmosphere->dist_cue.use) { /*---- LIB3DS_USE_DISTANCE_CUE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_USE_V_GRADIENT; + c.size=6; + lib3ds_chunk_write(&c,f); + } + + return(LIB3DS_TRUE); } @@ -322,3 +304,4 @@ lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, FILE *f) \sa _Lib3dsAtmosphere */ + diff --git a/src/osgPlugins/lib3ds/background.cpp b/src/osgPlugins/lib3ds/background.cpp index 824261292..b4c6a71cc 100644 --- a/src/osgPlugins/lib3ds/background.cpp +++ b/src/osgPlugins/lib3ds/background.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -24,6 +24,8 @@ #include #include #include +#include + /*! * \defgroup background Background Settings @@ -31,84 +33,79 @@ * \author J.E. Hoffmann */ + static Lib3dsBool solid_bgnd_read(Lib3dsBackground *background, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; - //Lib3dsBool have_lin=LIB3DS_FALSE; + Lib3dsChunk c; + Lib3dsWord chunk; + Lib3dsBool have_lin=LIB3DS_FALSE; + + if (!lib3ds_chunk_read_start(&c, LIB3DS_SOLID_BGND, f)) { + return(LIB3DS_FALSE); + } - if (!lib3ds_chunk_read_start(&c, LIB3DS_SOLID_BGND, f)) - { - return(LIB3DS_FALSE); + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_LIN_COLOR_F: + lib3ds_rgb_read(background->solid.col, f); + have_lin=LIB3DS_TRUE; + break; + case LIB3DS_COLOR_F: + lib3ds_rgb_read(background->solid.col, f); + break; + default: + lib3ds_chunk_unknown(chunk); } - - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) - { - case LIB3DS_LIN_COLOR_F: - lib3ds_rgb_read(background->solid.col, f); - //have_lin=LIB3DS_TRUE; - break; - case LIB3DS_COLOR_F: - lib3ds_rgb_read(background->solid.col, f); - break; - default: - lib3ds_chunk_unknown(chunk); - } - } - - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool v_gradient_read(Lib3dsBackground *background, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; - int index[2]; - Lib3dsRgb col[2][3]; - int have_lin=0; + Lib3dsChunk c; + Lib3dsWord chunk; + int index[2]; + Lib3dsRgb col[2][3]; + int have_lin=0; + - if (!lib3ds_chunk_read_start(&c, LIB3DS_V_GRADIENT, f)) - { - return(LIB3DS_FALSE); - } - background->gradient.percent=lib3ds_float_read(f); - lib3ds_chunk_read_tell(&c, f); + if (!lib3ds_chunk_read_start(&c, LIB3DS_V_GRADIENT, f)) { + return(LIB3DS_FALSE); + } + background->gradient.percent=lib3ds_float_read(f); + lib3ds_chunk_read_tell(&c, f); - index[0]=index[1]=0; - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) - { - case LIB3DS_COLOR_F: - lib3ds_rgb_read(col[0][index[0]],f); - index[0]++; - break; - case LIB3DS_LIN_COLOR_F: - lib3ds_rgb_read(col[1][index[1]],f); - index[1]++; - have_lin=1; - break; - default: - lib3ds_chunk_unknown(chunk); - } + index[0]=index[1]=0; + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_COLOR_F: + lib3ds_rgb_read(col[0][index[0]],f); + index[0]++; + break; + case LIB3DS_LIN_COLOR_F: + lib3ds_rgb_read(col[1][index[1]],f); + index[1]++; + have_lin=1; + break; + default: + lib3ds_chunk_unknown(chunk); } - { - int i; - for (i=0; i<3; ++i) - { - background->gradient.top[i]=col[have_lin][0][i]; - background->gradient.middle[i]=col[have_lin][1][i]; - background->gradient.bottom[i]=col[have_lin][2][i]; - } + } + { + int i; + for (i=0; i<3; ++i) { + background->gradient.top[i]=col[have_lin][0][i]; + background->gradient.middle[i]=col[have_lin][1][i]; + background->gradient.bottom[i]=col[have_lin][2][i]; } - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + } + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } @@ -118,77 +115,85 @@ v_gradient_read(Lib3dsBackground *background, FILE *f) Lib3dsBool lib3ds_background_read(Lib3dsBackground *background, FILE *f) { - Lib3dsChunk c; + Lib3dsChunk c; - if (!lib3ds_chunk_read(&c, f)) - { - return(LIB3DS_FALSE); - } - - switch (c.chunk) - { - case LIB3DS_BIT_MAP: + if (!lib3ds_chunk_read(&c, f)) { + return(LIB3DS_FALSE); + } + + switch (c.chunk) { + case LIB3DS_BIT_MAP: + { + if (!lib3ds_string_read(background->bitmap.name, 64, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_SOLID_BGND: { - if (!lib3ds_string_read(background->bitmap.name, 64, f)) - { - return(LIB3DS_FALSE); - } + lib3ds_chunk_read_reset(&c, f); + if (!solid_bgnd_read(background, f)) { + return(LIB3DS_FALSE); + } } break; - case LIB3DS_SOLID_BGND: + case LIB3DS_V_GRADIENT: { - lib3ds_chunk_read_reset(&c, f); - if (!solid_bgnd_read(background, f)) - { - return(LIB3DS_FALSE); - } + lib3ds_chunk_read_reset(&c, f); + if (!v_gradient_read(background, f)) { + return(LIB3DS_FALSE); + } } break; - case LIB3DS_V_GRADIENT: + case LIB3DS_USE_BIT_MAP: { - lib3ds_chunk_read_reset(&c, f); - if (!v_gradient_read(background, f)) - { - return(LIB3DS_FALSE); - } + background->bitmap.use=LIB3DS_TRUE; } break; - case LIB3DS_USE_BIT_MAP: + case LIB3DS_USE_SOLID_BGND: { - background->bitmap.use=LIB3DS_TRUE; + background->solid.use=LIB3DS_TRUE; } break; - case LIB3DS_USE_SOLID_BGND: + case LIB3DS_USE_V_GRADIENT: { - background->solid.use=LIB3DS_TRUE; + background->gradient.use=LIB3DS_TRUE; } break; - case LIB3DS_USE_V_GRADIENT: - { - background->gradient.use=LIB3DS_TRUE; - } - break; - } - - return(LIB3DS_TRUE); + } + + return(LIB3DS_TRUE); } static Lib3dsBool colorf_write(Lib3dsRgba rgb, FILE *f) { - Lib3dsChunk c; + Lib3dsChunk c; - c.chunk=LIB3DS_COLOR_F; - c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(rgb,f); + c.chunk=LIB3DS_COLOR_F; + c.size=18; + lib3ds_chunk_write(&c,f); + lib3ds_rgb_write(rgb,f); - c.chunk=LIB3DS_LIN_COLOR_F; - c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(rgb,f); - return(LIB3DS_TRUE); + c.chunk=LIB3DS_LIN_COLOR_F; + c.size=18; + lib3ds_chunk_write(&c,f); + lib3ds_rgb_write(rgb,f); + return(LIB3DS_TRUE); +} + + +static Lib3dsBool +colorf_defined(Lib3dsRgba rgb) +{ + int i; + for (i=0; i<3; ++i) { + if (fabs(rgb[i])>LIB3DS_EPSILON) { + break; + } + } + return(i<3); } @@ -198,53 +203,57 @@ colorf_write(Lib3dsRgba rgb, FILE *f) Lib3dsBool lib3ds_background_write(Lib3dsBackground *background, FILE *f) { - { /*---- LIB3DS_BIT_MAP ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_BIT_MAP; - c.size=6+1+strlen(background->bitmap.name); - lib3ds_chunk_write(&c,f); - lib3ds_string_write(background->bitmap.name, f); - } - { /*---- LIB3DS_SOLID_BGND ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_SOLID_BGND; - c.size=42; - lib3ds_chunk_write(&c,f); - colorf_write(background->solid.col,f); - } - { /*---- LIB3DS_V_GRADIENT ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_V_GRADIENT; - c.size=118; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(background->gradient.percent,f); - colorf_write(background->gradient.top,f); - colorf_write(background->gradient.middle,f); - colorf_write(background->gradient.bottom,f); - } - if (background->bitmap.use) /*---- LIB3DS_USE_BIT_MAP ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_USE_BIT_MAP; - c.size=6; - lib3ds_chunk_write(&c,f); - } - if (background->solid.use) /*---- LIB3DS_USE_SOLID_BGND ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_USE_SOLID_BGND; - c.size=6; - lib3ds_chunk_write(&c,f); - } - if (background->gradient.use)/*---- LIB3DS_USE_V_GRADIENT ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_USE_V_GRADIENT; - c.size=6; - lib3ds_chunk_write(&c,f); - } + if (strlen(background->bitmap.name)) { /*---- LIB3DS_BIT_MAP ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_BIT_MAP; + c.size=6+1+strlen(background->bitmap.name); + lib3ds_chunk_write(&c,f); + lib3ds_string_write(background->bitmap.name, f); + } - return(LIB3DS_TRUE); + if (colorf_defined(background->solid.col)) { /*---- LIB3DS_SOLID_BGND ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_SOLID_BGND; + c.size=42; + lib3ds_chunk_write(&c,f); + colorf_write(background->solid.col,f); + } + + if (colorf_defined(background->gradient.top) || + colorf_defined(background->gradient.middle) || + colorf_defined(background->gradient.bottom)) { /*---- LIB3DS_V_GRADIENT ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_V_GRADIENT; + c.size=118; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(background->gradient.percent,f); + colorf_write(background->gradient.top,f); + colorf_write(background->gradient.middle,f); + colorf_write(background->gradient.bottom,f); + } + + if (background->bitmap.use) { /*---- LIB3DS_USE_BIT_MAP ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_USE_BIT_MAP; + c.size=6; + lib3ds_chunk_write(&c,f); + } + + if (background->solid.use) { /*---- LIB3DS_USE_SOLID_BGND ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_USE_SOLID_BGND; + c.size=6; + lib3ds_chunk_write(&c,f); + } + + if (background->gradient.use) { /*---- LIB3DS_USE_V_GRADIENT ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_USE_V_GRADIENT; + c.size=6; + lib3ds_chunk_write(&c,f); + } + + return(LIB3DS_TRUE); } diff --git a/src/osgPlugins/lib3ds/camera.cpp b/src/osgPlugins/lib3ds/camera.cpp index 93830c8cd..1aa6a86ff 100644 --- a/src/osgPlugins/lib3ds/camera.cpp +++ b/src/osgPlugins/lib3ds/camera.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -26,36 +26,48 @@ #include #include #include -//#include +#include #ifdef WITH_DMALLOC #include #endif + /*! * \defgroup camera Cameras * * \author J.E. Hoffmann */ + /*! * \ingroup camera */ Lib3dsCamera* lib3ds_camera_new(const char *name) { - Lib3dsCamera *camera; + Lib3dsCamera *camera; - ASSERT(name); - ASSERT(strlen(name)<64); + ASSERT(name); + ASSERT(strlen(name)<64); + + camera=(Lib3dsCamera*)calloc(sizeof(Lib3dsCamera), 1); + if (!camera) { + return(0); + } + strcpy(camera->name, name); + camera->fov=45.0f; + return(camera); +} - camera=(Lib3dsCamera*)calloc(sizeof(Lib3dsCamera), 1); - if (!camera) - { - return(0); - } - strcpy(camera->name, name); - camera->fov=45.0f; - return(camera); + +/*! + * \ingroup camera + */ +void +lib3ds_camera_free(Lib3dsCamera *camera) +{ + memset(camera, 0, sizeof(Lib3dsCamera)); + free(camera); } @@ -63,10 +75,20 @@ lib3ds_camera_new(const char *name) * \ingroup camera */ void -lib3ds_camera_free(Lib3dsCamera *camera) +lib3ds_camera_dump(Lib3dsCamera *camera) { - memset(camera, 0, sizeof(Lib3dsCamera)); - free(camera); + ASSERT(camera); + printf(" name: %s\n", camera->name); + printf(" position: (%f, %f, %f)\n", + camera->position[0], camera->position[1], camera->position[2]); + printf(" target (%f, %f, %f)\n", + camera->target[0], camera->target[1], camera->target[2]); + printf(" roll: %f\n", camera->roll); + printf(" fov: %f\n", camera->fov); + printf(" see_cone: %s\n", camera->see_cone ? "yes" : "no"); + printf(" near_range: %f\n", camera->near_range); + printf(" far_range: %f\n", camera->near_range); + printf("\n"); } @@ -76,61 +98,54 @@ lib3ds_camera_free(Lib3dsCamera *camera) Lib3dsBool lib3ds_camera_read(Lib3dsCamera *camera, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_N_CAMERA, f)) - { - return(LIB3DS_FALSE); + if (!lib3ds_chunk_read_start(&c, LIB3DS_N_CAMERA, f)) { + return(LIB3DS_FALSE); + } + { + int i; + for (i=0; i<3; ++i) { + camera->position[i]=lib3ds_float_read(f); } - { - int i; - for (i=0; i<3; ++i) - { - camera->position[i]=lib3ds_float_read(f); - } - for (i=0; i<3; ++i) - { - camera->target[i]=lib3ds_float_read(f); - } + for (i=0; i<3; ++i) { + camera->target[i]=lib3ds_float_read(f); } - camera->roll=lib3ds_float_read(f); - { - float s; - s=lib3ds_float_read(f); - if (fabs(s)fov=45.0; - } - else - { - camera->fov=2400.0f/s; - } + } + camera->roll=lib3ds_float_read(f); + { + float s; + s=lib3ds_float_read(f); + if (fabs(s)fov=45.0; } - lib3ds_chunk_read_tell(&c, f); - - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) - { - case LIB3DS_CAM_SEE_CONE: - { - camera->see_cone=LIB3DS_TRUE; - } - break; - case LIB3DS_CAM_RANGES: - { - camera->near_range=lib3ds_float_read(f); - camera->far_range=lib3ds_float_read(f); - } - break; - default: - lib3ds_chunk_unknown(chunk); - } + else { + camera->fov=2400.0f/s; } - - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + } + lib3ds_chunk_read_tell(&c, f); + + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_CAM_SEE_CONE: + { + camera->see_cone=LIB3DS_TRUE; + } + break; + case LIB3DS_CAM_RANGES: + { + camera->near_range=lib3ds_float_read(f); + camera->far_range=lib3ds_float_read(f); + } + break; + default: + lib3ds_chunk_unknown(chunk); + } + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } @@ -140,47 +155,42 @@ lib3ds_camera_read(Lib3dsCamera *camera, FILE *f) Lib3dsBool lib3ds_camera_write(Lib3dsCamera *camera, FILE *f) { + Lib3dsChunk c; + + c.chunk=LIB3DS_N_CAMERA; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + + lib3ds_vector_write(camera->position, f); + lib3ds_vector_write(camera->target, f); + lib3ds_float_write(camera->roll, f); + if (fabs(camera->fov)fov, f); + } + + if (camera->see_cone) { Lib3dsChunk c; + c.chunk=LIB3DS_CAM_SEE_CONE; + c.size=6; + lib3ds_chunk_write(&c, f); + } + { + Lib3dsChunk c; + c.chunk=LIB3DS_CAM_RANGES; + c.size=14; + lib3ds_chunk_write(&c, f); + lib3ds_float_write(camera->near_range, f); + lib3ds_float_write(camera->far_range, f); + } - c.chunk=LIB3DS_N_CAMERA; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - - lib3ds_vector_write(camera->position, f); - lib3ds_vector_write(camera->target, f); - lib3ds_float_write(camera->roll, f); - if (fabs(camera->fov)fov, f); - } - - if (camera->see_cone) - { - Lib3dsChunk c; - c.chunk=LIB3DS_CAM_SEE_CONE; - c.size=6; - lib3ds_chunk_write(&c, f); - } - { - Lib3dsChunk c; - c.chunk=LIB3DS_CAM_RANGES; - c.size=14; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(camera->near_range, f); - lib3ds_float_write(camera->far_range, f); - } - - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } diff --git a/src/osgPlugins/lib3ds/camera.h b/src/osgPlugins/lib3ds/camera.h index d0dfcb936..c600a8ede 100644 --- a/src/osgPlugins/lib3ds/camera.h +++ b/src/osgPlugins/lib3ds/camera.h @@ -49,6 +49,7 @@ struct _Lib3dsCamera { extern LIB3DSAPI Lib3dsCamera* lib3ds_camera_new(const char *name); extern LIB3DSAPI void lib3ds_camera_free(Lib3dsCamera *mesh); +extern LIB3DSAPI void lib3ds_camera_dump(Lib3dsCamera *camera); extern LIB3DSAPI Lib3dsBool lib3ds_camera_read(Lib3dsCamera *camera, FILE *f); extern LIB3DSAPI Lib3dsBool lib3ds_camera_write(Lib3dsCamera *camera, FILE *f); diff --git a/src/osgPlugins/lib3ds/chunk.cpp b/src/osgPlugins/lib3ds/chunk.cpp index 4c398092a..b6912c334 100644 --- a/src/osgPlugins/lib3ds/chunk.cpp +++ b/src/osgPlugins/lib3ds/chunk.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -24,48 +24,50 @@ #include #include #include +#include + /*#define LIB3DS_CHUNK_DEBUG*/ /*#define LIB3DS_CHUNK_WARNING*/ + /*! * \defgroup chunk Chunk Handling * * \author J.E. Hoffmann */ + static Lib3dsBool enable_dump=LIB3DS_FALSE; static Lib3dsBool enable_unknown=LIB3DS_FALSE; static char lib3ds_chunk_level[128]=""; + static void - /*c*/ -lib3ds_chunk_debug_enter(Lib3dsChunk* ) +lib3ds_chunk_debug_enter(Lib3dsChunk *c) { - strcat(lib3ds_chunk_level, " "); + strcat(lib3ds_chunk_level, " "); } static void - /*c*/ -lib3ds_chunk_debug_leave(Lib3dsChunk* ) +lib3ds_chunk_debug_leave(Lib3dsChunk *c) { - lib3ds_chunk_level[strlen(lib3ds_chunk_level)-2]=0; + lib3ds_chunk_level[strlen(lib3ds_chunk_level)-2]=0; } static void lib3ds_chunk_debug_dump(Lib3dsChunk *c) { - if (enable_dump) - { - printf("%s%s (0x%X) size=%lu\n", - lib3ds_chunk_level, - lib3ds_chunk_name(c->chunk), - c->chunk, - c->size - ); - } + if (enable_dump) { + printf("%s%s (0x%X) size=%lu\n", + lib3ds_chunk_level, + lib3ds_chunk_name(c->chunk), + c->chunk, + c->size + ); + } } @@ -75,8 +77,8 @@ lib3ds_chunk_debug_dump(Lib3dsChunk *c) void lib3ds_chunk_enable_dump(Lib3dsBool enable, Lib3dsBool unknown) { - enable_dump=enable; - enable_unknown=unknown; + enable_dump=enable; + enable_unknown=unknown; } @@ -93,19 +95,18 @@ lib3ds_chunk_enable_dump(Lib3dsBool enable, Lib3dsBool unknown) Lib3dsBool lib3ds_chunk_read(Lib3dsChunk *c, FILE *f) { - ASSERT(c); - ASSERT(f); - c->cur=ftell(f); - c->chunk=lib3ds_word_read(f); - c->size=lib3ds_dword_read(f); - c->end=c->cur+c->size; - c->cur+=6; - if (ferror(f) || (c->size<6)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); - + ASSERT(c); + ASSERT(f); + c->cur=ftell(f); + c->chunk=lib3ds_word_read(f); + c->size=lib3ds_dword_read(f); + c->end=c->cur+c->size; + c->cur+=6; + if (ferror(f) || (c->size<6)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); + } @@ -115,14 +116,13 @@ lib3ds_chunk_read(Lib3dsChunk *c, FILE *f) Lib3dsBool lib3ds_chunk_read_start(Lib3dsChunk *c, Lib3dsWord chunk, FILE *f) { - ASSERT(c); - ASSERT(f); - if (!lib3ds_chunk_read(c, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_debug_enter(c); - return((chunk==0) || (c->chunk==chunk)); + ASSERT(c); + ASSERT(f); + if (!lib3ds_chunk_read(c, f)) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_debug_enter(c); + return((chunk==0) || (c->chunk==chunk)); } @@ -132,7 +132,7 @@ lib3ds_chunk_read_start(Lib3dsChunk *c, Lib3dsWord chunk, FILE *f) void lib3ds_chunk_read_tell(Lib3dsChunk *c, FILE *f) { - c->cur=ftell(f); + c->cur=ftell(f); } @@ -142,20 +142,19 @@ lib3ds_chunk_read_tell(Lib3dsChunk *c, FILE *f) Lib3dsWord lib3ds_chunk_read_next(Lib3dsChunk *c, FILE *f) { - Lib3dsChunk d; + Lib3dsChunk d; - if (c->cur>=c->end) - { - ASSERT(c->cur==c->end); - return(0); - } + if (c->cur>=c->end) { + ASSERT(c->cur==c->end); + return(0); + } - fseek(f, (long)c->cur, SEEK_SET); - d.chunk=lib3ds_word_read(f); - d.size=lib3ds_dword_read(f); - lib3ds_chunk_debug_dump(&d); - c->cur+=d.size; - return(d.chunk); + fseek(f, (long)c->cur, SEEK_SET); + d.chunk=lib3ds_word_read(f); + d.size=lib3ds_dword_read(f); + lib3ds_chunk_debug_dump(&d); + c->cur+=d.size; + return(d.chunk); } @@ -163,10 +162,9 @@ lib3ds_chunk_read_next(Lib3dsChunk *c, FILE *f) * \ingroup chunk */ void - /*c*/ -lib3ds_chunk_read_reset(Lib3dsChunk* , FILE *f) +lib3ds_chunk_read_reset(Lib3dsChunk *c, FILE *f) { - fseek(f, -6, SEEK_CUR); + fseek(f, -6, SEEK_CUR); } @@ -176,8 +174,8 @@ lib3ds_chunk_read_reset(Lib3dsChunk* , FILE *f) void lib3ds_chunk_read_end(Lib3dsChunk *c, FILE *f) { - lib3ds_chunk_debug_leave(c); - fseek(f, c->end, SEEK_SET); + lib3ds_chunk_debug_leave(c); + fseek(f, c->end, SEEK_SET); } @@ -194,18 +192,16 @@ lib3ds_chunk_read_end(Lib3dsChunk *c, FILE *f) Lib3dsBool lib3ds_chunk_write(Lib3dsChunk *c, FILE *f) { - ASSERT(c); - if (!lib3ds_word_write(c->chunk, f)) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - if (!lib3ds_dword_write(c->size, f)) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + ASSERT(c); + if (!lib3ds_word_write(c->chunk, f)) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + if (!lib3ds_dword_write(c->size, f)) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -215,18 +211,16 @@ lib3ds_chunk_write(Lib3dsChunk *c, FILE *f) Lib3dsBool lib3ds_chunk_write_start(Lib3dsChunk *c, FILE *f) { - ASSERT(c); - c->size=0; - c->cur=ftell(f); - if (!lib3ds_word_write(c->chunk, f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_dword_write(c->size, f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + ASSERT(c); + c->size=0; + c->cur=ftell(f); + if (!lib3ds_word_write(c->chunk, f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_dword_write(c->size, f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -236,23 +230,21 @@ lib3ds_chunk_write_start(Lib3dsChunk *c, FILE *f) Lib3dsBool lib3ds_chunk_write_end(Lib3dsChunk *c, FILE *f) { - ASSERT(c); - c->size=ftell(f) - c->cur; - fseek(f, c->cur+2, SEEK_SET); - if (!lib3ds_dword_write(c->size, f)) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } + ASSERT(c); + c->size=ftell(f) - c->cur; + fseek(f, c->cur+2, SEEK_SET); + if (!lib3ds_dword_write(c->size, f)) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } - c->cur+=c->size; - fseek(f, c->cur, SEEK_SET); - if (ferror(f)) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + c->cur+=c->size; + fseek(f, c->cur, SEEK_SET); + if (ferror(f)) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -262,16 +254,14 @@ lib3ds_chunk_write_end(Lib3dsChunk *c, FILE *f) const char* lib3ds_chunk_name(Lib3dsWord chunk) { - Lib3dsChunkTable *p; + Lib3dsChunkTable *p; - for (p=lib3ds_chunk_table; p->name!=0; ++p) - { - if (p->chunk==chunk) - { - return(p->name); - } + for (p=lib3ds_chunk_table; p->name!=0; ++p) { + if (p->chunk==chunk) { + return(p->name); } - return("***UNKNOWN***"); + } + return("***UNKNOWN***"); } @@ -281,12 +271,37 @@ lib3ds_chunk_name(Lib3dsWord chunk) void lib3ds_chunk_unknown(Lib3dsWord chunk) { - if (enable_unknown) - { - printf("%s***WARNING*** Unknown Chunk: %s (0x%X)\n", - lib3ds_chunk_level, - lib3ds_chunk_name(chunk), - chunk - ); - } + if (enable_unknown) { + printf("%s***WARNING*** Unknown Chunk: %s (0x%X)\n", + lib3ds_chunk_level, + lib3ds_chunk_name(chunk), + chunk + ); + } } + + +/*! + * \ingroup chunk + */ +void +lib3ds_chunk_dump_info(const char *format, ...) +{ + if (enable_dump) { + char s[1024]; + va_list marker; + + va_start(marker, format); + vsprintf(s, format, marker); + va_end(marker); + + printf("%s%s\n", lib3ds_chunk_level, s); + } +} + + + + + + + diff --git a/src/osgPlugins/lib3ds/chunk.h b/src/osgPlugins/lib3ds/chunk.h index dfd930a67..37b69094e 100644 --- a/src/osgPlugins/lib3ds/chunk.h +++ b/src/osgPlugins/lib3ds/chunk.h @@ -278,6 +278,7 @@ extern LIB3DSAPI Lib3dsBool lib3ds_chunk_write_start(Lib3dsChunk *c, FILE *f); extern LIB3DSAPI Lib3dsBool lib3ds_chunk_write_end(Lib3dsChunk *c, FILE *f); extern LIB3DSAPI const char* lib3ds_chunk_name(Lib3dsWord chunk); extern LIB3DSAPI void lib3ds_chunk_unknown(Lib3dsWord chunk); +extern LIB3DSAPI void lib3ds_chunk_dump_info(const char *format, ...); #ifdef __cplusplus }; diff --git a/src/osgPlugins/lib3ds/ease.cpp b/src/osgPlugins/lib3ds/ease.cpp index a0e658473..8e23902c4 100644 --- a/src/osgPlugins/lib3ds/ease.cpp +++ b/src/osgPlugins/lib3ds/ease.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -22,47 +22,44 @@ #define LIB3DS_EXPORT #include + /*! * \defgroup ease Ease * * \author J.E. Hoffmann */ + /*! * \ingroup ease */ Lib3dsFloat lib3ds_ease(Lib3dsFloat fp, Lib3dsFloat fc, Lib3dsFloat fn, -Lib3dsFloat ease_from, Lib3dsFloat ease_to) + Lib3dsFloat ease_from, Lib3dsFloat ease_to) { - Lib3dsDouble s,step; - Lib3dsDouble tofrom; - Lib3dsDouble a; + Lib3dsDouble s,step; + Lib3dsDouble tofrom; + Lib3dsDouble a; - s=step=(Lib3dsFloat)(fc-fp)/(fn-fp); - tofrom=ease_to+ease_from; - if (tofrom!=0.0) - { - if (tofrom>1.0) - { - ease_to=(Lib3dsFloat)(ease_to/tofrom); - ease_from=(Lib3dsFloat)(ease_from/tofrom); - } - a=1.0/(2.0-(ease_to+ease_from)); - - if (step1.0) { + ease_to=(Lib3dsFloat)(ease_to/tofrom); + ease_from=(Lib3dsFloat)(ease_from/tofrom); } - return((Lib3dsFloat)s); + a=1.0/(2.0-(ease_to+ease_from)); + + if (step #include #include +#include #include #include -//#include +#include +#include #ifdef WITH_DMALLOC #include #endif + /*! * \defgroup file Files * * \author J.E. Hoffmann */ + /*! + * Loads a .3DS file from disk into memory. + * + * \param filename The filename of the .3DS file + * + * \return A pointer to the Lib3dsFile structure containing the + * data of the .3DS file. + * If the .3DS file can not be loaded NULL is returned. + * + * \note To free the returned pointer use lib3ds_free. + * + * \see lib3ds_file_save + * \see lib3ds_file_new + * \see lib3ds_file_free + * * \ingroup file */ Lib3dsFile* -lib3ds_open(const char *filename) +lib3ds_file_load(const char *filename) { - FILE *f; - Lib3dsFile *file; + FILE *f; + Lib3dsFile *file; - f=fopen(filename, "rb"); - if (!f) - { - return(0); - } - file=lib3ds_file_new(); - if (!file) - { - fclose(f); - return(0); - } - - if (!lib3ds_file_read(file, f)) - { - free(file); - fclose(f); - return(0); - } + f=fopen(filename, "rb"); + if (!f) { + return(0); + } + file=lib3ds_file_new(); + if (!file) { fclose(f); - return(file); + return(0); + } + + if (!lib3ds_file_read(file, f)) { + free(file); + fclose(f); + return(0); + } + fclose(f); + return(file); } /*! + * Saves a .3DS file from memory to disk. + * + * \param file A pointer to a Lib3dsFile structure containing the + * the data that should be stored. + * \param filename The filename of the .3DS file to store the data in. + * + * \return TRUE on success, FALSE otherwise. + * + * \see lib3ds_file_load + * * \ingroup file */ -void -lib3ds_close(Lib3dsFile *file) +Lib3dsBool +lib3ds_file_save(Lib3dsFile *file, const char *filename) { - lib3ds_file_free(file); + FILE *f; + + f=fopen(filename, "wb"); + if (!f) { + return(LIB3DS_FALSE); + } + + if (!lib3ds_file_write(file, f)) { + fclose(f); + return(LIB3DS_FALSE); + } + fclose(f); + return(LIB3DS_TRUE); } @@ -89,17 +126,17 @@ lib3ds_close(Lib3dsFile *file) Lib3dsFile* lib3ds_file_new() { - Lib3dsFile *file; + Lib3dsFile *file; - file=(Lib3dsFile*)calloc(sizeof(Lib3dsFile),1); - if (!file) - { - return(0); - } - file->mesh_version=3; - file->master_scale=1.0f; - file->keyf_revision=5; - return(file); + file=(Lib3dsFile*)calloc(sizeof(Lib3dsFile),1); + if (!file) { + return(0); + } + file->mesh_version=3; + file->master_scale=1.0f; + file->keyf_revision=5; + strcpy(file->name, "LIB3DS"); + return(file); } @@ -109,58 +146,53 @@ lib3ds_file_new() void lib3ds_file_free(Lib3dsFile* file) { - ASSERT(file); - lib3ds_viewport_set_views(&file->viewport,0); - { - Lib3dsMaterial *p,*q; - - for (p=file->materials; p; p=q) - { - q=p->next; - lib3ds_material_free(p); - } - file->materials=0; + ASSERT(file); + lib3ds_viewport_set_views(&file->viewport,0); + { + Lib3dsMaterial *p,*q; + + for (p=file->materials; p; p=q) { + q=p->next; + lib3ds_material_free(p); } - { - Lib3dsCamera *p,*q; - - for (p=file->cameras; p; p=q) - { - q=p->next; - lib3ds_camera_free(p); - } - file->cameras=0; + file->materials=0; + } + { + Lib3dsCamera *p,*q; + + for (p=file->cameras; p; p=q) { + q=p->next; + lib3ds_camera_free(p); } - { - Lib3dsLight *p,*q; - - for (p=file->lights; p; p=q) - { - q=p->next; - lib3ds_light_free(p); - } - file->lights=0; + file->cameras=0; + } + { + Lib3dsLight *p,*q; + + for (p=file->lights; p; p=q) { + q=p->next; + lib3ds_light_free(p); } - { - Lib3dsMesh *p,*q; - - for (p=file->meshes; p; p=q) - { - q=p->next; - lib3ds_mesh_free(p); - } - file->meshes=0; + file->lights=0; + } + { + Lib3dsMesh *p,*q; + + for (p=file->meshes; p; p=q) { + q=p->next; + lib3ds_mesh_free(p); } - { - Lib3dsNode *p,*q; - - for (p=file->nodes; p; p=q) - { - q=p->next; - lib3ds_node_free(p); - } + file->meshes=0; + } + { + Lib3dsNode *p,*q; + + for (p=file->nodes; p; p=q) { + q=p->next; + lib3ds_node_free(p); } - free(file); + } + free(file); } @@ -170,436 +202,390 @@ lib3ds_file_free(Lib3dsFile* file) void lib3ds_file_eval(Lib3dsFile *file, Lib3dsFloat t) { - Lib3dsNode *p; + Lib3dsNode *p; - for (p=file->nodes; p!=0; p=p->next) - { - lib3ds_node_eval(p, t); - } + for (p=file->nodes; p!=0; p=p->next) { + lib3ds_node_eval(p, t); + } } static Lib3dsBool named_object_read(Lib3dsFile *file, FILE *f) { - Lib3dsChunk c; - char name[64]; - Lib3dsWord chunk; + Lib3dsChunk c; + char name[64]; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_NAMED_OBJECT, f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_string_read(name, 64, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_tell(&c, f); + if (!lib3ds_chunk_read_start(&c, LIB3DS_NAMED_OBJECT, f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_string_read(name, 64, f)) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_dump_info(" NAME=%s", name); + lib3ds_chunk_read_tell(&c, f); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_N_TRI_OBJECT: { - case LIB3DS_N_TRI_OBJECT: - { - Lib3dsMesh *mesh; + Lib3dsMesh *mesh; - mesh=lib3ds_mesh_new(name); - if (!mesh) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_mesh_read(mesh, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_file_insert_mesh(file, mesh); - } - break; - case LIB3DS_N_CAMERA: - { - Lib3dsCamera *camera; - - camera=lib3ds_camera_new(name); - if (!camera) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_camera_read(camera, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_file_insert_camera(file, camera); - } - break; - case LIB3DS_N_DIRECT_LIGHT: - { - Lib3dsLight *light; - - light=lib3ds_light_new(name); - if (!light) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_light_read(light, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_file_insert_light(file, light); - } - break; - default: - lib3ds_chunk_unknown(chunk); + mesh=lib3ds_mesh_new(name); + if (!mesh) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_mesh_read(mesh, f)) { + return(LIB3DS_FALSE); + } + lib3ds_file_insert_mesh(file, mesh); } - } + break; + case LIB3DS_N_CAMERA: + { + Lib3dsCamera *camera; - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + camera=lib3ds_camera_new(name); + if (!camera) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_camera_read(camera, f)) { + return(LIB3DS_FALSE); + } + lib3ds_file_insert_camera(file, camera); + } + break; + case LIB3DS_N_DIRECT_LIGHT: + { + Lib3dsLight *light; + + light=lib3ds_light_new(name); + if (!light) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_light_read(light, f)) { + return(LIB3DS_FALSE); + } + lib3ds_file_insert_light(file, light); + } + break; + default: + lib3ds_chunk_unknown(chunk); + } + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool ambient_read(Lib3dsFile *file, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; - Lib3dsBool have_lin=LIB3DS_FALSE; + Lib3dsChunk c; + Lib3dsWord chunk; + Lib3dsBool have_lin=LIB3DS_FALSE; - if (!lib3ds_chunk_read_start(&c, LIB3DS_AMBIENT_LIGHT, f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_read_start(&c, LIB3DS_AMBIENT_LIGHT, f)) { + return(LIB3DS_FALSE); + } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_LIN_COLOR_F: { - case LIB3DS_LIN_COLOR_F: - { - int i; - for (i=0; i<3; ++i) - { - file->ambient[i]=lib3ds_float_read(f); - } - } - have_lin=LIB3DS_TRUE; - break; - case LIB3DS_COLOR_F: - { - /* gamma corrected color chunk - replaced in 3ds R3 by LIN_COLOR_24 */ - if (!have_lin) - { - int i; - for (i=0; i<3; ++i) - { - file->ambient[i]=lib3ds_float_read(f); - } - } - } - break; - default: - lib3ds_chunk_unknown(chunk); + int i; + for (i=0; i<3; ++i) { + file->ambient[i]=lib3ds_float_read(f); + } } + have_lin=LIB3DS_TRUE; + break; + case LIB3DS_COLOR_F: + { + /* gamma corrected color chunk + replaced in 3ds R3 by LIN_COLOR_24 */ + if (!have_lin) { + int i; + for (i=0; i<3; ++i) { + file->ambient[i]=lib3ds_float_read(f); + } + } + } + break; + default: + lib3ds_chunk_unknown(chunk); } - - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool mdata_read(Lib3dsFile *file, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_MDATA, f)) - { - return(LIB3DS_FALSE); - } - - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + if (!lib3ds_chunk_read_start(&c, LIB3DS_MDATA, f)) { + return(LIB3DS_FALSE); + } + + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_MESH_VERSION: { - case LIB3DS_MESH_VERSION: - { - file->mesh_version=lib3ds_intd_read(f); - } - break; - case LIB3DS_MASTER_SCALE: - { - file->master_scale=lib3ds_float_read(f); - } - break; - case LIB3DS_SHADOW_MAP_SIZE: - case LIB3DS_LO_SHADOW_BIAS: - case LIB3DS_HI_SHADOW_BIAS: - case LIB3DS_SHADOW_SAMPLES: - case LIB3DS_SHADOW_RANGE: - case LIB3DS_SHADOW_FILTER: - case LIB3DS_RAY_BIAS: - { - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_shadow_read(&file->shadow, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_VIEWPORT_LAYOUT: - case LIB3DS_DEFAULT_VIEW: - { - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_viewport_read(&file->viewport, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_O_CONSTS: - { - int i; - for (i=0; i<3; ++i) - { - file->construction_plane[i]=lib3ds_float_read(f); - } - } - break; - case LIB3DS_AMBIENT_LIGHT: - { - lib3ds_chunk_read_reset(&c, f); - if (!ambient_read(file, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_BIT_MAP: - case LIB3DS_SOLID_BGND: - case LIB3DS_V_GRADIENT: - case LIB3DS_USE_BIT_MAP: - case LIB3DS_USE_SOLID_BGND: - case LIB3DS_USE_V_GRADIENT: - { - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_background_read(&file->background, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_FOG: - case LIB3DS_LAYER_FOG: - case LIB3DS_DISTANCE_CUE: - case LIB3DS_USE_FOG: - case LIB3DS_USE_LAYER_FOG: - case LIB3DS_USE_DISTANCE_CUE: - { - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_atmosphere_read(&file->atmosphere, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_ENTRY: - { - Lib3dsMaterial *material; - - material=lib3ds_material_new(); - if (!material) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_material_read(material, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_file_insert_material(file, material); - } - break; - case LIB3DS_NAMED_OBJECT: - { - lib3ds_chunk_read_reset(&c, f); - if (!named_object_read(file, f)) - { - return(LIB3DS_FALSE); - } - } - break; - default: - lib3ds_chunk_unknown(chunk); + file->mesh_version=lib3ds_intd_read(f); } - } + break; + case LIB3DS_MASTER_SCALE: + { + file->master_scale=lib3ds_float_read(f); + } + break; + case LIB3DS_SHADOW_MAP_SIZE: + case LIB3DS_LO_SHADOW_BIAS: + case LIB3DS_HI_SHADOW_BIAS: + case LIB3DS_SHADOW_SAMPLES: + case LIB3DS_SHADOW_RANGE: + case LIB3DS_SHADOW_FILTER: + case LIB3DS_RAY_BIAS: + { + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_shadow_read(&file->shadow, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_VIEWPORT_LAYOUT: + case LIB3DS_DEFAULT_VIEW: + { + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_viewport_read(&file->viewport, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_O_CONSTS: + { + int i; + for (i=0; i<3; ++i) { + file->construction_plane[i]=lib3ds_float_read(f); + } + } + break; + case LIB3DS_AMBIENT_LIGHT: + { + lib3ds_chunk_read_reset(&c, f); + if (!ambient_read(file, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_BIT_MAP: + case LIB3DS_SOLID_BGND: + case LIB3DS_V_GRADIENT: + case LIB3DS_USE_BIT_MAP: + case LIB3DS_USE_SOLID_BGND: + case LIB3DS_USE_V_GRADIENT: + { + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_background_read(&file->background, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_FOG: + case LIB3DS_LAYER_FOG: + case LIB3DS_DISTANCE_CUE: + case LIB3DS_USE_FOG: + case LIB3DS_USE_LAYER_FOG: + case LIB3DS_USE_DISTANCE_CUE: + { + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_atmosphere_read(&file->atmosphere, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_ENTRY: + { + Lib3dsMaterial *material; - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + material=lib3ds_material_new(); + if (!material) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_material_read(material, f)) { + return(LIB3DS_FALSE); + } + lib3ds_file_insert_material(file, material); + } + break; + case LIB3DS_NAMED_OBJECT: + { + lib3ds_chunk_read_reset(&c, f); + if (!named_object_read(file, f)) { + return(LIB3DS_FALSE); + } + } + break; + default: + lib3ds_chunk_unknown(chunk); + } + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool kfdata_read(Lib3dsFile *file, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_KFDATA, f)) - { - return(LIB3DS_FALSE); - } - - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + if (!lib3ds_chunk_read_start(&c, LIB3DS_KFDATA, f)) { + return(LIB3DS_FALSE); + } + + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_KFHDR: { - case LIB3DS_KFHDR: - { - char s[64]; - file->keyf_revision=lib3ds_word_read(f); - if (!lib3ds_string_read(s, 64, f)) - { - return(LIB3DS_FALSE); - } - file->frames=lib3ds_intd_read(f); - } - break; - case LIB3DS_KFSEG: - { - file->segment_from=lib3ds_intd_read(f); - file->segment_to=lib3ds_intd_read(f); - } - break; - case LIB3DS_KFCURTIME: - { - file->current_frame=lib3ds_intd_read(f); - } - break; - case LIB3DS_VIEWPORT_LAYOUT: - case LIB3DS_DEFAULT_VIEW: - { - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_viewport_read(&file->viewport_keyf, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_AMBIENT_NODE_TAG: - { - Lib3dsNode *node; - - node=lib3ds_node_new_ambient(); - if (!node) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_file_insert_node(file, node); - } - break; - case LIB3DS_OBJECT_NODE_TAG: - { - Lib3dsNode *node; - - node=lib3ds_node_new_object(); - if (!node) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_file_insert_node(file, node); - } - break; - case LIB3DS_CAMERA_NODE_TAG: - { - Lib3dsNode *node; - - node=lib3ds_node_new_camera(); - if (!node) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_file_insert_node(file, node); - } - break; - case LIB3DS_TARGET_NODE_TAG: - { - Lib3dsNode *node; - - node=lib3ds_node_new_target(); - if (!node) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_file_insert_node(file, node); - } - break; - case LIB3DS_LIGHT_NODE_TAG: - case LIB3DS_SPOTLIGHT_NODE_TAG: - { - Lib3dsNode *node; - - node=lib3ds_node_new_light(); - if (!node) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_file_insert_node(file, node); - } - break; - case LIB3DS_L_TARGET_NODE_TAG: - { - Lib3dsNode *node; - - node=lib3ds_node_new_spot(); - if (!node) - { - return(LIB3DS_FALSE); - } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_file_insert_node(file, node); - } - break; - default: - lib3ds_chunk_unknown(chunk); + file->keyf_revision=lib3ds_word_read(f); + if (!lib3ds_string_read(file->name, 12+1, f)) { + return(LIB3DS_FALSE); + } + file->frames=lib3ds_intd_read(f); } - } + break; + case LIB3DS_KFSEG: + { + file->segment_from=lib3ds_intd_read(f); + file->segment_to=lib3ds_intd_read(f); + } + break; + case LIB3DS_KFCURTIME: + { + file->current_frame=lib3ds_intd_read(f); + } + break; + case LIB3DS_VIEWPORT_LAYOUT: + case LIB3DS_DEFAULT_VIEW: + { + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_viewport_read(&file->viewport_keyf, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_AMBIENT_NODE_TAG: + { + Lib3dsNode *node; - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + node=lib3ds_node_new_ambient(); + if (!node) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_node_read(node, file, f)) { + return(LIB3DS_FALSE); + } + lib3ds_file_insert_node(file, node); + } + break; + case LIB3DS_OBJECT_NODE_TAG: + { + Lib3dsNode *node; + + node=lib3ds_node_new_object(); + if (!node) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_node_read(node, file, f)) { + return(LIB3DS_FALSE); + } + lib3ds_file_insert_node(file, node); + } + break; + case LIB3DS_CAMERA_NODE_TAG: + { + Lib3dsNode *node; + + node=lib3ds_node_new_camera(); + if (!node) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_node_read(node, file, f)) { + return(LIB3DS_FALSE); + } + lib3ds_file_insert_node(file, node); + } + break; + case LIB3DS_TARGET_NODE_TAG: + { + Lib3dsNode *node; + + node=lib3ds_node_new_target(); + if (!node) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_node_read(node, file, f)) { + return(LIB3DS_FALSE); + } + lib3ds_file_insert_node(file, node); + } + break; + case LIB3DS_LIGHT_NODE_TAG: + case LIB3DS_SPOTLIGHT_NODE_TAG: + { + Lib3dsNode *node; + + node=lib3ds_node_new_light(); + if (!node) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_node_read(node, file, f)) { + return(LIB3DS_FALSE); + } + lib3ds_file_insert_node(file, node); + } + break; + case LIB3DS_L_TARGET_NODE_TAG: + { + Lib3dsNode *node; + + node=lib3ds_node_new_spot(); + if (!node) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_read_reset(&c, f); + if (!lib3ds_node_read(node, file, f)) { + return(LIB3DS_FALSE); + } + lib3ds_file_insert_node(file, node); + } + break; + default: + lib3ds_chunk_unknown(chunk); + } + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } @@ -609,283 +595,274 @@ kfdata_read(Lib3dsFile *file, FILE *f) Lib3dsBool lib3ds_file_read(Lib3dsFile *file, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, 0, f)) - { - return(LIB3DS_FALSE); - } - switch (c.chunk) - { - case LIB3DS_MDATA: - { - lib3ds_chunk_read_reset(&c, f); - if (!mdata_read(file, f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_read_start(&c, 0, f)) { + return(LIB3DS_FALSE); + } + switch (c.chunk) { + case LIB3DS_MDATA: + { + lib3ds_chunk_read_reset(&c, f); + if (!mdata_read(file, f)) { + return(LIB3DS_FALSE); } - break; - case LIB3DS_M3DMAGIC: - case LIB3DS_MLIBMAGIC: - case LIB3DS_CMAGIC: - { - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) - { - case LIB3DS_M3D_VERSION: - { - file->mesh_version=lib3ds_dword_read(f); - } - break; - case LIB3DS_MDATA: - { - lib3ds_chunk_read_reset(&c, f); - if (!mdata_read(file, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_KFDATA: - { - lib3ds_chunk_read_reset(&c, f); - if (!kfdata_read(file, f)) - { - return(LIB3DS_FALSE); - } - } - break; - default: - lib3ds_chunk_unknown(chunk); + } + break; + case LIB3DS_M3DMAGIC: + case LIB3DS_MLIBMAGIC: + case LIB3DS_CMAGIC: + { + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_M3D_VERSION: + { + file->mesh_version=lib3ds_dword_read(f); + } + break; + case LIB3DS_MDATA: + { + lib3ds_chunk_read_reset(&c, f); + if (!mdata_read(file, f)) { + return(LIB3DS_FALSE); } - } + } + break; + case LIB3DS_KFDATA: + { + lib3ds_chunk_read_reset(&c, f); + if (!kfdata_read(file, f)) { + return(LIB3DS_FALSE); + } + } + break; + default: + lib3ds_chunk_unknown(chunk); + } } - break; - default: - lib3ds_chunk_unknown(c.chunk); - return(LIB3DS_FALSE); - } + } + break; + default: + lib3ds_chunk_unknown(c.chunk); + return(LIB3DS_FALSE); + } - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool colorf_write(Lib3dsRgba rgb, FILE *f) { - Lib3dsChunk c; + Lib3dsChunk c; - c.chunk=LIB3DS_COLOR_F; - c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(rgb,f); + c.chunk=LIB3DS_COLOR_F; + c.size=18; + lib3ds_chunk_write(&c,f); + lib3ds_rgb_write(rgb,f); - c.chunk=LIB3DS_LIN_COLOR_F; - c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(rgb,f); - return(LIB3DS_TRUE); + c.chunk=LIB3DS_LIN_COLOR_F; + c.size=18; + lib3ds_chunk_write(&c,f); + lib3ds_rgb_write(rgb,f); + return(LIB3DS_TRUE); } static Lib3dsBool mdata_write(Lib3dsFile *file, FILE *f) { + Lib3dsChunk c; + + c.chunk=LIB3DS_MDATA; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + + { /*---- LIB3DS_MESH_VERSION ----*/ Lib3dsChunk c; - - c.chunk=LIB3DS_MDATA; - if (!lib3ds_chunk_write_start(&c,f)) - { + c.chunk=LIB3DS_MESH_VERSION; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_intd_write(file->mesh_version,f); + } + { /*---- LIB3DS_MASTER_SCALE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MASTER_SCALE; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(file->master_scale,f); + } + { /*---- LIB3DS_O_CONSTS ----*/ + int i; + for (i=0; i<3; ++i) { + if (fabs(file->construction_plane[i])>LIB3DS_EPSILON) { + break; + } + } + if (i<3) { + Lib3dsChunk c; + c.chunk=LIB3DS_O_CONSTS; + c.size=18; + lib3ds_chunk_write(&c,f); + lib3ds_vector_write(file->construction_plane,f); + } + } + + { /*---- LIB3DS_AMBIENT_LIGHT ----*/ + int i; + for (i=0; i<3; ++i) { + if (fabs(file->ambient[i])>LIB3DS_EPSILON) { + break; + } + } + if (i<3) { + Lib3dsChunk c; + c.chunk=LIB3DS_AMBIENT_LIGHT; + c.size=42; + lib3ds_chunk_write(&c,f); + colorf_write(file->ambient,f); + } + } + lib3ds_background_write(&file->background, f); + lib3ds_atmosphere_write(&file->atmosphere, f); + lib3ds_shadow_write(&file->shadow, f); + lib3ds_viewport_write(&file->viewport, f); + { + Lib3dsMaterial *p; + for (p=file->materials; p!=0; p=p->next) { + if (!lib3ds_material_write(p,f)) { return(LIB3DS_FALSE); + } } - - { /*---- LIB3DS_MESH_VERSION ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MESH_VERSION; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_intd_write(file->mesh_version,f); - } - { /*---- LIB3DS_MASTER_SCALE ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MASTER_SCALE; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(file->master_scale,f); - } - { /*---- LIB3DS_O_CONSTS ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_O_CONSTS; - c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(file->construction_plane,f); - } - - { /*---- LIB3DS_AMBIENT_LIGHT ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_AMBIENT_LIGHT; - c.size=42; - lib3ds_chunk_write(&c,f); - colorf_write(file->ambient,f); - } - lib3ds_background_write(&file->background, f); - lib3ds_atmosphere_write(&file->atmosphere, f); - lib3ds_shadow_write(&file->shadow, f); - lib3ds_viewport_write(&file->viewport, f); - { - Lib3dsMaterial *p; - for (p=file->materials; p!=0; p=p->next) - { - if (!lib3ds_material_write(p,f)) - { - return(LIB3DS_FALSE); - } - } - } - { - Lib3dsCamera *p; - Lib3dsChunk c; - - for (p=file->cameras; p!=0; p=p->next) - { - c.chunk=LIB3DS_NAMED_OBJECT; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - lib3ds_string_write(p->name,f); - lib3ds_camera_write(p,f); - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } - } - } - { - Lib3dsLight *p; - Lib3dsChunk c; - - for (p=file->lights; p!=0; p=p->next) - { - c.chunk=LIB3DS_NAMED_OBJECT; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - lib3ds_string_write(p->name,f); - lib3ds_light_write(p,f); - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } - } - } - { - Lib3dsMesh *p; - Lib3dsChunk c; - - for (p=file->meshes; p!=0; p=p->next) - { - c.chunk=LIB3DS_NAMED_OBJECT; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - lib3ds_string_write(p->name,f); - lib3ds_mesh_write(p,f); - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } - } - } - - if (!lib3ds_chunk_write_end(&c,f)) - { + } + { + Lib3dsCamera *p; + Lib3dsChunk c; + + for (p=file->cameras; p!=0; p=p->next) { + c.chunk=LIB3DS_NAMED_OBJECT; + if (!lib3ds_chunk_write_start(&c,f)) { return(LIB3DS_FALSE); + } + lib3ds_string_write(p->name,f); + lib3ds_camera_write(p,f); + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } } - return(LIB3DS_TRUE); + } + { + Lib3dsLight *p; + Lib3dsChunk c; + + for (p=file->lights; p!=0; p=p->next) { + c.chunk=LIB3DS_NAMED_OBJECT; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + lib3ds_string_write(p->name,f); + lib3ds_light_write(p,f); + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + } + { + Lib3dsMesh *p; + Lib3dsChunk c; + + for (p=file->meshes; p!=0; p=p->next) { + c.chunk=LIB3DS_NAMED_OBJECT; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + lib3ds_string_write(p->name,f); + lib3ds_mesh_write(p,f); + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + } + + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } + static Lib3dsBool nodes_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) { - { - Lib3dsNode *p; - for (p=node->childs; p!=0; p=p->next) - { - if (!lib3ds_node_write(p, file, f)) - { - return(LIB3DS_FALSE); - } - nodes_write(p, file, f); - } + { + Lib3dsNode *p; + for (p=node->childs; p!=0; p=p->next) { + if (!lib3ds_node_write(p, file, f)) { + return(LIB3DS_FALSE); + } + nodes_write(p, file, f); } - return(LIB3DS_TRUE); + } + return(LIB3DS_TRUE); } static Lib3dsBool kfdata_write(Lib3dsFile *file, FILE *f) { + Lib3dsChunk c; + + c.chunk=LIB3DS_KFDATA; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + + { /*---- LIB3DS_KFHDR ----*/ Lib3dsChunk c; - - c.chunk=LIB3DS_KFDATA; - if (!lib3ds_chunk_write_start(&c,f)) - { + c.chunk=LIB3DS_KFHDR; + c.size=6 + 2 + strlen(file->name)+1 +4; + lib3ds_chunk_write(&c,f); + lib3ds_intw_write(file->keyf_revision,f); + lib3ds_string_write(file->name, f); + lib3ds_intd_write(file->frames, f); + } + { /*---- LIB3DS_KFSEG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_KFSEG; + c.size=14; + lib3ds_chunk_write(&c,f); + lib3ds_intd_write(file->segment_from,f); + lib3ds_intd_write(file->segment_to,f); + } + { /*---- LIB3DS_KFCURTIME ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_KFCURTIME; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_intd_write(file->current_frame,f); + } + lib3ds_viewport_write(&file->viewport_keyf, f); + + { + Lib3dsNode *p; + for (p=file->nodes; p!=0; p=p->next) { + if (!lib3ds_node_write(p, file, f)) { return(LIB3DS_FALSE); - } - - { /*---- LIB3DS_KFHDR ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_KFHDR; - c.size=25; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(file->keyf_revision,f); - lib3ds_string_write("????????.3ds", f); - lib3ds_intd_write(file->frames, f); - } - { /*---- LIB3DS_KFSEG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_KFSEG; - c.size=14; - lib3ds_chunk_write(&c,f); - lib3ds_intd_write(file->segment_from,f); - lib3ds_intd_write(file->segment_to,f); - } - { /*---- LIB3DS_KFCURTIME ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_KFCURTIME; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_intd_write(file->current_frame,f); - } - - { - Lib3dsNode *p; - for (p=file->nodes; p!=0; p=p->next) - { - if (!lib3ds_node_write(p, file, f)) - { - return(LIB3DS_FALSE); - } - if (!nodes_write(p, file, f)) - { - return(LIB3DS_FALSE); - } - } - } - - if (!lib3ds_chunk_write_end(&c,f)) - { + } + if (!nodes_write(p, file, f)) { return(LIB3DS_FALSE); + } } - return(LIB3DS_TRUE); + } + + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -895,38 +872,34 @@ kfdata_write(Lib3dsFile *file, FILE *f) Lib3dsBool lib3ds_file_write(Lib3dsFile *file, FILE *f) { + Lib3dsChunk c; + + c.chunk=LIB3DS_M3DMAGIC; + if (!lib3ds_chunk_write_start(&c,f)) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + + { /*---- LIB3DS_M3D_VERSION ----*/ Lib3dsChunk c; - c.chunk=LIB3DS_M3DMAGIC; - if (!lib3ds_chunk_write_start(&c,f)) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } + c.chunk=LIB3DS_M3D_VERSION; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_dword_write(file->mesh_version, f); + } - { /*---- LIB3DS_M3D_VERSION ----*/ - Lib3dsChunk c; + if (!mdata_write(file, f)) { + return(LIB3DS_FALSE); + } + if (!kfdata_write(file, f)) { + return(LIB3DS_FALSE); + } - c.chunk=LIB3DS_M3D_VERSION; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_dword_write(file->mesh_version, f); - } - - if (!mdata_write(file, f)) - { - return(LIB3DS_FALSE); - } - if (!kfdata_write(file, f)) - { - return(LIB3DS_FALSE); - } - - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -936,31 +909,27 @@ lib3ds_file_write(Lib3dsFile *file, FILE *f) void lib3ds_file_insert_material(Lib3dsFile *file, Lib3dsMaterial *material) { - Lib3dsMaterial *p,*q; + Lib3dsMaterial *p,*q; + + ASSERT(file); + ASSERT(material); + ASSERT(!material->next); - ASSERT(file); - ASSERT(material); - ASSERT(!material->next); - - q=0; - for (p=file->materials; p!=0; p=p->next) - { - if (strcmp(material->name, p->name)<0) - { - break; - } - q=p; - } - if (!q) - { - material->next=file->materials; - file->materials=material; - } - else - { - material->next=q->next; - q->next=material; + q=0; + for (p=file->materials; p!=0; p=p->next) { + if (strcmp(material->name, p->name)<0) { + break; } + q=p; + } + if (!q) { + material->next=file->materials; + file->materials=material; + } + else { + material->next=q->next; + q->next=material; + } } @@ -970,32 +939,27 @@ lib3ds_file_insert_material(Lib3dsFile *file, Lib3dsMaterial *material) void lib3ds_file_remove_material(Lib3dsFile *file, Lib3dsMaterial *material) { - Lib3dsMaterial *p,*q; + Lib3dsMaterial *p,*q; - ASSERT(file); - ASSERT(material); - ASSERT(file->materials); - for (p=0,q=file->materials; q; p=q,q=q->next) - { - if (q==material) - { - break; - } + ASSERT(file); + ASSERT(material); + ASSERT(file->materials); + for (p=0,q=file->materials; q; p=q,q=q->next) { + if (q==material) { + break; } - if (!q) - { - ASSERT(LIB3DS_FALSE); - return; - } - if (!p) - { - file->materials=material->next; - } - else - { - p->next=q->next; - } - material->next=0; + } + if (!q) { + ASSERT(LIB3DS_FALSE); + return; + } + if (!p) { + file->materials=material->next; + } + else { + p->next=q->next; + } + material->next=0; } @@ -1005,17 +969,15 @@ lib3ds_file_remove_material(Lib3dsFile *file, Lib3dsMaterial *material) Lib3dsMaterial* lib3ds_file_material_by_name(Lib3dsFile *file, const char *name) { - Lib3dsMaterial *p; + Lib3dsMaterial *p; - ASSERT(file); - for (p=file->materials; p!=0; p=p->next) - { - if (strcmp(p->name,name)==0) - { - return(p); - } + ASSERT(file); + for (p=file->materials; p!=0; p=p->next) { + if (strcmp(p->name,name)==0) { + return(p); } - return(0); + } + return(0); } @@ -1025,13 +987,12 @@ lib3ds_file_material_by_name(Lib3dsFile *file, const char *name) void lib3ds_file_dump_materials(Lib3dsFile *file) { - Lib3dsMaterial *p; + Lib3dsMaterial *p; - ASSERT(file); - for (p=file->materials; p!=0; p=p->next) - { - lib3ds_material_dump(p); - } + ASSERT(file); + for (p=file->materials; p!=0; p=p->next) { + lib3ds_material_dump(p); + } } @@ -1041,31 +1002,27 @@ lib3ds_file_dump_materials(Lib3dsFile *file) void lib3ds_file_insert_mesh(Lib3dsFile *file, Lib3dsMesh *mesh) { - Lib3dsMesh *p,*q; + Lib3dsMesh *p,*q; + + ASSERT(file); + ASSERT(mesh); + ASSERT(!mesh->next); - ASSERT(file); - ASSERT(mesh); - ASSERT(!mesh->next); - - q=0; - for (p=file->meshes; p!=0; p=p->next) - { - if (strcmp(mesh->name, p->name)<0) - { - break; - } - q=p; - } - if (!q) - { - mesh->next=file->meshes; - file->meshes=mesh; - } - else - { - mesh->next=q->next; - q->next=mesh; + q=0; + for (p=file->meshes; p!=0; p=p->next) { + if (strcmp(mesh->name, p->name)<0) { + break; } + q=p; + } + if (!q) { + mesh->next=file->meshes; + file->meshes=mesh; + } + else { + mesh->next=q->next; + q->next=mesh; + } } @@ -1075,32 +1032,27 @@ lib3ds_file_insert_mesh(Lib3dsFile *file, Lib3dsMesh *mesh) void lib3ds_file_remove_mesh(Lib3dsFile *file, Lib3dsMesh *mesh) { - Lib3dsMesh *p,*q; + Lib3dsMesh *p,*q; - ASSERT(file); - ASSERT(mesh); - ASSERT(file->meshes); - for (p=0,q=file->meshes; q; p=q,q=q->next) - { - if (q==mesh) - { - break; - } + ASSERT(file); + ASSERT(mesh); + ASSERT(file->meshes); + for (p=0,q=file->meshes; q; p=q,q=q->next) { + if (q==mesh) { + break; } - if (!q) - { - ASSERT(LIB3DS_FALSE); - return; - } - if (!p) - { - file->meshes=mesh->next; - } - else - { - p->next=q->next; - } - mesh->next=0; + } + if (!q) { + ASSERT(LIB3DS_FALSE); + return; + } + if (!p) { + file->meshes=mesh->next; + } + else { + p->next=q->next; + } + mesh->next=0; } @@ -1110,17 +1062,15 @@ lib3ds_file_remove_mesh(Lib3dsFile *file, Lib3dsMesh *mesh) Lib3dsMesh* lib3ds_file_mesh_by_name(Lib3dsFile *file, const char *name) { - Lib3dsMesh *p; + Lib3dsMesh *p; - ASSERT(file); - for (p=file->meshes; p!=0; p=p->next) - { - if (strcmp(p->name,name)==0) - { - return(p); - } + ASSERT(file); + for (p=file->meshes; p!=0; p=p->next) { + if (strcmp(p->name,name)==0) { + return(p); } - return(0); + } + return(0); } @@ -1130,35 +1080,32 @@ lib3ds_file_mesh_by_name(Lib3dsFile *file, const char *name) void lib3ds_file_dump_meshes(Lib3dsFile *file) { - Lib3dsMesh *p; + Lib3dsMesh *p; - ASSERT(file); - for (p=file->meshes; p!=0; p=p->next) - { - lib3ds_mesh_dump(p); - } + ASSERT(file); + for (p=file->meshes; p!=0; p=p->next) { + lib3ds_mesh_dump(p); + } } static void dump_instances(Lib3dsNode *node, const char* parent) { - Lib3dsNode *p; - char name[255]; + Lib3dsNode *p; + char name[255]; - ASSERT(node); - ASSERT(parent); - strcpy(name, parent); - strcat(name, "."); - strcat(name, node->name); - if (node->type==LIB3DS_OBJECT_NODE) - { - printf(" %s : %s\n", name, node->data.object.instance); - } - for (p=node->childs; p!=0; p=p->next) - { - dump_instances(p, parent); - } + ASSERT(node); + ASSERT(parent); + strcpy(name, parent); + strcat(name, "."); + strcat(name, node->name); + if (node->type==LIB3DS_OBJECT_NODE) { + printf(" %s : %s\n", name, node->data.object.instance); + } + for (p=node->childs; p!=0; p=p->next) { + dump_instances(p, parent); + } } @@ -1168,13 +1115,12 @@ dump_instances(Lib3dsNode *node, const char* parent) void lib3ds_file_dump_instances(Lib3dsFile *file) { - Lib3dsNode *p; + Lib3dsNode *p; - ASSERT(file); - for (p=file->nodes; p!=0; p=p->next) - { - dump_instances(p,""); - } + ASSERT(file); + for (p=file->nodes; p!=0; p=p->next) { + dump_instances(p,""); + } } @@ -1184,31 +1130,27 @@ lib3ds_file_dump_instances(Lib3dsFile *file) void lib3ds_file_insert_camera(Lib3dsFile *file, Lib3dsCamera *camera) { - Lib3dsCamera *p,*q; + Lib3dsCamera *p,*q; + + ASSERT(file); + ASSERT(camera); + ASSERT(!camera->next); - ASSERT(file); - ASSERT(camera); - ASSERT(!camera->next); - - q=0; - for (p=file->cameras; p!=0; p=p->next) - { - if (strcmp(camera->name, p->name)<0) - { - break; - } - q=p; - } - if (!q) - { - camera->next=file->cameras; - file->cameras=camera; - } - else - { - camera->next=q->next; - q->next=camera; + q=0; + for (p=file->cameras; p!=0; p=p->next) { + if (strcmp(camera->name, p->name)<0) { + break; } + q=p; + } + if (!q) { + camera->next=file->cameras; + file->cameras=camera; + } + else { + camera->next=q->next; + q->next=camera; + } } @@ -1218,32 +1160,27 @@ lib3ds_file_insert_camera(Lib3dsFile *file, Lib3dsCamera *camera) void lib3ds_file_remove_camera(Lib3dsFile *file, Lib3dsCamera *camera) { - Lib3dsCamera *p,*q; + Lib3dsCamera *p,*q; - ASSERT(file); - ASSERT(camera); - ASSERT(file->cameras); - for (p=0,q=file->cameras; q; p=q,q=q->next) - { - if (q==camera) - { - break; - } + ASSERT(file); + ASSERT(camera); + ASSERT(file->cameras); + for (p=0,q=file->cameras; q; p=q,q=q->next) { + if (q==camera) { + break; } - if (!q) - { - ASSERT(LIB3DS_FALSE); - return; - } - if (!p) - { - file->cameras=camera->next; - } - else - { - p->next=q->next; - } - camera->next=0; + } + if (!q) { + ASSERT(LIB3DS_FALSE); + return; + } + if (!p) { + file->cameras=camera->next; + } + else { + p->next=q->next; + } + camera->next=0; } @@ -1253,17 +1190,15 @@ lib3ds_file_remove_camera(Lib3dsFile *file, Lib3dsCamera *camera) Lib3dsCamera* lib3ds_file_camera_by_name(Lib3dsFile *file, const char *name) { - Lib3dsCamera *p; + Lib3dsCamera *p; - ASSERT(file); - for (p=file->cameras; p!=0; p=p->next) - { - if (strcmp(p->name,name)==0) - { - return(p); - } + ASSERT(file); + for (p=file->cameras; p!=0; p=p->next) { + if (strcmp(p->name,name)==0) { + return(p); } - return(0); + } + return(0); } @@ -1273,13 +1208,12 @@ lib3ds_file_camera_by_name(Lib3dsFile *file, const char *name) void lib3ds_file_dump_cameras(Lib3dsFile *file) { - Lib3dsCamera *p; + Lib3dsCamera *p; - ASSERT(file); - for (p=file->cameras; p!=0; p=p->next) - { - printf(" %s\n", p->name); - } + ASSERT(file); + for (p=file->cameras; p!=0; p=p->next) { + lib3ds_camera_dump(p); + } } @@ -1289,31 +1223,27 @@ lib3ds_file_dump_cameras(Lib3dsFile *file) void lib3ds_file_insert_light(Lib3dsFile *file, Lib3dsLight *light) { - Lib3dsLight *p,*q; + Lib3dsLight *p,*q; + + ASSERT(file); + ASSERT(light); + ASSERT(!light->next); - ASSERT(file); - ASSERT(light); - ASSERT(!light->next); - - q=0; - for (p=file->lights; p!=0; p=p->next) - { - if (strcmp(light->name, p->name)<0) - { - break; - } - q=p; - } - if (!q) - { - light->next=file->lights; - file->lights=light; - } - else - { - light->next=q->next; - q->next=light; + q=0; + for (p=file->lights; p!=0; p=p->next) { + if (strcmp(light->name, p->name)<0) { + break; } + q=p; + } + if (!q) { + light->next=file->lights; + file->lights=light; + } + else { + light->next=q->next; + q->next=light; + } } @@ -1323,32 +1253,27 @@ lib3ds_file_insert_light(Lib3dsFile *file, Lib3dsLight *light) void lib3ds_file_remove_light(Lib3dsFile *file, Lib3dsLight *light) { - Lib3dsLight *p,*q; + Lib3dsLight *p,*q; - ASSERT(file); - ASSERT(light); - ASSERT(file->lights); - for (p=0,q=file->lights; q; p=q,q=q->next) - { - if (q==light) - { - break; - } + ASSERT(file); + ASSERT(light); + ASSERT(file->lights); + for (p=0,q=file->lights; q; p=q,q=q->next) { + if (q==light) { + break; } - if (!q) - { - ASSERT(LIB3DS_FALSE); - return; - } - if (!p) - { - file->lights=light->next; - } - else - { - p->next=q->next; - } - light->next=0; + } + if (!q) { + ASSERT(LIB3DS_FALSE); + return; + } + if (!p) { + file->lights=light->next; + } + else { + p->next=q->next; + } + light->next=0; } @@ -1358,17 +1283,15 @@ lib3ds_file_remove_light(Lib3dsFile *file, Lib3dsLight *light) Lib3dsLight* lib3ds_file_light_by_name(Lib3dsFile *file, const char *name) { - Lib3dsLight *p; + Lib3dsLight *p; - ASSERT(file); - for (p=file->lights; p!=0; p=p->next) - { - if (strcmp(p->name,name)==0) - { - return(p); - } + ASSERT(file); + for (p=file->lights; p!=0; p=p->next) { + if (strcmp(p->name,name)==0) { + return(p); } - return(0); + } + return(0); } @@ -1378,38 +1301,95 @@ lib3ds_file_light_by_name(Lib3dsFile *file, const char *name) void lib3ds_file_dump_lights(Lib3dsFile *file) { - Lib3dsLight *p; + Lib3dsLight *p; - ASSERT(file); - for (p=file->lights; p!=0; p=p->next) - { - printf(" %s\n", p->name); - } + ASSERT(file); + for (p=file->lights; p!=0; p=p->next) { + lib3ds_light_dump(p); + } } +/*! + * \ingroup file + */ +void +lib3ds_file_bounding_box(Lib3dsFile *file, Lib3dsVector min, Lib3dsVector max) +{ + Lib3dsBool init=LIB3DS_FALSE; + + { + Lib3dsVector lmin, lmax; + Lib3dsMesh *p=file->meshes; + + if (!init && p) { + init = LIB3DS_TRUE; + lib3ds_mesh_bounding_box(p, min, max); + p = p->next; + } + while (p) { + lib3ds_mesh_bounding_box(p, lmin, lmax); + lib3ds_vector_min(min, lmin); + lib3ds_vector_max(max, lmax); + p=p->next; + } + } + { + Lib3dsCamera *p=file->cameras; + if (!init && p) { + init = LIB3DS_TRUE; + lib3ds_vector_copy(min, p->position); + lib3ds_vector_copy(max, p->position); + } + + while (p) { + lib3ds_vector_min(min, p->position); + lib3ds_vector_max(max, p->position); + lib3ds_vector_min(min, p->target); + lib3ds_vector_max(max, p->target); + p=p->next; + } + } + { + Lib3dsLight *p=file->lights; + if (!init && p) { + init = LIB3DS_TRUE; + lib3ds_vector_copy(min, p->position); + lib3ds_vector_copy(max, p->position); + } + + while (p) { + lib3ds_vector_min(min, p->position); + lib3ds_vector_max(max, p->position); + if (p->spot_light) { + lib3ds_vector_min(min, p->spot); + lib3ds_vector_max(max, p->spot); + } + p=p->next; + } + } +} + + /*! * \ingroup file */ Lib3dsNode* lib3ds_file_node_by_name(Lib3dsFile *file, const char* name, Lib3dsNodeTypes type) { - Lib3dsNode *p,*q; + Lib3dsNode *p,*q; - ASSERT(file); - for (p=file->nodes; p!=0; p=p->next) - { - if ((p->type==type) && (strcmp(p->name, name)==0)) - { - return(p); - } - q=lib3ds_node_by_name(p, name, type); - if (q) - { - return(q); - } + ASSERT(file); + for (p=file->nodes; p!=0; p=p->next) { + if ((p->type==type) && (strcmp(p->name, name)==0)) { + return(p); } - return(0); + q=lib3ds_node_by_name(p, name, type); + if (q) { + return(q); + } + } + return(0); } @@ -1417,24 +1397,21 @@ lib3ds_file_node_by_name(Lib3dsFile *file, const char* name, Lib3dsNodeTypes typ * \ingroup file */ Lib3dsNode* -lib3ds_file_node_by_id(Lib3dsFile *file, Lib3dsWord id) +lib3ds_file_node_by_id(Lib3dsFile *file, Lib3dsWord node_id) { - Lib3dsNode *p,*q; + Lib3dsNode *p,*q; - ASSERT(file); - for (p=file->nodes; p!=0; p=p->next) - { - if (p->id==id) - { - return(p); - } - q=lib3ds_node_by_id(p, id); - if (q) - { - return(q); - } + ASSERT(file); + for (p=file->nodes; p!=0; p=p->next) { + if (p->node_id==node_id) { + return(p); } - return(0); + q=lib3ds_node_by_id(p, node_id); + if (q) { + return(q); + } + } + return(0); } @@ -1444,72 +1421,58 @@ lib3ds_file_node_by_id(Lib3dsFile *file, Lib3dsWord id) void lib3ds_file_insert_node(Lib3dsFile *file, Lib3dsNode *node) { - Lib3dsNode *parent,*p,*n; + Lib3dsNode *parent,*p,*n; + + ASSERT(node); + ASSERT(!node->next); + ASSERT(!node->parent); - ASSERT(node); - ASSERT(!node->next); - ASSERT(!node->parent); + parent=0; + if (node->parent_id!=LIB3DS_NO_PARENT) { + parent=lib3ds_file_node_by_id(file, node->parent_id); + } + node->parent=parent; + + if (!parent) { + for (p=0,n=file->nodes; n!=0; p=n,n=n->next) { + if (strcmp(n->name, node->name)>0) { + break; + } + } + if (!p) { + node->next=file->nodes; + file->nodes=node; + } + else { + node->next=p->next; + p->next=node; + } + } + else { + for (p=0,n=parent->childs; n!=0; p=n,n=n->next) { + if (strcmp(n->name, node->name)>0) { + break; + } + } + if (!p) { + node->next=parent->childs; + parent->childs=node; + } + else { + node->next=p->next; + p->next=node; + } + } - parent=0; - if (node->parent_id!=LIB3DS_NO_PARENT) - { - parent=lib3ds_file_node_by_id(file, node->parent_id); - } - node->parent=parent; - - if (!parent) - { - for (p=0,n=file->nodes; n!=0; p=n,n=n->next) - { - if (strcmp(n->name, node->name)>0) - { - break; - } - } - if (!p) - { - node->next=file->nodes; - file->nodes=node; - } - else - { - node->next=p->next; - p->next=node; - } - } - else - { - for (p=0,n=parent->childs; n!=0; p=n,n=n->next) - { - if (strcmp(n->name, node->name)>0) - { - break; - } - } - if (!p) - { - node->next=parent->childs; - parent->childs=node; - } - else - { - node->next=p->next; - p->next=node; - } - } - - if (node->id!=LIB3DS_NO_PARENT) - { - for (n=file->nodes; n!=0; n=p) - { - p=n->next; - if (n->parent_id==node->id) - { - lib3ds_file_remove_node(file, n); - lib3ds_file_insert_node(file, n); - } - } + if (node->node_id!=LIB3DS_NO_PARENT) { + for (n=file->nodes; n!=0; n=p) { + p=n->next; + if (n->parent_id==node->node_id) { + lib3ds_file_remove_node(file, n); + lib3ds_file_insert_node(file, n); + } } + } } @@ -1519,55 +1482,43 @@ lib3ds_file_insert_node(Lib3dsFile *file, Lib3dsNode *node) Lib3dsBool lib3ds_file_remove_node(Lib3dsFile *file, Lib3dsNode *node) { - Lib3dsNode *p,*n; + Lib3dsNode *p,*n; - if (node->parent) - { - for (p=0,n=node->parent->childs; n; p=n,n=n->next) - { - if (n==node) - { - break; - } - } - if (!n) - { - return(LIB3DS_FALSE); - } - - if (!p) - { - node->parent->childs=n->next; - } - else - { - p->next=n->next; - } + if (node->parent) { + for (p=0,n=node->parent->childs; n; p=n,n=n->next) { + if (n==node) { + break; + } } - else - { - for (p=0,n=file->nodes; n; p=n,n=n->next) - { - if (n==node) - { - break; - } - } - if (!n) - { - return(LIB3DS_FALSE); - } - - if (!p) - { - file->nodes=n->next; - } - else - { - p->next=n->next; - } + if (!n) { + return(LIB3DS_FALSE); } - return(LIB3DS_TRUE); + + if (!p) { + node->parent->childs=n->next; + } + else { + p->next=n->next; + } + } + else { + for (p=0,n=file->nodes; n; p=n,n=n->next) { + if (n==node) { + break; + } + } + if (!n) { + return(LIB3DS_FALSE); + } + + if (!p) { + file->nodes=n->next; + } + else { + p->next=n->next; + } + } + return(LIB3DS_TRUE); } @@ -1577,13 +1528,12 @@ lib3ds_file_remove_node(Lib3dsFile *file, Lib3dsNode *node) void lib3ds_file_dump_nodes(Lib3dsFile *file) { - Lib3dsNode *p; + Lib3dsNode *p; - ASSERT(file); - for (p=file->nodes; p!=0; p=p->next) - { - lib3ds_node_dump(p,1); - } + ASSERT(file); + for (p=file->nodes; p!=0; p=p->next) { + lib3ds_node_dump(p,1); + } } diff --git a/src/osgPlugins/lib3ds/file.h b/src/osgPlugins/lib3ds/file.h index a64f8b2a5..ee0444b13 100644 --- a/src/osgPlugins/lib3ds/file.h +++ b/src/osgPlugins/lib3ds/file.h @@ -47,6 +47,7 @@ extern "C" { struct _Lib3dsFile { Lib3dsDword mesh_version; Lib3dsWord keyf_revision; + char name[12+1]; Lib3dsFloat master_scale; Lib3dsVector construction_plane; Lib3dsRgb ambient; @@ -66,40 +67,33 @@ struct _Lib3dsFile { Lib3dsNode *nodes; }; -extern LIB3DSAPI Lib3dsFile* lib3ds_open(const char *filename); -extern LIB3DSAPI void lib3ds_close(Lib3dsFile *file); - +extern LIB3DSAPI Lib3dsFile* lib3ds_file_load(const char *filename); +extern LIB3DSAPI Lib3dsBool lib3ds_file_save(Lib3dsFile *file, const char *filename); extern LIB3DSAPI Lib3dsFile* lib3ds_file_new(); extern LIB3DSAPI void lib3ds_file_free(Lib3dsFile *file); extern LIB3DSAPI void lib3ds_file_eval(Lib3dsFile *file, Lib3dsFloat t); - extern LIB3DSAPI Lib3dsBool lib3ds_file_read(Lib3dsFile *file, FILE *f); extern LIB3DSAPI Lib3dsBool lib3ds_file_write(Lib3dsFile *file, FILE *f); - extern LIB3DSAPI void lib3ds_file_insert_material(Lib3dsFile *file, Lib3dsMaterial *material); extern LIB3DSAPI void lib3ds_file_remove_material(Lib3dsFile *file, Lib3dsMaterial *material); extern LIB3DSAPI Lib3dsMaterial* lib3ds_file_material_by_name(Lib3dsFile *file, const char *name); extern LIB3DSAPI void lib3ds_file_dump_materials(Lib3dsFile *file); - extern LIB3DSAPI void lib3ds_file_insert_mesh(Lib3dsFile *file, Lib3dsMesh *mesh); extern LIB3DSAPI void lib3ds_file_remove_mesh(Lib3dsFile *file, Lib3dsMesh *mesh); extern LIB3DSAPI Lib3dsMesh* lib3ds_file_mesh_by_name(Lib3dsFile *file, const char *name); extern LIB3DSAPI void lib3ds_file_dump_meshes(Lib3dsFile *file); extern LIB3DSAPI void lib3ds_file_dump_instances(Lib3dsFile *file); - extern LIB3DSAPI void lib3ds_file_insert_camera(Lib3dsFile *file, Lib3dsCamera *camera); extern LIB3DSAPI void lib3ds_file_remove_camera(Lib3dsFile *file, Lib3dsCamera *camera); extern LIB3DSAPI Lib3dsCamera* lib3ds_file_camera_by_name(Lib3dsFile *file, const char *name); extern LIB3DSAPI void lib3ds_file_dump_cameras(Lib3dsFile *file); - extern LIB3DSAPI void lib3ds_file_insert_light(Lib3dsFile *file, Lib3dsLight *light); extern LIB3DSAPI void lib3ds_file_remove_light(Lib3dsFile *file, Lib3dsLight *light); extern LIB3DSAPI Lib3dsLight* lib3ds_file_light_by_name(Lib3dsFile *file, const char *name); extern LIB3DSAPI void lib3ds_file_dump_lights(Lib3dsFile *file); - extern LIB3DSAPI Lib3dsNode* lib3ds_file_node_by_name(Lib3dsFile *file, const char* name, Lib3dsNodeTypes type); -extern LIB3DSAPI Lib3dsNode* lib3ds_file_node_by_id(Lib3dsFile *file, Lib3dsWord id); +extern LIB3DSAPI Lib3dsNode* lib3ds_file_node_by_id(Lib3dsFile *file, Lib3dsWord node_id); extern LIB3DSAPI void lib3ds_file_insert_node(Lib3dsFile *file, Lib3dsNode *node); extern LIB3DSAPI Lib3dsBool lib3ds_file_remove_node(Lib3dsFile *file, Lib3dsNode *node); extern LIB3DSAPI void lib3ds_file_dump_nodes(Lib3dsFile *file); diff --git a/src/osgPlugins/lib3ds/lib3ds_float.cpp b/src/osgPlugins/lib3ds/lib3ds_float.cpp index 2124d5342..443ff370c 100644 --- a/src/osgPlugins/lib3ds/lib3ds_float.cpp +++ b/src/osgPlugins/lib3ds/lib3ds_float.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -22,23 +22,26 @@ #define LIB3DS_EXPORT #include + /*! * \defgroup float Floating Point Mathematics * * \author J.E. Hoffmann */ + /*! * \ingroup float */ Lib3dsFloat lib3ds_float_cubic(Lib3dsFloat a, Lib3dsFloat p, Lib3dsFloat q, Lib3dsFloat b, Lib3dsFloat t) { - Lib3dsDouble x,y,z,w; + Lib3dsDouble x,y,z,w; - x=2*t*t*t - 3*t*t + 1; - y=-2*t*t*t + 3*t*t; - z=t*t*t - 2*t*t + t; - w=t*t*t - t*t; - return((Lib3dsFloat)(x*a + y*b + z*p + w*q)); + x=2*t*t*t - 3*t*t + 1; + y=-2*t*t*t + 3*t*t; + z=t*t*t - 2*t*t + t; + w=t*t*t - t*t; + return((Lib3dsFloat)(x*a + y*b + z*p + w*q)); } + diff --git a/src/osgPlugins/lib3ds/light.cpp b/src/osgPlugins/lib3ds/light.cpp index ddccc5642..e64e4134e 100644 --- a/src/osgPlugins/lib3ds/light.cpp +++ b/src/osgPlugins/lib3ds/light.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -26,16 +26,26 @@ #include #include #include -//#include +#include #ifdef WITH_DMALLOC #include #endif + /*! * \defgroup light Lights * * \author J.E. Hoffmann */ +/*! + +\typedef Lib3dsLight + \ingroup light + \sa _Lib3dsLight + +*/ + + /*! * \ingroup light @@ -43,18 +53,17 @@ Lib3dsLight* lib3ds_light_new(const char *name) { - Lib3dsLight *light; + Lib3dsLight *light; - ASSERT(name); - ASSERT(strlen(name)<64); - - light=(Lib3dsLight*)calloc(sizeof(Lib3dsLight), 1); - if (!light) - { - return(0); - } - strcpy(light->name, name); - return(light); + ASSERT(name); + ASSERT(strlen(name)<64); + + light=(Lib3dsLight*)calloc(sizeof(Lib3dsLight), 1); + if (!light) { + return(0); + } + strcpy(light->name, name); + return(light); } @@ -64,8 +73,134 @@ lib3ds_light_new(const char *name) void lib3ds_light_free(Lib3dsLight *light) { - memset(light, 0, sizeof(Lib3dsLight)); - free(light); + memset(light, 0, sizeof(Lib3dsLight)); + free(light); +} + + +/*! + * \ingroup light + */ +void +lib3ds_light_dump(Lib3dsLight *light) +{ + ASSERT(light); + printf(" name: %s\n", light->name); + printf(" spot_light: %s\n", light->spot_light ? "yes" : "no"); + printf(" see_cone: %s\n", light->see_cone ? "yes" : "no"); + printf(" color: (%f, %f, %f)\n", + light->color[0], light->color[1], light->color[2]); + printf(" position (%f, %f, %f)\n", + light->position[0], light->position[1], light->position[2]); + printf(" spot (%f, %f, %f)\n", + light->spot[0], light->spot[1], light->spot[2]); + printf(" roll: %f\n", light->roll); + printf(" off: %s\n", light->off ? "yes" : "no"); + printf(" outer_range: %f\n", light->outer_range); + printf(" inner_range: %f\n", light->inner_range); + printf(" multiplier: %f\n", light->multiplier); + printf(" attenuation: %f\n", light->attenuation); + printf(" rectangular_spot: %s\n", light->rectangular_spot ? "yes" : "no"); + printf(" shadowed: %s\n", light->shadowed ? "yes" : "no"); + printf(" shadow_bias: %f\n", light->shadow_bias); + printf(" shadow_filter: %f\n", light->shadow_filter); + printf(" shadow_size: %d\n", light->shadow_size); + printf(" spot_aspect: %f\n", light->spot_aspect); + printf(" use_projector: %s\n", light->use_projector ? "yes" : "no"); + printf(" projector: %s\n", light->projector); + printf(" spot_overshoot: %d\n", light->spot_overshoot); + printf(" ray_shadows: %s\n", light->ray_shadows ? "yes" : "no"); + printf(" ray_bias: %f\n", light->ray_bias); + printf(" hot_spot: %f\n", light->hot_spot); + printf(" fall_off: %f\n", light->fall_off); + printf("\n"); +} + + +/*! + * \ingroup light + */ +static Lib3dsBool +spotlight_read(Lib3dsLight *light, FILE *f) +{ + Lib3dsChunk c; + Lib3dsWord chunk; + int i; + + if (!lib3ds_chunk_read_start(&c, LIB3DS_DL_SPOTLIGHT, f)) { + return(LIB3DS_FALSE); + } + light->spot_light=LIB3DS_TRUE; + for (i=0; i<3; ++i) { + light->spot[i]=lib3ds_float_read(f); + } + light->hot_spot = lib3ds_float_read(f); + light->fall_off = lib3ds_float_read(f); + lib3ds_chunk_read_tell(&c, f); + + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_DL_SPOT_ROLL: + { + light->roll=lib3ds_float_read(f); + } + break; + case LIB3DS_DL_SHADOWED: + { + light->shadowed=LIB3DS_TRUE; + } + break; + case LIB3DS_DL_LOCAL_SHADOW2: + { + light->shadow_bias=lib3ds_float_read(f); + light->shadow_filter=lib3ds_float_read(f); + light->shadow_size=lib3ds_intw_read(f); + } + break; + case LIB3DS_DL_SEE_CONE: + { + light->see_cone=LIB3DS_TRUE; + } + break; + case LIB3DS_DL_SPOT_RECTANGULAR: + { + light->rectangular_spot=LIB3DS_TRUE; + } + break; + case LIB3DS_DL_SPOT_ASPECT: + { + light->spot_aspect=lib3ds_float_read(f); + } + break; + case LIB3DS_DL_SPOT_PROJECTOR: + { + light->use_projector=LIB3DS_TRUE; + if (!lib3ds_string_read(light->projector, 64, f)) { + return(LIB3DS_FALSE); + } + } + case LIB3DS_DL_SPOT_OVERSHOOT: + { + light->spot_overshoot=LIB3DS_TRUE; + } + break; + case LIB3DS_DL_RAY_BIAS: + { + light->ray_bias=lib3ds_float_read(f); + } + break; + case LIB3DS_DL_RAYSHAD: + { + light->ray_shadows=LIB3DS_TRUE; + } + break; + default: + lib3ds_chunk_unknown(chunk); + } + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } @@ -75,139 +210,75 @@ lib3ds_light_free(Lib3dsLight *light) Lib3dsBool lib3ds_light_read(Lib3dsLight *light, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_N_DIRECT_LIGHT, f)) - { - return(LIB3DS_FALSE); + if (!lib3ds_chunk_read_start(&c, LIB3DS_N_DIRECT_LIGHT, f)) { + return(LIB3DS_FALSE); + } + { + int i; + for (i=0; i<3; ++i) { + light->position[i]=lib3ds_float_read(f); } - { - int i; - for (i=0; i<3; ++i) + } + lib3ds_chunk_read_tell(&c, f); + + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_COLOR_F: { - light->position[i]=lib3ds_float_read(f); + int i; + for (i=0; i<3; ++i) { + light->color[i]=lib3ds_float_read(f); + } } - } - lib3ds_chunk_read_tell(&c, f); - - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + break; + case LIB3DS_DL_OFF: { - case LIB3DS_COLOR_F: - { - int i; - for (i=0; i<3; ++i) - { - light->color[i]=lib3ds_float_read(f); - } - } - break; - case LIB3DS_DL_OFF: - { - light->off=LIB3DS_TRUE; - } - break; - case LIB3DS_DL_OUTER_RANGE: - { - light->outer_range=lib3ds_float_read(f); - } - break; - case LIB3DS_DL_INNER_RANGE: - { - light->inner_range=lib3ds_float_read(f); - } - break; - case LIB3DS_DL_MULTIPLIER: - { - light->multiplier=lib3ds_float_read(f); - } - break; - case LIB3DS_DL_EXCLUDE: - { - /* FIXME: */ - lib3ds_chunk_unknown(chunk); - } - case LIB3DS_DL_ATTENUATE: - { - light->attenuation=lib3ds_float_read(f); - } - break; - case LIB3DS_DL_SPOTLIGHT: - { - int i; - light->spot_light=LIB3DS_TRUE; - for (i=0; i<3; ++i) - { - light->spot[i]=lib3ds_float_read(f); - } - light->hot_spot=lib3ds_float_read(f); - light->fall_off=lib3ds_float_read(f); - } - break; - case LIB3DS_DL_SPOT_ROLL: - { - light->roll=lib3ds_float_read(f); - } - break; - case LIB3DS_DL_SHADOWED: - { - light->shadowed=LIB3DS_TRUE; - } - break; - case LIB3DS_DL_LOCAL_SHADOW2: - { - light->shadow_bias=lib3ds_float_read(f); - light->shadow_filter=lib3ds_float_read(f); - light->shadow_size=lib3ds_intw_read(f); - } - break; - case LIB3DS_DL_SEE_CONE: - { - light->see_cone=LIB3DS_TRUE; - } - break; - case LIB3DS_DL_SPOT_RECTANGULAR: - { - light->rectangular_spot=LIB3DS_TRUE; - } - break; - case LIB3DS_DL_SPOT_ASPECT: - { - light->spot_aspect=lib3ds_float_read(f); - } - break; - case LIB3DS_DL_SPOT_PROJECTOR: - { - light->use_projector=LIB3DS_TRUE; - if (!lib3ds_string_read(light->projector, 64, f)) - { - return(LIB3DS_FALSE); - } - } - case LIB3DS_DL_SPOT_OVERSHOOT: - { - light->spot_overshoot=LIB3DS_TRUE; - } - break; - case LIB3DS_DL_RAY_BIAS: - { - light->ray_bias=lib3ds_float_read(f); - } - break; - case LIB3DS_DL_RAYSHAD: - { - light->ray_shadows=LIB3DS_TRUE; - } - break; - default: - lib3ds_chunk_unknown(chunk); + light->off=LIB3DS_TRUE; } + break; + case LIB3DS_DL_OUTER_RANGE: + { + light->outer_range=lib3ds_float_read(f); + } + break; + case LIB3DS_DL_INNER_RANGE: + { + light->inner_range=lib3ds_float_read(f); + } + break; + case LIB3DS_DL_MULTIPLIER: + { + light->multiplier=lib3ds_float_read(f); + } + break; + case LIB3DS_DL_EXCLUDE: + { + /* FIXME: */ + lib3ds_chunk_unknown(chunk); + } + case LIB3DS_DL_ATTENUATE: + { + light->attenuation=lib3ds_float_read(f); + } + break; + case LIB3DS_DL_SPOTLIGHT: + { + lib3ds_chunk_read_reset(&c, f); + if (!spotlight_read(light, f)) { + return(LIB3DS_FALSE); + } + } + break; + default: + lib3ds_chunk_unknown(chunk); } - - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } @@ -217,162 +288,142 @@ lib3ds_light_read(Lib3dsLight *light, FILE *f) Lib3dsBool lib3ds_light_write(Lib3dsLight *light, FILE *f) { + Lib3dsChunk c; + + c.chunk=LIB3DS_N_DIRECT_LIGHT; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + lib3ds_vector_write(light->position, f); + { /*---- LIB3DS_COLOR_F ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_COLOR_F; + c.size=18; + lib3ds_chunk_write(&c, f); + lib3ds_rgb_write(light->color,f); + } + if (light->off) { /*---- LIB3DS_DL_OFF ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_OFF; + c.size=6; + lib3ds_chunk_write(&c, f); + } + { /*---- LIB3DS_DL_OUTER_RANGE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_OUTER_RANGE; + c.size=10; + lib3ds_chunk_write(&c, f); + lib3ds_float_write(light->outer_range,f); + } + { /*---- LIB3DS_DL_INNER_RANGE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_INNER_RANGE; + c.size=10; + lib3ds_chunk_write(&c, f); + lib3ds_float_write(light->inner_range,f); + } + { /*---- LIB3DS_DL_MULTIPLIER ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_MULTIPLIER; + c.size=10; + lib3ds_chunk_write(&c, f); + lib3ds_float_write(light->multiplier, f); + } + if (light->attenuation) { /*---- LIB3DS_DL_ATTENUATE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_ATTENUATE; + c.size=6; + lib3ds_chunk_write(&c, f); + } + + if (light->spot_light) { Lib3dsChunk c; - c.chunk=LIB3DS_N_DIRECT_LIGHT; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); + c.chunk=LIB3DS_DL_SPOTLIGHT; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); } - lib3ds_vector_write(light->position, f); - { /*---- LIB3DS_COLOR_F ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_COLOR_F; - c.size=18; - lib3ds_chunk_write(&c, f); - lib3ds_rgb_write(light->color,f); + lib3ds_vector_write(light->spot, f); + lib3ds_float_write(light->hot_spot, f); + lib3ds_float_write(light->fall_off, f); + + { /*---- LIB3DS_DL_SPOT_ROLL ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_SPOT_ROLL; + c.size=10; + lib3ds_chunk_write(&c, f); + lib3ds_float_write(light->roll,f); } - if (light->off) /*---- LIB3DS_DL_OFF ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_DL_OFF; - c.size=6; - lib3ds_chunk_write(&c, f); + if (light->shadowed) { /*---- LIB3DS_DL_SHADOWED ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_SHADOWED; + c.size=6; + lib3ds_chunk_write(&c, f); } - { /*---- LIB3DS_DL_OUTER_RANGE ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_DL_OUTER_RANGE; - c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->outer_range,f); + if ((fabs(light->shadow_bias)>LIB3DS_EPSILON) || + (fabs(light->shadow_filter)>LIB3DS_EPSILON) || + (light->shadow_size!=0)) { /*---- LIB3DS_DL_LOCAL_SHADOW2 ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_LOCAL_SHADOW2; + c.size=16; + lib3ds_chunk_write(&c, f); + lib3ds_float_write(light->shadow_bias,f); + lib3ds_float_write(light->shadow_filter,f); + lib3ds_intw_write(light->shadow_size,f); } - { /*---- LIB3DS_DL_INNER_RANGE ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_DL_INNER_RANGE; - c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->inner_range,f); + if (light->see_cone) { /*---- LIB3DS_DL_SEE_CONE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_SEE_CONE; + c.size=6; + lib3ds_chunk_write(&c, f); } - { /*---- LIB3DS_DL_MULTIPLIER ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_DL_MULTIPLIER; - c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->multiplier, f); + if (light->rectangular_spot) { /*---- LIB3DS_DL_SPOT_RECTANGULAR ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_SPOT_RECTANGULAR; + c.size=6; + lib3ds_chunk_write(&c, f); } - if (light->attenuation) /*---- LIB3DS_DL_ATTENUATE ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_DL_ATTENUATE; - c.size=6; - lib3ds_chunk_write(&c, f); + if (fabs(light->spot_aspect)>LIB3DS_EPSILON) { /*---- LIB3DS_DL_SPOT_ASPECT ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_SPOT_ASPECT; + c.size=10; + lib3ds_chunk_write(&c, f); + lib3ds_float_write(light->spot_aspect,f); } - - if (light->spot_light) - { - Lib3dsChunk c; - - c.chunk=LIB3DS_DL_SPOTLIGHT; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - lib3ds_vector_write(light->spot, f); - lib3ds_float_write(light->hot_spot, f); - lib3ds_float_write(light->fall_off, f); - - { /*---- LIB3DS_DL_SPOT_ROLL ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_DL_SPOT_ROLL; - c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->roll,f); - } - if (light->shadowed) /*---- LIB3DS_DL_SHADOWED ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_DL_SHADOWED; - c.size=6; - lib3ds_chunk_write(&c, f); - } - { /*---- LIB3DS_DL_LOCAL_SHADOW2 ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_DL_LOCAL_SHADOW2; - c.size=16; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->shadow_bias,f); - lib3ds_float_write(light->shadow_filter,f); - lib3ds_intw_write(light->shadow_size,f); - } - if (light->see_cone) /*---- LIB3DS_DL_SEE_CONE ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_DL_SEE_CONE; - c.size=6; - lib3ds_chunk_write(&c, f); - } - /*---- LIB3DS_DL_SPOT_RECTANGULAR ----*/ - if (light->rectangular_spot) - { - Lib3dsChunk c; - c.chunk=LIB3DS_DL_SPOT_RECTANGULAR; - c.size=6; - lib3ds_chunk_write(&c, f); - } - { /*---- LIB3DS_DL_SPOT_ASPECT ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_DL_SPOT_ASPECT; - c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->spot_aspect,f); - } - if (light->use_projector)/*---- LIB3DS_DL_SPOT_PROJECTOR ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_DL_SPOT_PROJECTOR; - c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_string_write(light->projector,f); - } - /*---- LIB3DS_DL_SPOT_OVERSHOOT ----*/ - if (light->spot_overshoot) - { - Lib3dsChunk c; - c.chunk=LIB3DS_DL_SPOT_OVERSHOOT; - c.size=6; - lib3ds_chunk_write(&c, f); - } - { /*---- LIB3DS_DL_RAY_BIAS ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_DL_RAY_BIAS; - c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->ray_bias,f); - } - if (light->ray_shadows) /*---- LIB3DS_DL_RAYSHAD ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_DL_RAYSHAD; - c.size=6; - lib3ds_chunk_write(&c, f); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (light->use_projector) { /*---- LIB3DS_DL_SPOT_PROJECTOR ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_SPOT_PROJECTOR; + c.size=10; + lib3ds_chunk_write(&c, f); + lib3ds_string_write(light->projector,f); } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); + if (light->spot_overshoot) { /*---- LIB3DS_DL_SPOT_OVERSHOOT ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_SPOT_OVERSHOOT; + c.size=6; + lib3ds_chunk_write(&c, f); } - return(LIB3DS_TRUE); + if (fabs(light->ray_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_DL_RAY_BIAS ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_RAY_BIAS; + c.size=10; + lib3ds_chunk_write(&c, f); + lib3ds_float_write(light->ray_bias,f); + } + if (light->ray_shadows) { /*---- LIB3DS_DL_RAYSHAD ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_DL_RAYSHAD; + c.size=6; + lib3ds_chunk_write(&c, f); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } -/*! - -\typedef Lib3dsLight - \ingroup light - \sa _Lib3dsLight - -*/ diff --git a/src/osgPlugins/lib3ds/light.h b/src/osgPlugins/lib3ds/light.h index 8d981dc07..3076ecd1e 100644 --- a/src/osgPlugins/lib3ds/light.h +++ b/src/osgPlugins/lib3ds/light.h @@ -67,6 +67,7 @@ struct _Lib3dsLight { extern LIB3DSAPI Lib3dsLight* lib3ds_light_new(const char *name); extern LIB3DSAPI void lib3ds_light_free(Lib3dsLight *mesh); +extern LIB3DSAPI void lib3ds_light_dump(Lib3dsLight *light); extern LIB3DSAPI Lib3dsBool lib3ds_light_read(Lib3dsLight *light, FILE *f); extern LIB3DSAPI Lib3dsBool lib3ds_light_write(Lib3dsLight *light, FILE *f); diff --git a/src/osgPlugins/lib3ds/material.cpp b/src/osgPlugins/lib3ds/material.cpp index ed8c1920f..7d9214d2a 100644 --- a/src/osgPlugins/lib3ds/material.cpp +++ b/src/osgPlugins/lib3ds/material.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -26,31 +26,32 @@ #include #include #include -//#include +#include #ifdef WITH_DMALLOC #include #endif + /*! * \defgroup material Materials * * \author J.E. Hoffmann */ + /*! * \ingroup material */ Lib3dsMaterial* lib3ds_material_new() { - Lib3dsMaterial *material; + Lib3dsMaterial *material; - material=(Lib3dsMaterial*)calloc(sizeof(Lib3dsMaterial), 1); - if (!material) - { - return(0); - } - return(material); + material=(Lib3dsMaterial*)calloc(sizeof(Lib3dsMaterial), 1); + if (!material) { + return(0); + } + return(material); } @@ -60,197 +61,216 @@ lib3ds_material_new() void lib3ds_material_free(Lib3dsMaterial *material) { - memset(material, 0, sizeof(Lib3dsMaterial)); - free(material); + memset(material, 0, sizeof(Lib3dsMaterial)); + free(material); } static Lib3dsBool color_read(Lib3dsRgba rgb, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; - Lib3dsBool have_lin=LIB3DS_FALSE; + Lib3dsChunk c; + Lib3dsWord chunk; + Lib3dsBool have_lin=LIB3DS_FALSE; - if (!lib3ds_chunk_read_start(&c, 0, f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_read_start(&c, 0, f)) { + return(LIB3DS_FALSE); + } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_LIN_COLOR_24: { - case LIB3DS_LIN_COLOR_24: - { - int i; - for (i=0; i<3; ++i) - { - rgb[i]=1.0f*lib3ds_byte_read(f)/255.0f; - } - rgb[3]=1.0f; - } - have_lin=LIB3DS_TRUE; - break; - case LIB3DS_COLOR_24: - /* gamma corrected color chunk - replaced in 3ds R3 by LIN_COLOR_24 */ - if (!have_lin) - { - int i; - for (i=0; i<3; ++i) - { - rgb[i]=1.0f*lib3ds_byte_read(f)/255.0f; - } - rgb[3]=1.0f; - } - break; - default: - lib3ds_chunk_unknown(chunk); + int i; + for (i=0; i<3; ++i) { + rgb[i]=1.0f*lib3ds_byte_read(f)/255.0f; + } + rgb[3]=1.0f; } + have_lin=LIB3DS_TRUE; + break; + case LIB3DS_COLOR_24: + /* gamma corrected color chunk + replaced in 3ds R3 by LIN_COLOR_24 */ + if (!have_lin) { + int i; + for (i=0; i<3; ++i) { + rgb[i]=1.0f*lib3ds_byte_read(f)/255.0f; + } + rgb[3]=1.0f; + } + break; + default: + lib3ds_chunk_unknown(chunk); } - - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool int_percentage_read(Lib3dsFloat *p, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, 0, f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_read_start(&c, 0, f)) { + return(LIB3DS_FALSE); + } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_INT_PERCENTAGE: { - case LIB3DS_INT_PERCENTAGE: - { - Lib3dsIntw i=lib3ds_intw_read(f); - *p=(Lib3dsFloat)(1.0*i/100.0); - } - break; - default: - lib3ds_chunk_unknown(chunk); + Lib3dsIntw i=lib3ds_intw_read(f); + *p=(Lib3dsFloat)(1.0*i/100.0); } + break; + default: + lib3ds_chunk_unknown(chunk); } - - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool texture_map_read(Lib3dsTextureMap *map, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, 0, f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_read_start(&c, 0, f)) { + return(LIB3DS_FALSE); + } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_INT_PERCENTAGE: { - case LIB3DS_INT_PERCENTAGE: - { - map->percent=1.0f*lib3ds_intw_read(f)/100.0f; - } - break; - case LIB3DS_MAT_MAPNAME: - { - if (!lib3ds_string_read(map->name, 64, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_MAP_TILING: - { - map->flags=lib3ds_word_read(f); - } - break; - case LIB3DS_MAT_MAP_TEXBLUR: - { - map->blur=lib3ds_float_read(f); - } - break; - case LIB3DS_MAT_MAP_USCALE: - { - map->scale[0]=lib3ds_float_read(f); - } - break; - case LIB3DS_MAT_MAP_VSCALE: - { - map->scale[1]=lib3ds_float_read(f); - } - break; - case LIB3DS_MAT_MAP_UOFFSET: - { - map->offset[0]=lib3ds_float_read(f); - } - break; - case LIB3DS_MAT_MAP_VOFFSET: - { - map->offset[1]=lib3ds_float_read(f); - } - break; - case LIB3DS_MAT_MAP_ANG: - { - map->rotation=lib3ds_float_read(f); - } - break; - case LIB3DS_MAT_MAP_COL1: - { - map->tint_1[0]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_1[1]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_1[2]=1.0f*lib3ds_byte_read(f)/255.0f; - } - break; - case LIB3DS_MAT_MAP_COL2: - { - map->tint_2[0]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_2[1]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_2[2]=1.0f*lib3ds_byte_read(f)/255.0f; - } - break; - case LIB3DS_MAT_MAP_RCOL: - { - map->tint_r[0]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_r[1]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_r[2]=1.0f*lib3ds_byte_read(f)/255.0f; - } - break; - case LIB3DS_MAT_MAP_GCOL: - { - map->tint_g[0]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_g[1]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_g[2]=1.0f*lib3ds_byte_read(f)/255.0f; - } - break; - case LIB3DS_MAT_MAP_BCOL: - { - map->tint_b[0]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_b[1]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_b[2]=1.0f*lib3ds_byte_read(f)/255.0f; - } - break; - default: - lib3ds_chunk_unknown(chunk); + map->percent=1.0f*lib3ds_intw_read(f)/100.0f; } + break; + case LIB3DS_MAT_MAPNAME: + { + if (!lib3ds_string_read(map->name, 64, f)) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_dump_info(" NAME=%s", map->name); + } + break; + case LIB3DS_MAT_MAP_TILING: + { + map->flags=lib3ds_word_read(f); + } + break; + case LIB3DS_MAT_MAP_TEXBLUR: + { + map->blur=lib3ds_float_read(f); + } + break; + case LIB3DS_MAT_MAP_USCALE: + { + map->scale[0]=lib3ds_float_read(f); + } + break; + case LIB3DS_MAT_MAP_VSCALE: + { + map->scale[1]=lib3ds_float_read(f); + } + break; + case LIB3DS_MAT_MAP_UOFFSET: + { + map->offset[0]=lib3ds_float_read(f); + } + break; + case LIB3DS_MAT_MAP_VOFFSET: + { + map->offset[1]=lib3ds_float_read(f); + } + break; + case LIB3DS_MAT_MAP_ANG: + { + map->rotation=lib3ds_float_read(f); + } + break; + case LIB3DS_MAT_MAP_COL1: + { + map->tint_1[0]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_1[1]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_1[2]=1.0f*lib3ds_byte_read(f)/255.0f; + } + break; + case LIB3DS_MAT_MAP_COL2: + { + map->tint_2[0]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_2[1]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_2[2]=1.0f*lib3ds_byte_read(f)/255.0f; + } + break; + case LIB3DS_MAT_MAP_RCOL: + { + map->tint_r[0]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_r[1]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_r[2]=1.0f*lib3ds_byte_read(f)/255.0f; + } + break; + case LIB3DS_MAT_MAP_GCOL: + { + map->tint_g[0]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_g[1]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_g[2]=1.0f*lib3ds_byte_read(f)/255.0f; + } + break; + case LIB3DS_MAT_MAP_BCOL: + { + map->tint_b[0]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_b[1]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_b[2]=1.0f*lib3ds_byte_read(f)/255.0f; + } + break; + default: + lib3ds_chunk_unknown(chunk); } + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); +} - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + +/*! + * \ingroup material + */ +static void +texture_dump(const char *maptype, Lib3dsTextureMap *texture) +{ + ASSERT(texture); + if (strlen(texture->name)==0) { + return; + } + printf(" %s:\n", maptype); + printf(" name: %s\n", texture->name); + printf(" flags: %X\n", texture->flags); + printf(" percent: %f\n", texture->percent); + printf(" blur: %f\n", texture->blur); + printf(" scale: (%f, %f)\n", texture->scale[0], texture->scale[1]); + printf(" offset: (%f, %f)\n", texture->offset[0], texture->offset[1]); + printf(" rotation: %f\n", texture->rotation); + printf(" tint_1: (%f, %f, %f)\n", + texture->tint_1[0], texture->tint_1[1], texture->tint_1[2]); + printf(" tint_2: (%f, %f, %f)\n", + texture->tint_2[0], texture->tint_2[1], texture->tint_2[2]); + printf(" tint_r: (%f, %f, %f)\n", + texture->tint_r[0], texture->tint_r[1], texture->tint_r[2]); + printf(" tint_g: (%f, %f, %f)\n", + texture->tint_g[0], texture->tint_g[1], texture->tint_g[2]); + printf(" tint_b: (%f, %f, %f)\n", + texture->tint_b[0], texture->tint_b[1], texture->tint_b[2]); } @@ -260,33 +280,52 @@ texture_map_read(Lib3dsTextureMap *map, FILE *f) void lib3ds_material_dump(Lib3dsMaterial *material) { - ASSERT(material); - printf(" %s\n", material->name); - printf(" Ambient: %1.2f %1.2f %1.2f\n", - material->ambient[0],material->ambient[1], material->ambient[2]); - printf(" Diffuse: %1.2f %1.2f %1.2f\n", - material->diffuse[0],material->diffuse[1], material->diffuse[2]); - printf(" Specular: %1.2f %1.2f %1.2f\n", - material->specular[0],material->specular[1], material->specular[2]); - printf(" Shininess: %1.2f\n", material->shininess); - printf(" Shin. Strength: %1.2f\n", material->shin_strength); - printf(" Shading: %d\n", material->shading); - if (*material->texture1_map.name) - { - printf(" Texture 1 Map: '%s'\n", material->texture1_map.name); - } - if (*material->texture1_mask.name) - { - printf(" Texture 1 Mask: '%s'\n", material->texture1_mask.name); - } - if (*material->texture2_map.name) - { - printf(" Texture 2 Map: '%s'\n", material->texture2_map.name); - } - if (*material->texture2_mask.name) - { - printf(" Texture 2 Mask: '%s'\n", material->texture2_mask.name); - } + ASSERT(material); + printf(" name: %s\n", material->name); + printf(" ambient: (%f, %f, %f)\n", + material->ambient[0], material->ambient[1], material->ambient[2]); + printf(" diffuse: (%f, %f, %f)\n", + material->diffuse[0], material->diffuse[1], material->diffuse[2]); + printf(" specular: (%f, %f, %f)\n", + material->specular[0], material->specular[1], material->specular[2]); + printf(" shininess: %f\n", material->shininess); + printf(" shin_strength: %f\n", material->shin_strength); + printf(" use_blur: %s\n", material->use_blur ? "yes" : "no"); + printf(" blur: %f\n", material->blur); + printf(" falloff: %f\n", material->falloff); + printf(" additive: %s\n", material->additive ? "yes" : "no"); + printf(" use_falloff: %s\n", material->use_falloff ? "yes" : "no"); + printf(" self_illum: %s\n", material->self_illum ? "yes" : "no"); + printf(" shading: %d\n", material->shading); + printf(" soften: %s\n", material->soften ? "yes" : "no"); + printf(" face_map: %s\n", material->face_map ? "yes" : "no"); + printf(" two_sided: %s\n", material->two_sided ? "yes" : "no"); + printf(" map_decal: %s\n", material->map_decal ? "yes" : "no"); + printf(" use_wire: %s\n", material->use_wire ? "yes" : "no"); + printf(" use_wire_abs: %s\n", material->use_wire_abs ? "yes" : "no"); + printf(" wire_size: %f\n", material->wire_size); + texture_dump("texture1_map", &material->texture1_map); + texture_dump("texture1_mask", &material->texture1_mask); + texture_dump("texture2_map", &material->texture2_map); + texture_dump("texture2_mask", &material->texture2_mask); + texture_dump("opacity_map", &material->opacity_map); + texture_dump("opacity_mask", &material->opacity_mask); + texture_dump("bump_map", &material->bump_map); + texture_dump("bump_mask", &material->bump_mask); + texture_dump("specular_map", &material->specular_map); + texture_dump("specular_mask", &material->specular_mask); + texture_dump("shininess_map", &material->shininess_map); + texture_dump("shininess_mask", &material->shininess_mask); + texture_dump("self_illum_map", &material->self_illum_map); + texture_dump("self_illum_mask", &material->self_illum_mask); + texture_dump("reflection_map", &material->reflection_map); + texture_dump("reflection_mask", &material->reflection_mask); + printf(" autorefl_map:\n"); + printf(" flags %X\n", material->autorefl_map.flags); + printf(" level %d\n", material->autorefl_map.level); + printf(" size %d\n", material->autorefl_map.size); + printf(" frame_step %d\n", material->autorefl_map.frame_step); + printf("\n"); } @@ -296,495 +335,465 @@ lib3ds_material_dump(Lib3dsMaterial *material) Lib3dsBool lib3ds_material_read(Lib3dsMaterial *material, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - ASSERT(material); - if (!lib3ds_chunk_read_start(&c, LIB3DS_MAT_ENTRY, f)) - { - return(LIB3DS_FALSE); - } + ASSERT(material); + if (!lib3ds_chunk_read_start(&c, LIB3DS_MAT_ENTRY, f)) { + return(LIB3DS_FALSE); + } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_MAT_NAME: { - case LIB3DS_MAT_NAME: - { - if (!lib3ds_string_read(material->name, 64, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_AMBIENT: - { - lib3ds_chunk_read_reset(&c, f); - if (!color_read(material->ambient, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_DIFFUSE: - { - lib3ds_chunk_read_reset(&c, f); - if (!color_read(material->diffuse, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_SPECULAR: - { - lib3ds_chunk_read_reset(&c, f); - if (!color_read(material->specular, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_SHININESS: - { - lib3ds_chunk_read_reset(&c, f); - if (!int_percentage_read(&material->shininess, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_SHIN2PCT: - { - lib3ds_chunk_read_reset(&c, f); - if (!int_percentage_read(&material->shin_strength, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_TRANSPARENCY: - { - lib3ds_chunk_read_reset(&c, f); - if (!int_percentage_read(&material->transparency, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_XPFALL: - { - lib3ds_chunk_read_reset(&c, f); - if (!int_percentage_read(&material->falloff, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_USE_XPFALL: - { - material->use_falloff=LIB3DS_TRUE; - } - break; - case LIB3DS_MAT_REFBLUR: - { - lib3ds_chunk_read_reset(&c, f); - if (!int_percentage_read(&material->blur, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_USE_REFBLUR: - { - material->use_blur=LIB3DS_TRUE; - } - break; - case LIB3DS_MAT_SHADING: - { - material->shading=lib3ds_intw_read(f); - } - break; - case LIB3DS_MAT_SELF_ILLUM: - { - material->self_illum=LIB3DS_TRUE; - } - break; - case LIB3DS_MAT_TWO_SIDE: - { - material->two_sided=LIB3DS_TRUE; - } - break; - case LIB3DS_MAT_DECAL: - { - material->map_decal=LIB3DS_TRUE; - } - break; - case LIB3DS_MAT_ADDITIVE: - { - material->additive=LIB3DS_TRUE; - } - break; - case LIB3DS_MAT_FACEMAP: - { - material->face_map=LIB3DS_TRUE; - } - break; - case LIB3DS_MAT_PHONGSOFT: - { - material->soften=LIB3DS_TRUE; - } - break; - case LIB3DS_MAT_WIRE: - { - material->use_wire=LIB3DS_TRUE; - } - break; - case LIB3DS_MAT_WIREABS: - { - material->use_wire_abs=LIB3DS_TRUE; - } - break; - case LIB3DS_MAT_WIRE_SIZE: - { - material->wire_size=lib3ds_float_read(f); - } - break; - case LIB3DS_MAT_TEXMAP: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->texture1_map, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_TEXMASK: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->texture1_mask, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_TEX2MAP: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->texture2_map, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_TEX2MASK: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->texture2_mask, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_OPACMAP: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->opacity_map, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_OPACMASK: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->opacity_mask, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_BUMPMAP: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->bump_map, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_BUMPMASK: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->bump_mask, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_SPECMAP: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->specular_map, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_SPECMASK: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->specular_mask, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_SHINMAP: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->shininess_map, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_SHINMASK: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->shininess_mask, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_SELFIMAP: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->self_illum_map, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_SELFIMASK: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->self_illum_mask, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_REFLMAP: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->reflection_map, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_REFLMASK: - { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->reflection_mask, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MAT_ACUBIC: - { - lib3ds_intb_read(f); - material->autorefl_map.level=lib3ds_intb_read(f); - material->autorefl_map.flags=lib3ds_intw_read(f); - material->autorefl_map.size=lib3ds_intd_read(f); - material->autorefl_map.frame_step=lib3ds_intd_read(f); - } - break; - default: - lib3ds_chunk_unknown(chunk); + if (!lib3ds_string_read(material->name, 64, f)) { + return(LIB3DS_FALSE); + } + lib3ds_chunk_dump_info(" NAME=%s", material->name); } + break; + case LIB3DS_MAT_AMBIENT: + { + lib3ds_chunk_read_reset(&c, f); + if (!color_read(material->ambient, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_DIFFUSE: + { + lib3ds_chunk_read_reset(&c, f); + if (!color_read(material->diffuse, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_SPECULAR: + { + lib3ds_chunk_read_reset(&c, f); + if (!color_read(material->specular, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_SHININESS: + { + lib3ds_chunk_read_reset(&c, f); + if (!int_percentage_read(&material->shininess, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_SHIN2PCT: + { + lib3ds_chunk_read_reset(&c, f); + if (!int_percentage_read(&material->shin_strength, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_TRANSPARENCY: + { + lib3ds_chunk_read_reset(&c, f); + if (!int_percentage_read(&material->transparency, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_XPFALL: + { + lib3ds_chunk_read_reset(&c, f); + if (!int_percentage_read(&material->falloff, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_USE_XPFALL: + { + material->use_falloff=LIB3DS_TRUE; + } + break; + case LIB3DS_MAT_REFBLUR: + { + lib3ds_chunk_read_reset(&c, f); + if (!int_percentage_read(&material->blur, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_USE_REFBLUR: + { + material->use_blur=LIB3DS_TRUE; + } + break; + case LIB3DS_MAT_SHADING: + { + material->shading=lib3ds_intw_read(f); + } + break; + case LIB3DS_MAT_SELF_ILLUM: + { + material->self_illum=LIB3DS_TRUE; + } + break; + case LIB3DS_MAT_TWO_SIDE: + { + material->two_sided=LIB3DS_TRUE; + } + break; + case LIB3DS_MAT_DECAL: + { + material->map_decal=LIB3DS_TRUE; + } + break; + case LIB3DS_MAT_ADDITIVE: + { + material->additive=LIB3DS_TRUE; + } + break; + case LIB3DS_MAT_FACEMAP: + { + material->face_map=LIB3DS_TRUE; + } + break; + case LIB3DS_MAT_PHONGSOFT: + { + material->soften=LIB3DS_TRUE; + } + break; + case LIB3DS_MAT_WIRE: + { + material->use_wire=LIB3DS_TRUE; + } + break; + case LIB3DS_MAT_WIREABS: + { + material->use_wire_abs=LIB3DS_TRUE; + } + break; + case LIB3DS_MAT_WIRE_SIZE: + { + material->wire_size=lib3ds_float_read(f); + } + break; + case LIB3DS_MAT_TEXMAP: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->texture1_map, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_TEXMASK: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->texture1_mask, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_TEX2MAP: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->texture2_map, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_TEX2MASK: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->texture2_mask, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_OPACMAP: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->opacity_map, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_OPACMASK: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->opacity_mask, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_BUMPMAP: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->bump_map, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_BUMPMASK: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->bump_mask, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_SPECMAP: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->specular_map, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_SPECMASK: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->specular_mask, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_SHINMAP: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->shininess_map, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_SHINMASK: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->shininess_mask, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_SELFIMAP: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->self_illum_map, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_SELFIMASK: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->self_illum_mask, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_REFLMAP: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->reflection_map, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_REFLMASK: + { + lib3ds_chunk_read_reset(&c, f); + if (!texture_map_read(&material->reflection_mask, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MAT_ACUBIC: + { + lib3ds_intb_read(f); + material->autorefl_map.level=lib3ds_intb_read(f); + material->autorefl_map.flags=lib3ds_intw_read(f); + material->autorefl_map.size=lib3ds_intd_read(f); + material->autorefl_map.frame_step=lib3ds_intd_read(f); + } + break; + default: + lib3ds_chunk_unknown(chunk); } + } - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool color_write(Lib3dsRgba rgb, FILE *f) { - Lib3dsChunk c; + Lib3dsChunk c; - c.chunk=LIB3DS_COLOR_24; - c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[0]+0.5),f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[1]+0.5),f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[2]+0.5),f); + c.chunk=LIB3DS_COLOR_24; + c.size=9; + lib3ds_chunk_write(&c,f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[0]+0.5),f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[1]+0.5),f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[2]+0.5),f); - c.chunk=LIB3DS_LIN_COLOR_24; - c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[0]+0.5),f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[1]+0.5),f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[2]+0.5),f); + c.chunk=LIB3DS_LIN_COLOR_24; + c.size=9; + lib3ds_chunk_write(&c,f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[0]+0.5),f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[1]+0.5),f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[2]+0.5),f); - return(LIB3DS_TRUE); + return(LIB3DS_TRUE); } static Lib3dsBool int_percentage_write(Lib3dsFloat p, FILE *f) { - Lib3dsChunk c; + Lib3dsChunk c; - c.chunk=LIB3DS_INT_PERCENTAGE; - c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write((Lib3dsByte)floor(100.0*p+0.5),f); + c.chunk=LIB3DS_INT_PERCENTAGE; + c.size=8; + lib3ds_chunk_write(&c,f); + lib3ds_intw_write((Lib3dsByte)floor(100.0*p+0.5),f); - return(LIB3DS_TRUE); + return(LIB3DS_TRUE); } static Lib3dsBool texture_map_write(Lib3dsWord chunk, Lib3dsTextureMap *map, FILE *f) { - Lib3dsChunk c; + Lib3dsChunk c; - if (strlen(map->name)==0) - { - return(LIB3DS_TRUE); - } - c.chunk=chunk; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - - int_percentage_write(map->percent,f); - - { /*---- LIB3DS_MAT_MAPNAME ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAPNAME; - c.size=6+strlen(map->name)+1; - lib3ds_chunk_write(&c,f); - lib3ds_string_write(map->name,f); - } - - { /*---- LIB3DS_MAT_MAP_TILING ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_TILING; - c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_word_write((Lib3dsWord)map->flags,f); - } - - { /*---- LIB3DS_MAT_MAP_TEXBLUR ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_TEXBLUR; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->blur,f); - } - - { /*---- LIB3DS_MAT_MAP_USCALE ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_USCALE; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->scale[0],f); - } - - { /*---- LIB3DS_MAT_MAP_VSCALE ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_VSCALE; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->scale[1],f); - } - - { /*---- LIB3DS_MAT_MAP_UOFFSET ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_UOFFSET; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->offset[0],f); - } - - { /*---- LIB3DS_MAT_MAP_VOFFSET ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_VOFFSET; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->offset[1],f); - } - - { /*---- LIB3DS_MAT_MAP_ANG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_ANG; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->rotation,f); - } - - { /*---- LIB3DS_MAT_MAP_COL1 ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_COL1; - c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[0]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[1]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[2]+0.5), f); - } - - { /*---- LIB3DS_MAT_MAP_COL2 ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_COL2; - c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[0]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[1]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[2]+0.5), f); - } - - { /*---- LIB3DS_MAT_MAP_RCOL ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_RCOL; - c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[0]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[1]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[2]+0.5), f); - } - - { /*---- LIB3DS_MAT_MAP_GCOL ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_GCOL; - c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[0]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[1]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[2]+0.5), f); - } - - { /*---- LIB3DS_MAT_MAP_BCOL ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_MAP_BCOL; - c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[0]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[1]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[2]+0.5), f); - } - - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (strlen(map->name)==0) { return(LIB3DS_TRUE); + } + c.chunk=chunk; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + + int_percentage_write(map->percent,f); + + { /*---- LIB3DS_MAT_MAPNAME ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAPNAME; + c.size=6+strlen(map->name)+1; + lib3ds_chunk_write(&c,f); + lib3ds_string_write(map->name,f); + } + + { /*---- LIB3DS_MAT_MAP_TILING ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_TILING; + c.size=8; + lib3ds_chunk_write(&c,f); + lib3ds_word_write((Lib3dsWord)map->flags,f); + } + + { /*---- LIB3DS_MAT_MAP_TEXBLUR ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_TEXBLUR; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(map->blur,f); + } + + { /*---- LIB3DS_MAT_MAP_USCALE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_USCALE; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(map->scale[0],f); + } + + { /*---- LIB3DS_MAT_MAP_VSCALE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_VSCALE; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(map->scale[1],f); + } + + { /*---- LIB3DS_MAT_MAP_UOFFSET ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_UOFFSET; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(map->offset[0],f); + } + + { /*---- LIB3DS_MAT_MAP_VOFFSET ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_VOFFSET; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(map->offset[1],f); + } + + { /*---- LIB3DS_MAT_MAP_ANG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_ANG; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(map->rotation,f); + } + + { /*---- LIB3DS_MAT_MAP_COL1 ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_COL1; + c.size=9; + lib3ds_chunk_write(&c,f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[0]+0.5), f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[1]+0.5), f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[2]+0.5), f); + } + + { /*---- LIB3DS_MAT_MAP_COL2 ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_COL2; + c.size=9; + lib3ds_chunk_write(&c,f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[0]+0.5), f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[1]+0.5), f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[2]+0.5), f); + } + + { /*---- LIB3DS_MAT_MAP_RCOL ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_RCOL; + c.size=9; + lib3ds_chunk_write(&c,f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[0]+0.5), f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[1]+0.5), f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[2]+0.5), f); + } + + { /*---- LIB3DS_MAT_MAP_GCOL ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_GCOL; + c.size=9; + lib3ds_chunk_write(&c,f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[0]+0.5), f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[1]+0.5), f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[2]+0.5), f); + } + + { /*---- LIB3DS_MAT_MAP_BCOL ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_MAP_BCOL; + c.size=9; + lib3ds_chunk_write(&c,f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[0]+0.5), f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[1]+0.5), f); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[2]+0.5), f); + } + + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -794,252 +803,224 @@ texture_map_write(Lib3dsWord chunk, Lib3dsTextureMap *map, FILE *f) Lib3dsBool lib3ds_material_write(Lib3dsMaterial *material, FILE *f) { + Lib3dsChunk c; + + c.chunk=LIB3DS_MAT_ENTRY; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + + { /*---- LIB3DS_MAT_NAME ----*/ Lib3dsChunk c; + c.chunk=LIB3DS_MAT_NAME; + c.size=6+strlen(material->name)+1; + lib3ds_chunk_write(&c,f); + lib3ds_string_write(material->name,f); + } - c.chunk=LIB3DS_MAT_ENTRY; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } + { /*---- LIB3DS_MAT_AMBIENT ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_AMBIENT; + c.size=24; + lib3ds_chunk_write(&c,f); + color_write(material->ambient,f); + } - { /*---- LIB3DS_MAT_NAME ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_NAME; - c.size=6+strlen(material->name)+1; - lib3ds_chunk_write(&c,f); - lib3ds_string_write(material->name,f); - } + { /*---- LIB3DS_MAT_DIFFUSE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_DIFFUSE; + c.size=24; + lib3ds_chunk_write(&c,f); + color_write(material->diffuse,f); + } - { /*---- LIB3DS_MAT_AMBIENT ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_AMBIENT; - c.size=24; - lib3ds_chunk_write(&c,f); - color_write(material->ambient,f); - } + { /*---- LIB3DS_MAT_SPECULAR ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_SPECULAR; + c.size=24; + lib3ds_chunk_write(&c,f); + color_write(material->specular,f); + } - { /*---- LIB3DS_MAT_DIFFUSE ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_DIFFUSE; - c.size=24; - lib3ds_chunk_write(&c,f); - color_write(material->diffuse,f); - } + { /*---- LIB3DS_MAT_SHININESS ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_SHININESS; + c.size=14; + lib3ds_chunk_write(&c,f); + int_percentage_write(material->shininess,f); + } - { /*---- LIB3DS_MAT_SPECULAR ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_SPECULAR; - c.size=24; - lib3ds_chunk_write(&c,f); - color_write(material->specular,f); - } + { /*---- LIB3DS_MAT_SHIN2PCT ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_SHIN2PCT; + c.size=14; + lib3ds_chunk_write(&c,f); + int_percentage_write(material->shin_strength,f); + } - { /*---- LIB3DS_MAT_SHININESS ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_SHININESS; - c.size=14; - lib3ds_chunk_write(&c,f); - int_percentage_write(material->shininess,f); - } + { /*---- LIB3DS_MAT_TRANSPARENCY ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_TRANSPARENCY; + c.size=14; + lib3ds_chunk_write(&c,f); + int_percentage_write(material->transparency,f); + } - { /*---- LIB3DS_MAT_SHIN2PCT ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_SHIN2PCT; - c.size=14; - lib3ds_chunk_write(&c,f); - int_percentage_write(material->shin_strength,f); - } + { /*---- LIB3DS_MAT_XPFALL ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_XPFALL; + c.size=14; + lib3ds_chunk_write(&c,f); + int_percentage_write(material->falloff,f); + } - { /*---- LIB3DS_MAT_TRANSPARENCY ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_TRANSPARENCY; - c.size=14; - lib3ds_chunk_write(&c,f); - int_percentage_write(material->transparency,f); - } + if (material->use_falloff) { /*---- LIB3DS_MAT_USE_XPFALL ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_USE_XPFALL; + c.size=6; + lib3ds_chunk_write(&c,f); + } - { /*---- LIB3DS_MAT_XPFALL ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_XPFALL; - c.size=14; - lib3ds_chunk_write(&c,f); - int_percentage_write(material->falloff,f); - } + { /*---- LIB3DS_MAT_SHADING ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_SHADING; + c.size=8; + lib3ds_chunk_write(&c,f); + lib3ds_intw_write(material->shading,f); + } - if (material->use_falloff) /*---- LIB3DS_MAT_USE_XPFALL ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_USE_XPFALL; - c.size=6; - lib3ds_chunk_write(&c,f); - } + { /*---- LIB3DS_MAT_REFBLUR ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_REFBLUR; + c.size=14; + lib3ds_chunk_write(&c,f); + int_percentage_write(material->blur,f); + } - { /*---- LIB3DS_MAT_SHADING ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_SHADING; - c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(material->shading,f); - } + if (material->use_blur) { /*---- LIB3DS_MAT_USE_REFBLUR ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_USE_REFBLUR; + c.size=6; + lib3ds_chunk_write(&c,f); + } - { /*---- LIB3DS_MAT_REFBLUR ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_REFBLUR; - c.size=14; - lib3ds_chunk_write(&c,f); - int_percentage_write(material->blur,f); - } + if (material->self_illum) { /*---- LIB3DS_MAT_SELF_ILLUM ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_SELF_ILLUM; + c.size=6; + lib3ds_chunk_write(&c,f); + } - if (material->use_blur) /*---- LIB3DS_MAT_USE_REFBLUR ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_USE_REFBLUR; - c.size=6; - lib3ds_chunk_write(&c,f); - } + if (material->two_sided) { /*---- LIB3DS_MAT_TWO_SIDE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_TWO_SIDE; + c.size=6; + lib3ds_chunk_write(&c,f); + } + + if (material->map_decal) { /*---- LIB3DS_MAT_DECAL ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_DECAL; + c.size=6; + lib3ds_chunk_write(&c,f); + } - if (material->self_illum) /*---- LIB3DS_MAT_SELF_ILLUM ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_SELF_ILLUM; - c.size=6; - lib3ds_chunk_write(&c,f); - } + if (material->additive) { /*---- LIB3DS_MAT_ADDITIVE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_ADDITIVE; + c.size=6; + lib3ds_chunk_write(&c,f); + } - if (material->two_sided) /*---- LIB3DS_MAT_TWO_SIDE ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_TWO_SIDE; - c.size=6; - lib3ds_chunk_write(&c,f); - } + if (material->use_wire) { /*---- LIB3DS_MAT_WIRE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_WIRE; + c.size=6; + lib3ds_chunk_write(&c,f); + } - if (material->map_decal) /*---- LIB3DS_MAT_DECAL ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_DECAL; - c.size=6; - lib3ds_chunk_write(&c,f); - } + if (material->use_wire_abs) { /*---- LIB3DS_MAT_WIREABS ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_WIREABS; + c.size=6; + lib3ds_chunk_write(&c,f); + } - if (material->additive) /*---- LIB3DS_MAT_ADDITIVE ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_ADDITIVE; - c.size=6; - lib3ds_chunk_write(&c,f); - } + { /*---- LIB3DS_MAT_WIRE_SIZE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_WIRE_SIZE; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(material->wire_size,f); + } - if (material->use_wire) /*---- LIB3DS_MAT_WIRE ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_WIRE; - c.size=6; - lib3ds_chunk_write(&c,f); - } + if (material->face_map) { /*---- LIB3DS_MAT_FACEMAP ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_FACEMAP; + c.size=6; + lib3ds_chunk_write(&c,f); + } - if (material->use_wire_abs) /*---- LIB3DS_MAT_WIREABS ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_WIREABS; - c.size=6; - lib3ds_chunk_write(&c,f); - } + if (material->soften) { /*---- LIB3DS_MAT_PHONGSOFT ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MAT_PHONGSOFT; + c.size=6; + lib3ds_chunk_write(&c,f); + } - { /*---- LIB3DS_MAT_WIRE_SIZE ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_WIRE_SIZE; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(material->wire_size,f); - } + if (!texture_map_write(LIB3DS_MAT_TEXMAP, &material->texture1_map, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_TEXMASK, &material->texture1_mask, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_TEX2MAP, &material->texture2_map, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_TEX2MASK, &material->texture2_mask, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_OPACMAP, &material->opacity_map, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_OPACMASK, &material->opacity_mask, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_BUMPMAP, &material->bump_map, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_BUMPMASK, &material->bump_mask, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_SPECMAP, &material->specular_map, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_SPECMASK, &material->specular_mask, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_SHINMAP, &material->shininess_map, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_SHINMASK, &material->shininess_mask, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_SELFIMAP, &material->self_illum_map, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_SELFIMASK, &material->self_illum_mask, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_REFLMAP, &material->reflection_map, f)) { + return(LIB3DS_FALSE); + } + if (!texture_map_write(LIB3DS_MAT_REFLMASK, &material->reflection_mask, f)) { + return(LIB3DS_FALSE); + } - if (material->face_map) /*---- LIB3DS_MAT_FACEMAP ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_FACEMAP; - c.size=6; - lib3ds_chunk_write(&c,f); - } - - if (material->soften) /*---- LIB3DS_MAT_PHONGSOFT ----*/ - { - Lib3dsChunk c; - c.chunk=LIB3DS_MAT_PHONGSOFT; - c.size=6; - lib3ds_chunk_write(&c,f); - } - - if (!texture_map_write(LIB3DS_MAT_TEXMAP, &material->texture1_map, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_TEXMASK, &material->texture1_mask, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_TEX2MAP, &material->texture2_map, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_TEX2MASK, &material->texture2_mask, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_OPACMAP, &material->opacity_map, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_OPACMASK, &material->opacity_mask, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_BUMPMAP, &material->bump_map, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_BUMPMASK, &material->bump_mask, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_SPECMAP, &material->specular_map, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_SPECMASK, &material->specular_mask, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_SHINMAP, &material->shininess_map, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_SHINMASK, &material->shininess_mask, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_SELFIMAP, &material->self_illum_map, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_SELFIMASK, &material->self_illum_mask, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_REFLMAP, &material->reflection_map, f)) - { - return(LIB3DS_FALSE); - } - if (!texture_map_write(LIB3DS_MAT_REFLMASK, &material->reflection_mask, f)) - { - return(LIB3DS_FALSE); - } - - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -1050,3 +1031,4 @@ lib3ds_material_write(Lib3dsMaterial *material, FILE *f) \sa _Lib3dsMaterial */ + diff --git a/src/osgPlugins/lib3ds/matrix.cpp b/src/osgPlugins/lib3ds/matrix.cpp index 9b8690b82..ba9a40ca3 100644 --- a/src/osgPlugins/lib3ds/matrix.cpp +++ b/src/osgPlugins/lib3ds/matrix.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -26,6 +26,7 @@ #include #include + /*! * \defgroup matrix Matrix Mathematics * @@ -36,18 +37,18 @@ * \ingroup matrix */ + /*! * \ingroup matrix */ void lib3ds_matrix_zero(Lib3dsMatrix m) { - int i,j; + int i,j; - for (i=0; i<4; i++) - { - for (j=0; j<4; j++) m[i][j]=0.0f; - } + for (i=0; i<4; i++) { + for (j=0; j<4; j++) m[i][j]=0.0f; + } } @@ -57,13 +58,12 @@ lib3ds_matrix_zero(Lib3dsMatrix m) void lib3ds_matrix_identity(Lib3dsMatrix m) { - int i,j; + int i,j; - for (i=0; i<4; i++) - { - for (j=0; j<4; j++) m[i][j]=0.0; - } - for (i=0; i<4; i++) m[i][i]=1.0; + for (i=0; i<4; i++) { + for (j=0; j<4; j++) m[i][j]=0.0; + } + for (i=0; i<4; i++) m[i][i]=1.0; } @@ -73,43 +73,39 @@ lib3ds_matrix_identity(Lib3dsMatrix m) void lib3ds_matrix_copy(Lib3dsMatrix dest, Lib3dsMatrix src) { - memcpy(dest, src, sizeof(Lib3dsMatrix)); + memcpy(dest, src, sizeof(Lib3dsMatrix)); } /*! * \ingroup matrix */ -void +void lib3ds_matrix_neg(Lib3dsMatrix m) { - int i,j; + int i,j; - for (j=0; j<4; j++) - { - for (i=0; i<4; i++) - { - m[j][i]=-m[j][i]; - } + for (j=0; j<4; j++) { + for (i=0; i<4; i++) { + m[j][i]=-m[j][i]; } + } } /*! * \ingroup matrix */ -void +void lib3ds_matrix_abs(Lib3dsMatrix m) { - int i,j; + int i,j; - for (j=0; j<4; j++) - { - for (i=0; i<4; i++) - { - m[j][i]=(Lib3dsFloat)fabs(m[j][i]); - } + for (j=0; j<4; j++) { + for (i=0; i<4; i++) { + m[j][i]=(Lib3dsFloat)fabs(m[j][i]); } + } } @@ -119,18 +115,16 @@ lib3ds_matrix_abs(Lib3dsMatrix m) void lib3ds_matrix_transpose(Lib3dsMatrix m) { - int i,j; - Lib3dsFloat swp; + int i,j; + Lib3dsFloat swp; - for (j=0; j<4; j++) - { - for (i=j+1; i<4; i++) - { - swp=m[j][i]; - m[j][i]=m[i][j]; - m[i][j]=swp; - } + for (j=0; j<4; j++) { + for (i=j+1; i<4; i++) { + swp=m[j][i]; + m[j][i]=m[i][j]; + m[i][j]=swp; } + } } @@ -140,15 +134,13 @@ lib3ds_matrix_transpose(Lib3dsMatrix m) void lib3ds_matrix_add(Lib3dsMatrix m, Lib3dsMatrix a, Lib3dsMatrix b) { - int i,j; + int i,j; - for (j=0; j<4; j++) - { - for (i=0; i<4; i++) - { - m[j][i]=a[j][i]+b[j][i]; - } + for (j=0; j<4; j++) { + for (i=0; i<4; i++) { + m[j][i]=a[j][i]+b[j][i]; } + } } @@ -158,15 +150,13 @@ lib3ds_matrix_add(Lib3dsMatrix m, Lib3dsMatrix a, Lib3dsMatrix b) void lib3ds_matrix_sub(Lib3dsMatrix m, Lib3dsMatrix a, Lib3dsMatrix b) { - int i,j; + int i,j; - for (j=0; j<4; j++) - { - for (i=0; i<4; i++) - { - m[j][i]=a[j][i]-b[j][i]; - } + for (j=0; j<4; j++) { + for (i=0; i<4; i++) { + m[j][i]=a[j][i]-b[j][i]; } + } } @@ -176,18 +166,16 @@ lib3ds_matrix_sub(Lib3dsMatrix m, Lib3dsMatrix a, Lib3dsMatrix b) void lib3ds_matrix_mul(Lib3dsMatrix m, Lib3dsMatrix a, Lib3dsMatrix b) { - int i,j,k; - Lib3dsFloat ab; + int i,j,k; + Lib3dsFloat ab; - for (j=0; j<4; j++) - { - for (i=0; i<4; i++) - { - ab=0.0f; - for (k=0; k<4; k++) ab+=a[k][i]*b[j][k]; - m[j][i]=ab; - } + for (j=0; j<4; j++) { + for (i=0; i<4; i++) { + ab=0.0f; + for (k=0; k<4; k++) ab+=a[k][i]*b[j][k]; + m[j][i]=ab; } + } } @@ -197,38 +185,36 @@ lib3ds_matrix_mul(Lib3dsMatrix m, Lib3dsMatrix a, Lib3dsMatrix b) void lib3ds_matrix_scalar(Lib3dsMatrix m, Lib3dsFloat k) { - int i,j; + int i,j; - for (j=0; j<4; j++) - { - for (i=0; i<4; i++) - { - m[j][i]*=k; - } + for (j=0; j<4; j++) { + for (i=0; i<4; i++) { + m[j][i]*=k; } + } } static Lib3dsFloat det2x2( -Lib3dsFloat a, Lib3dsFloat b, -Lib3dsFloat c, Lib3dsFloat d) + Lib3dsFloat a, Lib3dsFloat b, + Lib3dsFloat c, Lib3dsFloat d) { - return((a)*(d)-(b)*(c)); + return((a)*(d)-(b)*(c)); } static Lib3dsFloat det3x3( -Lib3dsFloat a1, Lib3dsFloat a2, Lib3dsFloat a3, -Lib3dsFloat b1, Lib3dsFloat b2, Lib3dsFloat b3, -Lib3dsFloat c1, Lib3dsFloat c2, Lib3dsFloat c3) + Lib3dsFloat a1, Lib3dsFloat a2, Lib3dsFloat a3, + Lib3dsFloat b1, Lib3dsFloat b2, Lib3dsFloat b3, + Lib3dsFloat c1, Lib3dsFloat c2, Lib3dsFloat c3) { - return( - a1*det2x2(b2,b3,c2,c3)- - b1*det2x2(a2,a3,c2,c3)+ - c1*det2x2(a2,a3,b2,b3) - ); + return( + a1*det2x2(b2,b3,c2,c3)- + b1*det2x2(a2,a3,c2,c3)+ + c1*det2x2(a2,a3,b2,b3) + ); } @@ -238,30 +224,30 @@ Lib3dsFloat c1, Lib3dsFloat c2, Lib3dsFloat c3) Lib3dsFloat lib3ds_matrix_det(Lib3dsMatrix m) { - Lib3dsFloat a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4; + Lib3dsFloat a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4; - a1 = m[0][0]; - b1 = m[1][0]; - c1 = m[2][0]; - d1 = m[3][0]; - a2 = m[0][1]; - b2 = m[1][1]; - c2 = m[2][1]; - d2 = m[3][1]; - a3 = m[0][2]; - b3 = m[1][2]; - c3 = m[2][2]; - d3 = m[3][2]; - a4 = m[0][3]; - b4 = m[1][3]; - c4 = m[2][3]; - d4 = m[3][3]; - return( - a1 * det3x3(b2, b3, b4, c2, c3, c4, d2, d3, d4)- - b1 * det3x3(a2, a3, a4, c2, c3, c4, d2, d3, d4)+ - c1 * det3x3(a2, a3, a4, b2, b3, b4, d2, d3, d4)- - d1 * det3x3(a2, a3, a4, b2, b3, b4, c2, c3, c4) - ); + a1 = m[0][0]; + b1 = m[1][0]; + c1 = m[2][0]; + d1 = m[3][0]; + a2 = m[0][1]; + b2 = m[1][1]; + c2 = m[2][1]; + d2 = m[3][1]; + a3 = m[0][2]; + b3 = m[1][2]; + c3 = m[2][2]; + d3 = m[3][2]; + a4 = m[0][3]; + b4 = m[1][3]; + c4 = m[2][3]; + d4 = m[3][3]; + return( + a1 * det3x3(b2, b3, b4, c2, c3, c4, d2, d3, d4)- + b1 * det3x3(a2, a3, a4, c2, c3, c4, d2, d3, d4)+ + c1 * det3x3(a2, a3, a4, b2, b3, b4, d2, d3, d4)- + d1 * det3x3(a2, a3, a4, b2, b3, b4, c2, c3, c4) + ); } @@ -271,159 +257,142 @@ lib3ds_matrix_det(Lib3dsMatrix m) void lib3ds_matrix_adjoint(Lib3dsMatrix m) { - Lib3dsFloat a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4; + Lib3dsFloat a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4; - a1 = m[0][0]; - b1 = m[1][0]; - c1 = m[2][0]; - d1 = m[3][0]; - a2 = m[0][1]; - b2 = m[1][1]; - c2 = m[2][1]; - d2 = m[3][1]; - a3 = m[0][2]; - b3 = m[1][2]; - c3 = m[2][2]; - d3 = m[3][2]; - a4 = m[0][3]; - b4 = m[1][3]; - c4 = m[2][3]; - d4 = m[3][3]; - m[0][0]= det3x3 (b2, b3, b4, c2, c3, c4, d2, d3, d4); - m[0][1]= -det3x3 (a2, a3, a4, c2, c3, c4, d2, d3, d4); - m[0][2]= det3x3 (a2, a3, a4, b2, b3, b4, d2, d3, d4); - m[0][3]= -det3x3 (a2, a3, a4, b2, b3, b4, c2, c3, c4); - m[1][0]= -det3x3 (b1, b3, b4, c1, c3, c4, d1, d3, d4); - m[1][1]= det3x3 (a1, a3, a4, c1, c3, c4, d1, d3, d4); - m[1][2]= -det3x3 (a1, a3, a4, b1, b3, b4, d1, d3, d4); - m[1][3]= det3x3 (a1, a3, a4, b1, b3, b4, c1, c3, c4); - m[2][0]= det3x3 (b1, b2, b4, c1, c2, c4, d1, d2, d4); - m[2][1]= -det3x3 (a1, a2, a4, c1, c2, c4, d1, d2, d4); - m[2][2]= det3x3 (a1, a2, a4, b1, b2, b4, d1, d2, d4); - m[2][3]= -det3x3 (a1, a2, a4, b1, b2, b4, c1, c2, c4); - m[3][0]= -det3x3 (b1, b2, b3, c1, c2, c3, d1, d2, d3); - m[3][1]= det3x3 (a1, a2, a3, c1, c2, c3, d1, d2, d3); - m[3][2]= -det3x3 (a1, a2, a3, b1, b2, b3, d1, d2, d3); - m[3][3]= det3x3 (a1, a2, a3, b1, b2, b3, c1, c2, c3); + a1 = m[0][0]; + b1 = m[1][0]; + c1 = m[2][0]; + d1 = m[3][0]; + a2 = m[0][1]; + b2 = m[1][1]; + c2 = m[2][1]; + d2 = m[3][1]; + a3 = m[0][2]; + b3 = m[1][2]; + c3 = m[2][2]; + d3 = m[3][2]; + a4 = m[0][3]; + b4 = m[1][3]; + c4 = m[2][3]; + d4 = m[3][3]; + m[0][0]= det3x3 (b2, b3, b4, c2, c3, c4, d2, d3, d4); + m[0][1]= -det3x3 (a2, a3, a4, c2, c3, c4, d2, d3, d4); + m[0][2]= det3x3 (a2, a3, a4, b2, b3, b4, d2, d3, d4); + m[0][3]= -det3x3 (a2, a3, a4, b2, b3, b4, c2, c3, c4); + m[1][0]= -det3x3 (b1, b3, b4, c1, c3, c4, d1, d3, d4); + m[1][1]= det3x3 (a1, a3, a4, c1, c3, c4, d1, d3, d4); + m[1][2]= -det3x3 (a1, a3, a4, b1, b3, b4, d1, d3, d4); + m[1][3]= det3x3 (a1, a3, a4, b1, b3, b4, c1, c3, c4); + m[2][0]= det3x3 (b1, b2, b4, c1, c2, c4, d1, d2, d4); + m[2][1]= -det3x3 (a1, a2, a4, c1, c2, c4, d1, d2, d4); + m[2][2]= det3x3 (a1, a2, a4, b1, b2, b4, d1, d2, d4); + m[2][3]= -det3x3 (a1, a2, a4, b1, b2, b4, c1, c2, c4); + m[3][0]= -det3x3 (b1, b2, b3, c1, c2, c3, d1, d2, d3); + m[3][1]= det3x3 (a1, a2, a3, c1, c2, c3, d1, d2, d3); + m[3][2]= -det3x3 (a1, a2, a3, b1, b2, b3, d1, d2, d3); + m[3][3]= det3x3 (a1, a2, a3, b1, b2, b3, c1, c2, c3); } /*! * \ingroup matrix * - * GGemsII, K.Wu, Fast Matrix Inversion + * GGemsII, K.Wu, Fast Matrix Inversion */ Lib3dsBool lib3ds_matrix_inv(Lib3dsMatrix m) -{ - int i,j,k; - int pvt_i[4], pvt_j[4]; /* Locations of pivot elements */ - Lib3dsFloat pvt_val; /* Value of current pivot element */ - Lib3dsFloat hold; /* Temporary storage */ - Lib3dsFloat determinat; +{ + int i,j,k; + int pvt_i[4], pvt_j[4]; /* Locations of pivot elements */ + Lib3dsFloat pvt_val; /* Value of current pivot element */ + Lib3dsFloat hold; /* Temporary storage */ + Lib3dsFloat determinat; - determinat = 1.0f; - for (k=0; k<4; k++) - { - /* Locate k'th pivot element */ - pvt_val=m[k][k]; /* Initialize for search */ - pvt_i[k]=k; - pvt_j[k]=k; - for (i=k; i<4; i++) - { - for (j=k; j<4; j++) - { - if (fabs(m[i][j]) > fabs(pvt_val)) - { - pvt_i[k]=i; - pvt_j[k]=j; - pvt_val=m[i][j]; - } - } + determinat = 1.0f; + for (k=0; k<4; k++) { + /* Locate k'th pivot element */ + pvt_val=m[k][k]; /* Initialize for search */ + pvt_i[k]=k; + pvt_j[k]=k; + for (i=k; i<4; i++) { + for (j=k; j<4; j++) { + if (fabs(m[i][j]) > fabs(pvt_val)) { + pvt_i[k]=i; + pvt_j[k]=j; + pvt_val=m[i][j]; } - - /* Product of pivots, gives determinant when finished */ - determinat*=pvt_val; - if (fabs(determinat)=0; k--) /* Don't need to work with 1 by 1 corner*/ - { - i=pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */ - if (i!=k) /* If rows are different */ - { - for(j=0; j<4; j++) - { - hold = m[k][j]; - m[k][j]=-m[i][j]; - m[i][j]=hold; - } - } - - j=pvt_i[k]; /* Columns to swap correspond to pivot ROW */ - if (j!=k) /* If columns are different */ - for (i=0; i<4; i++) - { - hold=m[i][k]; - m[i][k]=-m[i][j]; - m[i][j]=hold; - } + /* Product of pivots, gives determinant when finished */ + determinat*=pvt_val; + if (fabs(determinat)=0; k--) { /* Don't need to work with 1 by 1 corner*/ + i=pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */ + if (i!=k) { /* If rows are different */ + for(j=0; j<4; j++) { + hold = m[k][j]; + m[k][j]=-m[i][j]; + m[i][j]=hold; + } + } + + j=pvt_i[k]; /* Columns to swap correspond to pivot ROW */ + if (j!=k) /* If columns are different */ + for (i=0; i<4; i++) { + hold=m[i][k]; + m[i][k]=-m[i][j]; + m[i][j]=hold; + } + } + return(LIB3DS_TRUE); } @@ -433,12 +402,11 @@ lib3ds_matrix_inv(Lib3dsMatrix m) void lib3ds_matrix_translate_xyz(Lib3dsMatrix m, Lib3dsFloat x, Lib3dsFloat y, Lib3dsFloat z) { - int i; - - for (i=0; i<3; i++) - { - m[3][i]+= m[0][i]*x + m[1][i]*y + m[2][i]*z; - } + int i; + + for (i=0; i<3; i++) { + m[3][i]+= m[0][i]*x + m[1][i]*y + m[2][i]*z; + } } @@ -448,12 +416,11 @@ lib3ds_matrix_translate_xyz(Lib3dsMatrix m, Lib3dsFloat x, Lib3dsFloat y, Lib3ds void lib3ds_matrix_translate(Lib3dsMatrix m, Lib3dsVector t) { - int i; - - for (i=0; i<3; i++) - { - m[3][i]+= m[0][i]*t[0] + m[1][i]*t[1] + m[2][i]*t[2]; - } + int i; + + for (i=0; i<3; i++) { + m[3][i]+= m[0][i]*t[0] + m[1][i]*t[1] + m[2][i]*t[2]; + } } @@ -463,14 +430,13 @@ lib3ds_matrix_translate(Lib3dsMatrix m, Lib3dsVector t) void lib3ds_matrix_scale_xyz(Lib3dsMatrix m, Lib3dsFloat x, Lib3dsFloat y, Lib3dsFloat z) { - int i; + int i; - for (i=0; i<4; i++) - { - m[0][i]*=x; - m[1][i]*=y; - m[2][i]*=z; - } + for (i=0; i<4; i++) { + m[0][i]*=x; + m[1][i]*=y; + m[2][i]*=z; + } } @@ -480,14 +446,13 @@ lib3ds_matrix_scale_xyz(Lib3dsMatrix m, Lib3dsFloat x, Lib3dsFloat y, Lib3dsFloa void lib3ds_matrix_scale(Lib3dsMatrix m, Lib3dsVector s) { - int i; + int i; - for (i=0; i<4; i++) - { - m[0][i]*=s[0]; - m[1][i]*=s[1]; - m[2][i]*=s[2]; - } + for (i=0; i<4; i++) { + m[0][i]*=s[0]; + m[1][i]*=s[1]; + m[2][i]*=s[2]; + } } @@ -497,21 +462,21 @@ lib3ds_matrix_scale(Lib3dsMatrix m, Lib3dsVector s) void lib3ds_matrix_rotate_x(Lib3dsMatrix m, Lib3dsFloat phi) { - Lib3dsFloat SinPhi,CosPhi; - Lib3dsFloat a1[4],a2[4]; + Lib3dsFloat SinPhi,CosPhi; + Lib3dsFloat a1[4],a2[4]; - SinPhi=(Lib3dsFloat)sin(phi); - CosPhi=(Lib3dsFloat)cos(phi); - memcpy(a1,m[1],4*sizeof(Lib3dsFloat)); - memcpy(a2,m[2],4*sizeof(Lib3dsFloat)); - m[1][0]=CosPhi*a1[0]+SinPhi*a2[0]; - m[1][1]=CosPhi*a1[1]+SinPhi*a2[1]; - m[1][2]=CosPhi*a1[2]+SinPhi*a2[2]; - m[1][3]=CosPhi*a1[3]+SinPhi*a2[3]; - m[2][0]=-SinPhi*a1[0]+CosPhi*a2[0]; - m[2][1]=-SinPhi*a1[1]+CosPhi*a2[1]; - m[2][2]=-SinPhi*a1[2]+CosPhi*a2[2]; - m[2][3]=-SinPhi*a1[3]+CosPhi*a2[3]; + SinPhi=(Lib3dsFloat)sin(phi); + CosPhi=(Lib3dsFloat)cos(phi); + memcpy(a1,m[1],4*sizeof(Lib3dsFloat)); + memcpy(a2,m[2],4*sizeof(Lib3dsFloat)); + m[1][0]=CosPhi*a1[0]+SinPhi*a2[0]; + m[1][1]=CosPhi*a1[1]+SinPhi*a2[1]; + m[1][2]=CosPhi*a1[2]+SinPhi*a2[2]; + m[1][3]=CosPhi*a1[3]+SinPhi*a2[3]; + m[2][0]=-SinPhi*a1[0]+CosPhi*a2[0]; + m[2][1]=-SinPhi*a1[1]+CosPhi*a2[1]; + m[2][2]=-SinPhi*a1[2]+CosPhi*a2[2]; + m[2][3]=-SinPhi*a1[3]+CosPhi*a2[3]; } @@ -521,21 +486,21 @@ lib3ds_matrix_rotate_x(Lib3dsMatrix m, Lib3dsFloat phi) void lib3ds_matrix_rotate_y(Lib3dsMatrix m, Lib3dsFloat phi) { - Lib3dsFloat SinPhi,CosPhi; - Lib3dsFloat a0[4],a2[4]; + Lib3dsFloat SinPhi,CosPhi; + Lib3dsFloat a0[4],a2[4]; - SinPhi=(Lib3dsFloat)sin(phi); - CosPhi=(Lib3dsFloat)cos(phi); - memcpy(a0,m[0],4*sizeof(Lib3dsFloat)); - memcpy(a2,m[2],4*sizeof(Lib3dsFloat)); - m[0][0]=CosPhi*a0[0]-SinPhi*a2[0]; - m[0][1]=CosPhi*a0[1]-SinPhi*a2[1]; - m[0][2]=CosPhi*a0[2]-SinPhi*a2[2]; - m[0][3]=CosPhi*a0[3]-SinPhi*a2[3]; - m[2][0]=SinPhi*a0[0]+CosPhi*a2[0]; - m[2][1]=SinPhi*a0[1]+CosPhi*a2[1]; - m[2][2]=SinPhi*a0[2]+CosPhi*a2[2]; - m[2][3]=SinPhi*a0[3]+CosPhi*a2[3]; + SinPhi=(Lib3dsFloat)sin(phi); + CosPhi=(Lib3dsFloat)cos(phi); + memcpy(a0,m[0],4*sizeof(Lib3dsFloat)); + memcpy(a2,m[2],4*sizeof(Lib3dsFloat)); + m[0][0]=CosPhi*a0[0]-SinPhi*a2[0]; + m[0][1]=CosPhi*a0[1]-SinPhi*a2[1]; + m[0][2]=CosPhi*a0[2]-SinPhi*a2[2]; + m[0][3]=CosPhi*a0[3]-SinPhi*a2[3]; + m[2][0]=SinPhi*a0[0]+CosPhi*a2[0]; + m[2][1]=SinPhi*a0[1]+CosPhi*a2[1]; + m[2][2]=SinPhi*a0[2]+CosPhi*a2[2]; + m[2][3]=SinPhi*a0[3]+CosPhi*a2[3]; } @@ -545,21 +510,21 @@ lib3ds_matrix_rotate_y(Lib3dsMatrix m, Lib3dsFloat phi) void lib3ds_matrix_rotate_z(Lib3dsMatrix m, Lib3dsFloat phi) { - Lib3dsFloat SinPhi,CosPhi; - Lib3dsFloat a0[4],a1[4]; - - SinPhi=(Lib3dsFloat)sin(phi); - CosPhi=(Lib3dsFloat)cos(phi); - memcpy(a0,m[0],4*sizeof(Lib3dsFloat)); - memcpy(a1,m[1],4*sizeof(Lib3dsFloat)); - m[0][0]=CosPhi*a0[0]+SinPhi*a1[0]; - m[0][1]=CosPhi*a0[1]+SinPhi*a1[1]; - m[0][2]=CosPhi*a0[2]+SinPhi*a1[2]; - m[0][3]=CosPhi*a0[3]+SinPhi*a1[3]; - m[1][0]=-SinPhi*a0[0]+CosPhi*a1[0]; - m[1][1]=-SinPhi*a0[1]+CosPhi*a1[1]; - m[1][2]=-SinPhi*a0[2]+CosPhi*a1[2]; - m[1][3]=-SinPhi*a0[3]+CosPhi*a1[3]; + Lib3dsFloat SinPhi,CosPhi; + Lib3dsFloat a0[4],a1[4]; + + SinPhi=(Lib3dsFloat)sin(phi); + CosPhi=(Lib3dsFloat)cos(phi); + memcpy(a0,m[0],4*sizeof(Lib3dsFloat)); + memcpy(a1,m[1],4*sizeof(Lib3dsFloat)); + m[0][0]=CosPhi*a0[0]+SinPhi*a1[0]; + m[0][1]=CosPhi*a0[1]+SinPhi*a1[1]; + m[0][2]=CosPhi*a0[2]+SinPhi*a1[2]; + m[0][3]=CosPhi*a0[3]+SinPhi*a1[3]; + m[1][0]=-SinPhi*a0[0]+CosPhi*a1[0]; + m[1][1]=-SinPhi*a0[1]+CosPhi*a1[1]; + m[1][2]=-SinPhi*a0[2]+CosPhi*a1[2]; + m[1][3]=-SinPhi*a0[3]+CosPhi*a1[3]; } @@ -569,39 +534,37 @@ lib3ds_matrix_rotate_z(Lib3dsMatrix m, Lib3dsFloat phi) void lib3ds_matrix_rotate(Lib3dsMatrix m, Lib3dsQuat q) { - Lib3dsFloat s,xs,ys,zs,wx,wy,wz,xx,xy,xz,yy,yz,zz,l; - Lib3dsMatrix a,b; + Lib3dsFloat s,xs,ys,zs,wx,wy,wz,xx,xy,xz,yy,yz,zz,l; + Lib3dsMatrix a,b; - lib3ds_matrix_copy(a, m); + lib3ds_matrix_copy(a, m); - l=q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]; - if (fabs(l) #include #include -//#include +#include +#include #ifdef WITH_DMALLOC #include #endif + /*! * \defgroup mesh Meshes * * \author J.E. Hoffmann */ + static Lib3dsBool face_array_read(Lib3dsMesh *mesh, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; - int i; - int faces; + Lib3dsChunk c; + Lib3dsWord chunk; + int i; + int faces; - if (!lib3ds_chunk_read_start(&c, LIB3DS_FACE_ARRAY, f)) - { - return(LIB3DS_FALSE); + if (!lib3ds_chunk_read_start(&c, LIB3DS_FACE_ARRAY, f)) { + return(LIB3DS_FALSE); + } + lib3ds_mesh_free_face_list(mesh); + + faces=lib3ds_word_read(f); + if (faces) { + if (!lib3ds_mesh_new_face_list(mesh, faces)) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); } - lib3ds_mesh_free_face_list(mesh); + for (i=0; ifaceL[i].material, ""); + mesh->faceL[i].points[0]=lib3ds_word_read(f); + mesh->faceL[i].points[1]=lib3ds_word_read(f); + mesh->faceL[i].points[2]=lib3ds_word_read(f); + mesh->faceL[i].flags=lib3ds_word_read(f); + } + lib3ds_chunk_read_tell(&c, f); - faces=lib3ds_word_read(f); - if (faces) - { - if (!lib3ds_mesh_new_face_list(mesh, faces)) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - for (i=0; ifaceL[i].material, ""); - mesh->faceL[i].points[0]=lib3ds_word_read(f); - mesh->faceL[i].points[1]=lib3ds_word_read(f); - mesh->faceL[i].points[2]=lib3ds_word_read(f); - mesh->faceL[i].flags=lib3ds_word_read(f); - } - lib3ds_chunk_read_tell(&c, f); + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_SMOOTH_GROUP: + { + unsigned i; - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) - { - case LIB3DS_SMOOTH_GROUP: - { - unsigned i; - - for (i=0; ifaces; ++i) - { - mesh->faceL[i].smoothing=lib3ds_dword_read(f); - } - } - break; - case LIB3DS_MSH_MAT_GROUP: - { - char name[64]; - unsigned faces; - unsigned i; - unsigned index; - - if (!lib3ds_string_read(name, 64, f)) - { - return(LIB3DS_FALSE); - } - faces=lib3ds_word_read(f); - for (i=0; ifaces); - strcpy(mesh->faceL[index].material, name); - } - } - break; - case LIB3DS_MSH_BOXMAP: - { - char name[64]; - - if (!lib3ds_string_read(name, 64, f)) - { - return(LIB3DS_FALSE); - } - strcpy(mesh->box_map.front, name); - if (!lib3ds_string_read(name, 64, f)) - { - return(LIB3DS_FALSE); - } - strcpy(mesh->box_map.back, name); - if (!lib3ds_string_read(name, 64, f)) - { - return(LIB3DS_FALSE); - } - strcpy(mesh->box_map.left, name); - if (!lib3ds_string_read(name, 64, f)) - { - return(LIB3DS_FALSE); - } - strcpy(mesh->box_map.right, name); - if (!lib3ds_string_read(name, 64, f)) - { - return(LIB3DS_FALSE); - } - strcpy(mesh->box_map.top, name); - if (!lib3ds_string_read(name, 64, f)) - { - return(LIB3DS_FALSE); - } - strcpy(mesh->box_map.bottom, name); - } - break; - default: - lib3ds_chunk_unknown(chunk); + for (i=0; ifaces; ++i) { + mesh->faceL[i].smoothing=lib3ds_dword_read(f); } - } + } + break; + case LIB3DS_MSH_MAT_GROUP: + { + char name[64]; + unsigned faces; + unsigned i; + unsigned index; + if (!lib3ds_string_read(name, 64, f)) { + return(LIB3DS_FALSE); + } + faces=lib3ds_word_read(f); + for (i=0; ifaces); + strcpy(mesh->faceL[index].material, name); + } + } + break; + case LIB3DS_MSH_BOXMAP: + { + char name[64]; + + if (!lib3ds_string_read(name, 64, f)) { + return(LIB3DS_FALSE); + } + strcpy(mesh->box_map.front, name); + if (!lib3ds_string_read(name, 64, f)) { + return(LIB3DS_FALSE); + } + strcpy(mesh->box_map.back, name); + if (!lib3ds_string_read(name, 64, f)) { + return(LIB3DS_FALSE); + } + strcpy(mesh->box_map.left, name); + if (!lib3ds_string_read(name, 64, f)) { + return(LIB3DS_FALSE); + } + strcpy(mesh->box_map.right, name); + if (!lib3ds_string_read(name, 64, f)) { + return(LIB3DS_FALSE); + } + strcpy(mesh->box_map.top, name); + if (!lib3ds_string_read(name, 64, f)) { + return(LIB3DS_FALSE); + } + strcpy(mesh->box_map.bottom, name); + } + break; + default: + lib3ds_chunk_unknown(chunk); + } } - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + + } + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } @@ -157,19 +145,19 @@ face_array_read(Lib3dsMesh *mesh, FILE *f) Lib3dsMesh* lib3ds_mesh_new(const char *name) { - Lib3dsMesh *mesh; + Lib3dsMesh *mesh; - ASSERT(name); - ASSERT(strlen(name)<64); - - mesh=(Lib3dsMesh*)calloc(sizeof(Lib3dsMesh), 1); - if (!mesh) - { - return(0); - } - strcpy(mesh->name, name); - lib3ds_matrix_identity(mesh->matrix); - return(mesh); + ASSERT(name); + ASSERT(strlen(name)<64); + + mesh=(Lib3dsMesh*)calloc(sizeof(Lib3dsMesh), 1); + if (!mesh) { + return(0); + } + strcpy(mesh->name, name); + lib3ds_matrix_identity(mesh->matrix); + mesh->map_data.maptype=LIB3DS_MAP_NONE; + return(mesh); } @@ -179,12 +167,12 @@ lib3ds_mesh_new(const char *name) void lib3ds_mesh_free(Lib3dsMesh *mesh) { - lib3ds_mesh_free_point_list(mesh); - lib3ds_mesh_free_flag_list(mesh); - lib3ds_mesh_free_texel_list(mesh); - lib3ds_mesh_free_face_list(mesh); - memset(mesh, 0, sizeof(Lib3dsMesh)); - free(mesh); + lib3ds_mesh_free_point_list(mesh); + lib3ds_mesh_free_flag_list(mesh); + lib3ds_mesh_free_texel_list(mesh); + lib3ds_mesh_free_face_list(mesh); + memset(mesh, 0, sizeof(Lib3dsMesh)); + free(mesh); } @@ -194,22 +182,20 @@ lib3ds_mesh_free(Lib3dsMesh *mesh) Lib3dsBool lib3ds_mesh_new_point_list(Lib3dsMesh *mesh, Lib3dsDword points) { - ASSERT(mesh); - if (mesh->pointL) - { - ASSERT(mesh->points); - lib3ds_mesh_free_point_list(mesh); - } - ASSERT(!mesh->pointL && !mesh->points); - mesh->points=0; - mesh->pointL=(Lib3dsPoint*)calloc(sizeof(Lib3dsPoint)*points,1); - if (!mesh->pointL) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - mesh->points=points; - return(LIB3DS_TRUE); + ASSERT(mesh); + if (mesh->pointL) { + ASSERT(mesh->points); + lib3ds_mesh_free_point_list(mesh); + } + ASSERT(!mesh->pointL && !mesh->points); + mesh->points=0; + mesh->pointL=(Lib3dsPoint*)calloc(sizeof(Lib3dsPoint)*points,1); + if (!mesh->pointL) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + mesh->points=points; + return(LIB3DS_TRUE); } @@ -219,18 +205,16 @@ lib3ds_mesh_new_point_list(Lib3dsMesh *mesh, Lib3dsDword points) void lib3ds_mesh_free_point_list(Lib3dsMesh *mesh) { - ASSERT(mesh); - if (mesh->pointL) - { - ASSERT(mesh->points); - free(mesh->pointL); - mesh->pointL=0; - mesh->points=0; - } - else - { - ASSERT(!mesh->points); - } + ASSERT(mesh); + if (mesh->pointL) { + ASSERT(mesh->points); + free(mesh->pointL); + mesh->pointL=0; + mesh->points=0; + } + else { + ASSERT(!mesh->points); + } } @@ -240,22 +224,20 @@ lib3ds_mesh_free_point_list(Lib3dsMesh *mesh) Lib3dsBool lib3ds_mesh_new_flag_list(Lib3dsMesh *mesh, Lib3dsDword flags) { - ASSERT(mesh); - if (mesh->flagL) - { - ASSERT(mesh->flags); - lib3ds_mesh_free_flag_list(mesh); - } - ASSERT(!mesh->flagL && !mesh->flags); - mesh->flags=0; - mesh->flagL=(Lib3dsWord*)calloc(sizeof(Lib3dsWord)*flags,1); - if (!mesh->flagL) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - mesh->flags=flags; - return(LIB3DS_TRUE); + ASSERT(mesh); + if (mesh->flagL) { + ASSERT(mesh->flags); + lib3ds_mesh_free_flag_list(mesh); + } + ASSERT(!mesh->flagL && !mesh->flags); + mesh->flags=0; + mesh->flagL=(Lib3dsWord*)calloc(sizeof(Lib3dsWord)*flags,1); + if (!mesh->flagL) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + mesh->flags=flags; + return(LIB3DS_TRUE); } @@ -265,18 +247,16 @@ lib3ds_mesh_new_flag_list(Lib3dsMesh *mesh, Lib3dsDword flags) void lib3ds_mesh_free_flag_list(Lib3dsMesh *mesh) { - ASSERT(mesh); - if (mesh->flagL) - { - ASSERT(mesh->flags); - free(mesh->flagL); - mesh->flagL=0; - mesh->flags=0; - } - else - { - ASSERT(!mesh->flags); - } + ASSERT(mesh); + if (mesh->flagL) { + ASSERT(mesh->flags); + free(mesh->flagL); + mesh->flagL=0; + mesh->flags=0; + } + else { + ASSERT(!mesh->flags); + } } @@ -286,22 +266,20 @@ lib3ds_mesh_free_flag_list(Lib3dsMesh *mesh) Lib3dsBool lib3ds_mesh_new_texel_list(Lib3dsMesh *mesh, Lib3dsDword texels) { - ASSERT(mesh); - if (mesh->texelL) - { - ASSERT(mesh->texels); - lib3ds_mesh_free_texel_list(mesh); - } - ASSERT(!mesh->texelL && !mesh->texels); - mesh->texels=0; - mesh->texelL=(Lib3dsTexel*)calloc(sizeof(Lib3dsTexel)*texels,1); - if (!mesh->texelL) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - mesh->texels=texels; - return(LIB3DS_TRUE); + ASSERT(mesh); + if (mesh->texelL) { + ASSERT(mesh->texels); + lib3ds_mesh_free_texel_list(mesh); + } + ASSERT(!mesh->texelL && !mesh->texels); + mesh->texels=0; + mesh->texelL=(Lib3dsTexel*) calloc(sizeof(Lib3dsTexel)*texels,1); + if (!mesh->texelL) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + mesh->texels=texels; + return(LIB3DS_TRUE); } @@ -311,18 +289,16 @@ lib3ds_mesh_new_texel_list(Lib3dsMesh *mesh, Lib3dsDword texels) void lib3ds_mesh_free_texel_list(Lib3dsMesh *mesh) { - ASSERT(mesh); - if (mesh->texelL) - { - ASSERT(mesh->texels); - free(mesh->texelL); - mesh->texelL=0; - mesh->texels=0; - } - else - { - ASSERT(!mesh->texels); - } + ASSERT(mesh); + if (mesh->texelL) { + ASSERT(mesh->texels); + free(mesh->texelL); + mesh->texelL=0; + mesh->texels=0; + } + else { + ASSERT(!mesh->texels); + } } @@ -332,22 +308,20 @@ lib3ds_mesh_free_texel_list(Lib3dsMesh *mesh) Lib3dsBool lib3ds_mesh_new_face_list(Lib3dsMesh *mesh, Lib3dsDword faces) { - ASSERT(mesh); - if (mesh->faceL) - { - ASSERT(mesh->faces); - lib3ds_mesh_free_face_list(mesh); - } - ASSERT(!mesh->faceL && !mesh->faces); - mesh->faces=0; - mesh->faceL=(Lib3dsFace*)calloc(sizeof(Lib3dsFace)*faces,1); - if (!mesh->faceL) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - mesh->faces=faces; - return(LIB3DS_TRUE); + ASSERT(mesh); + if (mesh->faceL) { + ASSERT(mesh->faces); + lib3ds_mesh_free_face_list(mesh); + } + ASSERT(!mesh->faceL && !mesh->faces); + mesh->faces=0; + mesh->faceL=(Lib3dsFace*)calloc(sizeof(Lib3dsFace)*faces,1); + if (!mesh->faceL) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + mesh->faces=faces; + return(LIB3DS_TRUE); } @@ -357,80 +331,191 @@ lib3ds_mesh_new_face_list(Lib3dsMesh *mesh, Lib3dsDword faces) void lib3ds_mesh_free_face_list(Lib3dsMesh *mesh) { - ASSERT(mesh); - if (mesh->faceL) - { - ASSERT(mesh->faces); - free(mesh->faceL); - mesh->faceL=0; - mesh->faces=0; - } - else - { - ASSERT(!mesh->faces); - } + ASSERT(mesh); + if (mesh->faceL) { + ASSERT(mesh->faces); + free(mesh->faceL); + mesh->faceL=0; + mesh->faces=0; + } + else { + ASSERT(!mesh->faces); + } } +typedef struct _Lib3dsFaces Lib3dsFaces; + +struct _Lib3dsFaces { + Lib3dsFaces *next; + Lib3dsFace *face; +}; + + + + /*! * \ingroup mesh */ void lib3ds_mesh_bounding_box(Lib3dsMesh *mesh, Lib3dsVector min, Lib3dsVector max) { - unsigned i,j; - Lib3dsFloat v; + unsigned i,j; + Lib3dsFloat v; - if (!mesh->points) - { - lib3ds_vector_zero(min); - lib3ds_vector_zero(max); - return; + if (!mesh->points) { + lib3ds_vector_zero(min); + lib3ds_vector_zero(max); + return; + } + + lib3ds_vector_copy(min, mesh->pointL[0].pos); + lib3ds_vector_copy(max, mesh->pointL[0].pos); + for (i=1; ipoints; ++i) { + for (j=0; j<3; ++j) { + v=mesh->pointL[i].pos[j]; + if (vmax[j]) { + max[j]=v; + } } - - lib3ds_vector_copy(min, mesh->pointL[0].pos); - lib3ds_vector_copy(max, mesh->pointL[0].pos); - for (i=1; ipoints; ++i) - { - for (j=0; j<3; ++j) - { - v=mesh->pointL[i].pos[j]; - if (vmax[j]) - { - max[j]=v; - } - } - }; + }; } /*! + * Calculates the vertex normals corresponding to the smoothing group + * settings for each face of a mesh. + * + * \param mesh A pointer to the mesh to calculate the normals for. + * \param normalL A pointer to a buffer to store the calculated + * normals. The buffer must have the size: + * 3*sizeof(Lib3dsVector)*mesh->faces. + * + * To allocate the normal buffer do for example the following: + * \code + * Lib3dsVector *normalL = malloc(3*sizeof(Lib3dsVector)*mesh->faces); + * \endcode + * + * To access the normal of the i-th vertex of the j-th face do the + * following: + * \code + * normalL[3*j+i] + * \endcode + */ +void +lib3ds_mesh_calculate_normals(Lib3dsMesh *mesh, Lib3dsVector *normalL) +{ + Lib3dsFaces **fl; + Lib3dsFaces *fa; + unsigned i,j,k; + + if (!mesh->faces) { + return; + } + + fl=(Lib3dsFaces**)calloc(sizeof(Lib3dsFaces*),mesh->points); + ASSERT(fl); + fa=(Lib3dsFaces*)calloc(sizeof(Lib3dsFaces),3*mesh->faces); + ASSERT(fa); + k=0; + for (i=0; ifaces; ++i) { + Lib3dsFace *f=&mesh->faceL[i]; + for (j=0; j<3; ++j) { + Lib3dsFaces* l=&fa[k++]; + ASSERT(f->points[j]points); + l->face=f; + l->next=fl[f->points[j]]; + fl[f->points[j]]=l; + } + } + + for (i=0; ifaces; ++i) { + Lib3dsFace *f=&mesh->faceL[i]; + for (j=0; j<3; ++j) { + Lib3dsVector n,N[32]; + Lib3dsFaces *p; + int k,l; + int found; + + ASSERT(f->points[j]points); + + if (f->smoothing) { + lib3ds_vector_zero(n); + k=0; + for (p=fl[f->points[j]]; p; p=p->next) { + found=0; + for (l=0; lface->normal)-1.0)<1e-5) { + found=1; + break; + } + } + if (!found) { + if (f->smoothing & p->face->smoothing) { + lib3ds_vector_add(n,n, p->face->normal); + lib3ds_vector_copy(N[k], p->face->normal); + ++k; + } + } + } + } + else { + lib3ds_vector_copy(n, f->normal); + } + lib3ds_vector_normalize(n); + + lib3ds_vector_copy(normalL[3*i+j], n); + } + } + + free(fa); + free(fl); +} + + +/*! + * This function prints data associated with the specified mesh such as + * vertex and point lists. + * + * \param mesh Points to a mesh that you wish to view the data for. + * + * \return None + * + * \warning WIN32: Should only be used in a console window not in a GUI. + * * \ingroup mesh */ void lib3ds_mesh_dump(Lib3dsMesh *mesh) { - unsigned i; - Lib3dsVector p; + unsigned i; + Lib3dsVector p; - ASSERT(mesh); - printf(" %s vertices=%ld faces=%ld\n", - mesh->name, - mesh->points, - mesh->faces - ); - printf(" matrix:\n"); - lib3ds_matrix_dump(mesh->matrix); - printf(" point list:\n"); - for (i=0; ipoints; ++i) - { - lib3ds_vector_transform(p, mesh->matrix, mesh->pointL[i].pos); - printf (" %8f %8f %8f\n", p[0], p[1], p[2]); - } + ASSERT(mesh); + printf(" %s vertices=%ld faces=%ld\n", + mesh->name, + mesh->points, + mesh->faces + ); + printf(" matrix:\n"); + lib3ds_matrix_dump(mesh->matrix); + printf(" point list:\n"); + for (i=0; ipoints; ++i) { + lib3ds_vector_copy(p, mesh->pointL[i].pos); + printf (" %8f %8f %8f\n", p[0], p[1], p[2]); + } + printf(" facelist:\n"); + for (i=0; ipoints; ++i) { + printf (" %4d %4d %4d smoothing:%X\n", + mesh->faceL[i].points[0], + mesh->faceL[i].points[1], + mesh->faceL[i].points[2], + mesh->faceL[i].smoothing + ); + } } @@ -440,367 +525,325 @@ lib3ds_mesh_dump(Lib3dsMesh *mesh) Lib3dsBool lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_N_TRI_OBJECT, f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_read_start(&c, LIB3DS_N_TRI_OBJECT, f)) { + return(LIB3DS_FALSE); + } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_MESH_MATRIX: { - case LIB3DS_MESH_MATRIX: - { - int i,j; - - lib3ds_matrix_identity(mesh->matrix); - for (i=0; i<4; i++) - { - for (j=0; j<3; j++) - { - mesh->matrix[i][j]=lib3ds_float_read(f); - } - } + int i,j; + + lib3ds_matrix_identity(mesh->matrix); + for (i=0; i<4; i++) { + for (j=0; j<3; j++) { + mesh->matrix[i][j]=lib3ds_float_read(f); } - break; - case LIB3DS_MESH_COLOR: - { - mesh->color=lib3ds_byte_read(f); - } - break; - case LIB3DS_POINT_ARRAY: - { - unsigned i,j; - unsigned points; - - lib3ds_mesh_free_point_list(mesh); - points=lib3ds_word_read(f); - if (points) - { - if (!lib3ds_mesh_new_point_list(mesh, points)) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - for (i=0; ipoints; ++i) - { - for (j=0; j<3; ++j) - { - mesh->pointL[i].pos[j]=lib3ds_float_read(f); - } - } - ASSERT((!mesh->flags) || (mesh->points==mesh->flags)); - ASSERT((!mesh->texels) || (mesh->points==mesh->texels)); - } - } - break; - case LIB3DS_POINT_FLAG_ARRAY: - { - unsigned i; - unsigned flags; - - lib3ds_mesh_free_flag_list(mesh); - flags=lib3ds_word_read(f); - if (flags) - { - if (!lib3ds_mesh_new_flag_list(mesh, flags)) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - for (i=0; iflags; ++i) - { - mesh->flagL[i]=lib3ds_word_read(f); - } - ASSERT((!mesh->points) || (mesh->flags==mesh->points)); - ASSERT((!mesh->texels) || (mesh->flags==mesh->texels)); - } - } - break; - case LIB3DS_FACE_ARRAY: - { - lib3ds_chunk_read_reset(&c, f); - if (!face_array_read(mesh, f)) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_MESH_TEXTURE_INFO: - { - int i,j; - - for (i=0; i<2; ++i) - { - mesh->map_data.tile[i]=lib3ds_float_read(f); - } - for (i=0; i<3; ++i) - { - mesh->map_data.pos[i]=lib3ds_float_read(f); - } - mesh->map_data.scale=lib3ds_float_read(f); - - lib3ds_matrix_identity(mesh->map_data.matrix); - for (i=0; i<4; i++) - { - for (j=0; j<3; j++) - { - mesh->map_data.matrix[i][j]=lib3ds_float_read(f); - } - } - for (i=0; i<2; ++i) - { - mesh->map_data.planar_size[i]=lib3ds_float_read(f); - } - mesh->map_data.cylinder_height=lib3ds_float_read(f); - } - break; - case LIB3DS_TEX_VERTS: - { - unsigned i; - unsigned texels; - - lib3ds_mesh_free_texel_list(mesh); - texels=lib3ds_word_read(f); - if (texels) - { - if (!lib3ds_mesh_new_texel_list(mesh, texels)) - { - LIB3DS_ERROR_LOG; - return(LIB3DS_FALSE); - } - for (i=0; itexels; ++i) - { - mesh->texelL[i][0]=lib3ds_float_read(f); - mesh->texelL[i][1]=lib3ds_float_read(f); - } - ASSERT((!mesh->points) || (mesh->texels==mesh->points)); - ASSERT((!mesh->flags) || (mesh->texels==mesh->flags)); - } - } - break; - default: - lib3ds_chunk_unknown(chunk); + } } - } - { - unsigned j; - - for (j=0; jfaces; ++j) + break; + case LIB3DS_MESH_COLOR: { - ASSERT(mesh->faceL[j].points[0]points); - ASSERT(mesh->faceL[j].points[1]points); - ASSERT(mesh->faceL[j].points[2]points); - lib3ds_vector_normal( - mesh->faceL[j].normal, - mesh->pointL[mesh->faceL[j].points[0]].pos, - mesh->pointL[mesh->faceL[j].points[1]].pos, - mesh->pointL[mesh->faceL[j].points[2]].pos - ); + mesh->color=lib3ds_byte_read(f); } - } + break; + case LIB3DS_POINT_ARRAY: + { + unsigned i,j; + unsigned points; + + lib3ds_mesh_free_point_list(mesh); + points=lib3ds_word_read(f); + if (points) { + if (!lib3ds_mesh_new_point_list(mesh, points)) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + for (i=0; ipoints; ++i) { + for (j=0; j<3; ++j) { + mesh->pointL[i].pos[j]=lib3ds_float_read(f); + } + } + ASSERT((!mesh->flags) || (mesh->points==mesh->flags)); + ASSERT((!mesh->texels) || (mesh->points==mesh->texels)); + } + } + break; + case LIB3DS_POINT_FLAG_ARRAY: + { + unsigned i; + unsigned flags; + + lib3ds_mesh_free_flag_list(mesh); + flags=lib3ds_word_read(f); + if (flags) { + if (!lib3ds_mesh_new_flag_list(mesh, flags)) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + for (i=0; iflags; ++i) { + mesh->flagL[i]=lib3ds_word_read(f); + } + ASSERT((!mesh->points) || (mesh->flags==mesh->points)); + ASSERT((!mesh->texels) || (mesh->flags==mesh->texels)); + } + } + break; + case LIB3DS_FACE_ARRAY: + { + lib3ds_chunk_read_reset(&c, f); + if (!face_array_read(mesh, f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_MESH_TEXTURE_INFO: + { + int i,j; - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + for (i=0; i<2; ++i) { + mesh->map_data.tile[i]=lib3ds_float_read(f); + } + for (i=0; i<3; ++i) { + mesh->map_data.pos[i]=lib3ds_float_read(f); + } + mesh->map_data.scale=lib3ds_float_read(f); + + lib3ds_matrix_identity(mesh->map_data.matrix); + for (i=0; i<4; i++) { + for (j=0; j<3; j++) { + mesh->map_data.matrix[i][j]=lib3ds_float_read(f); + } + } + for (i=0; i<2; ++i) { + mesh->map_data.planar_size[i]=lib3ds_float_read(f); + } + mesh->map_data.cylinder_height=lib3ds_float_read(f); + } + break; + case LIB3DS_TEX_VERTS: + { + unsigned i; + unsigned texels; + + lib3ds_mesh_free_texel_list(mesh); + texels=lib3ds_word_read(f); + if (texels) { + if (!lib3ds_mesh_new_texel_list(mesh, texels)) { + LIB3DS_ERROR_LOG; + return(LIB3DS_FALSE); + } + for (i=0; itexels; ++i) { + mesh->texelL[i][0]=lib3ds_float_read(f); + mesh->texelL[i][1]=lib3ds_float_read(f); + } + ASSERT((!mesh->points) || (mesh->texels==mesh->points)); + ASSERT((!mesh->flags) || (mesh->texels==mesh->flags)); + } + } + break; + default: + lib3ds_chunk_unknown(chunk); + } + } + { + unsigned j; + + for (j=0; jfaces; ++j) { + ASSERT(mesh->faceL[j].points[0]points); + ASSERT(mesh->faceL[j].points[1]points); + ASSERT(mesh->faceL[j].points[2]points); + lib3ds_vector_normal( + mesh->faceL[j].normal, + mesh->pointL[mesh->faceL[j].points[0]].pos, + mesh->pointL[mesh->faceL[j].points[1]].pos, + mesh->pointL[mesh->faceL[j].points[2]].pos + ); + } + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } static Lib3dsBool point_array_write(Lib3dsMesh *mesh, FILE *f) { - Lib3dsChunk c; - unsigned i; + Lib3dsChunk c; + unsigned i; - if (!mesh->points || !mesh->pointL) - { - return(LIB3DS_TRUE); - } - ASSERT(mesh->points<0x10000); - c.chunk=LIB3DS_POINT_ARRAY; - c.size=8+12*mesh->points; - lib3ds_chunk_write(&c, f); - - lib3ds_word_write((Lib3dsWord)mesh->points, f); - for (i=0; ipoints; ++i) - { - lib3ds_vector_write(mesh->pointL[i].pos, f); - } + if (!mesh->points || !mesh->pointL) { return(LIB3DS_TRUE); + } + ASSERT(mesh->points<0x10000); + c.chunk=LIB3DS_POINT_ARRAY; + c.size=8+12*mesh->points; + lib3ds_chunk_write(&c, f); + + lib3ds_word_write((Lib3dsWord)mesh->points, f); + for (i=0; ipoints; ++i) { + lib3ds_vector_write(mesh->pointL[i].pos, f); + } + return(LIB3DS_TRUE); } static Lib3dsBool flag_array_write(Lib3dsMesh *mesh, FILE *f) { - Lib3dsChunk c; - unsigned i; - - if (!mesh->flags || !mesh->flagL) - { - return(LIB3DS_TRUE); - } - ASSERT(mesh->flags<0x10000); - c.chunk=LIB3DS_POINT_FLAG_ARRAY; - c.size=8+2*mesh->flags; - lib3ds_chunk_write(&c, f); - - lib3ds_word_write((Lib3dsWord)mesh->flags, f); - for (i=0; iflags; ++i) - { - lib3ds_word_write(mesh->flagL[i], f); - } + Lib3dsChunk c; + unsigned i; + + if (!mesh->flags || !mesh->flagL) { return(LIB3DS_TRUE); + } + ASSERT(mesh->flags<0x10000); + c.chunk=LIB3DS_POINT_FLAG_ARRAY; + c.size=8+2*mesh->flags; + lib3ds_chunk_write(&c, f); + + lib3ds_word_write((Lib3dsWord)mesh->flags, f); + for (i=0; iflags; ++i) { + lib3ds_word_write(mesh->flagL[i], f); + } + return(LIB3DS_TRUE); } static Lib3dsBool face_array_write(Lib3dsMesh *mesh, FILE *f) { + Lib3dsChunk c; + + if (!mesh->faces || !mesh->faceL) { + return(LIB3DS_TRUE); + } + ASSERT(mesh->faces<0x10000); + c.chunk=LIB3DS_FACE_ARRAY; + if (!lib3ds_chunk_write_start(&c, f)) { + return(LIB3DS_FALSE); + } + { + unsigned i; + + lib3ds_word_write((Lib3dsWord)mesh->faces, f); + for (i=0; ifaces; ++i) { + lib3ds_word_write(mesh->faceL[i].points[0], f); + lib3ds_word_write(mesh->faceL[i].points[1], f); + lib3ds_word_write(mesh->faceL[i].points[2], f); + lib3ds_word_write(mesh->faceL[i].flags, f); + } + } + + { /*---- MSH_MAT_GROUP ----*/ + Lib3dsChunk c; + unsigned i,j; + Lib3dsWord num; + char *matf=(char*)calloc(sizeof(char), mesh->faces); + if (!matf) { + return(LIB3DS_FALSE); + } + + for (i=0; ifaces; ++i) { + if (!matf[i] && strlen(mesh->faceL[i].material)) { + matf[i]=1; + num=1; + + for (j=i+1; jfaces; ++j) { + if (strcmp(mesh->faceL[i].material, mesh->faceL[j].material)==0) ++num; + } + + c.chunk=LIB3DS_MSH_MAT_GROUP; + c.size=6+ strlen(mesh->faceL[i].material)+1 +2+2*num; + lib3ds_chunk_write(&c, f); + lib3ds_string_write(mesh->faceL[i].material, f); + lib3ds_word_write(num, f); + lib3ds_word_write((Lib3dsWord)i, f); + + for (j=i+1; jfaces; ++j) { + if (strcmp(mesh->faceL[i].material, mesh->faceL[j].material)==0) { + lib3ds_word_write((Lib3dsWord)j, f); + matf[j]=1; + } + } + } + } + free(matf); + } + + { /*---- SMOOTH_GROUP ----*/ + Lib3dsChunk c; + unsigned i; + + c.chunk=LIB3DS_SMOOTH_GROUP; + c.size=6+4*mesh->faces; + lib3ds_chunk_write(&c, f); + + for (i=0; ifaces; ++i) { + lib3ds_dword_write(mesh->faceL[i].smoothing, f); + } + } + + { /*---- MSH_BOXMAP ----*/ Lib3dsChunk c; - if (!mesh->faces || !mesh->faceL) - { - return(LIB3DS_TRUE); - } - ASSERT(mesh->faces<0x10000); - c.chunk=LIB3DS_FACE_ARRAY; - if (!lib3ds_chunk_write_start(&c, f)) - { + if (strlen(mesh->box_map.front) || + strlen(mesh->box_map.back) || + strlen(mesh->box_map.left) || + strlen(mesh->box_map.right) || + strlen(mesh->box_map.top) || + strlen(mesh->box_map.bottom)) { + + c.chunk=LIB3DS_MSH_BOXMAP; + if (!lib3ds_chunk_write_start(&c, f)) { return(LIB3DS_FALSE); - } - { - unsigned i; - - lib3ds_word_write((Lib3dsWord)mesh->faces, f); - for (i=0; ifaces; ++i) - { - lib3ds_word_write(mesh->faceL[i].points[0], f); - lib3ds_word_write(mesh->faceL[i].points[1], f); - lib3ds_word_write(mesh->faceL[i].points[2], f); - lib3ds_word_write(mesh->faceL[i].flags, f); - } - } - - { /*---- MSH_MAT_GROUP ----*/ - Lib3dsChunk c; - unsigned i,j; - Lib3dsWord num; - char *matf=(char *)calloc(sizeof(char), mesh->faces); - if (!matf) - { - return(LIB3DS_FALSE); - } - - for (i=0; ifaces; ++i) - { - if (!matf[i]) - { - matf[i]=1; - num=1; - - for (j=i+1; jfaces; ++j) - { - if (strcmp(mesh->faceL[i].material, mesh->faceL[j].material)==0) ++num; - } - - c.chunk=LIB3DS_MSH_MAT_GROUP; - c.size=6+ strlen(mesh->faceL[i].material)+1 +2+2*num; - lib3ds_chunk_write(&c, f); - lib3ds_string_write(mesh->faceL[i].material, f); - lib3ds_word_write(num, f); - lib3ds_word_write((Lib3dsWord)i, f); - - for (j=i+1; jfaces; ++j) - { - if (strcmp(mesh->faceL[i].material, mesh->faceL[j].material)==0) - { - lib3ds_word_write((Lib3dsWord)j, f); - matf[j]=1; - } - } - } - } - free(matf); - } - - { /*---- SMOOTH_GROUP ----*/ - Lib3dsChunk c; - unsigned i; - - c.chunk=LIB3DS_SMOOTH_GROUP; - c.size=6+4*mesh->faces; - lib3ds_chunk_write(&c, f); - - for (i=0; ifaces; ++i) - { - lib3ds_dword_write(mesh->faceL[i].smoothing, f); - } - } - - { /*---- MSH_BOXMAP ----*/ - Lib3dsChunk c; - - if (strlen(mesh->box_map.front) || - strlen(mesh->box_map.back) || - strlen(mesh->box_map.left) || - strlen(mesh->box_map.right) || - strlen(mesh->box_map.top) || - strlen(mesh->box_map.bottom)) - { - - c.chunk=LIB3DS_MSH_BOXMAP; - if (!lib3ds_chunk_write_start(&c, f)) - { - return(LIB3DS_FALSE); - } - - lib3ds_string_write(mesh->box_map.front, f); - lib3ds_string_write(mesh->box_map.back, f); - lib3ds_string_write(mesh->box_map.left, f); - lib3ds_string_write(mesh->box_map.right, f); - lib3ds_string_write(mesh->box_map.top, f); - lib3ds_string_write(mesh->box_map.bottom, f); - - if (!lib3ds_chunk_write_end(&c, f)) - { - return(LIB3DS_FALSE); - } - } - } - - if (!lib3ds_chunk_write_end(&c, f)) - { + } + + lib3ds_string_write(mesh->box_map.front, f); + lib3ds_string_write(mesh->box_map.back, f); + lib3ds_string_write(mesh->box_map.left, f); + lib3ds_string_write(mesh->box_map.right, f); + lib3ds_string_write(mesh->box_map.top, f); + lib3ds_string_write(mesh->box_map.bottom, f); + + if (!lib3ds_chunk_write_end(&c, f)) { return(LIB3DS_FALSE); + } } - return(LIB3DS_TRUE); + } + + if (!lib3ds_chunk_write_end(&c, f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } static Lib3dsBool texel_array_write(Lib3dsMesh *mesh, FILE *f) { - Lib3dsChunk c; - unsigned i; - - if (!mesh->texels || !mesh->texelL) - { - return(LIB3DS_TRUE); - } - ASSERT(mesh->texels<0x10000); - c.chunk=LIB3DS_TEX_VERTS; - c.size=8+8*mesh->texels; - lib3ds_chunk_write(&c, f); - - lib3ds_word_write((Lib3dsWord)mesh->texels, f); - for (i=0; itexels; ++i) - { - lib3ds_float_write(mesh->texelL[i][0], f); - lib3ds_float_write(mesh->texelL[i][1], f); - } + Lib3dsChunk c; + unsigned i; + + if (!mesh->texels || !mesh->texelL) { return(LIB3DS_TRUE); + } + ASSERT(mesh->texels<0x10000); + c.chunk=LIB3DS_TEX_VERTS; + c.size=8+8*mesh->texels; + lib3ds_chunk_write(&c, f); + + lib3ds_word_write((Lib3dsWord)mesh->texels, f); + for (i=0; itexels; ++i) { + lib3ds_float_write(mesh->texelL[i][0], f); + lib3ds_float_write(mesh->texelL[i][1], f); + } + return(LIB3DS_TRUE); } @@ -810,100 +853,87 @@ texel_array_write(Lib3dsMesh *mesh, FILE *f) Lib3dsBool lib3ds_mesh_write(Lib3dsMesh *mesh, FILE *f) { + Lib3dsChunk c; + + c.chunk=LIB3DS_N_TRI_OBJECT; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!point_array_write(mesh, f)) { + return(LIB3DS_FALSE); + } + if (!texel_array_write(mesh, f)) { + return(LIB3DS_FALSE); + } + + if (mesh->map_data.maptype!=LIB3DS_MAP_NONE) { /*---- LIB3DS_MESH_TEXTURE_INFO ----*/ Lib3dsChunk c; - - c.chunk=LIB3DS_N_TRI_OBJECT; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!point_array_write(mesh, f)) - { - return(LIB3DS_FALSE); - } - if (!flag_array_write(mesh, f)) - { - return(LIB3DS_FALSE); - } - if (!face_array_write(mesh, f)) - { - return(LIB3DS_FALSE); - } - if (!texel_array_write(mesh, f)) - { - return(LIB3DS_FALSE); - } - { /*---- LIB3DS_MESH_MATRIX ----*/ - Lib3dsChunk c; - int i,j; - - c.chunk=LIB3DS_MESH_MATRIX; - c.size=54; - if (!lib3ds_chunk_write(&c,f)) - { - return(LIB3DS_FALSE); - } - for (i=0; i<4; i++) - { - for (j=0; j<3; j++) - { - lib3ds_float_write(mesh->matrix[i][j], f); - } - } + int i,j; + + c.chunk=LIB3DS_MESH_TEXTURE_INFO; + c.size=92; + if (!lib3ds_chunk_write(&c,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_MESH_COLOR ----*/ - Lib3dsChunk c; + lib3ds_word_write(mesh->map_data.maptype, f); - c.chunk=LIB3DS_MESH_COLOR; - c.size=7; - if (!lib3ds_chunk_write(&c,f)) - { - return(LIB3DS_FALSE); - } - lib3ds_byte_write(mesh->color, f); + for (i=0; i<2; ++i) { + lib3ds_float_write(mesh->map_data.tile[i], f); } - - { /*---- LIB3DS_MESH_TEXTURE_INFO ----*/ - Lib3dsChunk c; - int i,j; - - c.chunk=LIB3DS_MESH_TEXTURE_INFO; - c.size=90; - if (!lib3ds_chunk_write(&c,f)) - { - return(LIB3DS_FALSE); - } - - for (i=0; i<2; ++i) - { - lib3ds_float_write(mesh->map_data.tile[i], f); - } - for (i=0; i<3; ++i) - { - lib3ds_float_write(mesh->map_data.pos[i], f); - } - lib3ds_float_write(mesh->map_data.scale, f); - - for (i=0; i<4; i++) - { - for (j=0; j<3; j++) - { - lib3ds_float_write(mesh->map_data.matrix[i][j], f); - } - } - for (i=0; i<2; ++i) - { - lib3ds_float_write(mesh->map_data.planar_size[i], f); - } - lib3ds_float_write(mesh->map_data.cylinder_height, f); + for (i=0; i<3; ++i) { + lib3ds_float_write(mesh->map_data.pos[i], f); } + lib3ds_float_write(mesh->map_data.scale, f); - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); + for (i=0; i<4; i++) { + for (j=0; j<3; j++) { + lib3ds_float_write(mesh->map_data.matrix[i][j], f); + } } - return(LIB3DS_TRUE); + for (i=0; i<2; ++i) { + lib3ds_float_write(mesh->map_data.planar_size[i], f); + } + lib3ds_float_write(mesh->map_data.cylinder_height, f); + } + + if (!flag_array_write(mesh, f)) { + return(LIB3DS_FALSE); + } + { /*---- LIB3DS_MESH_MATRIX ----*/ + Lib3dsChunk c; + int i,j; + + c.chunk=LIB3DS_MESH_MATRIX; + c.size=54; + if (!lib3ds_chunk_write(&c,f)) { + return(LIB3DS_FALSE); + } + for (i=0; i<4; i++) { + for (j=0; j<3; j++) { + lib3ds_float_write(mesh->matrix[i][j], f); + } + } + } + + if (mesh->color) { /*---- LIB3DS_MESH_COLOR ----*/ + Lib3dsChunk c; + + c.chunk=LIB3DS_MESH_COLOR; + c.size=7; + if (!lib3ds_chunk_write(&c,f)) { + return(LIB3DS_FALSE); + } + lib3ds_byte_write(mesh->color, f); + } + if (!face_array_write(mesh, f)) { + return(LIB3DS_FALSE); + } + + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } diff --git a/src/osgPlugins/lib3ds/mesh.h b/src/osgPlugins/lib3ds/mesh.h index d70473d5e..d10ad0461 100644 --- a/src/osgPlugins/lib3ds/mesh.h +++ b/src/osgPlugins/lib3ds/mesh.h @@ -65,11 +65,23 @@ struct _Lib3dsBoxMap { char bottom[64]; }; +/*! + * Lib3dsMapData maptype + * \ingroup tracks + */ +typedef enum { + LIB3DS_MAP_NONE =0xFFFF, + LIB3DS_MAP_PLANAR =0, + LIB3DS_MAP_CYLINDRICAL =1, + LIB3DS_MAP_SPHERICAL =2 +} Lib3dsMapType; + /*! * Triangular mesh texture mapping data * \ingroup mesh */ struct _Lib3dsMapData { + Lib3dsWord maptype; Lib3dsVector pos; Lib3dsMatrix matrix; Lib3dsFloat scale; @@ -111,6 +123,7 @@ extern LIB3DSAPI void lib3ds_mesh_free_texel_list(Lib3dsMesh *mesh); extern LIB3DSAPI Lib3dsBool lib3ds_mesh_new_face_list(Lib3dsMesh *mesh, Lib3dsDword flags); extern LIB3DSAPI void lib3ds_mesh_free_face_list(Lib3dsMesh *mesh); extern LIB3DSAPI void lib3ds_mesh_bounding_box(Lib3dsMesh *mesh, Lib3dsVector min, Lib3dsVector max); +extern LIB3DSAPI void lib3ds_mesh_calculate_normals(Lib3dsMesh *mesh, Lib3dsVector *normalL); extern LIB3DSAPI void lib3ds_mesh_dump(Lib3dsMesh *mesh); extern LIB3DSAPI Lib3dsBool lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f); extern LIB3DSAPI Lib3dsBool lib3ds_mesh_write(Lib3dsMesh *mesh, FILE *f); diff --git a/src/osgPlugins/lib3ds/node.cpp b/src/osgPlugins/lib3ds/node.cpp index e98cbe669..6189e1609 100644 --- a/src/osgPlugins/lib3ds/node.cpp +++ b/src/osgPlugins/lib3ds/node.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -27,27 +27,30 @@ #include #include #include -//#include +#include +#include #ifdef WITH_DMALLOC #include #endif + /*! * \defgroup node Animation Nodes * * \author J.E. Hoffmann */ + /*! * \ingroup node */ Lib3dsNode* lib3ds_node_new_ambient() { - Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); - node->type=LIB3DS_AMBIENT_NODE; - lib3ds_matrix_identity(node->matrix); - return(node); + Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); + node->type=LIB3DS_AMBIENT_NODE; + lib3ds_matrix_identity(node->matrix); + return(node); } @@ -57,10 +60,10 @@ lib3ds_node_new_ambient() Lib3dsNode* lib3ds_node_new_object() { - Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); - node->type=LIB3DS_OBJECT_NODE; - lib3ds_matrix_identity(node->matrix); - return(node); + Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); + node->type=LIB3DS_OBJECT_NODE; + lib3ds_matrix_identity(node->matrix); + return(node); } @@ -70,10 +73,10 @@ lib3ds_node_new_object() Lib3dsNode* lib3ds_node_new_camera() { - Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); - node->type=LIB3DS_CAMERA_NODE; - lib3ds_matrix_identity(node->matrix); - return(node); + Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); + node->type=LIB3DS_CAMERA_NODE; + lib3ds_matrix_identity(node->matrix); + return(node); } @@ -83,10 +86,10 @@ lib3ds_node_new_camera() Lib3dsNode* lib3ds_node_new_target() { - Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); - node->type=LIB3DS_TARGET_NODE; - lib3ds_matrix_identity(node->matrix); - return(node); + Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); + node->type=LIB3DS_TARGET_NODE; + lib3ds_matrix_identity(node->matrix); + return(node); } @@ -96,10 +99,10 @@ lib3ds_node_new_target() Lib3dsNode* lib3ds_node_new_light() { - Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); - node->type=LIB3DS_LIGHT_NODE; - lib3ds_matrix_identity(node->matrix); - return(node); + Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); + node->type=LIB3DS_LIGHT_NODE; + lib3ds_matrix_identity(node->matrix); + return(node); } @@ -109,79 +112,77 @@ lib3ds_node_new_light() Lib3dsNode* lib3ds_node_new_spot() { - Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); - node->type=LIB3DS_SPOT_NODE; - lib3ds_matrix_identity(node->matrix); - return(node); + Lib3dsNode *node=(Lib3dsNode*)calloc(sizeof(Lib3dsNode), 1); + node->type=LIB3DS_SPOT_NODE; + lib3ds_matrix_identity(node->matrix); + return(node); } static void free_node_and_childs(Lib3dsNode *node) { - ASSERT(node); - switch (node->type) - { - case LIB3DS_UNKNOWN_NODE: - break; - case LIB3DS_AMBIENT_NODE: - { - Lib3dsAmbientData *n=&node->data.ambient; - lib3ds_lin3_track_free_keys(&n->col_track); - } - break; - case LIB3DS_OBJECT_NODE: - { - Lib3dsObjectData *n=&node->data.object; + ASSERT(node); + switch (node->type) { + case LIB3DS_UNKNOWN_NODE: + break; + case LIB3DS_AMBIENT_NODE: + { + Lib3dsAmbientData *n=&node->data.ambient; + lib3ds_lin3_track_free_keys(&n->col_track); + } + break; + case LIB3DS_OBJECT_NODE: + { + Lib3dsObjectData *n=&node->data.object; - lib3ds_lin3_track_free_keys(&n->pos_track); - lib3ds_quat_track_free_keys(&n->rot_track); - lib3ds_lin3_track_free_keys(&n->scl_track); - lib3ds_bool_track_free_keys(&n->hide_track); - lib3ds_morph_track_free_keys(&n->morph_track); - } - break; - case LIB3DS_CAMERA_NODE: - { - Lib3dsCameraData *n=&node->data.camera; - lib3ds_lin3_track_free_keys(&n->pos_track); - lib3ds_lin1_track_free_keys(&n->fov_track); - lib3ds_lin1_track_free_keys(&n->roll_track); - } - break; - case LIB3DS_TARGET_NODE: - { - Lib3dsTargetData *n=&node->data.target; - lib3ds_lin3_track_free_keys(&n->pos_track); - } - break; - case LIB3DS_LIGHT_NODE: - { - Lib3dsLightData *n=&node->data.light; - lib3ds_lin3_track_free_keys(&n->pos_track); - lib3ds_lin3_track_free_keys(&n->col_track); - lib3ds_lin1_track_free_keys(&n->hotspot_track); - lib3ds_lin1_track_free_keys(&n->falloff_track); - lib3ds_lin1_track_free_keys(&n->roll_track); - } - break; - case LIB3DS_SPOT_NODE: - { - Lib3dsSpotData *n=&node->data.spot; - lib3ds_lin3_track_free_keys(&n->pos_track); - } - break; + lib3ds_lin3_track_free_keys(&n->pos_track); + lib3ds_quat_track_free_keys(&n->rot_track); + lib3ds_lin3_track_free_keys(&n->scl_track); + lib3ds_bool_track_free_keys(&n->hide_track); + lib3ds_morph_track_free_keys(&n->morph_track); + } + break; + case LIB3DS_CAMERA_NODE: + { + Lib3dsCameraData *n=&node->data.camera; + lib3ds_lin3_track_free_keys(&n->pos_track); + lib3ds_lin1_track_free_keys(&n->fov_track); + lib3ds_lin1_track_free_keys(&n->roll_track); + } + break; + case LIB3DS_TARGET_NODE: + { + Lib3dsTargetData *n=&node->data.target; + lib3ds_lin3_track_free_keys(&n->pos_track); + } + break; + case LIB3DS_LIGHT_NODE: + { + Lib3dsLightData *n=&node->data.light; + lib3ds_lin3_track_free_keys(&n->pos_track); + lib3ds_lin3_track_free_keys(&n->col_track); + lib3ds_lin1_track_free_keys(&n->hotspot_track); + lib3ds_lin1_track_free_keys(&n->falloff_track); + lib3ds_lin1_track_free_keys(&n->roll_track); + } + break; + case LIB3DS_SPOT_NODE: + { + Lib3dsSpotData *n=&node->data.spot; + lib3ds_lin3_track_free_keys(&n->pos_track); + } + break; + } + { + Lib3dsNode *p,*q; + for (p=node->childs; p; p=q) { + q=p->next; + free_node_and_childs(p); } - { - Lib3dsNode *p,*q; - for (p=node->childs; p; p=q) - { - q=p->next; - free_node_and_childs(p); - } - } - node->type=LIB3DS_UNKNOWN_NODE; - free(node); + } + node->type=LIB3DS_UNKNOWN_NODE; + free(node); } @@ -191,8 +192,8 @@ free_node_and_childs(Lib3dsNode *node) void lib3ds_node_free(Lib3dsNode *node) { - ASSERT(node); - free_node_and_childs(node); + ASSERT(node); + free_node_and_childs(node); } @@ -202,129 +203,115 @@ lib3ds_node_free(Lib3dsNode *node) void lib3ds_node_eval(Lib3dsNode *node, Lib3dsFloat t) { - ASSERT(node); - switch (node->type) - { - case LIB3DS_UNKNOWN_NODE: - { - ASSERT(LIB3DS_FALSE); + ASSERT(node); + switch (node->type) { + case LIB3DS_UNKNOWN_NODE: + { + ASSERT(LIB3DS_FALSE); + } + break; + case LIB3DS_AMBIENT_NODE: + { + Lib3dsAmbientData *n=&node->data.ambient; + if (node->parent) { + lib3ds_matrix_copy(node->matrix, node->parent->matrix); } - break; - case LIB3DS_AMBIENT_NODE: - { - Lib3dsAmbientData *n=&node->data.ambient; - if (node->parent) - { - lib3ds_matrix_copy(node->matrix, node->parent->matrix); - } - else - { - lib3ds_matrix_identity(node->matrix); - } - lib3ds_lin3_track_eval(&n->col_track, n->col, t); + else { + lib3ds_matrix_identity(node->matrix); } - break; - case LIB3DS_OBJECT_NODE: - { - Lib3dsMatrix M; - Lib3dsObjectData *n=&node->data.object; + lib3ds_lin3_track_eval(&n->col_track, n->col, t); + } + break; + case LIB3DS_OBJECT_NODE: + { + Lib3dsMatrix M; + Lib3dsObjectData *n=&node->data.object; - lib3ds_lin3_track_eval(&n->pos_track, n->pos, t); - lib3ds_quat_track_eval(&n->rot_track, n->rot, t); - lib3ds_lin3_track_eval(&n->scl_track, n->scl, t); - lib3ds_bool_track_eval(&n->hide_track, &n->hide, t); - lib3ds_morph_track_eval(&n->morph_track, n->morph, t); + lib3ds_lin3_track_eval(&n->pos_track, n->pos, t); + lib3ds_quat_track_eval(&n->rot_track, n->rot, t); + lib3ds_lin3_track_eval(&n->scl_track, n->scl, t); + lib3ds_bool_track_eval(&n->hide_track, &n->hide, t); + lib3ds_morph_track_eval(&n->morph_track, n->morph, t); - lib3ds_matrix_identity(M); - lib3ds_matrix_translate(M, n->pos); - lib3ds_matrix_rotate(M, n->rot); - lib3ds_matrix_scale(M, n->scl); + lib3ds_matrix_identity(M); + lib3ds_matrix_translate(M, n->pos); + lib3ds_matrix_rotate(M, n->rot); + lib3ds_matrix_scale(M, n->scl); + + if (node->parent) { + lib3ds_matrix_mul(node->matrix, node->parent->matrix, M); + } + else { + lib3ds_matrix_copy(node->matrix, M); + } + } + break; + case LIB3DS_CAMERA_NODE: + { + Lib3dsCameraData *n=&node->data.camera; + lib3ds_lin3_track_eval(&n->pos_track, n->pos, t); + lib3ds_lin1_track_eval(&n->fov_track, &n->fov, t); + lib3ds_lin1_track_eval(&n->roll_track, &n->roll, t); + if (node->parent) { + lib3ds_matrix_copy(node->matrix, node->parent->matrix); + } + else { + lib3ds_matrix_identity(node->matrix); + } + lib3ds_matrix_translate(node->matrix, n->pos); + } + break; + case LIB3DS_TARGET_NODE: + { + Lib3dsTargetData *n=&node->data.target; + lib3ds_lin3_track_eval(&n->pos_track, n->pos, t); + if (node->parent) { + lib3ds_matrix_copy(node->matrix, node->parent->matrix); + } + else { + lib3ds_matrix_identity(node->matrix); + } + lib3ds_matrix_translate(node->matrix, n->pos); + } + break; + case LIB3DS_LIGHT_NODE: + { + Lib3dsLightData *n=&node->data.light; + lib3ds_lin3_track_eval(&n->pos_track, n->pos, t); + lib3ds_lin3_track_eval(&n->col_track, n->col, t); + lib3ds_lin1_track_eval(&n->hotspot_track, &n->hotspot, t); + lib3ds_lin1_track_eval(&n->falloff_track, &n->falloff, t); + lib3ds_lin1_track_eval(&n->roll_track, &n->roll, t); + if (node->parent) { + lib3ds_matrix_copy(node->matrix, node->parent->matrix); + } + else { + lib3ds_matrix_identity(node->matrix); + } + lib3ds_matrix_translate(node->matrix, n->pos); + } + break; + case LIB3DS_SPOT_NODE: + { + Lib3dsSpotData *n=&node->data.spot; + lib3ds_lin3_track_eval(&n->pos_track, n->pos, t); + if (node->parent) { + lib3ds_matrix_copy(node->matrix, node->parent->matrix); + } + else { + lib3ds_matrix_identity(node->matrix); + } + lib3ds_matrix_translate(node->matrix, n->pos); + } + break; + } + { + Lib3dsNode *p; - if (node->parent) - { - lib3ds_matrix_mul(node->matrix, node->parent->matrix, M); - } - else - { - lib3ds_matrix_copy(node->matrix, M); - } - } - break; - case LIB3DS_CAMERA_NODE: - { - Lib3dsCameraData *n=&node->data.camera; - lib3ds_lin3_track_eval(&n->pos_track, n->pos, t); - lib3ds_lin1_track_eval(&n->fov_track, &n->fov, t); - lib3ds_lin1_track_eval(&n->roll_track, &n->roll, t); - if (node->parent) - { - lib3ds_matrix_copy(node->matrix, node->parent->matrix); - } - else - { - lib3ds_matrix_identity(node->matrix); - } - lib3ds_matrix_translate(node->matrix, n->pos); - } - break; - case LIB3DS_TARGET_NODE: - { - Lib3dsTargetData *n=&node->data.target; - lib3ds_lin3_track_eval(&n->pos_track, n->pos, t); - if (node->parent) - { - lib3ds_matrix_copy(node->matrix, node->parent->matrix); - } - else - { - lib3ds_matrix_identity(node->matrix); - } - lib3ds_matrix_translate(node->matrix, n->pos); - } - break; - case LIB3DS_LIGHT_NODE: - { - Lib3dsLightData *n=&node->data.light; - lib3ds_lin3_track_eval(&n->pos_track, n->pos, t); - lib3ds_lin3_track_eval(&n->col_track, n->col, t); - lib3ds_lin1_track_eval(&n->hotspot_track, &n->hotspot, t); - lib3ds_lin1_track_eval(&n->falloff_track, &n->falloff, t); - lib3ds_lin1_track_eval(&n->roll_track, &n->roll, t); - if (node->parent) - { - lib3ds_matrix_copy(node->matrix, node->parent->matrix); - } - else - { - lib3ds_matrix_identity(node->matrix); - } - lib3ds_matrix_translate(node->matrix, n->pos); - } - break; - case LIB3DS_SPOT_NODE: - { - Lib3dsSpotData *n=&node->data.spot; - lib3ds_lin3_track_eval(&n->pos_track, n->pos, t); - if (node->parent) - { - lib3ds_matrix_copy(node->matrix, node->parent->matrix); - } - else - { - lib3ds_matrix_identity(node->matrix); - } - lib3ds_matrix_translate(node->matrix, n->pos); - } - break; - } - { - Lib3dsNode *p; - - for (p=node->childs; p!=0; p=p->next) - { - lib3ds_node_eval(p, t); - } + for (p=node->childs; p!=0; p=p->next) { + lib3ds_node_eval(p, t); } + } } @@ -334,21 +321,18 @@ lib3ds_node_eval(Lib3dsNode *node, Lib3dsFloat t) Lib3dsNode* lib3ds_node_by_name(Lib3dsNode *node, const char* name, Lib3dsNodeTypes type) { - Lib3dsNode *p,*q; + Lib3dsNode *p,*q; - for (p=node->childs; p!=0; p=p->next) - { - if ((p->type==type) && (strcmp(p->name, name)==0)) - { - return(p); - } - q=lib3ds_node_by_name(p, name, type); - if (q) - { - return(q); - } + for (p=node->childs; p!=0; p=p->next) { + if ((p->type==type) && (strcmp(p->name, name)==0)) { + return(p); } - return(0); + q=lib3ds_node_by_name(p, name, type); + if (q) { + return(q); + } + } + return(0); } @@ -356,72 +340,66 @@ lib3ds_node_by_name(Lib3dsNode *node, const char* name, Lib3dsNodeTypes type) * \ingroup node */ Lib3dsNode* -lib3ds_node_by_id(Lib3dsNode *node, Lib3dsWord id) +lib3ds_node_by_id(Lib3dsNode *node, Lib3dsWord node_id) { - Lib3dsNode *p,*q; + Lib3dsNode *p,*q; - for (p=node->childs; p!=0; p=p->next) - { - if (p->id==id) - { - return(p); - } - q=lib3ds_node_by_id(p, id); - if (q) - { - return(q); - } + for (p=node->childs; p!=0; p=p->next) { + if (p->node_id==node_id) { + return(p); } - return(0); + q=lib3ds_node_by_id(p, node_id); + if (q) { + return(q); + } + } + return(0); } -static const char* node_names_table[]= -{ - "***Unknown***", - "Ambient", - "Object", - "Camera", - "Target", - "Light", - "Spot" +static const char* node_names_table[]= { + "***Unknown***", + "Ambient", + "Object", + "Camera", + "Target", + "Light", + "Spot" }; + /*! * \ingroup node */ void lib3ds_node_dump(Lib3dsNode *node, Lib3dsIntd level) { - Lib3dsNode *p; - char l[128]; + Lib3dsNode *p; + char l[128]; - ASSERT(node); - memset(l, ' ', 2*level); - l[2*level]=0; + ASSERT(node); + memset(l, ' ', 2*level); + l[2*level]=0; - if (node->type==LIB3DS_OBJECT_NODE) - { - printf("%s%s [%s] (%s)\n", - l, - node->name, - node->data.object.instance, - node_names_table[node->type] - ); - } - else - { - printf("%s%s (%s)\n", - l, - node->name, - node_names_table[node->type] - ); - } - - for (p=node->childs; p!=0; p=p->next) - { - lib3ds_node_dump(p, level+1); - } + if (node->type==LIB3DS_OBJECT_NODE) { + printf("%s%s [%s] (%s)\n", + l, + node->name, + node->data.object.instance, + node_names_table[node->type] + ); + } + else { + printf("%s%s (%s)\n", + l, + node->name, + node_names_table[node->type] + ); + } + + for (p=node->childs; p!=0; p=p->next) { + lib3ds_node_dump(p, level+1); + } } @@ -429,299 +407,256 @@ lib3ds_node_dump(Lib3dsNode *node, Lib3dsIntd level) * \ingroup node */ Lib3dsBool -lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) +lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *file, FILE *f) { - // parameters lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *file, FILE *f) + Lib3dsChunk c; + Lib3dsWord chunk; - Lib3dsChunk c; - Lib3dsWord chunk; + ASSERT(node); + if (!lib3ds_chunk_read_start(&c, 0, f)) { + return(LIB3DS_FALSE); + } + switch (c.chunk) { + case LIB3DS_AMBIENT_NODE_TAG: + case LIB3DS_OBJECT_NODE_TAG: + case LIB3DS_CAMERA_NODE_TAG: + case LIB3DS_TARGET_NODE_TAG: + case LIB3DS_LIGHT_NODE_TAG: + case LIB3DS_SPOTLIGHT_NODE_TAG: + case LIB3DS_L_TARGET_NODE_TAG: + break; + default: + return(LIB3DS_FALSE); + } - ASSERT(node); - if (!lib3ds_chunk_read_start(&c, 0, f)) - { - return(LIB3DS_FALSE); - } - switch (c.chunk) - { - case LIB3DS_AMBIENT_NODE_TAG: - case LIB3DS_OBJECT_NODE_TAG: - case LIB3DS_CAMERA_NODE_TAG: - case LIB3DS_TARGET_NODE_TAG: - case LIB3DS_LIGHT_NODE_TAG: - case LIB3DS_SPOTLIGHT_NODE_TAG: - case LIB3DS_L_TARGET_NODE_TAG: - break; - default: - return(LIB3DS_FALSE); - } - - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_NODE_ID: { - case LIB3DS_NODE_ID: - { - node->id=lib3ds_word_read(f); - } - break; - case LIB3DS_NODE_HDR: - { - if (!lib3ds_string_read(node->name, 64, f)) - { - return(LIB3DS_FALSE); - } - node->flags1=lib3ds_word_read(f); - node->flags2=lib3ds_word_read(f); - node->parent_id=lib3ds_word_read(f); - } - break; - case LIB3DS_PIVOT: - { - if (node->type==LIB3DS_OBJECT_NODE) - { - int i; - for (i=0; i<3; ++i) - { - node->data.object.pivot[i]=lib3ds_float_read(f); - } - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - case LIB3DS_INSTANCE_NAME: - { - if (node->type==LIB3DS_OBJECT_NODE) - { - if (!lib3ds_string_read(node->data.object.instance, 64, f)) - { - return(LIB3DS_FALSE); - } - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - case LIB3DS_BOUNDBOX: - { - if (node->type==LIB3DS_OBJECT_NODE) - { - int i; - for (i=0; i<3; ++i) - { - node->data.object.bbox_min[i]=lib3ds_float_read(f); - } - for (i=0; i<3; ++i) - { - node->data.object.bbox_max[i]=lib3ds_float_read(f); - } - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - case LIB3DS_COL_TRACK_TAG: - { - Lib3dsBool result=LIB3DS_TRUE; - - switch (node->type) - { - case LIB3DS_AMBIENT_NODE: - result=lib3ds_lin3_track_read(&node->data.ambient.col_track, f); - break; - case LIB3DS_LIGHT_NODE: - result=lib3ds_lin3_track_read(&node->data.light.col_track, f); - break; - default: - lib3ds_chunk_unknown(chunk); - } - if (!result) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_POS_TRACK_TAG: - { - Lib3dsBool result=LIB3DS_TRUE; - - switch (node->type) - { - case LIB3DS_OBJECT_NODE: - result=lib3ds_lin3_track_read(&node->data.object.pos_track, f); - break; - case LIB3DS_CAMERA_NODE: - result=lib3ds_lin3_track_read(&node->data.camera.pos_track, f); - break; - case LIB3DS_TARGET_NODE: - result=lib3ds_lin3_track_read(&node->data.target.pos_track, f); - break; - case LIB3DS_LIGHT_NODE: - result=lib3ds_lin3_track_read(&node->data.light.pos_track, f); - break; - case LIB3DS_SPOT_NODE: - result=lib3ds_lin3_track_read(&node->data.spot.pos_track, f); - break; - default: - lib3ds_chunk_unknown(chunk); - } - if (!result) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_ROT_TRACK_TAG: - { - if (node->type==LIB3DS_OBJECT_NODE) - { - if (!lib3ds_quat_track_read(&node->data.object.rot_track, f)) - { - return(LIB3DS_FALSE); - } - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - case LIB3DS_SCL_TRACK_TAG: - { - if (node->type==LIB3DS_OBJECT_NODE) - { - if (!lib3ds_lin3_track_read(&node->data.object.scl_track, f)) - { - return(LIB3DS_FALSE); - } - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - case LIB3DS_FOV_TRACK_TAG: - { - if (node->type==LIB3DS_CAMERA_NODE) - { - if (!lib3ds_lin1_track_read(&node->data.camera.fov_track, f)) - { - return(LIB3DS_FALSE); - } - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - case LIB3DS_HOT_TRACK_TAG: - { - if (node->type==LIB3DS_LIGHT_NODE) - { - if (!lib3ds_lin1_track_read(&node->data.light.hotspot_track, f)) - { - return(LIB3DS_FALSE); - } - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - case LIB3DS_FALL_TRACK_TAG: - { - if (node->type==LIB3DS_LIGHT_NODE) - { - if (!lib3ds_lin1_track_read(&node->data.light.falloff_track, f)) - { - return(LIB3DS_FALSE); - } - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - case LIB3DS_ROLL_TRACK_TAG: - { - Lib3dsBool result=LIB3DS_TRUE; - - switch (node->type) - { - case LIB3DS_CAMERA_NODE: - result=lib3ds_lin1_track_read(&node->data.camera.roll_track, f); - break; - case LIB3DS_LIGHT_NODE: - result=lib3ds_lin1_track_read(&node->data.light.roll_track, f); - break; - default: - lib3ds_chunk_unknown(chunk); - } - if (!result) - { - return(LIB3DS_FALSE); - } - } - break; - case LIB3DS_HIDE_TRACK_TAG: - { - if (node->type==LIB3DS_OBJECT_NODE) - { - if (!lib3ds_bool_track_read(&node->data.object.hide_track, f)) - { - return(LIB3DS_FALSE); - } - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - case LIB3DS_MORPH_SMOOTH: - { - if (node->type==LIB3DS_OBJECT_NODE) - { - node->data.object.morph_smooth=lib3ds_float_read(f); - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - case LIB3DS_MORPH_TRACK_TAG: - { - if (node->type==LIB3DS_OBJECT_NODE) - { - if (!lib3ds_morph_track_read(&node->data.object.morph_track, f)) - { - return(LIB3DS_FALSE); - } - } - else - { - lib3ds_chunk_unknown(chunk); - } - } - break; - default: - lib3ds_chunk_unknown(chunk); + node->node_id=lib3ds_word_read(f); + lib3ds_chunk_dump_info(" ID = %d", (short)node->node_id); } - } + break; + case LIB3DS_NODE_HDR: + { + if (!lib3ds_string_read(node->name, 64, f)) { + return(LIB3DS_FALSE); + } + node->flags1=lib3ds_word_read(f); + node->flags2=lib3ds_word_read(f); + node->parent_id=lib3ds_word_read(f); + lib3ds_chunk_dump_info(" NAME =%s", node->name); + lib3ds_chunk_dump_info(" PARENT=%d", (short)node->parent_id); + } + break; + case LIB3DS_PIVOT: + { + if (node->type==LIB3DS_OBJECT_NODE) { + int i; + for (i=0; i<3; ++i) { + node->data.object.pivot[i]=lib3ds_float_read(f); + } + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + case LIB3DS_INSTANCE_NAME: + { + if (node->type==LIB3DS_OBJECT_NODE) { + if (!lib3ds_string_read(node->data.object.instance, 64, f)) { + return(LIB3DS_FALSE); + } + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + case LIB3DS_BOUNDBOX: + { + if (node->type==LIB3DS_OBJECT_NODE) { + int i; + for (i=0; i<3; ++i) { + node->data.object.bbox_min[i]=lib3ds_float_read(f); + } + for (i=0; i<3; ++i) { + node->data.object.bbox_max[i]=lib3ds_float_read(f); + } + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + case LIB3DS_COL_TRACK_TAG: + { + Lib3dsBool result=LIB3DS_TRUE; + + switch (node->type) { + case LIB3DS_AMBIENT_NODE: + result=lib3ds_lin3_track_read(&node->data.ambient.col_track, f); + break; + case LIB3DS_LIGHT_NODE: + result=lib3ds_lin3_track_read(&node->data.light.col_track, f); + break; + default: + lib3ds_chunk_unknown(chunk); + } + if (!result) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_POS_TRACK_TAG: + { + Lib3dsBool result=LIB3DS_TRUE; - lib3ds_chunk_read_end(&c, f); - return(LIB3DS_TRUE); + switch (node->type) { + case LIB3DS_OBJECT_NODE: + result=lib3ds_lin3_track_read(&node->data.object.pos_track, f); + break; + case LIB3DS_CAMERA_NODE: + result=lib3ds_lin3_track_read(&node->data.camera.pos_track, f); + break; + case LIB3DS_TARGET_NODE: + result=lib3ds_lin3_track_read(&node->data.target.pos_track, f); + break; + case LIB3DS_LIGHT_NODE: + result=lib3ds_lin3_track_read(&node->data.light.pos_track, f); + break; + case LIB3DS_SPOT_NODE: + result=lib3ds_lin3_track_read(&node->data.spot.pos_track, f); + break; + default: + lib3ds_chunk_unknown(chunk); + } + if (!result) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_ROT_TRACK_TAG: + { + if (node->type==LIB3DS_OBJECT_NODE) { + if (!lib3ds_quat_track_read(&node->data.object.rot_track, f)) { + return(LIB3DS_FALSE); + } + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + case LIB3DS_SCL_TRACK_TAG: + { + if (node->type==LIB3DS_OBJECT_NODE) { + if (!lib3ds_lin3_track_read(&node->data.object.scl_track, f)) { + return(LIB3DS_FALSE); + } + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + case LIB3DS_FOV_TRACK_TAG: + { + if (node->type==LIB3DS_CAMERA_NODE) { + if (!lib3ds_lin1_track_read(&node->data.camera.fov_track, f)) { + return(LIB3DS_FALSE); + } + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + case LIB3DS_HOT_TRACK_TAG: + { + if (node->type==LIB3DS_LIGHT_NODE) { + if (!lib3ds_lin1_track_read(&node->data.light.hotspot_track, f)) { + return(LIB3DS_FALSE); + } + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + case LIB3DS_FALL_TRACK_TAG: + { + if (node->type==LIB3DS_LIGHT_NODE) { + if (!lib3ds_lin1_track_read(&node->data.light.falloff_track, f)) { + return(LIB3DS_FALSE); + } + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + case LIB3DS_ROLL_TRACK_TAG: + { + Lib3dsBool result=LIB3DS_TRUE; + + switch (node->type) { + case LIB3DS_CAMERA_NODE: + result=lib3ds_lin1_track_read(&node->data.camera.roll_track, f); + break; + case LIB3DS_LIGHT_NODE: + result=lib3ds_lin1_track_read(&node->data.light.roll_track, f); + break; + default: + lib3ds_chunk_unknown(chunk); + } + if (!result) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_HIDE_TRACK_TAG: + { + if (node->type==LIB3DS_OBJECT_NODE) { + if (!lib3ds_bool_track_read(&node->data.object.hide_track, f)) { + return(LIB3DS_FALSE); + } + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + case LIB3DS_MORPH_SMOOTH: + { + if (node->type==LIB3DS_OBJECT_NODE) { + node->data.object.morph_smooth=lib3ds_float_read(f); + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + case LIB3DS_MORPH_TRACK_TAG: + { + if (node->type==LIB3DS_OBJECT_NODE) { + if (!lib3ds_morph_track_read(&node->data.object.morph_track, f)) { + return(LIB3DS_FALSE); + } + } + else { + lib3ds_chunk_unknown(chunk); + } + } + break; + default: + lib3ds_chunk_unknown(chunk); + } + } + + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } @@ -731,397 +666,346 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) Lib3dsBool lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) { + Lib3dsChunk c; + + switch (node->type) { + case LIB3DS_AMBIENT_NODE: + c.chunk=LIB3DS_AMBIENT_NODE_TAG; + break; + case LIB3DS_OBJECT_NODE: + c.chunk=LIB3DS_OBJECT_NODE_TAG; + break; + case LIB3DS_CAMERA_NODE: + c.chunk=LIB3DS_CAMERA_NODE_TAG; + break; + case LIB3DS_TARGET_NODE: + c.chunk=LIB3DS_TARGET_NODE_TAG; + break; + case LIB3DS_LIGHT_NODE: + if (lib3ds_file_node_by_name(file, node->name, LIB3DS_SPOT_NODE)) { + c.chunk=LIB3DS_SPOTLIGHT_NODE_TAG; + } + else { + c.chunk=LIB3DS_LIGHT_NODE_TAG; + } + break; + case LIB3DS_SPOT_NODE: + c.chunk=LIB3DS_L_TARGET_NODE_TAG; + break; + default: + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + + { /*---- LIB3DS_NODE_ID ----*/ Lib3dsChunk c; + c.chunk=LIB3DS_NODE_ID; + c.size=8; + lib3ds_chunk_write(&c,f); + lib3ds_intw_write(node->node_id,f); + } - switch (node->type) - { - case LIB3DS_AMBIENT_NODE: - c.chunk=LIB3DS_AMBIENT_NODE_TAG; - break; - case LIB3DS_OBJECT_NODE: - c.chunk=LIB3DS_OBJECT_NODE_TAG; - break; - case LIB3DS_CAMERA_NODE: - c.chunk=LIB3DS_CAMERA_NODE_TAG; - break; - case LIB3DS_TARGET_NODE: - c.chunk=LIB3DS_TARGET_NODE_TAG; - break; - case LIB3DS_LIGHT_NODE: - if (lib3ds_file_node_by_name(file, node->name, LIB3DS_SPOT_NODE)) - { - c.chunk=LIB3DS_SPOTLIGHT_NODE_TAG; - } - else - { - c.chunk=LIB3DS_LIGHT_NODE_TAG; - } - break; - case LIB3DS_SPOT_NODE: - c.chunk=LIB3DS_L_TARGET_NODE_TAG; - break; - default: - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } + { /*---- LIB3DS_NODE_HDR ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_NODE_HDR; + c.size=6+ 1+strlen(node->name) +2+2+2; + lib3ds_chunk_write(&c,f); + lib3ds_string_write(node->name,f); + lib3ds_word_write(node->flags1,f); + lib3ds_word_write(node->flags2,f); + lib3ds_word_write(node->parent_id,f); + } - { /*---- LIB3DS_NODE_ID ----*/ + switch (c.chunk) { + case LIB3DS_AMBIENT_NODE_TAG: + { /*---- LIB3DS_COL_TRACK_TAG ----*/ Lib3dsChunk c; - c.chunk=LIB3DS_NODE_ID; - c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(node->id,f); - } - - { /*---- LIB3DS_NODE_HDR ----*/ + c.chunk=LIB3DS_COL_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin3_track_write(&node->data.ambient.col_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_OBJECT_NODE_TAG: + { /*---- LIB3DS_PIVOT ----*/ Lib3dsChunk c; - c.chunk=LIB3DS_NODE_HDR; - c.size=6+ 1+strlen(node->name) +2+2+2; + c.chunk=LIB3DS_PIVOT; + c.size=18; lib3ds_chunk_write(&c,f); - lib3ds_string_write(node->name,f); - lib3ds_word_write(node->flags1,f); - lib3ds_word_write(node->flags2,f); - lib3ds_word_write(node->parent_id,f); - } + lib3ds_vector_write(node->data.object.pivot,f); + } + { /*---- LIB3DS_INSTANCE_NAME ----*/ + Lib3dsChunk c; + const char *name; + if (strlen(node->data.object.instance)) { + name=node->data.object.instance; - switch (c.chunk) - { - case LIB3DS_AMBIENT_NODE_TAG: - { /*---- LIB3DS_COL_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_COL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin3_track_write(&node->data.ambient.col_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + c.chunk=LIB3DS_INSTANCE_NAME; + c.size=6+1+strlen(name); + lib3ds_chunk_write(&c,f); + lib3ds_string_write(name,f); } - break; - case LIB3DS_OBJECT_NODE_TAG: - { /*---- LIB3DS_PIVOT ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_PIVOT; - c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(node->data.object.pivot,f); + } + { + int i; + for (i=0; i<3; ++i) { + if ((fabs(node->data.object.bbox_min[i])>LIB3DS_EPSILON) || + (fabs(node->data.object.bbox_max[i])>LIB3DS_EPSILON)) { + break; + } } - { /*---- LIB3DS_INSTANCE_NAME ----*/ - Lib3dsChunk c; - const char *name; - if (strlen(node->data.object.instance)) - { - name=node->data.object.instance; - } - else - { - name=node->name; - } - c.chunk=LIB3DS_INSTANCE_NAME; - c.size=6+1+strlen(name); - lib3ds_chunk_write(&c,f); - lib3ds_string_write(name,f); + + if (i<3) { /*---- LIB3DS_BOUNDBOX ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_BOUNDBOX; + c.size=30; + lib3ds_chunk_write(&c,f); + lib3ds_vector_write(node->data.object.bbox_min, f); + lib3ds_vector_write(node->data.object.bbox_max, f); } - { /*---- LIB3DS_BOUNDBOX ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_BOUNDBOX; - c.size=30; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(node->data.object.bbox_min, f); - lib3ds_vector_write(node->data.object.bbox_max, f); + } + { /*---- LIB3DS_POS_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_POS_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_POS_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin3_track_write(&node->data.object.pos_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_lin3_track_write(&node->data.object.pos_track,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_ROT_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_ROT_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_quat_track_write(&node->data.object.rot_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_SCL_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_SCL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin3_track_write(&node->data.object.scl_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + } + { /*---- LIB3DS_ROT_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_ROT_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_HIDE_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_HIDE_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_bool_track_write(&node->data.object.hide_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_quat_track_write(&node->data.object.rot_track,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_MORPH_SMOOTH ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_MORPH_SMOOTH; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(node->data.object.morph_smooth,f); + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); } - break; - case LIB3DS_CAMERA_NODE_TAG: - { /*---- LIB3DS_POS_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin3_track_write(&node->data.camera.pos_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + } + { /*---- LIB3DS_SCL_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_SCL_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_FOV_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_FOV_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin1_track_write(&node->data.camera.fov_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_lin3_track_write(&node->data.object.scl_track,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_ROLL_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_ROLL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin1_track_write(&node->data.camera.roll_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); } - break; - case LIB3DS_TARGET_NODE_TAG: - { /*---- LIB3DS_POS_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin3_track_write(&node->data.target.pos_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + } + if (node->data.object.hide_track.keyL) { /*---- LIB3DS_HIDE_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_HIDE_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); } - break; - case LIB3DS_LIGHT_NODE_TAG: - { /*---- LIB3DS_POS_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin3_track_write(&node->data.light.pos_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_bool_track_write(&node->data.object.hide_track,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_COL_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_COL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin3_track_write(&node->data.light.col_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); } - break; - case LIB3DS_SPOTLIGHT_NODE_TAG: - { /*---- LIB3DS_POS_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin3_track_write(&node->data.light.pos_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + } + if (fabs(node->data.object.morph_smooth)>LIB3DS_EPSILON){ /*---- LIB3DS_MORPH_SMOOTH ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_MORPH_SMOOTH; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(node->data.object.morph_smooth,f); + } + break; + case LIB3DS_CAMERA_NODE_TAG: + { /*---- LIB3DS_POS_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_POS_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_COL_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_COL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin3_track_write(&node->data.light.col_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_lin3_track_write(&node->data.camera.pos_track,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_HOT_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_HOT_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin1_track_write(&node->data.light.hotspot_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_FALL_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_FALL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin1_track_write(&node->data.light.falloff_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + } + { /*---- LIB3DS_FOV_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_FOV_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); } - { /*---- LIB3DS_ROLL_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_ROLL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin1_track_write(&node->data.light.roll_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_lin1_track_write(&node->data.camera.fov_track,f)) { + return(LIB3DS_FALSE); } - break; - case LIB3DS_L_TARGET_NODE_TAG: - { /*---- LIB3DS_POS_TRACK_TAG ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_lin3_track_write(&node->data.spot.pos_track,f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); } - break; - default: - return(LIB3DS_FALSE); - } + } + { /*---- LIB3DS_ROLL_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_ROLL_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin1_track_write(&node->data.camera.roll_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_TARGET_NODE_TAG: + { /*---- LIB3DS_POS_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_POS_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin3_track_write(&node->data.target.pos_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_LIGHT_NODE_TAG: + { /*---- LIB3DS_POS_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_POS_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin3_track_write(&node->data.light.pos_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + { /*---- LIB3DS_COL_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_COL_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin3_track_write(&node->data.light.col_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_SPOTLIGHT_NODE_TAG: + { /*---- LIB3DS_POS_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_POS_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin3_track_write(&node->data.light.pos_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + { /*---- LIB3DS_COL_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_COL_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin3_track_write(&node->data.light.col_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + { /*---- LIB3DS_HOT_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_HOT_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin1_track_write(&node->data.light.hotspot_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + { /*---- LIB3DS_FALL_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_FALL_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin1_track_write(&node->data.light.falloff_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + { /*---- LIB3DS_ROLL_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_ROLL_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin1_track_write(&node->data.light.roll_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + break; + case LIB3DS_L_TARGET_NODE_TAG: + { /*---- LIB3DS_POS_TRACK_TAG ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_POS_TRACK_TAG; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_lin3_track_write(&node->data.spot.pos_track,f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + break; + default: + return(LIB3DS_FALSE); + } - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -1207,3 +1091,5 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) \sa _Lib3dsMorphTrack */ + + diff --git a/src/osgPlugins/lib3ds/node.h b/src/osgPlugins/lib3ds/node.h index 78438c7ba..0492ce3bc 100644 --- a/src/osgPlugins/lib3ds/node.h +++ b/src/osgPlugins/lib3ds/node.h @@ -138,7 +138,7 @@ struct _Lib3dsNode { Lib3dsNode *childs;\ Lib3dsNode *parent;\ Lib3dsNodeTypes type;\ - Lib3dsWord id;\ + Lib3dsWord node_id;\ char name[64];\ Lib3dsWord flags1;\ Lib3dsWord flags2;\ @@ -157,7 +157,7 @@ extern LIB3DSAPI void lib3ds_node_free(Lib3dsNode *node); extern LIB3DSAPI void lib3ds_node_eval(Lib3dsNode *node, Lib3dsFloat t); extern LIB3DSAPI Lib3dsNode* lib3ds_node_by_name(Lib3dsNode *node, const char* name, Lib3dsNodeTypes type); -extern LIB3DSAPI Lib3dsNode* lib3ds_node_by_id(Lib3dsNode *node, Lib3dsWord id); +extern LIB3DSAPI Lib3dsNode* lib3ds_node_by_id(Lib3dsNode *node, Lib3dsWord node_id); extern LIB3DSAPI void lib3ds_node_dump(Lib3dsNode *node, Lib3dsIntd level); extern LIB3DSAPI Lib3dsBool lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *file, FILE *f); extern LIB3DSAPI Lib3dsBool lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f); diff --git a/src/osgPlugins/lib3ds/quat.cpp b/src/osgPlugins/lib3ds/quat.cpp index 11e2ad242..17bb02b78 100644 --- a/src/osgPlugins/lib3ds/quat.cpp +++ b/src/osgPlugins/lib3ds/quat.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -23,6 +23,7 @@ #include #include + /*! * \defgroup quat Quaternion Mathematics * @@ -33,13 +34,14 @@ * \ingroup quat */ + /*! * \ingroup quat */ void lib3ds_quat_zero(Lib3dsQuat c) { - c[0]=c[1]=c[2]=c[3]=0.0f; + c[0]=c[1]=c[2]=c[3]=0.0f; } @@ -49,22 +51,21 @@ lib3ds_quat_zero(Lib3dsQuat c) void lib3ds_quat_identity(Lib3dsQuat c) { - c[0]=c[1]=c[2]=0.0f; - c[3]=1.0f; + c[0]=c[1]=c[2]=0.0f; + c[3]=1.0f; } /*! * \ingroup quat */ -void +void lib3ds_quat_copy(Lib3dsQuat dest, Lib3dsQuat src) { - int i; - for (i=0; i<4; ++i) - { - dest[i]=src[i]; - } + int i; + for (i=0; i<4; ++i) { + dest[i]=src[i]; + } } @@ -74,24 +75,22 @@ lib3ds_quat_copy(Lib3dsQuat dest, Lib3dsQuat src) void lib3ds_quat_axis_angle(Lib3dsQuat c, Lib3dsVector axis, Lib3dsFloat angle) { - Lib3dsDouble omega,s; - Lib3dsDouble l; + Lib3dsDouble omega,s; + Lib3dsDouble l; - l=sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]); - if (lLIB3DS_EPSILON) - { - if (fabs(l)>1.0f) l/=fabs(l); - om=acos(l); - sinom=sin(om); - if (fabs(sinom)>LIB3DS_EPSILON) - { - sp=sin((1.0f-t)*om)/sinom; - sq=sin(t*om)/sinom; - } - else - { - sp=1.0f-t; - sq=t; - } - c[0]=(Lib3dsFloat)(sp*a[0] + sq*b[0]); - c[1]=(Lib3dsFloat)(sp*a[1] + sq*b[1]); - c[2]=(Lib3dsFloat)(sp*a[2] + sq*b[2]); - c[3]=(Lib3dsFloat)(sp*a[3] + sq*b[3]); + l=a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]; + if ((1.0+l)>LIB3DS_EPSILON) { + if (fabs(l)>1.0f) l/=fabs(l); + om=acos(l); + sinom=sin(om); + if (fabs(sinom)>LIB3DS_EPSILON) { + sp=sin((1.0f-t)*om)/sinom; + sq=sin(t*om)/sinom; } - else - { - q[0]=-a[1]; - q[1]=a[0]; - q[2]=-a[3]; - q[3]=a[2]; - sp=sin((1.0-t)*LIB3DS_HALFPI); - sq=sin(t*LIB3DS_HALFPI); - c[0]=(Lib3dsFloat)(sp*a[0] + sq*q[0]); - c[1]=(Lib3dsFloat)(sp*a[1] + sq*q[1]); - c[2]=(Lib3dsFloat)(sp*a[2] + sq*q[2]); - c[3]=(Lib3dsFloat)(sp*a[3] + sq*q[3]); + else { + sp=1.0f-t; + sq=t; } + c[0]=(Lib3dsFloat)(sp*a[0] + sq*b[0]); + c[1]=(Lib3dsFloat)(sp*a[1] + sq*b[1]); + c[2]=(Lib3dsFloat)(sp*a[2] + sq*b[2]); + c[3]=(Lib3dsFloat)(sp*a[3] + sq*b[3]); + } + else { + q[0]=-a[1]; + q[1]=a[0]; + q[2]=-a[3]; + q[3]=a[2]; + sp=sin((1.0-t)*LIB3DS_HALFPI); + sq=sin(t*LIB3DS_HALFPI); + c[0]=(Lib3dsFloat)(sp*a[0] + sq*q[0]); + c[1]=(Lib3dsFloat)(sp*a[1] + sq*q[1]); + c[2]=(Lib3dsFloat)(sp*a[2] + sq*q[2]); + c[3]=(Lib3dsFloat)(sp*a[3] + sq*q[3]); + } } @@ -370,14 +350,14 @@ lib3ds_quat_slerp(Lib3dsQuat c, Lib3dsQuat a, Lib3dsQuat b, Lib3dsFloat t) */ void lib3ds_quat_squad(Lib3dsQuat c, Lib3dsQuat a, Lib3dsQuat p, Lib3dsQuat q, -Lib3dsQuat b, Lib3dsFloat t) + Lib3dsQuat b, Lib3dsFloat t) { - Lib3dsQuat ab; - Lib3dsQuat pq; + Lib3dsQuat ab; + Lib3dsQuat pq; - lib3ds_quat_slerp(ab,a,b,t); - lib3ds_quat_slerp(pq,p,q,t); - lib3ds_quat_slerp(c,ab,pq,2*t*(1-t)); + lib3ds_quat_slerp(ab,a,b,t); + lib3ds_quat_slerp(pq,p,q,t); + lib3ds_quat_slerp(c,ab,pq,2*t*(1-t)); } @@ -387,18 +367,17 @@ Lib3dsQuat b, Lib3dsFloat t) void lib3ds_quat_tangent(Lib3dsQuat c, Lib3dsQuat p, Lib3dsQuat q, Lib3dsQuat n) { - Lib3dsQuat dn,dp,x; - int i; + Lib3dsQuat dn,dp,x; + int i; - lib3ds_quat_ln_dif(dn, q, n); - lib3ds_quat_ln_dif(dp, q, p); + lib3ds_quat_ln_dif(dn, q, n); + lib3ds_quat_ln_dif(dp, q, p); - for (i=0; i<4; i++) - { - x[i]=-1.0f/4.0f*(dn[i]+dp[i]); - } - lib3ds_quat_exp(x); - lib3ds_quat_mul(c,q,x); + for (i=0; i<4; i++) { + x[i]=-1.0f/4.0f*(dn[i]+dp[i]); + } + lib3ds_quat_exp(x); + lib3ds_quat_mul(c,q,x); } @@ -408,5 +387,6 @@ lib3ds_quat_tangent(Lib3dsQuat c, Lib3dsQuat p, Lib3dsQuat q, Lib3dsQuat n) void lib3ds_quat_dump(Lib3dsQuat q) { - printf("%f %f %f %f\n", q[0], q[1], q[2], q[3]); + printf("%f %f %f %f\n", q[0], q[1], q[2], q[3]); } + diff --git a/src/osgPlugins/lib3ds/readwrite.cpp b/src/osgPlugins/lib3ds/readwrite.cpp index e5d2a7c55..2b0e6cc61 100644 --- a/src/osgPlugins/lib3ds/readwrite.cpp +++ b/src/osgPlugins/lib3ds/readwrite.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -22,168 +22,170 @@ #define LIB3DS_EXPORT #include + /*! * \defgroup readwrite Portable Binary Input/Ouput * * \author J.E. Hoffmann */ + /*! * \ingroup readwrite * - * Read a byte from a file stream. + * Read a byte from a file stream. * - * \param f Input file stream. + * \param f Input file stream. * - * \return The byte read. + * \return The byte read. */ Lib3dsByte lib3ds_byte_read(FILE *f) { - Lib3dsByte b; + Lib3dsByte b; - ASSERT(f); - fread(&b,1,1,f); - return(b); + ASSERT(f); + fread(&b,1,1,f); + return(b); } /** - * Read a word from a file stream in little endian format. + * Read a word from a file stream in little endian format. * - * \param f Input file stream. + * \param f Input file stream. * - * \return The word read. + * \return The word read. */ Lib3dsWord lib3ds_word_read(FILE *f) { - Lib3dsByte b[2]; - Lib3dsWord w; + Lib3dsByte b[2]; + Lib3dsWord w; - ASSERT(f); - fread(b,2,1,f); - w=((Lib3dsWord)b[1] << 8) | - ((Lib3dsWord)b[0]); - return(w); + ASSERT(f); + fread(b,2,1,f); + w=((Lib3dsWord)b[1] << 8) | + ((Lib3dsWord)b[0]); + return(w); } /*! * \ingroup readwrite * - * Read a dword from file a stream in little endian format. + * Read a dword from file a stream in little endian format. * - * \param f Input file stream. + * \param f Input file stream. * - * \return The dword read. + * \return The dword read. */ Lib3dsDword lib3ds_dword_read(FILE *f) { - Lib3dsByte b[4]; - Lib3dsDword d; - - ASSERT(f); - fread(b,4,1,f); - d=((Lib3dsDword)b[3] << 24) | - ((Lib3dsDword)b[2] << 16) | - ((Lib3dsDword)b[1] << 8) | - ((Lib3dsDword)b[0]); - return(d); + Lib3dsByte b[4]; + Lib3dsDword d; + + ASSERT(f); + fread(b,4,1,f); + d=((Lib3dsDword)b[3] << 24) | + ((Lib3dsDword)b[2] << 16) | + ((Lib3dsDword)b[1] << 8) | + ((Lib3dsDword)b[0]); + return(d); } /*! * \ingroup readwrite * - * Read a signed byte from a file stream. + * Read a signed byte from a file stream. * - * \param f Input file stream. + * \param f Input file stream. * - * \return The signed byte read. + * \return The signed byte read. */ Lib3dsIntb lib3ds_intb_read(FILE *f) { - Lib3dsIntb b; + Lib3dsIntb b; - ASSERT(f); - fread(&b,1,1,f); - return(b); + ASSERT(f); + fread(&b,1,1,f); + return(b); } /*! * \ingroup readwrite * - * Read a signed word from a file stream in little endian format. + * Read a signed word from a file stream in little endian format. * - * \param f Input file stream. + * \param f Input file stream. * - * \return The signed word read. + * \return The signed word read. */ Lib3dsIntw lib3ds_intw_read(FILE *f) { - Lib3dsByte b[2]; - Lib3dsWord w; + Lib3dsByte b[2]; + Lib3dsWord w; - ASSERT(f); - fread(b,2,1,f); - w=((Lib3dsWord)b[1] << 8) | - ((Lib3dsWord)b[0]); - return((Lib3dsIntw)w); + ASSERT(f); + fread(b,2,1,f); + w=((Lib3dsWord)b[1] << 8) | + ((Lib3dsWord)b[0]); + return((Lib3dsIntw)w); } /*! * \ingroup readwrite * - * Read a signed dword a from file stream in little endian format. + * Read a signed dword a from file stream in little endian format. * - * \param f Input file stream. + * \param f Input file stream. * - * \return The signed dword read. + * \return The signed dword read. */ Lib3dsIntd lib3ds_intd_read(FILE *f) { - Lib3dsByte b[4]; - Lib3dsDword d; - - ASSERT(f); - fread(b,4,1,f); - d=((Lib3dsDword)b[3] << 24) | - ((Lib3dsDword)b[2] << 16) | - ((Lib3dsDword)b[1] << 8) | - ((Lib3dsDword)b[0]); - return((Lib3dsIntd)d); + Lib3dsByte b[4]; + Lib3dsDword d; + + ASSERT(f); + fread(b,4,1,f); + d=((Lib3dsDword)b[3] << 24) | + ((Lib3dsDword)b[2] << 16) | + ((Lib3dsDword)b[1] << 8) | + ((Lib3dsDword)b[0]); + return((Lib3dsIntd)d); } /*! * \ingroup readwrite * - * Read a float from a file stream in little endian format. + * Read a float from a file stream in little endian format. * - * \param f Input file stream. + * \param f Input file stream. * - * \return The float read. + * \return The float read. */ Lib3dsFloat lib3ds_float_read(FILE *f) { - Lib3dsByte b[4]; - Lib3dsDword d; + Lib3dsByte b[4]; + Lib3dsDword d; - ASSERT(f); - fread(b,4,1,f); - d=((Lib3dsDword)b[3] << 24) | - ((Lib3dsDword)b[2] << 16) | - ((Lib3dsDword)b[1] << 8) | - ((Lib3dsDword)b[0]); - return(*((Lib3dsFloat*)&d)); + ASSERT(f); + fread(b,4,1,f); + d=((Lib3dsDword)b[3] << 24) | + ((Lib3dsDword)b[2] << 16) | + ((Lib3dsDword)b[1] << 8) | + ((Lib3dsDword)b[0]); + return(*((Lib3dsFloat*)&d)); } @@ -191,25 +193,24 @@ lib3ds_float_read(FILE *f) * \ingroup readwrite * \ingroup vector * - * Read a vector from a file stream in little endian format. + * Read a vector from a file stream in little endian format. * - * \param v The vector to store the data. - * \param f Input file stream. + * \param v The vector to store the data. + * \param f Input file stream. * - * \return The float read. + * \return The float read. */ Lib3dsBool lib3ds_vector_read(Lib3dsVector v, FILE *f) { - v[0]=lib3ds_float_read(f); - v[1]=lib3ds_float_read(f); - v[2]=lib3ds_float_read(f); + v[0]=lib3ds_float_read(f); + v[1]=lib3ds_float_read(f); + v[2]=lib3ds_float_read(f); - if (ferror(f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + if (ferror(f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -219,15 +220,14 @@ lib3ds_vector_read(Lib3dsVector v, FILE *f) Lib3dsBool lib3ds_rgb_read(Lib3dsRgb rgb, FILE *f) { - rgb[0]=lib3ds_float_read(f); - rgb[1]=lib3ds_float_read(f); - rgb[2]=lib3ds_float_read(f); + rgb[0]=lib3ds_float_read(f); + rgb[1]=lib3ds_float_read(f); + rgb[2]=lib3ds_float_read(f); - if (ferror(f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + if (ferror(f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -245,20 +245,17 @@ lib3ds_rgb_read(Lib3dsRgb rgb, FILE *f) Lib3dsBool lib3ds_string_read(char *s, int buflen, FILE *f) { - int k=0; - ASSERT(f); - while ((*s++=fgetc(f))!=0) - { - if (++k>buflen) - { - return(LIB3DS_FALSE); - } + int k=0; + ASSERT(f); + while ((*s++=fgetc(f))!=0) { + if (++k>=buflen) { + return(LIB3DS_FALSE); } - if (ferror(f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + } + if (ferror(f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -275,12 +272,11 @@ lib3ds_string_read(char *s, int buflen, FILE *f) Lib3dsBool lib3ds_byte_write(Lib3dsByte b, FILE *f) { - ASSERT(f); - if (fwrite(&b,1,1,f)!=1) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + ASSERT(f); + if (fwrite(&b,1,1,f)!=1) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -297,16 +293,15 @@ lib3ds_byte_write(Lib3dsByte b, FILE *f) Lib3dsBool lib3ds_word_write(Lib3dsWord w, FILE *f) { - Lib3dsByte b[2]; + Lib3dsByte b[2]; - ASSERT(f); - b[1]=((Lib3dsWord)w & 0xFF00) >> 8; - b[0]=((Lib3dsWord)w & 0x00FF); - if (fwrite(b,2,1,f)!=1) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + ASSERT(f); + b[1]=((Lib3dsWord)w & 0xFF00) >> 8; + b[0]=((Lib3dsWord)w & 0x00FF); + if (fwrite(b,2,1,f)!=1) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -323,18 +318,17 @@ lib3ds_word_write(Lib3dsWord w, FILE *f) Lib3dsBool lib3ds_dword_write(Lib3dsDword d, FILE *f) { - Lib3dsByte b[4]; + Lib3dsByte b[4]; - ASSERT(f); - b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24); - b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16); - b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8); - b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF)); - if (fwrite(b,4,1,f)!=1) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + ASSERT(f); + b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24); + b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16); + b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8); + b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF)); + if (fwrite(b,4,1,f)!=1) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -351,12 +345,11 @@ lib3ds_dword_write(Lib3dsDword d, FILE *f) Lib3dsBool lib3ds_intb_write(Lib3dsIntb b, FILE *f) { - ASSERT(f); - if (fwrite(&b,1,1,f)!=1) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + ASSERT(f); + if (fwrite(&b,1,1,f)!=1) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -373,16 +366,15 @@ lib3ds_intb_write(Lib3dsIntb b, FILE *f) Lib3dsBool lib3ds_intw_write(Lib3dsIntw w, FILE *f) { - Lib3dsByte b[2]; + Lib3dsByte b[2]; - ASSERT(f); - b[1]=((Lib3dsWord)w & 0xFF00) >> 8; - b[0]=((Lib3dsWord)w & 0x00FF); - if (fwrite(b,2,1,f)!=1) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + ASSERT(f); + b[1]=((Lib3dsWord)w & 0xFF00) >> 8; + b[0]=((Lib3dsWord)w & 0x00FF); + if (fwrite(b,2,1,f)!=1) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -399,18 +391,17 @@ lib3ds_intw_write(Lib3dsIntw w, FILE *f) Lib3dsBool lib3ds_intd_write(Lib3dsIntd d, FILE *f) { - Lib3dsByte b[4]; + Lib3dsByte b[4]; - ASSERT(f); - b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24); - b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16); - b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8); - b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF)); - if (fwrite(b,4,1,f)!=1) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + ASSERT(f); + b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24); + b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16); + b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8); + b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF)); + if (fwrite(b,4,1,f)!=1) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -427,20 +418,19 @@ lib3ds_intd_write(Lib3dsIntd d, FILE *f) Lib3dsBool lib3ds_float_write(Lib3dsFloat l, FILE *f) { - Lib3dsByte b[4]; - Lib3dsDword d; + Lib3dsByte b[4]; + Lib3dsDword d; - ASSERT(f); - d=*((Lib3dsDword*)&l); - b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24); - b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16); - b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8); - b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF)); - if (fwrite(b,4,1,f)!=1) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + ASSERT(f); + d=*((Lib3dsDword*)&l); + b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24); + b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16); + b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8); + b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF)); + if (fwrite(b,4,1,f)!=1) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -448,27 +438,24 @@ lib3ds_float_write(Lib3dsFloat l, FILE *f) * \ingroup readwrite * \ingroup vector * - * Writes a vector into a file stream in little endian format. + * Writes a vector into a file stream in little endian format. * - * \param v The vector to write to the file stream. - * \param f Input file stream. + * \param v The vector to write to the file stream. + * \param f Input file stream. */ Lib3dsBool lib3ds_vector_write(Lib3dsVector v, FILE *f) { - if (!lib3ds_float_write(v[0], f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_float_write(v[1], f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_float_write(v[2], f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + if (!lib3ds_float_write(v[0], f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_float_write(v[1], f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_float_write(v[2], f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -478,19 +465,16 @@ lib3ds_vector_write(Lib3dsVector v, FILE *f) Lib3dsBool lib3ds_rgb_write(Lib3dsRgb rgb, FILE *f) { - if (!lib3ds_float_write(rgb[0], f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_float_write(rgb[1], f)) - { - return(LIB3DS_FALSE); - } - if (!lib3ds_float_write(rgb[2], f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + if (!lib3ds_float_write(rgb[0], f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_float_write(rgb[1], f)) { + return(LIB3DS_FALSE); + } + if (!lib3ds_float_write(rgb[2], f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } @@ -507,12 +491,12 @@ lib3ds_rgb_write(Lib3dsRgb rgb, FILE *f) Lib3dsBool lib3ds_string_write(const char *s, FILE *f) { - ASSERT(s); - ASSERT(f); - do fputc(*s,f); while (*s++); - if (ferror(f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + ASSERT(s); + ASSERT(f); + do fputc(*s,f); while (*s++); + if (ferror(f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } + diff --git a/src/osgPlugins/lib3ds/shadow.cpp b/src/osgPlugins/lib3ds/shadow.cpp index 60e1c2de5..e32fc7a3e 100644 --- a/src/osgPlugins/lib3ds/shadow.cpp +++ b/src/osgPlugins/lib3ds/shadow.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -23,6 +23,8 @@ #include #include #include +#include + /*! * \defgroup shadow Shadow Map Settings @@ -30,118 +32,122 @@ * \author J.E. Hoffmann */ + /*! - * \ingroup shadow + * \ingroup shadow */ Lib3dsBool lib3ds_shadow_read(Lib3dsShadow *shadow, FILE *f) { - Lib3dsChunk c; + Lib3dsChunk c; - if (!lib3ds_chunk_read(&c, f)) - { - return(LIB3DS_FALSE); - } - - switch (c.chunk) - { - case LIB3DS_SHADOW_MAP_SIZE: - { - shadow->map_size=lib3ds_intw_read(f); - } - break; - case LIB3DS_LO_SHADOW_BIAS: - { - shadow->lo_bias=lib3ds_float_read(f); - } - break; - case LIB3DS_HI_SHADOW_BIAS: - { - shadow->hi_bias=lib3ds_float_read(f); - } - break; - case LIB3DS_SHADOW_SAMPLES: - { - shadow->samples=lib3ds_intw_read(f); - } - break; - case LIB3DS_SHADOW_RANGE: - { - shadow->range=lib3ds_intw_read(f); - } - break; - case LIB3DS_SHADOW_FILTER: - { - shadow->filter=lib3ds_float_read(f); - } - break; - case LIB3DS_RAY_BIAS: - { - shadow->ray_bias=lib3ds_float_read(f); - } - break; - } - - return(LIB3DS_TRUE); + if (!lib3ds_chunk_read(&c, f)) { + return(LIB3DS_FALSE); + } + + switch (c.chunk) { + case LIB3DS_SHADOW_MAP_SIZE: + { + shadow->map_size=lib3ds_intw_read(f); + } + break; + case LIB3DS_LO_SHADOW_BIAS: + { + shadow->lo_bias=lib3ds_float_read(f); + } + break; + case LIB3DS_HI_SHADOW_BIAS: + { + shadow->hi_bias=lib3ds_float_read(f); + } + break; + case LIB3DS_SHADOW_SAMPLES: + { + shadow->samples=lib3ds_intw_read(f); + } + break; + case LIB3DS_SHADOW_RANGE: + { + shadow->range=lib3ds_intd_read(f); + } + break; + case LIB3DS_SHADOW_FILTER: + { + shadow->filter=lib3ds_float_read(f); + } + break; + case LIB3DS_RAY_BIAS: + { + shadow->ray_bias=lib3ds_float_read(f); + } + break; + } + + return(LIB3DS_TRUE); } /*! - * \ingroup shadow + * \ingroup shadow */ Lib3dsBool lib3ds_shadow_write(Lib3dsShadow *shadow, FILE *f) { - { /*---- LIB3DS_LO_SHADOW_BIAS ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_LO_SHADOW_BIAS; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(shadow->lo_bias,f); - } - { /*---- LIB3DS_HI_SHADOW_BIAS ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_HI_SHADOW_BIAS; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(shadow->hi_bias,f); - } - { /*---- LIB3DS_SHADOW_MAP_SIZE ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_SHADOW_MAP_SIZE; - c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(shadow->map_size,f); - } - { /*---- LIB3DS_SHADOW_SAMPLES ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_SHADOW_SAMPLES; - c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(shadow->samples,f); - } - { /*---- LIB3DS_SHADOW_RANGE ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_SHADOW_RANGE; - c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(shadow->range,f); - } - { /*---- LIB3DS_SHADOW_FILTER ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_SHADOW_FILTER; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(shadow->filter,f); - } - { /*---- LIB3DS_RAY_BIAS ----*/ - Lib3dsChunk c; - c.chunk=LIB3DS_RAY_BIAS; - c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(shadow->ray_bias,f); - } - return(LIB3DS_TRUE); + if (fabs(shadow->lo_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_LO_SHADOW_BIAS ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_LO_SHADOW_BIAS; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(shadow->lo_bias,f); + } + + if (fabs(shadow->hi_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_HI_SHADOW_BIAS ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_HI_SHADOW_BIAS; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(shadow->hi_bias,f); + } + + if (shadow->map_size) { /*---- LIB3DS_SHADOW_MAP_SIZE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_SHADOW_MAP_SIZE; + c.size=8; + lib3ds_chunk_write(&c,f); + lib3ds_intw_write(shadow->map_size,f); + } + + if (shadow->samples) { /*---- LIB3DS_SHADOW_SAMPLES ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_SHADOW_SAMPLES; + c.size=8; + lib3ds_chunk_write(&c,f); + lib3ds_intw_write(shadow->samples,f); + } + + if (shadow->range) { /*---- LIB3DS_SHADOW_RANGE ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_SHADOW_RANGE; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_intd_write(shadow->range,f); + } + + if (fabs(shadow->filter)>LIB3DS_EPSILON) { /*---- LIB3DS_SHADOW_FILTER ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_SHADOW_FILTER; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(shadow->filter,f); + } + if (fabs(shadow->ray_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_RAY_BIAS ----*/ + Lib3dsChunk c; + c.chunk=LIB3DS_RAY_BIAS; + c.size=10; + lib3ds_chunk_write(&c,f); + lib3ds_float_write(shadow->ray_bias,f); + } + return(LIB3DS_TRUE); } diff --git a/src/osgPlugins/lib3ds/shadow.h b/src/osgPlugins/lib3ds/shadow.h index 48d26bea9..e178492eb 100644 --- a/src/osgPlugins/lib3ds/shadow.h +++ b/src/osgPlugins/lib3ds/shadow.h @@ -40,7 +40,7 @@ struct _Lib3dsShadow { Lib3dsFloat lo_bias; Lib3dsFloat hi_bias; Lib3dsIntw samples; - Lib3dsIntw range; + Lib3dsIntd range; Lib3dsFloat filter; Lib3dsFloat ray_bias; }; diff --git a/src/osgPlugins/lib3ds/tcb.cpp b/src/osgPlugins/lib3ds/tcb.cpp index 6c62e8c87..fae26e043 100644 --- a/src/osgPlugins/lib3ds/tcb.cpp +++ b/src/osgPlugins/lib3ds/tcb.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -24,125 +24,116 @@ #include #include + /*! * \defgroup tcb Tension/Continuity/Bias Splines * * \author J.E. Hoffmann */ + /*! - * \ingroup tcb + * \ingroup tcb */ void lib3ds_tcb(Lib3dsTcb *p, Lib3dsTcb *pc, Lib3dsTcb *c, Lib3dsTcb *nc, Lib3dsTcb *n, -Lib3dsFloat *ksm, Lib3dsFloat *ksp, Lib3dsFloat *kdm, Lib3dsFloat *kdp) + Lib3dsFloat *ksm, Lib3dsFloat *ksp, Lib3dsFloat *kdm, Lib3dsFloat *kdp) { - Lib3dsFloat tm,cm,cp,bm,bp,tmcm,tmcp,cc; - Lib3dsFloat dt,fp,fn; + Lib3dsFloat tm,cm,cp,bm,bp,tmcm,tmcp,cc; + Lib3dsFloat dt,fp,fn; - if (!pc) - { - pc=c; - } - if (!nc) - { - nc=c; - } + if (!pc) { + pc=c; + } + if (!nc) { + nc=c; + } + + fp=fn=1.0f; + if (p&&n) { + dt=0.5f*(Lib3dsFloat)(pc->frame-p->frame+n->frame-nc->frame); + fp=((Lib3dsFloat)(pc->frame-p->frame))/dt; + fn=((Lib3dsFloat)(n->frame-nc->frame))/dt; + cc=(Lib3dsFloat)fabs(c->cont); + fp=fp+cc-cc*fp; + fn=fn+cc-cc*fn; + } - fp=fn=1.0f; - if (p&&n) - { - dt=0.5f*(Lib3dsFloat)(pc->frame-p->frame+n->frame-nc->frame); - fp=((Lib3dsFloat)(pc->frame-p->frame))/dt; - fn=((Lib3dsFloat)(n->frame-nc->frame))/dt; - cc=(Lib3dsFloat)fabs(c->cont); - fp=fp+cc-cc*fp; - fn=fn+cc-cc*fn; - } - - cm=1.0f-c->cont; - tm=0.5f*(1.0f-c->tens); - cp=2.0f-cm; - bm=1.0f-c->bias; - bp=2.0f-bm; - tmcm=tm*cm; - tmcp=tm*cp; - *ksm=tmcm*bp*fp; - *ksp=tmcp*bm*fp; - *kdm=tmcp*bp*fn; - *kdp=tmcm*bm*fn; + cm=1.0f-c->cont; + tm=0.5f*(1.0f-c->tens); + cp=2.0f-cm; + bm=1.0f-c->bias; + bp=2.0f-bm; + tmcm=tm*cm; + tmcp=tm*cp; + *ksm=tmcm*bp*fp; + *ksp=tmcp*bm*fp; + *kdm=tmcp*bp*fn; + *kdp=tmcm*bm*fn; } /*! - * \ingroup tcb + * \ingroup tcb */ Lib3dsBool lib3ds_tcb_read(Lib3dsTcb *tcb, FILE *f) { - Lib3dsWord flags; - - tcb->frame=lib3ds_intd_read(f); - tcb->flags=flags=lib3ds_word_read(f); - if (flags&LIB3DS_USE_TENSION) - { - tcb->tens=lib3ds_float_read(f); - } - if (flags&LIB3DS_USE_CONTINUITY) - { - tcb->cont=lib3ds_float_read(f); - } - if (flags&LIB3DS_USE_BIAS) - { - tcb->bias=lib3ds_float_read(f); - } - if (flags&LIB3DS_USE_EASE_TO) - { - tcb->ease_to=lib3ds_float_read(f); - } - if (flags&LIB3DS_USE_EASE_FROM) - { - tcb->ease_from=lib3ds_float_read(f); - } - if (ferror(f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + Lib3dsWord flags; + + tcb->frame=lib3ds_intd_read(f); + tcb->flags=flags=lib3ds_word_read(f); + if (flags&LIB3DS_USE_TENSION) { + tcb->tens=lib3ds_float_read(f); + } + if (flags&LIB3DS_USE_CONTINUITY) { + tcb->cont=lib3ds_float_read(f); + } + if (flags&LIB3DS_USE_BIAS) { + tcb->bias=lib3ds_float_read(f); + } + if (flags&LIB3DS_USE_EASE_TO) { + tcb->ease_to=lib3ds_float_read(f); + } + if (flags&LIB3DS_USE_EASE_FROM) { + tcb->ease_from=lib3ds_float_read(f); + } + if (ferror(f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } /*! - * \ingroup tcb + * \ingroup tcb */ Lib3dsBool lib3ds_tcb_write(Lib3dsTcb *tcb, FILE *f) { - lib3ds_intd_write(tcb->frame,f); - lib3ds_word_write(tcb->flags,f); - if (tcb->flags&LIB3DS_USE_TENSION) - { - lib3ds_float_write(tcb->tens,f); - } - if (tcb->flags&LIB3DS_USE_CONTINUITY) - { - lib3ds_float_write(tcb->cont,f); - } - if (tcb->flags&LIB3DS_USE_BIAS) - { - lib3ds_float_write(tcb->bias,f); - } - if (tcb->flags&LIB3DS_USE_EASE_TO) - { - lib3ds_float_write(tcb->ease_to,f); - } - if (tcb->flags&LIB3DS_USE_EASE_FROM) - { - lib3ds_float_write(tcb->ease_from,f); - } - if (ferror(f)) - { - return(LIB3DS_FALSE); - } - return(LIB3DS_TRUE); + lib3ds_intd_write(tcb->frame,f); + lib3ds_word_write(tcb->flags,f); + if (tcb->flags&LIB3DS_USE_TENSION) { + lib3ds_float_write(tcb->tens,f); + } + if (tcb->flags&LIB3DS_USE_CONTINUITY) { + lib3ds_float_write(tcb->cont,f); + } + if (tcb->flags&LIB3DS_USE_BIAS) { + lib3ds_float_write(tcb->bias,f); + } + if (tcb->flags&LIB3DS_USE_EASE_TO) { + lib3ds_float_write(tcb->ease_to,f); + } + if (tcb->flags&LIB3DS_USE_EASE_FROM) { + lib3ds_float_write(tcb->ease_from,f); + } + if (ferror(f)) { + return(LIB3DS_FALSE); + } + return(LIB3DS_TRUE); } + + + + diff --git a/src/osgPlugins/lib3ds/tracks.cpp b/src/osgPlugins/lib3ds/tracks.cpp index 5f3987fb0..9dd94e08c 100644 --- a/src/osgPlugins/lib3ds/tracks.cpp +++ b/src/osgPlugins/lib3ds/tracks.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -29,1525 +29,1334 @@ #include #include #include -//#include +#include #ifdef WITH_DMALLOC #include #endif + /*! * \defgroup tracks Keyframing Tracks * * \author J.E. Hoffmann */ + /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsBoolKey* lib3ds_bool_key_new() { - Lib3dsBoolKey* k; - k=(Lib3dsBoolKey*)calloc(sizeof(Lib3dsBoolKey), 1); - return(k); + Lib3dsBoolKey* k; + k=(Lib3dsBoolKey*)calloc(sizeof(Lib3dsBoolKey), 1); + return(k); } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_bool_key_free(Lib3dsBoolKey *key) { - ASSERT(key); - free(key); + ASSERT(key); + free(key); } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_bool_track_free_keys(Lib3dsBoolTrack *track) { - Lib3dsBoolKey *p,*q; + Lib3dsBoolKey *p,*q; - ASSERT(track); - for (p=track->keyL; p; p=q) - { - q=p->next; - lib3ds_bool_key_free(p); - } + ASSERT(track); + for (p=track->keyL; p; p=q) { + q=p->next; + lib3ds_bool_key_free(p); + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_bool_track_insert(Lib3dsBoolTrack *track, Lib3dsBoolKey *key) { - ASSERT(track); - ASSERT(key); - ASSERT(!key->next); + ASSERT(track); + ASSERT(key); + ASSERT(!key->next); - if (!track->keyL) - { - track->keyL=key; - key->next=0; + if (!track->keyL) { + track->keyL=key; + key->next=0; + } + else { + Lib3dsBoolKey *k,*p; + + for (p=0,k=track->keyL; k!=0; p=k, k=k->next) { + if (k->tcb.frame>key->tcb.frame) { + break; + } } - else - { - Lib3dsBoolKey *k,*p; - - for (p=0,k=track->keyL; k!=0; p=k, k=k->next) - { - if (k->tcb.frame>key->tcb.frame) - { - break; - } - } - if (!p) - { - key->next=track->keyL; - track->keyL=key; - } - else - { - key->next=k; - p->next=key; - } - - if (k && (key->tcb.frame==k->tcb.frame)) - { - key->next=k->next; - lib3ds_bool_key_free(k); - } + if (!p) { + key->next=track->keyL; + track->keyL=key; } + else { + key->next=k; + p->next=key; + } + + if (k && (key->tcb.frame==k->tcb.frame)) { + key->next=k->next; + lib3ds_bool_key_free(k); + } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_bool_track_remove(Lib3dsBoolTrack *track, Lib3dsIntd frame) { - Lib3dsBoolKey *k,*p; - - ASSERT(track); - if (!track->keyL) - { - return; - } - for (p=0,k=track->keyL; k!=0; p=k, k=k->next) - { - if (k->tcb.frame==frame) - { - if (!p) - { - track->keyL=track->keyL->next; - } - else - { - p->next=k->next; - } - lib3ds_bool_key_free(k); - break; - } + Lib3dsBoolKey *k,*p; + + ASSERT(track); + if (!track->keyL) { + return; + } + for (p=0,k=track->keyL; k!=0; p=k, k=k->next) { + if (k->tcb.frame==frame) { + if (!p) { + track->keyL=track->keyL->next; + } + else { + p->next=k->next; + } + lib3ds_bool_key_free(k); + break; } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_bool_track_eval(Lib3dsBoolTrack *track, Lib3dsBool *p, Lib3dsFloat t) { - Lib3dsBoolKey *k; - Lib3dsBool result; + Lib3dsBoolKey *k; + Lib3dsBool result; - ASSERT(p); - if (!track->keyL) - { - *p=LIB3DS_FALSE; - return; - } - if (!track->keyL->next) - { - *p = LIB3DS_TRUE; - return; - } + ASSERT(p); + if (!track->keyL) { + *p=LIB3DS_FALSE; + return; + } + if (!track->keyL->next) { + *p = LIB3DS_TRUE; + return; + } - result=LIB3DS_FALSE; - k=track->keyL; - while ((t<(Lib3dsFloat)k->tcb.frame) && (t>=(Lib3dsFloat)k->next->tcb.frame)) - { - if (result) - { - result=LIB3DS_FALSE; - } - else - { - result=LIB3DS_TRUE; - } - if (!k->next) - { - if (track->flags&LIB3DS_REPEAT) - { - t-=(Lib3dsFloat)k->tcb.frame; - k=track->keyL; - } - else - { - break; - } - } - else - { - k=k->next; - } + result=LIB3DS_FALSE; + k=track->keyL; + while ((t<(Lib3dsFloat)k->tcb.frame) && (t>=(Lib3dsFloat)k->next->tcb.frame)) { + if (result) { + result=LIB3DS_FALSE; } - *p=result; + else { + result=LIB3DS_TRUE; + } + if (!k->next) { + if (track->flags&LIB3DS_REPEAT) { + t-=(Lib3dsFloat)k->tcb.frame; + k=track->keyL; + } + else { + break; + } + } + else { + k=k->next; + } + } + *p=result; } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsBool lib3ds_bool_track_read(Lib3dsBoolTrack *track, FILE *f) { - int keys; - int i; - Lib3dsBoolKey *k; + int keys; + int i; + Lib3dsBoolKey *k; - track->flags=lib3ds_word_read(f); - lib3ds_dword_read(f); - lib3ds_dword_read(f); - keys=lib3ds_intd_read(f); + track->flags=lib3ds_word_read(f); + lib3ds_dword_read(f); + lib3ds_dword_read(f); + keys=lib3ds_intd_read(f); - for (i=0; itcb, f)) - { - return(LIB3DS_FALSE); - } - lib3ds_bool_track_insert(track, k); + for (i=0; itcb, f)) { + return(LIB3DS_FALSE); } - - return(LIB3DS_TRUE); + lib3ds_bool_track_insert(track, k); + } + + return(LIB3DS_TRUE); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsBool lib3ds_bool_track_write(Lib3dsBoolTrack *track, FILE *f) { - Lib3dsBoolKey *k; - Lib3dsDword num=0; - for (k=track->keyL; k; k=k->next) - { - ++num; - } - lib3ds_word_write((Lib3dsWord)track->flags,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(num,f); + Lib3dsBoolKey *k; + Lib3dsDword num=0; + for (k=track->keyL; k; k=k->next) { + ++num; + } + lib3ds_word_write((Lib3dsWord)track->flags,f); + lib3ds_dword_write(0,f); + lib3ds_dword_write(0,f); + lib3ds_dword_write(num,f); - for (k=track->keyL; k; k=k->next) - { - if (!lib3ds_tcb_write(&k->tcb,f)) - { - return(LIB3DS_FALSE); - } + for (k=track->keyL; k; k=k->next) { + if (!lib3ds_tcb_write(&k->tcb,f)) { + return(LIB3DS_FALSE); } - return(LIB3DS_TRUE); + } + return(LIB3DS_TRUE); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsLin1Key* lib3ds_lin1_key_new() { - Lib3dsLin1Key* k; - k=(Lib3dsLin1Key*)calloc(sizeof(Lib3dsLin1Key), 1); - return(k); + Lib3dsLin1Key* k; + k=(Lib3dsLin1Key*)calloc(sizeof(Lib3dsLin1Key), 1); + return(k); } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin1_key_free(Lib3dsLin1Key *key) { - ASSERT(key); - free(key); + ASSERT(key); + free(key); } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin1_track_free_keys(Lib3dsLin1Track *track) { - Lib3dsLin1Key *p,*q; + Lib3dsLin1Key *p,*q; - ASSERT(track); - for (p=track->keyL; p; p=q) - { - q=p->next; - lib3ds_lin1_key_free(p); - } + ASSERT(track); + for (p=track->keyL; p; p=q) { + q=p->next; + lib3ds_lin1_key_free(p); + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin1_key_setup(Lib3dsLin1Key *p, Lib3dsLin1Key *cp, Lib3dsLin1Key *c, -Lib3dsLin1Key *cn, Lib3dsLin1Key *n) + Lib3dsLin1Key *cn, Lib3dsLin1Key *n) { - Lib3dsFloat np,nn; - Lib3dsFloat ksm,ksp,kdm,kdp; + Lib3dsFloat np,nn; + Lib3dsFloat ksm,ksp,kdm,kdp; + + ASSERT(c); + if (!cp) { + cp=c; + } + if (!cn) { + cn=c; + } + if (!p && !n) { + c->ds=0; + c->dd=0; + return; + } - ASSERT(c); - if (!cp) - { - cp=c; - } - if (!cn) - { - cn=c; - } - if (!p && !n) - { - c->ds=0; - c->dd=0; - return; - } + if (n && p) { + lib3ds_tcb(&p->tcb, &cp->tcb, &c->tcb, &cn->tcb, &n->tcb, &ksm, &ksp, &kdm, &kdp); + np = c->value - p->value; + nn = n->value - c->value; - if (n && p) - { - lib3ds_tcb(&p->tcb, &cp->tcb, &c->tcb, &cn->tcb, &n->tcb, &ksm, &ksp, &kdm, &kdp); - np = c->value - p->value; - nn = n->value - c->value; - - c->ds=ksm*np + ksp*nn; - c->dd=kdm*np + kdp*nn; + c->ds=ksm*np + ksp*nn; + c->dd=kdm*np + kdp*nn; + } + else { + if (p) { + np = c->value - p->value; + c->ds = np; + c->dd = np; } - else - { - if (p) - { - np = c->value - p->value; - c->ds = np; - c->dd = np; - } - if (n) - { - nn = n->value - c->value; - c->ds = nn; - c->dd = nn; - } + if (n) { + nn = n->value - c->value; + c->ds = nn; + c->dd = nn; } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin1_track_setup(Lib3dsLin1Track *track) { - Lib3dsLin1Key *pp,*pc,*pn,*pl; + Lib3dsLin1Key *pp,*pc,*pn,*pl; - ASSERT(track); - pc=track->keyL; - if (!pc) - { - return; - } - if (!pc->next) - { - pc->ds=0; - pc->dd=0; - return; - } + ASSERT(track); + pc=track->keyL; + if (!pc) { + return; + } + if (!pc->next) { + pc->ds=0; + pc->dd=0; + return; + } - if (track->flags&LIB3DS_SMOOTH) - { - for (pl=track->keyL; pl->next->next; pl=pl->next); - lib3ds_lin1_key_setup(pl, pl->next, pc, 0, pc->next); - } - else - { - lib3ds_lin1_key_setup(0, 0, pc, 0, pc->next); - } - for (;;) - { - pp=pc; - pc=pc->next; - pn=pc->next; - if (!pn) - { - break; - } - lib3ds_lin1_key_setup(pp, 0, pc, 0, pn); + if (track->flags&LIB3DS_SMOOTH) { + for (pl=track->keyL; pl->next->next; pl=pl->next); + lib3ds_lin1_key_setup(pl, pl->next, pc, 0, pc->next); + } + else { + lib3ds_lin1_key_setup(0, 0, pc, 0, pc->next); + } + for (;;) { + pp=pc; + pc=pc->next; + pn=pc->next; + if (!pn) { + break; } + lib3ds_lin1_key_setup(pp, 0, pc, 0, pn); + } - if (track->flags&LIB3DS_SMOOTH) - { - lib3ds_lin1_key_setup(pp, 0, pc, track->keyL, track->keyL->next); - } - else - { - lib3ds_lin1_key_setup(pp, 0, pc, 0, 0); - } + if (track->flags&LIB3DS_SMOOTH) { + lib3ds_lin1_key_setup(pp, 0, pc, track->keyL, track->keyL->next); + } + else { + lib3ds_lin1_key_setup(pp, 0, pc, 0, 0); + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin1_track_insert(Lib3dsLin1Track *track, Lib3dsLin1Key *key) { - ASSERT(track); - ASSERT(key); - ASSERT(!key->next); + ASSERT(track); + ASSERT(key); + ASSERT(!key->next); - if (!track->keyL) - { - track->keyL=key; - key->next=0; + if (!track->keyL) { + track->keyL=key; + key->next=0; + } + else { + Lib3dsLin1Key *k,*p; + + for (p=0,k=track->keyL; k!=0; p=k, k=k->next) { + if (k->tcb.frame>key->tcb.frame) { + break; + } } - else - { - Lib3dsLin1Key *k,*p; - - for (p=0,k=track->keyL; k!=0; p=k, k=k->next) - { - if (k->tcb.frame>key->tcb.frame) - { - break; - } - } - if (!p) - { - key->next=track->keyL; - track->keyL=key; - } - else - { - key->next=k; - p->next=key; - } - - if (k && (key->tcb.frame==k->tcb.frame)) - { - key->next=k->next; - lib3ds_lin1_key_free(k); - } + if (!p) { + key->next=track->keyL; + track->keyL=key; } + else { + key->next=k; + p->next=key; + } + + if (k && (key->tcb.frame==k->tcb.frame)) { + key->next=k->next; + lib3ds_lin1_key_free(k); + } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin1_track_remove(Lib3dsLin1Track *track, Lib3dsIntd frame) { - Lib3dsLin1Key *k,*p; - - ASSERT(track); - if (!track->keyL) - { - return; - } - for (p=0,k=track->keyL; k!=0; p=k, k=k->next) - { - if (k->tcb.frame==frame) - { - if (!p) - { - track->keyL=track->keyL->next; - } - else - { - p->next=k->next; - } - lib3ds_lin1_key_free(k); - break; - } + Lib3dsLin1Key *k,*p; + + ASSERT(track); + if (!track->keyL) { + return; + } + for (p=0,k=track->keyL; k!=0; p=k, k=k->next) { + if (k->tcb.frame==frame) { + if (!p) { + track->keyL=track->keyL->next; + } + else { + p->next=k->next; + } + lib3ds_lin1_key_free(k); + break; } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin1_track_eval(Lib3dsLin1Track *track, Lib3dsFloat *p, Lib3dsFloat t) { - Lib3dsLin1Key *k; - Lib3dsFloat nt; - Lib3dsFloat u; + Lib3dsLin1Key *k; + Lib3dsFloat nt; + Lib3dsFloat u; - ASSERT(p); - if (!track->keyL) - { - *p=0; - return; - } - if (!track->keyL->next) - { - *p = track->keyL->value; - return; - } + ASSERT(p); + if (!track->keyL) { + *p=0; + return; + } + if (!track->keyL->next) { + *p = track->keyL->value; + return; + } - for (k=track->keyL; k->next!=0; k=k->next) - { - if ((t>=(Lib3dsFloat)k->tcb.frame) && (t<(Lib3dsFloat)k->next->tcb.frame)) - { - break; - } + for (k=track->keyL; k->next!=0; k=k->next) { + if ((t>=(Lib3dsFloat)k->tcb.frame) && (t<(Lib3dsFloat)k->next->tcb.frame)) { + break; } - if (!k->next) - { - if (track->flags&LIB3DS_REPEAT) - { - nt=(Lib3dsFloat)fmod(t, k->tcb.frame); - for (k=track->keyL; k->next!=0; k=k->next) - { - if ((nt>=(Lib3dsFloat)k->tcb.frame) && (nt<(Lib3dsFloat)k->next->tcb.frame)) - { - break; - } - } - ASSERT(k->next); - } - else - { - *p = k->value; - return; + } + if (!k->next) { + if (track->flags&LIB3DS_REPEAT) { + nt=(Lib3dsFloat)fmod(t, k->tcb.frame); + for (k=track->keyL; k->next!=0; k=k->next) { + if ((nt>=(Lib3dsFloat)k->tcb.frame) && (nt<(Lib3dsFloat)k->next->tcb.frame)) { + break; } + } + ASSERT(k->next); } - else - { - nt=t; + else { + *p = k->value; + return; } - u=nt - (Lib3dsFloat)k->tcb.frame; - u/=(Lib3dsFloat)(k->next->tcb.frame - k->tcb.frame); + } + else { + nt=t; + } + u=nt - (Lib3dsFloat)k->tcb.frame; + u/=(Lib3dsFloat)(k->next->tcb.frame - k->tcb.frame); - *p = lib3ds_float_cubic( - k->value, - k->dd, - k->next->ds, - k->next->value, - u - ); + *p = lib3ds_float_cubic( + k->value, + k->dd, + k->next->ds, + k->next->value, + u + ); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsBool lib3ds_lin1_track_read(Lib3dsLin1Track *track, FILE *f) { - int keys; - int i; - Lib3dsLin1Key *k; + int keys; + int i; + Lib3dsLin1Key *k; - track->flags=lib3ds_word_read(f); - lib3ds_dword_read(f); - lib3ds_dword_read(f); - keys=lib3ds_intd_read(f); + track->flags=lib3ds_word_read(f); + lib3ds_dword_read(f); + lib3ds_dword_read(f); + keys=lib3ds_intd_read(f); - for (i=0; itcb, f)) - { - return(LIB3DS_FALSE); - } - k->value=lib3ds_float_read(f); - lib3ds_lin1_track_insert(track, k); + for (i=0; itcb, f)) { + return(LIB3DS_FALSE); } - lib3ds_lin1_track_setup(track); - return(LIB3DS_TRUE); + k->value=lib3ds_float_read(f); + lib3ds_lin1_track_insert(track, k); + } + lib3ds_lin1_track_setup(track); + return(LIB3DS_TRUE); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsBool lib3ds_lin1_track_write(Lib3dsLin1Track *track, FILE *f) { - Lib3dsLin1Key *k; - Lib3dsDword num=0; - for (k=track->keyL; k; k=k->next) - { - ++num; - } - lib3ds_word_write((Lib3dsWord)track->flags,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(num,f); + Lib3dsLin1Key *k; + Lib3dsDword num=0; + for (k=track->keyL; k; k=k->next) { + ++num; + } + lib3ds_word_write((Lib3dsWord)track->flags,f); + lib3ds_dword_write(0,f); + lib3ds_dword_write(0,f); + lib3ds_dword_write(num,f); - for (k=track->keyL; k; k=k->next) - { - if (!lib3ds_tcb_write(&k->tcb,f)) - { - return(LIB3DS_FALSE); - } - lib3ds_float_write(k->value,f); + for (k=track->keyL; k; k=k->next) { + if (!lib3ds_tcb_write(&k->tcb,f)) { + return(LIB3DS_FALSE); } - return(LIB3DS_TRUE); + lib3ds_float_write(k->value,f); + } + return(LIB3DS_TRUE); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsLin3Key* lib3ds_lin3_key_new() { - Lib3dsLin3Key* k; - k=(Lib3dsLin3Key*)calloc(sizeof(Lib3dsLin3Key), 1); - return(k); + Lib3dsLin3Key* k; + k=(Lib3dsLin3Key*)calloc(sizeof(Lib3dsLin3Key), 1); + return(k); } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin3_key_free(Lib3dsLin3Key *key) { - ASSERT(key); - free(key); + ASSERT(key); + free(key); } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin3_track_free_keys(Lib3dsLin3Track *track) { - Lib3dsLin3Key *p,*q; + Lib3dsLin3Key *p,*q; - ASSERT(track); - for (p=track->keyL; p; p=q) - { - q=p->next; - lib3ds_lin3_key_free(p); - } + ASSERT(track); + for (p=track->keyL; p; p=q) { + q=p->next; + lib3ds_lin3_key_free(p); + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin3_key_setup(Lib3dsLin3Key *p, Lib3dsLin3Key *cp, Lib3dsLin3Key *c, -Lib3dsLin3Key *cn, Lib3dsLin3Key *n) + Lib3dsLin3Key *cn, Lib3dsLin3Key *n) { - Lib3dsVector np,nn; - Lib3dsFloat ksm,ksp,kdm,kdp; - int i; + Lib3dsVector np,nn; + Lib3dsFloat ksm,ksp,kdm,kdp; + int i; + + ASSERT(c); + if (!cp) { + cp=c; + } + if (!cn) { + cn=c; + } + if (!p && !n) { + lib3ds_vector_zero(c->ds); + lib3ds_vector_zero(c->dd); + return; + } - ASSERT(c); - if (!cp) - { - cp=c; - } - if (!cn) - { - cn=c; - } - if (!p && !n) - { - lib3ds_vector_zero(c->ds); - lib3ds_vector_zero(c->dd); - return; - } + if (n && p) { + lib3ds_tcb(&p->tcb, &cp->tcb, &c->tcb, &cn->tcb, &n->tcb, &ksm, &ksp, &kdm, &kdp); + lib3ds_vector_sub(np, c->value, p->value); + lib3ds_vector_sub(nn, n->value, c->value); - if (n && p) - { - lib3ds_tcb(&p->tcb, &cp->tcb, &c->tcb, &cn->tcb, &n->tcb, &ksm, &ksp, &kdm, &kdp); - lib3ds_vector_sub(np, c->value, p->value); - lib3ds_vector_sub(nn, n->value, c->value); - - for(i=0; i<3; ++i) - { - c->ds[i]=ksm*np[i] + ksp*nn[i]; - c->dd[i]=kdm*np[i] + kdp*nn[i]; - } + for(i=0; i<3; ++i) { + c->ds[i]=ksm*np[i] + ksp*nn[i]; + c->dd[i]=kdm*np[i] + kdp*nn[i]; } - else - { - if (p) - { - lib3ds_vector_sub(np, c->value, p->value); - lib3ds_vector_copy(c->ds, np); - lib3ds_vector_copy(c->dd, np); - } - if (n) - { - lib3ds_vector_sub(nn, n->value, c->value); - lib3ds_vector_copy(c->ds, nn); - lib3ds_vector_copy(c->dd, nn); - } + } + else { + if (p) { + lib3ds_vector_sub(np, c->value, p->value); + lib3ds_vector_copy(c->ds, np); + lib3ds_vector_copy(c->dd, np); } + if (n) { + lib3ds_vector_sub(nn, n->value, c->value); + lib3ds_vector_copy(c->ds, nn); + lib3ds_vector_copy(c->dd, nn); + } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin3_track_setup(Lib3dsLin3Track *track) { - Lib3dsLin3Key *pp,*pc,*pn,*pl; + Lib3dsLin3Key *pp,*pc,*pn,*pl; - ASSERT(track); - pc=track->keyL; - if (!pc) - { - return; - } - if (!pc->next) - { - lib3ds_vector_zero(pc->ds); - lib3ds_vector_zero(pc->dd); - return; - } + ASSERT(track); + pc=track->keyL; + if (!pc) { + return; + } + if (!pc->next) { + lib3ds_vector_zero(pc->ds); + lib3ds_vector_zero(pc->dd); + return; + } - if (track->flags&LIB3DS_SMOOTH) - { - for (pl=track->keyL; pl->next->next; pl=pl->next); - lib3ds_lin3_key_setup(pl, pl->next, pc, 0, pc->next); - } - else - { - lib3ds_lin3_key_setup(0, 0, pc, 0, pc->next); - } - for (;;) - { - pp=pc; - pc=pc->next; - pn=pc->next; - if (!pn) - { - break; - } - lib3ds_lin3_key_setup(pp, 0, pc, 0, pn); + if (track->flags&LIB3DS_SMOOTH) { + for (pl=track->keyL; pl->next->next; pl=pl->next); + lib3ds_lin3_key_setup(pl, pl->next, pc, 0, pc->next); + } + else { + lib3ds_lin3_key_setup(0, 0, pc, 0, pc->next); + } + for (;;) { + pp=pc; + pc=pc->next; + pn=pc->next; + if (!pn) { + break; } + lib3ds_lin3_key_setup(pp, 0, pc, 0, pn); + } - if (track->flags&LIB3DS_SMOOTH) - { - lib3ds_lin3_key_setup(pp, 0, pc, track->keyL, track->keyL->next); - } - else - { - lib3ds_lin3_key_setup(pp, 0, pc, 0, 0); - } + if (track->flags&LIB3DS_SMOOTH) { + lib3ds_lin3_key_setup(pp, 0, pc, track->keyL, track->keyL->next); + } + else { + lib3ds_lin3_key_setup(pp, 0, pc, 0, 0); + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin3_track_insert(Lib3dsLin3Track *track, Lib3dsLin3Key *key) { - ASSERT(track); - ASSERT(key); - ASSERT(!key->next); + ASSERT(track); + ASSERT(key); + ASSERT(!key->next); - if (!track->keyL) - { - track->keyL=key; - key->next=0; + if (!track->keyL) { + track->keyL=key; + key->next=0; + } + else { + Lib3dsLin3Key *k,*p; + + for (p=0,k=track->keyL; k!=0; p=k, k=k->next) { + if (k->tcb.frame>key->tcb.frame) { + break; + } } - else - { - Lib3dsLin3Key *k,*p; - - for (p=0,k=track->keyL; k!=0; p=k, k=k->next) - { - if (k->tcb.frame>key->tcb.frame) - { - break; - } - } - if (!p) - { - key->next=track->keyL; - track->keyL=key; - } - else - { - key->next=k; - p->next=key; - } - - if (k && (key->tcb.frame==k->tcb.frame)) - { - key->next=k->next; - lib3ds_lin3_key_free(k); - } + if (!p) { + key->next=track->keyL; + track->keyL=key; } + else { + key->next=k; + p->next=key; + } + + if (k && (key->tcb.frame==k->tcb.frame)) { + key->next=k->next; + lib3ds_lin3_key_free(k); + } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin3_track_remove(Lib3dsLin3Track *track, Lib3dsIntd frame) { - Lib3dsLin3Key *k,*p; - - ASSERT(track); - if (!track->keyL) - { - return; - } - for (p=0,k=track->keyL; k!=0; p=k, k=k->next) - { - if (k->tcb.frame==frame) - { - if (!p) - { - track->keyL=track->keyL->next; - } - else - { - p->next=k->next; - } - lib3ds_lin3_key_free(k); - break; - } + Lib3dsLin3Key *k,*p; + + ASSERT(track); + if (!track->keyL) { + return; + } + for (p=0,k=track->keyL; k!=0; p=k, k=k->next) { + if (k->tcb.frame==frame) { + if (!p) { + track->keyL=track->keyL->next; + } + else { + p->next=k->next; + } + lib3ds_lin3_key_free(k); + break; } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_lin3_track_eval(Lib3dsLin3Track *track, Lib3dsVector p, Lib3dsFloat t) { - Lib3dsLin3Key *k; - Lib3dsFloat nt; - Lib3dsFloat u; + Lib3dsLin3Key *k; + Lib3dsFloat nt; + Lib3dsFloat u; - if (!track->keyL) - { - lib3ds_vector_zero(p); - return; - } - if (!track->keyL->next) - { - lib3ds_vector_copy(p, track->keyL->value); - return; - } + if (!track->keyL) { + lib3ds_vector_zero(p); + return; + } + if (!track->keyL->next) { + lib3ds_vector_copy(p, track->keyL->value); + return; + } - for (k=track->keyL; k->next!=0; k=k->next) - { - if ((t>=(Lib3dsFloat)k->tcb.frame) && (t<(Lib3dsFloat)k->next->tcb.frame)) - { - break; - } + for (k=track->keyL; k->next!=0; k=k->next) { + if ((t>=(Lib3dsFloat)k->tcb.frame) && (t<(Lib3dsFloat)k->next->tcb.frame)) { + break; } - if (!k->next) - { - if (track->flags&LIB3DS_REPEAT) - { - nt=(Lib3dsFloat)fmod(t, k->tcb.frame); - for (k=track->keyL; k->next!=0; k=k->next) - { - if ((nt>=(Lib3dsFloat)k->tcb.frame) && (nt<(Lib3dsFloat)k->next->tcb.frame)) - { - break; - } - } - ASSERT(k->next); - } - else - { - lib3ds_vector_copy(p, k->value); - return; + } + if (!k->next) { + if (track->flags&LIB3DS_REPEAT) { + nt=(Lib3dsFloat)fmod(t, k->tcb.frame); + for (k=track->keyL; k->next!=0; k=k->next) { + if ((nt>=(Lib3dsFloat)k->tcb.frame) && (nt<(Lib3dsFloat)k->next->tcb.frame)) { + break; } + } + ASSERT(k->next); } - else - { - nt=t; + else { + lib3ds_vector_copy(p, k->value); + return; } - u=nt - (Lib3dsFloat)k->tcb.frame; - u/=(Lib3dsFloat)(k->next->tcb.frame - k->tcb.frame); - - lib3ds_vector_cubic( - p, - k->value, - k->dd, - k->next->ds, - k->next->value, - u - ); + } + else { + nt=t; + } + u=nt - (Lib3dsFloat)k->tcb.frame; + u/=(Lib3dsFloat)(k->next->tcb.frame - k->tcb.frame); + + lib3ds_vector_cubic( + p, + k->value, + k->dd, + k->next->ds, + k->next->value, + u + ); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsBool lib3ds_lin3_track_read(Lib3dsLin3Track *track, FILE *f) { - int keys; - int i,j; - Lib3dsLin3Key *k; + int keys; + int i,j; + Lib3dsLin3Key *k; - track->flags=lib3ds_word_read(f); - lib3ds_dword_read(f); - lib3ds_dword_read(f); - keys=lib3ds_intd_read(f); + track->flags=lib3ds_word_read(f); + lib3ds_dword_read(f); + lib3ds_dword_read(f); + keys=lib3ds_intd_read(f); - for (i=0; itcb, f)) - { - return(LIB3DS_FALSE); - } - for (j=0; j<3; ++j) - { - k->value[j]=lib3ds_float_read(f); - } - lib3ds_lin3_track_insert(track, k); + for (i=0; itcb, f)) { + return(LIB3DS_FALSE); } - lib3ds_lin3_track_setup(track); - return(LIB3DS_TRUE); + for (j=0; j<3; ++j) { + k->value[j]=lib3ds_float_read(f); + } + lib3ds_lin3_track_insert(track, k); + } + lib3ds_lin3_track_setup(track); + return(LIB3DS_TRUE); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsBool lib3ds_lin3_track_write(Lib3dsLin3Track *track, FILE *f) { - Lib3dsLin3Key *k; - Lib3dsDword num=0; - for (k=track->keyL; k; k=k->next) - { - ++num; - } - lib3ds_word_write((Lib3dsWord)track->flags,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(num,f); + Lib3dsLin3Key *k; + Lib3dsDword num=0; + for (k=track->keyL; k; k=k->next) { + ++num; + } + lib3ds_word_write((Lib3dsWord)track->flags,f); + lib3ds_dword_write(0,f); + lib3ds_dword_write(0,f); + lib3ds_dword_write(num,f); - for (k=track->keyL; k; k=k->next) - { - if (!lib3ds_tcb_write(&k->tcb,f)) - { - return(LIB3DS_FALSE); - } - lib3ds_vector_write(k->value,f); + for (k=track->keyL; k; k=k->next) { + if (!lib3ds_tcb_write(&k->tcb,f)) { + return(LIB3DS_FALSE); } - return(LIB3DS_TRUE); + lib3ds_vector_write(k->value,f); + } + return(LIB3DS_TRUE); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsQuatKey* lib3ds_quat_key_new() { - Lib3dsQuatKey* k; - k=(Lib3dsQuatKey*)calloc(sizeof(Lib3dsQuatKey), 1); - return(k); + Lib3dsQuatKey* k; + k=(Lib3dsQuatKey*)calloc(sizeof(Lib3dsQuatKey), 1); + return(k); } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_quat_key_free(Lib3dsQuatKey *key) { - ASSERT(key); - free(key); + ASSERT(key); + free(key); } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_quat_track_free_keys(Lib3dsQuatTrack *track) { - Lib3dsQuatKey *p,*q; + Lib3dsQuatKey *p,*q; - ASSERT(track); - for (p=track->keyL; p; p=q) - { - q=p->next; - lib3ds_quat_key_free(p); - } + ASSERT(track); + for (p=track->keyL; p; p=q) { + q=p->next; + lib3ds_quat_key_free(p); + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_quat_key_setup(Lib3dsQuatKey *p, Lib3dsQuatKey *cp, Lib3dsQuatKey *c, -Lib3dsQuatKey *cn, Lib3dsQuatKey *n) + Lib3dsQuatKey *cn, Lib3dsQuatKey *n) { - Lib3dsFloat ksm,ksp,kdm,kdp; - Lib3dsQuat q,qp,qn,qa,qb; - int i; + Lib3dsFloat ksm,ksp,kdm,kdp; + Lib3dsQuat q,qp,qn,qa,qb; + int i; + + ASSERT(c); + if (!cp) { + cp=c; + } + if (!cn) { + cn=c; + } + if (!p || !n) { + lib3ds_quat_copy(c->ds, c->q); + lib3ds_quat_copy(c->dd, c->q); + return; + } - ASSERT(c); - if (!cp) - { - cp=c; + if (p) { + if (p->angle>LIB3DS_TWOPI-LIB3DS_EPSILON) { + lib3ds_quat_axis_angle(qp, p->axis, 0.0f); + lib3ds_quat_ln(qp); } - if (!cn) - { - cn=c; + else { + lib3ds_quat_copy(q, p->q); + if (lib3ds_quat_dot(q,c->q)<0) lib3ds_quat_neg(q); + lib3ds_quat_ln_dif(qp, c->q, q); } - if (!p || !n) - { - lib3ds_quat_copy(c->ds, c->q); - lib3ds_quat_copy(c->dd, c->q); - return; + } + if (n) { + if (n->angle>LIB3DS_TWOPI-LIB3DS_EPSILON) { + lib3ds_quat_axis_angle(qn, n->axis, 0.0f); + lib3ds_quat_ln(qn); } + else { + lib3ds_quat_copy(q, n->q); + if (lib3ds_quat_dot(q,c->q)<0) lib3ds_quat_neg(q); + lib3ds_quat_ln_dif(qn, c->q, q); + } + } - if (p) - { - if (p->angle>LIB3DS_TWOPI-LIB3DS_EPSILON) - { - lib3ds_quat_axis_angle(qp, p->axis, 0.0f); - lib3ds_quat_ln(qp); - } - else - { - lib3ds_quat_copy(q, p->q); - if (lib3ds_quat_dot(q,c->q)<0) lib3ds_quat_neg(q); - lib3ds_quat_ln_dif(qp, c->q, q); - } + if (n && p) { + lib3ds_tcb(&p->tcb, &cp->tcb, &c->tcb, &cn->tcb, &n->tcb, &ksm, &ksp, &kdm, &kdp); + for(i=0; i<4; i++) { + qa[i]=-0.5f*(kdm*qn[i]+kdp*qp[i]); + qb[i]=-0.5f*(ksm*qn[i]+ksp*qp[i]); } - if (n) - { - if (n->angle>LIB3DS_TWOPI-LIB3DS_EPSILON) - { - lib3ds_quat_axis_angle(qn, n->axis, 0.0f); - lib3ds_quat_ln(qn); - } - else - { - lib3ds_quat_copy(q, n->q); - if (lib3ds_quat_dot(q,c->q)<0) lib3ds_quat_neg(q); - lib3ds_quat_ln_dif(qn, c->q, q); - } + lib3ds_quat_exp(qa); + lib3ds_quat_exp(qb); + + lib3ds_quat_mul(c->ds, c->q, qa); + lib3ds_quat_mul(c->dd, c->q, qb); + } + else { + if (p) { + lib3ds_quat_exp(qp); + lib3ds_quat_mul(c->ds, c->q, qp); + lib3ds_quat_mul(c->dd, c->q, qp); } - - if (n && p) - { - lib3ds_tcb(&p->tcb, &cp->tcb, &c->tcb, &cn->tcb, &n->tcb, &ksm, &ksp, &kdm, &kdp); - for(i=0; i<4; i++) - { - qa[i]=-0.5f*(kdm*qn[i]+kdp*qp[i]); - qb[i]=-0.5f*(ksm*qn[i]+ksp*qp[i]); - } - lib3ds_quat_exp(qa); - lib3ds_quat_exp(qb); - - lib3ds_quat_mul(c->ds, c->q, qa); - lib3ds_quat_mul(c->dd, c->q, qb); - } - else - { - if (p) - { - lib3ds_quat_exp(qp); - lib3ds_quat_mul(c->ds, c->q, qp); - lib3ds_quat_mul(c->dd, c->q, qp); - } - if (n) - { - lib3ds_quat_exp(qn); - lib3ds_quat_mul(c->ds, c->q, qn); - lib3ds_quat_mul(c->dd, c->q, qn); - } + if (n) { + lib3ds_quat_exp(qn); + lib3ds_quat_mul(c->ds, c->q, qn); + lib3ds_quat_mul(c->dd, c->q, qn); } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_quat_track_setup(Lib3dsQuatTrack *track) { - Lib3dsQuatKey *pp,*pc,*pn,*pl; - Lib3dsQuat q; + Lib3dsQuatKey *pp,*pc,*pn,*pl; + Lib3dsQuat q; - ASSERT(track); - for (pp=0,pc=track->keyL; pc; pp=pc,pc=pc->next) - { - lib3ds_quat_axis_angle(q, pc->axis, pc->angle); - if (pp) - { - lib3ds_quat_mul(pc->q, q, pp->q); - } - else - { - lib3ds_quat_copy(pc->q, q); - } + ASSERT(track); + for (pp=0,pc=track->keyL; pc; pp=pc,pc=pc->next) { + lib3ds_quat_axis_angle(q, pc->axis, pc->angle); + if (pp) { + lib3ds_quat_mul(pc->q, q, pp->q); } + else { + lib3ds_quat_copy(pc->q, q); + } + } - pc=track->keyL; - if (!pc) - { - return; - } - if (!pc->next) - { - lib3ds_quat_copy(pc->ds, pc->q); - lib3ds_quat_copy(pc->dd, pc->q); - return; - } + pc=track->keyL; + if (!pc) { + return; + } + if (!pc->next) { + lib3ds_quat_copy(pc->ds, pc->q); + lib3ds_quat_copy(pc->dd, pc->q); + return; + } - if (track->flags&LIB3DS_SMOOTH) - { - for (pl=track->keyL; pl->next->next; pl=pl->next); - lib3ds_quat_key_setup(pl, pl->next, pc, 0, pc->next); - } - else - { - lib3ds_quat_key_setup(0, 0, pc, 0, pc->next); - } - for (;;) - { - pp=pc; - pc=pc->next; - pn=pc->next; - if (!pn) - { - break; - } - lib3ds_quat_key_setup(pp, 0, pc, 0, pn); + if (track->flags&LIB3DS_SMOOTH) { + for (pl=track->keyL; pl->next->next; pl=pl->next); + lib3ds_quat_key_setup(pl, pl->next, pc, 0, pc->next); + } + else { + lib3ds_quat_key_setup(0, 0, pc, 0, pc->next); + } + for (;;) { + pp=pc; + pc=pc->next; + pn=pc->next; + if (!pn) { + break; } + lib3ds_quat_key_setup(pp, 0, pc, 0, pn); + } - if (track->flags&LIB3DS_SMOOTH) - { - lib3ds_quat_key_setup(pp, 0, pc, track->keyL, track->keyL->next); - } - else - { - lib3ds_quat_key_setup(pp, 0, pc, 0, 0); - } + if (track->flags&LIB3DS_SMOOTH) { + lib3ds_quat_key_setup(pp, 0, pc, track->keyL, track->keyL->next); + } + else { + lib3ds_quat_key_setup(pp, 0, pc, 0, 0); + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_quat_track_insert(Lib3dsQuatTrack *track, Lib3dsQuatKey *key) { - ASSERT(track); - ASSERT(key); - ASSERT(!key->next); + ASSERT(track); + ASSERT(key); + ASSERT(!key->next); - if (!track->keyL) - { - track->keyL=key; - key->next=0; + if (!track->keyL) { + track->keyL=key; + key->next=0; + } + else { + Lib3dsQuatKey *k,*p; + + for (p=0,k=track->keyL; k!=0; p=k, k=k->next) { + if (k->tcb.frame>key->tcb.frame) { + break; + } } - else - { - Lib3dsQuatKey *k,*p; - - for (p=0,k=track->keyL; k!=0; p=k, k=k->next) - { - if (k->tcb.frame>key->tcb.frame) - { - break; - } - } - if (!p) - { - key->next=track->keyL; - track->keyL=key; - } - else - { - key->next=k; - p->next=key; - } - - if (k && (key->tcb.frame==k->tcb.frame)) - { - key->next=k->next; - lib3ds_quat_key_free(k); - } + if (!p) { + key->next=track->keyL; + track->keyL=key; } + else { + key->next=k; + p->next=key; + } + + if (k && (key->tcb.frame==k->tcb.frame)) { + key->next=k->next; + lib3ds_quat_key_free(k); + } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_quat_track_remove(Lib3dsQuatTrack *track, Lib3dsIntd frame) { - Lib3dsQuatKey *k,*p; - - ASSERT(track); - if (!track->keyL) - { - return; - } - for (p=0,k=track->keyL; k!=0; p=k, k=k->next) - { - if (k->tcb.frame==frame) - { - if (!p) - { - track->keyL=track->keyL->next; - } - else - { - p->next=k->next; - } - lib3ds_quat_key_free(k); - break; - } + Lib3dsQuatKey *k,*p; + + ASSERT(track); + if (!track->keyL) { + return; + } + for (p=0,k=track->keyL; k!=0; p=k, k=k->next) { + if (k->tcb.frame==frame) { + if (!p) { + track->keyL=track->keyL->next; + } + else { + p->next=k->next; + } + lib3ds_quat_key_free(k); + break; } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_quat_track_eval(Lib3dsQuatTrack *track, Lib3dsQuat q, Lib3dsFloat t) { - Lib3dsQuatKey *k; - Lib3dsFloat nt; - Lib3dsFloat u; + Lib3dsQuatKey *k; + Lib3dsFloat nt; + Lib3dsFloat u; - if (!track->keyL) - { - lib3ds_quat_identity(q); - return; - } - if (!track->keyL->next) - { - lib3ds_quat_copy(q, track->keyL->q); - return; - } + if (!track->keyL) { + lib3ds_quat_identity(q); + return; + } + if (!track->keyL->next) { + lib3ds_quat_copy(q, track->keyL->q); + return; + } - for (k=track->keyL; k->next!=0; k=k->next) - { - if ((t>=k->tcb.frame) && (tnext->tcb.frame)) - { - break; - } + for (k=track->keyL; k->next!=0; k=k->next) { + if ((t>=k->tcb.frame) && (tnext->tcb.frame)) { + break; } - if (!k->next) - { - if (track->flags&LIB3DS_REPEAT) - { - nt=(Lib3dsFloat)fmod(t, k->tcb.frame); - for (k=track->keyL; k->next!=0; k=k->next) - { - if ((nt>=k->tcb.frame) && (ntnext->tcb.frame)) - { - break; - } - } - ASSERT(k->next); - } - else - { - lib3ds_quat_copy(q, k->q); - return; + } + if (!k->next) { + if (track->flags&LIB3DS_REPEAT) { + nt=(Lib3dsFloat)fmod(t, k->tcb.frame); + for (k=track->keyL; k->next!=0; k=k->next) { + if ((nt>=k->tcb.frame) && (ntnext->tcb.frame)) { + break; } + } + ASSERT(k->next); } - else - { - nt=t; + else { + lib3ds_quat_copy(q, k->q); + return; } - u=nt - k->tcb.frame; - u/=(k->next->tcb.frame - k->tcb.frame); + } + else { + nt=t; + } + u=nt - k->tcb.frame; + u/=(k->next->tcb.frame - k->tcb.frame); - lib3ds_quat_squad( - q, - k->q, - k->dd, - k->next->ds, - k->next->q, - u - ); + lib3ds_quat_squad( + q, + k->q, + k->dd, + k->next->ds, + k->next->q, + u + ); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsBool lib3ds_quat_track_read(Lib3dsQuatTrack *track, FILE *f) { - int keys; - int i,j; - // removed p definiation since MipsPro says that it ain't used.. - //Lib3dsQuatKey *p; - Lib3dsQuatKey *k; + int keys; + int i,j; + Lib3dsQuatKey *p,*k; - track->flags=lib3ds_word_read(f); - lib3ds_dword_read(f); - lib3ds_dword_read(f); - keys=lib3ds_intd_read(f); + track->flags=lib3ds_word_read(f); + lib3ds_dword_read(f); + lib3ds_dword_read(f); + keys=lib3ds_intd_read(f); - for (/*p=0,*/i=0; itcb, f)) - { - return(LIB3DS_FALSE); - } - k->angle=lib3ds_float_read(f); - for (j=0; j<3; ++j) - { - k->axis[j]=lib3ds_float_read(f); - } - lib3ds_quat_track_insert(track, k); + for (p=0,i=0; itcb, f)) { + return(LIB3DS_FALSE); } - lib3ds_quat_track_setup(track); - return(LIB3DS_TRUE); + k->angle=lib3ds_float_read(f); + for (j=0; j<3; ++j) { + k->axis[j]=lib3ds_float_read(f); + } + lib3ds_quat_track_insert(track, k); + } + lib3ds_quat_track_setup(track); + return(LIB3DS_TRUE); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsBool lib3ds_quat_track_write(Lib3dsQuatTrack *track, FILE *f) { - Lib3dsQuatKey *k; - Lib3dsDword num=0; - for (k=track->keyL; k; k=k->next) - { - ++num; - } - lib3ds_word_write((Lib3dsWord)track->flags,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(num,f); + Lib3dsQuatKey *k; + Lib3dsDword num=0; + for (k=track->keyL; k; k=k->next) { + ++num; + } + lib3ds_word_write((Lib3dsWord)track->flags,f); + lib3ds_dword_write(0,f); + lib3ds_dword_write(0,f); + lib3ds_dword_write(num,f); - for (k=track->keyL; k; k=k->next) - { - if (!lib3ds_tcb_write(&k->tcb,f)) - { - return(LIB3DS_FALSE); - } - lib3ds_float_write(k->angle,f); - lib3ds_vector_write(k->axis,f); + for (k=track->keyL; k; k=k->next) { + if (!lib3ds_tcb_write(&k->tcb,f)) { + return(LIB3DS_FALSE); } - return(LIB3DS_TRUE); + lib3ds_float_write(k->angle,f); + lib3ds_vector_write(k->axis,f); + } + return(LIB3DS_TRUE); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsMorphKey* lib3ds_morph_key_new() { - Lib3dsMorphKey* k; - k=(Lib3dsMorphKey*)calloc(sizeof(Lib3dsMorphKey), 1); - return(k); + Lib3dsMorphKey* k; + k=(Lib3dsMorphKey*)calloc(sizeof(Lib3dsMorphKey), 1); + return(k); } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_morph_key_free(Lib3dsMorphKey *key) { - ASSERT(key); - free(key); + ASSERT(key); + free(key); } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_morph_track_free_keys(Lib3dsMorphTrack *track) { - Lib3dsMorphKey *p,*q; + Lib3dsMorphKey *p,*q; - ASSERT(track); - for (p=track->keyL; p; p=q) - { - q=p->next; - lib3ds_morph_key_free(p); - } + ASSERT(track); + for (p=track->keyL; p; p=q) { + q=p->next; + lib3ds_morph_key_free(p); + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_morph_track_insert(Lib3dsMorphTrack *track, Lib3dsMorphKey *key) { - ASSERT(track); - ASSERT(key); - ASSERT(!key->next); + ASSERT(track); + ASSERT(key); + ASSERT(!key->next); - if (!track->keyL) - { - track->keyL=key; - key->next=0; + if (!track->keyL) { + track->keyL=key; + key->next=0; + } + else { + Lib3dsMorphKey *k,*p; + + for (p=0,k=track->keyL; k!=0; p=k, k=k->next) { + if (k->tcb.frame>key->tcb.frame) { + break; + } } - else - { - Lib3dsMorphKey *k,*p; - - for (p=0,k=track->keyL; k!=0; p=k, k=k->next) - { - if (k->tcb.frame>key->tcb.frame) - { - break; - } - } - if (!p) - { - key->next=track->keyL; - track->keyL=key; - } - else - { - key->next=k; - p->next=key; - } - - if (k && (key->tcb.frame==k->tcb.frame)) - { - key->next=k->next; - lib3ds_morph_key_free(k); - } + if (!p) { + key->next=track->keyL; + track->keyL=key; } + else { + key->next=k; + p->next=key; + } + + if (k && (key->tcb.frame==k->tcb.frame)) { + key->next=k->next; + lib3ds_morph_key_free(k); + } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_morph_track_remove(Lib3dsMorphTrack *track, Lib3dsIntd frame) { - Lib3dsMorphKey *k,*p; - - ASSERT(track); - if (!track->keyL) - { - return; - } - for (p=0,k=track->keyL; k!=0; p=k, k=k->next) - { - if (k->tcb.frame==frame) - { - if (!p) - { - track->keyL=track->keyL->next; - } - else - { - p->next=k->next; - } - lib3ds_morph_key_free(k); - break; - } + Lib3dsMorphKey *k,*p; + + ASSERT(track); + if (!track->keyL) { + return; + } + for (p=0,k=track->keyL; k!=0; p=k, k=k->next) { + if (k->tcb.frame==frame) { + if (!p) { + track->keyL=track->keyL->next; + } + else { + p->next=k->next; + } + lib3ds_morph_key_free(k); + break; } + } } /*! - * \ingroup tracks + * \ingroup tracks */ void lib3ds_morph_track_eval(Lib3dsMorphTrack *track, char *p, Lib3dsFloat t) { - Lib3dsMorphKey *k; - char* result; + Lib3dsMorphKey *k; + char* result; - ASSERT(p); - if (!track->keyL) - { - strcpy(p,""); - return; - } - if (!track->keyL->next) - { - strcpy(p,track->keyL->name); - return; - } + ASSERT(p); + if (!track->keyL) { + strcpy(p,""); + return; + } + if (!track->keyL->next) { + strcpy(p,track->keyL->name); + return; + } - result=0; - k=track->keyL; - while ((ttcb.frame) && (t>=k->next->tcb.frame)) - { - result=k->name; - if (!k->next) - { - if (track->flags&LIB3DS_REPEAT) - { - t-=k->tcb.frame; - k=track->keyL; - } - else - { - break; - } - } - else - { - k=k->next; - } + result=0; + k=track->keyL; + while ((ttcb.frame) && (t>=k->next->tcb.frame)) { + result=k->name; + if (!k->next) { + if (track->flags&LIB3DS_REPEAT) { + t-=k->tcb.frame; + k=track->keyL; + } + else { + break; + } } - if (result) - { - strcpy(p,result); - } - else - { - strcpy(p,""); + else { + k=k->next; } + } + if (result) { + strcpy(p,result); + } + else { + strcpy(p,""); + } } @@ -1555,22 +1364,22 @@ lib3ds_morph_track_eval(Lib3dsMorphTrack *track, char *p, Lib3dsFloat t) * \ingroup tracks */ Lib3dsBool -lib3ds_morph_track_read(Lib3dsMorphTrack *, FILE *) +lib3ds_morph_track_read(Lib3dsMorphTrack *track, FILE *f) { - // parameters lib3ds_morph_track_read(Lib3dsMorphTrack *track, FILE *f) - /* FIXME: */ - return(LIB3DS_TRUE); + /* FIXME: */ + return(LIB3DS_TRUE); } /*! - * \ingroup tracks + * \ingroup tracks */ Lib3dsBool -lib3ds_morph_track_write(Lib3dsMorphTrack *, FILE *) +lib3ds_morph_track_write(Lib3dsMorphTrack *track, FILE *f) { - // parameters : lib3ds_morph_track_write(Lib3dsMorphTrack *track, FILE *f) - /* FIXME: */ - ASSERT(0); - return(LIB3DS_FALSE); + /* FIXME: */ + ASSERT(0); + return(LIB3DS_FALSE); } + + diff --git a/src/osgPlugins/lib3ds/types.h b/src/osgPlugins/lib3ds/types.h index 60ef60fad..2d1b46d2f 100644 --- a/src/osgPlugins/lib3ds/types.h +++ b/src/osgPlugins/lib3ds/types.h @@ -26,15 +26,19 @@ extern "C" { #endif -#if defined(_WIN32) && (!defined(__GNUC__)) -#ifdef LIB3DS_EXPORT -#define LIB3DSAPI __declspec(dllexport) -#else -#define LIB3DSAPI __declspec(dllimport) -#endif -#else +/* + * #if defined (_LIB3DS_DLL) && defined(_WIN32) && (!defined(__GNUC__)) + * #ifdef LIB3DS_EXPORT + * #define LIB3DSAPI __declspec(dllexport) + * #else + * #define LIB3DSAPI __declspec(dllimport) + * #endif + * #else + * #define LIB3DSAPI + * #endif + */ +// within the OSG just use the libary inline, no need to export symbols. #define LIB3DSAPI -#endif #define LIB3DS_TRUE 1 #define LIB3DS_FALSE 0 diff --git a/src/osgPlugins/lib3ds/vector.cpp b/src/osgPlugins/lib3ds/vector.cpp index 168144667..a6fb56082 100644 --- a/src/osgPlugins/lib3ds/vector.cpp +++ b/src/osgPlugins/lib3ds/vector.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -23,6 +23,7 @@ #include #include + /*! * \defgroup vector Vector Mathematics * @@ -33,17 +34,17 @@ * \ingroup vector */ + /*! * \ingroup vector */ void lib3ds_vector_zero(Lib3dsVector c) { - int i; - for (i=0; i<3; ++i) - { - c[i]=0.0f; - } + int i; + for (i=0; i<3; ++i) { + c[i]=0.0f; + } } @@ -53,11 +54,10 @@ lib3ds_vector_zero(Lib3dsVector c) void lib3ds_vector_copy(Lib3dsVector dest, Lib3dsVector src) { - int i; - for (i=0; i<3; ++i) - { - dest[i]=src[i]; - } + int i; + for (i=0; i<3; ++i) { + dest[i]=src[i]; + } } @@ -67,11 +67,10 @@ lib3ds_vector_copy(Lib3dsVector dest, Lib3dsVector src) void lib3ds_vector_neg(Lib3dsVector c) { - int i; - for (i=0; i<3; ++i) - { - c[i]=-c[i]; - } + int i; + for (i=0; i<3; ++i) { + c[i]=-c[i]; + } } @@ -81,11 +80,10 @@ lib3ds_vector_neg(Lib3dsVector c) void lib3ds_vector_add(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b) { - int i; - for (i=0; i<3; ++i) - { - c[i]=a[i]+b[i]; - } + int i; + for (i=0; i<3; ++i) { + c[i]=a[i]+b[i]; + } } @@ -95,11 +93,10 @@ lib3ds_vector_add(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b) void lib3ds_vector_sub(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b) { - int i; - for (i=0; i<3; ++i) - { - c[i]=a[i]-b[i]; - } + int i; + for (i=0; i<3; ++i) { + c[i]=a[i]-b[i]; + } } @@ -109,11 +106,10 @@ lib3ds_vector_sub(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b) void lib3ds_vector_scalar(Lib3dsVector c, Lib3dsFloat k) { - int i; - for (i=0; i<3; ++i) - { - c[i]*=k; - } + int i; + for (i=0; i<3; ++i) { + c[i]*=k; + } } @@ -123,9 +119,9 @@ lib3ds_vector_scalar(Lib3dsVector c, Lib3dsFloat k) void lib3ds_vector_cross(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b) { - c[0]=a[1]*b[2] - a[2]*b[1]; - c[1]=a[2]*b[0] - a[0]*b[2]; - c[2]=a[0]*b[1] - a[1]*b[0]; + c[0]=a[1]*b[2] - a[2]*b[1]; + c[1]=a[2]*b[0] - a[0]*b[2]; + c[2]=a[0]*b[1] - a[1]*b[0]; } @@ -135,7 +131,7 @@ lib3ds_vector_cross(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b) Lib3dsFloat lib3ds_vector_dot(Lib3dsVector a, Lib3dsVector b) { - return(a[0]*b[0] + a[1]*b[1] + a[2]*b[2]); + return(a[0]*b[0] + a[1]*b[1] + a[2]*b[2]); } @@ -145,7 +141,7 @@ lib3ds_vector_dot(Lib3dsVector a, Lib3dsVector b) Lib3dsFloat lib3ds_vector_squared(Lib3dsVector c) { - return(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]); + return(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]); } @@ -155,7 +151,7 @@ lib3ds_vector_squared(Lib3dsVector c) Lib3dsFloat lib3ds_vector_length(Lib3dsVector c) { - return((Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2])); + return((Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2])); } @@ -165,33 +161,28 @@ lib3ds_vector_length(Lib3dsVector c) void lib3ds_vector_normalize(Lib3dsVector c) { - Lib3dsFloat l,m; + Lib3dsFloat l,m; - l=(Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]); - if (fabs(l)=c[1]) && (c[0]>=c[2])) - { - c[0]=1.0f; - } - else - if (c[1]>=c[2]) - { - c[1]=1.0f; - } - else - { - c[2]=1.0f; - } + l=(Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]); + if (fabs(l)=c[1]) && (c[0]>=c[2])) { + c[0]=1.0f; } else - { - m=1.0f/l; - c[0]*=m; - c[1]*=m; - c[2]*=m; + if (c[1]>=c[2]) { + c[1]=1.0f; } + else { + c[2]=1.0f; + } + } + else { + m=1.0f/l; + c[0]*=m; + c[1]*=m; + c[2]*=m; + } } @@ -201,12 +192,12 @@ lib3ds_vector_normalize(Lib3dsVector c) void lib3ds_vector_normal(Lib3dsVector n, Lib3dsVector a, Lib3dsVector b, Lib3dsVector c) { - Lib3dsVector p,q; + Lib3dsVector p,q; - lib3ds_vector_sub(p,c,b); - lib3ds_vector_sub(q,a,b); - lib3ds_vector_cross(n,p,q); - lib3ds_vector_normalize(n); + lib3ds_vector_sub(p,c,b); + lib3ds_vector_sub(q,a,b); + lib3ds_vector_cross(n,p,q); + lib3ds_vector_normalize(n); } @@ -216,9 +207,9 @@ lib3ds_vector_normal(Lib3dsVector n, Lib3dsVector a, Lib3dsVector b, Lib3dsVecto void lib3ds_vector_transform(Lib3dsVector c, Lib3dsMatrix m, Lib3dsVector a) { - c[0]= m[0][0]*a[0] + m[1][0]*a[1] + m[2][0]*a[2] + m[3][0]; - c[1]= m[0][1]*a[0] + m[1][1]*a[1] + m[2][1]*a[2] + m[3][1]; - c[2]= m[0][2]*a[0] + m[1][2]*a[1] + m[2][2]*a[2] + m[3][2]; + c[0]= m[0][0]*a[0] + m[1][0]*a[1] + m[2][0]*a[2] + m[3][0]; + c[1]= m[0][1]*a[0] + m[1][1]*a[1] + m[2][1]*a[2] + m[3][1]; + c[2]= m[0][2]*a[0] + m[1][2]*a[1] + m[2][2]*a[2] + m[3][2]; } @@ -227,17 +218,49 @@ lib3ds_vector_transform(Lib3dsVector c, Lib3dsMatrix m, Lib3dsVector a) */ void lib3ds_vector_cubic(Lib3dsVector c, Lib3dsVector a, Lib3dsVector p, Lib3dsVector q, -Lib3dsVector b, Lib3dsFloat t) + Lib3dsVector b, Lib3dsFloat t) { - Lib3dsDouble x,y,z,w; + Lib3dsDouble x,y,z,w; - x=2*t*t*t - 3*t*t + 1; - y=-2*t*t*t + 3*t*t; - z=t*t*t - 2*t*t + t; - w=t*t*t - t*t; - c[0]=(Lib3dsFloat)(x*a[0] + y*b[0] + z*p[0] + w*q[0]); - c[1]=(Lib3dsFloat)(x*a[1] + y*b[1] + z*p[1] + w*q[1]); - c[2]=(Lib3dsFloat)(x*a[2] + y*b[2] + z*p[2] + w*q[2]); + x=2*t*t*t - 3*t*t + 1; + y=-2*t*t*t + 3*t*t; + z=t*t*t - 2*t*t + t; + w=t*t*t - t*t; + c[0]=(Lib3dsFloat)(x*a[0] + y*b[0] + z*p[0] + w*q[0]); + c[1]=(Lib3dsFloat)(x*a[1] + y*b[1] + z*p[1] + w*q[1]); + c[2]=(Lib3dsFloat)(x*a[2] + y*b[2] + z*p[2] + w*q[2]); +} + + +/*! + * c[i] = min(c[i], a[i]); + * \ingroup vector + */ +void +lib3ds_vector_min(Lib3dsVector c, Lib3dsVector a) +{ + int i; + for (i=0; i<3; ++i) { + if (a[i]c[i]) { + c[i] = a[i]; + } + } } @@ -247,5 +270,6 @@ Lib3dsVector b, Lib3dsFloat t) void lib3ds_vector_dump(Lib3dsVector c) { - fprintf(stderr, "%f %f %f\n", c[0], c[1], c[2]); + fprintf(stderr, "%f %f %f\n", c[0], c[1], c[2]); } + diff --git a/src/osgPlugins/lib3ds/vector.h b/src/osgPlugins/lib3ds/vector.h index 606d84223..7588e75d5 100644 --- a/src/osgPlugins/lib3ds/vector.h +++ b/src/osgPlugins/lib3ds/vector.h @@ -47,6 +47,8 @@ extern LIB3DSAPI void lib3ds_vector_normal(Lib3dsVector n, Lib3dsVector a, extern LIB3DSAPI void lib3ds_vector_transform(Lib3dsVector c, Lib3dsMatrix m, Lib3dsVector a); extern LIB3DSAPI void lib3ds_vector_cubic(Lib3dsVector c, Lib3dsVector a, Lib3dsVector p, Lib3dsVector q, Lib3dsVector b, Lib3dsFloat t); +extern LIB3DSAPI void lib3ds_vector_min(Lib3dsVector c, Lib3dsVector a); +extern LIB3DSAPI void lib3ds_vector_max(Lib3dsVector c, Lib3dsVector a); extern LIB3DSAPI void lib3ds_vector_dump(Lib3dsVector c); #ifdef __cplusplus diff --git a/src/osgPlugins/lib3ds/viewport.cpp b/src/osgPlugins/lib3ds/viewport.cpp index 046d48633..d68ffc03b 100644 --- a/src/osgPlugins/lib3ds/viewport.cpp +++ b/src/osgPlugins/lib3ds/viewport.cpp @@ -4,13 +4,13 @@ * All rights reserved. * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License @@ -26,351 +26,335 @@ #include #include + /*! * \defgroup viewport Viewport and default view settings * * \author J.E. Hoffmann */ + /*! - * \ingroup viewport + * \ingroup viewport */ Lib3dsBool lib3ds_viewport_read(Lib3dsViewport *viewport, FILE *f) { - Lib3dsChunk c; - Lib3dsWord chunk; + Lib3dsChunk c; + Lib3dsWord chunk; - if (!lib3ds_chunk_read(&c, f)) - { - return(LIB3DS_FALSE); - } + if (!lib3ds_chunk_read_start(&c, 0, f)) { + return(LIB3DS_FALSE); + } + + switch (c.chunk) { + case LIB3DS_VIEWPORT_LAYOUT: + { + int cur=0; + viewport->layout.style=lib3ds_word_read(f); + viewport->layout.active=lib3ds_intw_read(f); + lib3ds_intw_read(f); + viewport->layout.swap=lib3ds_intw_read(f); + lib3ds_intw_read(f); + viewport->layout.swap_prior=lib3ds_intw_read(f); + viewport->layout.swap_view=lib3ds_intw_read(f); + lib3ds_chunk_read_tell(&c, f); + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_VIEWPORT_SIZE: + { + viewport->layout.position[0]=lib3ds_word_read(f); + viewport->layout.position[1]=lib3ds_word_read(f); + viewport->layout.size[0]=lib3ds_word_read(f); + viewport->layout.size[1]=lib3ds_word_read(f); + } + break; + case LIB3DS_VIEWPORT_DATA_3: + { + lib3ds_viewport_set_views(viewport,cur+1); + lib3ds_intw_read(f); + viewport->layout.viewL[cur].axis_lock=lib3ds_word_read(f); + viewport->layout.viewL[cur].position[0]=lib3ds_intw_read(f); + viewport->layout.viewL[cur].position[1]=lib3ds_intw_read(f); + viewport->layout.viewL[cur].size[0]=lib3ds_intw_read(f); + viewport->layout.viewL[cur].size[1]=lib3ds_intw_read(f); + viewport->layout.viewL[cur].type=lib3ds_word_read(f); + viewport->layout.viewL[cur].zoom=lib3ds_float_read(f); + lib3ds_vector_read(viewport->layout.viewL[cur].center,f); + viewport->layout.viewL[cur].horiz_angle=lib3ds_float_read(f); + viewport->layout.viewL[cur].vert_angle=lib3ds_float_read(f); + fread(viewport->layout.viewL[cur].camera,11,1,f); + ++cur; + } + break; + case LIB3DS_VIEWPORT_DATA: + /* 3DS R2 & R3 chunk + unsupported */ + break; + default: + lib3ds_chunk_unknown(chunk); + } + } + } + break; + case LIB3DS_DEFAULT_VIEW: + { + memset(&viewport->default_view,0,sizeof(Lib3dsDefaultView)); + while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + switch (chunk) { + case LIB3DS_VIEW_TOP: + { + viewport->default_view.type=LIB3DS_VIEW_TYPE_TOP; + lib3ds_vector_read(viewport->default_view.position,f); + viewport->default_view.width=lib3ds_float_read(f); + } + break; + case LIB3DS_VIEW_BOTTOM: + { + viewport->default_view.type=LIB3DS_VIEW_TYPE_BOTTOM; + lib3ds_vector_read(viewport->default_view.position,f); + viewport->default_view.width=lib3ds_float_read(f); + } + break; + case LIB3DS_VIEW_LEFT: + { + viewport->default_view.type=LIB3DS_VIEW_TYPE_LEFT; + lib3ds_vector_read(viewport->default_view.position,f); + viewport->default_view.width=lib3ds_float_read(f); + } + break; + case LIB3DS_VIEW_RIGHT: + { + viewport->default_view.type=LIB3DS_VIEW_TYPE_RIGHT; + lib3ds_vector_read(viewport->default_view.position,f); + viewport->default_view.width=lib3ds_float_read(f); + } + break; + case LIB3DS_VIEW_FRONT: + { + viewport->default_view.type=LIB3DS_VIEW_TYPE_FRONT; + lib3ds_vector_read(viewport->default_view.position,f); + viewport->default_view.width=lib3ds_float_read(f); + } + break; + case LIB3DS_VIEW_BACK: + { + viewport->default_view.type=LIB3DS_VIEW_TYPE_BACK; + lib3ds_vector_read(viewport->default_view.position,f); + viewport->default_view.width=lib3ds_float_read(f); + } + break; + case LIB3DS_VIEW_USER: + { + viewport->default_view.type=LIB3DS_VIEW_TYPE_USER; + lib3ds_vector_read(viewport->default_view.position,f); + viewport->default_view.width=lib3ds_float_read(f); + viewport->default_view.horiz_angle=lib3ds_float_read(f); + viewport->default_view.vert_angle=lib3ds_float_read(f); + viewport->default_view.roll_angle=lib3ds_float_read(f); + } + break; + case LIB3DS_VIEW_CAMERA: + { + viewport->default_view.type=LIB3DS_VIEW_TYPE_CAMERA; + fread(viewport->default_view.camera,11,1,f); + } + break; + default: + lib3ds_chunk_unknown(chunk); + } + } + } + break; + } - switch (c.chunk) - { - case LIB3DS_VIEWPORT_LAYOUT: - { - int cur=0; - viewport->layout.style=lib3ds_word_read(f); - viewport->layout.active=lib3ds_intw_read(f); - lib3ds_intw_read(f); - viewport->layout.swap=lib3ds_intw_read(f); - lib3ds_intw_read(f); - viewport->layout.swap_prior=lib3ds_intw_read(f); - viewport->layout.swap_view=lib3ds_intw_read(f); - lib3ds_chunk_read_tell(&c, f); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) - { - case LIB3DS_VIEWPORT_SIZE: - { - viewport->layout.position[0]=lib3ds_word_read(f); - viewport->layout.position[1]=lib3ds_word_read(f); - viewport->layout.size[0]=lib3ds_word_read(f); - viewport->layout.size[1]=lib3ds_word_read(f); - } - break; - case LIB3DS_VIEWPORT_DATA_3: - { - lib3ds_viewport_set_views(viewport,cur+1); - lib3ds_intw_read(f); - viewport->layout.viewL[cur].axis_lock=lib3ds_word_read(f); - viewport->layout.viewL[cur].position[0]=lib3ds_intw_read(f); - viewport->layout.viewL[cur].position[1]=lib3ds_intw_read(f); - viewport->layout.viewL[cur].size[0]=lib3ds_intw_read(f); - viewport->layout.viewL[cur].size[1]=lib3ds_intw_read(f); - viewport->layout.viewL[cur].type=lib3ds_word_read(f); - viewport->layout.viewL[cur].zoom=lib3ds_float_read(f); - lib3ds_vector_read(viewport->layout.viewL[cur].center,f); - viewport->layout.viewL[cur].horiz_angle=lib3ds_float_read(f); - viewport->layout.viewL[cur].vert_angle=lib3ds_float_read(f); - fread(viewport->layout.viewL[cur].camera,11,1,f); - ++cur; - } - break; - case LIB3DS_VIEWPORT_DATA: - /* 3DS R2 & R3 chunk - unsupported */ - break; - default: - lib3ds_chunk_unknown(chunk); - } - } - lib3ds_chunk_read_end(&c, f); - } - break; - case LIB3DS_DEFAULT_VIEW: - { - memset(&viewport->default_view,0,sizeof(Lib3dsDefaultView)); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) - { - switch (chunk) - { - case LIB3DS_VIEW_TOP: - { - viewport->default_view.type=LIB3DS_VIEW_TYPE_TOP; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); - } - break; - case LIB3DS_VIEW_BOTTOM: - { - viewport->default_view.type=LIB3DS_VIEW_TYPE_BOTTOM; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); - } - break; - case LIB3DS_VIEW_LEFT: - { - viewport->default_view.type=LIB3DS_VIEW_TYPE_LEFT; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); - } - break; - case LIB3DS_VIEW_RIGHT: - { - viewport->default_view.type=LIB3DS_VIEW_TYPE_RIGHT; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); - } - break; - case LIB3DS_VIEW_FRONT: - { - viewport->default_view.type=LIB3DS_VIEW_TYPE_FRONT; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); - } - break; - case LIB3DS_VIEW_BACK: - { - viewport->default_view.type=LIB3DS_VIEW_TYPE_BACK; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); - } - break; - case LIB3DS_VIEW_USER: - { - viewport->default_view.type=LIB3DS_VIEW_TYPE_USER; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); - viewport->default_view.horiz_angle=lib3ds_float_read(f); - viewport->default_view.vert_angle=lib3ds_float_read(f); - viewport->default_view.roll_angle=lib3ds_float_read(f); - } - break; - case LIB3DS_VIEW_CAMERA: - { - viewport->default_view.type=LIB3DS_VIEW_TYPE_CAMERA; - fread(viewport->default_view.camera,11,1,f); - } - break; - default: - lib3ds_chunk_unknown(chunk); - } - } - lib3ds_chunk_read_end(&c, f); - } - break; - } - return(LIB3DS_TRUE); + lib3ds_chunk_read_end(&c, f); + return(LIB3DS_TRUE); } /*! - * \ingroup viewport + * \ingroup viewport */ void lib3ds_viewport_set_views(Lib3dsViewport *viewport, Lib3dsDword views) { - ASSERT(viewport); - if (viewport->layout.views) - { - if (views) - { - viewport->layout.views=views; - viewport->layout.viewL=(Lib3dsView*)realloc(viewport->layout.viewL, sizeof(Lib3dsView)*views); - } - else - { - free(viewport->layout.viewL); - viewport->layout.views=0; - viewport->layout.viewL=0; - } + ASSERT(viewport); + if (viewport->layout.views) { + if (views) { + viewport->layout.views=views; + viewport->layout.viewL=(Lib3dsView*)realloc(viewport->layout.viewL, sizeof(Lib3dsView)*views); } - else - { - viewport->layout.views=views; - viewport->layout.viewL=(Lib3dsView*)calloc(sizeof(Lib3dsView),views); + else { + free(viewport->layout.viewL); + viewport->layout.views=0; + viewport->layout.viewL=0; } + } + else { + viewport->layout.views=views; + viewport->layout.viewL=(Lib3dsView*)calloc(sizeof(Lib3dsView),views); + } } /*! - * \ingroup viewport + * \ingroup viewport */ Lib3dsBool lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) { - if (viewport->layout.views) - { - Lib3dsChunk c; - unsigned i; + if (viewport->layout.views) { + Lib3dsChunk c; + unsigned i; - c.chunk=LIB3DS_VIEWPORT_LAYOUT; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - - lib3ds_word_write(viewport->layout.style,f); - lib3ds_intw_write(viewport->layout.active,f); - lib3ds_intw_write(0,f); - lib3ds_intw_write(viewport->layout.swap,f); - lib3ds_intw_write(0,f); - lib3ds_intw_write(viewport->layout.swap_prior,f); - lib3ds_intw_write(viewport->layout.swap_view,f); - - { - Lib3dsChunk c; - c.chunk=LIB3DS_VIEWPORT_SIZE; - c.size=14; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(viewport->layout.position[0],f); - lib3ds_intw_write(viewport->layout.position[1],f); - lib3ds_intw_write(viewport->layout.size[0],f); - lib3ds_intw_write(viewport->layout.size[1],f); - } - - for (i=0; ilayout.views; ++i) - { - Lib3dsChunk c; - c.chunk=LIB3DS_VIEWPORT_DATA_3; - c.size=55; - lib3ds_chunk_write(&c,f); - - lib3ds_intw_write(0,f); - lib3ds_word_write(viewport->layout.viewL[i].axis_lock,f); - lib3ds_intw_write(viewport->layout.viewL[i].position[0],f); - lib3ds_intw_write(viewport->layout.viewL[i].position[1],f); - lib3ds_intw_write(viewport->layout.viewL[i].size[0],f); - lib3ds_intw_write(viewport->layout.viewL[i].size[1],f); - lib3ds_word_write(viewport->layout.viewL[i].type,f); - lib3ds_float_write(viewport->layout.viewL[i].zoom,f); - lib3ds_vector_write(viewport->layout.viewL[i].center,f); - lib3ds_float_write(viewport->layout.viewL[i].horiz_angle,f); - lib3ds_float_write(viewport->layout.viewL[i].vert_angle,f); - fwrite(viewport->layout.viewL[i].camera,11,1,f); - } - - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + c.chunk=LIB3DS_VIEWPORT_LAYOUT; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); } - if (viewport->default_view.type) + lib3ds_word_write(viewport->layout.style,f); + lib3ds_intw_write(viewport->layout.active,f); + lib3ds_intw_write(0,f); + lib3ds_intw_write(viewport->layout.swap,f); + lib3ds_intw_write(0,f); + lib3ds_intw_write(viewport->layout.swap_prior,f); + lib3ds_intw_write(viewport->layout.swap_view,f); + { - Lib3dsChunk c; - - c.chunk=LIB3DS_DEFAULT_VIEW; - if (!lib3ds_chunk_write_start(&c,f)) - { - return(LIB3DS_FALSE); - } - - switch (viewport->default_view.type) - { - case LIB3DS_VIEW_TYPE_TOP: - { - Lib3dsChunk c; - c.chunk=LIB3DS_VIEW_TOP; - c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); - } - break; - case LIB3DS_VIEW_TYPE_BOTTOM: - { - Lib3dsChunk c; - c.chunk=LIB3DS_VIEW_BOTTOM; - c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); - } - break; - case LIB3DS_VIEW_TYPE_LEFT: - { - Lib3dsChunk c; - c.chunk=LIB3DS_VIEW_LEFT; - c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); - } - break; - case LIB3DS_VIEW_TYPE_RIGHT: - { - Lib3dsChunk c; - c.chunk=LIB3DS_VIEW_RIGHT; - c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); - } - break; - case LIB3DS_VIEW_TYPE_FRONT: - { - Lib3dsChunk c; - c.chunk=LIB3DS_VIEW_FRONT; - c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); - } - break; - case LIB3DS_VIEW_TYPE_BACK: - { - Lib3dsChunk c; - c.chunk=LIB3DS_VIEW_BACK; - c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); - } - break; - case LIB3DS_VIEW_TYPE_USER: - { - Lib3dsChunk c; - c.chunk=LIB3DS_VIEW_USER; - c.size=34; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); - lib3ds_float_write(viewport->default_view.horiz_angle,f); - lib3ds_float_write(viewport->default_view.vert_angle,f); - lib3ds_float_write(viewport->default_view.roll_angle,f); - } - break; - case LIB3DS_VIEW_TYPE_CAMERA: - { - Lib3dsChunk c; - c.chunk=LIB3DS_VIEW_CAMERA; - c.size=17; - lib3ds_chunk_write(&c,f); - fwrite(viewport->default_view.camera,1,11,f); - } - break; - } - - if (!lib3ds_chunk_write_end(&c,f)) - { - return(LIB3DS_FALSE); - } + Lib3dsChunk c; + c.chunk=LIB3DS_VIEWPORT_SIZE; + c.size=14; + lib3ds_chunk_write(&c,f); + lib3ds_intw_write(viewport->layout.position[0],f); + lib3ds_intw_write(viewport->layout.position[1],f); + lib3ds_intw_write(viewport->layout.size[0],f); + lib3ds_intw_write(viewport->layout.size[1],f); } - return(LIB3DS_TRUE); + + for (i=0; ilayout.views; ++i) { + Lib3dsChunk c; + c.chunk=LIB3DS_VIEWPORT_DATA_3; + c.size=55; + lib3ds_chunk_write(&c,f); + + lib3ds_intw_write(0,f); + lib3ds_word_write(viewport->layout.viewL[i].axis_lock,f); + lib3ds_intw_write(viewport->layout.viewL[i].position[0],f); + lib3ds_intw_write(viewport->layout.viewL[i].position[1],f); + lib3ds_intw_write(viewport->layout.viewL[i].size[0],f); + lib3ds_intw_write(viewport->layout.viewL[i].size[1],f); + lib3ds_word_write(viewport->layout.viewL[i].type,f); + lib3ds_float_write(viewport->layout.viewL[i].zoom,f); + lib3ds_vector_write(viewport->layout.viewL[i].center,f); + lib3ds_float_write(viewport->layout.viewL[i].horiz_angle,f); + lib3ds_float_write(viewport->layout.viewL[i].vert_angle,f); + fwrite(viewport->layout.viewL[i].camera,11,1,f); + } + + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + + if (viewport->default_view.type) { + Lib3dsChunk c; + + c.chunk=LIB3DS_DEFAULT_VIEW; + if (!lib3ds_chunk_write_start(&c,f)) { + return(LIB3DS_FALSE); + } + + switch (viewport->default_view.type) { + case LIB3DS_VIEW_TYPE_TOP: + { + Lib3dsChunk c; + c.chunk=LIB3DS_VIEW_TOP; + c.size=22; + lib3ds_chunk_write(&c,f); + lib3ds_vector_write(viewport->default_view.position,f); + lib3ds_float_write(viewport->default_view.width,f); + } + break; + case LIB3DS_VIEW_TYPE_BOTTOM: + { + Lib3dsChunk c; + c.chunk=LIB3DS_VIEW_BOTTOM; + c.size=22; + lib3ds_chunk_write(&c,f); + lib3ds_vector_write(viewport->default_view.position,f); + lib3ds_float_write(viewport->default_view.width,f); + } + break; + case LIB3DS_VIEW_TYPE_LEFT: + { + Lib3dsChunk c; + c.chunk=LIB3DS_VIEW_LEFT; + c.size=22; + lib3ds_chunk_write(&c,f); + lib3ds_vector_write(viewport->default_view.position,f); + lib3ds_float_write(viewport->default_view.width,f); + } + break; + case LIB3DS_VIEW_TYPE_RIGHT: + { + Lib3dsChunk c; + c.chunk=LIB3DS_VIEW_RIGHT; + c.size=22; + lib3ds_chunk_write(&c,f); + lib3ds_vector_write(viewport->default_view.position,f); + lib3ds_float_write(viewport->default_view.width,f); + } + break; + case LIB3DS_VIEW_TYPE_FRONT: + { + Lib3dsChunk c; + c.chunk=LIB3DS_VIEW_FRONT; + c.size=22; + lib3ds_chunk_write(&c,f); + lib3ds_vector_write(viewport->default_view.position,f); + lib3ds_float_write(viewport->default_view.width,f); + } + break; + case LIB3DS_VIEW_TYPE_BACK: + { + Lib3dsChunk c; + c.chunk=LIB3DS_VIEW_BACK; + c.size=22; + lib3ds_chunk_write(&c,f); + lib3ds_vector_write(viewport->default_view.position,f); + lib3ds_float_write(viewport->default_view.width,f); + } + break; + case LIB3DS_VIEW_TYPE_USER: + { + Lib3dsChunk c; + c.chunk=LIB3DS_VIEW_USER; + c.size=34; + lib3ds_chunk_write(&c,f); + lib3ds_vector_write(viewport->default_view.position,f); + lib3ds_float_write(viewport->default_view.width,f); + lib3ds_float_write(viewport->default_view.horiz_angle,f); + lib3ds_float_write(viewport->default_view.vert_angle,f); + lib3ds_float_write(viewport->default_view.roll_angle,f); + } + break; + case LIB3DS_VIEW_TYPE_CAMERA: + { + Lib3dsChunk c; + c.chunk=LIB3DS_VIEW_CAMERA; + c.size=17; + lib3ds_chunk_write(&c,f); + fwrite(viewport->default_view.camera,1,11,f); + } + break; + } + + if (!lib3ds_chunk_write_end(&c,f)) { + return(LIB3DS_FALSE); + } + } + return(LIB3DS_TRUE); }