diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-01-21 19:41:00 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-01-21 19:41:00 +0400 |
commit | caac27dcbc4fb652fbb417b4b312493b0ef0eea0 (patch) | |
tree | cb974c99dd41d9b15fdbe4518ea581d53ae6e666 /source/blender | |
parent | 4c0ebedc66c849e2b019f3932bd2208dd9667428 (diff) |
mesh-cache deform modifier,
supports MDD and PC2 formats.
see wiki docs:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Modifiers/Deform/Mesh_Cache
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenkernel/intern/bpath.c | 4 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_rotation.h | 3 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_rotation.c | 149 | ||||
-rw-r--r-- | source/blender/editors/space_outliner/outliner_draw.c | 3 | ||||
-rw-r--r-- | source/blender/makesdna/DNA_modifier_types.h | 56 | ||||
-rw-r--r-- | source/blender/makesrna/RNA_access.h | 1 | ||||
-rw-r--r-- | source/blender/makesrna/RNA_enum_types.h | 2 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_modifier.c | 134 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_object.c | 21 | ||||
-rw-r--r-- | source/blender/modifiers/CMakeLists.txt | 4 | ||||
-rw-r--r-- | source/blender/modifiers/MOD_modifiertypes.h | 1 | ||||
-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 |
16 files changed, 1321 insertions, 13 deletions
diff --git a/source/blender/blenkernel/intern/bpath.c b/source/blender/blenkernel/intern/bpath.c index bb610ede9f7..b0021050e8f 100644 --- a/source/blender/blenkernel/intern/bpath.c +++ b/source/blender/blenkernel/intern/bpath.c @@ -459,6 +459,10 @@ void BKE_bpath_traverse_id(Main *bmain, ID *id, BPathVisitor visit_cb, const int OceanModifierData *omd = (OceanModifierData *) md; rewrite_path_fixed(omd->cachepath, visit_cb, absbase, bpath_user_data); } + else if (md->type == eModifierType_MeshCache) { + MeshCacheModifierData *mcmd = (MeshCacheModifierData *) md; + rewrite_path_fixed(mcmd->filepath, visit_cb, absbase, bpath_user_data); + } } if (ob->soft) { diff --git a/source/blender/blenlib/BLI_math_rotation.h b/source/blender/blenlib/BLI_math_rotation.h index 652925fbe49..e349a05ac23 100644 --- a/source/blender/blenlib/BLI_math_rotation.h +++ b/source/blender/blenlib/BLI_math_rotation.h @@ -186,6 +186,9 @@ float fov_to_focallength(float fov, float sensor); float angle_wrap_rad(float angle); float angle_wrap_deg(float angle); +int mat3_from_axis_conversion(int from_forward, int from_up, int to_forward, int to_up, + float r_mat[3][3]); + #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/intern/math_rotation.c b/source/blender/blenlib/intern/math_rotation.c index dc54bf9ace3..b38b5a2de10 100644 --- a/source/blender/blenlib/intern/math_rotation.c +++ b/source/blender/blenlib/intern/math_rotation.c @@ -1737,3 +1737,152 @@ float angle_wrap_deg(float angle) { return mod_inline(angle + 180.0f, 360.0f) - 180.0f; } + +/* axis conversion */ +static float _axis_convert_matrix[23][3][3] = { + {{-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}}, + {{-1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}, {0.0, -1.0, 0.0}}, + {{-1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 1.0, 0.0}}, + {{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, -1.0}}, + {{0.0, -1.0, 0.0}, {-1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}}, + {{0.0, 0.0, 1.0}, {-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}}, + {{0.0, 0.0, -1.0}, {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}}, + {{0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}}, + {{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {-1.0, 0.0, 0.0}}, + {{0.0, 0.0, -1.0}, {0.0, -1.0, 0.0}, {-1.0, 0.0, 0.0}}, + {{0.0, 0.0, 1.0}, {0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0}}, + {{0.0, 1.0, 0.0}, {0.0, 0.0, -1.0}, {-1.0, 0.0, 0.0}}, + {{0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}, {1.0, 0.0, 0.0}}, + {{0.0, 0.0, 1.0}, {0.0, -1.0, 0.0}, {1.0, 0.0, 0.0}}, + {{0.0, 0.0, -1.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}}, + {{0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 0.0}}, + {{0.0, -1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}}, + {{0.0, 0.0, -1.0}, {1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}}, + {{0.0, 0.0, 1.0}, {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}}, + {{0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}}, + {{1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}}, + {{1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, -1.0, 0.0}}, + {{1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}, {0.0, 1.0, 0.0}}, +}; + +static int _axis_convert_lut[23][24] = { + {0x8C8, 0x4D0, 0x2E0, 0xAE8, 0x701, 0x511, 0x119, 0xB29, 0x682, 0x88A, + 0x09A, 0x2A2, 0x80B, 0x413, 0x223, 0xA2B, 0x644, 0x454, 0x05C, 0xA6C, + 0x745, 0x94D, 0x15D, 0x365}, + {0xAC8, 0x8D0, 0x4E0, 0x2E8, 0x741, 0x951, 0x159, 0x369, 0x702, 0xB0A, + 0x11A, 0x522, 0xA0B, 0x813, 0x423, 0x22B, 0x684, 0x894, 0x09C, 0x2AC, + 0x645, 0xA4D, 0x05D, 0x465}, + {0x4C8, 0x2D0, 0xAE0, 0x8E8, 0x681, 0x291, 0x099, 0x8A9, 0x642, 0x44A, + 0x05A, 0xA62, 0x40B, 0x213, 0xA23, 0x82B, 0x744, 0x354, 0x15C, 0x96C, + 0x705, 0x50D, 0x11D, 0xB25}, + {0x2C8, 0xAD0, 0x8E0, 0x4E8, 0x641, 0xA51, 0x059, 0x469, 0x742, 0x34A, + 0x15A, 0x962, 0x20B, 0xA13, 0x823, 0x42B, 0x704, 0xB14, 0x11C, 0x52C, + 0x685, 0x28D, 0x09D, 0x8A5}, + {0x708, 0xB10, 0x120, 0x528, 0x8C1, 0xAD1, 0x2D9, 0x4E9, 0x942, 0x74A, + 0x35A, 0x162, 0x64B, 0xA53, 0x063, 0x46B, 0x804, 0xA14, 0x21C, 0x42C, + 0x885, 0x68D, 0x29D, 0x0A5}, + {0xB08, 0x110, 0x520, 0x728, 0x941, 0x151, 0x359, 0x769, 0x802, 0xA0A, + 0x21A, 0x422, 0xA4B, 0x053, 0x463, 0x66B, 0x884, 0x094, 0x29C, 0x6AC, + 0x8C5, 0xACD, 0x2DD, 0x4E5}, + {0x508, 0x710, 0xB20, 0x128, 0x881, 0x691, 0x299, 0x0A9, 0x8C2, 0x4CA, + 0x2DA, 0xAE2, 0x44B, 0x653, 0xA63, 0x06B, 0x944, 0x754, 0x35C, 0x16C, + 0x805, 0x40D, 0x21D, 0xA25}, + {0x108, 0x510, 0x720, 0xB28, 0x801, 0x411, 0x219, 0xA29, 0x882, 0x08A, + 0x29A, 0x6A2, 0x04B, 0x453, 0x663, 0xA6B, 0x8C4, 0x4D4, 0x2DC, 0xAEC, + 0x945, 0x14D, 0x35D, 0x765}, + {0x748, 0x350, 0x160, 0x968, 0xAC1, 0x2D1, 0x4D9, 0x8E9, 0xA42, 0x64A, + 0x45A, 0x062, 0x68B, 0x293, 0x0A3, 0x8AB, 0xA04, 0x214, 0x41C, 0x82C, + 0xB05, 0x70D, 0x51D, 0x125}, + {0x948, 0x750, 0x360, 0x168, 0xB01, 0x711, 0x519, 0x129, 0xAC2, 0x8CA, + 0x4DA, 0x2E2, 0x88B, 0x693, 0x2A3, 0x0AB, 0xA44, 0x654, 0x45C, 0x06C, + 0xA05, 0x80D, 0x41D, 0x225}, + {0x348, 0x150, 0x960, 0x768, 0xA41, 0x051, 0x459, 0x669, 0xA02, 0x20A, + 0x41A, 0x822, 0x28B, 0x093, 0x8A3, 0x6AB, 0xB04, 0x114, 0x51C, 0x72C, + 0xAC5, 0x2CD, 0x4DD, 0x8E5}, + {0x148, 0x950, 0x760, 0x368, 0xA01, 0x811, 0x419, 0x229, 0xB02, 0x10A, + 0x51A, 0x722, 0x08B, 0x893, 0x6A3, 0x2AB, 0xAC4, 0x8D4, 0x4DC, 0x2EC, + 0xA45, 0x04D, 0x45D, 0x665}, + {0x688, 0x890, 0x0A0, 0x2A8, 0x4C1, 0x8D1, 0xAD9, 0x2E9, 0x502, 0x70A, + 0xB1A, 0x122, 0x74B, 0x953, 0x163, 0x36B, 0x404, 0x814, 0xA1C, 0x22C, + 0x445, 0x64D, 0xA5D, 0x065}, + {0x888, 0x090, 0x2A0, 0x6A8, 0x501, 0x111, 0xB19, 0x729, 0x402, 0x80A, + 0xA1A, 0x222, 0x94B, 0x153, 0x363, 0x76B, 0x444, 0x054, 0xA5C, 0x66C, + 0x4C5, 0x8CD, 0xADD, 0x2E5}, + {0x288, 0x690, 0x8A0, 0x0A8, 0x441, 0x651, 0xA59, 0x069, 0x4C2, 0x2CA, + 0xADA, 0x8E2, 0x34B, 0x753, 0x963, 0x16B, 0x504, 0x714, 0xB1C, 0x12C, + 0x405, 0x20D, 0xA1D, 0x825}, + {0x088, 0x290, 0x6A0, 0x8A8, 0x401, 0x211, 0xA19, 0x829, 0x442, 0x04A, + 0xA5A, 0x662, 0x14B, 0x353, 0x763, 0x96B, 0x4C4, 0x2D4, 0xADC, 0x8EC, + 0x505, 0x10D, 0xB1D, 0x725}, + {0x648, 0x450, 0x060, 0xA68, 0x2C1, 0x4D1, 0x8D9, 0xAE9, 0x282, 0x68A, + 0x89A, 0x0A2, 0x70B, 0x513, 0x123, 0xB2B, 0x204, 0x414, 0x81C, 0xA2C, + 0x345, 0x74D, 0x95D, 0x165}, + {0xA48, 0x650, 0x460, 0x068, 0x341, 0x751, 0x959, 0x169, 0x2C2, 0xACA, + 0x8DA, 0x4E2, 0xB0B, 0x713, 0x523, 0x12B, 0x284, 0x694, 0x89C, 0x0AC, + 0x205, 0xA0D, 0x81D, 0x425}, + {0x448, 0x050, 0xA60, 0x668, 0x281, 0x091, 0x899, 0x6A9, 0x202, 0x40A, + 0x81A, 0xA22, 0x50B, 0x113, 0xB23, 0x72B, 0x344, 0x154, 0x95C, 0x76C, + 0x2C5, 0x4CD, 0x8DD, 0xAE5}, + {0x048, 0xA50, 0x660, 0x468, 0x201, 0xA11, 0x819, 0x429, 0x342, 0x14A, + 0x95A, 0x762, 0x10B, 0xB13, 0x723, 0x52B, 0x2C4, 0xAD4, 0x8DC, 0x4EC, + 0x285, 0x08D, 0x89D, 0x6A5}, + {0x808, 0xA10, 0x220, 0x428, 0x101, 0xB11, 0x719, 0x529, 0x142, 0x94A, + 0x75A, 0x362, 0x8CB, 0xAD3, 0x2E3, 0x4EB, 0x044, 0xA54, 0x65C, 0x46C, + 0x085, 0x88D, 0x69D, 0x2A5}, + {0xA08, 0x210, 0x420, 0x828, 0x141, 0x351, 0x759, 0x969, 0x042, 0xA4A, + 0x65A, 0x462, 0xACB, 0x2D3, 0x4E3, 0x8EB, 0x084, 0x294, 0x69C, 0x8AC, + 0x105, 0xB0D, 0x71D, 0x525}, + {0x408, 0x810, 0xA20, 0x228, 0x081, 0x891, 0x699, 0x2A9, 0x102, 0x50A, + 0x71A, 0xB22, 0x4CB, 0x8D3, 0xAE3, 0x2EB, 0x144, 0x954, 0x75C, 0x36C, + 0x045, 0x44D, 0x65D, 0xA65}, + }; + +// _axis_convert_num = {'X': 0, 'Y': 1, 'Z': 2, '-X': 3, '-Y': 4, '-Z': 5} + +MINLINE int _axis_signed(const int axis) +{ + return (axis < 3) ? axis : axis - 3; +} + +/* + * Each argument us an axis in ['X', 'Y', 'Z', '-X', '-Y', '-Z'] + * where the first 2 are a source and the second 2 are the target. + */ +int mat3_from_axis_conversion(int from_forward, int from_up, int to_forward, int to_up, + float r_mat[3][3]) +{ + // from functools import reduce + int value; + int i; + + if (from_forward == to_forward && from_up == to_up) { + unit_m3(r_mat); + return false; + } + + if ((_axis_signed(from_forward) == _axis_signed(from_up)) || + (_axis_signed(to_forward) == _axis_signed(to_up))) + { + /* we could assert here! */ + unit_m3(r_mat); + return false; + } + + value = ((from_forward << (0 * 3)) | + (from_up << (1 * 3)) | + (to_forward << (2 * 3)) | + (to_up << (3 * 3))); + + for (i = 0; i < (sizeof(_axis_convert_matrix) / sizeof(*_axis_convert_matrix)); i++) { + int j; + for (j = 0; j < sizeof(*_axis_convert_lut) / sizeof(*_axis_convert_lut[0]); j++) { + if (_axis_convert_lut[i][j] == value) { + copy_m3_m3(r_mat, _axis_convert_matrix[i]); + return true; + } + } + + } +// BLI_assert(0); + return false; +} diff --git a/source/blender/editors/space_outliner/outliner_draw.c b/source/blender/editors/space_outliner/outliner_draw.c index 1a058104c78..b64019b01be 100644 --- a/source/blender/editors/space_outliner/outliner_draw.c +++ b/source/blender/editors/space_outliner/outliner_draw.c @@ -1066,7 +1066,8 @@ static void tselem_draw_icon(uiBlock *block, int xmax, float x, float y, TreeSto UI_icon_draw(x, y, ICON_MOD_SKIN); break; case eModifierType_Triangulate: UI_icon_draw(x, y, ICON_MOD_TRIANGULATE); break; - + case eModifierType_MeshCache: + UI_icon_draw(x, y, ICON_MOD_MESHDEFORM); break; /* XXX, needs own icon */ /* Default */ case eModifierType_None: case eModifierType_ShapeKey: diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h index feeaff0d4fb..2f9da059fc0 100644 --- a/source/blender/makesdna/DNA_modifier_types.h +++ b/source/blender/makesdna/DNA_modifier_types.h @@ -77,7 +77,8 @@ typedef enum ModifierType { eModifierType_Skin = 42, eModifierType_LaplacianSmooth = 43, eModifierType_Triangulate = 44, - eModifierType_UVWarp = 45, + eModifierType_UVWarp = 45, + eModifierType_MeshCache = 46, NUM_MODIFIER_TYPES } ModifierType; @@ -1158,4 +1159,57 @@ typedef struct UVWarpModifierData { char uvlayer_name[64]; /* MAX_CUSTOMDATA_LAYER_NAME */ } UVWarpModifierData; +/* cache modifier */ +typedef struct MeshCacheModifierData { + ModifierData modifier; + char flag; + char type; /* file format */ + char time_mode; + char play_mode; + + /* axis conversion */ + char forward_axis; + char up_axis; + char flip_axis; + + char interp; + + char pad[4]; + + /* play_mode == MOD_MESHCACHE_PLAY_CFEA */ + float frame_start; + float frame_scale; + + /* play_mode == MOD_MESHCACHE_PLAY_EVAL */ + /* we could use one float for all these but their purpose is very different */ + float eval_frame; + float eval_time; + float eval_factor; + + char filepath[1024]; // FILE_MAX +} MeshCacheModifierData; + +enum { + MOD_MESHCACHE_TYPE_MDD = 1, + MOD_MESHCACHE_TYPE_PC2 = 2 +}; + +enum { + MOD_MESHCACHE_INTERP_NONE = 0, + MOD_MESHCACHE_INTERP_LINEAR = 1, + // MOD_MESHCACHE_INTERP_CARDINAL = 2 +}; + +enum { + MOD_MESHCACHE_TIME_FRAME = 0, + MOD_MESHCACHE_TIME_SECONDS = 1, + MOD_MESHCACHE_TIME_FACTOR = 2, +}; + +enum { + MOD_MESHCACHE_PLAY_CFEA = 0, + MOD_MESHCACHE_PLAY_EVAL = 1, +}; + + #endif /* __DNA_MODIFIER_TYPES_H__ */ diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 29adb8ff599..6ce3ffa0461 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -92,6 +92,7 @@ extern StructRNA RNA_BoolProperty; extern StructRNA RNA_Brush; extern StructRNA RNA_BrushTextureSlot; extern StructRNA RNA_BuildModifier; +extern StructRNA RNA_MeshCacheModifier; extern StructRNA RNA_Camera; extern StructRNA RNA_CastModifier; extern StructRNA RNA_ChildOfConstraint; diff --git a/source/blender/makesrna/RNA_enum_types.h b/source/blender/makesrna/RNA_enum_types.h index 6733d8a5c05..75c8b2034b0 100644 --- a/source/blender/makesrna/RNA_enum_types.h +++ b/source/blender/makesrna/RNA_enum_types.h @@ -102,6 +102,8 @@ extern EnumPropertyItem object_type_items[]; extern EnumPropertyItem object_type_curve_items[]; +extern EnumPropertyItem object_axis_items[]; + extern EnumPropertyItem controller_type_items[]; extern EnumPropertyItem keymap_propvalue_items[]; diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c index 3fb9e10234e..2ea8b179bce 100644 --- a/source/blender/makesrna/intern/rna_modifier.c +++ b/source/blender/makesrna/intern/rna_modifier.c @@ -64,6 +64,7 @@ EnumPropertyItem modifier_type_items[] = { {eModifierType_WeightVGMix, "VERTEX_WEIGHT_MIX", ICON_MOD_VERTEX_WEIGHT, "Vertex Weight Mix", ""}, {eModifierType_WeightVGProximity, "VERTEX_WEIGHT_PROXIMITY", ICON_MOD_VERTEX_WEIGHT, "Vertex Weight Proximity", ""}, + {eModifierType_MeshCache, "MESH_CACHE", ICON_NONE, "Mesh Cache", ""}, {0, "", 0, N_("Generate"), ""}, {eModifierType_Array, "ARRAY", ICON_MOD_ARRAY, "Array", ""}, {eModifierType_Bevel, "BEVEL", ICON_MOD_BEVEL, "Bevel", ""}, @@ -219,6 +220,8 @@ static StructRNA *rna_Modifier_refine(struct PointerRNA *ptr) return &RNA_TriangulateModifier; case eModifierType_UVWarp: return &RNA_UVWarpModifier; + case eModifierType_MeshCache: + return &RNA_MeshCacheModifier; /* Default */ case eModifierType_None: case eModifierType_ShapeKey: @@ -3478,6 +3481,136 @@ static void rna_def_modifier_triangulate(BlenderRNA *brna) RNA_def_property_update(prop, 0, "rna_Modifier_update"); } +static void rna_def_modifier_meshcache(BlenderRNA *brna) +{ + static EnumPropertyItem prop_format_type_items[] = { + {MOD_MESHCACHE_TYPE_MDD, "MDD", 0, "MDD ", ""}, + {MOD_MESHCACHE_TYPE_PC2, "PC2", 0, "PC2", ""}, + {0, NULL, 0, NULL, NULL} + }; + + static EnumPropertyItem prop_interpolation_type_items[] = { + {MOD_MESHCACHE_INTERP_NONE, "NONE", 0, "None ", ""}, + {MOD_MESHCACHE_INTERP_LINEAR, "LINEAR", 0, "Linear", ""}, + /* for cardinal we'd need to read 4x cache's */ + // {MOD_MESHCACHE_INTERP_CARDINAL, "CARDINAL", 0, "Cardinal", ""}, + {0, NULL, 0, NULL, NULL} + }; + + static EnumPropertyItem prop_time_type_items[] = { + {MOD_MESHCACHE_TIME_FRAME, "FRAME", 0, "Frame", "Control playback using a frame-number " + "(ignoring time FPS and start frame from the file)"}, /* use 'eval_frame' */ + {MOD_MESHCACHE_TIME_SECONDS, "TIME", 0, "Time", "Control playback using time in seconds"}, /* use 'eval_time' */ + {MOD_MESHCACHE_TIME_FACTOR, "FACTOR", 0, "Factor", "Control playback using a valid between [0 - 1]"}, /* use 'eval_factor' */ + {0, NULL, 0, NULL, NULL} + }; + + static EnumPropertyItem prop_time_play_items[] = { + {MOD_MESHCACHE_PLAY_CFEA, "SCENE", 0, "Scene", "Use the time from the scene"}, + {MOD_MESHCACHE_PLAY_EVAL, "CUSTOM", 0, "Custom", "Use the modifiers own time evaluation"}, + {0, NULL, 0, NULL, NULL} + }; + + static EnumPropertyItem prop_flip_axis_flag_items[] = { + {(1 << 0), "X", 0, "X", ""}, + {(1 << 1), "Y", 0, "Y", ""}, + {(1 << 2), "Z", 0, "Z", ""}, + {0, NULL, 0, NULL, NULL} + }; + + StructRNA *srna; + PropertyRNA *prop; + + srna = RNA_def_struct(brna, "MeshCacheModifier", "Modifier"); + RNA_def_struct_ui_text(srna, "Cache Modifier", "Cache Mesh"); + RNA_def_struct_sdna(srna, "MeshCacheModifierData"); + RNA_def_struct_ui_icon(srna, ICON_MOD_MESHDEFORM); /* XXX, needs own icon */ + + prop = RNA_def_property(srna, "cache_format", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "type"); + RNA_def_property_enum_items(prop, prop_format_type_items); + RNA_def_property_ui_text(prop, "Format", ""); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "interpolation", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "interp"); + RNA_def_property_enum_items(prop, prop_interpolation_type_items); + RNA_def_property_ui_text(prop, "Interpolation", ""); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "time_mode", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "time_mode"); + RNA_def_property_enum_items(prop, prop_time_type_items); + RNA_def_property_ui_text(prop, "Time Mode", "Method to control playback time"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "play_mode", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "play_mode"); + RNA_def_property_enum_items(prop, prop_time_play_items); + RNA_def_property_ui_text(prop, "Time Mode", ""); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + + prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_FILEPATH); + RNA_def_property_ui_text(prop, "File Path", "Path to external displacements file"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + /* -------------------------------------------------------------------- */ + /* Axis Conversion */ + prop = RNA_def_property(srna, "forward_axis", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "forward_axis"); + RNA_def_property_enum_items(prop, object_axis_items); + RNA_def_property_ui_text(prop, "Forward", ""); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "up_axis", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "up_axis"); + RNA_def_property_enum_items(prop, object_axis_items); + RNA_def_property_ui_text(prop, "Up", ""); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "flip_axis", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "flip_axis"); + RNA_def_property_enum_items(prop, prop_flip_axis_flag_items); + RNA_def_property_flag(prop, PROP_ENUM_FLAG); + RNA_def_property_ui_text(prop, "Flip Axis", ""); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + /* -------------------------------------------------------------------- */ + /* For Scene time */ + prop = RNA_def_property(srna, "frame_start", PROP_FLOAT, PROP_NONE); + RNA_def_property_float_sdna(prop, NULL, "frame_start"); + RNA_def_property_range(prop, -MAXFRAME, MAXFRAME); + RNA_def_property_ui_text(prop, "Frame Start", "Add this to the start frame"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "frame_scale", PROP_FLOAT, PROP_NONE); + RNA_def_property_float_sdna(prop, NULL, "frame_scale"); + RNA_def_property_range(prop, 0.0f, 100.0f); + RNA_def_property_ui_text(prop, "Frame Scale", "Evaluation time in seconds"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + /* -------------------------------------------------------------------- */ + /* eval values depend on 'time_mode' */ + prop = RNA_def_property(srna, "eval_frame", PROP_FLOAT, PROP_NONE); + RNA_def_property_float_sdna(prop, NULL, "eval_frame"); + RNA_def_property_range(prop, MINFRAME, MAXFRAME); + RNA_def_property_ui_text(prop, "Evaluation Frame", "The frame to evaluage (starting at 0)"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "eval_time", PROP_FLOAT, PROP_NONE); + RNA_def_property_float_sdna(prop, NULL, "eval_time"); + RNA_def_property_range(prop, 0.0f, FLT_MAX); + RNA_def_property_ui_text(prop, "Evaluation Time", "Evaluation time in seconds"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "eval_factor", PROP_FLOAT, PROP_NONE); + RNA_def_property_float_sdna(prop, NULL, "eval_factor"); + RNA_def_property_range(prop, 0.0f, 1.0f); + RNA_def_property_ui_text(prop, "Evaluation Factor", "Evaluation time in seconds"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); +} + void RNA_def_modifier(BlenderRNA *brna) { StructRNA *srna; @@ -3587,6 +3720,7 @@ void RNA_def_modifier(BlenderRNA *brna) rna_def_modifier_skin(brna); rna_def_modifier_laplaciansmooth(brna); rna_def_modifier_triangulate(brna); + rna_def_modifier_meshcache(brna); } #endif diff --git a/source/blender/makesrna/intern/rna_object.c b/source/blender/makesrna/intern/rna_object.c index 3a4224e7849..5105470a103 100644 --- a/source/blender/makesrna/intern/rna_object.c +++ b/source/blender/makesrna/intern/rna_object.c @@ -156,6 +156,15 @@ EnumPropertyItem object_type_curve_items[] = { {0, NULL, 0, NULL, NULL} }; +EnumPropertyItem object_axis_items[] = { + {OB_POSX, "POS_X", 0, "+X", ""}, + {OB_POSY, "POS_Y", 0, "+Y", ""}, + {OB_POSZ, "POS_Z", 0, "+Z", ""}, + {OB_NEGX, "NEG_X", 0, "-X", ""}, + {OB_NEGY, "NEG_Y", 0, "-Y", ""}, + {OB_NEGZ, "NEG_Z", 0, "-Z", ""}, + {0, NULL, 0, NULL, NULL} +}; #ifdef RNA_RUNTIME @@ -2002,16 +2011,6 @@ static void rna_def_object(BlenderRNA *brna) { StructRNA *srna; PropertyRNA *prop; - - static EnumPropertyItem track_items[] = { - {OB_POSX, "POS_X", 0, "+X", ""}, - {OB_POSY, "POS_Y", 0, "+Y", ""}, - {OB_POSZ, "POS_Z", 0, "+Z", ""}, - {OB_NEGX, "NEG_X", 0, "-X", ""}, - {OB_NEGY, "NEG_Y", 0, "-Y", ""}, - {OB_NEGZ, "NEG_Z", 0, "-Z", ""}, - {0, NULL, 0, NULL, NULL} - }; static EnumPropertyItem up_items[] = { {OB_POSX, "X", 0, "X", ""}, @@ -2142,7 +2141,7 @@ static void rna_def_object(BlenderRNA *brna) * since some other tools still refer to this */ prop = RNA_def_property(srna, "track_axis", PROP_ENUM, PROP_NONE); RNA_def_property_enum_sdna(prop, NULL, "trackflag"); - RNA_def_property_enum_items(prop, track_items); + RNA_def_property_enum_items(prop, object_axis_items); RNA_def_property_ui_text(prop, "Track Axis", "Axis that points in 'forward' direction (applies to DupliFrame when " "parent 'Follow' is enabled)"); diff --git a/source/blender/modifiers/CMakeLists.txt b/source/blender/modifiers/CMakeLists.txt index 33209095164..50ba6241b4d 100644 --- a/source/blender/modifiers/CMakeLists.txt +++ b/source/blender/modifiers/CMakeLists.txt @@ -67,6 +67,9 @@ set(SRC intern/MOD_laplaciansmooth.c intern/MOD_lattice.c intern/MOD_mask.c + intern/MOD_meshcache.c + intern/MOD_meshcache_mdd.c + intern/MOD_meshcache_pc2.c intern/MOD_meshdeform.c intern/MOD_mirror.c intern/MOD_multires.c @@ -100,6 +103,7 @@ set(SRC MOD_modifiertypes.h intern/MOD_boolean_util.h intern/MOD_fluidsim_util.h + intern/MOD_meshcache_util.h intern/MOD_util.h intern/MOD_weightvg_util.h ) diff --git a/source/blender/modifiers/MOD_modifiertypes.h b/source/blender/modifiers/MOD_modifiertypes.h index 17e903e9ebb..bac75b3f30f 100644 --- a/source/blender/modifiers/MOD_modifiertypes.h +++ b/source/blender/modifiers/MOD_modifiertypes.h @@ -78,6 +78,7 @@ extern ModifierTypeInfo modifierType_Skin; extern ModifierTypeInfo modifierType_LaplacianSmooth; extern ModifierTypeInfo modifierType_Triangulate; extern ModifierTypeInfo modifierType_UVWarp; +extern ModifierTypeInfo modifierType_MeshCache; /* MOD_util.c */ void modifier_type_init(ModifierTypeInfo *types[]); 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 } |