diff options
Diffstat (limited to 'source/blender/modifiers/intern')
-rw-r--r-- | source/blender/modifiers/intern/MOD_meshcache.c | 314 | ||||
-rw-r--r-- | source/blender/modifiers/intern/MOD_meshcache_mdd.c | 301 | ||||
-rw-r--r-- | source/blender/modifiers/intern/MOD_meshcache_pc2.c | 277 | ||||
-rw-r--r-- | source/blender/modifiers/intern/MOD_meshcache_util.h | 63 | ||||
-rw-r--r-- | source/blender/modifiers/intern/MOD_util.c | 1 |
5 files changed, 956 insertions, 0 deletions
diff --git a/source/blender/modifiers/intern/MOD_meshcache.c b/source/blender/modifiers/intern/MOD_meshcache.c new file mode 100644 index 00000000000..c722eda28ec --- /dev/null +++ b/source/blender/modifiers/intern/MOD_meshcache.c @@ -0,0 +1,314 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Campbell Barton + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/modifiers/intern/MOD_meshcache.c + * \ingroup modifiers + */ + +#include <stdio.h> + +#include "DNA_scene_types.h" +#include "DNA_object_types.h" + +#include "BLI_utildefines.h" +#include "BLI_string.h" +#include "BLI_path_util.h" +#include "BLI_math.h" + + +#include "BKE_DerivedMesh.h" +#include "BKE_scene.h" +#include "BKE_global.h" +#include "BKE_main.h" + +#include "MOD_meshcache_util.h" /* utility functions */ + +#include "MOD_modifiertypes.h" + +#include "MOD_util.h" + +/* -------------------------------------------------------------------- */ +/* Utility function shared by formats */ +void MOD_meshcache_calc_range(const float frame, const char interp, + const int frame_tot, + int r_index_range[2], float *r_factor) +{ + if (interp == MOD_MESHCACHE_INTERP_NONE) { + r_index_range[0] = r_index_range[1] = max_ii(0, min_ii(frame_tot - 1, (int)(floorf(frame) + 0.5f))); + *r_factor = 1.0f; /* dummy */ + } + else { + const float tframe = floorf(frame); + const float range = frame - tframe; + r_index_range[0] = (int)tframe; + if (range <= FRAME_SNAP_EPS) { + /* we're close enough not to need blending */ + r_index_range[1] = r_index_range[0]; + *r_factor = 1.0f; /* dummy */ + } + else { + /* blend between 2 frames */ + r_index_range[1] = r_index_range[0] + 1; + *r_factor = range; + } + + /* clamp */ + if ((r_index_range[0] >= frame_tot) || + (r_index_range[1] >= frame_tot)) + { + r_index_range[0] = r_index_range[1] = frame_tot - 1; + *r_factor = 1.0f; /* dummy */ + } + else if ((r_index_range[0] < 0) || + (r_index_range[1] < 0)) + { + r_index_range[0] = r_index_range[1] = 0; + *r_factor = 1.0f; /* dummy */ + } + } +} + + +/* -------------------------------------------------------------------- */ + +static void initData(ModifierData *md) +{ + MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md; + + mcmd->flag = 0; + mcmd->type = MOD_MESHCACHE_TYPE_MDD; + mcmd->interp = MOD_MESHCACHE_INTERP_LINEAR; + mcmd->frame_scale = 1.0f; + + /* (Y, Z). Blender default */ + mcmd->forward_axis = 1; + mcmd->up_axis = 2; +} + +static void copyData(ModifierData *md, ModifierData *target) +{ + MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md; + MeshCacheModifierData *tmcmd = (MeshCacheModifierData *)target; + + tmcmd->flag = mcmd->flag; + tmcmd->type = mcmd->type; + + tmcmd->time_mode = mcmd->time_mode; + tmcmd->play_mode = mcmd->play_mode; + + tmcmd->forward_axis = mcmd->forward_axis; + tmcmd->up_axis = mcmd->up_axis; + tmcmd->flip_axis = mcmd->flip_axis; + + tmcmd->interp = mcmd->interp; + + tmcmd->frame_start = mcmd->frame_start; + tmcmd->frame_scale = mcmd->frame_scale; + + tmcmd->eval_frame = mcmd->eval_frame; + tmcmd->eval_time = mcmd->eval_time; + tmcmd->eval_factor = mcmd->eval_factor; + + BLI_strncpy(tmcmd->filepath, mcmd->filepath, sizeof(tmcmd->filepath)); +} + +static int dependsOnTime(ModifierData *md) +{ + MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md; + return (mcmd->play_mode == MOD_MESHCACHE_PLAY_CFEA); +} + +static int isDisabled(ModifierData *md, int UNUSED(useRenderParams)) +{ + MeshCacheModifierData *mcmd = (MeshCacheModifierData *) md; + + /* leave it up to the modifier to check the file is valid on calculation */ + return (mcmd->filepath[0] == '\0'); +} + + +static void meshcache_do( + MeshCacheModifierData *mcmd, Object *ob, DerivedMesh *UNUSED(dm), + float (*vertexCos)[3], int numVerts) +{ + Scene *scene = mcmd->modifier.scene; + const float fps = FPS; + + char filepath[FILE_MAX]; + const char *err_str = NULL; + bool ok; + + float time; + + + /* -------------------------------------------------------------------- */ + /* Interpret Time (the reading functions also do some of this ) */ + if (mcmd->play_mode == MOD_MESHCACHE_PLAY_CFEA) { + const float cfra = BKE_scene_frame_get(scene); + + switch (mcmd->time_mode) { + case MOD_MESHCACHE_TIME_FRAME: + { + time = cfra; + break; + } + case MOD_MESHCACHE_TIME_SECONDS: + { + time = cfra / fps; + break; + } + case MOD_MESHCACHE_TIME_FACTOR: + default: + { + time = cfra / fps; + break; + } + } + + /* apply offset and scale */ + time = (mcmd->frame_scale * time) - mcmd->frame_start; + } + else { /* if (mcmd->play_mode == MOD_MESHCACHE_PLAY_EVAL) { */ + switch (mcmd->time_mode) { + case MOD_MESHCACHE_TIME_FRAME: + { + time = mcmd->eval_frame; + break; + } + case MOD_MESHCACHE_TIME_SECONDS: + { + time = mcmd->eval_time; + break; + } + case MOD_MESHCACHE_TIME_FACTOR: + default: + { + time = mcmd->eval_factor; + break; + } + } + } + + + /* -------------------------------------------------------------------- */ + /* Read the File (or error out when the file is bad) */ + + /* would be nice if we could avoid doing this _every_ frame */ + BLI_strncpy(filepath, mcmd->filepath, sizeof(filepath)); + BLI_path_abs(filepath, ID_BLEND_PATH(G.main, (ID *)ob)); + + switch (mcmd->type) { + case MOD_MESHCACHE_TYPE_MDD: + ok = MOD_meshcache_read_mdd_times(filepath, vertexCos, numVerts, + mcmd->interp, time, fps, mcmd->time_mode, &err_str); + break; + case MOD_MESHCACHE_TYPE_PC2: + ok = MOD_meshcache_read_pc2_times(filepath, vertexCos, numVerts, + mcmd->interp, time, fps, mcmd->time_mode, &err_str); + break; + default: + ok = false; + break; + } + + + /* -------------------------------------------------------------------- */ + /* Apply the transformation matrix (if needed) */ + if (UNLIKELY(err_str)) { + modifier_setError(&mcmd->modifier, err_str); + } + else if (ok) { + bool use_matrix = false; + float mat[3][3]; + unit_m3(mat); + + if (mat3_from_axis_conversion(mcmd->forward_axis, mcmd->up_axis, 1, 2, mat)) { + use_matrix = true; + } + + if (mcmd->flip_axis) { + float tmat[3][3]; + unit_m3(tmat); + if (mcmd->flip_axis & (1 << 0)) tmat[0][0] = -1.0f; + if (mcmd->flip_axis & (1 << 1)) tmat[1][1] = -1.0f; + if (mcmd->flip_axis & (1 << 2)) tmat[2][2] = -1.0f; + mul_m3_m3m3(mat, tmat, mat); + + use_matrix = true; + } + + if (use_matrix) { + int i; + for (i = 0; i < numVerts; i++) { + mul_m3_v3(mat, vertexCos[i]); + } + } + } +} + +static void deformVerts(ModifierData *md, Object *ob, + DerivedMesh *derivedData, + float (*vertexCos)[3], + int numVerts, + ModifierApplyFlag UNUSED(flag)) +{ + MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md; + + meshcache_do(mcmd, ob, derivedData, vertexCos, numVerts); +} + +static void deformVertsEM( + ModifierData *md, Object *ob, struct BMEditMesh *UNUSED(editData), + DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts) +{ + MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md; + + meshcache_do(mcmd, ob, derivedData, vertexCos, numVerts); +} + + +ModifierTypeInfo modifierType_MeshCache = { + /* name */ "Mesh Cache", + /* structName */ "MeshCacheModifierData", + /* structSize */ sizeof(MeshCacheModifierData), + /* type */ eModifierTypeType_OnlyDeform, + /* flags */ eModifierTypeFlag_AcceptsCVs | + eModifierTypeFlag_SupportsEditmode, + + /* copyData */ copyData, + /* deformVerts */ deformVerts, + /* deformMatrices */ NULL, + /* deformVertsEM */ deformVertsEM, + /* deformMatricesEM */ NULL, + /* applyModifier */ NULL, + /* applyModifierEM */ NULL, + /* initData */ initData, + /* requiredDataMask */ NULL, + /* freeData */ NULL, + /* isDisabled */ isDisabled, + /* updateDepgraph */ NULL, + /* dependsOnTime */ dependsOnTime, + /* dependsOnNormals */ NULL, + /* foreachObjectLink */ NULL, + /* foreachIDLink */ NULL, + /* foreachTexLink */ NULL, +}; diff --git a/source/blender/modifiers/intern/MOD_meshcache_mdd.c b/source/blender/modifiers/intern/MOD_meshcache_mdd.c new file mode 100644 index 00000000000..e001855ba0b --- /dev/null +++ b/source/blender/modifiers/intern/MOD_meshcache_mdd.c @@ -0,0 +1,301 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Campbell Barton, pkowal + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/modifiers/intern/MOD_meshcache_mdd.c + * \ingroup modifiers + */ + +#include <stdio.h> +#include <string.h> +#include <errno.h> + +#include "BLO_sys_types.h" +#include "BLI_utildefines.h" +#include "BLI_endian_switch.h" +#include "BLI_fileops.h" +#include "BLI_math.h" + +#include "MOD_meshcache_util.h" /* own include */ + +#include "DNA_modifier_types.h" + +typedef struct MDDHead { + int frame_tot; + int verts_tot; +} MDDHead; /* frames, verts */ + +static bool meshcache_read_mdd_head(FILE *fp, const int verts_tot, + MDDHead *mdd_head, + const char **err_str) +{ + if (!fread(mdd_head, sizeof(*mdd_head), 1, fp)) { + *err_str = "Missing header"; + return false; + } + +#ifdef __LITTLE_ENDIAN__ + BLI_endian_switch_int32_array((int *)mdd_head, 2); +#endif + + if (mdd_head->verts_tot != verts_tot) { + *err_str = "Vertex count mismatch"; + return false; + } + + if (mdd_head->frame_tot <= 0) { + *err_str = "Invalid frame total"; + return false; + } + /* intentionally dont seek back */ + + return true; +} + +/** + * Gets the index frange and factor + */ +static bool meshcache_read_mdd_range(FILE *fp, + const int verts_tot, + const float frame, const char interp, + int r_index_range[2], float *r_factor, + const char **err_str) +{ + MDDHead mdd_head; + + /* first check interpolation and get the vert locations */ + + if (meshcache_read_mdd_head(fp, verts_tot, &mdd_head, err_str) == false) { + return false; + } + + MOD_meshcache_calc_range(frame, interp, mdd_head.frame_tot, r_index_range, r_factor); + + return true; +} + +static bool meshcache_read_mdd_range_from_time(FILE *fp, + const int verts_tot, + const float time, const float UNUSED(fps), + float *r_frame, + const char **err_str) +{ + MDDHead mdd_head; + int i; + float f_time, f_time_prev = FLT_MAX; + float frame; + + if (meshcache_read_mdd_head(fp, verts_tot, &mdd_head, err_str) == false) { + return false; + } + + for (i = 0; i < mdd_head.frame_tot; i++) { + fread(&f_time, sizeof(float), 1, fp); +#ifdef __LITTLE_ENDIAN__ + BLI_endian_switch_float(&f_time); +#endif + if (f_time >= time) { + break; + } + f_time_prev = f_time; + } + + if (i == mdd_head.frame_tot) { + frame = (float)(mdd_head.frame_tot - 1); + } + if (UNLIKELY(f_time_prev == FLT_MAX)) { + frame = 0.0f; + } + else { + const float range = f_time - f_time_prev; + + if (range <= FRAME_SNAP_EPS) { + frame = (float)i; + } + else { + frame = (float)(i - 1) + ((time - f_time_prev) / range); + } + } + + *r_frame = frame; + return true; +} + +bool MOD_meshcache_read_mdd_index(FILE *fp, + float (*vertexCos)[3], const int verts_tot, + const int index, const float factor, + const char **err_str) +{ + MDDHead mdd_head; + + if (meshcache_read_mdd_head(fp, verts_tot, &mdd_head, err_str) == false) { + return false; + } + + if (fseek(fp, mdd_head.frame_tot * sizeof(int), SEEK_CUR) != 0) { + *err_str = "Header seek failed"; + return false; + } + + if (fseek(fp, index * mdd_head.verts_tot * sizeof(float) * 3, SEEK_CUR) != 0) { + *err_str = "Failed to seek frame"; + return false; + } + + if (factor >= 1.0f) { +#if 1 + float *vco = *vertexCos; + unsigned int i; + for (i = mdd_head.verts_tot; i != 0 ; i--, vco += 3) { + fread(vco, sizeof(float) * 3, 1, fp); + +# ifdef __LITTLE_ENDIAN__ + BLI_endian_switch_float(vco + 0); + BLI_endian_switch_float(vco + 1); + BLI_endian_switch_float(vco + 2); +# endif /* __LITTLE_ENDIAN__ */ + } +#else + /* no blending */ + if (!fread(vertexCos, sizeof(float) * 3, mdd_head.verts_tot, f)) { + *err_str = errno ? strerror(errno) : "Failed to read frame"; + return false; + } +# ifdef __LITTLE_ENDIAN__ + BLI_endian_switch_float_array(vertexCos[0], mdd_head.verts_tot * 3); +# endif +#endif + } + else { + const float ifactor = 1.0f - factor; + float *vco = *vertexCos; + unsigned int i; + for (i = mdd_head.verts_tot; i != 0 ; i--, vco += 3) { + float tvec[3]; + fread(tvec, sizeof(float) * 3, 1, fp); + +#ifdef __LITTLE_ENDIAN__ + BLI_endian_switch_float(tvec + 0); + BLI_endian_switch_float(tvec + 1); + BLI_endian_switch_float(tvec + 2); +#endif + + vco[0] = (vco[0] * ifactor) + (tvec[0] * factor); + vco[1] = (vco[1] * ifactor) + (tvec[1] * factor); + vco[2] = (vco[2] * ifactor) + (tvec[2] * factor); + } + } + + return true; +} + +bool MOD_meshcache_read_mdd_frame(FILE *fp, + float (*vertexCos)[3], const int verts_tot, const char interp, + const float frame, + const char **err_str) +{ + int index_range[2]; + float factor; + + if (meshcache_read_mdd_range(fp, verts_tot, frame, interp, + index_range, &factor, /* read into these values */ + err_str) == false) + { + return false; + } + + if (index_range[0] == index_range[1]) { + /* read single */ + if ((fseek(fp, 0, SEEK_SET) == 0) && + MOD_meshcache_read_mdd_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, err_str)) + { + return true; + } + else { + return false; + } + } + else { + /* read both and interpolate */ + if ((fseek(fp, 0, SEEK_SET) == 0) && + MOD_meshcache_read_mdd_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, err_str) && + (fseek(fp, 0, SEEK_SET) == 0) && + MOD_meshcache_read_mdd_index(fp, vertexCos, verts_tot, index_range[1], factor, err_str)) + { + return true; + } + else { + return false; + } + } +} + +bool MOD_meshcache_read_mdd_times(const char *filepath, + float (*vertexCos)[3], const int verts_tot, const char interp, + const float time, const float fps, const char time_mode, + const char **err_str) +{ + float frame; + + FILE *fp = BLI_fopen(filepath, "rb"); + bool ok; + + if (fp == NULL) { + *err_str = errno ? strerror(errno) : "Unknown error opening file"; + return false; + } + + switch (time_mode) { + case MOD_MESHCACHE_TIME_FRAME: + { + frame = time; + break; + } + case MOD_MESHCACHE_TIME_SECONDS: + { + /* we need to find the closest time */ + if (meshcache_read_mdd_range_from_time(fp, verts_tot, time, fps, &frame, err_str) == false) { + fclose(fp); + return false; + } + rewind(fp); + break; + } + case MOD_MESHCACHE_TIME_FACTOR: + default: + { + MDDHead mdd_head; + if (meshcache_read_mdd_head(fp, verts_tot, &mdd_head, err_str) == false) { + fclose(fp); + return false; + } + + frame = CLAMPIS(time, 0.0f, 1.0f) * (float)mdd_head.frame_tot; + rewind(fp); + break; + } + } + + ok = MOD_meshcache_read_mdd_frame(fp, vertexCos, verts_tot, interp, frame, err_str); + + fclose(fp); + return ok; +} diff --git a/source/blender/modifiers/intern/MOD_meshcache_pc2.c b/source/blender/modifiers/intern/MOD_meshcache_pc2.c new file mode 100644 index 00000000000..1ecb347d174 --- /dev/null +++ b/source/blender/modifiers/intern/MOD_meshcache_pc2.c @@ -0,0 +1,277 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Campbell Barton + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/modifiers/intern/MOD_meshcache_pc2.c + * \ingroup modifiers + */ + +#include <stdio.h> +#include <string.h> +#include <errno.h> + +#include "BLO_sys_types.h" +#include "BLI_utildefines.h" +#include "BLI_endian_switch.h" +#include "BLI_fileops.h" +#include "BLI_math.h" + +#include "MOD_meshcache_util.h" /* own include */ + +#include "DNA_modifier_types.h" + +typedef struct PC2Head { + char header[12]; /* 'POINTCACHE2\0' */ + int file_version; /* unused - should be 1 */ + int verts_tot; + float start; + float sampling; + int frame_tot; +} PC2Head; /* frames, verts */ + +static bool meshcache_read_pc2_head(FILE *fp, const int verts_tot, + PC2Head *pc2_head, + const char **err_str) +{ + if (!fread(pc2_head, sizeof(*pc2_head), 1, fp)) { + *err_str = "Missing header"; + return false; + } + + if (strcmp(pc2_head->header, "POINTCACHE2") != 0) { + *err_str = "Invalid header"; + return false; + } + +#ifdef __BIG_ENDIAN__ + BLI_endian_switch_int32_array(&pc2_head->huh, (sizeof(*pc2_head) - sizeof(pc2_head->header)) / sizeof(int)); +#endif + + if (pc2_head->verts_tot != verts_tot) { + *err_str = "Vertex count mismatch"; + return false; + } + + if (pc2_head->frame_tot <= 0) { + *err_str = "Invalid frame total"; + return false; + } + /* intentionally dont seek back */ + + return true; +} + + +/** + * Gets the index frange and factor + * + * currently same as for MDD + */ +static bool meshcache_read_pc2_range(FILE *fp, + const int verts_tot, + const float frame, const char interp, + int r_index_range[2], float *r_factor, + const char **err_str) +{ + PC2Head pc2_head; + + /* first check interpolation and get the vert locations */ + + if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) { + return false; + } + + MOD_meshcache_calc_range(frame, interp, pc2_head.frame_tot, r_index_range, r_factor); + + return true; +} + +static bool meshcache_read_pc2_range_from_time(FILE *fp, + const int verts_tot, + const float time, const float fps, + float *r_frame, + const char **err_str) +{ + PC2Head pc2_head; + float frame; + + if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) { + return false; + } + + frame = ((time / fps) - pc2_head.start) / pc2_head.sampling; + + if (frame >= pc2_head.frame_tot) { + frame = (float)(pc2_head.frame_tot - 1); + } + else if (frame < 0.0f) { + frame = 0.0f; + } + + *r_frame = frame; + return true; +} + +bool MOD_meshcache_read_pc2_index(FILE *fp, + float (*vertexCos)[3], const int verts_tot, + const int index, const float factor, + const char **err_str) +{ + PC2Head pc2_head; + + if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) { + return false; + } + + if (fseek(fp, index * pc2_head.verts_tot * sizeof(float) * 3, SEEK_CUR) != 0) { + *err_str = "Failed to seek frame"; + return false; + } + + if (factor >= 1.0f) { + float *vco = *vertexCos; + unsigned int i; + for (i = pc2_head.verts_tot; i != 0 ; i--, vco += 3) { + fread(vco, sizeof(float) * 3, 1, fp); + +# ifdef __BIG_ENDIAN__ + BLI_endian_switch_float(vco + 0); + BLI_endian_switch_float(vco + 1); + BLI_endian_switch_float(vco + 2); +# endif /* __BIG_ENDIAN__ */ + } + } + else { + const float ifactor = 1.0f - factor; + float *vco = *vertexCos; + unsigned int i; + for (i = pc2_head.verts_tot; i != 0 ; i--, vco += 3) { + float tvec[3]; + fread(tvec, sizeof(float) * 3, 1, fp); + +#ifdef __BIG_ENDIAN__ + BLI_endian_switch_float(tvec + 0); + BLI_endian_switch_float(tvec + 1); + BLI_endian_switch_float(tvec + 2); +#endif /* __BIG_ENDIAN__ */ + + vco[0] = (vco[0] * ifactor) + (tvec[0] * factor); + vco[1] = (vco[1] * ifactor) + (tvec[1] * factor); + vco[2] = (vco[2] * ifactor) + (tvec[2] * factor); + } + } + + return true; +} + + +bool MOD_meshcache_read_pc2_frame(FILE *fp, + float (*vertexCos)[3], const int verts_tot, const char interp, + const float frame, + const char **err_str) +{ + int index_range[2]; + float factor; + + if (meshcache_read_pc2_range(fp, verts_tot, frame, interp, + index_range, &factor, /* read into these values */ + err_str) == false) + { + return false; + } + + if (index_range[0] == index_range[1]) { + /* read single */ + if ((fseek(fp, 0, SEEK_SET) == 0) && + MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, err_str)) + { + return true; + } + else { + return false; + } + } + else { + /* read both and interpolate */ + if ((fseek(fp, 0, SEEK_SET) == 0) && + MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, err_str) && + (fseek(fp, 0, SEEK_SET) == 0) && + MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[1], factor, err_str)) + { + return true; + } + else { + return false; + } + } +} + +bool MOD_meshcache_read_pc2_times(const char *filepath, + float (*vertexCos)[3], const int verts_tot, const char interp, + const float time, const float fps, const char time_mode, + const char **err_str) +{ + float frame; + + FILE *fp = BLI_fopen(filepath, "rb"); + bool ok; + + if (fp == NULL) { + *err_str = errno ? strerror(errno) : "Unknown error opening file"; + return false; + } + + switch (time_mode) { + case MOD_MESHCACHE_TIME_FRAME: + { + frame = time; + break; + } + case MOD_MESHCACHE_TIME_SECONDS: + { + /* we need to find the closest time */ + if (meshcache_read_pc2_range_from_time(fp, verts_tot, time, fps, &frame, err_str) == false) { + fclose(fp); + return false; + } + rewind(fp); + break; + } + case MOD_MESHCACHE_TIME_FACTOR: + default: + { + PC2Head pc2_head; + if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) { + fclose(fp); + return false; + } + + frame = CLAMPIS(time, 0.0f, 1.0f) * (float)pc2_head.frame_tot; + rewind(fp); + break; + } + } + + ok = MOD_meshcache_read_pc2_frame(fp, vertexCos, verts_tot, interp, frame, err_str); + + fclose(fp); + return ok; +} diff --git a/source/blender/modifiers/intern/MOD_meshcache_util.h b/source/blender/modifiers/intern/MOD_meshcache_util.h new file mode 100644 index 00000000000..4cbae43d051 --- /dev/null +++ b/source/blender/modifiers/intern/MOD_meshcache_util.h @@ -0,0 +1,63 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Campbell Barton + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/modifiers/intern/MOD_meshcache_util.h + * \ingroup modifiers + */ + +#ifndef __MOD_MESHCACHE_UTIL_H__ + +/* MOD_meshcache_mdd.c */ +bool MOD_meshcache_read_mdd_index(FILE *fp, + float (*vertexCos)[3], const int vertex_tot, + const int index, const float factor, + const char **err_str); +bool MOD_meshcache_read_mdd_frame(FILE *fp, + float (*vertexCos)[3], const int verts_tot, const char interp, + const float frame, + const char **err_str); +bool MOD_meshcache_read_mdd_times(const char *filepath, + float (*vertexCos)[3], const int verts_tot, const char interp, + const float time, const float fps, const char time_mode, + const char **err_str); + +/* MOD_meshcache_pc2.c */ +bool MOD_meshcache_read_pc2_index(FILE *fp, + float (*vertexCos)[3], const int verts_tot, + const int index, const float factor, + const char **err_str); +bool MOD_meshcache_read_pc2_frame(FILE *fp, + float (*vertexCos)[3], const int verts_tot, const char interp, + const float frame, + const char **err_str); +bool MOD_meshcache_read_pc2_times(const char *filepath, + float (*vertexCos)[3], const int verts_tot, const char interp, + const float time, const float fps, const char time_mode, + const char **err_str); + +void MOD_meshcache_calc_range(const float frame, const char interp, + const int frame_tot, + int r_index_range[2], float *r_factor); + +#define FRAME_SNAP_EPS 0.0001f + +#endif /* __MOD_MESHCACHE_UTIL_H__ */ diff --git a/source/blender/modifiers/intern/MOD_util.c b/source/blender/modifiers/intern/MOD_util.c index a94c51aa6b4..1084023fcf0 100644 --- a/source/blender/modifiers/intern/MOD_util.c +++ b/source/blender/modifiers/intern/MOD_util.c @@ -280,5 +280,6 @@ void modifier_type_init(ModifierTypeInfo *types[]) INIT_TYPE(LaplacianSmooth); INIT_TYPE(Triangulate); INIT_TYPE(UVWarp); + INIT_TYPE(MeshCache); #undef INIT_TYPE } |