diff options
author | Martin Poirier <theeth@yahoo.com> | 2008-10-03 00:39:57 +0400 |
---|---|---|
committer | Martin Poirier <theeth@yahoo.com> | 2008-10-03 00:39:57 +0400 |
commit | c35a2d6ea9dc5226911bd90848e96a59233bdaf8 (patch) | |
tree | be895ae3982a72a5b1cfd4ff9b1766037c07a6d0 /source/blender/blenkernel | |
parent | 39e66e4d6f48b7a54a510ec5504fd500cc07174f (diff) | |
parent | 06c43148a19ad7e8cddf9ba51549b51bdb1f6f0c (diff) |
merge trunk 16118 -> 116886
Diffstat (limited to 'source/blender/blenkernel')
80 files changed, 9387 insertions, 4691 deletions
diff --git a/source/blender/blenkernel/BKE_DerivedMesh.h b/source/blender/blenkernel/BKE_DerivedMesh.h index 7dc10c53e22..5a1e266adeb 100644 --- a/source/blender/blenkernel/BKE_DerivedMesh.h +++ b/source/blender/blenkernel/BKE_DerivedMesh.h @@ -55,6 +55,7 @@ struct EditMesh; struct ModifierData; struct MCol; struct ColorBand; +struct GPUVertexAttribs; /* number of sub-elements each mesh element has (for interpolation) */ #define SUB_ELEMS_VERT 0 @@ -198,7 +199,8 @@ struct DerivedMesh { * * Also called for *final* editmode DerivedMeshes */ - void (*drawFacesSolid)(DerivedMesh *dm, int (*setMaterial)(int)); + void (*drawFacesSolid)(DerivedMesh *dm, + int (*setMaterial)(int, void *attribs)); /* Draw all faces * o If useTwoSided, draw front and back using col arrays @@ -215,6 +217,13 @@ struct DerivedMesh { int (*setDrawOptions)(struct MTFace *tface, struct MCol *mcol, int matnr)); + /* Draw all faces with GLSL materials + * o setMaterial is called for every different material nr + * o Only if setMaterial returns true + */ + void (*drawFacesGLSL)(DerivedMesh *dm, + int (*setMaterial)(int, void *attribs)); + /* Draw mapped faces (no color, or texture) * o Only if !setDrawOptions or * setDrawOptions(userData, mapped-face-index, drawSmooth_r) @@ -241,6 +250,15 @@ struct DerivedMesh { int index), void *userData); + /* Draw mapped faces with GLSL materials + * o setMaterial is called for every different material nr + * o setDrawOptions is called for every face + * o Only if setMaterial and setDrawOptions return true + */ + void (*drawMappedFacesGLSL)(DerivedMesh *dm, + int (*setMaterial)(int, void *attribs), + int (*setDrawOptions)(void *userData, int index), void *userData); + /* Draw mapped edges as lines * o Only if !setDrawOptions or setDrawOptions(userData, mapped-edge) * returns true @@ -412,7 +430,10 @@ DerivedMesh *mesh_create_derived_for_modifier(struct Object *ob, struct Modifier DerivedMesh *mesh_create_derived_render(struct Object *ob, CustomDataMask dataMask); -/* same as above but wont use render settings */ + +DerivedMesh *mesh_create_derived_index_render(struct Object *ob, CustomDataMask dataMask, int index); + + /* same as above but wont use render settings */ DerivedMesh *mesh_create_derived_view(struct Object *ob, CustomDataMask dataMask); DerivedMesh *mesh_create_derived_no_deform(struct Object *ob, @@ -437,6 +458,35 @@ void weight_to_rgb(float input, float *fr, float *fg, float *fb); /* determines required DerivedMesh data according to view and edit modes */ CustomDataMask get_viewedit_datamask(); +/* convert layers requested by a GLSL material to actually available layers in + * the DerivedMesh, with both a pointer for arrays and an offset for editmesh */ +typedef struct DMVertexAttribs { + struct { + struct MTFace *array; + int emOffset, glIndex; + } tface[MAX_MTFACE]; + + struct { + struct MCol *array; + int emOffset, glIndex; + } mcol[MAX_MCOL]; + + struct { + float (*array)[3]; + int emOffset, glIndex; + } tang; + + struct { + float (*array)[3]; + int emOffset, glIndex; + } orco; + + int tottface, totmcol, tottang, totorco; +} DMVertexAttribs; + +void DM_vertex_attributes_from_gpu(DerivedMesh *dm, + struct GPUVertexAttribs *gattribs, DMVertexAttribs *attribs); + void DM_add_tangent_layer(DerivedMesh *dm); #endif diff --git a/source/blender/blenkernel/BKE_action.h b/source/blender/blenkernel/BKE_action.h index 716eac81b55..37ced4cb00b 100644 --- a/source/blender/blenkernel/BKE_action.h +++ b/source/blender/blenkernel/BKE_action.h @@ -159,10 +159,14 @@ float get_action_frame(struct Object *ob, float cframe); /* map strip time to global time (frame nr) */ float get_action_frame_inv(struct Object *ob, float cframe); /* builds a list of NlaIpoChannel with ipo values to write in datablock */ -void extract_ipochannels_from_action(ListBase *lb, struct ID *id, struct bAction *act, char *name, float ctime); +void extract_ipochannels_from_action(ListBase *lb, struct ID *id, struct bAction *act, const char *name, float ctime); /* write values returned by extract_ipochannels_from_action, returns the number of value written */ int execute_ipochannels(ListBase *lb); +/* functions used by the game engine */ +void game_copy_pose(struct bPose **dst, struct bPose *src); +void game_free_pose(struct bPose *pose); + #ifdef __cplusplus }; #endif diff --git a/source/blender/blenkernel/BKE_bad_level_calls.h b/source/blender/blenkernel/BKE_bad_level_calls.h index 3e45749c26a..0b623526562 100644 --- a/source/blender/blenkernel/BKE_bad_level_calls.h +++ b/source/blender/blenkernel/BKE_bad_level_calls.h @@ -115,10 +115,6 @@ void free_editArmature(void); void docenter_new(void); int saveover(char *str); -/* image.c */ -#include "DNA_image_types.h" -void free_realtime_image(Image *ima); // has to become a callback, opengl stuff - /* ipo.c */ void copy_view3d_lock(short val); // was a hack, to make scene layer ipo's possible @@ -145,7 +141,7 @@ short pupmenu(char *instr); // will be general callback /* scene.c */ #include "DNA_sequence_types.h" void free_editing(struct Editing *ed); // scenes and sequences problem... -void BPY_do_all_scripts (short int event); +void BPY_do_all_scripts (short int event, short int anim); int BPY_call_importloader(char *name); diff --git a/source/blender/blenkernel/BKE_blender.h b/source/blender/blenkernel/BKE_blender.h index e1eb6718a30..0105587c5b4 100644 --- a/source/blender/blenkernel/BKE_blender.h +++ b/source/blender/blenkernel/BKE_blender.h @@ -41,7 +41,7 @@ struct ListBase; struct MemFile; #define BLENDER_VERSION 247 -#define BLENDER_SUBVERSION 0 +#define BLENDER_SUBVERSION 9 #define BLENDER_MINVERSION 245 #define BLENDER_MINSUBVERSION 15 diff --git a/source/blender/blenkernel/BKE_bullet.h b/source/blender/blenkernel/BKE_bullet.h new file mode 100644 index 00000000000..1c3bb175d66 --- /dev/null +++ b/source/blender/blenkernel/BKE_bullet.h @@ -0,0 +1,43 @@ +/** + * + * $Id$ + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ +#ifndef BKE_BULLET_H +#define BKE_BULLET_H + +struct BulletSoftBody; + + +/* allocates and initializes general main data */ +extern struct BulletSoftBody *bsbNew(void); + +/* frees internal data and softbody itself */ +extern void bsbFree(struct BulletSoftBody *sb); + +#endif + diff --git a/source/blender/blenkernel/BKE_bvhutils.h b/source/blender/blenkernel/BKE_bvhutils.h new file mode 100644 index 00000000000..dd9ea61f24b --- /dev/null +++ b/source/blender/blenkernel/BKE_bvhutils.h @@ -0,0 +1,98 @@ +/** + * + * $Id$ + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2006 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): André Pinto + * + * ***** END GPL LICENSE BLOCK ***** + */ +#ifndef BKE_BVHUTILS_H +#define BKE_BVHUTILS_H + +#include "BLI_kdopbvh.h" + +/* + * This header encapsulates necessary code to buld a BVH + */ + +struct DerivedMesh; +struct MVert; +struct MFace; + +/* + * struct that kepts basic information about a BVHTree build from a mesh + */ +typedef struct BVHTreeFromMesh +{ + struct BVHTree *tree; + + /* default callbacks to bvh nearest and raycast */ + BVHTree_NearestPointCallback nearest_callback; + BVHTree_RayCastCallback raycast_callback; + + /* Mesh represented on this BVHTree */ + struct DerivedMesh *mesh; + + /* Vertex array, so that callbacks have instante access to data */ + struct MVert *vert; + struct MFace *face; + + /* radius for raycast */ + float sphere_radius; + +} BVHTreeFromMesh; + +/* + * Builds a bvh tree where nodes are the vertexs of the given mesh. + * Configures BVHTreeFromMesh. + * + * The tree is build in mesh space coordinates, this means special care must be made on queries + * so that the coordinates and rays are first translated on the mesh local coordinates. + * Reason for this is that later bvh_from_mesh_* might use a cache system and so it becames possible to reuse + * a BVHTree. + * + * free_bvhtree_from_mesh should be called when the tree is no longer needed. + */ +void bvhtree_from_mesh_verts(struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon, int tree_type, int axis); + +/* + * Builds a bvh tree where nodes are the faces of the given mesh. + * Configures BVHTreeFromMesh. + * + * The tree is build in mesh space coordinates, this means special care must be made on queries + * so that the coordinates and rays are first translated on the mesh local coordinates. + * Reason for this is that later bvh_from_mesh_* might use a cache system and so it becames possible to reuse + * a BVHTree. + * + * free_bvhtree_from_mesh should be called when the tree is no longer needed. + */ +void bvhtree_from_mesh_faces(struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon, int tree_type, int axis); + +/* + * Frees data allocated by a call to bvhtree_from_mesh_*. + */ +void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data); + +#endif + diff --git a/source/blender/blenkernel/BKE_collision.h b/source/blender/blenkernel/BKE_collision.h index 2966d932a49..c483148e4de 100644 --- a/source/blender/blenkernel/BKE_collision.h +++ b/source/blender/blenkernel/BKE_collision.h @@ -119,8 +119,10 @@ FaceCollPair; ///////////////////////////////////////////////// // used in modifier.c from collision.c ///////////////////////////////////////////////// + BVHTree *bvhtree_build_from_mvert ( MFace *mfaces, unsigned int numfaces, MVert *x, unsigned int numverts, float epsilon ); void bvhtree_update_from_mvert ( BVHTree * bvhtree, MFace *faces, int numfaces, MVert *x, MVert *xnew, int numverts, int moving ); + ///////////////////////////////////////////////// LinkNode *BLI_linklist_append_fast ( LinkNode **listp, void *ptr ); @@ -134,6 +136,15 @@ void collisions_compute_barycentric ( float pv[3], float p1[3], float p2[3], flo void interpolateOnTriangle ( float to[3], float v1[3], float v2[3], float v3[3], double w1, double w2, double w3 ); ///////////////////////////////////////////////// +// used in effect.c +///////////////////////////////////////////////// +CollisionModifierData **get_collisionobjects(Object *self, int *numcollobj); + +///////////////////////////////////////////////// + + + +///////////////////////////////////////////////// #endif diff --git a/source/blender/blenkernel/BKE_colortools.h b/source/blender/blenkernel/BKE_colortools.h index e78882220a9..555b467b1d6 100644 --- a/source/blender/blenkernel/BKE_colortools.h +++ b/source/blender/blenkernel/BKE_colortools.h @@ -57,6 +57,7 @@ void curvemapping_do_ibuf(struct CurveMapping *cumap, struct ImBuf *ibuf); void curvemapping_premultiply(struct CurveMapping *cumap, int restore); int curvemapping_RGBA_does_something(struct CurveMapping *cumap); void curvemapping_initialize(struct CurveMapping *cumap); +void curvemapping_table_RGBA(struct CurveMapping *cumap, float **array, int *size); #endif diff --git a/source/blender/blenkernel/BKE_curve.h b/source/blender/blenkernel/BKE_curve.h index 45d8193b16f..25d6d78c4aa 100644 --- a/source/blender/blenkernel/BKE_curve.h +++ b/source/blender/blenkernel/BKE_curve.h @@ -39,9 +39,12 @@ struct ListBase; struct BezTriple; struct BevList; -#define KNOTSU(nu) ( (nu)->orderu+ (nu)->pntsu+ (nu->orderu-1)*((nu)->flagu & CU_CYCLIC) ) -#define KNOTSV(nu) ( (nu)->orderv+ (nu)->pntsv+ (nu->orderv-1)*((nu)->flagv & CU_CYCLIC) ) +#define KNOTSU(nu) ( (nu)->orderu+ (nu)->pntsu+ (((nu)->flagu & CU_CYCLIC) ? (nu->orderu-1) : 0) ) +#define KNOTSV(nu) ( (nu)->orderv+ (nu)->pntsv+ (((nu)->flagv & CU_CYCLIC) ? (nu->orderv-1) : 0) ) +/* Non cyclic nurbs have 1 less segment */ +#define SEGMENTSU(nu) ( ((nu)->flagu & CU_CYCLIC) ? (nu)->pntsu : (nu)->pntsu-1 ) +#define SEGMENTSV(nu) ( ((nu)->flagv & CU_CYCLIC) ? (nu)->pntsv : (nu)->pntsv-1 ) void unlink_curve( struct Curve *cu); void free_curve( struct Curve *cu); @@ -62,15 +65,14 @@ void minmaxNurb( struct Nurb *nu, float *min, float *max); void makeknots( struct Nurb *nu, short uv, short type); -void makeNurbfaces( struct Nurb *nu, float *data, int rowstride); -void makeNurbcurve( struct Nurb *nu, float *data, int resolu, int dim); +void makeNurbfaces(struct Nurb *nu, float *coord_array, int rowstride); +void makeNurbcurve(struct Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, int resolu); void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride); float *make_orco_curve( struct Object *ob); float *make_orco_surf( struct Object *ob); void makebevelcurve( struct Object *ob, struct ListBase *disp); void makeBevelList( struct Object *ob); -float calc_curve_subdiv_radius( struct Curve *cu, struct Nurb *nu, int cursubdiv); void calchandleNurb( struct BezTriple *bezt, struct BezTriple *prev, struct BezTriple *next, int mode); void calchandlesNurb( struct Nurb *nu); diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h index e84c7d30956..c84b690bc49 100644 --- a/source/blender/blenkernel/BKE_customdata.h +++ b/source/blender/blenkernel/BKE_customdata.h @@ -32,9 +32,11 @@ #ifndef BKE_CUSTOMDATA_H #define BKE_CUSTOMDATA_H +#include "BLO_sys_types.h" // for intptr_t support + struct CustomData; struct CustomDataLayer; -typedef long CustomDataMask; +typedef intptr_t CustomDataMask; extern const CustomDataMask CD_MASK_BAREMESH; extern const CustomDataMask CD_MASK_MESH; diff --git a/source/blender/blenkernel/BKE_deform.h b/source/blender/blenkernel/BKE_deform.h index a1975dd4265..e982806a6cc 100644 --- a/source/blender/blenkernel/BKE_deform.h +++ b/source/blender/blenkernel/BKE_deform.h @@ -38,6 +38,7 @@ struct Object; struct ListBase; struct bDeformGroup; +struct MDeformVert; void copy_defgroups (struct ListBase *lb1, struct ListBase *lb2); struct bDeformGroup *copy_defgroup (struct bDeformGroup *ingroup); @@ -46,5 +47,8 @@ int get_defgroup_num (struct Object *ob, struct bDeformGroup *dg); int get_named_vertexgroup_num (Object *ob, char *name); void unique_vertexgroup_name (struct bDeformGroup *dg, struct Object *ob); +float deformvert_get_weight(const struct MDeformVert *dvert, int group_num); +float vertexgroup_get_vertex_weight(const struct MDeformVert *dvert, int index, int group_num); + #endif diff --git a/source/blender/blenkernel/BKE_effect.h b/source/blender/blenkernel/BKE_effect.h index 3763a659f2f..6475f7a71ac 100644 --- a/source/blender/blenkernel/BKE_effect.h +++ b/source/blender/blenkernel/BKE_effect.h @@ -37,6 +37,7 @@ struct Effect; struct ListBase; struct Particle; struct Group; +struct RNG; typedef struct pEffectorCache { struct pEffectorCache *next, *prev; @@ -64,6 +65,11 @@ struct ListBase *pdInitEffectors(struct Object *obsrc, struct Group *group); void pdEndEffectors(struct ListBase *lb); void pdDoEffectors(struct ListBase *lb, float *opco, float *force, float *speed, float cur_time, float loc_time, unsigned int flags); +/* required for particle_system.c */ +void do_physical_effector(Object *ob, float *opco, short type, float force_val, float distance, float falloff, float size, float damp, float *eff_velocity, float *vec_to_part, float *velocity, float *field, int planar, struct RNG *rng, float noise_factor, float charge, float pa_size); +float effector_falloff(struct PartDeflect *pd, float *eff_velocity, float *vec_to_part); + + #endif diff --git a/source/blender/blenkernel/BKE_endian.h b/source/blender/blenkernel/BKE_endian.h index 1757103eaf6..dc5efd5ea46 100644 --- a/source/blender/blenkernel/BKE_endian.h +++ b/source/blender/blenkernel/BKE_endian.h @@ -33,11 +33,11 @@ #define BKE_ENDIANNESS(a) { \ union { \ - long l; \ - char c[sizeof (long)]; \ + intptr_t l; \ + char c[sizeof (intptr_t)]; \ } u; \ u.l = 1; \ - a = (u.c[sizeof (long) - 1] == 1) ? 1 : 0; \ + a = (u.c[sizeof (intptr_t) - 1] == 1) ? 1 : 0; \ } #endif diff --git a/source/blender/blenkernel/BKE_fluidsim.h b/source/blender/blenkernel/BKE_fluidsim.h new file mode 100644 index 00000000000..33c706da82b --- /dev/null +++ b/source/blender/blenkernel/BKE_fluidsim.h @@ -0,0 +1,55 @@ +/** + * BKE_fluidsim.h + * + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) Blender Foundation + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "DNA_modifier_types.h" +#include "DNA_object_fluidsim.h" // N_T +#include "DNA_object_types.h" + +#include "BKE_DerivedMesh.h" + +/* old interface */ +FluidsimSettings *fluidsimSettingsNew(Object *srcob); + +void initElbeemMesh(Object *ob, int *numVertices, float **vertices, int *numTriangles, int **triangles, int useGlobalCoords, int modifierIndex); + + +/* new fluid-modifier interface */ +void fluidsim_init(FluidsimModifierData *fluidmd); +void fluidsim_free(FluidsimModifierData *fluidmd); + +DerivedMesh *fluidsim_read_cache(Object *ob, DerivedMesh *orgdm, FluidsimModifierData *fluidmd, int framenr, int useRenderParams); +DerivedMesh *fluidsimModifier_do(FluidsimModifierData *fluidmd, Object *ob, DerivedMesh *dm, int useRenderParams, int isFinalCalc); + +// get bounding box of mesh +void fluid_get_bb(MVert *mvert, int totvert, float obmat[][4], + /*RET*/ float start[3], /*RET*/ float size[3] ); + + + diff --git a/source/blender/blenkernel/BKE_global.h b/source/blender/blenkernel/BKE_global.h index 8c9634cba06..1cc336db69e 100644 --- a/source/blender/blenkernel/BKE_global.h +++ b/source/blender/blenkernel/BKE_global.h @@ -218,8 +218,15 @@ typedef struct Global { #define G_FILE_NO_UI (1 << 10) #define G_FILE_GAME_TO_IPO (1 << 11) #define G_FILE_GAME_MAT (1 << 12) -#define G_FILE_DIAPLAY_LISTS (1 << 13) +#define G_FILE_DISPLAY_LISTS (1 << 13) #define G_FILE_SHOW_PHYSICS (1 << 14) +#define G_FILE_GAME_MAT_GLSL (1 << 15) +#define G_FILE_GLSL_NO_LIGHTS (1 << 16) +#define G_FILE_GLSL_NO_SHADERS (1 << 17) +#define G_FILE_GLSL_NO_SHADOWS (1 << 18) +#define G_FILE_GLSL_NO_RAMPS (1 << 19) +#define G_FILE_GLSL_NO_NODES (1 << 20) +#define G_FILE_GLSL_NO_EXTRA_TEX (1 << 21) /* G.windowstate */ #define G_WINDOWSTATE_USERDEF 0 diff --git a/source/blender/blenkernel/BKE_ipo.h b/source/blender/blenkernel/BKE_ipo.h index 5b209cb8f5b..fdd176e0e64 100644 --- a/source/blender/blenkernel/BKE_ipo.h +++ b/source/blender/blenkernel/BKE_ipo.h @@ -54,41 +54,71 @@ struct bPoseChannel; struct bActionChannel; struct rctf; +/* ------------ Time Management ------------ */ + float frame_to_float(int cfra); +/* ------------ IPO Management ---------- */ + void free_ipo_curve(struct IpoCurve *icu); void free_ipo(struct Ipo *ipo); + void ipo_default_v2d_cur(int blocktype, struct rctf *cur); + struct Ipo *add_ipo(char *name, int idcode); struct Ipo *copy_ipo(struct Ipo *ipo); + void ipo_idnew(struct Ipo *ipo); + +struct IpoCurve *find_ipocurve(struct Ipo *ipo, int adrcode); +short has_ipo_code(struct Ipo *ipo, int code); + +/* -------------- Make Local -------------- */ + void make_local_obipo(struct Ipo *ipo); void make_local_matipo(struct Ipo *ipo); void make_local_keyipo(struct Ipo *ipo); void make_local_ipo(struct Ipo *ipo); -struct IpoCurve *find_ipocurve(struct Ipo *ipo, int adrcode); + +/* ------------ IPO-Curve Sanity ---------------- */ void calchandles_ipocurve(struct IpoCurve *icu); void testhandles_ipocurve(struct IpoCurve *icu); void sort_time_ipocurve(struct IpoCurve *icu); int test_time_ipocurve(struct IpoCurve *icu); + +/* -------- IPO-Curve (Bezier) Calculations ---------- */ + void correct_bezpart(float *v1, float *v2, float *v3, float *v4); int findzero(float x, float q0, float q1, float q2, float q3, float *o); void berekeny(float f1, float f2, float f3, float f4, float *o, int b); void berekenx(float *f, float *o, int b); + +/* -------- IPO Curve Calculation and Evaluation --------- */ + float eval_icu(struct IpoCurve *icu, float ipotime); void calc_icu(struct IpoCurve *icu, float ctime); float calc_ipo_time(struct Ipo *ipo, float ctime); void calc_ipo(struct Ipo *ipo, float ctime); + +/* ------------ Keyframe Column Tools -------------- */ + +void add_to_cfra_elem(struct ListBase *lb, struct BezTriple *bezt); +void make_cfra_list(struct Ipo *ipo, struct ListBase *elems); + +/* ---------------- IPO DataAPI ----------------- */ + void write_ipo_poin(void *poin, int type, float val); float read_ipo_poin(void *poin, int type); -void *give_mtex_poin(struct MTex *mtex, int adrcode ); -void *get_ipo_poin(struct ID *id, struct IpoCurve *icu, int *type); +void *give_mtex_poin(struct MTex *mtex, int adrcode ); void *get_pchan_ipo_poin(struct bPoseChannel *pchan, int adrcode); +void *get_ipo_poin(struct ID *id, struct IpoCurve *icu, int *type); void set_icu_vars(struct IpoCurve *icu); +/* ---------------- IPO Execution --------------- */ + void execute_ipo(struct ID *id, struct Ipo *ipo); void execute_action_ipo(struct bActionChannel *achan, struct bPoseChannel *pchan); @@ -99,21 +129,16 @@ void do_ob_ipo(struct Object *ob); void do_seq_ipo(struct Sequence *seq, int cfra); void do_ob_ipodrivers(struct Object *ob, struct Ipo *ipo, float ctime); -int has_ipo_code(struct Ipo *ipo, int code); void do_all_data_ipos(void); -int calc_ipo_spec(struct Ipo *ipo, int adrcode, float *ctime); +short calc_ipo_spec(struct Ipo *ipo, int adrcode, float *ctime); void clear_delta_obipo(struct Ipo *ipo); -void add_to_cfra_elem(struct ListBase *lb, struct BezTriple *bezt); -void make_cfra_list(struct Ipo *ipo, struct ListBase *elems); -/* the sort is an IPO_Channel... */ -int IPO_GetChannels(struct Ipo *ipo, short *channels); +/* ----------- IPO <-> GameEngine API ---------------- */ + +/* the short is an IPO_Channel... */ -float IPO_GetFloatValue(struct Ipo *ipo, -/* struct IPO_Channel channel, */ - /* channels are shorts... bit ugly for now*/ - short c, - float ctime); +short IPO_GetChannels(struct Ipo *ipo, short *channels); +float IPO_GetFloatValue(struct Ipo *ipo, short c, float ctime); #ifdef __cplusplus }; diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h index d4ae76e8984..2ca4b3aa39a 100644 --- a/source/blender/blenkernel/BKE_mesh.h +++ b/source/blender/blenkernel/BKE_mesh.h @@ -68,7 +68,6 @@ void mball_to_mesh(struct ListBase *lb, struct Mesh *me); void nurbs_to_mesh(struct Object *ob); void free_dverts(struct MDeformVert *dvert, int totvert); void copy_dverts(struct MDeformVert *dst, struct MDeformVert *src, int totvert); /* __NLA */ -int update_realtime_texture(struct MTFace *tface, double time); void mesh_delete_material_index(struct Mesh *me, int index); void mesh_set_smooth_flag(struct Object *meshOb, int enableSmooth); diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h index b3b68a9b3ff..01c54663c6d 100644 --- a/source/blender/blenkernel/BKE_node.h +++ b/source/blender/blenkernel/BKE_node.h @@ -47,6 +47,9 @@ struct rctf; struct ListBase; struct RenderData; struct Scene; +struct GPUMaterial; +struct GPUNode; +struct GPUNodeStack; #define SOCK_IN 1 #define SOCK_OUT 2 @@ -91,6 +94,9 @@ typedef struct bNodeType { void *pynode; /* holds pointer to python script */ void *pydict; /* holds pointer to python script dictionary (scope)*/ + /* gpu */ + int (*gpufunc)(struct GPUMaterial *mat, struct bNode *node, struct GPUNodeStack *in, struct GPUNodeStack *out); + } bNodeType; /* node->exec, now in use for composites (#define for break is same as ready yes) */ @@ -252,6 +258,8 @@ void nodeShaderSynchronizeID(struct bNode *node, int copyto); extern void (*node_shader_lamp_loop)(struct ShadeInput *, struct ShadeResult *); void set_node_shader_lamp_loop(void (*lamp_loop_func)(struct ShadeInput *, struct ShadeResult *)); +void ntreeGPUMaterialNodes(struct bNodeTree *ntree, struct GPUMaterial *mat); + /* ************** COMPOSITE NODES *************** */ diff --git a/source/blender/blenkernel/BKE_object.h b/source/blender/blenkernel/BKE_object.h index 116a59fa97e..a4a06b704bc 100644 --- a/source/blender/blenkernel/BKE_object.h +++ b/source/blender/blenkernel/BKE_object.h @@ -41,6 +41,7 @@ struct Camera; struct BoundBox; struct View3D; struct SoftBody; +struct BulletSoftBody; struct Group; struct bAction; @@ -48,10 +49,12 @@ void clear_workob(void); void copy_baseflags(void); void copy_objectflags(void); struct SoftBody *copy_softbody(struct SoftBody *sb); +struct BulletSoftBody *copy_bulletsoftbody(struct BulletSoftBody *sb); void copy_object_particlesystems(struct Object *obn, struct Object *ob); void copy_object_softbody(struct Object *obn, struct Object *ob); void object_free_particlesystems(struct Object *ob); void object_free_softbody(struct Object *ob); +void object_free_bulletsoftbody(struct Object *ob); void update_base_layer(struct Object *ob); void free_object(struct Object *ob); @@ -85,6 +88,8 @@ void set_field_offs(float field); void disable_speed_curve(int val); float bsystem_time(struct Object *ob, float cfra, float ofs); +void object_scale_to_mat3(struct Object *ob, float mat[][3]); +void object_rot_to_mat3(struct Object *ob, float mat[][3]); void object_to_mat3(struct Object *ob, float mat[][3]); void object_to_mat4(struct Object *ob, float mat[][4]); diff --git a/source/blender/blenkernel/BKE_particle.h b/source/blender/blenkernel/BKE_particle.h index 944a9081679..caba63ef8ce 100644 --- a/source/blender/blenkernel/BKE_particle.h +++ b/source/blender/blenkernel/BKE_particle.h @@ -73,6 +73,7 @@ typedef struct ParticleEffectorCache { short type, psys_nbr; struct Object obcopy; /* for restoring transformation data */ + struct RNG *rng; /* random noise generator for e.g. wind */ } ParticleEffectorCache; typedef struct ParticleReactEvent { @@ -225,7 +226,7 @@ void psys_interpolate_mcol(struct MCol *mcol, int quad, float *uv, struct MCol * void copy_particle_key(struct ParticleKey *to, struct ParticleKey *from, int time); -void psys_particle_on_emitter(struct Object *ob, struct ParticleSystemModifierData *psmd, int distr, int index, int index_dmcache, float *fuv, float foffset, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor); +void psys_particle_on_emitter(struct ParticleSystemModifierData *psmd, int distr, int index, int index_dmcache, float *fuv, float foffset, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor); struct ParticleSystemModifierData *psys_get_modifier(struct Object *ob, struct ParticleSystem *psys); struct ParticleSettings *psys_new_settings(char *name, struct Main *main); @@ -283,11 +284,12 @@ void psys_mat_hair_to_orco(struct Object *ob, struct DerivedMesh *dm, short from float *psys_cache_vgroup(struct DerivedMesh *dm, struct ParticleSystem *psys, int vgroup); void psys_get_texture(struct Object *ob, struct Material *ma, struct ParticleSystemModifierData *psmd, struct ParticleSystem *psys, struct ParticleData *pa, struct ParticleTexture *ptex, int event); void psys_interpolate_face(struct MVert *mvert, struct MFace *mface, struct MTFace *tface, float (*orcodata)[3], float *uv, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor); +float psys_particle_value_from_verts(struct DerivedMesh *dm, short from, struct ParticleData *pa, float *values); float psys_interpolate_value_from_verts(struct DerivedMesh *dm, short from, int index, float *fw, float *values); void psys_get_from_key(struct ParticleKey *key, float *loc, float *vel, float *rot, float *time); int psys_intersect_dm(struct Object *ob, struct DerivedMesh *dm, float *vert_cos, float *co1, float* co2, float *min_d, int *min_face, float *min_uv, float *face_minmax, float *pa_minmax, float radius, float *ipoint); -void psys_particle_on_dm(struct Object *ob, struct DerivedMesh *dm, int from, int index, int index_dmcache, float *fw, float foffset, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor); +void psys_particle_on_dm(struct DerivedMesh *dm, int from, int index, int index_dmcache, float *fw, float foffset, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor); /* particle_system.c */ void initialize_particle(struct ParticleData *pa, int p, struct Object *ob, struct ParticleSystem *psys, struct ParticleSystemModifierData *psmd); @@ -302,6 +304,7 @@ int psys_particle_dm_face_lookup(struct Object *ob, struct DerivedMesh *dm, int #define PSYS_RESET_ALL 1 #define PSYS_RESET_DEPSGRAPH 2 #define PSYS_RESET_CHILDREN 3 +#define PSYS_RESET_CACHE_MISS 4 /* ParticleEffectorCache->type */ #define PSYS_EC_EFFECTOR 1 diff --git a/source/blender/blenkernel/BKE_property.h b/source/blender/blenkernel/BKE_property.h index f1587790c4a..6af1deda727 100644 --- a/source/blender/blenkernel/BKE_property.h +++ b/source/blender/blenkernel/BKE_property.h @@ -41,7 +41,8 @@ struct bProperty *copy_property(struct bProperty *prop); void copy_properties(struct ListBase *lbn, struct ListBase *lbo); void init_property(struct bProperty *prop); struct bProperty *new_property(int type); -struct bProperty *get_property(struct Object *ob, char *name); +struct bProperty *get_ob_property(struct Object *ob, char *name); +void set_ob_property(struct Object *ob, struct bProperty *propc); int compare_property(struct bProperty *prop, char *str); void set_property(struct bProperty *prop, char *str); void add_property(struct bProperty *prop, char *str); diff --git a/source/blender/blenkernel/BKE_shrinkwrap.h b/source/blender/blenkernel/BKE_shrinkwrap.h new file mode 100644 index 00000000000..e8276238ff2 --- /dev/null +++ b/source/blender/blenkernel/BKE_shrinkwrap.h @@ -0,0 +1,146 @@ +/** + * BKE_shrinkwrap.h + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ +#ifndef BKE_SHRINKWRAP_H +#define BKE_SHRINKWRAP_H + +/* mesh util */ +//TODO: move this somewhere else +#include "BKE_customdata.h" +struct DerivedMesh; +struct Object; +struct DerivedMesh *object_get_derived_final(struct Object *ob, CustomDataMask dataMask); + + +/* SpaceTransform stuff */ +/* + * TODO: move this somewhere else + * + * this structs encapsulates all needed data to convert between 2 coordinate spaces + * (where conversion can be represented by a matrix multiplication) + * + * This is used to reduce the number of arguments to pass to functions that need to perform + * this kind of operation and make it easier for the coder, as he/she doenst needs to recode + * the matrix calculation. + * + * A SpaceTransform is initialized using: + * space_transform_setup( &data, ob1, ob2 ) + * + * After that the following calls can be used: + * space_transform_apply (&data, co); //converts a coordinate in ob1 coords space to the corresponding ob2 coords + * space_transform_invert(&data, co); //converts a coordinate in ob2 coords space to the corresponding ob1 coords + * + * //Same Concept as space_transform_apply and space_transform_invert, but no is normalized after conversion + * space_transform_apply_normal (&data, &no); + * space_transform_invert_normal(&data, &no); + * + */ +struct Object; + +typedef struct SpaceTransform +{ + float local2target[4][4]; + float target2local[4][4]; + +} SpaceTransform; + +void space_transform_from_matrixs(SpaceTransform *data, float local[][4], float target[][4]); +#define space_transform_setup(data, local, target) space_transform_from_matrixs(data, (local)->obmat, (target)->obmat) + +void space_transform_apply (const SpaceTransform *data, float *co); +void space_transform_invert(const SpaceTransform *data, float *co); + +void space_transform_apply_normal (const SpaceTransform *data, float *no); +void space_transform_invert_normal(const SpaceTransform *data, float *no); + +/* Shrinkwrap stuff */ +#include "BKE_bvhutils.h" + +/* + * Shrinkwrap is composed by a set of functions and options that define the type of shrink. + * + * 3 modes are available: + * - Nearest vertex + * - Nearest surface + * - Normal projection + * + * ShrinkwrapCalcData encapsulates all needed data for shrinkwrap functions. + * (So that you dont have to pass an enormous ammount of arguments to functions) + */ + +struct Object; +struct DerivedMesh; +struct ShrinkwrapModifierData; +struct BVHTree; + + +typedef struct ShrinkwrapCalcData +{ + ShrinkwrapModifierData *smd; //shrinkwrap modifier data + + struct Object *ob; //object we are applying shrinkwrap to + struct DerivedMesh *original; //mesh before shrinkwrap + + float (*vertexCos)[3]; //vertexs being shrinkwraped + int numVerts; + + struct DerivedMesh *target; //mesh we are shrinking to + SpaceTransform local2target; //transform to move bettwem local and target space + + float keepDist; //Distance to kept from target (units are in local space) + +} ShrinkwrapCalcData; + +void shrinkwrap_calc_nearest_vertex(ShrinkwrapCalcData *data); +void shrinkwrap_calc_normal_projection(ShrinkwrapCalcData *data); +void shrinkwrap_calc_nearest_surface_point(ShrinkwrapCalcData *data); + +void shrinkwrapModifier_deform(struct ShrinkwrapModifierData *smd, struct Object *ob, struct DerivedMesh *dm, float (*vertexCos)[3], int numVerts); + +/* + * This function casts a ray in the given BVHTree.. but it takes into consideration the space_transform, that is: + * + * if transf was configured with "space_transform_setup( &transf, ob1, ob2 )" + * then the input (vert, dir, BVHTreeRayHit) must be defined in ob1 coordinates space + * and the BVHTree must be built in ob2 coordinate space. + * + * Thus it provides an easy way to cast the same ray across several trees (where each tree was built on its own coords space) + */ +int normal_projection_project_vertex(char options, const float *vert, const float *dir, const SpaceTransform *transf, BVHTree *tree, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata); + +/* + * NULL initializers to local data + */ +#define NULL_ShrinkwrapCalcData {NULL, } +#define NULL_BVHTreeFromMesh {NULL, } +#define NULL_BVHTreeRayHit {NULL, } +#define NULL_BVHTreeNearest {0, } + + +#endif + diff --git a/source/blender/blenkernel/BKE_simple_deform.h b/source/blender/blenkernel/BKE_simple_deform.h new file mode 100644 index 00000000000..161871a64bc --- /dev/null +++ b/source/blender/blenkernel/BKE_simple_deform.h @@ -0,0 +1,39 @@ +/** + * BKE_shrinkwrap.h + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ +#ifndef BKE_SIMPLE_DEFORM_H +#define BKE_SIMPLE_DEFORM_H + +struct Object; +struct DerivedMesh; +struct SimpleDeformModifierData; + +void SimpleDeformModifier_do(SimpleDeformModifierData *smd, struct Object *ob, struct DerivedMesh *dm, float (*vertexCos)[3], int numVerts); + +#endif + diff --git a/source/blender/blenkernel/BKE_suggestions.h b/source/blender/blenkernel/BKE_suggestions.h new file mode 100644 index 00000000000..d58b8f58bf5 --- /dev/null +++ b/source/blender/blenkernel/BKE_suggestions.h @@ -0,0 +1,93 @@ +/** + * $Id: $ + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2008, Blender Foundation + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ +#ifndef BKE_SUGGESTIONS_H +#define BKE_SUGGESTIONS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* **************************************************************************** +Suggestions should be added in sorted order although a linear sorting method is +implemented. The list is then divided up based on the prefix provided by +update_suggestions: + +Example: + Prefix: ab + aaa <-- first + aab + aba <-- firstmatch + abb <-- lastmatch + baa + bab <-- last +**************************************************************************** */ + +struct Text; + +typedef struct SuggItem { + struct SuggItem *prev, *next; + char *name; + char type; +} SuggItem; + +typedef struct SuggList { + SuggItem *first, *last; + SuggItem *firstmatch, *lastmatch; + SuggItem *selected; + int top; +} SuggList; + +/* Free all text tool memory */ +void free_texttools(); + +/* Used to identify which Text object the current tools should appear against */ +void texttool_text_set_active(Text *text); +void texttool_text_clear(); +short texttool_text_is_active(Text *text); + +/* Suggestions */ +void texttool_suggest_add(const char *name, char type); +void texttool_suggest_prefix(const char *prefix); +void texttool_suggest_clear(); +SuggItem *texttool_suggest_first(); +SuggItem *texttool_suggest_last(); +void texttool_suggest_select(SuggItem *sel); +SuggItem *texttool_suggest_selected(); +int *texttool_suggest_top(); + +/* Documentation */ +void texttool_docs_show(const char *docs); +char *texttool_docs_get(); +void texttool_docs_clear(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index 6f891ab4887..002c804f17f 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -52,20 +52,24 @@ void txt_free_cut_buffer (void); char* txt_to_buf (struct Text *text); void txt_clean_text (struct Text *text); void txt_order_cursors (struct Text *text); -int txt_find_string (struct Text *text, char *findstr); +int txt_find_string (struct Text *text, char *findstr, int wrap); int txt_has_sel (struct Text *text); int txt_get_span (struct TextLine *from, struct TextLine *to); void txt_move_up (struct Text *text, short sel); void txt_move_down (struct Text *text, short sel); void txt_move_left (struct Text *text, short sel); void txt_move_right (struct Text *text, short sel); +void txt_jump_left (struct Text *text, short sel); +void txt_jump_right (struct Text *text, short sel); void txt_move_bof (struct Text *text, short sel); void txt_move_eof (struct Text *text, short sel); void txt_move_bol (struct Text *text, short sel); void txt_move_eol (struct Text *text, short sel); void txt_move_toline (struct Text *text, unsigned int line, short sel); +void txt_move_to (struct Text *text, unsigned int line, unsigned int ch, short sel); void txt_pop_sel (struct Text *text); void txt_delete_char (struct Text *text); +void txt_delete_word (struct Text *text); void txt_copy_sel (struct Text *text); void txt_sel_all (struct Text *text); void txt_sel_line (struct Text *text); @@ -80,8 +84,10 @@ void txt_do_undo (struct Text *text); void txt_do_redo (struct Text *text); void txt_split_curline (struct Text *text); void txt_backspace_char (struct Text *text); +void txt_backspace_word (struct Text *text); int txt_add_char (struct Text *text, char add); -void txt_find_panel (struct SpaceText *st, int again); +int txt_replace_char (struct Text *text, char add); +void find_and_replace (struct SpaceText *st, short mode); void run_python_script (struct SpaceText *st); int jumptoline_interactive (struct SpaceText *st); void txt_export_to_object (struct Text *text); @@ -94,6 +100,17 @@ int setcurr_tab (struct Text *text); void convert_tabs (struct SpaceText *st, int tab); void txt_copy_clipboard (struct Text *text); void txt_paste_clipboard (struct Text *text); + +void txt_add_marker (struct Text *text, struct TextLine *line, int start, int end, char color[4], int group, int flags); +short txt_clear_marker_region (struct Text *text, struct TextLine *line, int start, int end, int group, int flags); +short txt_clear_markers (struct Text *text, int group, int flags); +struct TextMarker *txt_find_marker (struct Text *text, struct TextLine *line, int curs, int group, int flags); +struct TextMarker *txt_find_marker_region (struct Text *text, struct TextLine *line, int start, int end, int group, int flags); +struct TextMarker *txt_prev_marker (struct Text *text, struct TextMarker *marker); +struct TextMarker *txt_next_marker (struct Text *text, struct TextMarker *marker); +struct TextMarker *txt_prev_marker_color (struct Text *text, struct TextMarker *marker); +struct TextMarker *txt_next_marker_color (struct Text *text, struct TextMarker *marker); + /* Undo opcodes */ /* Simple main cursor movement */ @@ -135,6 +152,14 @@ void txt_paste_clipboard (struct Text *text); #define UNDO_COMMENT 034 #define UNDO_UNCOMMENT 035 +/* Find and replace flags */ +#define TXT_FIND_WRAP 0x01 +#define TXT_FIND_ALLTEXTS 0x02 + +/* Marker flags */ +#define TMARK_TEMP 0x01 /* Remove on non-editing events, don't save */ +#define TMARK_EDITALL 0x02 /* Edit all markers of the same group as one */ + #ifdef __cplusplus } #endif diff --git a/source/blender/blenkernel/BKE_texture.h b/source/blender/blenkernel/BKE_texture.h index c162a04e055..cfcae3c44bc 100644 --- a/source/blender/blenkernel/BKE_texture.h +++ b/source/blender/blenkernel/BKE_texture.h @@ -53,6 +53,7 @@ void free_plugin_tex(struct PluginTex *pit); void init_colorband(struct ColorBand *coba, int rangetype); struct ColorBand *add_colorband(int rangetype); int do_colorband(struct ColorBand *coba, float in, float out[4]); +void colorband_table_RGBA(struct ColorBand *coba, float **array, int *size); void default_tex(struct Tex *tex); struct Tex *add_texture(char *name); diff --git a/source/blender/blenkernel/BKE_utildefines.h b/source/blender/blenkernel/BKE_utildefines.h index a96a3e10f40..9662d6fbff8 100644 --- a/source/blender/blenkernel/BKE_utildefines.h +++ b/source/blender/blenkernel/BKE_utildefines.h @@ -55,6 +55,8 @@ #define ELEM7(a, b, c, d, e, f, g, h) ( ELEM3(a, b, c, d) || ELEM4(a, e, f, g, h) ) #define ELEM8(a, b, c, d, e, f, g, h, i) ( ELEM4(a, b, c, d, e) || ELEM4(a, f, g, h, i) ) #define ELEM9(a, b, c, d, e, f, g, h, i, j) ( ELEM4(a, b, c, d, e) || ELEM5(a, f, g, h, i, j) ) +#define ELEM10(a, b, c, d, e, f, g, h, i, j, k) ( ELEM4(a, b, c, d, e) || ELEM6(a, f, g, h, i, j, k) ) +#define ELEM11(a, b, c, d, e, f, g, h, i, j, k, l) ( ELEM4(a, b, c, d, e) || ELEM7(a, f, g, h, i, j, k, l) ) /* shift around elements */ #define SHIFT3(type, a, b, c) { type tmp; tmp = a; a = c; c = b; b = tmp; } @@ -100,6 +102,8 @@ #define AVG2(x, y) ( 0.5 * ((x) + (y)) ) +#define FTOCHAR(val) (val<=0.0f)? 0 : ((val>(1.0f-0.5f/255.0f))? 255 : (char)((255.0f*val)+0.5f)) + #define VECCOPY(v1,v2) {*(v1)= *(v2); *(v1+1)= *(v2+1); *(v1+2)= *(v2+2);} #define VECCOPY2D(v1,v2) {*(v1)= *(v2); *(v1+1)= *(v2+1);} #define QUATCOPY(v1,v2) {*(v1)= *(v2); *(v1+1)= *(v2+1); *(v1+2)= *(v2+2); *(v1+3)= *(v2+3);} @@ -197,8 +201,8 @@ /* Warning-free macros for storing ints in pointers. Use these _only_ * for storing an int in a pointer, not a pointer in an int (64bit)! */ -#define SET_INT_IN_POINTER(i) ((void*)(long)(i)) -#define GET_INT_FROM_POINTER(i) ((int)(long)(i)) +#define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i)) +#define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i)) #endif diff --git a/source/blender/blenkernel/BKE_writeframeserver.h b/source/blender/blenkernel/BKE_writeframeserver.h index a6ece63bc56..4774906a2fa 100644 --- a/source/blender/blenkernel/BKE_writeframeserver.h +++ b/source/blender/blenkernel/BKE_writeframeserver.h @@ -37,7 +37,7 @@ struct RenderData; extern void start_frameserver(struct RenderData *rd, int rectx, int recty); extern void end_frameserver(void); extern void append_frameserver(int frame, int *pixels, int rectx, int recty); -extern int frameserver_loop(); +extern int frameserver_loop(void); #ifdef __cplusplus } diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt index 9088b410e27..c3cbc36a94f 100644 --- a/source/blender/blenkernel/CMakeLists.txt +++ b/source/blender/blenkernel/CMakeLists.txt @@ -32,7 +32,7 @@ SET(INC ../imbuf ../avi ../../../intern/elbeem/extern ../../../intern/opennl/extern ../../../intern/iksolver/extern ../blenloader ../quicktime ../../../intern/bmfont ../../../extern/bullet2/src - ../nodes + ../nodes ../../../extern/glew/include ../gpu ${SDL_INC} ${ZLIB_INC} ${PYTHON_INC} @@ -73,3 +73,6 @@ IF(WITH_INTERNATIONAL) ADD_DEFINITIONS(-DWITH_FREETYPE2) ENDIF(WITH_INTERNATIONAL) +IF(NOT WITH_ELBEEM) + ADD_DEFINITIONS(-DDISABLE_ELBEEM) +ENDIF(NOT WITH_ELBEEM) diff --git a/source/blender/blenkernel/SConscript b/source/blender/blenkernel/SConscript index 1bb98239a68..f891b307b2c 100644 --- a/source/blender/blenkernel/SConscript +++ b/source/blender/blenkernel/SConscript @@ -10,6 +10,7 @@ incs += ' #/intern/iksolver/extern ../blenloader ../quicktime' incs += ' #/extern/bullet2/src' incs += ' #/intern/bmfont' incs += ' #/intern/opennl/extern' +incs += ' ../gpu #/extern/glew/include' incs += ' ' + env['BF_PYTHON_INC'] incs += ' ' + env['BF_OPENGL_INC'] @@ -42,7 +43,8 @@ if env['WITH_BF_QUICKTIME'] == 1: defs += ' WITH_QUICKTIME' incs += ' ' + env['BF_QUICKTIME_INC'] -defs += ' WITH_CCGSUBSURF' +if env['BF_NO_ELBEEM'] == 1: + defs += ' DISABLE_ELBEEM' if env['WITH_BF_PLAYER']: SConscript(['bad_level_call_stubs/SConscript']) diff --git a/source/blender/blenkernel/bad_level_call_stubs/stubs.c b/source/blender/blenkernel/bad_level_call_stubs/stubs.c index 561d8d7c2a6..ae336d0fc26 100644 --- a/source/blender/blenkernel/bad_level_call_stubs/stubs.c +++ b/source/blender/blenkernel/bad_level_call_stubs/stubs.c @@ -60,7 +60,7 @@ struct EditFace; char *getIpoCurveName( struct IpoCurve * icu ); void insert_vert_icu(struct IpoCurve *icu, float x, float y, short fast); -struct IpoCurve *verify_ipocurve(struct ID *id, short a, char *b, char *d, int e); +struct IpoCurve *verify_ipocurve(struct ID *id, short a, char *b, char *d, int e, short f); void elbeemDebugOut(char *msg); void fluidsimSettingsFree(struct FluidsimSettings* sb); void fluidsimSettingsCopy(struct FluidsimSettings* sb); @@ -88,7 +88,7 @@ void insert_vert_icu(struct IpoCurve *icu, float x, float y, short fast) } -struct IpoCurve *verify_ipocurve(struct ID *id, short a, char *b, char *d, int e) +struct IpoCurve *verify_ipocurve(struct ID *id, short a, char *b, char *d, int e, short f) { return 0; } @@ -176,10 +176,6 @@ void free_editMesh(struct EditMesh *em){} void docenter_new(void){} int saveover(char *str){ return 0;} -/* image.c */ -#include "DNA_image_types.h" -void free_realtime_image(Image *ima){} // has to become a callback, opengl stuff - /* ipo.c */ void copy_view3d_lock(short val){} // was a hack, to make scene layer ipo's possible @@ -205,7 +201,7 @@ short pupmenu(char *instr){ return 0;} // will be general callback /* scene.c */ #include "DNA_sequence_types.h" void free_editing(struct Editing *ed){} // scenes and sequences problem... -void BPY_do_all_scripts (short int event){} +void BPY_do_all_scripts (short int event, short int anim){} /*editmesh_lib.c*/ void EM_select_face(struct EditFace *efa, int sel) {} @@ -220,30 +216,6 @@ struct EditEdge *findedgelist(struct EditVert *v1, struct EditVert *v2) { retur void countall(void) {} - -/* IKsolver stubs */ -#include "IK_solver.h" - -IK_Segment *IK_CreateSegment(int flag) { return 0; } -void IK_FreeSegment(IK_Segment *seg) {} - -void IK_SetParent(IK_Segment *seg, IK_Segment *parent) {} -void IK_SetTransform(IK_Segment *seg, float start[3], float rest_basis[][3], float basis[][3], float length) {} -void IK_GetBasisChange(IK_Segment *seg, float basis_change[][3]) {} -void IK_GetTranslationChange(IK_Segment *seg, float *translation_change) {}; -void IK_SetLimit(IK_Segment *seg, IK_SegmentAxis axis, float lower, float upper) {}; -void IK_SetStiffness(IK_Segment *seg, IK_SegmentAxis axis, float stiffness) {}; - -IK_Solver *IK_CreateSolver(IK_Segment *root) { return 0; } -void IK_FreeSolver(IK_Solver *solver) {}; - -void IK_SolverAddGoal(IK_Solver *solver, IK_Segment *tip, float goal[3], float weight) {} -void IK_SolverAddGoalOrientation(IK_Solver *solver, IK_Segment *tip, float goal[][3], float weight) {} -void IK_SolverSetPoleVectorConstraint(IK_Solver *solver, IK_Segment *tip, float goal[3], float polegoal[3], float poleangle, int getangle) {} -float IK_SolverGetPoleAngle(IK_Solver *solver) { return 0.0f; } - -int IK_Solve(IK_Solver *solver, float tolerance, int max_iterations) { return 0; } - /* exotic.c */ int BPY_call_importloader(char *name) { diff --git a/source/blender/blenkernel/intern/CCGSubSurf.c b/source/blender/blenkernel/intern/CCGSubSurf.c index 9dcb6b6e7fa..ce4458b4307 100644 --- a/source/blender/blenkernel/intern/CCGSubSurf.c +++ b/source/blender/blenkernel/intern/CCGSubSurf.c @@ -7,6 +7,8 @@ #include "CCGSubSurf.h" +#include "BLO_sys_types.h" // for intptr_t support + /***/ typedef unsigned char byte; @@ -35,7 +37,7 @@ typedef struct _EHash { #define EHASH_alloc(eh, nb) ((eh)->allocatorIFC.alloc((eh)->allocator, nb)) #define EHASH_free(eh, ptr) ((eh)->allocatorIFC.free((eh)->allocator, ptr)) -#define EHASH_hash(eh, item) (((unsigned long) (item))%((unsigned int) (eh)->curSize)) +#define EHASH_hash(eh, item) (((uintptr_t) (item))%((unsigned int) (eh)->curSize)) static EHash *_ehash_new(int estimatedNumEntries, CCGAllocatorIFC *allocatorIFC, CCGAllocatorHDL allocator) { EHash *eh = allocatorIFC->alloc(allocator, sizeof(*eh)); @@ -199,21 +201,21 @@ static CCGAllocatorIFC *_getStandardAllocatorIFC(void) { static int VertDataEqual(float *a, float *b) { return a[0]==b[0] && a[1]==b[1] && a[2]==b[2]; } -#define VertDataZero(av) { float *a = (float*) av; a[0] = a[1] = a[2] = 0.0f; } -#define VertDataCopy(av, bv) { float *a = (float*) av, *b = (float*) bv; a[0] =b[0]; a[1] =b[1]; a[2] =b[2]; } -#define VertDataAdd(av, bv) { float *a = (float*) av, *b = (float*) bv; a[0]+=b[0]; a[1]+=b[1]; a[2]+=b[2]; } -#define VertDataSub(av, bv) { float *a = (float*) av, *b = (float*) bv; a[0]-=b[0]; a[1]-=b[1]; a[2]-=b[2]; } -#define VertDataMulN(av, n) { float *a = (float*) av; a[0]*=n; a[1]*=n; a[2]*=n; } +#define VertDataZero(av) { float *_a = (float*) av; _a[0] = _a[1] = _a[2] = 0.0f; } +#define VertDataCopy(av, bv) { float *_a = (float*) av, *_b = (float*) bv; _a[0] =_b[0]; _a[1] =_b[1]; _a[2] =_b[2]; } +#define VertDataAdd(av, bv) { float *_a = (float*) av, *_b = (float*) bv; _a[0]+=_b[0]; _a[1]+=_b[1]; _a[2]+=_b[2]; } +#define VertDataSub(av, bv) { float *_a = (float*) av, *_b = (float*) bv; _a[0]-=_b[0]; _a[1]-=_b[1]; _a[2]-=_b[2]; } +#define VertDataMulN(av, n) { float *_a = (float*) av; _a[0]*=n; _a[1]*=n; _a[2]*=n; } #define VertDataAvg4(tv, av, bv, cv, dv) \ { \ - float *t = (float*) tv, *a = (float*) av, *b = (float*) bv, *c = (float*) cv, *d = (float*) dv; \ - t[0] = (a[0]+b[0]+c[0]+d[0])*.25; \ - t[1] = (a[1]+b[1]+c[1]+d[1])*.25; \ - t[2] = (a[2]+b[2]+c[2]+d[2])*.25; \ + float *_t = (float*) tv, *_a = (float*) av, *_b = (float*) bv, *_c = (float*) cv, *_d = (float*) dv; \ + _t[0] = (_a[0]+_b[0]+_c[0]+_d[0])*.25; \ + _t[1] = (_a[1]+_b[1]+_c[1]+_d[1])*.25; \ + _t[2] = (_a[2]+_b[2]+_c[2]+_d[2])*.25; \ } -#define NormZero(av) { float *a = (float*) av; a[0] = a[1] = a[2] = 0.0f; } -#define NormCopy(av, bv) { float *a = (float*) av, *b = (float*) bv; a[0] =b[0]; a[1] =b[1]; a[2] =b[2]; } -#define NormAdd(av, bv) { float *a = (float*) av, *b = (float*) bv; a[0]+=b[0]; a[1]+=b[1]; a[2]+=b[2]; } +#define NormZero(av) { float *_a = (float*) av; _a[0] = _a[1] = _a[2] = 0.0f; } +#define NormCopy(av, bv) { float *_a = (float*) av, *_b = (float*) bv; _a[0] =_b[0]; _a[1] =_b[1]; _a[2] =_b[2]; } +#define NormAdd(av, bv) { float *_a = (float*) av, *_b = (float*) bv; _a[0]+=_b[0]; _a[1]+=_b[1]; _a[2]+=_b[2]; } static int _edge_isBoundary(CCGEdge *e); @@ -328,7 +330,7 @@ struct _CCGSubSurf { /***/ -static CCGVert *_vert_new(CCGVertHDL vHDL, int levels, int dataSize, CCGSubSurf *ss) { +static CCGVert *_vert_new(CCGVertHDL vHDL, CCGSubSurf *ss) { CCGVert *v = CCGSUBSURF_alloc(ss, sizeof(CCGVert) + ss->meshIFC.vertDataSize * (ss->subdivLevels+1) + ss->meshIFC.vertUserSize); byte *userData; @@ -344,7 +346,7 @@ static CCGVert *_vert_new(CCGVertHDL vHDL, int levels, int dataSize, CCGSubSurf return v; } -static void _vert_remEdge(CCGVert *v, CCGEdge *e, CCGSubSurf *ss) { +static void _vert_remEdge(CCGVert *v, CCGEdge *e) { int i; for (i=0; i<v->numEdges; i++) { if (v->edges[i]==e) { @@ -353,7 +355,7 @@ static void _vert_remEdge(CCGVert *v, CCGEdge *e, CCGSubSurf *ss) { } } } -static void _vert_remFace(CCGVert *v, CCGFace *f, CCGSubSurf *ss) { +static void _vert_remFace(CCGVert *v, CCGFace *f) { int i; for (i=0; i<v->numFaces; i++) { if (v->faces[i]==f) { @@ -401,13 +403,13 @@ static void _vert_free(CCGVert *v, CCGSubSurf *ss) { CCGSUBSURF_free(ss, v); } -static int VERT_seam(CCGVert *v, CCGSubSurf *ss) { +static int VERT_seam(CCGVert *v) { return ((v->flags & Vert_eSeam) != 0); } /***/ -static CCGEdge *_edge_new(CCGEdgeHDL eHDL, CCGVert *v0, CCGVert *v1, float crease, int levels, int dataSize, CCGSubSurf *ss) { +static CCGEdge *_edge_new(CCGEdgeHDL eHDL, CCGVert *v0, CCGVert *v1, float crease, CCGSubSurf *ss) { CCGEdge *e = CCGSUBSURF_alloc(ss, sizeof(CCGEdge) + ss->meshIFC.vertDataSize *((ss->subdivLevels+1) + (1<<(ss->subdivLevels+1))-1) + ss->meshIFC.edgeUserSize); byte *userData; @@ -427,7 +429,7 @@ static CCGEdge *_edge_new(CCGEdgeHDL eHDL, CCGVert *v0, CCGVert *v1, float creas return e; } -static void _edge_remFace(CCGEdge *e, CCGFace *f, CCGSubSurf *ss) { +static void _edge_remFace(CCGEdge *e, CCGFace *f) { int i; for (i=0; i<e->numFaces; i++) { if (e->faces[i]==f) { @@ -476,8 +478,8 @@ static void _edge_free(CCGEdge *e, CCGSubSurf *ss) { CCGSUBSURF_free(ss, e); } static void _edge_unlinkMarkAndFree(CCGEdge *e, CCGSubSurf *ss) { - _vert_remEdge(e->v0, e, ss); - _vert_remEdge(e->v1, e, ss); + _vert_remEdge(e->v0, e); + _vert_remEdge(e->v1, e); e->v0->flags |= Vert_eEffected; e->v1->flags |= Vert_eEffected; _edge_free(e, ss); @@ -494,7 +496,7 @@ static float EDGE_getSharpness(CCGEdge *e, int lvl) { return e->crease - lvl; } -static CCGFace *_face_new(CCGFaceHDL fHDL, CCGVert **verts, CCGEdge **edges, int numVerts, int levels, int dataSize, CCGSubSurf *ss) { +static CCGFace *_face_new(CCGFaceHDL fHDL, CCGVert **verts, CCGEdge **edges, int numVerts, CCGSubSurf *ss) { int maxGridSize = 1 + (1<<(ss->subdivLevels-1)); CCGFace *f = CCGSUBSURF_alloc(ss, sizeof(CCGFace) + sizeof(CCGVert*)*numVerts + sizeof(CCGEdge*)*numVerts + ss->meshIFC.vertDataSize *(1 + numVerts*maxGridSize + numVerts*maxGridSize*maxGridSize) + ss->meshIFC.faceUserSize); byte *userData; @@ -608,8 +610,8 @@ static void _face_free(CCGFace *f, CCGSubSurf *ss) { static void _face_unlinkMarkAndFree(CCGFace *f, CCGSubSurf *ss) { int j; for (j=0; j<f->numVerts; j++) { - _vert_remFace(FACE_getVerts(f)[j], f, ss); - _edge_remFace(FACE_getEdges(f)[j], f, ss); + _vert_remFace(FACE_getVerts(f)[j], f); + _edge_remFace(FACE_getEdges(f)[j], f); FACE_getVerts(f)[j]->flags |= Vert_eEffected; } _face_free(f, ss); @@ -882,7 +884,7 @@ CCGError ccgSubSurf_syncVert(CCGSubSurf *ss, CCGVertHDL vHDL, void *vertData, in if (ss->syncState==eSyncState_Partial) { v = _ehash_lookupWithPrev(ss->vMap, vHDL, &prevp); if (!v) { - v = _vert_new(vHDL, ss->subdivLevels, ss->meshIFC.vertDataSize, ss); + v = _vert_new(vHDL, ss); VertDataCopy(_vert_getCo(v,0,ss->meshIFC.vertDataSize), vertData); _ehash_insert(ss->vMap, (EHEntry*) v); v->flags = Vert_eEffected|seamflag; @@ -911,7 +913,7 @@ CCGError ccgSubSurf_syncVert(CCGSubSurf *ss, CCGVertHDL vHDL, void *vertData, in v = _ehash_lookupWithPrev(ss->oldVMap, vHDL, &prevp); if (!v) { - v = _vert_new(vHDL, ss->subdivLevels, ss->meshIFC.vertDataSize, ss); + v = _vert_new(vHDL, ss); VertDataCopy(_vert_getCo(v,0,ss->meshIFC.vertDataSize), vertData); _ehash_insert(ss->vMap, (EHEntry*) v); v->flags = Vert_eEffected|seamflag; @@ -941,7 +943,7 @@ CCGError ccgSubSurf_syncEdge(CCGSubSurf *ss, CCGEdgeHDL eHDL, CCGVertHDL e_vHDL0 CCGVert *v0 = _ehash_lookup(ss->vMap, e_vHDL0); CCGVert *v1 = _ehash_lookup(ss->vMap, e_vHDL1); - eNew = _edge_new(eHDL, v0, v1, crease, ss->subdivLevels, ss->meshIFC.vertDataSize, ss); + eNew = _edge_new(eHDL, v0, v1, crease, ss); if (e) { *prevp = eNew; @@ -966,7 +968,7 @@ CCGError ccgSubSurf_syncEdge(CCGSubSurf *ss, CCGEdgeHDL eHDL, CCGVertHDL e_vHDL0 if (!e || e->v0->vHDL!=e_vHDL0 || e->v1->vHDL!=e_vHDL1|| e->crease!=crease) { CCGVert *v0 = _ehash_lookup(ss->vMap, e_vHDL0); CCGVert *v1 = _ehash_lookup(ss->vMap, e_vHDL1); - e = _edge_new(eHDL, v0, v1, crease, ss->subdivLevels, ss->meshIFC.vertDataSize, ss); + e = _edge_new(eHDL, v0, v1, crease, ss); _ehash_insert(ss->eMap, (EHEntry*) e); e->v0->flags |= Vert_eEffected; e->v1->flags |= Vert_eEffected; @@ -1015,7 +1017,7 @@ CCGError ccgSubSurf_syncFace(CCGSubSurf *ss, CCGFaceHDL fHDL, int numVerts, CCGV } if (!f || topologyChanged) { - fNew = _face_new(fHDL, ss->tempVerts, ss->tempEdges, numVerts, ss->subdivLevels, ss->meshIFC.vertDataSize, ss); + fNew = _face_new(fHDL, ss->tempVerts, ss->tempEdges, numVerts, ss); if (f) { ss->numGrids += numVerts - f->numVerts; @@ -1052,7 +1054,7 @@ CCGError ccgSubSurf_syncFace(CCGSubSurf *ss, CCGFaceHDL fHDL, int numVerts, CCGV if (!ss->tempEdges[k]) { if (ss->allowEdgeCreation) { - CCGEdge *e = ss->tempEdges[k] = _edge_new((CCGEdgeHDL) -1, ss->tempVerts[k], ss->tempVerts[(k+1)%numVerts], ss->defaultCreaseValue, ss->subdivLevels, ss->meshIFC.vertDataSize, ss); + CCGEdge *e = ss->tempEdges[k] = _edge_new((CCGEdgeHDL) -1, ss->tempVerts[k], ss->tempVerts[(k+1)%numVerts], ss->defaultCreaseValue, ss); _ehash_insert(ss->eMap, (EHEntry*) e); e->v0->flags |= Vert_eEffected; e->v1->flags |= Vert_eEffected; @@ -1073,7 +1075,7 @@ CCGError ccgSubSurf_syncFace(CCGSubSurf *ss, CCGFaceHDL fHDL, int numVerts, CCGV } if (!f || topologyChanged) { - f = _face_new(fHDL, ss->tempVerts, ss->tempEdges, numVerts, ss->subdivLevels, ss->meshIFC.vertDataSize, ss); + f = _face_new(fHDL, ss->tempVerts, ss->tempEdges, numVerts, ss); _ehash_insert(ss->fMap, (EHEntry*) f); ss->numGrids += numVerts; @@ -1226,7 +1228,7 @@ static void ccgSubSurf__sync(CCGSubSurf *ss) { void *nCo = VERT_getCo(v, nextLvl); int sharpCount = 0, allSharp = 1; float avgSharpness = 0.0; - int seam = VERT_seam(v, ss), seamEdges = 0; + int seam = VERT_seam(v), seamEdges = 0; for (i=0; i<v->numEdges; i++) { CCGEdge *e = v->edges[i]; @@ -1524,7 +1526,7 @@ static void ccgSubSurf__sync(CCGSubSurf *ss) { void *nCo = VERT_getCo(v, nextLvl); int sharpCount = 0, allSharp = 1; float avgSharpness = 0.0; - int seam = VERT_seam(v, ss), seamEdges = 0; + int seam = VERT_seam(v), seamEdges = 0; for (i=0; i<v->numEdges; i++) { CCGEdge *e = v->edges[i]; @@ -2069,7 +2071,7 @@ int ccgSubSurf_getGridLevelSize(CCGSubSurf *ss, int level) { /* Vert accessors */ -CCGVertHDL ccgSubSurf_getVertVertHandle(CCGSubSurf *ss, CCGVert *v) { +CCGVertHDL ccgSubSurf_getVertVertHandle(CCGVert *v) { return v->vHDL; } int ccgSubSurf_getVertAge(CCGSubSurf *ss, CCGVert *v) { @@ -2083,20 +2085,20 @@ int ccgSubSurf_getVertAge(CCGSubSurf *ss, CCGVert *v) { void *ccgSubSurf_getVertUserData(CCGSubSurf *ss, CCGVert *v) { return VERT_getLevelData(v) + ss->meshIFC.vertDataSize*(ss->subdivLevels+1); } -int ccgSubSurf_getVertNumFaces(CCGSubSurf *ss, CCGVert *v) { +int ccgSubSurf_getVertNumFaces(CCGVert *v) { return v->numFaces; } -CCGFace *ccgSubSurf_getVertFace(CCGSubSurf *ss, CCGVert *v, int index) { +CCGFace *ccgSubSurf_getVertFace(CCGVert *v, int index) { if (index<0 || index>=v->numFaces) { return NULL; } else { return v->faces[index]; } } -int ccgSubSurf_getVertNumEdges(CCGSubSurf *ss, CCGVert *v) { +int ccgSubSurf_getVertNumEdges(CCGVert *v) { return v->numEdges; } -CCGEdge *ccgSubSurf_getVertEdge(CCGSubSurf *ss, CCGVert *v, int index) { +CCGEdge *ccgSubSurf_getVertEdge(CCGVert *v, int index) { if (index<0 || index>=v->numEdges) { return NULL; } else { @@ -2116,7 +2118,7 @@ void *ccgSubSurf_getVertLevelData(CCGSubSurf *ss, CCGVert *v, int level) { /* Edge accessors */ -CCGEdgeHDL ccgSubSurf_getEdgeEdgeHandle(CCGSubSurf *ss, CCGEdge *e) { +CCGEdgeHDL ccgSubSurf_getEdgeEdgeHandle(CCGEdge *e) { return e->eHDL; } int ccgSubSurf_getEdgeAge(CCGSubSurf *ss, CCGEdge *e) { @@ -2130,20 +2132,20 @@ int ccgSubSurf_getEdgeAge(CCGSubSurf *ss, CCGEdge *e) { void *ccgSubSurf_getEdgeUserData(CCGSubSurf *ss, CCGEdge *e) { return EDGE_getLevelData(e) + ss->meshIFC.vertDataSize *((ss->subdivLevels+1) + (1<<(ss->subdivLevels+1))-1); } -int ccgSubSurf_getEdgeNumFaces(CCGSubSurf *ss, CCGEdge *e) { +int ccgSubSurf_getEdgeNumFaces(CCGEdge *e) { return e->numFaces; } -CCGFace *ccgSubSurf_getEdgeFace(CCGSubSurf *ss, CCGEdge *e, int index) { +CCGFace *ccgSubSurf_getEdgeFace(CCGEdge *e, int index) { if (index<0 || index>=e->numFaces) { return NULL; } else { return e->faces[index]; } } -CCGVert *ccgSubSurf_getEdgeVert0(CCGSubSurf *ss, CCGEdge *e) { +CCGVert *ccgSubSurf_getEdgeVert0(CCGEdge *e) { return e->v0; } -CCGVert *ccgSubSurf_getEdgeVert1(CCGSubSurf *ss, CCGEdge *e) { +CCGVert *ccgSubSurf_getEdgeVert1(CCGEdge *e) { return e->v1; } void *ccgSubSurf_getEdgeDataArray(CCGSubSurf *ss, CCGEdge *e) { @@ -2159,7 +2161,7 @@ void *ccgSubSurf_getEdgeLevelData(CCGSubSurf *ss, CCGEdge *e, int x, int level) return _edge_getCo(e, level, x, ss->meshIFC.vertDataSize); } } -float ccgSubSurf_getEdgeCrease(CCGSubSurf *ss, CCGEdge *e) { +float ccgSubSurf_getEdgeCrease(CCGEdge *e) { return e->crease; } @@ -2180,7 +2182,7 @@ void *ccgSubSurf_getFaceUserData(CCGSubSurf *ss, CCGFace *f) { int maxGridSize = 1 + (1<<(ss->subdivLevels-1)); return FACE_getCenterData(f) + ss->meshIFC.vertDataSize *(1 + f->numVerts*maxGridSize + f->numVerts*maxGridSize*maxGridSize); } -int ccgSubSurf_getFaceNumVerts(CCGSubSurf *ss, CCGFace *f) { +int ccgSubSurf_getFaceNumVerts(CCGFace *f) { return f->numVerts; } CCGVert *ccgSubSurf_getFaceVert(CCGSubSurf *ss, CCGFace *f, int index) { @@ -2197,7 +2199,7 @@ CCGEdge *ccgSubSurf_getFaceEdge(CCGSubSurf *ss, CCGFace *f, int index) { return FACE_getEdges(f)[index]; } } -int ccgSubSurf_getFaceEdgeIndex(CCGSubSurf *ss, CCGFace *f, CCGEdge *e) { +int ccgSubSurf_getFaceEdgeIndex(CCGFace *f, CCGEdge *e) { int i; for (i=0; i<f->numVerts; i++) @@ -2206,7 +2208,7 @@ int ccgSubSurf_getFaceEdgeIndex(CCGSubSurf *ss, CCGFace *f, CCGEdge *e) { return -1; } -void *ccgSubSurf_getFaceCenterData(CCGSubSurf *ss, CCGFace *f) { +void *ccgSubSurf_getFaceCenterData(CCGFace *f) { return FACE_getCenterData(f); } void *ccgSubSurf_getFaceGridEdgeDataArray(CCGSubSurf *ss, CCGFace *f, int gridIndex) { diff --git a/source/blender/blenkernel/intern/CCGSubSurf.h b/source/blender/blenkernel/intern/CCGSubSurf.h index 91f3ffab43b..fbd0aecc0a5 100644 --- a/source/blender/blenkernel/intern/CCGSubSurf.h +++ b/source/blender/blenkernel/intern/CCGSubSurf.h @@ -82,11 +82,11 @@ int ccgSubSurf_getGridSize (CCGSubSurf *ss); int ccgSubSurf_getGridLevelSize (CCGSubSurf *ss, int level); CCGVert* ccgSubSurf_getVert (CCGSubSurf *ss, CCGVertHDL v); -CCGVertHDL ccgSubSurf_getVertVertHandle (CCGSubSurf *ss, CCGVert *v); -int ccgSubSurf_getVertNumFaces (CCGSubSurf *ss, CCGVert *v); -CCGFace* ccgSubSurf_getVertFace (CCGSubSurf *ss, CCGVert *v, int index); -int ccgSubSurf_getVertNumEdges (CCGSubSurf *ss, CCGVert *v); -CCGEdge* ccgSubSurf_getVertEdge (CCGSubSurf *ss, CCGVert *v, int index); +CCGVertHDL ccgSubSurf_getVertVertHandle (CCGVert *v); +int ccgSubSurf_getVertNumFaces (CCGVert *v); +CCGFace* ccgSubSurf_getVertFace (CCGVert *v, int index); +int ccgSubSurf_getVertNumEdges (CCGVert *v); +CCGEdge* ccgSubSurf_getVertEdge (CCGVert *v, int index); int ccgSubSurf_getVertAge (CCGSubSurf *ss, CCGVert *v); void* ccgSubSurf_getVertUserData (CCGSubSurf *ss, CCGVert *v); @@ -94,12 +94,12 @@ void* ccgSubSurf_getVertData (CCGSubSurf *ss, CCGVert *v); void* ccgSubSurf_getVertLevelData (CCGSubSurf *ss, CCGVert *v, int level); CCGEdge* ccgSubSurf_getEdge (CCGSubSurf *ss, CCGEdgeHDL e); -CCGEdgeHDL ccgSubSurf_getEdgeEdgeHandle (CCGSubSurf *ss, CCGEdge *e); -int ccgSubSurf_getEdgeNumFaces (CCGSubSurf *ss, CCGEdge *e); -CCGFace* ccgSubSurf_getEdgeFace (CCGSubSurf *ss, CCGEdge *e, int index); -CCGVert* ccgSubSurf_getEdgeVert0 (CCGSubSurf *ss, CCGEdge *e); -CCGVert* ccgSubSurf_getEdgeVert1 (CCGSubSurf *ss, CCGEdge *e); -float ccgSubSurf_getEdgeCrease (CCGSubSurf *ss, CCGEdge *e); +CCGEdgeHDL ccgSubSurf_getEdgeEdgeHandle (CCGEdge *e); +int ccgSubSurf_getEdgeNumFaces (CCGEdge *e); +CCGFace* ccgSubSurf_getEdgeFace (CCGEdge *e, int index); +CCGVert* ccgSubSurf_getEdgeVert0 (CCGEdge *e); +CCGVert* ccgSubSurf_getEdgeVert1 (CCGEdge *e); +float ccgSubSurf_getEdgeCrease (CCGEdge *e); int ccgSubSurf_getEdgeAge (CCGSubSurf *ss, CCGEdge *e); void* ccgSubSurf_getEdgeUserData (CCGSubSurf *ss, CCGEdge *e); @@ -109,14 +109,14 @@ void* ccgSubSurf_getEdgeLevelData (CCGSubSurf *ss, CCGEdge *e, int x, int lev CCGFace* ccgSubSurf_getFace (CCGSubSurf *ss, CCGFaceHDL f); CCGFaceHDL ccgSubSurf_getFaceFaceHandle (CCGSubSurf *ss, CCGFace *f); -int ccgSubSurf_getFaceNumVerts (CCGSubSurf *ss, CCGFace *f); +int ccgSubSurf_getFaceNumVerts (CCGFace *f); CCGVert* ccgSubSurf_getFaceVert (CCGSubSurf *ss, CCGFace *f, int index); CCGEdge* ccgSubSurf_getFaceEdge (CCGSubSurf *ss, CCGFace *f, int index); -int ccgSubSurf_getFaceEdgeIndex (CCGSubSurf *ss, CCGFace *f, CCGEdge *e); +int ccgSubSurf_getFaceEdgeIndex (CCGFace *f, CCGEdge *e); int ccgSubSurf_getFaceAge (CCGSubSurf *ss, CCGFace *f); void* ccgSubSurf_getFaceUserData (CCGSubSurf *ss, CCGFace *f); -void* ccgSubSurf_getFaceCenterData (CCGSubSurf *ss, CCGFace *f); +void* ccgSubSurf_getFaceCenterData (CCGFace *f); void* ccgSubSurf_getFaceGridEdgeDataArray (CCGSubSurf *ss, CCGFace *f, int gridIndex); void* ccgSubSurf_getFaceGridEdgeData (CCGSubSurf *ss, CCGFace *f, int gridIndex, int x); void* ccgSubSurf_getFaceGridDataArray (CCGSubSurf *ss, CCGFace *f, int gridIndex); diff --git a/source/blender/blenkernel/intern/DerivedMesh.c b/source/blender/blenkernel/intern/DerivedMesh.c index 67cf89d5ee2..d4b17f97ad9 100644 --- a/source/blender/blenkernel/intern/DerivedMesh.c +++ b/source/blender/blenkernel/intern/DerivedMesh.c @@ -33,8 +33,6 @@ #include <config.h> #endif -#include <zlib.h> - #include "PIL_time.h" #include "MEM_guardedalloc.h" @@ -67,6 +65,7 @@ #include "BKE_deform.h" #include "BKE_displist.h" #include "BKE_effect.h" +#include "BKE_fluidsim.h" #include "BKE_global.h" #include "BKE_key.h" #include "BKE_material.h" @@ -79,6 +78,8 @@ #include "BKE_utildefines.h" #include "BKE_particle.h" +#include "BLO_sys_types.h" // for intptr_t support + #ifdef WITH_VERSE #include "BKE_verse.h" #endif @@ -86,15 +87,14 @@ #include "BIF_gl.h" #include "BIF_glutil.h" -// headers for fluidsim bobj meshes -#include <stdlib.h> -#include "LBM_fluidsim.h" -#include "elbeem.h" +#include "GPU_draw.h" +#include "GPU_extensions.h" +#include "GPU_material.h" /////////////////////////////////// /////////////////////////////////// -MVert *dm_getVertArray(DerivedMesh *dm) +static MVert *dm_getVertArray(DerivedMesh *dm) { MVert *mvert = CustomData_get_layer(&dm->vertData, CD_MVERT); @@ -108,7 +108,7 @@ MVert *dm_getVertArray(DerivedMesh *dm) return mvert; } -MEdge *dm_getEdgeArray(DerivedMesh *dm) +static MEdge *dm_getEdgeArray(DerivedMesh *dm) { MEdge *medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE); @@ -122,7 +122,7 @@ MEdge *dm_getEdgeArray(DerivedMesh *dm) return medge; } -MFace *dm_getFaceArray(DerivedMesh *dm) +static MFace *dm_getFaceArray(DerivedMesh *dm) { MFace *mface = CustomData_get_layer(&dm->faceData, CD_MFACE); @@ -136,7 +136,7 @@ MFace *dm_getFaceArray(DerivedMesh *dm) return mface; } -MVert *dm_dupVertArray(DerivedMesh *dm) +static MVert *dm_dupVertArray(DerivedMesh *dm) { MVert *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumVerts(dm), "dm_dupVertArray tmp"); @@ -146,7 +146,7 @@ MVert *dm_dupVertArray(DerivedMesh *dm) return tmp; } -MEdge *dm_dupEdgeArray(DerivedMesh *dm) +static MEdge *dm_dupEdgeArray(DerivedMesh *dm) { MEdge *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumEdges(dm), "dm_dupEdgeArray tmp"); @@ -156,7 +156,7 @@ MEdge *dm_dupEdgeArray(DerivedMesh *dm) return tmp; } -MFace *dm_dupFaceArray(DerivedMesh *dm) +static MFace *dm_dupFaceArray(DerivedMesh *dm) { MFace *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumFaces(dm), "dm_dupFaceArray tmp"); @@ -409,38 +409,20 @@ void DM_swap_face_data(DerivedMesh *dm, int index, int *corner_indices) CustomData_swap(&dm->faceData, index, corner_indices); } +/// + static DerivedMesh *getMeshDerivedMesh(Mesh *me, Object *ob, float (*vertCos)[3]) { DerivedMesh *dm = CDDM_from_mesh(me, ob); - int i, dofluidsim; - - dofluidsim = ((ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) && - (ob->fluidsimSettings->type & OB_FLUIDSIM_DOMAIN)&& - (ob->fluidsimSettings->meshSurface) && - (me->totvert == ((Mesh *)(ob->fluidsimSettings->meshSurface))->totvert)); - - if (vertCos && !dofluidsim) + + if(!dm) + return NULL; + + if (vertCos) CDDM_apply_vert_coords(dm, vertCos); CDDM_calc_normals(dm); - /* apply fluidsim normals */ - if (dofluidsim) { - // use normals from readBobjgz - // TODO? check for modifiers!? - MVert *fsvert = ob->fluidsimSettings->meshSurfNormals; - short (*normals)[3] = MEM_mallocN(sizeof(short)*3*me->totvert, "fluidsim nor"); - - for (i=0; i<me->totvert; i++) { - VECCOPY(normals[i], fsvert[i].no); - //mv->no[0]= 30000; mv->no[1]= mv->no[2]= 0; // DEBUG fixed test normals - } - - CDDM_apply_vert_normals(dm, normals); - - MEM_freeN(normals); - } - return dm; } @@ -479,7 +461,7 @@ static void emDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData, EditVert *eve; for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next) - eve->tmp.l = (long) i++; + eve->tmp.l = (intptr_t) i++; for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) func(userData, i, emdm->vertexCos[(int) eed->v1->tmp.l], emdm->vertexCos[(int) eed->v2->tmp.l]); } else { @@ -497,7 +479,7 @@ static void emDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *us EditVert *eve; for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next) - eve->tmp.l = (long) i++; + eve->tmp.l = (intptr_t) i++; glBegin(GL_LINES); for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) { @@ -532,7 +514,7 @@ static void emDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(vo EditVert *eve; for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next) - eve->tmp.l = (long) i++; + eve->tmp.l = (intptr_t) i++; glBegin(GL_LINES); for (i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) { @@ -619,7 +601,7 @@ static void emDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *use if (emdm->vertexCos) { for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next) - eve->tmp.l = (long) i++; + eve->tmp.l = (intptr_t) i++; } for(i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) { @@ -637,7 +619,7 @@ static void emDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *us EditVert *eve; for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next) - eve->tmp.l = (long) i++; + eve->tmp.l = (intptr_t) i++; for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) { int drawSmooth = (efa->flag & ME_SMOOTH); @@ -733,7 +715,7 @@ static void emDM_drawFacesTex_common(DerivedMesh *dm, EditVert *eve; for (i=0,eve=em->verts.first; eve; eve= eve->next) - eve->tmp.l = (long) i++; + eve->tmp.l = (intptr_t) i++; for (i=0,efa= em->faces.first; efa; i++,efa= efa->next) { MTFace *tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE); @@ -892,6 +874,162 @@ static void emDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void emDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData); } +static void emDM_drawMappedFacesGLSL(DerivedMesh *dm, + int (*setMaterial)(int, void *attribs), + int (*setDrawOptions)(void *userData, int index), void *userData) +{ + EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm; + EditMesh *em= emdm->em; + float (*vertexCos)[3]= emdm->vertexCos; + float (*vertexNos)[3]= emdm->vertexNos; + EditVert *eve; + EditFace *efa; + DMVertexAttribs attribs; + GPUVertexAttribs gattribs; + MTFace *tf; + int transp, new_transp, orig_transp, tfoffset; + int i, b, matnr, new_matnr, dodraw, layer; + + dodraw = 0; + matnr = -1; + + transp = GPU_get_material_blend_mode(); + orig_transp = transp; + layer = CustomData_get_layer_index(&em->fdata, CD_MTFACE); + tfoffset = (layer == -1)? -1: em->fdata.layers[layer].offset; + + memset(&attribs, 0, sizeof(attribs)); + + /* always use smooth shading even for flat faces, else vertex colors wont interpolate */ + glShadeModel(GL_SMOOTH); + + for (i=0,eve=em->verts.first; eve; eve= eve->next) + eve->tmp.l = (long) i++; + +#define PASSATTRIB(efa, eve, vert) { \ + if(attribs.totorco) { \ + float *orco = attribs.orco.array[eve->tmp.l]; \ + glVertexAttrib3fvARB(attribs.orco.glIndex, orco); \ + } \ + for(b = 0; b < attribs.tottface; b++) { \ + MTFace *_tf = (MTFace*)((char*)efa->data + attribs.tface[b].emOffset); \ + glVertexAttrib2fvARB(attribs.tface[b].glIndex, _tf->uv[vert]); \ + } \ + for(b = 0; b < attribs.totmcol; b++) { \ + MCol *cp = (MCol*)((char*)efa->data + attribs.mcol[b].emOffset); \ + GLubyte col[4]; \ + col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a; \ + glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col); \ + } \ + if(attribs.tottang) { \ + float *tang = attribs.tang.array[i*4 + vert]; \ + glVertexAttrib3fvARB(attribs.tang.glIndex, tang); \ + } \ +} + + for (i=0,efa= em->faces.first; efa; i++,efa= efa->next) { + int drawSmooth= (efa->flag & ME_SMOOTH); + + if(setDrawOptions && !setDrawOptions(userData, i)) + continue; + + new_matnr = efa->mat_nr + 1; + if(new_matnr != matnr) { + dodraw = setMaterial(matnr = new_matnr, &gattribs); + if(dodraw) + DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs); + } + + if(tfoffset != -1) { + tf = (MTFace*)((char*)efa->data)+tfoffset; + new_transp = tf->transp; + + if(new_transp != transp) { + if(new_transp == GPU_BLEND_SOLID && orig_transp != GPU_BLEND_SOLID) + GPU_set_material_blend_mode(orig_transp); + else + GPU_set_material_blend_mode(new_transp); + transp = new_transp; + } + } + + if(dodraw) { + glBegin(efa->v4?GL_QUADS:GL_TRIANGLES); + if (!drawSmooth) { + if(vertexCos) glNormal3fv(emdm->faceNos[i]); + else glNormal3fv(efa->n); + + PASSATTRIB(efa, efa->v1, 0); + if(vertexCos) glVertex3fv(vertexCos[(int) efa->v1->tmp.l]); + else glVertex3fv(efa->v1->co); + + PASSATTRIB(efa, efa->v2, 1); + if(vertexCos) glVertex3fv(vertexCos[(int) efa->v2->tmp.l]); + else glVertex3fv(efa->v2->co); + + PASSATTRIB(efa, efa->v3, 2); + if(vertexCos) glVertex3fv(vertexCos[(int) efa->v3->tmp.l]); + else glVertex3fv(efa->v3->co); + + if(efa->v4) { + PASSATTRIB(efa, efa->v4, 3); + if(vertexCos) glVertex3fv(vertexCos[(int) efa->v4->tmp.l]); + else glVertex3fv(efa->v4->co); + } + } else { + PASSATTRIB(efa, efa->v1, 0); + if(vertexCos) { + glNormal3fv(vertexNos[(int) efa->v1->tmp.l]); + glVertex3fv(vertexCos[(int) efa->v1->tmp.l]); + } + else { + glNormal3fv(efa->v1->no); + glVertex3fv(efa->v1->co); + } + + PASSATTRIB(efa, efa->v2, 1); + if(vertexCos) { + glNormal3fv(vertexNos[(int) efa->v2->tmp.l]); + glVertex3fv(vertexCos[(int) efa->v2->tmp.l]); + } + else { + glNormal3fv(efa->v2->no); + glVertex3fv(efa->v2->co); + } + + PASSATTRIB(efa, efa->v3, 2); + if(vertexCos) { + glNormal3fv(vertexNos[(int) efa->v3->tmp.l]); + glVertex3fv(vertexCos[(int) efa->v3->tmp.l]); + } + else { + glNormal3fv(efa->v3->no); + glVertex3fv(efa->v3->co); + } + + if(efa->v4) { + PASSATTRIB(efa, efa->v4, 3); + if(vertexCos) { + glNormal3fv(vertexNos[(int) efa->v4->tmp.l]); + glVertex3fv(vertexCos[(int) efa->v4->tmp.l]); + } + else { + glNormal3fv(efa->v4->no); + glVertex3fv(efa->v4->co); + } + } + } + glEnd(); + } + } +} + +static void emDM_drawFacesGLSL(DerivedMesh *dm, + int (*setMaterial)(int, void *attribs)) +{ + dm->drawMappedFacesGLSL(dm, setMaterial, NULL, NULL); +} + static void emDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3]) { EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm; @@ -931,7 +1069,7 @@ static int emDM_getNumFaces(DerivedMesh *dm) return BLI_countlist(&emdm->em->faces); } -void emDM_getVert(DerivedMesh *dm, int index, MVert *vert_r) +static void emDM_getVert(DerivedMesh *dm, int index, MVert *vert_r) { EditVert *ev = ((EditMeshDerivedMesh *)dm)->em->verts.first; int i; @@ -949,7 +1087,7 @@ void emDM_getVert(DerivedMesh *dm, int index, MVert *vert_r) vert_r->bweight = (unsigned char) (ev->bweight*255.0f); } -void emDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r) +static void emDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r) { EditMesh *em = ((EditMeshDerivedMesh *)dm)->em; EditEdge *ee = em->edges.first; @@ -984,7 +1122,7 @@ void emDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r) } } -void emDM_getFace(DerivedMesh *dm, int index, MFace *face_r) +static void emDM_getFace(DerivedMesh *dm, int index, MFace *face_r) { EditMesh *em = ((EditMeshDerivedMesh *)dm)->em; EditFace *ef = em->faces.first; @@ -1026,7 +1164,7 @@ void emDM_getFace(DerivedMesh *dm, int index, MFace *face_r) test_index_face(face_r, NULL, 0, ef->v4?4:3); } -void emDM_copyVertArray(DerivedMesh *dm, MVert *vert_r) +static void emDM_copyVertArray(DerivedMesh *dm, MVert *vert_r) { EditVert *ev = ((EditMeshDerivedMesh *)dm)->em->verts.first; @@ -1044,7 +1182,7 @@ void emDM_copyVertArray(DerivedMesh *dm, MVert *vert_r) } } -void emDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r) +static void emDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r) { EditMesh *em = ((EditMeshDerivedMesh *)dm)->em; EditEdge *ee = em->edges.first; @@ -1053,7 +1191,7 @@ void emDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r) /* store vertex indices in tmp union */ for(ev = em->verts.first, i = 0; ev; ev = ev->next, ++i) - ev->tmp.l = (long) i; + ev->tmp.l = (intptr_t) i; for( ; ee; ee = ee->next, ++edge_r) { edge_r->crease = (unsigned char) (ee->crease*255.0f); @@ -1072,7 +1210,7 @@ void emDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r) } } -void emDM_copyFaceArray(DerivedMesh *dm, MFace *face_r) +static void emDM_copyFaceArray(DerivedMesh *dm, MFace *face_r) { EditMesh *em = ((EditMeshDerivedMesh *)dm)->em; EditFace *ef = em->faces.first; @@ -1081,7 +1219,7 @@ void emDM_copyFaceArray(DerivedMesh *dm, MFace *face_r) /* store vertexes indices in tmp union */ for(ev = em->verts.first, i = 0; ev; ev = ev->next, ++i) - ev->tmp.l = (long) i; + ev->tmp.l = (intptr_t) i; for( ; ef; ef = ef->next, ++face_r) { face_r->mat_nr = ef->mat_nr; @@ -1097,6 +1235,43 @@ void emDM_copyFaceArray(DerivedMesh *dm, MFace *face_r) } } +static void *emDM_getFaceDataArray(DerivedMesh *dm, int type) +{ + EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm; + EditMesh *em= emdm->em; + EditFace *efa; + char *data, *emdata; + void *datalayer; + int index, offset, size; + + datalayer = DM_get_face_data_layer(dm, type); + if(datalayer) + return datalayer; + + /* layers are store per face for editmesh, we convert to a temporary + * data layer array in the derivedmesh when these are requested */ + if(type == CD_MTFACE || type == CD_MCOL) { + index = CustomData_get_layer_index(&em->fdata, type); + + if(index != -1) { + offset = em->fdata.layers[index].offset; + size = CustomData_sizeof(type); + + DM_add_face_layer(dm, type, CD_CALLOC, NULL); + index = CustomData_get_layer_index(&dm->faceData, type); + dm->faceData.layers[index].flag |= CD_FLAG_TEMPORARY; + + data = datalayer = DM_get_face_data_layer(dm, type); + for(efa=em->faces.first; efa; efa=efa->next, data+=size) { + emdata = CustomData_em_get(&em->fdata, efa->data, type); + memcpy(data, emdata, size); + } + } + } + + return datalayer; +} + static void emDM_release(DerivedMesh *dm) { EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm; @@ -1132,6 +1307,7 @@ static DerivedMesh *getEditMeshDerivedMesh(EditMesh *em, Object *ob, emdm->dm.copyVertArray = emDM_copyVertArray; emdm->dm.copyEdgeArray = emDM_copyEdgeArray; emdm->dm.copyFaceArray = emDM_copyFaceArray; + emdm->dm.getFaceDataArray = emDM_getFaceDataArray; emdm->dm.foreachMappedVert = emDM_foreachMappedVert; emdm->dm.foreachMappedEdge = emDM_foreachMappedEdge; @@ -1142,7 +1318,9 @@ static DerivedMesh *getEditMeshDerivedMesh(EditMesh *em, Object *ob, emdm->dm.drawMappedEdgesInterp = emDM_drawMappedEdgesInterp; emdm->dm.drawMappedFaces = emDM_drawMappedFaces; emdm->dm.drawMappedFacesTex = emDM_drawMappedFacesTex; + emdm->dm.drawMappedFacesGLSL = emDM_drawMappedFacesGLSL; emdm->dm.drawFacesTex = emDM_drawFacesTex; + emdm->dm.drawFacesGLSL = emDM_drawFacesGLSL; emdm->dm.drawUVEdges = emDM_drawUVEdges; emdm->dm.release = emDM_release; @@ -1168,7 +1346,7 @@ static DerivedMesh *getEditMeshDerivedMesh(EditMesh *em, Object *ob, int i; for (i=0,eve=em->verts.first; eve; eve= eve->next) - eve->tmp.l = (long) i++; + eve->tmp.l = (intptr_t) i++; emdm->vertexNos = MEM_callocN(sizeof(*emdm->vertexNos)*i, "emdm_vno"); emdm->faceNos = MEM_mallocN(sizeof(*emdm->faceNos)*totface, "emdm_vno"); @@ -1572,7 +1750,7 @@ static void vDM_drawUVEdges(DerivedMesh *dm) } /* draw all VerseFaces */ -static void vDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int)) +static void vDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) { VDerivedMesh *vdm = (VDerivedMesh*)dm; struct VerseFace *vface; @@ -1672,6 +1850,8 @@ static void vDM_drawMappedFacesTex( int (*setDrawParams)(void *userData, int index), void *userData) { + /* not supported yet */ + vDM_drawFacesTex(dm, NULL); } /**/ @@ -1813,19 +1993,23 @@ CustomDataMask get_viewedit_datamask() ScrArea *sa; /* check if we need tfaces & mcols due to face select or texture paint */ - if(FACESEL_PAINT_TEST || G.f & G_TEXTUREPAINT) { + if(FACESEL_PAINT_TEST || G.f & G_TEXTUREPAINT) mask |= CD_MASK_MTFACE | CD_MASK_MCOL; - } else { - /* check if we need tfaces & mcols due to view mode */ - for(sa = G.curscreen->areabase.first; sa; sa = sa->next) { - if(sa->spacetype == SPACE_VIEW3D) { - View3D *view = sa->spacedata.first; - if(view->drawtype == OB_SHADED) { - /* this includes normals for mesh_create_shadedColors */ - mask |= CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_NORMAL | CD_MASK_ORCO; - } - if((view->drawtype == OB_TEXTURE) || ((view->drawtype == OB_SOLID) && (view->flag2 & V3D_SOLID_TEX))) { - mask |= CD_MASK_MTFACE | CD_MASK_MCOL; + + /* check if we need tfaces & mcols due to view mode */ + for(sa = G.curscreen->areabase.first; sa; sa = sa->next) { + if(sa->spacetype == SPACE_VIEW3D) { + View3D *view = sa->spacedata.first; + if(view->drawtype == OB_SHADED) { + /* this includes normals for mesh_create_shadedColors */ + mask |= CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_NORMAL | CD_MASK_ORCO; + } + if((view->drawtype == OB_TEXTURE) || ((view->drawtype == OB_SOLID) && (view->flag2 & V3D_SOLID_TEX))) { + mask |= CD_MASK_MTFACE | CD_MASK_MCOL; + + if((G.fileflags & G_FILE_GAME_MAT) && + (G.fileflags & G_FILE_GAME_MAT_GLSL)) { + mask |= CD_MASK_ORCO; } } } @@ -1838,13 +2022,41 @@ CustomDataMask get_viewedit_datamask() return mask; } -static DerivedMesh *create_orco_dm(Object *ob, Mesh *me) +static float *get_editmesh_orco_verts(EditMesh *em) +{ + EditVert *eve; + float *orco; + int a, totvert; + + /* these may not really be the orco's, but it's only for preview. + * could be solver better once, but isn't simple */ + + totvert= 0; + for(eve=em->verts.first; eve; eve=eve->next) + totvert++; + + orco = MEM_mallocN(sizeof(float)*3*totvert, "EditMesh Orco"); + + for(a=0, eve=em->verts.first; eve; eve=eve->next, a+=3) + VECCOPY(orco+a, eve->co); + + return orco; +} + +static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, EditMesh *em) { DerivedMesh *dm; float (*orco)[3]; - dm= CDDM_from_mesh(me, ob); - orco= (float(*)[3])get_mesh_orco_verts(ob); + if(em) { + dm= CDDM_from_editmesh(em, me); + orco= (float(*)[3])get_editmesh_orco_verts(em); + } + else { + dm= CDDM_from_mesh(me, ob); + orco= (float(*)[3])get_mesh_orco_verts(ob); + } + CDDM_apply_vert_coords(dm, orco); CDDM_calc_normals(dm); MEM_freeN(orco); @@ -1852,7 +2064,7 @@ static DerivedMesh *create_orco_dm(Object *ob, Mesh *me) return dm; } -static void add_orco_dm(Object *ob, DerivedMesh *dm, DerivedMesh *orcodm) +static void add_orco_dm(Object *ob, EditMesh *em, DerivedMesh *dm, DerivedMesh *orcodm) { float (*orco)[3], (*layerorco)[3]; int totvert; @@ -1867,8 +2079,10 @@ static void add_orco_dm(Object *ob, DerivedMesh *dm, DerivedMesh *orcodm) else dm->getVertCos(dm, orco); } - else - orco= (float(*)[3])get_mesh_orco_verts(ob); + else { + if(em) orco= (float(*)[3])get_editmesh_orco_verts(em); + else orco= (float(*)[3])get_mesh_orco_verts(ob); + } transform_mesh_orco_verts(ob->data, orco, totvert, 0); @@ -1883,7 +2097,7 @@ static void add_orco_dm(Object *ob, DerivedMesh *dm, DerivedMesh *orcodm) static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], DerivedMesh **deform_r, DerivedMesh **final_r, int useRenderParams, int useDeform, - int needMapping, CustomDataMask dataMask) + int needMapping, CustomDataMask dataMask, int index) { Mesh *me = ob->data; ModifierData *firstmd, *md; @@ -1892,7 +2106,6 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], float (*deformedVerts)[3] = NULL; DerivedMesh *dm, *orcodm, *finaldm; int numVerts = me->totvert; - int fluidsimMeshUsed = 0; int required_mode; md = firstmd = modifiers_getVirtualModifierList(ob); @@ -1908,21 +2121,6 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], if(deform_r) *deform_r = NULL; *final_r = NULL; - /* replace original mesh by fluidsim surface mesh for fluidsim - * domain objects - */ - if((G.obedit!=ob) && !needMapping) { - if((ob->fluidsimFlag & OB_FLUIDSIM_ENABLE)) { - if(ob->fluidsimSettings->type & OB_FLUIDSIM_DOMAIN) { - loadFluidsimMesh(ob,useRenderParams); - fluidsimMeshUsed = 1; - /* might have changed... */ - me = ob->data; - numVerts = me->totvert; - } - } - } - if(useRenderParams) required_mode = eModifierMode_Render; else required_mode = eModifierMode_Realtime; @@ -1931,7 +2129,7 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], deformedVerts = mesh_getVertexCos(me, &numVerts); /* Apply all leading deforming modifiers */ - for(; md; md = md->next, curr = curr->next) { + for(;md; md = md->next, curr = curr->next) { ModifierTypeInfo *mti = modifierType_getInfo(md->type); if((md->mode & required_mode) != required_mode) continue; @@ -1945,6 +2143,10 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], } else { break; } + + /* grab modifiers until index i */ + if((index >= 0) && (modifiers_indexInObject(ob, md) >= index)) + break; } /* Result of all leading deforming modifiers is cached for @@ -1970,18 +2172,11 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], #endif } } else { - if(!fluidsimMeshUsed) { - /* default behaviour for meshes */ - if(inputVertexCos) - deformedVerts = inputVertexCos; - else - deformedVerts = mesh_getRefKeyCos(me, &numVerts); - } else { - /* the fluid sim mesh might have more vertices than the original - * one, so inputVertexCos shouldnt be used - */ - deformedVerts = mesh_getVertexCos(me, &numVerts); - } + /* default behaviour for meshes */ + if(inputVertexCos) + deformedVerts = inputVertexCos; + else + deformedVerts = mesh_getRefKeyCos(me, &numVerts); } @@ -1998,7 +2193,7 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], if(me->vnode) dm = derivedmesh_from_versemesh(me->vnode, deformedVerts); #endif - for(; md; md = md->next, curr = curr->next) { + for(;md; md = md->next, curr = curr->next) { ModifierTypeInfo *mti = modifierType_getInfo(md->type); if((md->mode & required_mode) != required_mode) continue; @@ -2014,7 +2209,7 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], if(dm && mti->requiredDataMask) { mask = mti->requiredDataMask(md); if(mask & CD_MASK_ORCO) - add_orco_dm(ob, dm, orcodm); + add_orco_dm(ob, NULL, dm, orcodm); } /* How to apply modifier depends on (a) what we already have as @@ -2067,7 +2262,7 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], mask= (CustomDataMask)curr->link; if(mask & CD_MASK_ORCO) { if(!orcodm) - orcodm= create_orco_dm(ob, me); + orcodm= create_orco_dm(ob, me, NULL); mask &= ~CD_MASK_ORCO; DM_set_only_copy(orcodm, mask); @@ -2104,6 +2299,10 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], } } } + + /* grab modifiers until index i */ + if((index >= 0) && (modifiers_indexInObject(ob, md) >= index)) + break; } for(md=firstmd; md; md=md->next) @@ -2144,10 +2343,10 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], /* add an orco layer if needed */ if(dataMask & CD_MASK_ORCO) { - add_orco_dm(ob, finaldm, orcodm); + add_orco_dm(ob, NULL, finaldm, orcodm); if(deform_r && *deform_r) - add_orco_dm(ob, *deform_r, NULL); + add_orco_dm(ob, NULL, *deform_r, NULL); } *final_r = finaldm; @@ -2159,9 +2358,6 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3], MEM_freeN(deformedVerts); BLI_linklist_free(datamasks, NULL); - - /* restore mesh in any case */ - if(fluidsimMeshUsed) ob->data = ob->fluidsimSettings->orgMesh; } static float (*editmesh_getVertexCos(EditMesh *em, int *numVerts_r))[3] @@ -2203,7 +2399,8 @@ static void editmesh_calc_modifiers(DerivedMesh **cage_r, EditMesh *em = G.editMesh; ModifierData *md; float (*deformedVerts)[3] = NULL; - DerivedMesh *dm; + CustomDataMask mask; + DerivedMesh *dm, *orcodm = NULL; int i, numVerts = 0, cageIndex = modifiers_getCageIndex(ob, NULL); LinkNode *datamasks, *curr; @@ -2228,6 +2425,13 @@ static void editmesh_calc_modifiers(DerivedMesh **cage_r, if(!editmesh_modifier_is_enabled(md, dm)) continue; + /* add an orco layer if needed by this modifier */ + if(dm && mti->requiredDataMask) { + mask = mti->requiredDataMask(md); + if(mask & CD_MASK_ORCO) + add_orco_dm(ob, em, dm, orcodm); + } + /* How to apply modifier depends on (a) what we already have as * a result of previous modifiers (could be a DerivedMesh or just * deformed vertices) and (b) what type the modifier is. @@ -2278,6 +2482,23 @@ static void editmesh_calc_modifiers(DerivedMesh **cage_r, } } + /* create an orco derivedmesh in parallel */ + mask= (CustomDataMask)curr->link; + if(mask & CD_MASK_ORCO) { + if(!orcodm) + orcodm= create_orco_dm(ob, ob->data, em); + + mask &= ~CD_MASK_ORCO; + DM_set_only_copy(orcodm, mask); + ndm = mti->applyModifierEM(md, ob, em, orcodm); + + if(ndm) { + /* if the modifier returned a new dm, release the old one */ + if(orcodm && orcodm != ndm) orcodm->release(orcodm); + orcodm = ndm; + } + } + /* set the DerivedMesh to only copy needed data */ DM_set_only_copy(dm, (CustomDataMask)curr->link); @@ -2336,6 +2557,13 @@ static void editmesh_calc_modifiers(DerivedMesh **cage_r, deformedVerts = NULL; } + /* add an orco layer if needed */ + if(dataMask & CD_MASK_ORCO) + add_orco_dm(ob, em, *final_r, orcodm); + + if(orcodm) + orcodm->release(orcodm); + if(deformedVerts) MEM_freeN(deformedVerts); } @@ -2481,13 +2709,13 @@ static void mesh_build_data(Object *ob, CustomDataMask dataMask) mesh_calc_modifiers(ob, NULL, &ob->derivedDeform, &ob->derivedFinal, 0, 1, - needMapping, dataMask); + needMapping, dataMask, -1); CustomData_free_layer_active(&me->fdata, CD_MCOL, me->totface); } else { mesh_calc_modifiers(ob, NULL, &ob->derivedDeform, &ob->derivedFinal, G.rendering, 1, - needMapping, dataMask); + needMapping, dataMask, -1); } INIT_MINMAX(min, max); @@ -2630,7 +2858,7 @@ void multires_render_final(Object *ob, Mesh *me, DerivedMesh **dm, float *vert_c old->release(old); if(dataMask & CD_MASK_ORCO) - add_orco_dm(ob, *dm, NULL); + add_orco_dm(ob, NULL, *dm, NULL); /* Restore the original verts */ me->mr->newlvl= BLI_countlist(&me->mr->levels); @@ -2658,7 +2886,21 @@ DerivedMesh *mesh_create_derived_render(Object *ob, CustomDataMask dataMask) int orig_lvl= 0; vert_copy= multires_render_pin(ob, me, &orig_lvl); - mesh_calc_modifiers(ob, NULL, NULL, &final, 1, 1, 0, dataMask); + mesh_calc_modifiers(ob, NULL, NULL, &final, 1, 1, 0, dataMask, -1); + multires_render_final(ob, me, &final, vert_copy, orig_lvl, dataMask); + + return final; +} + +DerivedMesh *mesh_create_derived_index_render(Object *ob, CustomDataMask dataMask, int index) +{ + DerivedMesh *final; + Mesh *me= get_mesh(ob); + float *vert_copy= NULL; + int orig_lvl= 0; + + vert_copy= multires_render_pin(ob, me, &orig_lvl); + mesh_calc_modifiers(ob, NULL, NULL, &final, 1, 1, 0, dataMask, index); multires_render_final(ob, me, &final, vert_copy, orig_lvl, dataMask); return final; @@ -2668,7 +2910,7 @@ DerivedMesh *mesh_create_derived_view(Object *ob, CustomDataMask dataMask) { DerivedMesh *final; - mesh_calc_modifiers(ob, NULL, NULL, &final, 0, 1, 0, dataMask); + mesh_calc_modifiers(ob, NULL, NULL, &final, 0, 1, 0, dataMask, -1); return final; } @@ -2678,7 +2920,7 @@ DerivedMesh *mesh_create_derived_no_deform(Object *ob, float (*vertCos)[3], { DerivedMesh *final; - mesh_calc_modifiers(ob, vertCos, NULL, &final, 0, 0, 0, dataMask); + mesh_calc_modifiers(ob, vertCos, NULL, &final, 0, 0, 0, dataMask, -1); return final; } @@ -2693,7 +2935,7 @@ DerivedMesh *mesh_create_derived_no_deform_render(Object *ob, int orig_lvl= 0; vert_copy= multires_render_pin(ob, me, &orig_lvl); - mesh_calc_modifiers(ob, vertCos, NULL, &final, 1, 0, 0, dataMask); + mesh_calc_modifiers(ob, vertCos, NULL, &final, 1, 0, 0, dataMask, -1); multires_render_final(ob, me, &final, vert_copy, orig_lvl, dataMask); return final; @@ -2845,6 +3087,8 @@ int editmesh_get_first_deform_matrices(float (**deformmats)[3][3], float (**defo return numleft; } +/* ******************* GLSL ******************** */ + void DM_add_tangent_layer(DerivedMesh *dm) { /* mesh vars */ @@ -2965,583 +3209,89 @@ void DM_add_tangent_layer(DerivedMesh *dm) MEM_freeN(vtangents); } - -/* ************************* fluidsim bobj file handling **************************** */ - -#ifndef DISABLE_ELBEEM - -#ifdef WIN32 -#ifndef snprintf -#define snprintf _snprintf -#endif -#endif - -/* write .bobj.gz file for a mesh object */ -void writeBobjgz(char *filename, struct Object *ob, int useGlobalCoords, int append, float time) +void DM_vertex_attributes_from_gpu(DerivedMesh *dm, GPUVertexAttribs *gattribs, DMVertexAttribs *attribs) { - char debugStrBuffer[256]; - int wri,i,j,totvert,totface; - float wrf; - gzFile gzf; - DerivedMesh *dm; - float vec[3]; - float rotmat[3][3]; - MVert *mvert; - MFace *mface; - //if(append)return; // DEBUG - - if(!ob->data || (ob->type!=OB_MESH)) { - snprintf(debugStrBuffer,256,"Writing GZ_BOBJ Invalid object %s ...\n", ob->id.name); - elbeemDebugOut(debugStrBuffer); - return; - } - if((ob->size[0]<0.0) || (ob->size[0]<0.0) || (ob->size[0]<0.0) ) { - snprintf(debugStrBuffer,256,"\nfluidSim::writeBobjgz:: Warning object %s has negative scaling - check triangle ordering...?\n\n", ob->id.name); - elbeemDebugOut(debugStrBuffer); - } + CustomData *vdata, *fdata, *tfdata = NULL; + int a, b, layer; - snprintf(debugStrBuffer,256,"Writing GZ_BOBJ '%s' ... ",filename); elbeemDebugOut(debugStrBuffer); - if(append) gzf = gzopen(filename, "a+b9"); - else gzf = gzopen(filename, "wb9"); - if (!gzf) { - snprintf(debugStrBuffer,256,"writeBobjgz::error - Unable to open file for writing '%s'\n", filename); - elbeemDebugOut(debugStrBuffer); - return; - } - - dm = mesh_create_derived_render(ob, CD_MASK_BAREMESH); - //dm = mesh_create_derived_no_deform(ob,NULL); - - mvert = dm->getVertArray(dm); - mface = dm->getFaceArray(dm); - totvert = dm->getNumVerts(dm); - totface = dm->getNumFaces(dm); - - // write time value for appended anim mesh - if(append) { - gzwrite(gzf, &time, sizeof(time)); - } - - // continue with verts/norms - if(sizeof(wri)!=4) { snprintf(debugStrBuffer,256,"Writing GZ_BOBJ, Invalid int size %d...\n", wri); elbeemDebugOut(debugStrBuffer); return; } // paranoia check - wri = dm->getNumVerts(dm); - mvert = dm->getVertArray(dm); - gzwrite(gzf, &wri, sizeof(wri)); - for(i=0; i<wri;i++) { - VECCOPY(vec, mvert[i].co); - if(useGlobalCoords) { Mat4MulVecfl(ob->obmat, vec); } - for(j=0; j<3; j++) { - wrf = vec[j]; - gzwrite(gzf, &wrf, sizeof( wrf )); - } - } + /* From the layers requested by the GLSL shader, figure out which ones are + * actually available for this derivedmesh, and retrieve the pointers */ - // should be the same as Vertices.size - wri = totvert; - gzwrite(gzf, &wri, sizeof(wri)); - EulToMat3(ob->rot, rotmat); - for(i=0; i<wri;i++) { - VECCOPY(vec, mvert[i].no); - Normalize(vec); - if(useGlobalCoords) { Mat3MulVecfl(rotmat, vec); } - for(j=0; j<3; j++) { - wrf = vec[j]; - gzwrite(gzf, &wrf, sizeof( wrf )); - } - } + memset(attribs, 0, sizeof(DMVertexAttribs)); - // append only writes verts&norms - if(!append) { - //float side1[3],side2[3],norm1[3],norm2[3]; - //float inpf; - - // compute no. of triangles - wri = 0; - for(i=0; i<totface; i++) { - wri++; - if(mface[i].v4) { wri++; } - } - gzwrite(gzf, &wri, sizeof(wri)); - for(i=0; i<totface; i++) { - - int face[4]; - face[0] = mface[i].v1; - face[1] = mface[i].v2; - face[2] = mface[i].v3; - face[3] = mface[i].v4; - //snprintf(debugStrBuffer,256,"F %s %d = %d,%d,%d,%d \n",ob->id.name, i, face[0],face[1],face[2],face[3] ); elbeemDebugOut(debugStrBuffer); - //VecSubf(side1, mvert[face[1]].co,mvert[face[0]].co); - //VecSubf(side2, mvert[face[2]].co,mvert[face[0]].co); - //Crossf(norm1,side1,side2); - gzwrite(gzf, &(face[0]), sizeof( face[0] )); - gzwrite(gzf, &(face[1]), sizeof( face[1] )); - gzwrite(gzf, &(face[2]), sizeof( face[2] )); - if(face[3]) { - //VecSubf(side1, mvert[face[2]].co,mvert[face[0]].co); - //VecSubf(side2, mvert[face[3]].co,mvert[face[0]].co); - //Crossf(norm2,side1,side2); - //inpf = Inpf(norm1,norm2); - //if(inpf>0.) { - gzwrite(gzf, &(face[0]), sizeof( face[0] )); - gzwrite(gzf, &(face[2]), sizeof( face[2] )); - gzwrite(gzf, &(face[3]), sizeof( face[3] )); - //} else { - //gzwrite(gzf, &(face[0]), sizeof( face[0] )); - //gzwrite(gzf, &(face[3]), sizeof( face[3] )); - //gzwrite(gzf, &(face[2]), sizeof( face[2] )); - //} - } // quad - } - } + vdata = &dm->vertData; + fdata = &dm->faceData; - snprintf(debugStrBuffer,256,"Done. #Vertices: %d, #Triangles: %d\n", totvert, totface ); - elbeemDebugOut(debugStrBuffer); - - gzclose( gzf ); - dm->release(dm); -} - -void initElbeemMesh(struct Object *ob, - int *numVertices, float **vertices, - int *numTriangles, int **triangles, - int useGlobalCoords) -{ - DerivedMesh *dm = NULL; - MVert *mvert; - MFace *mface; - int countTris=0, i, totvert, totface; - float *verts; - int *tris; - - dm = mesh_create_derived_render(ob, CD_MASK_BAREMESH); - //dm = mesh_create_derived_no_deform(ob,NULL); - - mvert = dm->getVertArray(dm); - mface = dm->getFaceArray(dm); - totvert = dm->getNumVerts(dm); - totface = dm->getNumFaces(dm); - - *numVertices = totvert; - verts = MEM_callocN( totvert*3*sizeof(float), "elbeemmesh_vertices"); - for(i=0; i<totvert; i++) { - VECCOPY( &verts[i*3], mvert[i].co); - if(useGlobalCoords) { Mat4MulVecfl(ob->obmat, &verts[i*3]); } - } - *vertices = verts; - - for(i=0; i<totface; i++) { - countTris++; - if(mface[i].v4) { countTris++; } - } - *numTriangles = countTris; - tris = MEM_callocN( countTris*3*sizeof(int), "elbeemmesh_triangles"); - countTris = 0; - for(i=0; i<totface; i++) { - int face[4]; - face[0] = mface[i].v1; - face[1] = mface[i].v2; - face[2] = mface[i].v3; - face[3] = mface[i].v4; - - tris[countTris*3+0] = face[0]; - tris[countTris*3+1] = face[1]; - tris[countTris*3+2] = face[2]; - countTris++; - if(face[3]) { - tris[countTris*3+0] = face[0]; - tris[countTris*3+1] = face[2]; - tris[countTris*3+2] = face[3]; - countTris++; - } - } - *triangles = tris; - - dm->release(dm); -} + /* ugly hack, editmesh derivedmesh doesn't copy face data, this way we + * can use offsets instead */ + if(dm->release == emDM_release) + tfdata = &((EditMeshDerivedMesh*)dm)->em->fdata; + else + tfdata = fdata; + + /* add a tangent layer if necessary */ + for(b = 0; b < gattribs->totlayer; b++) + if(gattribs->layer[b].type == CD_TANGENT) + if(CustomData_get_layer_index(fdata, CD_TANGENT) == -1) + DM_add_tangent_layer(dm); + + for(b = 0; b < gattribs->totlayer; b++) { + if(gattribs->layer[b].type == CD_MTFACE) { + /* uv coordinates */ + if(gattribs->layer[b].name[0]) + layer = CustomData_get_named_layer_index(tfdata, CD_MTFACE, + gattribs->layer[b].name); + else + layer = CustomData_get_active_layer_index(tfdata, CD_MTFACE); -/* read .bobj.gz file into a fluidsimDerivedMesh struct */ -Mesh* readBobjgz(char *filename, Mesh *orgmesh, float* bbstart, float *bbsize) //, fluidsimDerivedMesh *fsdm) -{ - int wri,i,j; - char debugStrBuffer[256]; - float wrf; - Mesh *newmesh; - const int debugBobjRead = 1; - // init data from old mesh (materials,flags) - MFace *origMFace = &((MFace*) orgmesh->mface)[0]; - int mat_nr = -1; - int flag = -1; - MFace *fsface = NULL; - int gotBytes; - gzFile gzf; - - if(!orgmesh) return NULL; - if(!origMFace) return NULL; - mat_nr = origMFace->mat_nr; - flag = origMFace->flag; - - // similar to copy_mesh - newmesh = MEM_dupallocN(orgmesh); - newmesh->mat= orgmesh->mat; - - newmesh->mvert= NULL; - newmesh->medge= NULL; - newmesh->mface= NULL; - newmesh->mtface= NULL; - - newmesh->dvert = NULL; - - newmesh->mcol= NULL; - newmesh->msticky= NULL; - newmesh->texcomesh= NULL; - memset(&newmesh->vdata, 0, sizeof(newmesh->vdata)); - memset(&newmesh->edata, 0, sizeof(newmesh->edata)); - memset(&newmesh->fdata, 0, sizeof(newmesh->fdata)); - - newmesh->key= NULL; - newmesh->totface = 0; - newmesh->totvert = 0; - newmesh->totedge = 0; - newmesh->medge = NULL; - - - snprintf(debugStrBuffer,256,"Reading '%s' GZ_BOBJ... ",filename); elbeemDebugOut(debugStrBuffer); - gzf = gzopen(filename, "rb"); - // gzf = fopen(filename, "rb"); - // debug: fread(b,c,1,a) = gzread(a,b,c) - if (!gzf) { - //snprintf(debugStrBuffer,256,"readBobjgz::error - Unable to open file for reading '%s'\n", filename); // DEBUG - MEM_freeN(newmesh); - return NULL; - } + if(layer != -1) { + a = attribs->tottface++; - //if(sizeof(wri)!=4) { snprintf(debugStrBuffer,256,"Reading GZ_BOBJ, Invalid int size %d...\n", wri); return NULL; } // paranoia check - gotBytes = gzread(gzf, &wri, sizeof(wri)); - newmesh->totvert = wri; - newmesh->mvert = CustomData_add_layer(&newmesh->vdata, CD_MVERT, CD_CALLOC, NULL, newmesh->totvert); - if(debugBobjRead){ snprintf(debugStrBuffer,256,"#vertices %d ", newmesh->totvert); elbeemDebugOut(debugStrBuffer); } //DEBUG - for(i=0; i<newmesh->totvert;i++) { - //if(debugBobjRead) snprintf(debugStrBuffer,256,"V %d = ",i); - for(j=0; j<3; j++) { - gotBytes = gzread(gzf, &wrf, sizeof( wrf )); - newmesh->mvert[i].co[j] = wrf; - //if(debugBobjRead) snprintf(debugStrBuffer,256,"%25.20f ", wrf); + attribs->tface[a].array = tfdata->layers[layer].data; + attribs->tface[a].emOffset = tfdata->layers[layer].offset; + attribs->tface[a].glIndex = gattribs->layer[b].glindex; + } } - //if(debugBobjRead) snprintf(debugStrBuffer,256,"\n"); - } + else if(gattribs->layer[b].type == CD_MCOL) { + /* vertex colors */ + if(gattribs->layer[b].name[0]) + layer = CustomData_get_named_layer_index(tfdata, CD_MCOL, + gattribs->layer[b].name); + else + layer = CustomData_get_active_layer_index(tfdata, CD_MCOL); - // should be the same as Vertices.size - gotBytes = gzread(gzf, &wri, sizeof(wri)); - if(wri != newmesh->totvert) { - // complain #vertices has to be equal to #normals, reset&abort - CustomData_free_layer_active(&newmesh->vdata, CD_MVERT, newmesh->totvert); - MEM_freeN(newmesh); - snprintf(debugStrBuffer,256,"Reading GZ_BOBJ, #normals=%d, #vertices=%d, aborting...\n", wri,newmesh->totvert ); - return NULL; - } - for(i=0; i<newmesh->totvert;i++) { - for(j=0; j<3; j++) { - gotBytes = gzread(gzf, &wrf, sizeof( wrf )); - newmesh->mvert[i].no[j] = (short)(wrf*32767.0f); - //newmesh->mvert[i].no[j] = 0.5; // DEBUG tst - } - //fprintf(stderr," DEBDPCN nm%d, %d = %d,%d,%d \n", - //(int)(newmesh->mvert), i, newmesh->mvert[i].no[0], newmesh->mvert[i].no[1], newmesh->mvert[i].no[2]); - } - //fprintf(stderr," DPCN 0 = %d,%d,%d \n", newmesh->mvert[0].no[0], newmesh->mvert[0].no[1], newmesh->mvert[0].no[2]); + if(layer != -1) { + a = attribs->totmcol++; - - /* compute no. of triangles */ - gotBytes = gzread(gzf, &wri, sizeof(wri)); - newmesh->totface = wri; - newmesh->mface = CustomData_add_layer(&newmesh->fdata, CD_MFACE, CD_CALLOC, NULL, newmesh->totface); - if(debugBobjRead){ snprintf(debugStrBuffer,256,"#faces %d ", newmesh->totface); elbeemDebugOut(debugStrBuffer); } //DEBUG - fsface = newmesh->mface; - for(i=0; i<newmesh->totface; i++) { - int face[4]; - - gotBytes = gzread(gzf, &(face[0]), sizeof( face[0] )); - gotBytes = gzread(gzf, &(face[1]), sizeof( face[1] )); - gotBytes = gzread(gzf, &(face[2]), sizeof( face[2] )); - face[3] = 0; - - fsface[i].v1 = face[0]; - fsface[i].v2 = face[1]; - fsface[i].v3 = face[2]; - fsface[i].v4 = face[3]; - } - - // correct triangles with v3==0 for blender, cycle verts - for(i=0; i<newmesh->totface; i++) { - if(!fsface[i].v3) { - int temp = fsface[i].v1; - fsface[i].v1 = fsface[i].v2; - fsface[i].v2 = fsface[i].v3; - fsface[i].v3 = temp; - } - } - - gzclose( gzf ); - for(i=0;i<newmesh->totface;i++) { - fsface[i].mat_nr = mat_nr; - fsface[i].flag = flag; - fsface[i].edcode = ME_V1V2 | ME_V2V3 | ME_V3V1; - //snprintf(debugStrBuffer,256,"%d : %d,%d,%d\n", i,fsface[i].mat_nr, fsface[i].flag, fsface[i].edcode ); - } - - snprintf(debugStrBuffer,256," (%d,%d) done\n", newmesh->totvert,newmesh->totface); elbeemDebugOut(debugStrBuffer); //DEBUG - return newmesh; -} - -/* read zipped fluidsim velocities into the co's of the fluidsimsettings normals struct */ -void readVelgz(char *filename, Object *srcob) -{ - char debugStrBuffer[256]; - int wri, i, j; - float wrf; - gzFile gzf; - MVert *vverts = srcob->fluidsimSettings->meshSurfNormals; - int len = strlen(filename); - Mesh *mesh = srcob->data; - // mesh and vverts have to be valid from loading... - - // clean up in any case - for(i=0; i<mesh->totvert;i++) { - for(j=0; j<3; j++) { - vverts[i].co[j] = 0.; - } - } - if(srcob->fluidsimSettings->domainNovecgen>0) return; - - if(len<7) { - //printf("readVelgz Eror: invalid filename '%s'\n",filename); // DEBUG - return; - } - - // .bobj.gz , correct filename - // 87654321 - filename[len-6] = 'v'; - filename[len-5] = 'e'; - filename[len-4] = 'l'; - - snprintf(debugStrBuffer,256,"Reading '%s' GZ_VEL... ",filename); elbeemDebugOut(debugStrBuffer); - gzf = gzopen(filename, "rb"); - if (!gzf) { - //printf("readVelgz Eror: unable to open file '%s'\n",filename); // DEBUG - return; - } - - gzread(gzf, &wri, sizeof( wri )); - if(wri != mesh->totvert) { - //printf("readVelgz Eror: invalid no. of velocities %d vs. %d aborting.\n" ,wri ,mesh->totvert ); // DEBUG - return; - } - - for(i=0; i<mesh->totvert;i++) { - for(j=0; j<3; j++) { - gzread(gzf, &wrf, sizeof( wrf )); - vverts[i].co[j] = wrf; + attribs->mcol[a].array = tfdata->layers[layer].data; + attribs->mcol[a].emOffset = tfdata->layers[layer].offset; + attribs->mcol[a].glIndex = gattribs->layer[b].glindex; + } } - //if(i<20) fprintf(stderr, "GZ_VELload %d = %f,%f,%f \n",i,vverts[i].co[0],vverts[i].co[1],vverts[i].co[2]); // DEBUG - } + else if(gattribs->layer[b].type == CD_TANGENT) { + /* tangents */ + layer = CustomData_get_layer_index(fdata, CD_TANGENT); - gzclose(gzf); -} - - -/* ***************************** fluidsim derived mesh ***************************** */ - -/* check which file to load, and replace old mesh of the object with it */ -/* this replacement is undone at the end of mesh_calc_modifiers */ -void loadFluidsimMesh(Object *srcob, int useRenderParams) -{ - Mesh *mesh = NULL; - float *bbStart = NULL, *bbSize = NULL; - float lastBB[3]; - int displaymode = 0; - int curFrame = G.scene->r.cfra - 1 /*G.scene->r.sfra*/; /* start with 0 at start frame */ - char targetDir[FILE_MAXFILE+FILE_MAXDIR], targetFile[FILE_MAXFILE+FILE_MAXDIR]; - char debugStrBuffer[256]; - //snprintf(debugStrBuffer,256,"loadFluidsimMesh call (obid '%s', rp %d)\n", srcob->id.name, useRenderParams); // debug + if(layer != -1) { + attribs->tottang = 1; - if((!srcob)||(!srcob->fluidsimSettings)) { - snprintf(debugStrBuffer,256,"DEBUG - Invalid loadFluidsimMesh call, rp %d, dm %d)\n", useRenderParams, displaymode); // debug - elbeemDebugOut(debugStrBuffer); // debug - return; - } - // make sure the original mesh data pointer is stored - if(!srcob->fluidsimSettings->orgMesh) { - srcob->fluidsimSettings->orgMesh = srcob->data; - } - - // free old mesh, if there is one (todo, check if it's still valid?) - if(srcob->fluidsimSettings->meshSurface) { - Mesh *freeFsMesh = srcob->fluidsimSettings->meshSurface; - - // similar to free_mesh(...) , but no things like unlink... - CustomData_free(&freeFsMesh->vdata, freeFsMesh->totvert); - CustomData_free(&freeFsMesh->edata, freeFsMesh->totedge); - CustomData_free(&freeFsMesh->fdata, freeFsMesh->totface); - MEM_freeN(freeFsMesh); - - if(srcob->data == srcob->fluidsimSettings->meshSurface) - srcob->data = srcob->fluidsimSettings->orgMesh; - srcob->fluidsimSettings->meshSurface = NULL; - - if(srcob->fluidsimSettings->meshSurfNormals) MEM_freeN(srcob->fluidsimSettings->meshSurfNormals); - srcob->fluidsimSettings->meshSurfNormals = NULL; - } - - // init bounding box - bbStart = srcob->fluidsimSettings->bbStart; - bbSize = srcob->fluidsimSettings->bbSize; - lastBB[0] = bbSize[0]; // TEST - lastBB[1] = bbSize[1]; - lastBB[2] = bbSize[2]; - fluidsimGetAxisAlignedBB(srcob->fluidsimSettings->orgMesh, srcob->obmat, bbStart, bbSize, &srcob->fluidsimSettings->meshBB); - // check free fsmesh... TODO - - if(!useRenderParams) { - displaymode = srcob->fluidsimSettings->guiDisplayMode; - } else { - displaymode = srcob->fluidsimSettings->renderDisplayMode; - } - - snprintf(debugStrBuffer,256,"loadFluidsimMesh call (obid '%s', rp %d, dm %d), curFra=%d, sFra=%d #=%d \n", - srcob->id.name, useRenderParams, displaymode, G.scene->r.cfra, G.scene->r.sfra, curFrame ); // debug - elbeemDebugOut(debugStrBuffer); // debug - - strncpy(targetDir, srcob->fluidsimSettings->surfdataPath, FILE_MAXDIR); - // use preview or final mesh? - if(displaymode==1) { - // just display original object - srcob->data = srcob->fluidsimSettings->orgMesh; - return; - } else if(displaymode==2) { - strcat(targetDir,"fluidsurface_preview_####"); - } else { // 3 - strcat(targetDir,"fluidsurface_final_####"); - } - BLI_convertstringcode(targetDir, G.sce); - BLI_convertstringframe(targetDir, curFrame); // fixed #frame-no - - strcpy(targetFile,targetDir); - strcat(targetFile, ".bobj.gz"); - - snprintf(debugStrBuffer,256,"loadFluidsimMesh call (obid '%s', rp %d, dm %d) '%s' \n", srcob->id.name, useRenderParams, displaymode, targetFile); // debug - elbeemDebugOut(debugStrBuffer); // debug - - if(displaymode!=2) { // dont add bounding box for final - mesh = readBobjgz(targetFile, srcob->fluidsimSettings->orgMesh ,NULL,NULL); - } else { - mesh = readBobjgz(targetFile, srcob->fluidsimSettings->orgMesh, bbSize,bbSize ); - } - if(!mesh) { - // switch, abort background rendering when fluidsim mesh is missing - const char *strEnvName2 = "BLENDER_ELBEEMBOBJABORT"; // from blendercall.cpp - if(G.background==1) { - if(getenv(strEnvName2)) { - int elevel = atoi(getenv(strEnvName2)); - if(elevel>0) { - printf("Env. var %s set, fluid sim mesh '%s' not found, aborting render...\n",strEnvName2, targetFile); - exit(1); - } + attribs->tang.array = fdata->layers[layer].data; + attribs->tang.emOffset = fdata->layers[layer].offset; + attribs->tang.glIndex = gattribs->layer[b].glindex; } } - - // display org. object upon failure - srcob->data = srcob->fluidsimSettings->orgMesh; - return; - } - - if((mesh)&&(mesh->totvert>0)) { - make_edges(mesh, 0); // 0 = make all edges draw - } - srcob->fluidsimSettings->meshSurface = mesh; - srcob->data = mesh; - srcob->fluidsimSettings->meshSurfNormals = MEM_dupallocN(mesh->mvert); - - // load vertex velocities, if they exist... - // TODO? use generate flag as loading flag as well? - // warning, needs original .bobj.gz mesh loading filename - if(displaymode==3) { - readVelgz(targetFile, srcob); - } else { - // no data for preview, only clear... - int i,j; - for(i=0; i<mesh->totvert;i++) { for(j=0; j<3; j++) { srcob->fluidsimSettings->meshSurfNormals[i].co[j] = 0.; }} - } + else if(gattribs->layer[b].type == CD_ORCO) { + /* original coordinates */ + layer = CustomData_get_layer_index(vdata, CD_ORCO); - //fprintf(stderr,"LOADFLM DEBXHCH fs=%d 3:%d,%d,%d \n", (int)mesh, ((Mesh *)(srcob->fluidsimSettings->meshSurface))->mvert[3].no[0], ((Mesh *)(srcob->fluidsimSettings->meshSurface))->mvert[3].no[1], ((Mesh *)(srcob->fluidsimSettings->meshSurface))->mvert[3].no[2]); - return; -} + if(layer != -1) { + attribs->totorco = 1; -/* helper function */ -/* init axis aligned BB for mesh object */ -void fluidsimGetAxisAlignedBB(struct Mesh *mesh, float obmat[][4], - /*RET*/ float start[3], /*RET*/ float size[3], /*RET*/ struct Mesh **bbmesh ) -{ - float bbsx=0.0, bbsy=0.0, bbsz=0.0; - float bbex=1.0, bbey=1.0, bbez=1.0; - int i; - float vec[3]; - - VECCOPY(vec, mesh->mvert[0].co); - Mat4MulVecfl(obmat, vec); - bbsx = vec[0]; bbsy = vec[1]; bbsz = vec[2]; - bbex = vec[0]; bbey = vec[1]; bbez = vec[2]; - - for(i=1; i<mesh->totvert;i++) { - VECCOPY(vec, mesh->mvert[i].co); - Mat4MulVecfl(obmat, vec); - - if(vec[0] < bbsx){ bbsx= vec[0]; } - if(vec[1] < bbsy){ bbsy= vec[1]; } - if(vec[2] < bbsz){ bbsz= vec[2]; } - if(vec[0] > bbex){ bbex= vec[0]; } - if(vec[1] > bbey){ bbey= vec[1]; } - if(vec[2] > bbez){ bbez= vec[2]; } - } - - // return values... - if(start) { - start[0] = bbsx; - start[1] = bbsy; - start[2] = bbsz; - } - if(size) { - size[0] = bbex-bbsx; - size[1] = bbey-bbsy; - size[2] = bbez-bbsz; - } - - // init bounding box mesh? - if(bbmesh) { - int i,j; - Mesh *newmesh = NULL; - if(!(*bbmesh)) { newmesh = MEM_callocN(sizeof(Mesh), "fluidsimGetAxisAlignedBB_meshbb"); } - else { newmesh = *bbmesh; } - - newmesh->totvert = 8; - if(!newmesh->mvert) - newmesh->mvert = CustomData_add_layer(&newmesh->vdata, CD_MVERT, CD_CALLOC, NULL, newmesh->totvert); - for(i=0; i<8; i++) { - for(j=0; j<3; j++) newmesh->mvert[i].co[j] = start[j]; + attribs->orco.array = vdata->layers[layer].data; + attribs->orco.emOffset = vdata->layers[layer].offset; + attribs->orco.glIndex = gattribs->layer[b].glindex; + } } - - newmesh->totface = 6; - if(!newmesh->mface) - newmesh->mface = CustomData_add_layer(&newmesh->fdata, CD_MFACE, CD_CALLOC, NULL, newmesh->totface); - - *bbmesh = newmesh; } } -#else // DISABLE_ELBEEM - -/* dummy for mesh_calc_modifiers */ -void loadFluidsimMesh(Object *srcob, int useRenderParams) { -} - -#endif // DISABLE_ELBEEM - diff --git a/source/blender/blenkernel/intern/Makefile b/source/blender/blenkernel/intern/Makefile index 74c6c551a83..8921ea5d3b7 100644 --- a/source/blender/blenkernel/intern/Makefile +++ b/source/blender/blenkernel/intern/Makefile @@ -36,6 +36,7 @@ include nan_compile.mk CFLAGS += $(LEVEL_1_C_WARNINGS) # OpenGL and Python +CPPFLAGS += -I$(NAN_GLEW)/include CPPFLAGS += -I$(OPENGL_HEADERS) CPPFLAGS += -I$(NAN_PYTHON)/include/python$(NAN_PYTHON_VERSION) @@ -74,6 +75,9 @@ CPPFLAGS += -I$(NAN_ZLIB)/include #path to nodes CPPFLAGS += -I../../nodes +#path to gpu +CPPFLAGS += -I../../gpu + # path to our own external headerfiles CPPFLAGS += -I.. diff --git a/source/blender/blenkernel/intern/action.c b/source/blender/blenkernel/intern/action.c index 5b96bf11056..e0afdec5e23 100644 --- a/source/blender/blenkernel/intern/action.c +++ b/source/blender/blenkernel/intern/action.c @@ -62,6 +62,7 @@ #include "BLI_arithb.h" #include "BLI_blenlib.h" +#include "BLI_ghash.h" #include "nla.h" @@ -340,6 +341,60 @@ void free_pose(bPose *pose) } } +void game_copy_pose(bPose **dst, bPose *src) +{ + bPose *out; + bPoseChannel *pchan, *outpchan; + GHash *ghash; + + /* the game engine copies the current armature pose and then swaps + * the object pose pointer. this makes it possible to change poses + * without affecting the original blender data. */ + + if (!src) { + *dst=NULL; + return; + } + else if (*dst==src) { + printf("copy_pose source and target are the same\n"); + *dst=NULL; + return; + } + + out= MEM_dupallocN(src); + out->agroups.first= out->agroups.last= NULL; + duplicatelist(&out->chanbase, &src->chanbase); + + /* remap pointers */ + ghash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp); + + pchan= src->chanbase.first; + outpchan= out->chanbase.first; + for (; pchan; pchan=pchan->next, outpchan=outpchan->next) + BLI_ghash_insert(ghash, pchan, outpchan); + + for (pchan=out->chanbase.first; pchan; pchan=pchan->next) { + pchan->parent= BLI_ghash_lookup(ghash, pchan->parent); + pchan->child= BLI_ghash_lookup(ghash, pchan->child); + pchan->path= NULL; + } + + BLI_ghash_free(ghash, NULL, NULL); + + *dst=out; +} + +void game_free_pose(bPose *pose) +{ + if (pose) { + /* we don't free constraints, those are owned by the original pose */ + if(pose->chanbase.first) + BLI_freelistN(&pose->chanbase); + + MEM_freeN(pose); + } +} + static void copy_pose_channel_data(bPoseChannel *pchan, const bPoseChannel *chan) { bConstraint *pcon, *con; @@ -347,6 +402,8 @@ static void copy_pose_channel_data(bPoseChannel *pchan, const bPoseChannel *chan VECCOPY(pchan->loc, chan->loc); VECCOPY(pchan->size, chan->size); QUATCOPY(pchan->quat, chan->quat); + Mat4CpyMat4(pchan->chan_mat, (float(*)[4])chan->chan_mat); + Mat4CpyMat4(pchan->pose_mat, (float(*)[4])chan->pose_mat); pchan->flag= chan->flag; con= chan->constraints.first; @@ -860,7 +917,7 @@ typedef struct NlaIpoChannel { int type; } NlaIpoChannel; -void extract_ipochannels_from_action(ListBase *lb, ID *id, bAction *act, char *name, float ctime) +void extract_ipochannels_from_action(ListBase *lb, ID *id, bAction *act, const char *name, float ctime) { bActionChannel *achan= get_action_channel(act, name); IpoCurve *icu; @@ -991,7 +1048,7 @@ static float nla_time(float cfra, float unit) static float stridechannel_frame(Object *ob, float sizecorr, bActionStrip *strip, Path *path, float pathdist, float *stride_offset) { bAction *act= strip->act; - char *name= strip->stridechannel; + const char *name= strip->stridechannel; bActionChannel *achan= get_action_channel(act, name); int stride_axis= strip->stride_axis; diff --git a/source/blender/blenkernel/intern/anim.c b/source/blender/blenkernel/intern/anim.c index 1592c3e5504..731c2a18c55 100644 --- a/source/blender/blenkernel/intern/anim.c +++ b/source/blender/blenkernel/intern/anim.c @@ -52,6 +52,7 @@ #include "DNA_vfont_types.h" #include "BKE_anim.h" +#include "BKE_curve.h" #include "BKE_DerivedMesh.h" #include "BKE_displist.h" #include "BKE_effect.h" @@ -118,7 +119,7 @@ void calc_curvepath(Object *ob) path->len= tot+1; /* exception: vector handle paths and polygon paths should be subdivided at least a factor resolu */ - if(path->len<nu->resolu*nu->pntsu) path->len= nu->resolu*nu->pntsu; + if(path->len<nu->resolu*SEGMENTSU(nu)) path->len= nu->resolu*SEGMENTSU(nu); dist= (float *)MEM_mallocN((tot+1)*4, "calcpathdist"); @@ -303,7 +304,7 @@ static void group_duplilist(ListBase *lb, Object *ob, int level, int animated) DupliObject *dob; Group *group; GroupObject *go; - float mat[4][4]; + float mat[4][4], tmat[4][4]; if(ob->dup_group==NULL) return; group= ob->dup_group; @@ -320,7 +321,15 @@ static void group_duplilist(ListBase *lb, Object *ob, int level, int animated) /* note, if you check on layer here, render goes wrong... it still deforms verts and uses parent imat */ if(go->ob!=ob) { - Mat4MulMat4(mat, go->ob->obmat, ob->obmat); + /* Group Dupli Offset, should apply after everything else */ + if (group->dupli_ofs[0] || group->dupli_ofs[1] || group->dupli_ofs[2]) { + Mat4CpyMat4(tmat, go->ob->obmat); + VecSubf(tmat[3], tmat[3], group->dupli_ofs); + Mat4MulMat4(mat, tmat, ob->obmat); + } else { + Mat4MulMat4(mat, go->ob->obmat, ob->obmat); + } + dob= new_dupli_object(lb, go->ob, mat, ob->lay, 0, OB_DUPLIGROUP, animated); dob->no_draw= (dob->origlay & group->layer)==0; @@ -775,6 +784,10 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Object *par, float par_ hair= (totchild == 0 || psys->childcache) && psys->pathcache; if(!hair) return; + + /* we use cache, update totchild according to cached data */ + totchild = psys->totchildcache; + totpart = psys->totcached; } psys->lattice = psys_get_lattice(par, psys); @@ -857,6 +870,8 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Object *par, float par_ } VECCOPY(pamat[3], cache->co); + pamat[3][3]= 1.0f; + } else if(step_nbr) { /* other keys */ @@ -1059,7 +1074,8 @@ static void object_duplilist_recursive(ID *id, Object *ob, ListBase *duplilist, } } -/* note; group dupli's already set transform matrix. see note in group_duplilist() */ +/* Returns a list of DupliObject + * note; group dupli's already set transform matrix. see note in group_duplilist() */ ListBase *object_duplilist(Scene *sce, Object *ob) { ListBase *duplilist= MEM_mallocN(sizeof(ListBase), "duplilist"); diff --git a/source/blender/blenkernel/intern/armature.c b/source/blender/blenkernel/intern/armature.c index fb7d59c137a..155d5a7d21c 100644 --- a/source/blender/blenkernel/intern/armature.c +++ b/source/blender/blenkernel/intern/armature.c @@ -1573,21 +1573,16 @@ static void initialize_posetree(struct Object *ob, bPoseChannel *pchan_tip) /* find IK constraint, and validate it */ for(con= pchan_tip->constraints.first; con; con= con->next) { - if(con->type==CONSTRAINT_TYPE_KINEMATIC) break; + if(con->type==CONSTRAINT_TYPE_KINEMATIC) { + data=(bKinematicConstraint*)con->data; + if (data->flag & CONSTRAINT_IK_AUTO) break; + if (data->tar==NULL) continue; + if (data->tar->type==OB_ARMATURE && data->subtarget[0]==0) continue; + if ((con->flag & CONSTRAINT_DISABLE)==0 && (con->enforce!=0.0)) break; + } } if(con==NULL) return; - data=(bKinematicConstraint*)con->data; - - /* two types of targets */ - if(data->flag & CONSTRAINT_IK_AUTO); - else { - if(con->flag & CONSTRAINT_DISABLE) return; /* checked in editconstraint.c */ - if(con->enforce == 0.0f) return; - if(data->tar==NULL) return; - if(data->tar->type==OB_ARMATURE && data->subtarget[0]==0) return; - } - /* exclude tip from chain? */ if(!(data->flag & CONSTRAINT_IK_TIP)) pchan_tip= pchan_tip->parent; @@ -1690,7 +1685,7 @@ static void initialize_posetree(struct Object *ob, bPoseChannel *pchan_tip) were executed & assigned. Now as last we do an IK pass */ static void execute_posetree(Object *ob, PoseTree *tree) { - float R_parmat[3][3]; + float R_parmat[3][3], identity[3][3]; float iR_parmat[3][3]; float R_bonemat[3][3]; float goalrot[3][3], goalpos[3]; @@ -1699,7 +1694,8 @@ static void execute_posetree(Object *ob, PoseTree *tree) float irest_basis[3][3], full_basis[3][3]; float end_pose[4][4], world_pose[4][4]; float length, basis[3][3], rest_basis[3][3], start[3], *ikstretch=NULL; - int a, flag, hasstretch=0; + float resultinf=0.0f; + int a, flag, hasstretch=0, resultblend=0; bPoseChannel *pchan; IK_Segment *seg, *parent, **iktree, *iktarget; IK_Solver *solver; @@ -1844,6 +1840,12 @@ static void execute_posetree(Object *ob, PoseTree *tree) Mat4MulMat4(goal, rootmat, goalinv); VECCOPY(polepos, goal[3]); poleconstrain= 1; + + /* for pole targets, we blend the result of the ik solver + * instead of the target position, otherwise we can't get + * a smooth transition */ + resultblend= 1; + resultinf= target->con->enforce; if(data->flag & CONSTRAINT_IK_GETANGLE) { poleangledata= data; @@ -1853,7 +1855,7 @@ static void execute_posetree(Object *ob, PoseTree *tree) } /* do we need blending? */ - if (target->con->enforce!=1.0) { + if (!resultblend && target->con->enforce!=1.0) { float q1[4], q2[4], q[4]; float fac= target->con->enforce; float mfac= 1.0-fac; @@ -1903,7 +1905,7 @@ static void execute_posetree(Object *ob, PoseTree *tree) tree->basis_change= MEM_mallocN(sizeof(float[3][3])*tree->totchannel, "ik basis change"); if(hasstretch) ikstretch= MEM_mallocN(sizeof(float)*tree->totchannel, "ik stretch"); - + for(a=0; a<tree->totchannel; a++) { IK_GetBasisChange(iktree[a], tree->basis_change[a]); @@ -1931,6 +1933,12 @@ static void execute_posetree(Object *ob, PoseTree *tree) VecMulf(tree->basis_change[a][1], stretch); VecMulf(tree->basis_change[a][2], stretch); } + + if(resultblend && resultinf!=1.0f) { + Mat3One(identity); + Mat3BlendMat3(tree->basis_change[a], identity, + tree->basis_change[a], resultinf); + } IK_FreeSegment(iktree[a]); } diff --git a/source/blender/blenkernel/intern/blender.c b/source/blender/blenkernel/intern/blender.c index aca51e56c6e..1fe6447752e 100644 --- a/source/blender/blenkernel/intern/blender.c +++ b/source/blender/blenkernel/intern/blender.c @@ -525,6 +525,7 @@ typedef struct UndoElem { char str[FILE_MAXDIR+FILE_MAXFILE]; char name[MAXUNDONAME]; MemFile memfile; + uintptr_t undosize; } UndoElem; static ListBase undobase={NULL, NULL}; @@ -555,6 +556,7 @@ static int read_undosave(UndoElem *uel) /* name can be a dynamic string */ void BKE_write_undo(char *name) { + uintptr_t maxmem, totmem, memused; int nr, success; UndoElem *uel; @@ -616,8 +618,36 @@ void BKE_write_undo(char *name) if(curundo->prev) prevfile= &(curundo->prev->memfile); + memused= MEM_get_memory_in_use(); success= BLO_write_file_mem(prevfile, &curundo->memfile, G.fileflags, &err); - + curundo->undosize= MEM_get_memory_in_use() - memused; + } + + if(U.undomemory != 0) { + /* limit to maximum memory (afterwards, we can't know in advance) */ + totmem= 0; + maxmem= ((uintptr_t)U.undomemory)*1024*1024; + + /* keep at least two (original + other) */ + uel= undobase.last; + while(uel && uel->prev) { + totmem+= uel->undosize; + if(totmem>maxmem) break; + uel= uel->prev; + } + + if(uel) { + if(uel->prev && uel->prev->prev) + uel= uel->prev; + + while(undobase.first!=uel) { + UndoElem *first= undobase.first; + BLI_remlink(&undobase, first); + /* the merge is because of compression */ + BLO_merge_memfile(&first->memfile, &first->next->memfile); + MEM_freeN(first); + } + } } } @@ -683,14 +713,14 @@ char *BKE_undo_menu_string(void) UndoElem *uel; DynStr *ds= BLI_dynstr_new(); char *menu; - + BLI_dynstr_append(ds, "Global Undo History %t"); for(uel= undobase.first; uel; uel= uel->next) { BLI_dynstr_append(ds, "|"); BLI_dynstr_append(ds, uel->name); } - + menu= BLI_dynstr_get_cstring(ds); BLI_dynstr_free(ds); diff --git a/source/blender/blenkernel/intern/brush.c b/source/blender/blenkernel/intern/brush.c index 6ea470a2263..1c53af97dbb 100644 --- a/source/blender/blenkernel/intern/brush.c +++ b/source/blender/blenkernel/intern/brush.c @@ -359,7 +359,6 @@ void brush_sample_tex(Brush *brush, float *xy, float *rgba) rgba[0]= rgba[1]= rgba[2]= rgba[3]= 1.0f; } -#define FTOCHAR(val) val<=0.0f?0: (val>=1.0f?255: (char)(255.0f*val)) void brush_imbuf_new(Brush *brush, short flt, short texfall, int size, ImBuf **outbuf) { diff --git a/source/blender/blenkernel/intern/bullet.c b/source/blender/blenkernel/intern/bullet.c new file mode 100644 index 00000000000..eeec4d0bd4e --- /dev/null +++ b/source/blender/blenkernel/intern/bullet.c @@ -0,0 +1,95 @@ +/* + * + * $Id$ + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) Blender Foundation + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "MEM_guardedalloc.h" + +/* types */ +#include "DNA_object_force.h" /* here is the softbody struct */ + +#include "BKE_bullet.h" + + +/* ************ Object level, exported functions *************** */ + +/* allocates and initializes general main data */ +BulletSoftBody *bsbNew(void) +{ + BulletSoftBody *bsb; + + bsb= MEM_callocN(sizeof(BulletSoftBody), "bulletsoftbody"); + + bsb->flag = OB_BSB_BENDING_CONSTRAINTS | OB_BSB_SHAPE_MATCHING | OB_BSB_AERO_VPOINT; + bsb->linStiff = 0.5f; + bsb->angStiff = 1.0f; + bsb->volume = 1.0f; + + + bsb->viterations = 0; + bsb->piterations = 2; + bsb->diterations = 0; + bsb->citerations = 4; + + bsb->kSRHR_CL = 0.1f; + bsb->kSKHR_CL = 1.f; + bsb->kSSHR_CL = 0.5f; + bsb->kSR_SPLT_CL = 0.5f; + + bsb->kSK_SPLT_CL = 0.5f; + bsb->kSS_SPLT_CL = 0.5f; + bsb->kVCF = 1; + bsb->kDP = 0; + + bsb->kDG = 0; + bsb->kLF = 0; + bsb->kPR = 0; + bsb->kVC = 0; + + bsb->kDF = 0.2f; + bsb->kMT = 0.05; + bsb->kCHR = 1.0f; + bsb->kKHR = 0.1f; + + bsb->kSHR = 1.0f; + bsb->kAHR = 0.7f; + bsb->collisionflags = 0; + //bsb->collisionflags = OB_BSB_COL_CL_RS + OB_BSB_COL_CL_SS; + bsb->numclusteriterations = 64; + + return bsb; +} + +/* frees all */ +void bsbFree(BulletSoftBody *bsb) +{ + /* no internal data yet */ + MEM_freeN(bsb); +} + + diff --git a/source/blender/blenkernel/intern/bvhutils.c b/source/blender/blenkernel/intern/bvhutils.c new file mode 100644 index 00000000000..5b68a637ea2 --- /dev/null +++ b/source/blender/blenkernel/intern/bvhutils.c @@ -0,0 +1,426 @@ +/** + * + * $Id$ + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): André Pinto. + * + * ***** END GPL LICENSE BLOCK ***** + */ +#include <stdio.h> +#include <string.h> +#include <math.h> + +#include "BKE_bvhutils.h" + +#include "DNA_object_types.h" +#include "DNA_modifier_types.h" +#include "DNA_meshdata_types.h" + +#include "BKE_DerivedMesh.h" +#include "BKE_utildefines.h" +#include "BKE_deform.h" +#include "BKE_cdderivedmesh.h" +#include "BKE_displist.h" +#include "BKE_global.h" + +#include "BLI_arithb.h" + +/* Math stuff for ray casting on mesh faces and for nearest surface */ + +static float nearest_point_in_tri_surface(const float *point, const float *v0, const float *v1, const float *v2, float *nearest); + +#define ISECT_EPSILON 1e-6 +static float ray_tri_intersection(const BVHTreeRay *ray, const float m_dist, const float *v0, const float *v1, const float *v2) +{ + float dist; + + if(RayIntersectsTriangle(ray->origin, ray->direction, v0, v1, v2, &dist, NULL)) + return dist; + + return FLT_MAX; +} + +static float sphereray_tri_intersection(const BVHTreeRay *ray, float radius, const float m_dist, const float *v0, const float *v1, const float *v2) +{ + + float idist; + float p1[3]; + float plane_normal[3], hit_point[3]; + + CalcNormFloat((float*)v0, (float*)v1, (float*)v2, plane_normal); + + VECADDFAC( p1, ray->origin, ray->direction, m_dist); + if(SweepingSphereIntersectsTriangleUV(ray->origin, p1, radius, v0, v1, v2, &idist, &hit_point)) + { + return idist * m_dist; + } + + return FLT_MAX; +} + +/* + * This calculates the distance from point to the plane + * Distance is negative if point is on the back side of plane + */ +static float point_plane_distance(const float *point, const float *plane_point, const float *plane_normal) +{ + float pp[3]; + VECSUB(pp, point, plane_point); + return INPR(pp, plane_normal); +} +static float choose_nearest(const float v0[2], const float v1[2], const float point[2], float closest[2]) +{ + float d[2][2], sdist[2]; + VECSUB2D(d[0], v0, point); + VECSUB2D(d[1], v1, point); + + sdist[0] = d[0][0]*d[0][0] + d[0][1]*d[0][1]; + sdist[1] = d[1][0]*d[1][0] + d[1][1]*d[1][1]; + + if(sdist[0] < sdist[1]) + { + if(closest) + VECCOPY2D(closest, v0); + return sdist[0]; + } + else + { + if(closest) + VECCOPY2D(closest, v1); + return sdist[1]; + } +} +/* + * calculates the closest point between point-tri (2D) + * returns that tri must be right-handed + * Returns square distance + */ +static float closest_point_in_tri2D(const float point[2], /*const*/ float tri[3][2], float closest[2]) +{ + float edge_di[2]; + float v_point[2]; + float proj[2]; //point projected over edge-dir, edge-normal (witouth normalized edge) + const float *v0 = tri[2], *v1; + float edge_slen, d; //edge squared length + int i; + const float *nearest_vertex = NULL; + + + //for each edge + for(i=0, v0=tri[2], v1=tri[0]; i < 3; v0=tri[i++], v1=tri[i]) + { + VECSUB2D(edge_di, v1, v0); + VECSUB2D(v_point, point, v0); + + proj[1] = v_point[0]*edge_di[1] - v_point[1]*edge_di[0]; //dot product with edge normal + + //point inside this edge + if(proj[1] < 0) + continue; + + proj[0] = v_point[0]*edge_di[0] + v_point[1]*edge_di[1]; + + //closest to this edge is v0 + if(proj[0] < 0) + { + if(nearest_vertex == NULL || nearest_vertex == v0) + nearest_vertex = v0; + else + { + //choose nearest + return choose_nearest(nearest_vertex, v0, point, closest); + } + i++; //We can skip next edge + continue; + } + + edge_slen = edge_di[0]*edge_di[0] + edge_di[1]*edge_di[1]; //squared edge len + //closest to this edge is v1 + if(proj[0] > edge_slen) + { + if(nearest_vertex == NULL || nearest_vertex == v1) + nearest_vertex = v1; + else + { + return choose_nearest(nearest_vertex, v1, point, closest); + } + continue; + } + + //nearest is on this edge + d= proj[1] / edge_slen; + closest[0] = point[0] - edge_di[1] * d; + closest[1] = point[1] + edge_di[0] * d; + + return proj[1]*proj[1]/edge_slen; + } + + if(nearest_vertex) + { + VECSUB2D(v_point, nearest_vertex, point); + VECCOPY2D(closest, nearest_vertex); + return v_point[0]*v_point[0] + v_point[1]*v_point[1]; + } + else + { + VECCOPY(closest, point); //point is already inside + return 0.0f; + } +} + +/* + * Returns the square of the minimum distance between the point and a triangle surface + * If nearest is not NULL the nearest surface point is written on it + */ +static float nearest_point_in_tri_surface(const float *point, const float *v0, const float *v1, const float *v2, float *nearest) +{ + //Lets solve the 2D problem (closest point-tri) + float normal_dist, plane_sdist, plane_offset; + float du[3], dv[3], dw[3]; //orthogonal axis (du=(v0->v1), dw=plane normal) + + float p_2d[2], tri_2d[3][2], nearest_2d[2]; + + CalcNormFloat((float*)v0, (float*)v1, (float*)v2, dw); + + //point-plane distance and calculate axis + normal_dist = point_plane_distance(point, v0, dw); + + // OPTIMIZATION + // if we are only interested in nearest distance if its closer than some distance already found + // we can: + // if(normal_dist*normal_dist >= best_dist_so_far) return FLOAT_MAX; + // + + VECSUB(du, v1, v0); + Normalize(du); + Crossf(dv, dw, du); + plane_offset = INPR(v0, dw); + + //project stuff to 2d + tri_2d[0][0] = INPR(du, v0); + tri_2d[0][1] = INPR(dv, v0); + + tri_2d[1][0] = INPR(du, v1); + tri_2d[1][1] = INPR(dv, v1); + + tri_2d[2][0] = INPR(du, v2); + tri_2d[2][1] = INPR(dv, v2); + + p_2d[0] = INPR(du, point); + p_2d[1] = INPR(dv, point); + + //we always have a right-handed tri + //this should always happen because of the way normal is calculated + plane_sdist = closest_point_in_tri2D(p_2d, tri_2d, nearest_2d); + + //project back to 3d + if(nearest) + { + nearest[0] = du[0]*nearest_2d[0] + dv[0] * nearest_2d[1] + dw[0] * plane_offset; + nearest[1] = du[1]*nearest_2d[0] + dv[1] * nearest_2d[1] + dw[1] * plane_offset; + nearest[2] = du[2]*nearest_2d[0] + dv[2] * nearest_2d[1] + dw[2] * plane_offset; + } + + return plane_sdist + normal_dist*normal_dist; +} + + +/* + * BVH from meshs callbacks + */ + +// Callback to bvh tree nearest point. The tree must bust have been built using bvhtree_from_mesh_faces. +// userdata must be a BVHMeshCallbackUserdata built from the same mesh as the tree. +static void mesh_faces_nearest_point(void *userdata, int index, const float *co, BVHTreeNearest *nearest) +{ + const BVHTreeFromMesh *data = (BVHTreeFromMesh*) userdata; + MVert *vert = data->vert; + MFace *face = data->face + index; + + float *t0, *t1, *t2, *t3; + t0 = vert[ face->v1 ].co; + t1 = vert[ face->v2 ].co; + t2 = vert[ face->v3 ].co; + t3 = face->v4 ? vert[ face->v4].co : NULL; + + + do + { + float nearest_tmp[3], dist; + + dist = nearest_point_in_tri_surface(co,t0, t1, t2, nearest_tmp); + if(dist < nearest->dist) + { + nearest->index = index; + nearest->dist = dist; + VECCOPY(nearest->co, nearest_tmp); + CalcNormFloat((float*)t0, (float*)t1, (float*)t2, nearest->no); //TODO.. (interpolate normals from the vertexs coordinates? + } + + + t1 = t2; + t2 = t3; + t3 = NULL; + + } while(t2); +} + +// Callback to bvh tree raycast. The tree must bust have been built using bvhtree_from_mesh_faces. +// userdata must be a BVHMeshCallbackUserdata built from the same mesh as the tree. +static void mesh_faces_spherecast(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit) +{ + const BVHTreeFromMesh *data = (BVHTreeFromMesh*) userdata; + MVert *vert = data->vert; + MFace *face = data->face + index; + + float *t0, *t1, *t2, *t3; + t0 = vert[ face->v1 ].co; + t1 = vert[ face->v2 ].co; + t2 = vert[ face->v3 ].co; + t3 = face->v4 ? vert[ face->v4].co : NULL; + + + do + { + float dist; + if(data->sphere_radius == 0.0f) + dist = ray_tri_intersection(ray, hit->dist, t0, t1, t2); + else + dist = sphereray_tri_intersection(ray, data->sphere_radius, hit->dist, t0, t1, t2); + + if(dist >= 0 && dist < hit->dist) + { + hit->index = index; + hit->dist = dist; + VECADDFAC(hit->co, ray->origin, ray->direction, dist); + + CalcNormFloat(t0, t1, t2, hit->no); + } + + t1 = t2; + t2 = t3; + t3 = NULL; + + } while(t2); +} + +/* + * BVH builders + */ +// Builds a bvh tree.. where nodes are the vertexs of the given mesh +void bvhtree_from_mesh_verts(BVHTreeFromMesh *data, DerivedMesh *mesh, float epsilon, int tree_type, int axis) +{ + int i; + int numVerts= mesh->getNumVerts(mesh); + MVert *vert = mesh->getVertDataArray(mesh, CD_MVERT); + BVHTree *tree = NULL; + + memset(data, 0, sizeof(*data)); + + if(vert == NULL) + { + printf("bvhtree cant be build: cant get a vertex array"); + return; + } + + tree = BLI_bvhtree_new(numVerts, epsilon, tree_type, axis); + if(tree != NULL) + { + for(i = 0; i < numVerts; i++) + BLI_bvhtree_insert(tree, i, vert[i].co, 1); + + BLI_bvhtree_balance(tree); + + data->tree = tree; + + //a NULL nearest callback works fine + //remeber the min distance to point is the same as the min distance to BV of point + data->nearest_callback = NULL; + data->raycast_callback = NULL; + + data->mesh = mesh; + data->vert = mesh->getVertDataArray(mesh, CD_MVERT); + data->face = mesh->getFaceDataArray(mesh, CD_MFACE); + + data->sphere_radius = epsilon; + } +} + +// Builds a bvh tree.. where nodes are the faces of the given mesh. +void bvhtree_from_mesh_faces(BVHTreeFromMesh *data, DerivedMesh *mesh, float epsilon, int tree_type, int axis) +{ + int i; + int numFaces= mesh->getNumFaces(mesh); + MVert *vert = mesh->getVertDataArray(mesh, CD_MVERT); + MFace *face = mesh->getFaceDataArray(mesh, CD_MFACE); + BVHTree *tree = NULL; + + memset(data, 0, sizeof(*data)); + + if(vert == NULL && face == NULL) + { + printf("bvhtree cant be build: cant get a vertex/face array"); + return; + } + + /* Create a bvh-tree of the given target */ + tree = BLI_bvhtree_new(numFaces, epsilon, tree_type, axis); + if(tree != NULL) + { + for(i = 0; i < numFaces; i++) + { + float co[4][3]; + VECCOPY(co[0], vert[ face[i].v1 ].co); + VECCOPY(co[1], vert[ face[i].v2 ].co); + VECCOPY(co[2], vert[ face[i].v3 ].co); + if(face[i].v4) + VECCOPY(co[3], vert[ face[i].v4 ].co); + + BLI_bvhtree_insert(tree, i, co[0], face[i].v4 ? 4 : 3); + } + BLI_bvhtree_balance(tree); + + data->tree = tree; + data->nearest_callback = mesh_faces_nearest_point; + data->raycast_callback = mesh_faces_spherecast; + + data->mesh = mesh; + data->vert = mesh->getVertDataArray(mesh, CD_MVERT); + data->face = mesh->getFaceDataArray(mesh, CD_MFACE); + + data->sphere_radius = epsilon; + } +} + +// Frees data allocated by a call to bvhtree_from_mesh_*. +void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data) +{ + if(data->tree) + { + BLI_bvhtree_free(data->tree); + memset( data, 0, sizeof(data) ); + } +} + + diff --git a/source/blender/blenkernel/intern/cdderivedmesh.c b/source/blender/blenkernel/intern/cdderivedmesh.c index 472df3d0f26..2c1b5ced614 100644 --- a/source/blender/blenkernel/intern/cdderivedmesh.c +++ b/source/blender/blenkernel/intern/cdderivedmesh.c @@ -58,6 +58,10 @@ #include "MEM_guardedalloc.h" +#include "GPU_draw.h" +#include "GPU_extensions.h" +#include "GPU_material.h" + #include <string.h> #include <limits.h> @@ -117,7 +121,7 @@ static void cdDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r) memcpy(edge_r, cddm->medge, sizeof(*edge_r) * dm->numEdgeData); } -void cdDM_copyFaceArray(DerivedMesh *dm, MFace *face_r) +static void cdDM_copyFaceArray(DerivedMesh *dm, MFace *face_r) { CDDerivedMesh *cddm = (CDDerivedMesh *)dm; memcpy(face_r, cddm->mface, sizeof(*face_r) * dm->numFaceData); @@ -243,7 +247,7 @@ static void cdDM_drawLooseEdges(DerivedMesh *dm) glEnd(); } -static void cdDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int)) +static void cdDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) { CDDerivedMesh *cddm = (CDDerivedMesh*) dm; MVert *mvert = cddm->mvert; @@ -271,7 +275,7 @@ static void cdDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int)) || new_shademodel != shademodel) { glEnd(); - drawCurrentMat = setMaterial(matnr = new_matnr); + drawCurrentMat = setMaterial(matnr = new_matnr, NULL); glShadeModel(shademodel = new_shademodel); glBegin(glmode = new_glmode); @@ -556,6 +560,134 @@ static void cdDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void cdDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData); } +static void cdDM_drawMappedFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs), int (*setDrawOptions)(void *userData, int index), void *userData) +{ + CDDerivedMesh *cddm = (CDDerivedMesh*) dm; + GPUVertexAttribs gattribs; + DMVertexAttribs attribs; + MVert *mvert = cddm->mvert; + MFace *mface = cddm->mface; + MTFace *tf = dm->getFaceDataArray(dm, CD_MTFACE); + float (*nors)[3] = dm->getFaceDataArray(dm, CD_NORMAL); + int a, b, dodraw, smoothnormal, matnr, new_matnr; + int transp, new_transp, orig_transp; + int orig, *index = dm->getFaceDataArray(dm, CD_ORIGINDEX); + + matnr = -1; + smoothnormal = 0; + dodraw = 0; + transp = GPU_get_material_blend_mode(); + orig_transp = transp; + + memset(&attribs, 0, sizeof(attribs)); + + glShadeModel(GL_SMOOTH); + glBegin(GL_QUADS); + + for(a = 0; a < dm->numFaceData; a++, mface++) { + new_matnr = mface->mat_nr + 1; + + if(new_matnr != matnr) { + glEnd(); + + dodraw = setMaterial(matnr = new_matnr, &gattribs); + if(dodraw) + DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs); + + glBegin(GL_QUADS); + } + + if(!dodraw) { + continue; + } + else if(setDrawOptions) { + orig = index[a]; + + if(orig == ORIGINDEX_NONE) + continue; + else if(!setDrawOptions(userData, orig)) + continue; + } + + if(tf) { + new_transp = tf[a].transp; + + if(new_transp != transp) { + glEnd(); + + if(new_transp == GPU_BLEND_SOLID && orig_transp != GPU_BLEND_SOLID) + GPU_set_material_blend_mode(orig_transp); + else + GPU_set_material_blend_mode(new_transp); + transp = new_transp; + + glBegin(GL_QUADS); + } + } + + smoothnormal = (mface->flag & ME_SMOOTH); + + if(!smoothnormal) { + if(nors) { + glNormal3fv(nors[a]); + } + else { + /* TODO ideally a normal layer should always be available */ + float nor[3]; + if(mface->v4) { + CalcNormFloat4(mvert[mface->v1].co, mvert[mface->v2].co, + mvert[mface->v3].co, mvert[mface->v4].co, + nor); + } else { + CalcNormFloat(mvert[mface->v1].co, mvert[mface->v2].co, + mvert[mface->v3].co, nor); + } + glNormal3fv(nor); + } + } + +#define PASSVERT(index, vert) { \ + if(attribs.totorco) \ + glVertexAttrib3fvARB(attribs.orco.glIndex, attribs.orco.array[index]); \ + for(b = 0; b < attribs.tottface; b++) { \ + MTFace *tf = &attribs.tface[b].array[a]; \ + glVertexAttrib2fvARB(attribs.tface[b].glIndex, tf->uv[vert]); \ + } \ + for(b = 0; b < attribs.totmcol; b++) { \ + MCol *cp = &attribs.mcol[b].array[a*4 + vert]; \ + GLubyte col[4]; \ + col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a; \ + glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col); \ + } \ + if(attribs.tottang) { \ + float *tang = attribs.tang.array[a*4 + vert]; \ + glVertexAttrib3fvARB(attribs.tang.glIndex, tang); \ + } \ + if(smoothnormal) \ + glNormal3sv(mvert[index].no); \ + glVertex3fv(mvert[index].co); \ +} + + PASSVERT(mface->v1, 0); + PASSVERT(mface->v2, 1); + PASSVERT(mface->v3, 2); + if(mface->v4) + PASSVERT(mface->v4, 3) + else + PASSVERT(mface->v3, 2) + +#undef PASSVERT + } + glEnd(); + + glShadeModel(GL_FLAT); +} + +static void cdDM_drawFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) +{ + dm->drawMappedFacesGLSL(dm, setMaterial, NULL, NULL); +} + static void cdDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData) { CDDerivedMesh *cddm = (CDDerivedMesh*) dm; @@ -713,8 +845,10 @@ static CDDerivedMesh *cdDM_create(const char *desc) dm->drawFacesSolid = cdDM_drawFacesSolid; dm->drawFacesColored = cdDM_drawFacesColored; dm->drawFacesTex = cdDM_drawFacesTex; + dm->drawFacesGLSL = cdDM_drawFacesGLSL; dm->drawMappedFaces = cdDM_drawMappedFaces; dm->drawMappedFacesTex = cdDM_drawMappedFacesTex; + dm->drawMappedFacesGLSL = cdDM_drawMappedFacesGLSL; dm->foreachMappedVert = cdDM_foreachMappedVert; dm->foreachMappedEdge = cdDM_foreachMappedEdge; @@ -764,10 +898,7 @@ DerivedMesh *CDDM_from_mesh(Mesh *mesh, Object *ob) dm->deformedOnly = 1; - if(ob && ob->fluidsimSettings && ob->fluidsimSettings->meshSurface) - alloctype= CD_DUPLICATE; - else - alloctype= CD_REFERENCE; + alloctype= CD_REFERENCE; CustomData_merge(&mesh->vdata, &dm->vertData, CD_MASK_MESH, alloctype, mesh->totvert); diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c index c7817b017ef..dbc94571cad 100644 --- a/source/blender/blenkernel/intern/cloth.c +++ b/source/blender/blenkernel/intern/cloth.c @@ -155,7 +155,7 @@ void cloth_init ( ClothModifierData *clmd ) BVHTree *bvhselftree_build_from_cloth (ClothModifierData *clmd, float epsilon) { - int i; + unsigned int i; BVHTree *bvhtree; Cloth *cloth = clmd->clothObject; ClothVertex *verts; @@ -196,7 +196,7 @@ BVHTree *bvhselftree_build_from_cloth (ClothModifierData *clmd, float epsilon) BVHTree *bvhtree_build_from_cloth (ClothModifierData *clmd, float epsilon) { - int i; + unsigned int i; BVHTree *bvhtree; Cloth *cloth = clmd->clothObject; ClothVertex *verts; @@ -782,11 +782,11 @@ static void cloth_to_object (Object *ob, ClothModifierData *clmd, DerivedMesh * /* can be optimized to do all groups in one loop */ static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm ) { - unsigned int i = 0; - unsigned int j = 0; + int i = 0; + int j = 0; MDeformVert *dvert = NULL; Cloth *clothObj = NULL; - unsigned int numverts = dm->getNumVerts ( dm ); + int numverts = dm->getNumVerts ( dm ); float goalfac = 0; ClothVertex *verts = NULL; @@ -857,7 +857,7 @@ static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm ) static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float framenr, int first) { - unsigned int i = 0; + int i = 0; MVert *mvert = NULL; ClothVertex *verts = NULL; float tnull[3] = {0,0,0}; @@ -1082,13 +1082,13 @@ int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm ) Cloth *cloth = clmd->clothObject; ClothSpring *spring = NULL, *tspring = NULL, *tspring2 = NULL; unsigned int struct_springs = 0, shear_springs=0, bend_springs = 0; - unsigned int i = 0; - unsigned int numverts = dm->getNumVerts ( dm ); - unsigned int numedges = dm->getNumEdges ( dm ); - unsigned int numfaces = dm->getNumFaces ( dm ); + int i = 0; + int numverts = dm->getNumVerts ( dm ); + int numedges = dm->getNumEdges ( dm ); + int numfaces = dm->getNumFaces ( dm ); MEdge *medge = CDDM_get_edges ( dm ); MFace *mface = CDDM_get_faces ( dm ); - unsigned int index2 = 0; // our second vertex index + int index2 = 0; // our second vertex index LinkNode **edgelist = NULL; EdgeHash *edgehash = NULL; LinkNode *search = NULL, *search2 = NULL; diff --git a/source/blender/blenkernel/intern/collision.c b/source/blender/blenkernel/intern/collision.c index cfcab54058d..b8f5b467c11 100644 --- a/source/blender/blenkernel/intern/collision.c +++ b/source/blender/blenkernel/intern/collision.c @@ -31,10 +31,11 @@ #include "BKE_cloth.h" -#include "DNA_group_types.h" -#include "DNA_object_types.h" #include "DNA_cloth_types.h" +#include "DNA_group_types.h" #include "DNA_mesh_types.h" +#include "DNA_object_types.h" +#include "DNA_object_force.h" #include "DNA_scene_types.h" #include "BKE_DerivedMesh.h" @@ -1306,10 +1307,39 @@ CollisionModifierData **get_collisionobjects(Object *self, int *numcollobj) // check all collision objects for ( base = G.scene->base.first; base; base = base->next ) { + /*Only proceed for mesh object in same layer */ + if(!(base->object->type==OB_MESH && (base->lay & self->lay))) + continue; + coll_ob = base->object; - collmd = ( CollisionModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Collision ); - - if ( !collmd ) + + if(coll_ob->pd && coll_ob->pd->deflect) + { + collmd = ( CollisionModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Collision ); + } + + if ( collmd ) + { + if(coll_ob == self) + continue; + + if(numobj >= maxobj) + { + // realloc + int oldmax = maxobj; + CollisionModifierData **tmp; + maxobj *= 2; + tmp = MEM_callocN(sizeof(CollisionModifierData *)*maxobj, "CollisionObjectsArray"); + memcpy(tmp, objs, sizeof(CollisionModifierData *)*oldmax); + MEM_freeN(objs); + objs = tmp; + + } + + objs[numobj] = collmd; + numobj++; + } + else { if ( coll_ob->dup_group ) { @@ -1319,8 +1349,12 @@ CollisionModifierData **get_collisionobjects(Object *self, int *numcollobj) for ( go= group->gobject.first; go; go= go->next ) { coll_ob = go->ob; - - collmd = ( CollisionModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Collision ); + collmd = NULL; + + if(coll_ob->pd && coll_ob->pd->deflect) + { + collmd = ( CollisionModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Collision ); + } if ( !collmd ) continue; @@ -1347,27 +1381,6 @@ CollisionModifierData **get_collisionobjects(Object *self, int *numcollobj) numobj++; } } - } - else - { - if(coll_ob == self) - continue; - - if(numobj >= maxobj) - { - // realloc - int oldmax = maxobj; - CollisionModifierData **tmp; - maxobj *= 2; - tmp = MEM_callocN(sizeof(CollisionModifierData *)*maxobj, "CollisionObjectsArray"); - memcpy(tmp, objs, sizeof(CollisionModifierData *)*oldmax); - MEM_freeN(objs); - objs = tmp; - - } - - objs[numobj] = collmd; - numobj++; } } *numcollobj = numobj; @@ -1484,6 +1497,9 @@ int cloth_bvh_objcollision ( Object *ob, ClothModifierData * clmd, float step, f BVHTreeOverlap *overlap = NULL; int result = 0; + if(!collmd->bvhtree) + continue; + /* move object to position (step) in time */ collision_move_object ( collmd, step + dt, step ); diff --git a/source/blender/blenkernel/intern/colortools.c b/source/blender/blenkernel/intern/colortools.c index 295b8fe3552..1bc34aea9a1 100644 --- a/source/blender/blenkernel/intern/colortools.c +++ b/source/blender/blenkernel/intern/colortools.c @@ -650,7 +650,6 @@ void curvemapping_evaluate_premulRGBF(CurveMapping *cumap, float *vecout, const vecout[2]= curvemap_evaluateF(cumap->cm+2, fac); } -#define FTOCHAR(val) val<=0.0f?0: (val>=1.0f?255: (char)(255.0f*val)) void curvemapping_do_ibuf(CurveMapping *cumap, ImBuf *ibuf) { @@ -730,3 +729,24 @@ void curvemapping_initialize(CurveMapping *cumap) curvemap_make_table(cumap->cm+a, &cumap->clipr); } } + +void curvemapping_table_RGBA(CurveMapping *cumap, float **array, int *size) +{ + int a; + + *size = CM_TABLE+1; + *array = MEM_callocN(sizeof(float)*(*size)*4, "CurveMapping"); + curvemapping_initialize(cumap); + + for(a=0; a<*size; a++) { + if(cumap->cm[0].table) + (*array)[a*4+0]= cumap->cm[0].table[a].y; + if(cumap->cm[1].table) + (*array)[a*4+1]= cumap->cm[1].table[a].y; + if(cumap->cm[2].table) + (*array)[a*4+2]= cumap->cm[2].table[a].y; + if(cumap->cm[3].table) + (*array)[a*4+3]= cumap->cm[3].table[a].y; + } +} + diff --git a/source/blender/blenkernel/intern/constraint.c b/source/blender/blenkernel/intern/constraint.c index f13fd5f9963..99cf58e8e5a 100644 --- a/source/blender/blenkernel/intern/constraint.c +++ b/source/blender/blenkernel/intern/constraint.c @@ -367,19 +367,12 @@ void constraint_mat_convertspace (Object *ob, bPoseChannel *pchan, float mat[][4 case CONSTRAINT_SPACE_WORLD: /* ---------- FROM WORLDSPACE ---------- */ { /* world to pose */ - if (to==CONSTRAINT_SPACE_POSE || to==CONSTRAINT_SPACE_LOCAL || to==CONSTRAINT_SPACE_PARLOCAL) { - Mat4Invert(imat, ob->obmat); - Mat4CpyMat4(tempmat, mat); - Mat4MulMat4(mat, tempmat, imat); - } + Mat4Invert(imat, ob->obmat); + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, tempmat, imat); - /* pose to local */ - if (to == CONSTRAINT_SPACE_LOCAL) { - /* call self with slightly different values */ - constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to); - } - /* pose to local + parent */ - else if (to == CONSTRAINT_SPACE_PARLOCAL) { + /* use pose-space as stepping stone for other spaces... */ + if (ELEM(to, CONSTRAINT_SPACE_LOCAL, CONSTRAINT_SPACE_PARLOCAL)) { /* call self with slightly different values */ constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to); } @@ -445,69 +438,66 @@ void constraint_mat_convertspace (Object *ob, bPoseChannel *pchan, float mat[][4 break; case CONSTRAINT_SPACE_LOCAL: /* ------------ FROM LOCALSPACE --------- */ { - /* local to pose */ - if (to==CONSTRAINT_SPACE_POSE || to==CONSTRAINT_SPACE_WORLD) { - /* do inverse procedure that was done for pose to local */ - if (pchan->bone) { - /* we need the posespace_matrix = local_matrix + (parent_posespace_matrix + restpos) */ - if (pchan->parent) { - float offs_bone[4][4]; + /* local to pose - do inverse procedure that was done for pose to local */ + if (pchan->bone) { + /* we need the posespace_matrix = local_matrix + (parent_posespace_matrix + restpos) */ + if (pchan->parent) { + float offs_bone[4][4]; + + /* construct offs_bone the same way it is done in armature.c */ + Mat4CpyMat3(offs_bone, pchan->bone->bone_mat); + VECCOPY(offs_bone[3], pchan->bone->head); + offs_bone[3][1]+= pchan->bone->parent->length; + + if (pchan->bone->flag & BONE_HINGE) { + /* pose_mat = par_pose-space_location * chan_mat */ + float tmat[4][4]; - /* construct offs_bone the same way it is done in armature.c */ - Mat4CpyMat3(offs_bone, pchan->bone->bone_mat); - VECCOPY(offs_bone[3], pchan->bone->head); - offs_bone[3][1]+= pchan->bone->parent->length; + /* the rotation of the parent restposition */ + Mat4CpyMat4(tmat, pchan->bone->parent->arm_mat); - if (pchan->bone->flag & BONE_HINGE) { - /* pose_mat = par_pose-space_location * chan_mat */ - float tmat[4][4]; - - /* the rotation of the parent restposition */ - Mat4CpyMat4(tmat, pchan->bone->parent->arm_mat); - - /* the location of actual parent transform */ - VECCOPY(tmat[3], offs_bone[3]); - offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f; - Mat4MulVecfl(pchan->parent->pose_mat, tmat[3]); - - Mat4MulMat4(diff_mat, offs_bone, tmat); - Mat4CpyMat4(tempmat, mat); - Mat4MulMat4(mat, tempmat, diff_mat); - } - else { - /* pose_mat = par_pose_mat * bone_mat * chan_mat */ - Mat4MulMat4(diff_mat, offs_bone, pchan->parent->pose_mat); - Mat4CpyMat4(tempmat, mat); - Mat4MulMat4(mat, tempmat, diff_mat); - } + /* the location of actual parent transform */ + VECCOPY(tmat[3], offs_bone[3]); + offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f; + Mat4MulVecfl(pchan->parent->pose_mat, tmat[3]); + + Mat4MulMat4(diff_mat, offs_bone, tmat); + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, tempmat, diff_mat); } else { - Mat4CpyMat4(diff_mat, pchan->bone->arm_mat); - + /* pose_mat = par_pose_mat * bone_mat * chan_mat */ + Mat4MulMat4(diff_mat, offs_bone, pchan->parent->pose_mat); Mat4CpyMat4(tempmat, mat); Mat4MulMat4(mat, tempmat, diff_mat); } } + else { + Mat4CpyMat4(diff_mat, pchan->bone->arm_mat); + + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, tempmat, diff_mat); + } } - /* local to world */ - if (to == CONSTRAINT_SPACE_WORLD) { + + /* use pose-space as stepping stone for other spaces */ + if (ELEM(to, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_PARLOCAL)) { /* call self with slightly different values */ constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to); - } + } } break; case CONSTRAINT_SPACE_PARLOCAL: /* -------------- FROM LOCAL WITH PARENT ---------- */ { - /* local to pose */ - if (to==CONSTRAINT_SPACE_POSE || to==CONSTRAINT_SPACE_WORLD) { - if (pchan->bone) { - Mat4CpyMat4(diff_mat, pchan->bone->arm_mat); - Mat4CpyMat4(tempmat, mat); - Mat4MulMat4(mat, diff_mat, tempmat); - } + /* local + parent to pose */ + if (pchan->bone) { + Mat4CpyMat4(diff_mat, pchan->bone->arm_mat); + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, diff_mat, tempmat); } - /* local to world */ - if (to == CONSTRAINT_SPACE_WORLD) { + + /* use pose-space as stepping stone for other spaces */ + if (ELEM(to, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL)) { /* call self with slightly different values */ constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to); } @@ -1882,7 +1872,7 @@ static void pycon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintT { bPythonConstraint *data= con->data; - if ((G.f & G_DOSCRIPTLINKS) && VALID_CONS_TARGET(ct)) { + if (VALID_CONS_TARGET(ct)) { /* special exception for curves - depsgraph issues */ if (ct->tar->type == OB_CURVE) { Curve *cu= ct->tar->data; @@ -1896,7 +1886,10 @@ static void pycon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintT * this matrix if it needs to do so */ constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail); - BPY_pyconstraint_target(data, ct); + + /* only execute target calculation if allowed */ + if (G.f & G_DOSCRIPTLINKS) + BPY_pyconstraint_target(data, ct); } else if (ct) Mat4One(ct->matrix); @@ -1906,6 +1899,7 @@ static void pycon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targ { bPythonConstraint *data= con->data; + /* only evaluate in python if we're allowed to do so */ if ((G.f & G_DOSCRIPTLINKS)==0) return; /* currently removed, until I this can be re-implemented for multiple targets */ diff --git a/source/blender/blenkernel/intern/curve.c b/source/blender/blenkernel/intern/curve.c index 396bdda9c10..c29a92558d6 100644 --- a/source/blender/blenkernel/intern/curve.c +++ b/source/blender/blenkernel/intern/curve.c @@ -247,7 +247,7 @@ void tex_space_curve(Curve *cu) { DispList *dl; BoundBox *bb; - float *data, min[3], max[3], loc[3], size[3]; + float *fp, min[3], max[3], loc[3], size[3]; int tot, doit= 0; if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox"); @@ -262,10 +262,10 @@ void tex_space_curve(Curve *cu) else tot= dl->nr*dl->parts; if(tot) doit= 1; - data= dl->verts; + fp= dl->verts; while(tot--) { - DO_MINMAX(data, min, max); - data+= 3; + DO_MINMAX(fp, min, max); + fp += 3; } dl= dl->next; } @@ -650,31 +650,31 @@ static void basisNurb(float t, short order, short pnts, float *knots, float *bas } -void makeNurbfaces(Nurb *nu, float *data, int rowstride) -/* data has to be 3*4*resolu*resolv in size, and zero-ed */ +void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride) +/* coord_array has to be 3*4*resolu*resolv in size, and zero-ed */ { BPoint *bp; float *basisu, *basis, *basisv, *sum, *fp, *in; float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv; int i, j, iofs, jofs, cycl, len, resolu, resolv; int istart, iend, jsta, jen, *jstart, *jend, ratcomp; - + + int totu = nu->pntsu*nu->resolu, totv = nu->pntsv*nu->resolv; + if(nu->knotsu==NULL || nu->knotsv==NULL) return; if(nu->orderu>nu->pntsu) return; if(nu->orderv>nu->pntsv) return; - if(data==0) return; - + if(coord_array==NULL) return; + /* allocate and initialize */ - len= nu->pntsu*nu->pntsv; + len = totu * totv; if(len==0) return; sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1"); - - resolu= nu->resolu; - resolv= nu->resolv; - len= resolu*resolv; + + len= totu*totv; if(len==0) { MEM_freeN(sum); return; @@ -690,12 +690,13 @@ void makeNurbfaces(Nurb *nu, float *data, int rowstride) } bp++; } - + fp= nu->knotsu; ustart= fp[nu->orderu-1]; if(nu->flagu & CU_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1]; else uend= fp[nu->pntsu]; - ustep= (uend-ustart)/(resolu-1+(nu->flagu & CU_CYCLIC)); + ustep= (uend-ustart)/((nu->flagu & CU_CYCLIC) ? totu : totu - 1); + basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3"); fp= nu->knotsv; @@ -703,17 +704,19 @@ void makeNurbfaces(Nurb *nu, float *data, int rowstride) if(nu->flagv & CU_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1]; else vend= fp[nu->pntsv]; - vstep= (vend-vstart)/(resolv-1+(nu->flagv & CU_CYCLIC)); + vstep= (vend-vstart)/((nu->flagv & CU_CYCLIC) ? totv : totv - 1); + len= KNOTSV(nu); - basisv= (float *)MEM_mallocN(sizeof(float)*len*resolv, "makeNurbfaces3"); - jstart= (int *)MEM_mallocN(sizeof(float)*resolv, "makeNurbfaces4"); - jend= (int *)MEM_mallocN(sizeof(float)*resolv, "makeNurbfaces5"); + basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3"); + jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4"); + jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5"); /* precalculation of basisv and jstart,jend */ if(nu->flagv & CU_CYCLIC) cycl= nu->orderv-1; else cycl= 0; v= vstart; basis= basisv; + resolv= totv; while(resolv--) { basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+resolv, jend+resolv); basis+= KNOTSV(nu); @@ -722,14 +725,15 @@ void makeNurbfaces(Nurb *nu, float *data, int rowstride) if(nu->flagu & CU_CYCLIC) cycl= nu->orderu-1; else cycl= 0; - in= data; + in= coord_array; u= ustart; + resolu= totu; while(resolu--) { basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend); basis= basisv; - resolv= nu->resolv; + resolv= totv; while(resolv--) { jsta= jstart[resolv]; @@ -798,7 +802,7 @@ void makeNurbfaces(Nurb *nu, float *data, int rowstride) basis+= KNOTSV(nu); } u+= ustep; - if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*nu->resolv*sizeof(*in))); + if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in))); } /* free */ @@ -809,24 +813,26 @@ void makeNurbfaces(Nurb *nu, float *data, int rowstride) MEM_freeN(jend); } -void makeNurbcurve(Nurb *nu, float *data, int resolu, int dim) -/* data has to be dim*4*pntsu*resolu in size and zero-ed */ +void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, int resolu) +/* coord_array has to be 3*4*pntsu*resolu in size and zero-ed + * tilt_array and radius_array will be written to if valid */ { BPoint *bp; float u, ustart, uend, ustep, sumdiv; - float *basisu, *sum, *fp, *in; + float *basisu, *sum, *fp; + float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array; int i, len, istart, iend, cycl; if(nu->knotsu==NULL) return; if(nu->orderu>nu->pntsu) return; - if(data==0) return; + if(coord_array==0) return; /* allocate and initialize */ len= nu->pntsu; if(len==0) return; sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1"); - - resolu*= nu->pntsu; + + resolu= (resolu*SEGMENTSU(nu))+1; if(resolu==0) { MEM_freeN(sum); return; @@ -836,13 +842,12 @@ void makeNurbcurve(Nurb *nu, float *data, int resolu, int dim) ustart= fp[nu->orderu-1]; if(nu->flagu & CU_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1]; else uend= fp[nu->pntsu]; - ustep= (uend-ustart)/(resolu-1+(nu->flagu & CU_CYCLIC)); + ustep= (uend-ustart)/(resolu-1); basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3"); if(nu->flagu & CU_CYCLIC) cycl= nu->orderu-1; else cycl= 0; - in= data; u= ustart; while(resolu--) { @@ -877,17 +882,24 @@ void makeNurbcurve(Nurb *nu, float *data, int resolu, int dim) if(*fp!=0.0) { - in[0]+= (*fp) * bp->vec[0]; - in[1]+= (*fp) * bp->vec[1]; - if(dim>=3) { - in[2]+= (*fp) * bp->vec[2]; - if(dim==4) in[3]+= (*fp) * bp->alfa; - } + coord_fp[0]+= (*fp) * bp->vec[0]; + coord_fp[1]+= (*fp) * bp->vec[1]; + coord_fp[2]+= (*fp) * bp->vec[2]; + + if (tilt_fp) + (*tilt_fp) += (*fp) * bp->alfa; + + if (radius_fp) + (*radius_fp) += (*fp) * bp->radius; + } } - in+= dim; - + coord_fp+= 3; + + if (tilt_fp) tilt_fp++; + if (radius_fp) radius_fp++; + u+= ustep; } @@ -932,7 +944,7 @@ float *make_orco_surf(Object *ob) Nurb *nu; int a, b, tot=0; int sizeu, sizev; - float *data, *orco; + float *fp, *coord_array; /* first calculate the size of the datablock */ nu= cu->nurb.first; @@ -946,8 +958,8 @@ float *make_orco_surf(Object *ob) See also convertblender.c: init_render_surf() */ - sizeu = nu->resolu; - sizev = nu->resolv; + sizeu = nu->pntsu*nu->resolu; + sizev = nu->pntsv*nu->resolv; if (nu->flagu & CU_CYCLIC) sizeu++; if (nu->flagv & CU_CYCLIC) sizev++; if(nu->pntsv>1) tot+= sizeu * sizev; @@ -955,13 +967,13 @@ float *make_orco_surf(Object *ob) nu= nu->next; } /* makeNurbfaces wants zeros */ - data= orco= MEM_callocN(3*sizeof(float)*tot, "make_orco"); + fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco"); nu= cu->nurb.first; while(nu) { if(nu->pntsv>1) { - sizeu = nu->resolu; - sizev = nu->resolv; + sizeu = nu->pntsu*nu->resolu; + sizev = nu->pntsv*nu->resolv; if (nu->flagu & CU_CYCLIC) sizeu++; if (nu->flagv & CU_CYCLIC) sizev++; @@ -969,20 +981,20 @@ float *make_orco_surf(Object *ob) for(b=0; b< sizeu; b++) { for(a=0; a< sizev; a++) { - if(sizev <2) data[0]= 0.0f; - else data[0]= -1.0f + 2.0f*((float)a)/(sizev - 1); + if(sizev <2) fp[0]= 0.0f; + else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1); - if(sizeu <2) data[1]= 0.0f; - else data[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1); + if(sizeu <2) fp[1]= 0.0f; + else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1); - data[2]= 0.0; + fp[2]= 0.0; - data+= 3; + fp+= 3; } } } else { - float *_tdata= MEM_callocN(nu->resolu*nu->resolv*3*sizeof(float), "temp data"); + float *_tdata= MEM_callocN((nu->pntsu*nu->resolu) * (nu->pntsv*nu->resolv) *3*sizeof(float), "temp data"); float *tdata= _tdata; makeNurbfaces(nu, tdata, 0); @@ -997,12 +1009,12 @@ float *make_orco_surf(Object *ob) if (a==sizev-1 && (nu->flagv & CU_CYCLIC)) use_a= 0; - tdata = _tdata + 3 * (use_b * nu->resolv + use_a); + tdata = _tdata + 3 * (use_b * (nu->pntsv*nu->resolv) + use_a); - data[0]= (tdata[0]-cu->loc[0])/cu->size[0]; - data[1]= (tdata[1]-cu->loc[1])/cu->size[1]; - data[2]= (tdata[2]-cu->loc[2])/cu->size[2]; - data+= 3; + fp[0]= (tdata[0]-cu->loc[0])/cu->size[0]; + fp[1]= (tdata[1]-cu->loc[1])/cu->size[1]; + fp[2]= (tdata[2]-cu->loc[2])/cu->size[2]; + fp+= 3; } } @@ -1012,7 +1024,7 @@ float *make_orco_surf(Object *ob) nu= nu->next; } - return orco; + return coord_array; } @@ -1024,7 +1036,7 @@ float *make_orco_curve(Object *ob) Curve *cu = ob->data; DispList *dl; int u, v, numVerts; - float *fp, *orco; + float *fp, *coord_array; int remakeDisp = 0; if (!(cu->flag&CU_UV_ORCO) && cu->key && cu->key->refkey) { @@ -1052,7 +1064,7 @@ float *make_orco_curve(Object *ob) } } - fp= orco= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco"); + fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco"); for (dl=cu->disp.first; dl; dl=dl->next) { if (dl->type==DL_INDEX3) { for (u=0; u<dl->nr; u++, fp+=3) { @@ -1102,7 +1114,7 @@ float *make_orco_curve(Object *ob) makeDispListCurveTypes(ob, 0); } - return orco; + return coord_array; } @@ -1429,7 +1441,7 @@ static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *si } -static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *data_a, int resolu) +static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, int resolu) { BezTriple *pprev, *next, *last; float fac, dfac, t[4]; @@ -1455,10 +1467,30 @@ static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float * dfac= 1.0f/(float)resolu; for(a=0; a<resolu; a++, fac+= dfac) { + if (tilt_array) { + if (nu->tilt_interp==3) { /* May as well support for tilt also 2.47 ease interp */ + tilt_array[a] = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac); + } else { + set_four_ipo(fac, t, nu->tilt_interp); + tilt_array[a]= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa; + } + } - set_four_ipo(fac, t, nu->tilt_interp); - - data_a[a]= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa; + if (radius_array) { + if (nu->radius_interp==3) { + /* Support 2.47 ease interp + * Note! - this only takes the 2 points into account, + * giving much more localized results to changes in radius, sometimes you want that */ + radius_array[a] = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac); + } else { + + /* reuse interpolation from tilt if we can */ + if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) { + set_four_ipo(fac, t, nu->radius_interp); + } + radius_array[a]= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius; + } + } } } @@ -1476,13 +1508,19 @@ void makeBevelList(Object *ob) BPoint *bp; BevList *bl, *blnew, *blnext; BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0; - float *data, *data_a, *v1, *v2, min, inp, x1, x2, y1, y2, vec[3]; + float min, inp, x1, x2, y1, y2, vec[3]; + float *coord_array, *tilt_array=NULL, *radius_array=NULL, *coord_fp, *tilt_fp=NULL, *radius_fp=NULL; + float *v1, *v2; struct bevelsort *sortdata, *sd, *sd1; int a, b, nr, poly, resolu, len=0; - + int do_tilt, do_radius; + /* this function needs an object, because of tflag and upflag */ cu= ob->data; + /* do we need to calculate the radius for each point? */ + /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */ + /* STEP 1: MAKE POLYS */ BLI_freelistN(&(cu->bev)); @@ -1490,10 +1528,15 @@ void makeBevelList(Object *ob) else nu= cu->nurb.first; while(nu) { + + /* check if we will calculate tilt data */ + do_tilt = ((nu->type & CU_2D) && (cu->flag & CU_3D)==0) ? 0 : 1; + do_radius = (do_tilt || cu->bevobj) ? 1 : 0; /* normal display uses the radius, better just to calculate them */ + /* check we are a single point? also check we are not a surface and that the orderu is sane, * enforced in the UI but can go wrong possibly */ if(!check_valid_nurb_u(nu)) { - bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList"); + bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1"); BLI_addtail(&(cu->bev), bl); bl->nr= 0; } else { @@ -1504,7 +1547,7 @@ void makeBevelList(Object *ob) if((nu->type & 7)==CU_POLY) { len= nu->pntsu; - bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList"); + bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2"); BLI_addtail(&(cu->bev), bl); if(nu->flagu & CU_CYCLIC) bl->poly= 0; @@ -1519,7 +1562,8 @@ void makeBevelList(Object *ob) bevp->y= bp->vec[1]; bevp->z= bp->vec[2]; bevp->alfa= bp->alfa; - bevp->f1= 1; + bevp->radius= bp->radius; + bevp->f1= SELECT; bevp++; bp++; } @@ -1527,7 +1571,7 @@ void makeBevelList(Object *ob) else if((nu->type & 7)==CU_BEZIER) { len= resolu*(nu->pntsu+ (nu->flagu & CU_CYCLIC) -1)+1; /* in case last point is not cyclic */ - bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList"); + bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints"); BLI_addtail(&(cu->bev), bl); if(nu->flagu & CU_CYCLIC) bl->poly= 0; @@ -1545,8 +1589,13 @@ void makeBevelList(Object *ob) bezt++; } - data= MEM_mallocN(3*sizeof(float)*(resolu+1), "makeBevelList2"); - data_a= MEM_callocN(sizeof(float)*(resolu+1), "data_a"); + coord_array= coord_fp= MEM_mallocN(3*sizeof(float)*(resolu+1), "makeBevelCoords"); + + if(do_tilt) + tilt_array= tilt_fp= MEM_callocN(sizeof(float)*(resolu+1), "makeBevelTilt"); + + if (do_radius) + radius_array= radius_fp= MEM_callocN(sizeof(float)*(resolu+1), "nakeBevelRadius"); while(a--) { if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) { @@ -1555,6 +1604,7 @@ void makeBevelList(Object *ob) bevp->y= prevbezt->vec[1][1]; bevp->z= prevbezt->vec[1][2]; bevp->alfa= prevbezt->alfa; + bevp->radius= prevbezt->radius; bevp->f1= SELECT; bevp->f2= 0; bevp++; @@ -1566,38 +1616,44 @@ void makeBevelList(Object *ob) v2= bezt->vec[0]; /* always do all three, to prevent data hanging around */ - forward_diff_bezier(v1[0], v1[3], v2[0], v2[3], data, resolu, 3); - forward_diff_bezier(v1[1], v1[4], v2[1], v2[4], data+1, resolu, 3); - forward_diff_bezier(v1[2], v1[5], v2[2], v2[5], data+2, resolu, 3); - - if((nu->type & CU_2D)==0) { - if(cu->flag & CU_3D) { - alfa_bezpart(prevbezt, bezt, nu, data_a, resolu); - } - } + forward_diff_bezier(v1[0], v1[3], v2[0], v2[3], coord_array, resolu, 3); + forward_diff_bezier(v1[1], v1[4], v2[1], v2[4], coord_array+1, resolu, 3); + forward_diff_bezier(v1[2], v1[5], v2[2], v2[5], coord_array+2, resolu, 3); + if (do_tilt || do_radius) + alfa_bezpart(prevbezt, bezt, nu, tilt_array, radius_array, resolu); /* indicate with handlecodes double points */ if(prevbezt->h1==prevbezt->h2) { - if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->f1= 1; + if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->f1= SELECT; } else { - if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->f1= 1; - else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->f1= 1; + if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->f1= SELECT; + else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->f1= SELECT; } - v1= data; - v2= data_a; nr= resolu; + coord_fp = coord_array; + tilt_fp = tilt_array; + radius_fp = radius_array; + while(nr--) { - bevp->x= v1[0]; - bevp->y= v1[1]; - bevp->z= v1[2]; - bevp->alfa= v2[0]; + bevp->x= coord_fp[0]; + bevp->y= coord_fp[1]; + bevp->z= coord_fp[2]; + coord_fp+=3; + + if (do_tilt) { + bevp->alfa= *tilt_fp; + tilt_fp++; + } + + if (do_radius) { + bevp->radius= *radius_fp; + radius_fp++; + } bevp++; - v1+=3; - v2++; } bl->nr+= resolu; @@ -1606,22 +1662,24 @@ void makeBevelList(Object *ob) bezt++; } - MEM_freeN(data); - MEM_freeN(data_a); + MEM_freeN(coord_array); + if (do_tilt) MEM_freeN(tilt_array); + if (do_radius) MEM_freeN(radius_array); + coord_array = tilt_array = radius_array = NULL; if((nu->flagu & CU_CYCLIC)==0) { /* not cyclic: endpoint */ bevp->x= prevbezt->vec[1][0]; bevp->y= prevbezt->vec[1][1]; bevp->z= prevbezt->vec[1][2]; bevp->alfa= prevbezt->alfa; + bevp->radius= prevbezt->radius; bl->nr++; } - } else if((nu->type & 7)==CU_NURBS) { if(nu->pntsv==1) { - len= resolu*nu->pntsu; - bl= MEM_mallocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3"); + len= (resolu*SEGMENTSU(nu))+1; + bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3"); BLI_addtail(&(cu->bev), bl); bl->nr= len; bl->flag= 0; @@ -1629,21 +1687,40 @@ void makeBevelList(Object *ob) else bl->poly= -1; bevp= (BevPoint *)(bl+1); - data= MEM_callocN(4*sizeof(float)*len, "makeBevelList4"); /* has to be zero-ed */ - makeNurbcurve(nu, data, resolu, 4); + coord_array= coord_fp= MEM_callocN(3*sizeof(float)*len, "makeBevelCoords"); /* has to be zero-ed */ + + if(do_tilt) + tilt_array= tilt_fp= MEM_callocN(sizeof(float)*len, "makeBevelTilt"); + + if (do_radius) + radius_array= radius_fp= MEM_callocN(sizeof(float)*len, "nakeBevelRadius"); + + makeNurbcurve(nu, coord_array, tilt_array, radius_array, resolu); - v1= data; while(len--) { - bevp->x= v1[0]; - bevp->y= v1[1]; - bevp->z= v1[2]; - bevp->alfa= v1[3]; + bevp->x= coord_fp[0]; + bevp->y= coord_fp[1]; + bevp->z= coord_fp[2]; + coord_fp+=3; + + if (do_tilt) { + bevp->alfa= *tilt_fp; + tilt_fp++; + } + + if (do_radius) { + bevp->radius= *radius_fp; + radius_fp++; + } + bevp->f1= bevp->f2= 0; bevp++; - v1+=4; } - MEM_freeN(data); + MEM_freeN(coord_array); + if (do_tilt) MEM_freeN(tilt_array); + if (do_radius) MEM_freeN(radius_array); + coord_array = tilt_array = radius_array = NULL; } } } @@ -1678,7 +1755,7 @@ void makeBevelList(Object *ob) blnext= bl->next; if(bl->nr && bl->flag) { nr= bl->nr- bl->flag+1; /* +1 because vectorbezier sets flag too */ - blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList"); + blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4"); memcpy(blnew, bl, sizeof(BevList)); blnew->nr= 0; BLI_remlink(&(cu->bev), bl); @@ -1886,137 +1963,6 @@ void makeBevelList(Object *ob) } } -/* calculates a bevel width (radius) for a particular subdivided curve part, - * based on the radius value of the surrounding CVs */ -float calc_curve_subdiv_radius(Curve *cu, Nurb *nu, int cursubdiv) -{ - BezTriple *bezt, *beztfirst, *beztlast, *beztnext, *beztprev; - BPoint *bp, *bpfirst, *bplast; - int resolu; - float prevrad=0.0, nextrad=0.0, rad=0.0, ratio=0.0; - int vectseg=0, subdivs=0; - - if((nu==NULL) || (nu->pntsu<=1)) return 1.0; - bezt= nu->bezt; - bp = nu->bp; - - if(G.rendering && cu->resolu_ren!=0) resolu= cu->resolu_ren; - else resolu= nu->resolu; - - if(((nu->type & 7)==CU_BEZIER) && (bezt != NULL)) { - beztfirst = nu->bezt; - beztlast = nu->bezt + (nu->pntsu - 1); - - /* loop through the CVs to end up with a pointer to the CV before the subdiv in question, and a ratio - * of how far that subdiv is between this CV and the next */ - while(bezt<=beztlast) { - beztnext = bezt+1; - beztprev = bezt-1; - vectseg=0; - - if (subdivs==cursubdiv) { - ratio= 0.0; - break; - } - - /* check to see if we're looking at a vector segment (no subdivisions) */ - if (nu->flagu & CU_CYCLIC) { - if (bezt == beztfirst) { - if ((beztlast->h2==HD_VECT) && (bezt->h1==HD_VECT)) vectseg = 1; - } else { - if ((beztprev->h2==HD_VECT) && (bezt->h1==HD_VECT)) vectseg = 1; - } - } else if ((bezt->h2==HD_VECT) && (beztnext->h1==HD_VECT)) vectseg = 1; - - - if (vectseg==0) { - /* if it's NOT a vector segment, check to see if the subdiv falls within the segment */ - subdivs += resolu; - - if (cursubdiv < subdivs) { - ratio = 1.0 - ((subdivs - cursubdiv)/(float)resolu); - break; - } - } else { - /* must be a vector segment.. loop again! */ - subdivs += 1; - } - - bezt++; - } - - /* Now we have a nice bezt pointer to the CV that we want. But cyclic messes it up, so must correct for that.. - * (cyclic goes last-> first -> first+1 -> first+2 -> ...) */ - if (nu->flagu & CU_CYCLIC) { - if (bezt == beztfirst) bezt = beztlast; - else bezt--; - } - - /* find the radii at the bounding CVs and interpolate between them based on ratio */ - rad = prevrad = bezt->radius; - - if ((bezt == beztlast) && (nu->flagu & CU_CYCLIC)) { /* loop around */ - bezt= beztfirst; - } else if (bezt != beztlast) { - bezt++; - } - nextrad = bezt->radius; - - } - else if( ( ((nu->type & 7)==CU_NURBS) || ((nu->type & 7)==CU_POLY)) && (bp != NULL)) { - /* follows similar algo as for bezt above */ - bpfirst = nu->bp; - bplast = nu->bp + (nu->pntsu - 1); - - if ((nu->type & 7)==CU_POLY) resolu=1; - - while(bp<=bplast) { - if (subdivs==cursubdiv) { - ratio= 0.0; - break; - } - - subdivs += resolu; - - if (cursubdiv < subdivs) { - ratio = 1.0 - ((subdivs - cursubdiv)/(float)resolu); - break; - } - - bp++; - } - - if ( ((nu->type & 7)==CU_NURBS) && (nu->flagu & CU_CYCLIC)) { - if (bp >= bplast) bp = bpfirst; - else bp++; - } else if ( bp > bplast ) { - /* this can happen in rare cases, refer to bug [#8596] */ - bp = bplast; - } - - rad = prevrad = bp->radius; - - if ((bp == bplast) && (nu->flagu & CU_CYCLIC)) { /* loop around */ - bp= bpfirst; - } else if (bp < bplast) { - bp++; - } - nextrad = bp->radius; - - } - - - if (nextrad != prevrad) { - /* smooth interpolation */ - rad = prevrad + (nextrad-prevrad)*(3.0f*ratio*ratio - 2.0f*ratio*ratio*ratio); - } - - if (rad > 0.0) - return rad; - else - return 1.0; -} - /* ****************** HANDLES ************** */ /* @@ -2385,9 +2331,9 @@ void sethandlesNurb(short code) bezt= nu->bezt; a= nu->pntsu; while(a--) { - if(bezt->f1 || bezt->f3) { - if(bezt->f1) bezt->h1= code; - if(bezt->f3) bezt->h2= code; + if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) { + if(bezt->f1 & SELECT) bezt->h1= code; + if(bezt->f3 & SELECT) bezt->h2= code; if(bezt->h1!=bezt->h2) { if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE; if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE; @@ -2415,8 +2361,8 @@ void sethandlesNurb(short code) bezt= nu->bezt; a= nu->pntsu; while(a--) { - if(bezt->f1 && bezt->h1) ok= 1; - if(bezt->f3 && bezt->h2) ok= 1; + if((bezt->f1 & SELECT) && bezt->h1) ok= 1; + if((bezt->f3 & SELECT) && bezt->h2) ok= 1; if(ok) break; bezt++; } @@ -2432,8 +2378,8 @@ void sethandlesNurb(short code) bezt= nu->bezt; a= nu->pntsu; while(a--) { - if(bezt->f1) bezt->h1= ok; - if(bezt->f3 ) bezt->h2= ok; + if(bezt->f1 & SELECT) bezt->h1= ok; + if(bezt->f3 & SELECT) bezt->h2= ok; bezt++; } diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c index 2c1f6bb84c1..e93266c85f3 100644 --- a/source/blender/blenkernel/intern/customdata.c +++ b/source/blender/blenkernel/intern/customdata.c @@ -265,9 +265,9 @@ static void layerSwap_tface(void *data, int *corner_indices) { MTFace *tf = data; float uv[4][2]; - const static short pin_flags[4] = + static const short pin_flags[4] = { TF_PIN1, TF_PIN2, TF_PIN3, TF_PIN4 }; - const static char sel_flags[4] = + static const char sel_flags[4] = { TF_SEL1, TF_SEL2, TF_SEL3, TF_SEL4 }; short unwrap = tf->unwrap & ~(TF_PIN1 | TF_PIN2 | TF_PIN3 | TF_PIN4); char flag = tf->flag & ~(TF_SEL1 | TF_SEL2 | TF_SEL3 | TF_SEL4); @@ -926,12 +926,9 @@ void *CustomData_add_layer_named(CustomData *data, int type, int alloctype, int CustomData_free_layer(CustomData *data, int type, int totelem, int index) { int i; - CustomDataLayer *layer; if (index < 0) return 0; - layer = &data->layers[index]; - customData_free_layer__internal(&data->layers[index], totelem); for (i=index+1; i < data->totlayer; ++i) diff --git a/source/blender/blenkernel/intern/deform.c b/source/blender/blenkernel/intern/deform.c index ab53571b62d..3143c5e4df2 100644 --- a/source/blender/blenkernel/intern/deform.c +++ b/source/blender/blenkernel/intern/deform.c @@ -220,3 +220,31 @@ void unique_vertexgroup_name (bDeformGroup *dg, Object *ob) } } } + +float deformvert_get_weight(const struct MDeformVert *dvert, int group_num) +{ + if(dvert) + { + const MDeformWeight *dw = dvert->dw; + int i; + + for(i=dvert->totweight; i>0; i--, dw++) + if(dw->def_nr == group_num) + return dw->weight; + } + + /* Not found */ + return 0.0; +} + +float vertexgroup_get_vertex_weight(const struct MDeformVert *dvert, int index, int group_num) +{ + if(group_num == -1) + return 1.0; + + if(dvert == 0) + return 0.0; + + return deformvert_get_weight(dvert+index, group_num); +} + diff --git a/source/blender/blenkernel/intern/depsgraph.c b/source/blender/blenkernel/intern/depsgraph.c index 80f450000bb..7f4910a9765 100644 --- a/source/blender/blenkernel/intern/depsgraph.c +++ b/source/blender/blenkernel/intern/depsgraph.c @@ -581,9 +581,12 @@ static void build_dag_object(DagForest *dag, DagNode *scenenode, Object *ob, int for(; psys; psys=psys->next) { ParticleSettings *part= psys->part; - + dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation"); + if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE) + continue; + if(part->phystype==PART_PHYS_KEYED && psys->keyed_ob && BLI_findlink(&psys->keyed_ob->particlesystem,psys->keyed_psys-1)) { node2 = dag_get_node(dag, psys->keyed_ob); @@ -862,12 +865,12 @@ DagNode * dag_get_sub_node (DagForest *forest,void * fob) return node; } -void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name) +static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name) { - DagAdjList *itA = fob1->child; + DagAdjList *itA = fob2->parent; while (itA) { /* search if relation exist already */ - if (itA->node == fob2) { + if (itA->node == fob1) { itA->type |= rel; itA->count += 1; return; @@ -876,20 +879,23 @@ void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel } /* create new relation and insert at head. MALLOC alert! */ itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list"); - itA->node = fob2; + itA->node = fob1; itA->type = rel; itA->count = 1; - itA->next = fob1->child; + itA->next = fob2->parent; itA->name = name; - fob1->child = itA; + fob2->parent = itA; } -static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name) +void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name) { - DagAdjList *itA = fob2->parent; + DagAdjList *itA = fob1->child; + /* parent relation is for cycle checking */ + dag_add_parent_relation(forest, fob1, fob2, rel, name); + while (itA) { /* search if relation exist already */ - if (itA->node == fob1) { + if (itA->node == fob2) { itA->type |= rel; itA->count += 1; return; @@ -898,12 +904,12 @@ static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *f } /* create new relation and insert at head. MALLOC alert! */ itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list"); - itA->node = fob1; + itA->node = fob2; itA->type = rel; itA->count = 1; - itA->next = fob2->parent; + itA->next = fob1->child; itA->name = name; - fob2->parent = itA; + fob1->child = itA; } static char *dag_node_name(DagNode *node) @@ -963,6 +969,63 @@ static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, printf("\n"); } +static int dag_node_recurs_level(DagNode *node, int level) +{ + DagAdjList *itA; + int newlevel; + + node->color= DAG_BLACK; /* done */ + newlevel= ++level; + + for(itA= node->parent; itA; itA= itA->next) { + if(itA->node->color==DAG_WHITE) { + itA->node->ancestor_count= dag_node_recurs_level(itA->node, level); + newlevel= MAX2(newlevel, level+itA->node->ancestor_count); + } + else + newlevel= MAX2(newlevel, level+itA->node->ancestor_count); + } + + return newlevel; +} + +static void dag_check_cycle(DagForest *dag) +{ + DagNode *node; + DagAdjList *itA; + + /* tag nodes unchecked */ + for(node = dag->DagNode.first; node; node= node->next) + node->color= DAG_WHITE; + + for(node = dag->DagNode.first; node; node= node->next) { + if(node->color==DAG_WHITE) { + node->ancestor_count= dag_node_recurs_level(node, 0); + } + } + + /* check relations, and print errors */ + for(node = dag->DagNode.first; node; node= node->next) { + for(itA= node->parent; itA; itA= itA->next) { + if(itA->node->ancestor_count > node->ancestor_count) { + if(node->ob && itA->node->ob) { + printf("Dependency cycle detected:\n"); + dag_node_print_dependency_cycle(dag, itA->node, node, itA->name); + } + } + } + } + + /* parent relations are only needed for cycle checking, so free now */ + for(node = dag->DagNode.first; node; node= node->next) { + while (node->parent) { + itA = node->parent->next; + MEM_freeN(node->parent); + node->parent = itA; + } + } +} + /* * MainDAG is the DAG of all objects in current scene * used only for drawing there is one also in each scene @@ -1600,6 +1663,8 @@ void DAG_scene_sort(struct Scene *sce) build_dag(sce, DAG_RL_ALL_BUT_DATA); + dag_check_cycle(sce->theDag); + nqueue = queue_create(DAGQUEUEALLOC); for(node = sce->theDag->DagNode.first; node; node= node->next) { @@ -1924,8 +1989,10 @@ static void dag_object_time_update_flags(Object *ob) } } } - else if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB; - else if(ob->parent) { + + if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB; + + if(ob->parent) { /* motion path or bone child */ if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB; } @@ -1946,10 +2013,11 @@ static void dag_object_time_update_flags(Object *ob) } } } - else if(modifiers_isSoftbodyEnabled(ob)) ob->recalc |= OB_RECALC_DATA; - else if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA; - else if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA; - else { + + if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA; + if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA; + + { Mesh *me; Curve *cu; Lattice *lt; @@ -1963,12 +2031,6 @@ static void dag_object_time_update_flags(Object *ob) ob->shapeflag &= ~OB_SHAPE_TEMPLOCK; } } - if((ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) && (ob->fluidsimSettings)) { - // fluidsimSettings might not be initialized during load... - if(ob->fluidsimSettings->type & (OB_FLUIDSIM_DOMAIN|OB_FLUIDSIM_PARTICLE)) { - ob->recalc |= OB_RECALC_DATA; // NT FSPARTICLE - } - } if(ob->particlesystem.first) ob->recalc |= OB_RECALC_DATA; break; @@ -2206,57 +2268,6 @@ void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay) /* ******************* DAG FOR ARMATURE POSE ***************** */ -static int node_recurs_level(DagNode *node, int level) -{ - DagAdjList *itA; - int newlevel; - - node->color= DAG_BLACK; /* done */ - newlevel= ++level; - - for(itA= node->parent; itA; itA= itA->next) { - if(itA->node->color==DAG_WHITE) { - itA->node->ancestor_count= node_recurs_level(itA->node, level); - newlevel= MAX2(newlevel, level+itA->node->ancestor_count); - } - else - newlevel= MAX2(newlevel, level+itA->node->ancestor_count); - } - - return newlevel; -} - -static void pose_check_cycle(DagForest *dag) -{ - DagNode *node; - DagAdjList *itA; - - /* tag nodes unchecked */ - for(node = dag->DagNode.first; node; node= node->next) - node->color= DAG_WHITE; - - for(node = dag->DagNode.first; node; node= node->next) { - if(node->color==DAG_WHITE) { - node->ancestor_count= node_recurs_level(node, 0); - } - } - - /* check relations, and print errors */ - for(node = dag->DagNode.first; node; node= node->next) { - for(itA= node->parent; itA; itA= itA->next) { - if(itA->node->ancestor_count > node->ancestor_count) { - bPoseChannel *pchan= (bPoseChannel *)node->ob; - bPoseChannel *parchan= (bPoseChannel *)itA->node->ob; - - if(pchan && parchan) { - printf("Cycle detected:\n"); - dag_node_print_dependency_cycle(dag, itA->node, node, itA->name); - } - } - } - } -} - /* we assume its an armature with pose */ void DAG_pose_sort(Object *ob) { @@ -2286,7 +2297,6 @@ void DAG_pose_sort(Object *ob) if(pchan->parent) { node2 = dag_get_node(dag, pchan->parent); dag_add_relation(dag, node2, node, 0, "Parent Relation"); - dag_add_parent_relation(dag, node2, node, 0, "Parent Relation"); addtoroot = 0; } for (con = pchan->constraints.first; con; con=con->next) { @@ -2305,7 +2315,6 @@ void DAG_pose_sort(Object *ob) if(target) { node2 = dag_get_node(dag, target); dag_add_relation(dag, node2, node, 0, "Ipo Driver"); - dag_add_parent_relation(dag, node2, node, 0, "Ipo Driver"); /* uncommented this line, results in dependencies * not being added properly for this constraint, @@ -2325,7 +2334,6 @@ void DAG_pose_sort(Object *ob) if (target) { node2= dag_get_node(dag, target); dag_add_relation(dag, node2, node, 0, "IK Constraint"); - dag_add_parent_relation(dag, node2, node, 0, "IK Constraint"); if (con->type==CONSTRAINT_TYPE_KINEMATIC) { bKinematicConstraint *data = (bKinematicConstraint *)con->data; @@ -2342,7 +2350,6 @@ void DAG_pose_sort(Object *ob) while (parchan) { node3= dag_get_node(dag, parchan); dag_add_relation(dag, node2, node3, 0, "IK Constraint"); - dag_add_parent_relation(dag, node2, node3, 0, "IK Constraint"); segcount++; if (segcount==data->rootbone || segcount>255) break; // 255 is weak @@ -2359,11 +2366,10 @@ void DAG_pose_sort(Object *ob) } if (addtoroot == 1 ) { dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation"); - dag_add_parent_relation(dag, rootnode, node, 0, "Root Bone Relation"); } } - pose_check_cycle(dag); + dag_check_cycle(dag); /* now we try to sort... */ tempbase.first= tempbase.last= NULL; diff --git a/source/blender/blenkernel/intern/displist.c b/source/blender/blenkernel/intern/displist.c index 298e4b81d5b..be24d1d2474 100644 --- a/source/blender/blenkernel/intern/displist.c +++ b/source/blender/blenkernel/intern/displist.c @@ -88,6 +88,8 @@ #include "RE_pipeline.h" #include "RE_shader_ext.h" +#include "BLO_sys_types.h" // for intptr_t support + static void boundbox_displist(Object *ob); @@ -755,7 +757,10 @@ void reshadeall_displist(void) for(base= G.scene->base.first; base; base= base->next) { ob= base->object; - freedisplist(&ob->disp); + + if(ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) + freedisplist(&ob->disp); + if(base->lay & G.scene->lay) { /* Metaballs have standard displist at the Object */ if(ob->type==OB_MBALL) shadeDispList(base); @@ -853,7 +858,8 @@ static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase) } } else if((nu->type & 7)==CU_NURBS) { - len= nu->pntsu*resolu; + len= (resolu*SEGMENTSU(nu))+1; + dl= MEM_callocN(sizeof(DispList), "makeDispListsurf"); dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts"); BLI_addtail(dispbase, dl); @@ -865,7 +871,7 @@ static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase) data= dl->verts; if(nu->flagu & CU_CYCLIC) dl->type= DL_POLY; else dl->type= DL_SEGM; - makeNurbcurve(nu, data, resolu, 3); + makeNurbcurve(nu, data, NULL, NULL, resolu); } else if((nu->type & 7)==CU_POLY) { len= nu->pntsu; @@ -986,9 +992,9 @@ void filldisplist(ListBase *dispbase, ListBase *to) efa= fillfacebase.first; index= dlnew->index; while(efa) { - index[0]= (long)efa->v1->tmp.l; - index[1]= (long)efa->v2->tmp.l; - index[2]= (long)efa->v3->tmp.l; + index[0]= (intptr_t)efa->v1->tmp.l; + index[1]= (intptr_t)efa->v2->tmp.l; + index[2]= (intptr_t)efa->v3->tmp.l; index+= 3; efa= efa->next; @@ -1228,7 +1234,7 @@ void curve_calc_modifiers_pre(Object *ob, ListBase *nurb, int forRender, float ( *numVerts_r = numVerts; } -void curve_calc_modifiers_post(Object *ob, ListBase *nurb, ListBase *dispbase, int forRender, float (*originalVerts)[3], float (*deformedVerts)[3]) +static void curve_calc_modifiers_post(Object *ob, ListBase *nurb, ListBase *dispbase, int forRender, float (*originalVerts)[3], float (*deformedVerts)[3]) { int editmode = (!forRender && ob==G.obedit); ModifierData *md = modifiers_getVirtualModifierList(ob); @@ -1317,7 +1323,7 @@ void makeDispListSurf(Object *ob, ListBase *dispbase, int forRender) for (nu=nubase->first; nu; nu=nu->next) { if(forRender || nu->hide==0) { if(nu->pntsv==1) { - len= nu->pntsu*nu->resolu; + len= nu->resolu*SEGMENTSU(nu)+1; dl= MEM_callocN(sizeof(DispList), "makeDispListsurf"); dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts"); @@ -1333,10 +1339,10 @@ void makeDispListSurf(Object *ob, ListBase *dispbase, int forRender) if(nu->flagu & CU_CYCLIC) dl->type= DL_POLY; else dl->type= DL_SEGM; - makeNurbcurve(nu, data, nu->resolu, 3); + makeNurbcurve(nu, data, NULL, NULL, nu->resolu); } else { - len= nu->resolu*nu->resolv; + len= (nu->pntsu*nu->resolu) * (nu->pntsv*nu->resolv); dl= MEM_callocN(sizeof(DispList), "makeDispListsurf"); dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts"); @@ -1348,9 +1354,9 @@ void makeDispListSurf(Object *ob, ListBase *dispbase, int forRender) data= dl->verts; dl->type= DL_SURF; - - dl->parts= nu->resolu; /* in reverse, because makeNurbfaces works that way */ - dl->nr= nu->resolv; + + dl->parts= (nu->pntsu*nu->resolu); /* in reverse, because makeNurbfaces works that way */ + dl->nr= (nu->pntsv*nu->resolv); if(nu->flagv & CU_CYCLIC) dl->flag|= DL_CYCL_U; /* reverse too! */ if(nu->flagu & CU_CYCLIC) dl->flag|= DL_CYCL_V; @@ -1480,7 +1486,7 @@ void makeDispListCurveTypes(Object *ob, int forOrco) float fac=1.0; if (cu->taperobj==NULL) { if ( (cu->bevobj!=NULL) || !((cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ) - fac = calc_curve_subdiv_radius(cu, nu, a); + fac = bevp->radius; } else { fac = calc_taper(cu->taperobj, a, bl->nr); } diff --git a/source/blender/blenkernel/intern/effect.c b/source/blender/blenkernel/intern/effect.c index 4588ef800e1..122c6c71a6c 100644 --- a/source/blender/blenkernel/intern/effect.c +++ b/source/blender/blenkernel/intern/effect.c @@ -59,6 +59,7 @@ #include "BKE_armature.h" #include "BKE_bad_level_calls.h" #include "BKE_blender.h" +#include "BKE_collision.h" #include "BKE_constraint.h" #include "BKE_deform.h" #include "BKE_depsgraph.h" @@ -157,6 +158,13 @@ static void add_to_effectorcache(ListBase *lb, Object *ob, Object *obsrc) } } else if(pd->forcefield) { + + if(pd->forcefield == PFIELD_WIND) + { + pd->rng = rng_new(1); + rng_srandom(pd->rng, (unsigned int)(ceil(PIL_check_seconds_timer()))); // use better seed + } + ec= MEM_callocN(sizeof(pEffectorCache), "effector cache"); ec->ob= ob; BLI_addtail(lb, ec); @@ -205,13 +213,288 @@ void pdEndEffectors(ListBase *lb) pEffectorCache *ec; /* restore full copy */ for(ec= lb->first; ec; ec= ec->next) + { + if(ec->ob->pd && (ec->ob->pd->forcefield == PFIELD_WIND)) + rng_free(ec->ob->pd->rng); + *(ec->ob)= ec->obcopy; + } BLI_freelistN(lb); } } +/************************************************/ +/* Effectors */ +/************************************************/ + +// triangle - ray callback function +static void eff_tri_ray_hit(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit) +{ + // whenever we hit a bounding box, we don't check further + hit->dist = -1; + hit->index = 1; +} + +// get visibility of a wind ray +static float eff_calc_visibility(Object *ob, float *co, float *dir) +{ + CollisionModifierData **collobjs = NULL; + int numcollobj = 0, i; + float norm[3], len = 0.0; + float visibility = 1.0; + + collobjs = get_collisionobjects(ob, &numcollobj); + + if(!collobjs) + return 0; + + VECCOPY(norm, dir); + VecMulf(norm, -1.0); + len = Normalize(norm); + + // check all collision objects + for(i = 0; i < numcollobj; i++) + { + CollisionModifierData *collmd = collobjs[i]; + + if(collmd->bvhtree) + { + BVHTreeRayHit hit; + + hit.index = -1; + hit.dist = len + FLT_EPSILON; + + // check if the way is blocked + if(BLI_bvhtree_ray_cast(collmd->bvhtree, co, norm, 0.0f, &hit, eff_tri_ray_hit, NULL)>=0) + { + // visibility is only between 0 and 1, calculated from 1-absorption + visibility *= MAX2(0.0, MIN2(1.0, (1.0-((float)collmd->absorption)*0.01))); + + if(visibility <= 0.0f) + break; + } + } + } + + MEM_freeN(collobjs); + + return visibility; +} + +// noise function for wind e.g. +static float wind_func(struct RNG *rng, float strength) +{ + int random = (rng_getInt(rng)+1) % 65535; // max 2357 + float force = rng_getFloat(rng) + 1.0f; + float ret; + float sign = 0; + + sign = (random > 32000.0) ? 1.0: -1.0; // dividing by 2 is not giving equal sign distribution + + ret = sign*((float)random / force)*strength/65535.0f; + + return ret; +} + + +static float falloff_func(float fac, int usemin, float mindist, int usemax, float maxdist, float power) +{ + if(!usemin) + mindist= 0.0f; + + if(fac < mindist) { + return 1.0f; + } + else if(usemax) { + if(fac>maxdist || (maxdist-mindist)<=0.0f) + return 0.0f; + + fac= (fac-mindist)/(maxdist-mindist); + return 1.0f - (float)pow((double)fac, (double)power); + } + else + return pow((double)1.0f+fac-mindist, (double)-power); +} + +static float falloff_func_dist(PartDeflect *pd, float fac) +{ + return falloff_func(fac, pd->flag&PFIELD_USEMIN, pd->mindist, pd->flag&PFIELD_USEMAX, pd->maxdist, pd->f_power); +} + +static float falloff_func_rad(PartDeflect *pd, float fac) +{ + return falloff_func(fac, pd->flag&PFIELD_USEMINR, pd->minrad, pd->flag&PFIELD_USEMAXR, pd->maxrad, pd->f_power_r); +} + +float effector_falloff(PartDeflect *pd, float *eff_velocity, float *vec_to_part) +{ + float eff_dir[3], temp[3]; + float falloff=1.0, fac, r_fac; + + if(pd->forcefield==PFIELD_LENNARDJ) + return falloff; /* Lennard-Jones field has it's own falloff built in */ + + VecCopyf(eff_dir,eff_velocity); + Normalize(eff_dir); + + if(pd->flag & PFIELD_POSZ && Inpf(eff_dir,vec_to_part)<0.0f) + falloff=0.0f; + else switch(pd->falloff){ + case PFIELD_FALL_SPHERE: + fac=VecLength(vec_to_part); + falloff= falloff_func_dist(pd, fac); + break; + + case PFIELD_FALL_TUBE: + fac=Inpf(vec_to_part,eff_dir); + falloff= falloff_func_dist(pd, ABS(fac)); + if(falloff == 0.0f) + break; + + VECADDFAC(temp,vec_to_part,eff_dir,-fac); + r_fac=VecLength(temp); + falloff*= falloff_func_rad(pd, r_fac); + break; + case PFIELD_FALL_CONE: + fac=Inpf(vec_to_part,eff_dir); + falloff= falloff_func_dist(pd, ABS(fac)); + if(falloff == 0.0f) + break; + + r_fac=saacos(fac/VecLength(vec_to_part))*180.0f/(float)M_PI; + falloff*= falloff_func_rad(pd, r_fac); + + break; + } + + return falloff; +} + +void do_physical_effector(Object *ob, float *opco, short type, float force_val, float distance, float falloff, float size, float damp, float *eff_velocity, float *vec_to_part, float *velocity, float *field, int planar, struct RNG *rng, float noise_factor, float charge, float pa_size) +{ + float mag_vec[3]={0,0,0}; + float temp[3], temp2[3]; + float eff_vel[3]; + float noise = 0, visibility; + + // calculate visibility + visibility = eff_calc_visibility(ob, opco, vec_to_part); + if(visibility <= 0.0) + return; + falloff *= visibility; + + VecCopyf(eff_vel,eff_velocity); + Normalize(eff_vel); + + switch(type){ + case PFIELD_WIND: + VECCOPY(mag_vec,eff_vel); + + // add wind noise here, only if we have wind + if((noise_factor > 0.0f) && (force_val > FLT_EPSILON)) + noise = wind_func(rng, noise_factor); + + VecMulf(mag_vec,(force_val+noise)*falloff); + VecAddf(field,field,mag_vec); + break; + + case PFIELD_FORCE: + if(planar) + Projf(mag_vec,vec_to_part,eff_vel); + else + VecCopyf(mag_vec,vec_to_part); + + Normalize(mag_vec); + + VecMulf(mag_vec,force_val*falloff); + VecAddf(field,field,mag_vec); + break; + + case PFIELD_VORTEX: + Crossf(mag_vec,eff_vel,vec_to_part); + + Normalize(mag_vec); + + VecMulf(mag_vec,force_val*distance*falloff); + VecAddf(field,field,mag_vec); + + break; + case PFIELD_MAGNET: + if(planar) + VecCopyf(temp,eff_vel); + else + /* magnetic field of a moving charge */ + Crossf(temp,eff_vel,vec_to_part); + + Normalize(temp); + + Crossf(temp2,velocity,temp); + VecAddf(mag_vec,mag_vec,temp2); + + VecMulf(mag_vec,force_val*falloff); + VecAddf(field,field,mag_vec); + break; + case PFIELD_HARMONIC: + if(planar) + Projf(mag_vec,vec_to_part,eff_vel); + else + VecCopyf(mag_vec,vec_to_part); + + Normalize(mag_vec); + + VecMulf(mag_vec,force_val*falloff); + VecSubf(field,field,mag_vec); + + VecCopyf(mag_vec,velocity); + /* 1.9 is an experimental value to get critical damping at damp=1.0 */ + VecMulf(mag_vec,damp*1.9f*(float)sqrt(force_val)); + VecSubf(field,field,mag_vec); + break; + case PFIELD_CHARGE: + if(planar) + Projf(mag_vec,vec_to_part,eff_vel); + else + VecCopyf(mag_vec,vec_to_part); + + Normalize(mag_vec); + + VecMulf(mag_vec,charge*force_val*falloff); + VecAddf(field,field,mag_vec); + break; + case PFIELD_LENNARDJ: + { + float fac; + + if(planar) { + Projf(mag_vec,vec_to_part,eff_vel); + distance = VecLength(mag_vec); + } + else + VecCopyf(mag_vec,vec_to_part); + + /* at this distance the field is 60 times weaker than maximum */ + if(distance > 2.22 * (size+pa_size)) + break; + + fac = pow((size+pa_size)/distance,6.0); + + fac = - fac * (1.0 - fac) / distance; + + /* limit the repulsive term drastically to avoid huge forces */ + fac = ((fac>2.0) ? 2.0 : fac); + + /* 0.003715 is the fac value at 2.22 times (size+pa_size), + substracted to avoid discontinuity at the border + */ + VecMulf(mag_vec, force_val * (fac-0.0037315)); + VecAddf(field,field,mag_vec); + break; + } + } +} + /* -------- pdDoEffectors() -------- generic force/speed system, now used for particles and softbodies lb = listbase with objects that take part in effecting @@ -243,14 +526,9 @@ void pdDoEffectors(ListBase *lb, float *opco, float *force, float *speed, float Object *ob; pEffectorCache *ec; PartDeflect *pd; - float vect_to_vert[3]; - float f_force, force_vec[3]; - float *obloc; - float distance, force_val, ffall_val; - float guidecollect[3], guidedist= 0.0f; - int cur_frame; - guidecollect[0]= guidecollect[1]= guidecollect[2]=0.0f; + float distance, vec_to_part[3]; + float falloff; /* Cycle through collected objects, get total of (1/(gravity_strength * dist^gravity_power)) */ /* Check for min distance here? (yes would be cool to add that, ton) */ @@ -261,178 +539,28 @@ void pdDoEffectors(ListBase *lb, float *opco, float *force, float *speed, float pd= ob->pd; /* Get IPO force strength and fall off values here */ - if (has_ipo_code(ob->ipo, OB_PD_FSTR)) - force_val = IPO_GetFloatValue(ob->ipo, OB_PD_FSTR, cur_time); - else - force_val = pd->f_strength; - - if (has_ipo_code(ob->ipo, OB_PD_FFALL)) - ffall_val = IPO_GetFloatValue(ob->ipo, OB_PD_FFALL, cur_time); - else - ffall_val = pd->f_power; - - /* Need to set r.cfra for paths (investigate, ton) (uses ob->ctime now, ton) */ - if(ob->ctime!=cur_time) { - cur_frame = G.scene->r.cfra; - G.scene->r.cfra = (int)cur_time; - where_is_object_time(ob, cur_time); - G.scene->r.cfra = cur_frame; - } + where_is_object_time(ob,cur_time); /* use center of object for distance calculus */ - obloc= ob->obmat[3]; - VECSUB(vect_to_vert, obloc, opco); - distance = VecLength(vect_to_vert); - - if((pd->flag & PFIELD_USEMAX) && distance>pd->maxdist && pd->forcefield != PFIELD_GUIDE) - ; /* don't do anything */ - else if((pd->flag & PFIELD_USEMIN) && distance<pd->mindist && pd->forcefield != PFIELD_GUIDE) + VecSubf(vec_to_part, opco, ob->obmat[3]); + distance = VecLength(vec_to_part); + + falloff=effector_falloff(pd,ob->obmat[2],vec_to_part); + + if(falloff<=0.0f) ; /* don't do anything */ - else if(pd->forcefield == PFIELD_WIND) { - VECCOPY(force_vec, ob->obmat[2]); - - /* wind works harder perpendicular to normal, would be nice for softbody later (ton) */ - - /* Limit minimum distance to vertex so that */ - /* the force is not too big */ - if (distance < 0.001) distance = 0.001f; - f_force = (force_val)*(1/(1000 * (float)pow((double)distance, (double)ffall_val))); - /* this option for softbody only */ - if(flags && PE_WIND_AS_SPEED){ - speed[0] -= (force_vec[0] * f_force ); - speed[1] -= (force_vec[1] * f_force ); - speed[2] -= (force_vec[2] * f_force ); - } - else{ - force[0] += force_vec[0]*f_force; - force[1] += force_vec[1]*f_force; - force[2] += force_vec[2]*f_force; - } - } - else if(pd->forcefield == PFIELD_FORCE) { - - /* only use center of object */ - obloc= ob->obmat[3]; - - /* Now calculate the gravitational force */ - VECSUB(vect_to_vert, obloc, opco); - distance = VecLength(vect_to_vert); - - /* Limit minimum distance to vertex so that */ - /* the force is not too big */ - if (distance < 0.001) distance = 0.001f; - f_force = (force_val)*(1.0/(1000.0 * (float)pow((double)distance, (double)ffall_val))); - force[0] += (vect_to_vert[0] * f_force ); - force[1] += (vect_to_vert[1] * f_force ); - force[2] += (vect_to_vert[2] * f_force ); - } - else if(pd->forcefield == PFIELD_VORTEX) { - float vortexvec[3]; + else { + float field[3]={0,0,0}, tmp[3]; + VECCOPY(field, force); + do_physical_effector(ob, opco, pd->forcefield,pd->f_strength,distance, + falloff,pd->f_dist,pd->f_damp,ob->obmat[2],vec_to_part, + speed,force,pd->flag&PFIELD_PLANAR, pd->rng, pd->f_noise, 0.0f, 0.0f); - /* only use center of object */ - obloc= ob->obmat[3]; - - /* Now calculate the vortex force */ - VECSUB(vect_to_vert, obloc, opco); - distance = VecLength(vect_to_vert); - - Crossf(force_vec, ob->obmat[2], vect_to_vert); - Normalize(force_vec); - - /* Limit minimum distance to vertex so that */ - /* the force is not too big */ - if (distance < 0.001) distance = 0.001f; - f_force = (force_val)*(1.0/(100.0 * (float)pow((double)distance, (double)ffall_val))); - vortexvec[0]= -(force_vec[0] * f_force ); - vortexvec[1]= -(force_vec[1] * f_force ); - vortexvec[2]= -(force_vec[2] * f_force ); - - /* this option for softbody only */ - if(flags &&PE_WIND_AS_SPEED) { - speed[0]+= vortexvec[0]; - speed[1]+= vortexvec[1]; - speed[2]+= vortexvec[2]; - } - else { - /* since vortex alters the speed, we have to correct for the previous vortex result */ - speed[0]+= vortexvec[0] - ec->oldspeed[0]; - speed[1]+= vortexvec[1] - ec->oldspeed[1]; - speed[2]+= vortexvec[2] - ec->oldspeed[2]; - - VECCOPY(ec->oldspeed, vortexvec); + // for softbody backward compatibility + if(flags & PE_WIND_AS_SPEED){ + VECSUB(tmp, force, field); + VECSUB(speed, speed, tmp); } } - else if(pd->forcefield == PFIELD_GUIDE) { - float guidevec[4], guidedir[3]; - float mindist= force_val; /* force_val is actually mindist in the UI */ - - distance= ec->guide_dist; - - /* WARNING: bails out with continue here */ - if((pd->flag & PFIELD_USEMAX) && distance>pd->maxdist) continue; - - /* calculate contribution factor for this guide */ - if(distance<=mindist) f_force= 1.0f; - else if(pd->flag & PFIELD_USEMAX) { - if(distance>pd->maxdist || mindist>=pd->maxdist) f_force= 0.0f; - else { - f_force= 1.0f - (distance-mindist)/(pd->maxdist - mindist); - if(ffall_val!=0.0f) - f_force = (float)pow(f_force, ffall_val+1.0); - } - } - else { - f_force= 1.0f/(1.0f + distance-mindist); - if(ffall_val!=0.0f) - f_force = (float)pow(f_force, ffall_val+1.0); - } - - /* now derive path point from loc_time */ - if(pd->flag & PFIELD_GUIDE_PATH_ADD) - where_on_path(ob, f_force*loc_time*ec->time_scale, guidevec, guidedir); - else - where_on_path(ob, loc_time*ec->time_scale, guidevec, guidedir); - - VECSUB(guidedir, guidevec, ec->oldloc); - VECCOPY(ec->oldloc, guidevec); - - Mat4Mul3Vecfl(ob->obmat, guidedir); - VecMulf(guidedir, ec->scale); /* correction for lifetime and speed */ - - /* we subtract the speed we gave it previous step */ - VECCOPY(guidevec, guidedir); - VECSUB(guidedir, guidedir, ec->oldspeed); - VECCOPY(ec->oldspeed, guidevec); - - /* if it fully contributes, we stop */ - if(f_force==1.0) { - VECCOPY(guidecollect, guidedir); - guidedist= 1.0f; - break; - } - else if(guidedist<1.0f) { - VecMulf(guidedir, f_force); - VECADD(guidecollect, guidecollect, guidedir); - guidedist += f_force; - } - } - } - - /* all guides are accumulated here */ - if(guidedist!=0.0f) { - if(guidedist!=1.0f) VecMulf(guidecollect, 1.0f/guidedist); - VECADD(speed, speed, guidecollect); } } - - -/* for paf start to end, store all matrices for objects */ -typedef struct pMatrixCache { - float obmat[4][4]; - float imat[3][3]; -} pMatrixCache; - -/* for fluidsim win32 debug messages */ -#if defined(WIN32) && (!(defined snprintf)) -#define snprintf _snprintf -#endif diff --git a/source/blender/blenkernel/intern/exotic.c b/source/blender/blenkernel/intern/exotic.c index fb48c875995..479be2ee644 100644 --- a/source/blender/blenkernel/intern/exotic.c +++ b/source/blender/blenkernel/intern/exotic.c @@ -2632,17 +2632,17 @@ static void write_videoscape_mesh(Object *ob, char *str) if(evl->v4==0) { fprintf(fp, "3 %ld %ld %ld 0x%x\n", - evl->v1->tmp.l, - evl->v2->tmp.l, - evl->v3->tmp.l, + (long int) evl->v1->tmp.l, + (long int) evl->v2->tmp.l, + (long int) evl->v3->tmp.l, kleur[evl->mat_nr]); } else { fprintf(fp, "4 %ld %ld %ld %ld 0x%x\n", - evl->v1->tmp.l, - evl->v2->tmp.l, - evl->v3->tmp.l, - evl->v4->tmp.l, + (long int) evl->v1->tmp.l, + (long int) evl->v2->tmp.l, + (long int) evl->v3->tmp.l, + (long int) evl->v4->tmp.l, kleur[evl->mat_nr]); } evl= evl->next; diff --git a/source/blender/blenkernel/intern/fluidsim.c b/source/blender/blenkernel/intern/fluidsim.c new file mode 100644 index 00000000000..9c1b3ee5a8d --- /dev/null +++ b/source/blender/blenkernel/intern/fluidsim.c @@ -0,0 +1,642 @@ +/** + * fluidsim.c + * + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) Blender Foundation + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "MEM_guardedalloc.h" + +#include "DNA_mesh_types.h" +#include "DNA_meshdata_types.h" +#include "DNA_object_force.h" // for pointcache +#include "DNA_particle_types.h" +#include "DNA_scene_types.h" // N_T + +#include "BLI_arithb.h" +#include "BLI_blenlib.h" + +#include "BKE_cdderivedmesh.h" +#include "BKE_customdata.h" +#include "BKE_DerivedMesh.h" +#include "BKE_fluidsim.h" +#include "BKE_global.h" +#include "BKE_modifier.h" +#include "BKE_mesh.h" +#include "BKE_pointcache.h" +#include "BKE_utildefines.h" + +// headers for fluidsim bobj meshes +#include <stdlib.h> +#include "LBM_fluidsim.h" +#include "elbeem.h" +#include <zlib.h> +#include <string.h> +#include <stdio.h> + +/* ************************* fluidsim bobj file handling **************************** */ + +// ----------------------------------------- +// forward decleration +// ----------------------------------------- + +// ----------------------------------------- + +void fluidsim_init(FluidsimModifierData *fluidmd) +{ +#ifndef DISABLE_ELBEEM + if(fluidmd) + { + FluidsimSettings *fss = MEM_callocN(sizeof(FluidsimSettings), "fluidsimsettings"); + + fluidmd->fss = fss; + + if(!fss) + return; + + fss->type = 0; + fss->show_advancedoptions = 0; + + fss->resolutionxyz = 50; + fss->previewresxyz = 25; + fss->realsize = 0.03; + fss->guiDisplayMode = 2; // preview + fss->renderDisplayMode = 3; // render + + fss->viscosityMode = 2; // default to water + fss->viscosityValue = 1.0; + fss->viscosityExponent = 6; + + // dg TODO: change this to [] + fss->gravx = 0.0; + fss->gravy = 0.0; + fss->gravz = -9.81; + fss->animStart = 0.0; + fss->animEnd = 0.30; + fss->gstar = 0.005; // used as normgstar + fss->maxRefine = -1; + // maxRefine is set according to resolutionxyz during bake + + // fluid/inflow settings + // fss->iniVel --> automatically set to 0 + + /* elubie: changed this to default to the same dir as the render output + to prevent saving to C:\ on Windows */ + BLI_strncpy(fss->surfdataPath, btempdir, FILE_MAX); + + // first init of bounding box + // no bounding box needed + + // todo - reuse default init from elbeem! + fss->typeFlags = 0; + fss->domainNovecgen = 0; + fss->volumeInitType = 1; // volume + fss->partSlipValue = 0.0; + + fss->generateTracers = 0; + fss->generateParticles = 0.0; + fss->surfaceSmoothing = 1.0; + fss->surfaceSubdivs = 1.0; + fss->particleInfSize = 0.0; + fss->particleInfAlpha = 0.0; + + // init fluid control settings + fss->attractforceStrength = 0.2; + fss->attractforceRadius = 0.75; + fss->velocityforceStrength = 0.2; + fss->velocityforceRadius = 0.75; + fss->cpsTimeStart = fss->animStart; + fss->cpsTimeEnd = fss->animEnd; + fss->cpsQuality = 10.0; // 1.0 / 10.0 => means 0.1 width + + /* + BAD TODO: this is done in buttons_object.c in the moment + Mesh *mesh = ob->data; + // calculate bounding box + fluid_get_bb(mesh->mvert, mesh->totvert, ob->obmat, fss->bbStart, fss->bbSize); + */ + + fss->lastgoodframe = -1; + + fss->flag = 0; + + } +#endif + return; +} + +void fluidsim_free(FluidsimModifierData *fluidmd) +{ +#ifndef DISABLE_ELBEEM + if(fluidmd) + { + MEM_freeN(fluidmd->fss); + } +#endif + return; +} + +DerivedMesh *fluidsimModifier_do(FluidsimModifierData *fluidmd, Object *ob, DerivedMesh *dm, int useRenderParams, int isFinalCalc) +{ +#ifndef DISABLE_ELBEEM + DerivedMesh *result = NULL; + int framenr; + FluidsimSettings *fss = NULL; + + framenr= (int)G.scene->r.cfra; + + // only handle fluidsim domains + if(fluidmd && fluidmd->fss && (fluidmd->fss->type != OB_FLUIDSIM_DOMAIN)) + return dm; + + // sanity check + if(!fluidmd || (fluidmd && !fluidmd->fss)) + return dm; + + fss = fluidmd->fss; + + // timescale not supported yet + // clmd->sim_parms->timescale= timescale; + + // support reversing of baked fluid frames here + if((fss->flag & OB_FLUIDSIM_REVERSE) && (fss->lastgoodframe >= 0)) + { + framenr = fss->lastgoodframe - framenr + 1; + CLAMP(framenr, 1, fss->lastgoodframe); + } + + /* try to read from cache */ + if(((fss->lastgoodframe >= framenr) || (fss->lastgoodframe < 0)) && (result = fluidsim_read_cache(ob, dm, fluidmd, framenr, useRenderParams))) + { + // fss->lastgoodframe = framenr; // set also in src/fluidsim.c + return result; + } + else + { + // display last known good frame + if(fss->lastgoodframe >= 0) + { + if((result = fluidsim_read_cache(ob, dm, fluidmd, fss->lastgoodframe, useRenderParams))) + { + return result; + } + + // it was supposed to be a valid frame but it isn't! + fss->lastgoodframe = framenr - 1; + + + // this could be likely the case when you load an old fluidsim + if((result = fluidsim_read_cache(ob, dm, fluidmd, fss->lastgoodframe, useRenderParams))) + { + return result; + } + } + + result = CDDM_copy(dm); + + if(result) + { + return result; + } + } + + return dm; +#else + return NULL; +#endif +} + +#ifndef DISABLE_ELBEEM +/* read .bobj.gz file into a fluidsimDerivedMesh struct */ +static DerivedMesh *fluidsim_read_obj(char *filename) +{ + int wri,i,j; + float wrf; + int gotBytes; + gzFile gzf; + int numverts = 0, numfaces = 0; + DerivedMesh *dm = NULL; + MFace *mface; + MVert *mvert; + short *normals; + + // ------------------------------------------------ + // get numverts + numfaces first + // ------------------------------------------------ + gzf = gzopen(filename, "rb"); + if (!gzf) + { + return NULL; + } + + // read numverts + gotBytes = gzread(gzf, &wri, sizeof(wri)); + numverts = wri; + + // skip verts + for(i=0; i<numverts*3; i++) + { + gotBytes = gzread(gzf, &wrf, sizeof( wrf )); + } + + // read number of normals + gotBytes = gzread(gzf, &wri, sizeof(wri)); + + // skip normals + for(i=0; i<numverts*3; i++) + { + gotBytes = gzread(gzf, &wrf, sizeof( wrf )); + } + + /* get no. of triangles */ + gotBytes = gzread(gzf, &wri, sizeof(wri)); + numfaces = wri; + + gzclose( gzf ); + // ------------------------------------------------ + + if(!numfaces || !numverts) + return NULL; + + gzf = gzopen(filename, "rb"); + if (!gzf) + { + return NULL; + } + + dm = CDDM_new(numverts, 0, numfaces); + + if(!dm) + { + gzclose( gzf ); + return NULL; + } + + // read numverts + gotBytes = gzread(gzf, &wri, sizeof(wri)); + + // read vertex position from file + mvert = CDDM_get_verts(dm); + for(i=0; i<numverts; i++) + { + MVert *mv = &mvert[i]; + + for(j=0; j<3; j++) + { + gotBytes = gzread(gzf, &wrf, sizeof( wrf )); + mv->co[j] = wrf; + } + } + + // should be the same as numverts + gotBytes = gzread(gzf, &wri, sizeof(wri)); + if(wri != numverts) + { + if(dm) + dm->release(dm); + gzclose( gzf ); + return NULL; + } + + normals = MEM_callocN(sizeof(short) * numverts * 3, "fluid_tmp_normals" ); + if(!normals) + { + if(dm) + dm->release(dm); + gzclose( gzf ); + return NULL; + } + + // read normals from file (but don't save them yet) + for(i=0; i<numverts*3; i++) + { + gotBytes = gzread(gzf, &wrf, sizeof( wrf )); + normals[i] = (short)(wrf*32767.0f); + } + + /* read no. of triangles */ + gotBytes = gzread(gzf, &wri, sizeof(wri)); + + if(wri!=numfaces) + printf("Fluidsim: error in reading data from file.\n"); + + // read triangles from file + mface = CDDM_get_faces(dm); + for(i=0; i<numfaces; i++) + { + int face[4]; + MFace *mf = &mface[i]; + + gotBytes = gzread(gzf, &(face[0]), sizeof( face[0] )); + gotBytes = gzread(gzf, &(face[1]), sizeof( face[1] )); + gotBytes = gzread(gzf, &(face[2]), sizeof( face[2] )); + face[3] = 0; + + // check if 3rd vertex has index 0 (not allowed in blender) + if(face[2]) + { + mf->v1 = face[0]; + mf->v2 = face[1]; + mf->v3 = face[2]; + } + else + { + mf->v1 = face[1]; + mf->v2 = face[2]; + mf->v3 = face[0]; + } + mf->v4 = face[3]; + + test_index_face(mf, NULL, 0, 3); + } + + gzclose( gzf ); + + CDDM_calc_edges(dm); + + CDDM_apply_vert_normals(dm, (short (*)[3])normals); + MEM_freeN(normals); + + // CDDM_calc_normals(result); + + return dm; +} + +DerivedMesh *fluidsim_read_cache(Object *ob, DerivedMesh *orgdm, FluidsimModifierData *fluidmd, int framenr, int useRenderParams) +{ + int displaymode = 0; + int curFrame = framenr - 1 /*G.scene->r.sfra*/; /* start with 0 at start frame */ + char targetDir[FILE_MAXFILE+FILE_MAXDIR], targetFile[FILE_MAXFILE+FILE_MAXDIR]; + FluidsimSettings *fss = fluidmd->fss; + DerivedMesh *dm = NULL; + MFace *mface; + int numfaces; + int mat_nr, flag, i; + + if(!useRenderParams) { + displaymode = fss->guiDisplayMode; + } else { + displaymode = fss->renderDisplayMode; + } + + strncpy(targetDir, fss->surfdataPath, FILE_MAXDIR); + + // use preview or final mesh? + if(displaymode==1) + { + // just display original object + return NULL; + } + else if(displaymode==2) + { + strcat(targetDir,"fluidsurface_preview_####"); + } + else + { // 3 + strcat(targetDir,"fluidsurface_final_####"); + } + + BLI_convertstringcode(targetDir, G.sce); + BLI_convertstringframe(targetDir, curFrame); // fixed #frame-no + + strcpy(targetFile,targetDir); + strcat(targetFile, ".bobj.gz"); + + dm = fluidsim_read_obj(targetFile); + + if(!dm) + { + // switch, abort background rendering when fluidsim mesh is missing + const char *strEnvName2 = "BLENDER_ELBEEMBOBJABORT"; // from blendercall.cpp + + if(G.background==1) { + if(getenv(strEnvName2)) { + int elevel = atoi(getenv(strEnvName2)); + if(elevel>0) { + printf("Env. var %s set, fluid sim mesh '%s' not found, aborting render...\n",strEnvName2, targetFile); + exit(1); + } + } + } + + // display org. object upon failure which is in dm + return NULL; + } + + // assign material + flags to new dm + mface = orgdm->getFaceArray(orgdm); + mat_nr = mface[0].mat_nr; + flag = mface[0].flag; + + mface = dm->getFaceArray(dm); + numfaces = dm->getNumFaces(dm); + for(i=0; i<numfaces; i++) + { + mface[i].mat_nr = mat_nr; + mface[i].flag = flag; + } + + // load vertex velocities, if they exist... + // TODO? use generate flag as loading flag as well? + // warning, needs original .bobj.gz mesh loading filename + /* + if(displaymode==3) + { + readVelgz(targetFile, srcob); + } + else + { + // no data for preview, only clear... + int i,j; + for(i=0; i<mesh->totvert;i++) { for(j=0; j<3; j++) { srcob->fluidsimSettings->meshSurfNormals[i].co[j] = 0.; }} + }*/ + + return dm; +} + +void fluid_get_bb(MVert *mvert, int totvert, float obmat[][4], + /*RET*/ float start[3], /*RET*/ float size[3] ) +{ + float bbsx=0.0, bbsy=0.0, bbsz=0.0; + float bbex=1.0, bbey=1.0, bbez=1.0; + int i; + float vec[3]; + + VECCOPY(vec, mvert[0].co); + Mat4MulVecfl(obmat, vec); + bbsx = vec[0]; bbsy = vec[1]; bbsz = vec[2]; + bbex = vec[0]; bbey = vec[1]; bbez = vec[2]; + + for(i = 1; i < totvert; i++) { + VECCOPY(vec, mvert[i].co); + Mat4MulVecfl(obmat, vec); + + if(vec[0] < bbsx){ bbsx= vec[0]; } + if(vec[1] < bbsy){ bbsy= vec[1]; } + if(vec[2] < bbsz){ bbsz= vec[2]; } + if(vec[0] > bbex){ bbex= vec[0]; } + if(vec[1] > bbey){ bbey= vec[1]; } + if(vec[2] > bbez){ bbez= vec[2]; } + } + + // return values... + if(start) { + start[0] = bbsx; + start[1] = bbsy; + start[2] = bbsz; + } + if(size) { + size[0] = bbex-bbsx; + size[1] = bbey-bbsy; + size[2] = bbez-bbsz; + } +} + +//------------------------------------------------------------------------------- +// old interface +//------------------------------------------------------------------------------- + + + +//------------------------------------------------------------------------------- +// file handling +//------------------------------------------------------------------------------- + +void initElbeemMesh(struct Object *ob, + int *numVertices, float **vertices, + int *numTriangles, int **triangles, + int useGlobalCoords, int modifierIndex) +{ + DerivedMesh *dm = NULL; + MVert *mvert; + MFace *mface; + int countTris=0, i, totvert, totface; + float *verts; + int *tris; + + dm = mesh_create_derived_index_render(ob, CD_MASK_BAREMESH, modifierIndex); + //dm = mesh_create_derived_no_deform(ob,NULL); + + mvert = dm->getVertArray(dm); + mface = dm->getFaceArray(dm); + totvert = dm->getNumVerts(dm); + totface = dm->getNumFaces(dm); + + *numVertices = totvert; + verts = MEM_callocN( totvert*3*sizeof(float), "elbeemmesh_vertices"); + for(i=0; i<totvert; i++) { + VECCOPY( &verts[i*3], mvert[i].co); + if(useGlobalCoords) { Mat4MulVecfl(ob->obmat, &verts[i*3]); } + } + *vertices = verts; + + for(i=0; i<totface; i++) { + countTris++; + if(mface[i].v4) { countTris++; } + } + *numTriangles = countTris; + tris = MEM_callocN( countTris*3*sizeof(int), "elbeemmesh_triangles"); + countTris = 0; + for(i=0; i<totface; i++) { + int face[4]; + face[0] = mface[i].v1; + face[1] = mface[i].v2; + face[2] = mface[i].v3; + face[3] = mface[i].v4; + + tris[countTris*3+0] = face[0]; + tris[countTris*3+1] = face[1]; + tris[countTris*3+2] = face[2]; + countTris++; + if(face[3]) { + tris[countTris*3+0] = face[0]; + tris[countTris*3+1] = face[2]; + tris[countTris*3+2] = face[3]; + countTris++; + } + } + *triangles = tris; + + dm->release(dm); +} + +/* read zipped fluidsim velocities into the co's of the fluidsimsettings normals struct */ +void readVelgz(char *filename, Object *srcob) +{ + int wri, i, j; + float wrf; + gzFile gzf; + MVert *vverts = srcob->fluidsimSettings->meshSurfNormals; + int len = strlen(filename); + Mesh *mesh = srcob->data; + // mesh and vverts have to be valid from loading... + + // clean up in any case + for(i=0; i<mesh->totvert;i++) + { + for(j=0; j<3; j++) + { + vverts[i].co[j] = 0.; + } + } + if(srcob->fluidsimSettings->domainNovecgen>0) return; + + if(len<7) + { + return; + } + + // .bobj.gz , correct filename + // 87654321 + filename[len-6] = 'v'; + filename[len-5] = 'e'; + filename[len-4] = 'l'; + + gzf = gzopen(filename, "rb"); + if (!gzf) + return; + + gzread(gzf, &wri, sizeof( wri )); + if(wri != mesh->totvert) + { + return; + } + + for(i=0; i<mesh->totvert;i++) + { + for(j=0; j<3; j++) + { + gzread(gzf, &wrf, sizeof( wrf )); + vverts[i].co[j] = wrf; + } + } + + gzclose(gzf); +} + + +#endif // DISABLE_ELBEEM + diff --git a/source/blender/blenkernel/intern/group.c b/source/blender/blenkernel/intern/group.c index 2ffe8590b59..0d2f86bb151 100644 --- a/source/blender/blenkernel/intern/group.c +++ b/source/blender/blenkernel/intern/group.c @@ -91,6 +91,16 @@ void unlink_group(Group *group) ma->group= NULL; } for (sce= G.main->scene.first; sce; sce= sce->id.next) { + Base *base= sce->base.first; + + /* ensure objects are not in this group */ + for(; base; base= base->next) { + if(rem_from_group(group, base->object) && find_group(base->object, NULL)==NULL) { + base->object->flag &= ~OB_FROMGROUP; + base->flag &= ~OB_FROMGROUP; + } + } + for(srl= sce->r.layers.first; srl; srl= srl->next) { if (srl->light_override==group) srl->light_override= NULL; @@ -117,6 +127,9 @@ void unlink_group(Group *group) psys->part->eff_group= NULL; } } + + /* group stays in library, but no members */ + free_group(group); group->id.us= 0; } diff --git a/source/blender/blenkernel/intern/icons.c b/source/blender/blenkernel/intern/icons.c index cab7865c1b6..b9e3c593ddf 100644 --- a/source/blender/blenkernel/intern/icons.c +++ b/source/blender/blenkernel/intern/icons.c @@ -50,6 +50,8 @@ #include "BKE_icons.h" #include "BKE_utildefines.h" +#include "BLO_sys_types.h" // for intptr_t support + #define GS(a) (*((short *)(a))) /* GLOBALS */ diff --git a/source/blender/blenkernel/intern/idprop.c b/source/blender/blenkernel/intern/idprop.c index b16f52571f6..0a140ebaba1 100644 --- a/source/blender/blenkernel/intern/idprop.c +++ b/source/blender/blenkernel/intern/idprop.c @@ -317,7 +317,7 @@ void IDP_FreeIterBeforeEnd(void *vself) This is because all ID Property freeing functions free only direct data (not the ID Property struct itself), but for Groups the child properties *are* considered direct data.*/ -void IDP_FreeGroup(IDProperty *prop) +static void IDP_FreeGroup(IDProperty *prop) { IDProperty *loop, *next; for (loop=prop->data.group.first; loop; loop=next) diff --git a/source/blender/blenkernel/intern/image.c b/source/blender/blenkernel/intern/image.c index b6c8ad59e08..18570515057 100644 --- a/source/blender/blenkernel/intern/image.c +++ b/source/blender/blenkernel/intern/image.c @@ -53,6 +53,7 @@ #include "DNA_packedFile_types.h" #include "DNA_scene_types.h" #include "DNA_camera_types.h" +#include "DNA_sequence_types.h" #include "DNA_texture_types.h" #include "DNA_userdef_types.h" @@ -77,15 +78,17 @@ #include "RE_pipeline.h" -/* bad level; call to free_realtime_image */ -#include "BKE_bad_level_calls.h" - /* for stamp drawing to an image */ #include "BMF_Api.h" #include "blendef.h" #include "BSE_time.h" +#include "GPU_extensions.h" +#include "GPU_draw.h" + +#include "BLO_sys_types.h" // for intptr_t support + /* max int, to indicate we don't store sequences in ibuf */ #define IMA_NO_INDEX 0x7FEFEFEF @@ -238,7 +241,7 @@ static void image_free_buffers(Image *ima) ima->rr= NULL; } - free_realtime_image(ima); + GPU_free_image(ima); ima->ok= IMA_OK; } @@ -582,7 +585,7 @@ void tag_image_time(Image *ima) ima->lastused = (int)PIL_check_seconds_timer(); } -void tag_all_images_time() +static void tag_all_images_time() { Image *ima; int ctime = (int)PIL_check_seconds_timer(); @@ -618,7 +621,7 @@ void free_old_images() This gives textures a "second chance" to be used before dying. */ if(ima->bindcode || ima->repbind) { - free_realtime_image(ima); + GPU_free_image(ima); ima->lastused = ctime; } /* Otherwise, just kill the buffers */ @@ -630,11 +633,11 @@ void free_old_images() } } -static unsigned long image_mem_size(Image *ima) +static uintptr_t image_mem_size(Image *ima) { ImBuf *ibuf, *ibufm; int level; - unsigned long size = 0; + uintptr_t size = 0; size= 0; for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) { @@ -656,7 +659,7 @@ static unsigned long image_mem_size(Image *ima) void BKE_image_print_memlist(void) { Image *ima; - unsigned long size, totsize= 0; + uintptr_t size, totsize= 0; for(ima= G.main->image.first; ima; ima= ima->id.next) totsize += image_mem_size(ima); diff --git a/source/blender/blenkernel/intern/implicit.c b/source/blender/blenkernel/intern/implicit.c index 297ac0b1530..93e35a4db06 100644 --- a/source/blender/blenkernel/intern/implicit.c +++ b/source/blender/blenkernel/intern/implicit.c @@ -1354,25 +1354,57 @@ DO_INLINE void cloth_apply_spring_force(ClothModifierData *clmd, ClothSpring *s, } } + +static void CalcFloat( float *v1, float *v2, float *v3, float *n) +{ + float n1[3],n2[3]; + + n1[0]= v1[0]-v2[0]; + n2[0]= v2[0]-v3[0]; + n1[1]= v1[1]-v2[1]; + n2[1]= v2[1]-v3[1]; + n1[2]= v1[2]-v2[2]; + n2[2]= v2[2]-v3[2]; + n[0]= n1[1]*n2[2]-n1[2]*n2[1]; + n[1]= n1[2]*n2[0]-n1[0]*n2[2]; + n[2]= n1[0]*n2[1]-n1[1]*n2[0]; +} + +static void CalcFloat4( float *v1, float *v2, float *v3, float *v4, float *n) +{ + /* real cross! */ + float n1[3],n2[3]; + + n1[0]= v1[0]-v3[0]; + n1[1]= v1[1]-v3[1]; + n1[2]= v1[2]-v3[2]; + + n2[0]= v2[0]-v4[0]; + n2[1]= v2[1]-v4[1]; + n2[2]= v2[2]-v4[2]; + + n[0]= n1[1]*n2[2]-n1[2]*n2[1]; + n[1]= n1[2]*n2[0]-n1[0]*n2[2]; + n[2]= n1[0]*n2[1]-n1[1]*n2[0]; +} + float calculateVertexWindForce(float wind[3], float vertexnormal[3]) { - return fabs(INPR(wind, vertexnormal)); + return (INPR(wind, vertexnormal)); } void cloth_calc_force(ClothModifierData *clmd, lfVector *lF, lfVector *lX, lfVector *lV, fmatrix3x3 *dFdV, fmatrix3x3 *dFdX, ListBase *effectors, float time, fmatrix3x3 *M) { /* Collect forces and derivatives: F,dFdX,dFdV */ Cloth *cloth = clmd->clothObject; - long i = 0; + int i = 0; float spring_air = clmd->sim_parms->Cvi * 0.01f; /* viscosity of air scaled in percent */ float gravity[3]; float tm2[3][3] = {{-spring_air,0,0}, {0,-spring_air,0},{0,0,-spring_air}}; MFace *mfaces = cloth->mfaces; - //ClothVertex *verts = cloth->verts; - float wind_normalized[3]; unsigned int numverts = cloth->numverts; LinkNode *search = cloth->springs; - + lfVector *winvec; VECCOPY(gravity, clmd->sim_parms->gravity); mul_fvector_S(gravity, gravity, 0.001f); /* scale gravity force */ @@ -1387,7 +1419,7 @@ void cloth_calc_force(ClothModifierData *clmd, lfVector *lF, lfVector *lX, lfVec /* multiply lF with mass matrix // force = mass * acceleration (in this case: gravity) */ - for(i = 0; i < (long)numverts; i++) + for(i = 0; i < numverts; i++) { float temp[3]; VECCOPY(temp, lF[i]); @@ -1399,70 +1431,61 @@ void cloth_calc_force(ClothModifierData *clmd, lfVector *lF, lfVector *lX, lfVec /* handle external forces like wind */ if(effectors) { - for(i = 0; i < cloth->numfaces; i++) + // 0 = force, 1 = normalized force + winvec = create_lfvector(cloth->numverts); + + if(!winvec) + printf("winvec: out of memory in implicit.c\n"); + + // precalculate wind forces + for(i = 0; i < cloth->numverts; i++) { - float vertexnormal[3]={0,0,0}; float speed[3] = {0.0f, 0.0f,0.0f}; - float force[3]= {0.0f, 0.0f, 0.0f}; + pdDoEffectors(effectors, lX[i], winvec[i], speed, (float)G.scene->r.cfra, 0.0f, 0); + } + + for(i = 0; i < cloth->numfaces; i++) + { + float trinormal[3]={0,0,0}; // normalized triangle normal + float triunnormal[3]={0,0,0}; // not-normalized-triangle normal + float tmp[3]={0,0,0}; + float factor = (mfaces[i].v4) ? 0.25 : 1.0 / 3.0; + factor *= 0.02; + + // calculate face normal if(mfaces[i].v4) - CalcNormFloat4(lX[mfaces[i].v1],lX[mfaces[i].v2],lX[mfaces[i].v3],lX[mfaces[i].v4],vertexnormal); + CalcFloat4(lX[mfaces[i].v1],lX[mfaces[i].v2],lX[mfaces[i].v3],lX[mfaces[i].v4],triunnormal); else - CalcNormFloat(lX[mfaces[i].v1],lX[mfaces[i].v2],lX[mfaces[i].v3],vertexnormal); + CalcFloat(lX[mfaces[i].v1],lX[mfaces[i].v2],lX[mfaces[i].v3],triunnormal); - pdDoEffectors(effectors, lX[mfaces[i].v1], force, speed, (float)G.scene->r.cfra, 0.0f, PE_WIND_AS_SPEED); - VECCOPY(wind_normalized, speed); - Normalize(wind_normalized); - VecMulf(wind_normalized, -calculateVertexWindForce(speed, vertexnormal)); + VECCOPY(trinormal, triunnormal); + Normalize(trinormal); - if(mfaces[i].v4) - { - VECADDS(lF[mfaces[i].v1], lF[mfaces[i].v1], wind_normalized, 0.25); - } - else - { - VECADDS(lF[mfaces[i].v1], lF[mfaces[i].v1], wind_normalized, 1.0 / 3.0); - } + // add wind from v1 + VECCOPY(tmp, trinormal); + VecMulf(tmp, calculateVertexWindForce(winvec[mfaces[i].v1], triunnormal)); + VECADDS(lF[mfaces[i].v1], lF[mfaces[i].v1], tmp, factor); - speed[0] = speed[1] = speed[2] = 0.0; - pdDoEffectors(effectors, lX[mfaces[i].v2], force, speed, (float)G.scene->r.cfra, 0.0f, PE_WIND_AS_SPEED); - VECCOPY(wind_normalized, speed); - Normalize(wind_normalized); - VecMulf(wind_normalized, -calculateVertexWindForce(speed, vertexnormal)); - if(mfaces[i].v4) - { - VECADDS(lF[mfaces[i].v2], lF[mfaces[i].v2], wind_normalized, 0.25); - } - else - { - VECADDS(lF[mfaces[i].v2], lF[mfaces[i].v2], wind_normalized, 1.0 / 3.0); - } + // add wind from v2 + VECCOPY(tmp, trinormal); + VecMulf(tmp, calculateVertexWindForce(winvec[mfaces[i].v2], triunnormal)); + VECADDS(lF[mfaces[i].v2], lF[mfaces[i].v2], tmp, factor); - speed[0] = speed[1] = speed[2] = 0.0; - pdDoEffectors(effectors, lX[mfaces[i].v3], force, speed, (float)G.scene->r.cfra, 0.0f, PE_WIND_AS_SPEED); - VECCOPY(wind_normalized, speed); - Normalize(wind_normalized); - VecMulf(wind_normalized, -calculateVertexWindForce(speed, vertexnormal)); - if(mfaces[i].v4) - { - VECADDS(lF[mfaces[i].v3], lF[mfaces[i].v3], wind_normalized, 0.25); - } - else - { - VECADDS(lF[mfaces[i].v3], lF[mfaces[i].v3], wind_normalized, 1.0 / 3.0); - } + // add wind from v3 + VECCOPY(tmp, trinormal); + VecMulf(tmp, calculateVertexWindForce(winvec[mfaces[i].v3], triunnormal)); + VECADDS(lF[mfaces[i].v3], lF[mfaces[i].v3], tmp, factor); - speed[0] = speed[1] = speed[2] = 0.0; + // add wind from v4 if(mfaces[i].v4) { - pdDoEffectors(effectors, lX[mfaces[i].v4], force, speed, (float)G.scene->r.cfra, 0.0f, PE_WIND_AS_SPEED); - VECCOPY(wind_normalized, speed); - Normalize(wind_normalized); - VecMulf(wind_normalized, -calculateVertexWindForce(speed, vertexnormal)); - VECADDS(lF[mfaces[i].v4], lF[mfaces[i].v4], wind_normalized, 0.25); + VECCOPY(tmp, trinormal); + VecMulf(tmp, calculateVertexWindForce(winvec[mfaces[i].v4], triunnormal)); + VECADDS(lF[mfaces[i].v4], lF[mfaces[i].v4], tmp, factor); } - } + del_lfvector(winvec); } // calculate spring forces diff --git a/source/blender/blenkernel/intern/ipo.c b/source/blender/blenkernel/intern/ipo.c index 59eb3837aab..a115ac39e1a 100644 --- a/source/blender/blenkernel/intern/ipo.c +++ b/source/blender/blenkernel/intern/ipo.c @@ -23,7 +23,7 @@ * * The Original Code is: all of this file. * - * Contributor(s): none yet. + * Contributor(s): 2008, Joshua Leung (IPO System cleanup) * * ***** END GPL LICENSE BLOCK ***** */ @@ -77,11 +77,16 @@ #define SMALL -1.0e-10 +/* ***************************** Adrcode Blocktype Defines ********************************* */ + /* This array concept was meant to make sure that defines such as OB_LOC_X don't have to be enumerated, also for backward compatibility, future changes, and to enable it all can be accessed with a for-next loop. + + This should whole adrcode system should eventually be replaced by a proper Data API */ + int co_ar[CO_TOTIPO]= { CO_ENFORCE, CO_HEADTAIL }; @@ -151,9 +156,9 @@ int wo_ar[WO_TOTIPO]= { }; int la_ar[LA_TOTIPO]= { - LA_ENERGY, LA_COL_R, LA_COL_G, LA_COL_B, + LA_ENERGY, LA_COL_R, LA_COL_G, LA_COL_B, LA_DIST, LA_SPOTSI, LA_SPOTBL, - LA_QUAD1, LA_QUAD2, LA_HALOINT, + LA_QUAD1, LA_QUAD2, LA_HALOINT, MA_MAP1+MAP_OFS_X, MA_MAP1+MAP_OFS_Y, MA_MAP1+MAP_OFS_Z, MA_MAP1+MAP_SIZE_X, MA_MAP1+MAP_SIZE_Y, MA_MAP1+MAP_SIZE_Z, @@ -175,460 +180,617 @@ int fluidsim_ar[FLUIDSIM_TOTIPO]= { FLUIDSIM_VISC, FLUIDSIM_TIME, FLUIDSIM_GRAV_X , FLUIDSIM_GRAV_Y , FLUIDSIM_GRAV_Z , FLUIDSIM_VEL_X , FLUIDSIM_VEL_Y , FLUIDSIM_VEL_Z , - FLUIDSIM_ACTIVE + FLUIDSIM_ACTIVE, + FLUIDSIM_ATTR_FORCE_STR, FLUIDSIM_ATTR_FORCE_RADIUS, + FLUIDSIM_VEL_FORCE_STR, FLUIDSIM_VEL_FORCE_RADIUS, }; int part_ar[PART_TOTIPO]= { PART_EMIT_FREQ, PART_EMIT_LIFE, PART_EMIT_VEL, PART_EMIT_AVE, PART_EMIT_SIZE, PART_AVE, PART_SIZE, PART_DRAG, PART_BROWN, PART_DAMP, PART_LENGTH, PART_CLUMP, PART_GRAV_X, PART_GRAV_Y, PART_GRAV_Z, PART_KINK_AMP, PART_KINK_FREQ, PART_KINK_SHAPE, - PART_BB_TILT + PART_BB_TILT, PART_PD_FSTR, PART_PD_FFALL, PART_PD_FMAXD, PART_PD2_FSTR, PART_PD2_FFALL, PART_PD2_FMAXD }; +/* ************************** Data-Level Functions ************************* */ + +/* ---------------------- Freeing --------------------------- */ -float frame_to_float(int cfra) /* see also bsystem_time in object.c */ +/* frees the ipo curve itself too */ +void free_ipo_curve (IpoCurve *icu) { - extern float bluroffs; /* bad stuff borrowed from object.c */ - extern float fieldoffs; - float ctime; + if (icu == NULL) + return; - ctime= (float)cfra; - ctime+= bluroffs+fieldoffs; - ctime*= G.scene->r.framelen; + if (icu->bezt) + MEM_freeN(icu->bezt); + if (icu->driver) + MEM_freeN(icu->driver); - return ctime; -} - -/* includes ipo curve itself */ -void free_ipo_curve(IpoCurve *icu) -{ - if(icu->bezt) MEM_freeN(icu->bezt); - if(icu->bp) MEM_freeN(icu->bp); - if(icu->driver) MEM_freeN(icu->driver); MEM_freeN(icu); } /* do not free ipo itself */ -void free_ipo(Ipo *ipo) +void free_ipo (Ipo *ipo) { - IpoCurve *icu; + IpoCurve *icu, *icn; - while( (icu= ipo->curve.first) ) { + if (ipo == NULL) + return; + + for (icu= ipo->curve.first; icu; icu= icn) { + icn= icu->next; + + /* must remove the link before freeing, as the curve is freed too */ BLI_remlink(&ipo->curve, icu); free_ipo_curve(icu); } } +/* ---------------------- Init --------------------------- */ + /* on adding new ipos, or for empty views */ -void ipo_default_v2d_cur(int blocktype, rctf *cur) +void ipo_default_v2d_cur (int blocktype, rctf *cur) { - if(blocktype==ID_CA) { + switch (blocktype) { + case ID_CA: cur->xmin= G.scene->r.sfra; cur->xmax= G.scene->r.efra; cur->ymin= 0.0; cur->ymax= 100.0; - } - else if ELEM5(blocktype, ID_MA, ID_CU, ID_WO, ID_LA, ID_CO) { + break; + + case ID_MA: case ID_WO: case ID_LA: + case ID_CU: case ID_CO: cur->xmin= (float)G.scene->r.sfra-0.1; cur->xmax= G.scene->r.efra; cur->ymin= (float)-0.1; cur->ymax= (float)+1.1; - } - else if(blocktype==ID_TE) { + break; + + case ID_TE: cur->xmin= (float)G.scene->r.sfra-0.1; cur->xmax= G.scene->r.efra; cur->ymin= (float)-0.1; cur->ymax= (float)+1.1; - } - else if(blocktype==ID_SEQ) { + break; + + case ID_SEQ: cur->xmin= -5.0; cur->xmax= 105.0; cur->ymin= (float)-0.1; cur->ymax= (float)+1.1; - } - else if(blocktype==ID_KE) { + break; + + case ID_KE: cur->xmin= (float)G.scene->r.sfra-0.1; cur->xmax= G.scene->r.efra; cur->ymin= (float)-0.1; cur->ymax= (float)+2.1; - } - else { /* ID_OB and everything else */ + break; + + default: /* ID_OB and everything else */ cur->xmin= G.scene->r.sfra; cur->xmax= G.scene->r.efra; cur->ymin= -5.0; cur->ymax= +5.0; + break; } } - -Ipo *add_ipo(char *name, int idcode) +/* create a new IPO block (allocates the block) */ +Ipo *add_ipo (char name[], int blocktype) { Ipo *ipo; ipo= alloc_libblock(&G.main->ipo, ID_IP, name); - ipo->blocktype= idcode; - ipo_default_v2d_cur(idcode, &ipo->cur); + ipo->blocktype= blocktype; + ipo_default_v2d_cur(blocktype, &ipo->cur); return ipo; } -Ipo *copy_ipo(Ipo *ipo) +/* ---------------------- Copy --------------------------- */ + +/* duplicate an IPO block and all its data */ +Ipo *copy_ipo (Ipo *src) { - Ipo *ipon; + Ipo *dst; IpoCurve *icu; - if(ipo==NULL) return 0; - - ipon= copy_libblock(ipo); + if (src == NULL) + return NULL; - duplicatelist(&(ipon->curve), &(ipo->curve)); + dst= copy_libblock(src); + duplicatelist(&dst->curve, &src->curve); - for(icu= ipo->curve.first; icu; icu= icu->next) { + for (icu= src->curve.first; icu; icu= icu->next) { icu->bezt= MEM_dupallocN(icu->bezt); - if(icu->driver) icu->driver= MEM_dupallocN(icu->driver); + + if (icu->driver) + icu->driver= MEM_dupallocN(icu->driver); } - return ipon; + return dst; } -/* uses id->newid to match pointers with other copied data */ -void ipo_idnew(Ipo *ipo) +/* ---------------------- Relink --------------------------- */ + +/* uses id->newid to match pointers with other copied data + * - called after single-user or other such + */ +void ipo_idnew (Ipo *ipo) { - if(ipo) { + if (ipo) { IpoCurve *icu; - for(icu= ipo->curve.first; icu; icu= icu->next) { - if(icu->driver) { + for (icu= ipo->curve.first; icu; icu= icu->next) { + if (icu->driver) ID_NEW(icu->driver->ob); - } } } } -void make_local_obipo(Ipo *ipo) +/* --------------------- Find + Check ----------------------- */ + +/* find the IPO-curve within a given IPO-block with the adrcode of interest */ +IpoCurve *find_ipocurve (Ipo *ipo, int adrcode) +{ + if (ipo) { + IpoCurve *icu; + + for (icu= ipo->curve.first; icu; icu= icu->next) { + if (icu->adrcode == adrcode) + return icu; + } + } + return NULL; +} + +/* return whether the given IPO block has a IPO-curve with the given adrcode */ +short has_ipo_code(Ipo *ipo, int adrcode) +{ + /* return success of faliure from trying to find such an IPO-curve */ + return (find_ipocurve(ipo, adrcode) != NULL); +} + +/* ---------------------- Make Local --------------------------- */ + + +/* make the given IPO local (for Objects) + * - only lib users: do nothing + * - only local users: set flag + * - mixed: make copy + */ +void make_local_obipo (Ipo *src) { Object *ob; - Ipo *ipon; + Ipo *dst; int local=0, lib=0; - /* - only lib users: do nothing - * - only local users: set flag - * - mixed: make copy - */ - - ob= G.main->object.first; - while(ob) { - if(ob->ipo==ipo) { - if(ob->id.lib) lib= 1; + /* check if only local and/or lib */ + for (ob= G.main->object.first; ob; ob= ob->id.next) { + if (ob->ipo == src) { + if (ob->id.lib) lib= 1; else local= 1; } - ob= ob->id.next; } - if(local && lib==0) { - ipo->id.lib= 0; - ipo->id.flag= LIB_LOCAL; - new_id(0, (ID *)ipo, 0); + /* only local - set flag */ + if (local && lib==0) { + src->id.lib= 0; + src->id.flag= LIB_LOCAL; + new_id(0, (ID *)src, 0); } - else if(local && lib) { - ipon= copy_ipo(ipo); - ipon->id.us= 0; + /* mixed: make copy */ + else if (local && lib) { + dst= copy_ipo(src); + dst->id.us= 0; - ob= G.main->object.first; - while(ob) { - if(ob->ipo==ipo) { - - if(ob->id.lib==NULL) { - ob->ipo= ipon; - ipon->id.us++; - ipo->id.us--; + for (ob= G.main->object.first; ob; ob= ob->id.next) { + if (ob->ipo == src) { + if (ob->id.lib == NULL) { + ob->ipo= dst; + dst->id.us++; + src->id.us--; } } - ob= ob->id.next; } } } -void make_local_matipo(Ipo *ipo) +/* make the given IPO local (for Materials) + * - only lib users: do nothing + * - only local users: set flag + * - mixed: make copy + */ +void make_local_matipo (Ipo *src) { Material *ma; - Ipo *ipon; + Ipo *dst; int local=0, lib=0; - - /* - only lib users: do nothing - * - only local users: set flag - * - mixed: make copy - */ - ma= G.main->mat.first; - while(ma) { - if(ma->ipo==ipo) { - if(ma->id.lib) lib= 1; + /* check if only local and/or lib */ + for (ma= G.main->mat.first; ma; ma= ma->id.next) { + if (ma->ipo == src) { + if (ma->id.lib) lib= 1; else local= 1; } - ma= ma->id.next; } - if(local && lib==0) { - ipo->id.lib= 0; - ipo->id.flag= LIB_LOCAL; - new_id(0, (ID *)ipo, 0); + /* only local - set flag */ + if (local && lib==0) { + src->id.lib= 0; + src->id.flag= LIB_LOCAL; + new_id(0, (ID *)src, 0); } - else if(local && lib) { - ipon= copy_ipo(ipo); - ipon->id.us= 0; + /* mixed: make copy */ + else if (local && lib) { + dst= copy_ipo(src); + dst->id.us= 0; - ma= G.main->mat.first; - while(ma) { - if(ma->ipo==ipo) { - - if(ma->id.lib==NULL) { - ma->ipo= ipon; - ipon->id.us++; - ipo->id.us--; + for (ma= G.main->mat.first; ma; ma= ma->id.next) { + if (ma->ipo == src) { + if (ma->id.lib == NULL) { + ma->ipo= dst; + dst->id.us++; + src->id.us--; } } - ma= ma->id.next; } } } -void make_local_keyipo(Ipo *ipo) +/* make the given IPO local (for ShapeKeys) + * - only lib users: do nothing + * - only local users: set flag + * - mixed: make copy + */ +void make_local_keyipo (Ipo *src) { Key *key; - Ipo *ipon; + Ipo *dst; int local=0, lib=0; - - /* - only lib users: do nothing - * - only local users: set flag - * - mixed: make copy - */ - key= G.main->key.first; - while(key) { - if(key->ipo==ipo) { - if(key->id.lib) lib= 1; + /* check if only local and/or lib */ + for (key= G.main->key.first; key; key= key->id.next) { + if (key->ipo == src) { + if (key->id.lib) lib= 1; else local= 1; } - key= key->id.next; } - if(local && lib==0) { - ipo->id.lib= 0; - ipo->id.flag= LIB_LOCAL; - new_id(0, (ID *)ipo, 0); + /* only local - set flag */ + if (local && lib==0) { + src->id.lib= 0; + src->id.flag= LIB_LOCAL; + new_id(0, (ID *)src, 0); } - else if(local && lib) { - ipon= copy_ipo(ipo); - ipon->id.us= 0; + /* mixed: make copy */ + else if (local && lib) { + dst= copy_ipo(src); + dst->id.us= 0; - key= G.main->key.first; - while(key) { - if(key->ipo==ipo) { - - if(key->id.lib==NULL) { - key->ipo= ipon; - ipon->id.us++; - ipo->id.us--; + for (key= G.main->key.first; key; key= key->id.next) { + if (key->ipo == src) { + if (key->id.lib == NULL) { + key->ipo= dst; + dst->id.us++; + src->id.us--; } } - key= key->id.next; } } } -void make_local_ipo(Ipo *ipo) +/* generic call to make IPO's local */ +void make_local_ipo (Ipo *ipo) { - - if(ipo->id.lib==NULL) return; - if(ipo->id.us==1) { + /* can't touch lib-linked data */ + if (ipo->id.lib == NULL) + return; + + /* with only one user, just set local flag */ + if (ipo->id.us == 1) { ipo->id.lib= 0; ipo->id.flag= LIB_LOCAL; new_id(0, (ID *)ipo, 0); return; } - if(ipo->blocktype==ID_OB) make_local_obipo(ipo); - else if(ipo->blocktype==ID_MA) make_local_matipo(ipo); - else if(ipo->blocktype==ID_KE) make_local_keyipo(ipo); - + /* when more than 1 user, can only make local for certain blocktypes */ + switch (ipo->blocktype) { + case ID_OB: + make_local_obipo(ipo); + break; + case ID_MA: + make_local_matipo(ipo); + break; + case ID_KE: + make_local_keyipo(ipo); + break; + } } -IpoCurve *find_ipocurve(Ipo *ipo, int adrcode) +/* ***************************** Keyframe Column Tools ********************************* */ + +/* add a BezTriple to a column */ +void add_to_cfra_elem(ListBase *lb, BezTriple *bezt) { - if(ipo) { - IpoCurve *icu; - for(icu= ipo->curve.first; icu; icu= icu->next) { - if(icu->adrcode==adrcode) return icu; + CfraElem *ce, *cen; + + for (ce= lb->first; ce; ce= ce->next) { + /* double key? */ + if (ce->cfra == bezt->vec[1][0]) { + if (bezt->f2 & SELECT) ce->sel= bezt->f2; + return; } + /* should key be inserted before this column? */ + else if (ce->cfra > bezt->vec[1][0]) break; } - return NULL; + + /* create a new column */ + cen= MEM_callocN(sizeof(CfraElem), "add_to_cfra_elem"); + if (ce) BLI_insertlinkbefore(lb, ce, cen); + else BLI_addtail(lb, cen); + + cen->cfra= bezt->vec[1][0]; + cen->sel= bezt->f2; } -void calchandles_ipocurve(IpoCurve *icu) +/* make a list of keyframe 'columns' in an IPO block */ +void make_cfra_list (Ipo *ipo, ListBase *elems) { - BezTriple *bezt, *prev, *next; + IpoCurve *icu; + BezTriple *bezt; int a; + + for (icu= ipo->curve.first; icu; icu= icu->next) { + if (icu->flag & IPO_VISIBLE) { + /* ... removed old checks for adrcode types from here ... + * - (was this used for IpoKeys in the past?) + */ + + bezt= icu->bezt; + if (bezt) { + for (a=0; a < icu->totvert; a++, bezt++) { + add_to_cfra_elem(elems, bezt); + } + } + } + } +} + +/* ***************************** Timing Stuff ********************************* */ - a= icu->totvert; +/* This (evil) function is needed to cope with two legacy Blender rendering features + * mblur (motion blur that renders 'subframes' and blurs them together), and fields + * rendering. Thus, the use of ugly globals from object.c + */ +// BAD... EVIL... JUJU...!!!! +float frame_to_float (int cfra) /* see also bsystem_time in object.c */ +{ + extern float bluroffs; /* bad stuff borrowed from object.c */ + extern float fieldoffs; + float ctime; - /* IPO_CONST doesn't have handles */ - if(a<2 || icu->ipo==IPO_CONST) return; + ctime= (float)cfra; + ctime+= bluroffs+fieldoffs; + ctime*= G.scene->r.framelen; - bezt= icu->bezt; - prev= 0; - next= bezt+1; + return ctime; +} - while(a--) { +/* ***************************** IPO Curve Sanity ********************************* */ +/* The functions here are used in various parts of Blender, usually after some editing + * of keyframe data has occurred. They ensure that keyframe data is properly ordered and + * that the handles are correctly + */ - if(bezt->vec[0][0]>bezt->vec[1][0]) bezt->vec[0][0]= bezt->vec[1][0]; - if(bezt->vec[2][0]<bezt->vec[1][0]) bezt->vec[2][0]= bezt->vec[1][0]; +/* This function recalculates the handles of an IPO-Curve + * If the BezTriples have been rearranged, sort them first before using this. + */ +void calchandles_ipocurve (IpoCurve *icu) +{ + BezTriple *bezt, *prev, *next; + int a= icu->totvert; - if(icu->flag & IPO_AUTO_HORIZ) + /* Error checking: + * - need at least two points + * - need bezier keys + * - only bezier-interpolation has handles (for now) + */ + if (ELEM(NULL, icu, icu->bezt) || (a < 2) || ELEM(icu->ipo, IPO_CONST, IPO_LIN)) + return; + + /* get initial pointers */ + bezt= icu->bezt; + prev= NULL; + next= (bezt + 1); + + /* loop over all beztriples, adjusting handles */ + while (a--) { + /* clamp timing of handles to be on either side of beztriple */ + if (bezt->vec[0][0] > bezt->vec[1][0]) bezt->vec[0][0]= bezt->vec[1][0]; + if (bezt->vec[2][0] < bezt->vec[1][0]) bezt->vec[2][0]= bezt->vec[1][0]; + + /* calculate autohandles */ + if (icu->flag & IPO_AUTO_HORIZ) calchandleNurb(bezt, prev, next, 2); /* 2==special autohandle && keep extrema horizontal */ else calchandleNurb(bezt, prev, next, 1); /* 1==special autohandle */ - - prev= bezt; - if(a==1) { - next= 0; - } - else next++; - + /* for automatic ease in and out */ - if(bezt->h1==HD_AUTO && bezt->h2==HD_AUTO) { - if(a==0 || a==icu->totvert-1) { - if(icu->extrap==IPO_HORIZ) { + if ((bezt->h1==HD_AUTO) && (bezt->h2==HD_AUTO)) { + /* only do this on first or last beztriple */ + if ((a==0) || (a==icu->totvert-1)) { + /* set both handles to have same horizontal value as keyframe */ + if (icu->extrap==IPO_HORIZ) { bezt->vec[0][1]= bezt->vec[2][1]= bezt->vec[1][1]; } } } + /* advance pointers for next iteration */ + prev= bezt; + if (a == 1) next= NULL; + else next++; bezt++; } } -void testhandles_ipocurve(IpoCurve *icu) +/* Use when IPO-Curve with handles has changed + * It treats all BezTriples with the following rules: + * - PHASE 1: do types have to be altered? + * -> Auto handles: become aligned when selection status is NOT(000 || 111) + * -> Vector handles: become 'nothing' when (one half selected AND other not) + * - PHASE 2: recalculate handles +*/ +void testhandles_ipocurve (IpoCurve *icu) { - /* use when something has changed with handles. - it treats all BezTriples with the following rules: - PHASE 1: do types have to be altered? - Auto handles: become aligned when selection status is NOT(000 || 111) - Vector handles: become 'nothing' when (one half selected AND other not) - PHASE 2: recalculate handles - */ - BezTriple *bezt; - int flag, a; + BezTriple *bezt; + int a; - bezt= icu->bezt; - if(bezt==NULL) return; + /* only beztriples have handles (bpoints don't though) */ + if (ELEM(NULL, icu, icu->bezt)) + return; - a= icu->totvert; - while(a--) { - flag= 0; - if(bezt->f1 & SELECT) flag++; - if(bezt->f2 & SELECT) flag += 2; - if(bezt->f3 & SELECT) flag += 4; - - if( !(flag==0 || flag==7) ) { - if(bezt->h1==HD_AUTO) { /* auto */ + /* loop over beztriples */ + for (a=0, bezt=icu->bezt; a < icu->totvert; a++, bezt++) { + short flag= 0; + + /* flag is initialised as selection status + * of beztriple control-points (labelled 0,1,2) + */ + if (bezt->f1 & SELECT) flag |= (1<<0); // == 1 + if (bezt->f2 & SELECT) flag |= (1<<1); // == 2 + if (bezt->f3 & SELECT) flag |= (1<<2); // == 4 + + /* one or two handles selected only */ + if (ELEM(flag, 0, 7)==0) { + /* auto handles become aligned */ + if (bezt->h1==HD_AUTO) bezt->h1= HD_ALIGN; - } - if(bezt->h2==HD_AUTO) { /* auto */ + if(bezt->h2==HD_AUTO) bezt->h2= HD_ALIGN; + + /* vector handles become 'free' when only one half selected */ + if(bezt->h1==HD_VECT) { + /* only left half (1 or 2 or 1+2) */ + if (flag < 4) + bezt->h1= 0; } - - if(bezt->h1==HD_VECT) { /* vector */ - if(flag < 4) bezt->h1= 0; - } - if(bezt->h2==HD_VECT) { /* vector */ - if( flag > 3) bezt->h2= 0; + if(bezt->h2==HD_VECT) { + /* only right half (4 or 2+4) */ + if (flag > 3) + bezt->h2= 0; } } - bezt++; } + /* recalculate handles */ calchandles_ipocurve(icu); } - +/* This function sorts BezTriples so that they are arranged in chronological order, + * as tools working on IPO-Curves expect that the BezTriples are in order. + */ void sort_time_ipocurve(IpoCurve *icu) { - BezTriple *bezt; - int a, ok= 1; + short ok= 1; - while(ok) { + /* keep adjusting order of beztriples until nothing moves (bubble-sort) */ + while (ok) { ok= 0; - - if(icu->bezt) { - bezt= icu->bezt; - a= icu->totvert; - while(a--) { - if(a>0) { - if( bezt->vec[1][0] > (bezt+1)->vec[1][0]) { + + /* currently, will only be needed when there are beztriples */ + if (icu->bezt) { + BezTriple *bezt; + int a; + + /* loop over ALL points to adjust position in array and recalculate handles */ + for (a=0, bezt=icu->bezt; a < icu->totvert; a++, bezt++) { + /* check if thee's a next beztriple which we could try to swap with current */ + if (a < (icu->totvert-1)) { + /* swap if one is after the other (and indicate that order has changed) */ + if (bezt->vec[1][0] > (bezt+1)->vec[1][0]) { SWAP(BezTriple, *bezt, *(bezt+1)); ok= 1; } + + /* if either one of both of the points exceeds crosses over the keyframe time... */ + if ( (bezt->vec[0][0] > bezt->vec[1][0]) && (bezt->vec[2][0] < bezt->vec[1][0]) ) { + /* swap handles if they have switched sides for some reason */ + SWAP(float, bezt->vec[0][0], bezt->vec[2][0]); + SWAP(float, bezt->vec[0][1], bezt->vec[2][1]); + } + else { + /* clamp handles */ + if (bezt->vec[0][0] > bezt->vec[1][0]) + bezt->vec[0][0]= bezt->vec[1][0]; + if (bezt->vec[2][0] < bezt->vec[1][0]) + bezt->vec[2][0]= bezt->vec[1][0]; + } } - if(bezt->vec[0][0]>bezt->vec[1][0] && bezt->vec[2][0]<bezt->vec[1][0]) { - SWAP(float, bezt->vec[0][0], bezt->vec[2][0]); - SWAP(float, bezt->vec[0][1], bezt->vec[2][1]); - } - else { - if(bezt->vec[0][0]>bezt->vec[1][0]) bezt->vec[0][0]= bezt->vec[1][0]; - if(bezt->vec[2][0]<bezt->vec[1][0]) bezt->vec[2][0]= bezt->vec[1][0]; - } - bezt++; } } - else { - - } } } -int test_time_ipocurve(IpoCurve *icu) +/* This function tests if any BezTriples are out of order, thus requiring a sort */ +int test_time_ipocurve (IpoCurve *icu) { - BezTriple *bezt; int a; - if(icu->bezt) { - bezt= icu->bezt; - a= icu->totvert-1; - while(a--) { - if( bezt->vec[1][0] > (bezt+1)->vec[1][0]) { - return 1; - } - bezt++; - } - } - else { + /* currently, only need to test beztriples */ + if (icu->bezt) { + BezTriple *bezt; + /* loop through all beztriples, stopping when one exceeds the one after it */ + for (a=0, bezt= icu->bezt; a < (icu->totvert - 1); a++, bezt++) { + if (bezt->vec[1][0] > (bezt+1)->vec[1][0]) + return 1; + } } - + + /* none need any swapping */ return 0; } -void correct_bezpart(float *v1, float *v2, float *v3, float *v4) +/* --------- */ + +/* The total length of the handles is not allowed to be more + * than the horizontal distance between (v1-v4). + * This is to prevent curve loops. +*/ +void correct_bezpart (float *v1, float *v2, float *v3, float *v4) { - /* the total length of the handles is not allowed to be more - * than the horizontal distance between (v1-v4) - * this to prevent curve loops - */ float h1[2], h2[2], len1, len2, len, fac; + /* calculate handle deltas */ h1[0]= v1[0]-v2[0]; h1[1]= v1[1]-v2[1]; h2[0]= v4[0]-v3[0]; h2[1]= v4[1]-v3[1]; + /* calculate distances: + * - len = span of time between keyframes + * - len1 = length of handle of start key + * - len2 = length of handle of end key + */ len= v4[0]- v1[0]; len1= (float)fabs(h1[0]); len2= (float)fabs(h2[0]); - if(len1+len2==0.0) return; - if(len1+len2 > len) { + /* if the handles have no length, no need to do any corrections */ + if ((len1+len2) == 0.0) + return; + + /* the two handles cross over each other, so force them + * apart using the proportion they overlap + */ + if ((len1+len2) > len) { fac= len/(len1+len2); v2[0]= (v1[0]-fac*h1[0]); @@ -636,57 +798,86 @@ void correct_bezpart(float *v1, float *v2, float *v3, float *v4) v3[0]= (v4[0]-fac*h2[0]); v3[1]= (v4[1]-fac*h2[1]); - } } -/* *********************** ARITH *********************** */ +#if 0 // TODO: enable when we have per-segment interpolation +/* This function sets the interpolation mode for an entire Ipo-Curve. + * It is primarily used for patching old files, but is also used in the interface + * to make sure that all segments of the curve use the same interpolation. + */ +void set_interpolation_ipocurve (IpoCurve *icu, short ipo) +{ + BezTriple *bezt; + int a; + + /* validate arguments */ + if (icu == NULL) return; + if (ELEM3(ipo, IPO_CONST, IPO_LIN, IPO_BEZ)==0) return; -int findzero(float x, float q0, float q1, float q2, float q3, float *o) + /* set interpolation mode for whole curve */ + icu->ipo= ipo; + + /* set interpolation mode of all beztriples */ + for (a=0, bezt=icu->bezt; a<icu->totvert; a++, bezt++) + bezt->ipo= ipo; +} +#endif // TODO: enable when we have per-segment interpolation + +/* ***************************** Curve Calculations ********************************* */ + +/* find root/zero */ +int findzero (float x, float q0, float q1, float q2, float q3, float *o) { double c0, c1, c2, c3, a, b, c, p, q, d, t, phi; int nr= 0; - c0= q0-x; - c1= 3*(q1-q0); - c2= 3*(q0-2*q1+q2); - c3= q3-q0+3*(q1-q2); + c0= q0 - x; + c1= 3 * (q1 - q0); + c2= 3 * (q0 - 2*q1 + q2); + c3= q3 - q0 + 3 * (q1 - q2); - if(c3!=0.0) { + if (c3 != 0.0) { a= c2/c3; b= c1/c3; c= c0/c3; a= a/3; - - p= b/3-a*a; - q= (2*a*a*a-a*b+c)/2; - d= q*q+p*p*p; - - if(d>0.0) { + + p= b/3 - a*a; + q= (2*a*a*a - a*b + c) / 2; + d= q*q + p*p*p; + + if (d > 0.0) { t= sqrt(d); - o[0]= (float)(Sqrt3d(-q+t)+Sqrt3d(-q-t)-a); - if(o[0]>= SMALL && o[0]<=1.000001) return 1; + o[0]= (float)(Sqrt3d(-q+t) + Sqrt3d(-q-t) - a); + + if ((o[0] >= SMALL) && (o[0] <= 1.000001)) return 1; else return 0; } - else if(d==0.0) { + else if (d == 0.0) { t= Sqrt3d(-q); - o[0]= (float)(2*t-a); - if(o[0]>=SMALL && o[0]<=1.000001) nr++; + o[0]= (float)(2*t - a); + + if ((o[0] >= SMALL) && (o[0] <= 1.000001)) nr++; o[nr]= (float)(-t-a); - if(o[nr]>=SMALL && o[nr]<=1.000001) return nr+1; + + if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) return nr+1; else return nr; } else { - phi= acos(-q/sqrt(-(p*p*p))); + phi= acos(-q / sqrt(-(p*p*p))); t= sqrt(-p); p= cos(phi/3); - q= sqrt(3-3*p*p); - o[0]= (float)(2*t*p-a); - if(o[0]>=SMALL && o[0]<=1.000001) nr++; - o[nr]= (float)(-t*(p+q)-a); - if(o[nr]>=SMALL && o[nr]<=1.000001) nr++; - o[nr]= (float)(-t*(p-q)-a); - if(o[nr]>=SMALL && o[nr]<=1.000001) return nr+1; + q= sqrt(3 - 3*p*p); + o[0]= (float)(2*t*p - a); + + if ((o[0] >= SMALL) && (o[0] <= 1.000001)) nr++; + o[nr]= (float)(-t * (p + q) - a); + + if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) nr++; + o[nr]= (float)(-t * (p - q) - a); + + if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) return nr+1; else return nr; } } @@ -695,83 +886,97 @@ int findzero(float x, float q0, float q1, float q2, float q3, float *o) b=c1; c=c0; - if(a!=0.0) { - p=b*b-4*a*c; - if(p>0) { + if (a != 0.0) { + // discriminant + p= b*b - 4*a*c; + + if (p > 0) { p= sqrt(p); - o[0]= (float)((-b-p)/(2*a)); - if(o[0]>=SMALL && o[0]<=1.000001) nr++; + o[0]= (float)((-b-p) / (2 * a)); + + if ((o[0] >= SMALL) && (o[0] <= 1.000001)) nr++; o[nr]= (float)((-b+p)/(2*a)); - if(o[nr]>=SMALL && o[nr]<=1.000001) return nr+1; + + if ((o[nr] >= SMALL) && (o[nr] <= 1.000001)) return nr+1; else return nr; } - else if(p==0) { - o[0]= (float)(-b/(2*a)); - if(o[0]>=SMALL && o[0]<=1.000001) return 1; + else if (p == 0) { + o[0]= (float)(-b / (2 * a)); + if ((o[0] >= SMALL) && (o[0] <= 1.000001)) return 1; else return 0; } } - else if(b!=0.0) { + else if (b != 0.0) { o[0]= (float)(-c/b); - if(o[0]>=SMALL && o[0]<=1.000001) return 1; + + if ((o[0] >= SMALL) && (o[0] <= 1.000001)) return 1; else return 0; } - else if(c==0.0) { + else if (c == 0.0) { o[0]= 0.0; return 1; } + return 0; } } -void berekeny(float f1, float f2, float f3, float f4, float *o, int b) +void berekeny (float f1, float f2, float f3, float f4, float *o, int b) { float t, c0, c1, c2, c3; int a; c0= f1; - c1= 3.0f*(f2 - f1); - c2= 3.0f*(f1 - 2.0f*f2 + f3); - c3= f4 - f1 + 3.0f*(f2-f3); + c1= 3.0f * (f2 - f1); + c2= 3.0f * (f1 - 2.0f*f2 + f3); + c3= f4 - f1 + 3.0f * (f2 - f3); - for(a=0; a<b; a++) { + for (a=0; a < b; a++) { t= o[a]; - o[a]= c0+t*c1+t*t*c2+t*t*t*c3; + o[a]= c0 + t*c1 + t*t*c2 + t*t*t*c3; } } -void berekenx(float *f, float *o, int b) +void berekenx (float *f, float *o, int b) { float t, c0, c1, c2, c3; int a; c0= f[0]; - c1= 3*(f[3]-f[0]); - c2= 3*(f[0]-2*f[3]+f[6]); - c3= f[9]-f[0]+3*(f[3]-f[6]); - for(a=0; a<b; a++) { + c1= 3 * (f[3] - f[0]); + c2= 3 * (f[0] - 2*f[3] + f[6]); + c3= f[9] - f[0] + 3 * (f[3] - f[6]); + + for (a=0; a < b; a++) { t= o[a]; - o[a]= c0+t*c1+t*t*c2+t*t*t*c3; + o[a]= c0 + t*c1 + t*t*c2 + t*t*t*c3; } } -/* we need the local transform = current transform - (parent transform + bone transform) */ -/* (local transform is on action channel level) */ -static void posechannel_get_local_transform(bPoseChannel *pchan, float *loc, float *eul, float *size) +/* ***************************** IPO - Calculations ********************************* */ + +/* ---------------------- Curve Evaluation --------------------------- */ + +/* helper function for evaluating drivers: + * - we need the local transform = current transform - (parent transform + bone transform) + * - (local transform is on action channel level) + */ +static void posechannel_get_local_transform (bPoseChannel *pchan, float loc[], float eul[], float size[]) { - float diff_mat[4][4]; float parmat[4][4], offs_bone[4][4], imat[4][4]; + float diff_mat[4][4]; + /* get first the parent + bone transform in parmat */ if (pchan->parent) { - /* get first the parent + bone transform in parmat */ - /* bone transform itself */ Mat4CpyMat3(offs_bone, pchan->bone->bone_mat); + /* The bone's root offset (is in the parent's coordinate system) */ VECCOPY(offs_bone[3], pchan->bone->head); + /* Get the length translation of parent (length along y axis) */ offs_bone[3][1]+= pchan->parent->bone->length; - + Mat4MulSerie(parmat, pchan->parent->pose_mat, offs_bone, NULL, NULL, NULL, NULL, NULL, NULL); /* invert it */ @@ -780,63 +985,78 @@ static void posechannel_get_local_transform(bPoseChannel *pchan, float *loc, flo else { Mat4CpyMat3(offs_bone, pchan->bone->bone_mat); VECCOPY(offs_bone[3], pchan->bone->head); - + /* invert it */ Mat4Invert(imat, offs_bone); - } /* difference: current transform - (parent transform + bone transform) */ Mat4MulMat4(diff_mat, pchan->pose_mat, imat); - if(loc) + /* extract relevant components */ + if (loc) VECCOPY(loc, diff_mat[3]); - if(eul) + if (eul) Mat4ToEul(diff_mat, eul); - if(size) + if (size) Mat4ToSize(diff_mat, size); - } -/* has to return a float value */ -static float eval_driver(IpoDriver *driver, float ipotime) +/* evaluate an IPO-driver to get a 'time' value to use instead of "ipotime" + * - "ipotime" is the frame at which IPO-curve is being evaluated + * - has to return a float value + */ +static float eval_driver (IpoDriver *driver, float ipotime) { - - if(driver->type == IPO_DRIVER_TYPE_PYTHON) { + /* currently, drivers are either PyDrivers (evaluating a PyExpression, or Object/Pose-Channel transforms) */ + if (driver->type == IPO_DRIVER_TYPE_PYTHON) { /* check for empty or invalid expression */ - if ((driver->name[0] == '\0') || - (driver->flag & IPO_DRIVER_FLAG_INVALID)) + if ( (driver->name[0] == '\0') || + (driver->flag & IPO_DRIVER_FLAG_INVALID) ) + { return 0.0f; - /* this evals the expression and returns its result: - * (on errors it reports, then returns 0.0f) */ + } + + /* this evaluates the expression using Python,and returns its result: + * - on errors it reports, then returns 0.0f + */ return BPY_pydriver_eval(driver); } else { Object *ob= driver->ob; - - if(ob==NULL) return 0.0f; - if(ob->proxy_from) + + /* must have an object to evaluate */ + if (ob == NULL) + return 0.0f; + + /* if a proxy, use the proxy source*/ + if (ob->proxy_from) ob= ob->proxy_from; - if(driver->blocktype==ID_OB) { - /* depsgraph failure; ob ipos are calculated in where_is_object, this might get called too late */ - if(ob->ipo && ob->ctime!=ipotime) { + /* use given object as driver */ + if (driver->blocktype == ID_OB) { + /* depsgraph failure: ob ipos are calculated in where_is_object, this might get called too late */ + if ((ob->ipo) && (ob->ctime != ipotime)) { + /* calculate the value of relevant channel on the Object, but do not write the value + * calculated on to the Object but onto "ipotime" instead + */ calc_ipo_spec(ob->ipo, driver->adrcode, &ipotime); return ipotime; } - switch(driver->adrcode) { + /* return the value of the relevant channel */ + switch (driver->adrcode) { case OB_LOC_X: return ob->loc[0]; case OB_LOC_Y: return ob->loc[1]; case OB_LOC_Z: return ob->loc[2]; - case OB_ROT_X: + case OB_ROT_X: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */ return ob->rot[0]/(M_PI_2/9.0); - case OB_ROT_Y: + case OB_ROT_Y: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */ return ob->rot[1]/(M_PI_2/9.0); - case OB_ROT_Z: + case OB_ROT_Z: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */ return ob->rot[2]/(M_PI_2/9.0); case OB_SIZE_X: return ob->size[0]; @@ -846,14 +1066,20 @@ static float eval_driver(IpoDriver *driver, float ipotime) return ob->size[2]; } } + + /* use given pose-channel as driver */ else { /* ID_AR */ bPoseChannel *pchan= get_pose_channel(ob->pose, driver->name); - if(pchan && pchan->bone) { - - /* rotation difference is not a simple driver (i.e. value drives value), but the angle between 2 bones is driving stuff... which is useful */ - if(driver->adrcode==OB_ROT_DIFF) { + + /* must have at least 1 bone to use */ + if (pchan && pchan->bone) { + /* rotation difference is not a simple driver (i.e. value drives value), but the angle between 2 bones is driving stuff... + * - the name of the second pchan is also stored in driver->name, but packed after the other one by DRIVER_NAME_OFFS chars + */ + if (driver->adrcode == OB_ROT_DIFF) { bPoseChannel *pchan2= get_pose_channel(ob->pose, driver->name+DRIVER_NAME_OFFS); - if(pchan2 && pchan2->bone) { + + if (pchan2 && pchan2->bone) { float q1[4], q2[4], quat[4], angle; Mat4ToQuat(pchan->pose_mat, q1); @@ -864,26 +1090,32 @@ static float eval_driver(IpoDriver *driver, float ipotime) angle = 2.0f * (saacos(quat[0])); angle= ABS(angle); - return angle>M_PI?2.0f*M_PI-angle:angle; + return (angle > M_PI) ? ((2.0f * M_PI) - angle) : (angle); } } + + /* standard driver */ else { float loc[3], eul[3], size[3]; + /* retrieve local transforms to return + * - we use eulers here NOT quats, so that Objects can be driven by bones easily + * also, this way is more understandable for users + */ posechannel_get_local_transform(pchan, loc, eul, size); - - switch(driver->adrcode) { + + switch (driver->adrcode) { case OB_LOC_X: return loc[0]; case OB_LOC_Y: return loc[1]; case OB_LOC_Z: return loc[2]; - case OB_ROT_X: + case OB_ROT_X: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */ return eul[0]/(M_PI_2/9.0); - case OB_ROT_Y: + case OB_ROT_Y: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */ return eul[1]/(M_PI_2/9.0); - case OB_ROT_Z: + case OB_ROT_Z: /* hack: euler rotations are divided by 10 deg to fit on same axes as other channels */ return eul[2]/(M_PI_2/9.0); case OB_SIZE_X: return size[0]; @@ -896,227 +1128,722 @@ static float eval_driver(IpoDriver *driver, float ipotime) } } } + + /* return 0.0f, as couldn't find relevant data to use */ return 0.0f; } -float eval_icu(IpoCurve *icu, float ipotime) +/* evaluate and return the value of the given IPO-curve at the specified frame ("evaltime") */ +float eval_icu(IpoCurve *icu, float evaltime) { - BezTriple *bezt, *prevbezt; - float v1[2], v2[2], v3[2], v4[2], opl[32], dx, fac; - float cycdx, cycdy, ofs, cycyofs, cvalue = 0.0; - int a, b; - - cycyofs= 0.0; + float cvalue = 0.0f; - if(icu->driver) { + /* if there is a driver, evaluate it to find value to use as "evaltime" + * - this value will also be returned as the value of the 'curve', if there are no keyframes + */ + if (icu->driver) { /* ipotime now serves as input for the curve */ - ipotime= cvalue= eval_driver(icu->driver, ipotime); + evaltime= cvalue= eval_driver(icu->driver, evaltime); } - if(icu->bezt) { + + /* there are keyframes (in the form of BezTriples) which can be interpolated between */ + if (icu->bezt) { + /* get pointers */ + BezTriple *bezt, *prevbezt, *lastbezt; + float v1[2], v2[2], v3[2], v4[2], opl[32], dx, fac; + float cycdx, cycdy, ofs, cycyofs= 0.0; + int a, b; + + /* get pointers */ + a= icu->totvert-1; prevbezt= icu->bezt; bezt= prevbezt+1; - a= icu->totvert-1; + lastbezt= prevbezt + a; - /* cyclic? */ - if(icu->extrap & IPO_CYCL) { - ofs= icu->bezt->vec[1][0]; - cycdx= (icu->bezt+icu->totvert-1)->vec[1][0] - ofs; - cycdy= (icu->bezt+icu->totvert-1)->vec[1][1] - icu->bezt->vec[1][1]; - if(cycdx!=0.0) { - - if(icu->extrap & IPO_DIR) { - cycyofs= (float)floor((ipotime-ofs)/cycdx); - cycyofs*= cycdy; + /* extrapolation mode is 'cyclic' - find relative place within a cycle */ + if (icu->extrap & IPO_CYCL) { + /* ofs is start frame of cycle */ + ofs= prevbezt->vec[1][0]; + + /* calculate period and amplitude (total height) of a cycle */ + cycdx= lastbezt->vec[1][0] - prevbezt->vec[1][0]; + cycdy= lastbezt->vec[1][1] - prevbezt->vec[1][1]; + + /* cycle occurs over some period of time (cycdx should be positive all the time) */ + if (cycdx) { + /* check if 'cyclic extrapolation', and thus calculate y-offset for this cycle + * - IPO_CYCLX = (IPO_CYCL + IPO_DIR) + */ + if (icu->extrap & IPO_DIR) { + cycyofs = (float)floor((evaltime - ofs) / cycdx); + cycyofs *= cycdy; } - - ipotime= (float)(fmod(ipotime-ofs, cycdx)+ofs); - if(ipotime<ofs) ipotime+= cycdx; + + /* calculate where in the cycle we are (overwrite evaltime to reflect this) */ + evaltime= (float)(fmod(evaltime-ofs, cycdx) + ofs); + if (evaltime < ofs) evaltime += cycdx; } } - /* endpoints? */ - - if(prevbezt->vec[1][0]>=ipotime) { - if( (icu->extrap & IPO_DIR) && icu->ipo!=IPO_CONST) { - dx= prevbezt->vec[1][0]-ipotime; - fac= prevbezt->vec[1][0]-prevbezt->vec[0][0]; - if(fac!=0.0) { - fac= (prevbezt->vec[1][1]-prevbezt->vec[0][1])/fac; - cvalue= prevbezt->vec[1][1]-fac*dx; + /* evaluation time at or past endpoints? */ + // TODO: for per-bezt interpolation, replace all icu->ipo with (bezt)->ipo + if (prevbezt->vec[1][0] >= evaltime) { + /* before or on first keyframe */ + if ((icu->extrap & IPO_DIR) && (icu->ipo != IPO_CONST)) { + /* linear or bezier interpolation */ + if (icu->ipo==IPO_LIN) { + /* Use the next center point instead of our own handle for + * linear interpolated extrapolate + */ + if (icu->totvert == 1) + cvalue= prevbezt->vec[1][1]; + else { + bezt = prevbezt+1; + dx= prevbezt->vec[1][0] - evaltime; + fac= bezt->vec[1][0] - prevbezt->vec[1][0]; + + /* prevent division by zero */ + if (fac) { + fac= (bezt->vec[1][1] - prevbezt->vec[1][1]) / fac; + cvalue= prevbezt->vec[1][1] - (fac * dx); + } + else + cvalue= prevbezt->vec[1][1]; + } + } + else { + /* Use the first handle (earlier) of first BezTriple to calculate the + * gradient and thus the value of the curve at evaltime + */ + dx= prevbezt->vec[1][0] - evaltime; + fac= prevbezt->vec[1][0] - prevbezt->vec[0][0]; + + /* prevent division by zero */ + if (fac) { + fac= (prevbezt->vec[1][1] - prevbezt->vec[0][1]) / fac; + cvalue= prevbezt->vec[1][1] - (fac * dx); + } + else + cvalue= prevbezt->vec[1][1]; } - else cvalue= prevbezt->vec[1][1]; } - else cvalue= prevbezt->vec[1][1]; - - cvalue+= cycyofs; + else { + /* constant (IPO_HORIZ) extrapolation or constant interpolation, + * so just extend first keyframe's value + */ + cvalue= prevbezt->vec[1][1]; + } } - else if( (prevbezt+a)->vec[1][0]<=ipotime) { - if( (icu->extrap & IPO_DIR) && icu->ipo!=IPO_CONST) { - prevbezt+= a; - dx= ipotime-prevbezt->vec[1][0]; - fac= prevbezt->vec[2][0]-prevbezt->vec[1][0]; - - if(fac!=0) { - fac= (prevbezt->vec[2][1]-prevbezt->vec[1][1])/fac; - cvalue= prevbezt->vec[1][1]+fac*dx; + else if (lastbezt->vec[1][0] <= evaltime) { + /* after or on last keyframe */ + if( (icu->extrap & IPO_DIR) && (icu->ipo != IPO_CONST)) { + /* linear or bezier interpolation */ + if (icu->ipo==IPO_LIN) { + /* Use the next center point instead of our own handle for + * linear interpolated extrapolate + */ + if (icu->totvert == 1) + cvalue= lastbezt->vec[1][1]; + else { + prevbezt = lastbezt - 1; + dx= evaltime - lastbezt->vec[1][0]; + fac= lastbezt->vec[1][0] - prevbezt->vec[1][0]; + + /* prevent division by zero */ + if (fac) { + fac= (lastbezt->vec[1][1] - prevbezt->vec[1][1]) / fac; + cvalue= lastbezt->vec[1][1] + (fac * dx); + } + else + cvalue= lastbezt->vec[1][1]; + } + } + else { + /* Use the gradient of the second handle (later) of last BezTriple to calculate the + * gradient and thus the value of the curve at evaltime + */ + dx= evaltime - lastbezt->vec[1][0]; + fac= lastbezt->vec[2][0] - lastbezt->vec[1][0]; + + /* prevent division by zero */ + if (fac) { + fac= (lastbezt->vec[2][1] - lastbezt->vec[1][1]) / fac; + cvalue= lastbezt->vec[1][1] + (fac * dx); + } + else + cvalue= lastbezt->vec[1][1]; } - else cvalue= prevbezt->vec[1][1]; } - else cvalue= (prevbezt+a)->vec[1][1]; - - cvalue+= cycyofs; + else { + /* constant (IPO_HORIZ) extrapolation or constant interpolation, + * so just extend last keyframe's value + */ + cvalue= lastbezt->vec[1][1]; + } } else { - while(a--) { - if(prevbezt->vec[1][0]<=ipotime && bezt->vec[1][0]>=ipotime) { - if(icu->ipo==IPO_CONST) { - cvalue= prevbezt->vec[1][1]+cycyofs; + /* evaltime occurs somewhere in the middle of the curve */ + // TODO: chould be optimised by using a binary search instead??? + for (a=0; prevbezt && bezt && (a < icu->totvert-1); a++, prevbezt=bezt, bezt++) { + /* evaltime occurs within the interval defined by these two keyframes */ + if ((prevbezt->vec[1][0] <= evaltime) && (bezt->vec[1][0] >= evaltime)) { + /* value depends on interpolation mode */ + if (icu->ipo == IPO_CONST) { + /* constant (evaltime not relevant, so no interpolation needed) */ + cvalue= prevbezt->vec[1][1]; } - else if(icu->ipo==IPO_LIN) { - fac= bezt->vec[1][0]-prevbezt->vec[1][0]; - if(fac==0) cvalue= cycyofs+prevbezt->vec[1][1]; - else { - fac= (ipotime-prevbezt->vec[1][0])/fac; - cvalue= cycyofs+prevbezt->vec[1][1]+ fac*(bezt->vec[1][1]-prevbezt->vec[1][1]); + else if (icu->ipo == IPO_LIN) { + /* linear - interpolate between values of the two keyframes */ + fac= bezt->vec[1][0] - prevbezt->vec[1][0]; + + /* prevent division by zero */ + if (fac) { + fac= (evaltime - prevbezt->vec[1][0]) / fac; + cvalue= prevbezt->vec[1][1] + (fac * (bezt->vec[1][1] - prevbezt->vec[1][1])); } + else + cvalue= prevbezt->vec[1][1]; } else { + /* bezier interpolation */ + /* v1,v2 are the first keyframe and its 2nd handle */ v1[0]= prevbezt->vec[1][0]; v1[1]= prevbezt->vec[1][1]; v2[0]= prevbezt->vec[2][0]; v2[1]= prevbezt->vec[2][1]; - + /* v3,v4 are the last keyframe's 1st handle + the last keyframe */ v3[0]= bezt->vec[0][0]; v3[1]= bezt->vec[0][1]; v4[0]= bezt->vec[1][0]; v4[1]= bezt->vec[1][1]; - + + /* adjust handles so that they don't overlap (forming a loop) */ correct_bezpart(v1, v2, v3, v4); - b= findzero(ipotime, v1[0], v2[0], v3[0], v4[0], opl); - if(b) { + /* try to get a value for this position - if failure, try another set of points */ + b= findzero(evaltime, v1[0], v2[0], v3[0], v4[0], opl); + if (b) { berekeny(v1[1], v2[1], v3[1], v4[1], opl, 1); - cvalue= opl[0]+cycyofs; + cvalue= opl[0]; break; } } } - prevbezt= bezt; - bezt++; } } + + /* apply y-offset (for 'cyclic extrapolation') to calculated value */ + cvalue+= cycyofs; } - - if(icu->ymin < icu->ymax) { - if(cvalue < icu->ymin) cvalue= icu->ymin; - else if(cvalue > icu->ymax) cvalue= icu->ymax; + + /* clamp evaluated value to lie within allowable value range for this channel */ + if (icu->ymin < icu->ymax) { + CLAMP(cvalue, icu->ymin, icu->ymax); } + /* return evaluated value */ return cvalue; } -void calc_icu(IpoCurve *icu, float ctime) +/* ------------------- IPO-Block/Curve Calculation - General API ----------------------- */ + +/* calculate the value of the given IPO-curve at the current frame, and set its curval */ +void calc_icu (IpoCurve *icu, float ctime) { + /* calculate and set curval (evaluates driver too) */ icu->curval= eval_icu(icu, ctime); } -float calc_ipo_time(Ipo *ipo, float ctime) +/* calculate for the current frame, all IPO-curves in IPO-block that can be evaluated + * - icu->curval is set for all IPO-curves which are evaluated! + */ +void calc_ipo (Ipo *ipo, float ctime) { + IpoCurve *icu; + + /* if there is no IPO block to evaluate, or whole block is "muted" */ + if (ipo == NULL) return; + if (ipo->muteipo) return; + + /* loop over all curves */ + for (icu= ipo->curve.first; icu; icu= icu->next) { + /* only evaluated curve if allowed to: + * - Muted channels should not be evaluated as they shouldn't have any effect + * --> user explictly turned them off! + * - Drivers should be evaluated at all updates + * --> TODO Note: drivers should be separated from standard channels + * - IPO_LOCK is not set, as it is set by some internal mechanisms to prevent + * IPO-curve from overwriting data (currently only used for IPO-Record). + */ + if ((icu->driver) || (icu->flag & IPO_LOCK)==0) { + if ((icu->flag & IPO_MUTE)==0) + calc_icu(icu, ctime); + } + } +} - if(ipo && ipo->blocktype==ID_OB) { - IpoCurve *icu= ipo->curve.first; +/* ------------------- IPO-Block/Curve Calculation - Special Hacks ----------------------- */ - while(icu) { - if (icu->adrcode==OB_TIME) { - calc_icu(icu, ctime); - return 10.0f*icu->curval; - } - icu= icu->next; - } +/* Calculate and return the value of the 'Time' Ipo-Curve from an Object, + * OR return the current time if not found + * - used in object.c -> bsystem_time() + */ +float calc_ipo_time (Ipo *ipo, float ctime) +{ + /* only Time IPO from Object IPO-blocks are relevant */ + if ((ipo) && (ipo->blocktype == ID_OB)) { + IpoCurve *icu= find_ipocurve(ipo, OB_TIME); + + /* only calculate (and set icu->curval) for time curve */ + if (icu) { + calc_icu(icu, ctime); + return (10.0f * icu->curval); + } } + /* no appropriate time-curve found */ return ctime; } -void calc_ipo(Ipo *ipo, float ctime) +/* Evaluate the specified channel in the given IPO block on the specified frame (ctime), + * writing the value into that channel's icu->curval, but ALSO dumping it in ctime. + * - Returns success and modifies ctime! + */ +short calc_ipo_spec (Ipo *ipo, int adrcode, float *ctime) +{ + IpoCurve *icu= find_ipocurve(ipo, adrcode); + + /* only evaluate if found */ + if (icu) { + /* only calculate if allowed to (not locked and not muted) + * - drivers not taken into account, because this may be called when calculating a driver + */ + if ((icu->flag & (IPO_LOCK|IPO_MUTE))==0) + calc_icu(icu, *ctime); + + /* value resulting from calculations is written into ctime! */ + *ctime= icu->curval; + return 1; + } + + /* couldn't evaluate */ + return 0; +} + +/* ***************************** IPO - DataAPI ********************************* */ + +/* --------------------- Flush/Execute IPO Values ----------------------------- */ + +/* Flush IpoCurve->curvals to the data they affect (defined by ID) + * - not for Actions or Constraints! (those have their own special handling) + */ +void execute_ipo (ID *id, Ipo *ipo) { IpoCurve *icu; + void *poin; + int type; - if(ipo==NULL) return; - if(ipo->muteipo) return; + /* don't do anything without an IPO block */ + if (ipo == NULL) + return; - for(icu= ipo->curve.first; icu; icu= icu->next) { - if(icu->driver || (icu->flag & IPO_LOCK)==0) { - if((icu->flag & IPO_MUTE)==0) - calc_icu(icu, ctime); + /* loop over IPO Curves, getting pointer to var to affect, and write into that pointer */ + for (icu= ipo->curve.first; icu; icu= icu->next) { + poin= get_ipo_poin(id, icu, &type); + if (poin) write_ipo_poin(poin, type, icu->curval); + } +} + +/* Flush Action-Channel IPO data to Pose Channel */ +void execute_action_ipo (bActionChannel *achan, bPoseChannel *pchan) +{ + /* only do this if there's an Action Channel and Pose Channel to use */ + if (achan && achan->ipo && pchan) { + IpoCurve *icu; + + /* loop over IPO-curves, getting a pointer to pchan var to write to + * - assume for now that only 'float' channels will ever get written into + */ + for (icu= achan->ipo->curve.first; icu; icu= icu->next) { + void *poin= get_pchan_ipo_poin(pchan, icu->adrcode); + if (poin) write_ipo_poin(poin, IPO_FLOAT, icu->curval); } } } -/* ************************************** */ -/* DO THE IPO! */ -/* ************************************** */ -void write_ipo_poin(void *poin, int type, float val) +/* --------------------- Force Calculation + Flush IPO Values ----------------------------- */ + +/* Calculate values for given IPO block, then flush to all of block's users + * - for general usage + */ +void do_ipo (Ipo *ipo) { + if (ipo) { + float ctime= frame_to_float(G.scene->r.cfra); + + /* calculate values, then flush to all users of this IPO block */ + calc_ipo(ipo, ctime); + do_ipo_nocalc(ipo); + } +} - switch(type) { - case IPO_FLOAT: - *( (float *)poin)= val; - break; - case IPO_FLOAT_DEGR: - *( (float *)poin)= (float)(val*M_PI_2/9.0); - break; - case IPO_INT: - case IPO_INT_BIT: - case IPO_LONG: - *( (int *)poin)= (int)val; - break; - case IPO_SHORT: - case IPO_SHORT_BIT: - *( (short *)poin)= (short)val; - break; - case IPO_CHAR: - case IPO_CHAR_BIT: - *( (char *)poin)= (char)val; - break; +/* Calculate values for given Material's IPO block, then flush to given Material only */ +void do_mat_ipo (Material *ma) +{ + float ctime; + + if (ELEM(NULL, ma, ma->ipo)) + return; + + ctime= frame_to_float(G.scene->r.cfra); + /* if(ob->ipoflag & OB_OFFS_OB) ctime-= ob->sf; */ + + /* calculate values for current time, then flush values to given material only */ + calc_ipo(ma->ipo, ctime); + execute_ipo((ID *)ma, ma->ipo); +} + +/* Calculate values for given Object's IPO block, then flush to given Object only + * - there's also some funky stuff that looks like it's for scene layers + */ +void do_ob_ipo (Object *ob) +{ + float ctime; + unsigned int lay; + + if (ob->ipo == NULL) + return; + + /* do not set ob->ctime here: for example when parent in invisible layer */ + ctime= bsystem_time(ob, (float) G.scene->r.cfra, 0.0); + + /* calculate values of */ + calc_ipo(ob->ipo, ctime); + + /* Patch: remember localview */ + lay= ob->lay & 0xFF000000; + + /* flush IPO values to this object only */ + execute_ipo((ID *)ob, ob->ipo); + + /* hack: for layer animation??? - is this what this is? (Aligorith, 28Sep2008) */ + ob->lay |= lay; + if ((ob->id.name[2]=='S') && (ob->id.name[3]=='C') && (ob->id.name[4]=='E')) { + if (strcmp(G.scene->id.name+2, ob->id.name+6)==0) { + G.scene->lay= ob->lay; + copy_view3d_lock(0); + /* no redraw here! creates too many calls */ + } } } -float read_ipo_poin(void *poin, int type) +/* Only execute those IPO-Curves with drivers, on the current frame, for the given Object + * - TODO: Drivers should really be made separate from standard anim channels + */ +void do_ob_ipodrivers (Object *ob, Ipo *ipo, float ctime) { - float val = 0.0; + IpoCurve *icu; + void *poin; + int type; - switch(type) { - case IPO_FLOAT: - val= *( (float *)poin); + for (icu= ipo->curve.first; icu; icu= icu->next) { + if (icu->driver) { + icu->curval= eval_icu(icu, ctime); + + poin= get_ipo_poin((ID *)ob, icu, &type); + if (poin) write_ipo_poin(poin, type, icu->curval); + } + } +} + +/* Special variation to calculate IPO values for Sequence + perform other stuff */ +void do_seq_ipo (Sequence *seq, int cfra) +{ + float ctime, div; + + /* seq_ipo has an exception: calc both fields immediately */ + if (seq->ipo) { + if ((seq->flag & SEQ_IPO_FRAME_LOCKED) != 0) { + ctime = frame_to_float(cfra); + div = 1.0; + } + else { + ctime= frame_to_float(cfra - seq->startdisp); + div= (seq->enddisp - seq->startdisp) / 100.0f; + if (div == 0.0) return; + } + + /* 2nd field */ + calc_ipo(seq->ipo, (ctime+0.5f)/div); + execute_ipo((ID *)seq, seq->ipo); + seq->facf1= seq->facf0; + + /* 1st field */ + calc_ipo(seq->ipo, ctime/div); + execute_ipo((ID *)seq, seq->ipo); + } + else + seq->facf1= seq->facf0= 1.0f; +} + +/* --------- */ + + +/* exception: it does calc for objects... + * now find out why this routine was used anyway! + */ +void do_ipo_nocalc (Ipo *ipo) +{ + Object *ob; + Material *ma; + Tex *tex; + World *wo; + Lamp *la; + Camera *ca; + bSound *snd; + + if (ipo == NULL) + return; + + /* only flush IPO values (without calculating first/again) on + * to the datablocks that use the given IPO block + */ + switch (ipo->blocktype) { + case ID_OB: + for (ob= G.main->object.first; ob; ob= ob->id.next) { + if (ob->ipo == ipo) do_ob_ipo(ob); + } break; - case IPO_FLOAT_DEGR: - val= *( (float *)poin); - val = (float)(val/(M_PI_2/9.0)); + case ID_MA: + for (ma= G.main->mat.first; ma; ma= ma->id.next) { + if (ma->ipo == ipo) execute_ipo((ID *)ma, ipo); + } break; - case IPO_INT: - case IPO_INT_BIT: - case IPO_LONG: - val= (float)(*( (int *)poin)); + case ID_TE: + for (tex= G.main->tex.first; tex; tex= tex->id.next) { + if (tex->ipo == ipo) execute_ipo((ID *)tex, ipo); + } break; - case IPO_SHORT: - case IPO_SHORT_BIT: - val= *( (short *)poin); + case ID_WO: + for (wo= G.main->world.first; wo; wo= wo->id.next) { + if (wo->ipo == ipo) execute_ipo((ID *)wo, ipo); + } break; - case IPO_CHAR: - case IPO_CHAR_BIT: - val= *( (char *)poin); + case ID_LA: + for (la= G.main->lamp.first; la; la= la->id.next) { + if (la->ipo == ipo) execute_ipo((ID *)la, ipo); + } + break; + case ID_CA: + for (ca= G.main->camera.first; ca; ca= ca->id.next) { + if (ca->ipo == ipo) execute_ipo((ID *)ca, ipo); + } + break; + case ID_SO: + for (snd= G.main->sound.first; snd; snd= snd->id.next) { + if (snd->ipo == ipo) execute_ipo((ID *)snd, ipo); + } break; } - return val; } -static void *give_tex_poin(Tex *tex, int adrcode, int *type ) +/* Executes IPO's for whole database on frame change, in a specified order, + * with datablocks being calculated in alphabetical order + * - called on scene_update_for_newframe() only + */ +void do_all_data_ipos () { - void *poin=0; + Material *ma; + Tex *tex; + World *wo; + Ipo *ipo; + Lamp *la; + Key *key; + Camera *ca; + bSound *snd; + Sequence *seq; + Editing *ed; + Base *base; + float ctime; + + ctime= frame_to_float(G.scene->r.cfra); + + /* this exception cannot be depgraphed yet... what todo with objects in other layers?... */ + for (base= G.scene->base.first; base; base= base->next) { + Object *ob= base->object; + + /* only update layer when an ipo */ + if (has_ipo_code(ob->ipo, OB_LAY)) { + do_ob_ipo(ob); + base->lay= ob->lay; + } + } + + /* layers for the set...*/ + if (G.scene->set) { + for (base= G.scene->set->base.first; base; base= base->next) { + Object *ob= base->object; + + if (has_ipo_code(ob->ipo, OB_LAY)) { + do_ob_ipo(ob); + base->lay= ob->lay; + } + } + } + + /* Calculate all IPO blocks in use, execept those for Objects */ + for (ipo= G.main->ipo.first; ipo; ipo= ipo->id.next) { + if ((ipo->id.us) && (ipo->blocktype != ID_OB)) { + calc_ipo(ipo, ctime); + } + } + + /* Texture Blocks */ + for (tex= G.main->tex.first; tex; tex= tex->id.next) { + if (tex->ipo) execute_ipo((ID *)tex, tex->ipo); + } + + /* Material Blocks */ + for (ma= G.main->mat.first; ma; ma= ma->id.next) { + if (ma->ipo) execute_ipo((ID *)ma, ma->ipo); + } + + /* World Blocks */ + for (wo= G.main->world.first; wo; wo= wo->id.next) { + if (wo->ipo) execute_ipo((ID *)wo, wo->ipo); + } + + /* ShapeKey Blocks */ + for (key= G.main->key.first; key; key= key->id.next) { + if (key->ipo) execute_ipo((ID *)key, key->ipo); + } + + /* Lamp Blocks */ + for (la= G.main->lamp.first; la; la= la->id.next) { + if (la->ipo) execute_ipo((ID *)la, la->ipo); + } + + /* Camera Blocks */ + for (ca= G.main->camera.first; ca; ca= ca->id.next) { + if (ca->ipo) execute_ipo((ID *)ca, ca->ipo); + } + + /* Sound Blocks (Old + Unused) */ + for (snd= G.main->sound.first; snd; snd= snd->id.next) { + if (snd->ipo) execute_ipo((ID *)snd, snd->ipo); + } + + /* Sequencer: process FAC Ipos used as volume envelopes */ + ed= G.scene->ed; + if (ed) { + for (seq= ed->seqbasep->first; seq; seq= seq->next) { + if ( ((seq->type == SEQ_RAM_SOUND) || (seq->type == SEQ_HD_SOUND)) && + (seq->startdisp <= G.scene->r.cfra+2) && + (seq->enddisp>G.scene->r.cfra) && + (seq->ipo) ) + { + do_seq_ipo(seq, G.scene->r.cfra); + } + } + } +} + + +/* --------------------- Assorted ----------------------------- */ + +/* clear delta-transforms on all Objects which use the given IPO block */ +void clear_delta_obipo(Ipo *ipo) +{ + Object *ob; + + /* only search if there's an IPO */ + if (ipo == NULL) + return; + + /* search through all objects in database */ + for (ob= G.main->object.first; ob; ob= ob->id.next) { + /* can only update if not a library */ + if (ob->id.lib == NULL) { + if (ob->ipo == ipo) { + memset(&ob->dloc, 0, 12); + memset(&ob->drot, 0, 12); + memset(&ob->dsize, 0, 12); + } + } + } +} + +/* ***************************** IPO - DataAPI ********************************* */ + +// !!!!!!!!!!!!!!!!!!!!!!!!!!!! FIXME - BAD CRUFT WARNING !!!!!!!!!!!!!!!!!!!!!!! + +/* These functions here should be replaced eventually by the Data API, as this is + * inflexible duplication... + */ - switch(adrcode) { +/* --------------------- Get Pointer API ----------------------------- */ + +/* get pointer to pose-channel's channel, but set appropriate flags first */ +void *get_pchan_ipo_poin (bPoseChannel *pchan, int adrcode) +{ + void *poin= NULL; + + switch (adrcode) { + case AC_QUAT_W: + poin= &(pchan->quat[0]); + pchan->flag |= POSE_ROT; + break; + case AC_QUAT_X: + poin= &(pchan->quat[1]); + pchan->flag |= POSE_ROT; + break; + case AC_QUAT_Y: + poin= &(pchan->quat[2]); + pchan->flag |= POSE_ROT; + break; + case AC_QUAT_Z: + poin= &(pchan->quat[3]); + pchan->flag |= POSE_ROT; + break; + + case AC_LOC_X: + poin= &(pchan->loc[0]); + pchan->flag |= POSE_LOC; + break; + case AC_LOC_Y: + poin= &(pchan->loc[1]); + pchan->flag |= POSE_LOC; + break; + case AC_LOC_Z: + poin= &(pchan->loc[2]); + pchan->flag |= POSE_LOC; + break; + + case AC_SIZE_X: + poin= &(pchan->size[0]); + pchan->flag |= POSE_SIZE; + break; + case AC_SIZE_Y: + poin= &(pchan->size[1]); + pchan->flag |= POSE_SIZE; + break; + case AC_SIZE_Z: + poin= &(pchan->size[2]); + pchan->flag |= POSE_SIZE; + break; + } + + /* return pointer */ + return poin; +} + +/* get texture channel */ +static void *give_tex_poin (Tex *tex, int adrcode, int *type ) +{ + void *poin= NULL; + + switch (adrcode) { case TE_NSIZE: poin= &(tex->noisesize); break; case TE_TURB: @@ -1169,17 +1896,18 @@ static void *give_tex_poin(Tex *tex, int adrcode, int *type ) poin= &(tex->bright); break; case TE_CONTRA: poin= &(tex->contrast); break; - } + /* return pointer */ return poin; } -void *give_mtex_poin(MTex *mtex, int adrcode ) +/* get texture-slot/mapping channel */ +void *give_mtex_poin (MTex *mtex, int adrcode) { - void *poin=0; - - switch(adrcode) { + void *poin= NULL; + + switch (adrcode) { case MAP_OFS_X: poin= &(mtex->ofs[0]); break; case MAP_OFS_Y: @@ -1210,6 +1938,7 @@ void *give_mtex_poin(MTex *mtex, int adrcode ) poin= &(mtex->dispfac); break; } + /* return pointer */ return poin; } @@ -1226,1284 +1955,909 @@ void *give_mtex_poin(MTex *mtex, int adrcode ) /* from misc_util: flip the bytes from x */ /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */ -void *get_ipo_poin(ID *id, IpoCurve *icu, int *type) + +/* general function to get pointer to source/destination data */ +void *get_ipo_poin (ID *id, IpoCurve *icu, int *type) { void *poin= NULL; - Object *ob; - Material *ma; - MTex *mtex; - Tex *tex; - Lamp *la; - Sequence *seq; - World *wo; - ParticleSettings *part; + MTex *mtex= NULL; + /* most channels will have float data, but those with other types will override this */ *type= IPO_FLOAT; - if( GS(id->name)==ID_OB) { - - ob= (Object *)id; - - switch(icu->adrcode) { - case OB_LOC_X: - poin= &(ob->loc[0]); break; - case OB_LOC_Y: - poin= &(ob->loc[1]); break; - case OB_LOC_Z: - poin= &(ob->loc[2]); break; - case OB_DLOC_X: - poin= &(ob->dloc[0]); break; - case OB_DLOC_Y: - poin= &(ob->dloc[1]); break; - case OB_DLOC_Z: - poin= &(ob->dloc[2]); break; - - case OB_ROT_X: - poin= &(ob->rot[0]); *type= IPO_FLOAT_DEGR; break; - case OB_ROT_Y: - poin= &(ob->rot[1]); *type= IPO_FLOAT_DEGR; break; - case OB_ROT_Z: - poin= &(ob->rot[2]); *type= IPO_FLOAT_DEGR; break; - case OB_DROT_X: - poin= &(ob->drot[0]); *type= IPO_FLOAT_DEGR; break; - case OB_DROT_Y: - poin= &(ob->drot[1]); *type= IPO_FLOAT_DEGR; break; - case OB_DROT_Z: - poin= &(ob->drot[2]); *type= IPO_FLOAT_DEGR; break; + /* data is divided into 'blocktypes' based on ID-codes */ + switch (GS(id->name)) { + case ID_OB: /* object channels ----------------------------- */ + { + Object *ob= (Object *)id; - case OB_SIZE_X: - poin= &(ob->size[0]); break; - case OB_SIZE_Y: - poin= &(ob->size[1]); break; - case OB_SIZE_Z: - poin= &(ob->size[2]); break; - case OB_DSIZE_X: - poin= &(ob->dsize[0]); break; - case OB_DSIZE_Y: - poin= &(ob->dsize[1]); break; - case OB_DSIZE_Z: - poin= &(ob->dsize[2]); break; - - case OB_LAY: - poin= &(ob->lay); *type= IPO_INT_BIT; break; + switch (icu->adrcode) { + case OB_LOC_X: + poin= &(ob->loc[0]); break; + case OB_LOC_Y: + poin= &(ob->loc[1]); break; + case OB_LOC_Z: + poin= &(ob->loc[2]); break; + case OB_DLOC_X: + poin= &(ob->dloc[0]); break; + case OB_DLOC_Y: + poin= &(ob->dloc[1]); break; + case OB_DLOC_Z: + poin= &(ob->dloc[2]); break; - case OB_COL_R: - poin= &(ob->col[0]); - break; - case OB_COL_G: - poin= &(ob->col[1]); - break; - case OB_COL_B: - poin= &(ob->col[2]); - break; - case OB_COL_A: - poin= &(ob->col[3]); - break; - case OB_PD_FSTR: - if(ob->pd) poin= &(ob->pd->f_strength); - break; - case OB_PD_FFALL: - if(ob->pd) poin= &(ob->pd->f_power); - break; - case OB_PD_SDAMP: - if(ob->pd) poin= &(ob->pd->pdef_damp); - break; - case OB_PD_RDAMP: - if(ob->pd) poin= &(ob->pd->pdef_rdamp); - break; - case OB_PD_PERM: - if(ob->pd) poin= &(ob->pd->pdef_perm); - break; - case OB_PD_FMAXD: - if(ob->pd) poin= &(ob->pd->maxdist); - break; - } - } - else if( GS(id->name)==ID_MA) { - - ma= (Material *)id; - - switch(icu->adrcode) { - case MA_COL_R: - poin= &(ma->r); break; - case MA_COL_G: - poin= &(ma->g); break; - case MA_COL_B: - poin= &(ma->b); break; - case MA_SPEC_R: - poin= &(ma->specr); break; - case MA_SPEC_G: - poin= &(ma->specg); break; - case MA_SPEC_B: - poin= &(ma->specb); break; - case MA_MIR_R: - poin= &(ma->mirr); break; - case MA_MIR_G: - poin= &(ma->mirg); break; - case MA_MIR_B: - poin= &(ma->mirb); break; - case MA_REF: - poin= &(ma->ref); break; - case MA_ALPHA: - poin= &(ma->alpha); break; - case MA_EMIT: - poin= &(ma->emit); break; - case MA_AMB: - poin= &(ma->amb); break; - case MA_SPEC: - poin= &(ma->spec); break; - case MA_HARD: - poin= &(ma->har); *type= IPO_SHORT; break; - case MA_SPTR: - poin= &(ma->spectra); break; - case MA_IOR: - poin= &(ma->ang); break; - case MA_MODE: - poin= &(ma->mode); *type= IPO_INT_BIT; break; - case MA_HASIZE: - poin= &(ma->hasize); break; - case MA_TRANSLU: - poin= &(ma->translucency); break; - case MA_RAYM: - poin= &(ma->ray_mirror); break; - case MA_FRESMIR: - poin= &(ma->fresnel_mir); break; - case MA_FRESMIRI: - poin= &(ma->fresnel_mir_i); break; - case MA_FRESTRA: - poin= &(ma->fresnel_tra); break; - case MA_FRESTRAI: - poin= &(ma->fresnel_tra_i); break; - case MA_ADD: - poin= &(ma->add); break; - } - - if(poin==NULL) { - mtex= 0; - if(icu->adrcode & MA_MAP1) mtex= ma->mtex[0]; - else if(icu->adrcode & MA_MAP2) mtex= ma->mtex[1]; - else if(icu->adrcode & MA_MAP3) mtex= ma->mtex[2]; - else if(icu->adrcode & MA_MAP4) mtex= ma->mtex[3]; - else if(icu->adrcode & MA_MAP5) mtex= ma->mtex[4]; - else if(icu->adrcode & MA_MAP6) mtex= ma->mtex[5]; - else if(icu->adrcode & MA_MAP7) mtex= ma->mtex[6]; - else if(icu->adrcode & MA_MAP8) mtex= ma->mtex[7]; - else if(icu->adrcode & MA_MAP9) mtex= ma->mtex[8]; - else if(icu->adrcode & MA_MAP10) mtex= ma->mtex[9]; + case OB_ROT_X: + poin= &(ob->rot[0]); *type= IPO_FLOAT_DEGR; break; + case OB_ROT_Y: + poin= &(ob->rot[1]); *type= IPO_FLOAT_DEGR; break; + case OB_ROT_Z: + poin= &(ob->rot[2]); *type= IPO_FLOAT_DEGR; break; + case OB_DROT_X: + poin= &(ob->drot[0]); *type= IPO_FLOAT_DEGR; break; + case OB_DROT_Y: + poin= &(ob->drot[1]); *type= IPO_FLOAT_DEGR; break; + case OB_DROT_Z: + poin= &(ob->drot[2]); *type= IPO_FLOAT_DEGR; break; + + case OB_SIZE_X: + poin= &(ob->size[0]); break; + case OB_SIZE_Y: + poin= &(ob->size[1]); break; + case OB_SIZE_Z: + poin= &(ob->size[2]); break; + case OB_DSIZE_X: + poin= &(ob->dsize[0]); break; + case OB_DSIZE_Y: + poin= &(ob->dsize[1]); break; + case OB_DSIZE_Z: + poin= &(ob->dsize[2]); break; - if(mtex) { - poin= give_mtex_poin(mtex, icu->adrcode & (MA_MAP1-1) ); - } - } - } - else if( GS(id->name)==ID_TE) { - tex= (Tex *)id; - - if(tex) poin= give_tex_poin(tex, icu->adrcode, type); - } - else if( GS(id->name)==ID_SEQ) { - seq= (Sequence *)id; - - switch(icu->adrcode) { - case SEQ_FAC1: - poin= &(seq->facf0); break; - } - } - else if( GS(id->name)==ID_CU) { - - poin= &(icu->curval); - - } - else if( GS(id->name)==ID_KE) { - KeyBlock *kb= ((Key *)id)->block.first; - - for(; kb; kb= kb->next) - if(kb->adrcode==icu->adrcode) + case OB_LAY: + poin= &(ob->lay); *type= IPO_INT_BIT; break; + + case OB_COL_R: + poin= &(ob->col[0]); break; + case OB_COL_G: + poin= &(ob->col[1]); break; + case OB_COL_B: + poin= &(ob->col[2]); break; + case OB_COL_A: + poin= &(ob->col[3]); break; + + case OB_PD_FSTR: + if (ob->pd) poin= &(ob->pd->f_strength); + break; + case OB_PD_FFALL: + if (ob->pd) poin= &(ob->pd->f_power); + break; + case OB_PD_SDAMP: + if (ob->pd) poin= &(ob->pd->pdef_damp); + break; + case OB_PD_RDAMP: + if (ob->pd) poin= &(ob->pd->pdef_rdamp); + break; + case OB_PD_PERM: + if (ob->pd) poin= &(ob->pd->pdef_perm); + break; + case OB_PD_FMAXD: + if (ob->pd) poin= &(ob->pd->maxdist); break; - if(kb) - poin= &(kb->curval); - - } - else if(GS(id->name)==ID_WO) { - - wo= (World *)id; - - switch(icu->adrcode) { - case WO_HOR_R: - poin= &(wo->horr); break; - case WO_HOR_G: - poin= &(wo->horg); break; - case WO_HOR_B: - poin= &(wo->horb); break; - case WO_ZEN_R: - poin= &(wo->zenr); break; - case WO_ZEN_G: - poin= &(wo->zeng); break; - case WO_ZEN_B: - poin= &(wo->zenb); break; - - case WO_EXPOS: - poin= &(wo->exposure); break; - - case WO_MISI: - poin= &(wo->misi); break; - case WO_MISTDI: - poin= &(wo->mistdist); break; - case WO_MISTSTA: - poin= &(wo->miststa); break; - case WO_MISTHI: - poin= &(wo->misthi); break; - - case WO_STAR_R: - poin= &(wo->starr); break; - case WO_STAR_G: - poin= &(wo->starg); break; - case WO_STAR_B: - poin= &(wo->starb); break; - - case WO_STARDIST: - poin= &(wo->stardist); break; - case WO_STARSIZE: - poin= &(wo->starsize); break; - } - - if(poin==NULL) { - mtex= 0; - if(icu->adrcode & MA_MAP1) mtex= wo->mtex[0]; - else if(icu->adrcode & MA_MAP2) mtex= wo->mtex[1]; - else if(icu->adrcode & MA_MAP3) mtex= wo->mtex[2]; - else if(icu->adrcode & MA_MAP4) mtex= wo->mtex[3]; - else if(icu->adrcode & MA_MAP5) mtex= wo->mtex[4]; - else if(icu->adrcode & MA_MAP6) mtex= wo->mtex[5]; - else if(icu->adrcode & MA_MAP7) mtex= wo->mtex[6]; - else if(icu->adrcode & MA_MAP8) mtex= wo->mtex[7]; - else if(icu->adrcode & MA_MAP9) mtex= wo->mtex[8]; - else if(icu->adrcode & MA_MAP10) mtex= wo->mtex[9]; - - if(mtex) { - poin= give_mtex_poin(mtex, icu->adrcode & (MA_MAP1-1) ); - } - } - } - else if( GS(id->name)==ID_LA) { - - la= (Lamp *)id; - - switch(icu->adrcode) { - case LA_ENERGY: - poin= &(la->energy); break; - case LA_COL_R: - poin= &(la->r); break; - case LA_COL_G: - poin= &(la->g); break; - case LA_COL_B: - poin= &(la->b); break; - case LA_DIST: - poin= &(la->dist); break; - case LA_SPOTSI: - poin= &(la->spotsize); break; - case LA_SPOTBL: - poin= &(la->spotblend); break; - case LA_QUAD1: - poin= &(la->att1); break; - case LA_QUAD2: - poin= &(la->att2); break; - case LA_HALOINT: - poin= &(la->haint); break; - } - - if(poin==NULL) { - mtex= 0; - if(icu->adrcode & MA_MAP1) mtex= la->mtex[0]; - else if(icu->adrcode & MA_MAP2) mtex= la->mtex[1]; - else if(icu->adrcode & MA_MAP3) mtex= la->mtex[2]; - else if(icu->adrcode & MA_MAP4) mtex= la->mtex[3]; - else if(icu->adrcode & MA_MAP5) mtex= la->mtex[4]; - else if(icu->adrcode & MA_MAP6) mtex= la->mtex[5]; - else if(icu->adrcode & MA_MAP7) mtex= la->mtex[6]; - else if(icu->adrcode & MA_MAP8) mtex= la->mtex[7]; - else if(icu->adrcode & MA_MAP9) mtex= la->mtex[8]; - else if(icu->adrcode & MA_MAP10) mtex= la->mtex[9]; - - if(mtex) { - poin= give_mtex_poin(mtex, icu->adrcode & (MA_MAP1-1) ); } } - } - else if(GS(id->name)==ID_CA) { - Camera *ca= (Camera *)id; - - /* yafray: aperture & focal distance params */ - switch(icu->adrcode) { - case CAM_LENS: - if(ca->type==CAM_ORTHO) - poin= &(ca->ortho_scale); - else - poin= &(ca->lens); break; - case CAM_STA: - poin= &(ca->clipsta); break; - case CAM_END: - poin= &(ca->clipend); break; - case CAM_YF_APERT: - poin= &(ca->YF_aperture); break; - case CAM_YF_FDIST: - poin= &(ca->YF_dofdist); break; - case CAM_SHIFT_X: - poin= &(ca->shiftx); break; - case CAM_SHIFT_Y: - poin= &(ca->shifty); break; - } - } - else if(GS(id->name)==ID_SO) { - bSound *snd= (bSound *)id; - - switch(icu->adrcode) { - case SND_VOLUME: - poin= &(snd->volume); break; - case SND_PITCH: - poin= &(snd->pitch); break; - case SND_PANNING: - poin= &(snd->panning); break; - case SND_ATTEN: - poin= &(snd->attenuation); break; - } - } - else if( GS(id->name)==ID_PA) { - - part= (ParticleSettings *)id; - - switch(icu->adrcode) { - case PART_EMIT_FREQ: - case PART_EMIT_LIFE: - case PART_EMIT_VEL: - case PART_EMIT_AVE: - case PART_EMIT_SIZE: - poin= NULL; break; - case PART_CLUMP: - poin= &(part->clumpfac); break; - case PART_AVE: - poin= &(part->avefac); break; - case PART_SIZE: - poin= &(part->size); break; - case PART_DRAG: - poin= &(part->dragfac); break; - case PART_BROWN: - poin= &(part->brownfac); break; - case PART_DAMP: - poin= &(part->dampfac); break; - case PART_LENGTH: - poin= &(part->length); break; - case PART_GRAV_X: - poin= &(part->acc[0]); break; - case PART_GRAV_Y: - poin= &(part->acc[1]); break; - case PART_GRAV_Z: - poin= &(part->acc[2]); break; - case PART_KINK_AMP: - poin= &(part->kink_amp); break; - case PART_KINK_FREQ: - poin= &(part->kink_freq); break; - case PART_KINK_SHAPE: - poin= &(part->kink_shape); break; - case PART_BB_TILT: - poin= &(part->bb_tilt); break; - } - } - - return poin; -} - -void set_icu_vars(IpoCurve *icu) -{ - /* defaults. 0.0 for y-extents makes these ignored */ - icu->ymin= icu->ymax= 0.0; - icu->ipo= IPO_BEZ; - - if(icu->blocktype==ID_OB) { - - if(icu->adrcode==OB_LAY) { - icu->ipo= IPO_CONST; - icu->vartype= IPO_BITS; - } - - } - else if(icu->blocktype==ID_MA) { - - if(icu->adrcode < MA_MAP1) { - switch(icu->adrcode) { - case MA_HASIZE: - icu->ymax= 10000.0; break; - case MA_HARD: - icu->ymax= 511.0; break; + case ID_MA: /* material channels ----------------------------- */ + { + Material *ma= (Material *)id; + + switch (icu->adrcode) { + case MA_COL_R: + poin= &(ma->r); break; + case MA_COL_G: + poin= &(ma->g); break; + case MA_COL_B: + poin= &(ma->b); break; + case MA_SPEC_R: + poin= &(ma->specr); break; + case MA_SPEC_G: + poin= &(ma->specg); break; + case MA_SPEC_B: + poin= &(ma->specb); break; + case MA_MIR_R: + poin= &(ma->mirr); break; + case MA_MIR_G: + poin= &(ma->mirg); break; + case MA_MIR_B: + poin= &(ma->mirb); break; + case MA_REF: + poin= &(ma->ref); break; + case MA_ALPHA: + poin= &(ma->alpha); break; + case MA_EMIT: + poin= &(ma->emit); break; + case MA_AMB: + poin= &(ma->amb); break; case MA_SPEC: - icu->ymax= 2.0; break; + poin= &(ma->spec); break; + case MA_HARD: + poin= &(ma->har); *type= IPO_SHORT; break; + case MA_SPTR: + poin= &(ma->spectra); break; + case MA_IOR: + poin= &(ma->ang); break; case MA_MODE: - icu->ipo= IPO_CONST; - icu->vartype= IPO_BITS; break; - case MA_RAYM: - icu->ymax= 1.0; break; + poin= &(ma->mode); *type= IPO_INT_BIT; break; // evil... dumping bitflags directly to user! + case MA_HASIZE: + poin= &(ma->hasize); break; case MA_TRANSLU: - icu->ymax= 1.0; break; - case MA_IOR: - icu->ymin= 1.0; - icu->ymax= 3.0; break; + poin= &(ma->translucency); break; + case MA_RAYM: + poin= &(ma->ray_mirror); break; case MA_FRESMIR: - icu->ymax= 5.0; break; + poin= &(ma->fresnel_mir); break; case MA_FRESMIRI: - icu->ymin= 1.0; - icu->ymax= 5.0; break; + poin= &(ma->fresnel_mir_i); break; case MA_FRESTRA: - icu->ymax= 5.0; break; + poin= &(ma->fresnel_tra); break; case MA_FRESTRAI: - icu->ymin= 1.0; - icu->ymax= 5.0; break; + poin= &(ma->fresnel_tra_i); break; case MA_ADD: - icu->ymax= 1.0; break; - default: - icu->ymax= 1.0; break; + poin= &(ma->add); break; + } + + if (poin == NULL) { + if (icu->adrcode & MA_MAP1) mtex= ma->mtex[0]; + else if (icu->adrcode & MA_MAP2) mtex= ma->mtex[1]; + else if (icu->adrcode & MA_MAP3) mtex= ma->mtex[2]; + else if (icu->adrcode & MA_MAP4) mtex= ma->mtex[3]; + else if (icu->adrcode & MA_MAP5) mtex= ma->mtex[4]; + else if (icu->adrcode & MA_MAP6) mtex= ma->mtex[5]; + else if (icu->adrcode & MA_MAP7) mtex= ma->mtex[6]; + else if (icu->adrcode & MA_MAP8) mtex= ma->mtex[7]; + else if (icu->adrcode & MA_MAP9) mtex= ma->mtex[8]; + else if (icu->adrcode & MA_MAP10) mtex= ma->mtex[9]; + else if (icu->adrcode & MA_MAP12) mtex= ma->mtex[11]; + else if (icu->adrcode & MA_MAP11) mtex= ma->mtex[10]; + else if (icu->adrcode & MA_MAP13) mtex= ma->mtex[12]; + else if (icu->adrcode & MA_MAP14) mtex= ma->mtex[13]; + else if (icu->adrcode & MA_MAP15) mtex= ma->mtex[14]; + else if (icu->adrcode & MA_MAP16) mtex= ma->mtex[15]; + else if (icu->adrcode & MA_MAP17) mtex= ma->mtex[16]; + else if (icu->adrcode & MA_MAP18) mtex= ma->mtex[17]; + + if (mtex) + poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1))); } } - else { - switch(icu->adrcode & (MA_MAP1-1)) { - case MAP_OFS_X: - case MAP_OFS_Y: - case MAP_OFS_Z: - case MAP_SIZE_X: - case MAP_SIZE_Y: - case MAP_SIZE_Z: - icu->ymax= 1000.0; - icu->ymin= -1000.0; + break; + case ID_TE: /* texture channels ----------------------------- */ + { + Tex *tex= (Tex *)id; - break; - case MAP_R: - case MAP_G: - case MAP_B: - case MAP_DVAR: - case MAP_COLF: - case MAP_VARF: - case MAP_DISP: - icu->ymax= 1.0; - break; - case MAP_NORF: - icu->ymax= 25.0; - break; + if (tex) + poin= give_tex_poin(tex, icu->adrcode, type); + } + break; + case ID_SEQ: /* sequence channels ----------------------------- */ + { + Sequence *seq= (Sequence *)id; + + switch (icu->adrcode) { + case SEQ_FAC1: + poin= &(seq->facf0); break; } } - } - else if(icu->blocktype==ID_TE) { - switch(icu->adrcode & (MA_MAP1-1)) { - case TE_NSIZE: - icu->ymin= 0.0001; - icu->ymax= 2.0; break; - case TE_NDEPTH: - icu->vartype= IPO_SHORT; - icu->ipo= IPO_CONST; - icu->ymax= 6.0; break; - case TE_NTYPE: - icu->vartype= IPO_SHORT; - icu->ipo= IPO_CONST; - icu->ymax= 1.0; break; - case TE_TURB: - icu->ymax= 200.0; break; - case TE_VNW1: - case TE_VNW2: - case TE_VNW3: - case TE_VNW4: - icu->ymax= 2.0; - icu->ymin= -2.0; break; - case TE_VNMEXP: - icu->ymax= 10.0; - icu->ymin= 0.01; break; - case TE_VN_DISTM: - icu->vartype= IPO_SHORT; - icu->ipo= IPO_CONST; - icu->ymax= 6.0; break; - case TE_VN_COLT: - icu->vartype= IPO_SHORT; - icu->ipo= IPO_CONST; - icu->ymax= 3.0; break; - case TE_ISCA: - icu->ymax= 10.0; - icu->ymin= 0.01; break; - case TE_DISTA: - icu->ymax= 10.0; break; - case TE_MG_TYP: - icu->vartype= IPO_SHORT; - icu->ipo= IPO_CONST; - icu->ymax= 6.0; break; - case TE_MGH: - icu->ymin= 0.0001; - icu->ymax= 2.0; break; - case TE_MG_LAC: - case TE_MG_OFF: - case TE_MG_GAIN: - icu->ymax= 6.0; break; - case TE_MG_OCT: - icu->ymax= 8.0; break; - case TE_N_BAS1: - case TE_N_BAS2: - icu->vartype= IPO_SHORT; - icu->ipo= IPO_CONST; - icu->ymax= 8.0; break; - case TE_COL_R: - icu->ymax= 0.0; break; - case TE_COL_G: - icu->ymax= 2.0; break; - case TE_COL_B: - icu->ymax= 2.0; break; - case TE_BRIGHT: - icu->ymax= 2.0; break; - case TE_CONTRA: - icu->ymax= 5.0; break; - + break; + case ID_CU: /* curve channels ----------------------------- */ + { + poin= &(icu->curval); } - } - else if(icu->blocktype==ID_SEQ) { - - icu->ymax= 1.0; - - } - else if(icu->blocktype==ID_CU) { - - icu->ymax= 1.0; - - } - else if(icu->blocktype==ID_WO) { - - if(icu->adrcode < MA_MAP1) { - switch(icu->adrcode) { + break; + case ID_KE: /* shapekey channels ----------------------------- */ + { + Key *key= (Key *)id; + KeyBlock *kb; + + for(kb= key->block.first; kb; kb= kb->next) { + if (kb->adrcode == icu->adrcode) + break; + } + + if (kb) + poin= &(kb->curval); + } + break; + case ID_WO: /* world channels ----------------------------- */ + { + World *wo= (World *)id; + + switch (icu->adrcode) { + case WO_HOR_R: + poin= &(wo->horr); break; + case WO_HOR_G: + poin= &(wo->horg); break; + case WO_HOR_B: + poin= &(wo->horb); break; + case WO_ZEN_R: + poin= &(wo->zenr); break; + case WO_ZEN_G: + poin= &(wo->zeng); break; + case WO_ZEN_B: + poin= &(wo->zenb); break; + case WO_EXPOS: - icu->ymax= 5.0; break; + poin= &(wo->exposure); break; + + case WO_MISI: + poin= &(wo->misi); break; case WO_MISTDI: + poin= &(wo->mistdist); break; case WO_MISTSTA: + poin= &(wo->miststa); break; case WO_MISTHI: + poin= &(wo->misthi); break; + + case WO_STAR_R: + poin= &(wo->starr); break; + case WO_STAR_G: + poin= &(wo->starg); break; + case WO_STAR_B: + poin= &(wo->starb); break; + case WO_STARDIST: + poin= &(wo->stardist); break; case WO_STARSIZE: - break; - - default: - icu->ymax= 1.0; - break; + poin= &(wo->starsize); break; } - } - else { - switch(icu->adrcode & (MA_MAP1-1)) { - case MAP_OFS_X: - case MAP_OFS_Y: - case MAP_OFS_Z: - case MAP_SIZE_X: - case MAP_SIZE_Y: - case MAP_SIZE_Z: - icu->ymax= 100.0; - icu->ymin= -100.0; - break; - case MAP_R: - case MAP_G: - case MAP_B: - case MAP_DVAR: - case MAP_COLF: - case MAP_NORF: - case MAP_VARF: - case MAP_DISP: - icu->ymax= 1.0; + if (poin == NULL) { + if (icu->adrcode & MA_MAP1) mtex= wo->mtex[0]; + else if (icu->adrcode & MA_MAP2) mtex= wo->mtex[1]; + else if (icu->adrcode & MA_MAP3) mtex= wo->mtex[2]; + else if (icu->adrcode & MA_MAP4) mtex= wo->mtex[3]; + else if (icu->adrcode & MA_MAP5) mtex= wo->mtex[4]; + else if (icu->adrcode & MA_MAP6) mtex= wo->mtex[5]; + else if (icu->adrcode & MA_MAP7) mtex= wo->mtex[6]; + else if (icu->adrcode & MA_MAP8) mtex= wo->mtex[7]; + else if (icu->adrcode & MA_MAP9) mtex= wo->mtex[8]; + else if (icu->adrcode & MA_MAP10) mtex= wo->mtex[9]; + else if (icu->adrcode & MA_MAP11) mtex= wo->mtex[10]; + else if (icu->adrcode & MA_MAP12) mtex= wo->mtex[11]; + else if (icu->adrcode & MA_MAP13) mtex= wo->mtex[12]; + else if (icu->adrcode & MA_MAP14) mtex= wo->mtex[13]; + else if (icu->adrcode & MA_MAP15) mtex= wo->mtex[14]; + else if (icu->adrcode & MA_MAP16) mtex= wo->mtex[15]; + else if (icu->adrcode & MA_MAP17) mtex= wo->mtex[16]; + else if (icu->adrcode & MA_MAP18) mtex= wo->mtex[17]; + + if (mtex) + poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1))); } } - } - else if(icu->blocktype==ID_LA) { - if(icu->adrcode < MA_MAP1) { - switch(icu->adrcode) { + break; + case ID_LA: /* lamp channels ----------------------------- */ + { + Lamp *la= (Lamp *)id; + + switch (icu->adrcode) { case LA_ENERGY: - case LA_DIST: - break; - + poin= &(la->energy); break; case LA_COL_R: + poin= &(la->r); break; case LA_COL_G: + poin= &(la->g); break; case LA_COL_B: + poin= &(la->b); break; + case LA_DIST: + poin= &(la->dist); break; + case LA_SPOTSI: + poin= &(la->spotsize); break; case LA_SPOTBL: + poin= &(la->spotblend); break; case LA_QUAD1: + poin= &(la->att1); break; case LA_QUAD2: - icu->ymax= 1.0; break; - case LA_SPOTSI: - icu->ymax= 180.0; break; + poin= &(la->att2); break; case LA_HALOINT: - icu->ymax= 5.0; break; + poin= &(la->haint); break; + } + + if (poin == NULL) { + if (icu->adrcode & MA_MAP1) mtex= la->mtex[0]; + else if (icu->adrcode & MA_MAP2) mtex= la->mtex[1]; + else if (icu->adrcode & MA_MAP3) mtex= la->mtex[2]; + else if (icu->adrcode & MA_MAP4) mtex= la->mtex[3]; + else if (icu->adrcode & MA_MAP5) mtex= la->mtex[4]; + else if (icu->adrcode & MA_MAP6) mtex= la->mtex[5]; + else if (icu->adrcode & MA_MAP7) mtex= la->mtex[6]; + else if (icu->adrcode & MA_MAP8) mtex= la->mtex[7]; + else if (icu->adrcode & MA_MAP9) mtex= la->mtex[8]; + else if (icu->adrcode & MA_MAP10) mtex= la->mtex[9]; + else if (icu->adrcode & MA_MAP11) mtex= la->mtex[10]; + else if (icu->adrcode & MA_MAP12) mtex= la->mtex[11]; + else if (icu->adrcode & MA_MAP13) mtex= la->mtex[12]; + else if (icu->adrcode & MA_MAP14) mtex= la->mtex[13]; + else if (icu->adrcode & MA_MAP15) mtex= la->mtex[14]; + else if (icu->adrcode & MA_MAP16) mtex= la->mtex[15]; + else if (icu->adrcode & MA_MAP17) mtex= la->mtex[16]; + else if (icu->adrcode & MA_MAP18) mtex= la->mtex[17]; + + if (mtex) + poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1))); } } - else { - switch(icu->adrcode & (MA_MAP1-1)) { - case MAP_OFS_X: - case MAP_OFS_Y: - case MAP_OFS_Z: - case MAP_SIZE_X: - case MAP_SIZE_Y: - case MAP_SIZE_Z: - icu->ymax= 100.0; - icu->ymin= -100.0; + break; + case ID_CA: /* camera channels ----------------------------- */ + { + Camera *ca= (Camera *)id; + + switch (icu->adrcode) { + case CAM_LENS: + if (ca->type == CAM_ORTHO) + poin= &(ca->ortho_scale); + else + poin= &(ca->lens); break; - case MAP_R: - case MAP_G: - case MAP_B: - case MAP_DVAR: - case MAP_COLF: - case MAP_NORF: - case MAP_VARF: - case MAP_DISP: - icu->ymax= 1.0; + case CAM_STA: + poin= &(ca->clipsta); break; + case CAM_END: + poin= &(ca->clipend); break; + + case CAM_YF_APERT: + poin= &(ca->YF_aperture); break; + case CAM_YF_FDIST: + poin= &(ca->YF_dofdist); break; + + case CAM_SHIFT_X: + poin= &(ca->shiftx); break; + case CAM_SHIFT_Y: + poin= &(ca->shifty); break; } } - } - else if(icu->blocktype==ID_CA) { - - /* yafray: aperture & focal distance params */ - switch(icu->adrcode) { - case CAM_LENS: - icu->ymin= 1.0; - icu->ymax= 1000.0; - break; - case CAM_STA: - icu->ymin= 0.001f; - break; - case CAM_END: - icu->ymin= 0.1f; - break; - case CAM_YF_APERT: - icu->ymin = 0.0; - icu->ymax = 2.0; - break; - case CAM_YF_FDIST: - icu->ymin = 0.0; - icu->ymax = 5000.0; break; + case ID_SO: /* sound channels ----------------------------- */ + { + bSound *snd= (bSound *)id; - case CAM_SHIFT_X: - case CAM_SHIFT_Y: - icu->ymin= -2.0f; - icu->ymax= 2.0f; - break; + switch (icu->adrcode) { + case SND_VOLUME: + poin= &(snd->volume); break; + case SND_PITCH: + poin= &(snd->pitch); break; + case SND_PANNING: + poin= &(snd->panning); break; + case SND_ATTEN: + poin= &(snd->attenuation); break; + } } - } - else if(icu->blocktype==ID_SO) { - - switch(icu->adrcode) { - case SND_VOLUME: - icu->ymin= 0.0; - icu->ymax= 1.0; - break; - case SND_PITCH: - icu->ymin= -12.0; - icu->ymin= 12.0; - break; - case SND_PANNING: - icu->ymin= 0.0; - icu->ymax= 1.0; - break; - case SND_ATTEN: - icu->ymin= 0.0; - icu->ymin= 1.0; break; + case ID_PA: /* particle channels ----------------------------- */ + { + ParticleSettings *part= (ParticleSettings *)id; + + switch (icu->adrcode) { + case PART_EMIT_FREQ: + case PART_EMIT_LIFE: + case PART_EMIT_VEL: + case PART_EMIT_AVE: + case PART_EMIT_SIZE: + poin= NULL; + break; + + case PART_CLUMP: + poin= &(part->clumpfac); break; + case PART_AVE: + poin= &(part->avefac); break; + case PART_SIZE: + poin= &(part->size); break; + case PART_DRAG: + poin= &(part->dragfac); break; + case PART_BROWN: + poin= &(part->brownfac); break; + case PART_DAMP: + poin= &(part->dampfac); break; + case PART_LENGTH: + poin= &(part->length); break; + case PART_GRAV_X: + poin= &(part->acc[0]); break; + case PART_GRAV_Y: + poin= &(part->acc[1]); break; + case PART_GRAV_Z: + poin= &(part->acc[2]); break; + case PART_KINK_AMP: + poin= &(part->kink_amp); break; + case PART_KINK_FREQ: + poin= &(part->kink_freq); break; + case PART_KINK_SHAPE: + poin= &(part->kink_shape); break; + case PART_BB_TILT: + poin= &(part->bb_tilt); break; + + case PART_PD_FSTR: + if (part->pd) poin= &(part->pd->f_strength); + break; + case PART_PD_FFALL: + if (part->pd) poin= &(part->pd->f_power); + break; + case PART_PD_FMAXD: + if (part->pd) poin= &(part->pd->maxdist); + break; + case PART_PD2_FSTR: + if (part->pd2) poin= &(part->pd2->f_strength); + break; + case PART_PD2_FFALL: + if (part->pd2) poin= &(part->pd2->f_power); + break; + case PART_PD2_FMAXD: + if (part->pd2) poin= &(part->pd2->maxdist); + break; + } } - } - else if(icu->blocktype==ID_PA){ - - switch(icu->adrcode) { - case PART_EMIT_LIFE: - case PART_SIZE: - case PART_KINK_FREQ: - case PART_EMIT_VEL: - case PART_EMIT_AVE: - case PART_EMIT_SIZE: - icu->ymin= 0.0; - break; - case PART_CLUMP: - icu->ymin= -1.0; - icu->ymax= 1.0; - break; - case PART_DRAG: - case PART_DAMP: - case PART_LENGTH: - icu->ymin= 0.0; - icu->ymax= 1.0; - break; - case PART_KINK_SHAPE: - icu->ymin= -0.999; - icu->ymax= 0.999; break; - } - } - else if(icu->blocktype==ID_CO) { - icu->ymin= 0.0; - icu->ymax= 1.0f; } - - /* by default, slider limits will be icu->ymin and icu->ymax */ - icu->slide_min= icu->ymin; - icu->slide_max= icu->ymax; -} -/* not for actions or constraints! */ -void execute_ipo(ID *id, Ipo *ipo) -{ - IpoCurve *icu; - void *poin; - int type; - - if(ipo==NULL) return; - - for(icu= ipo->curve.first; icu; icu= icu->next) { - poin= get_ipo_poin(id, icu, &type); - if(poin) write_ipo_poin(poin, type, icu->curval); - } + /* return pointer */ + return poin; } -void *get_pchan_ipo_poin(bPoseChannel *pchan, int adrcode) +/* --------------------- IPO-Curve Limits ----------------------------- */ + +/* set limits for IPO-curve + * Note: must be synced with UI and PyAPI + */ +void set_icu_vars (IpoCurve *icu) { - void *poin= NULL; + /* defaults. 0.0 for y-extents makes these ignored */ + icu->ymin= icu->ymax= 0.0; + icu->ipo= IPO_BEZ; - switch (adrcode) { - case AC_QUAT_W: - poin= &(pchan->quat[0]); - pchan->flag |= POSE_ROT; + switch (icu->blocktype) { + case ID_OB: /* object channels ----------------------------- */ + { + if (icu->adrcode == OB_LAY) { + icu->ipo= IPO_CONST; + icu->vartype= IPO_BITS; + } + } break; - case AC_QUAT_X: - poin= &(pchan->quat[1]); - pchan->flag |= POSE_ROT; + case ID_MA: /* material channels ----------------------------- */ + { + if (icu->adrcode < MA_MAP1) { + switch (icu->adrcode) { + case MA_HASIZE: + icu->ymax= 10000.0; break; + case MA_HARD: + icu->ymax= 511.0; break; + case MA_SPEC: + icu->ymax= 2.0; break; + case MA_MODE: + icu->ipo= IPO_CONST; + icu->vartype= IPO_BITS; break; + case MA_RAYM: + icu->ymax= 1.0; break; + case MA_TRANSLU: + icu->ymax= 1.0; break; + case MA_IOR: + icu->ymin= 1.0; + icu->ymax= 3.0; break; + case MA_FRESMIR: + icu->ymax= 5.0; break; + case MA_FRESMIRI: + icu->ymin= 1.0; + icu->ymax= 5.0; break; + case MA_FRESTRA: + icu->ymax= 5.0; break; + case MA_FRESTRAI: + icu->ymin= 1.0; + icu->ymax= 5.0; break; + case MA_ADD: + icu->ymax= 1.0; break; + case MA_EMIT: + icu->ymax= 2.0; break; + default: + icu->ymax= 1.0; break; + } + } + else { + switch (icu->adrcode & (MA_MAP1-1)) { + case MAP_OFS_X: + case MAP_OFS_Y: + case MAP_OFS_Z: + case MAP_SIZE_X: + case MAP_SIZE_Y: + case MAP_SIZE_Z: + icu->ymax= 1000.0; + icu->ymin= -1000.0; + break; + case MAP_R: + case MAP_G: + case MAP_B: + case MAP_DVAR: + case MAP_COLF: + case MAP_VARF: + case MAP_DISP: + icu->ymax= 1.0; + break; + case MAP_NORF: + icu->ymax= 25.0; + break; + } + } + } break; - case AC_QUAT_Y: - poin= &(pchan->quat[2]); - pchan->flag |= POSE_ROT; + case ID_TE: /* texture channels ----------------------------- */ + { + switch (icu->adrcode & (MA_MAP1-1)) { + case TE_NSIZE: + icu->ymin= 0.0001; + icu->ymax= 2.0; break; + case TE_NDEPTH: + icu->vartype= IPO_SHORT; + icu->ipo= IPO_CONST; + icu->ymax= 6.0; break; + case TE_NTYPE: + icu->vartype= IPO_SHORT; + icu->ipo= IPO_CONST; + icu->ymax= 1.0; break; + case TE_TURB: + icu->ymax= 200.0; break; + case TE_VNW1: + case TE_VNW2: + case TE_VNW3: + case TE_VNW4: + icu->ymax= 2.0; + icu->ymin= -2.0; break; + case TE_VNMEXP: + icu->ymax= 10.0; + icu->ymin= 0.01; break; + case TE_VN_DISTM: + icu->vartype= IPO_SHORT; + icu->ipo= IPO_CONST; + icu->ymax= 6.0; break; + case TE_VN_COLT: + icu->vartype= IPO_SHORT; + icu->ipo= IPO_CONST; + icu->ymax= 3.0; break; + case TE_ISCA: + icu->ymax= 10.0; + icu->ymin= 0.01; break; + case TE_DISTA: + icu->ymax= 10.0; break; + case TE_MG_TYP: + icu->vartype= IPO_SHORT; + icu->ipo= IPO_CONST; + icu->ymax= 6.0; break; + case TE_MGH: + icu->ymin= 0.0001; + icu->ymax= 2.0; break; + case TE_MG_LAC: + case TE_MG_OFF: + case TE_MG_GAIN: + icu->ymax= 6.0; break; + case TE_MG_OCT: + icu->ymax= 8.0; break; + case TE_N_BAS1: + case TE_N_BAS2: + icu->vartype= IPO_SHORT; + icu->ipo= IPO_CONST; + icu->ymax= 8.0; break; + case TE_COL_R: + icu->ymax= 0.0; break; + case TE_COL_G: + icu->ymax= 2.0; break; + case TE_COL_B: + icu->ymax= 2.0; break; + case TE_BRIGHT: + icu->ymax= 2.0; break; + case TE_CONTRA: + icu->ymax= 5.0; break; + } + } break; - case AC_QUAT_Z: - poin= &(pchan->quat[3]); - pchan->flag |= POSE_ROT; + case ID_SEQ: /* sequence channels ----------------------------- */ + { + icu->ymax= 1.0; + } break; - case AC_LOC_X: - poin= &(pchan->loc[0]); - pchan->flag |= POSE_LOC; + case ID_CU: /* curve channels ----------------------------- */ + { + icu->ymax= 1.0; + } break; - case AC_LOC_Y: - poin= &(pchan->loc[1]); - pchan->flag |= POSE_LOC; + case ID_WO: /* world channels ----------------------------- */ + { + if (icu->adrcode < MA_MAP1) { + switch (icu->adrcode) { + case WO_EXPOS: + icu->ymax= 5.0; break; + case WO_MISTDI: + case WO_MISTSTA: + case WO_MISTHI: + case WO_STARDIST: + case WO_STARSIZE: + break; + + default: + icu->ymax= 1.0; + break; + } + } + else { + switch (icu->adrcode & (MA_MAP1-1)) { + case MAP_OFS_X: + case MAP_OFS_Y: + case MAP_OFS_Z: + case MAP_SIZE_X: + case MAP_SIZE_Y: + case MAP_SIZE_Z: + icu->ymax= 100.0; + icu->ymin= -100.0; + break; + case MAP_R: + case MAP_G: + case MAP_B: + case MAP_DVAR: + case MAP_COLF: + case MAP_NORF: + case MAP_VARF: + case MAP_DISP: + icu->ymax= 1.0; + } + } + } break; - case AC_LOC_Z: - poin= &(pchan->loc[2]); - pchan->flag |= POSE_LOC; - break; - case AC_SIZE_X: - poin= &(pchan->size[0]); - pchan->flag |= POSE_SIZE; + case ID_LA: /* lamp channels ----------------------------- */ + { + if (icu->adrcode < MA_MAP1) { + switch (icu->adrcode) { + case LA_ENERGY: + case LA_DIST: + break; + + case LA_COL_R: + case LA_COL_G: + case LA_COL_B: + case LA_SPOTBL: + case LA_QUAD1: + case LA_QUAD2: + icu->ymax= 1.0; break; + + case LA_SPOTSI: + icu->ymax= 180.0; break; + + case LA_HALOINT: + icu->ymax= 5.0; break; + } + } + else { + switch (icu->adrcode & (MA_MAP1-1)) { + case MAP_OFS_X: + case MAP_OFS_Y: + case MAP_OFS_Z: + case MAP_SIZE_X: + case MAP_SIZE_Y: + case MAP_SIZE_Z: + icu->ymax= 100.0; + icu->ymin= -100.0; + break; + case MAP_R: + case MAP_G: + case MAP_B: + case MAP_DVAR: + case MAP_COLF: + case MAP_NORF: + case MAP_VARF: + case MAP_DISP: + icu->ymax= 1.0; + } + } + } break; - case AC_SIZE_Y: - poin= &(pchan->size[1]); - pchan->flag |= POSE_SIZE; + case ID_CA: /* camera channels ----------------------------- */ + { + switch (icu->adrcode) { + case CAM_LENS: + icu->ymin= 1.0; + icu->ymax= 1000.0; + break; + case CAM_STA: + icu->ymin= 0.001f; + break; + case CAM_END: + icu->ymin= 0.1f; + break; + + case CAM_YF_APERT: + icu->ymin = 0.0; + icu->ymax = 2.0; + break; + case CAM_YF_FDIST: + icu->ymin = 0.0; + icu->ymax = 5000.0; + break; + + case CAM_SHIFT_X: + case CAM_SHIFT_Y: + icu->ymin= -2.0f; + icu->ymax= 2.0f; + break; + } + } break; - case AC_SIZE_Z: - poin= &(pchan->size[2]); - pchan->flag |= POSE_SIZE; + case ID_SO: /* sound channels ----------------------------- */ + { + switch (icu->adrcode) { + case SND_VOLUME: + icu->ymin= 0.0; + icu->ymax= 1.0; + break; + case SND_PITCH: + icu->ymin= -12.0; + icu->ymin= 12.0; + break; + case SND_PANNING: + icu->ymin= 0.0; + icu->ymax= 1.0; + break; + case SND_ATTEN: + icu->ymin= 0.0; + icu->ymin= 1.0; + break; + } + } break; - } - return poin; -} - -void execute_action_ipo(bActionChannel *achan, bPoseChannel *pchan) -{ - - if(achan && achan->ipo) { - IpoCurve *icu; - for(icu= achan->ipo->curve.first; icu; icu= icu->next) { - void *poin= get_pchan_ipo_poin(pchan, icu->adrcode); - if(poin) { - write_ipo_poin(poin, IPO_FLOAT, icu->curval); - //printf("execute_action_ipo wrote_ipo_poin: %f\n", icu->curval); - //printf("%s has poin %p value %f\n", achan->name, poin, icu->curval); + case ID_PA: /* particle channels ----------------------------- */ + { + switch (icu->adrcode) { + case PART_EMIT_LIFE: + case PART_SIZE: + case PART_KINK_FREQ: + case PART_EMIT_VEL: + case PART_EMIT_AVE: + case PART_EMIT_SIZE: + icu->ymin= 0.0; + break; + case PART_CLUMP: + icu->ymin= -1.0; + icu->ymax= 1.0; + break; + case PART_DRAG: + case PART_DAMP: + case PART_LENGTH: + icu->ymin= 0.0; + icu->ymax= 1.0; + break; + case PART_KINK_SHAPE: + icu->ymin= -0.999; + icu->ymax= 0.999; + break; } } + break; + case ID_CO: /* constraint channels ----------------------------- */ + { + icu->ymin= 0.0; + icu->ymax= 1.0f; + } + break; } + + /* by default, slider limits will be icu->ymin and icu->ymax */ + icu->slide_min= icu->ymin; + icu->slide_max= icu->ymax; } -/* exception: it does calc for objects... - * now find out why this routine was used anyway! - */ -void do_ipo_nocalc(Ipo *ipo) +/* --------------------- Pointer I/O API ----------------------------- */ + +/* write the given value directly into the given pointer */ +void write_ipo_poin (void *poin, int type, float val) { - Object *ob; - Material *ma; - Tex *tex; - World *wo; - Lamp *la; - Camera *ca; - bSound *snd; - - if(ipo==NULL) return; - - switch(ipo->blocktype) { - case ID_OB: - ob= G.main->object.first; - while(ob) { - if(ob->ipo==ipo) { - do_ob_ipo(ob); - /* execute_ipo((ID *)ob, ipo); */ - } - ob= ob->id.next; - } - break; - case ID_MA: - ma= G.main->mat.first; - while(ma) { - if(ma->ipo==ipo) execute_ipo((ID *)ma, ipo); - ma= ma->id.next; - } - break; - case ID_TE: - tex= G.main->tex.first; - while(tex) { - if(tex->ipo==ipo) execute_ipo((ID *)tex, ipo); - tex=tex->id.next; - } + /* Note: we only support a limited number of types, with the value + * to set needing to be cast to the appropriate type first + * -> (float to integer conversions could be slow) + */ + switch(type) { + case IPO_FLOAT: + *((float *)poin)= val; break; - case ID_WO: - wo= G.main->world.first; - while(wo) { - if(wo->ipo==ipo) execute_ipo((ID *)wo, ipo); - wo= wo->id.next; - } + + case IPO_FLOAT_DEGR: /* special hack for rotation so that it fits on same axis as other transforms */ + *((float *)poin)= (float)(val * M_PI_2 / 9.0); break; - case ID_LA: - la= G.main->lamp.first; - while(la) { - if(la->ipo==ipo) execute_ipo((ID *)la, ipo); - la= la->id.next; - } + + case IPO_INT: + case IPO_INT_BIT: // fixme... directly revealing bitflag combinations is evil! + case IPO_LONG: + *((int *)poin)= (int)val; break; - case ID_CA: - ca= G.main->camera.first; - while(ca) { - if(ca->ipo==ipo) execute_ipo((ID *)ca, ipo); - ca= ca->id.next; - } + + case IPO_SHORT: + case IPO_SHORT_BIT: // fixme... directly revealing bitflag combinations is evil! + *((short *)poin)= (short)val; break; - case ID_SO: - snd= G.main->sound.first; - while(snd) { - if(snd->ipo==ipo) execute_ipo((ID *)snd, ipo); - snd= snd->id.next; - } + + case IPO_CHAR: + case IPO_CHAR_BIT: // fixme... directly revealing bitflag combinations is evil! + *((char *)poin)= (char)val; break; } } -void do_ipo(Ipo *ipo) +/* read the value from the pointer that was obtained */ +float read_ipo_poin (void *poin, int type) { - if(ipo) { - float ctime= frame_to_float(G.scene->r.cfra); - calc_ipo(ipo, ctime); - - do_ipo_nocalc(ipo); - } -} - - - -void do_mat_ipo(Material *ma) -{ - float ctime; - - if(ma==NULL || ma->ipo==NULL) return; - - ctime= frame_to_float(G.scene->r.cfra); - /* if(ob->ipoflag & OB_OFFS_OB) ctime-= ob->sf; */ - - calc_ipo(ma->ipo, ctime); - - execute_ipo((ID *)ma, ma->ipo); -} - -void do_ob_ipo(Object *ob) -{ - float ctime; - unsigned int lay; - - if(ob->ipo==NULL) return; - - /* do not set ob->ctime here: for example when parent in invisible layer */ - - ctime= bsystem_time(ob, (float) G.scene->r.cfra, 0.0); - - calc_ipo(ob->ipo, ctime); - - /* Patch: remember localview */ - lay= ob->lay & 0xFF000000; - - execute_ipo((ID *)ob, ob->ipo); - - ob->lay |= lay; - if(ob->id.name[2]=='S' && ob->id.name[3]=='C' && ob->id.name[4]=='E') { - if(strcmp(G.scene->id.name+2, ob->id.name+6)==0) { - G.scene->lay= ob->lay; - copy_view3d_lock(0); - /* no redraw here! creates too many calls */ - } - } -} - -void do_ob_ipodrivers(Object *ob, Ipo *ipo, float ctime) -{ - IpoCurve *icu; - void *poin; - int type; - - for(icu= ipo->curve.first; icu; icu= icu->next) { - if(icu->driver) { - icu->curval= eval_icu(icu, ctime); - poin= get_ipo_poin((ID *)ob, icu, &type); - if(poin) write_ipo_poin(poin, type, icu->curval); - } - } -} - -void do_seq_ipo(Sequence *seq, int cfra) -{ - float ctime, div; - - /* seq_ipo has an exception: calc both fields immediately */ + float val = 0.0; - if(seq->ipo) { - if((seq->flag & SEQ_IPO_FRAME_LOCKED) != 0) { - ctime = frame_to_float(cfra); - div = 1.0; - } else { - ctime= frame_to_float(cfra - seq->startdisp); - div= (seq->enddisp - seq->startdisp)/100.0f; - if(div==0.0) return; - } + /* Note: we only support a limited number of types, with the value + * to set needing to be cast to the appropriate type first + * -> (int to float conversions may loose accuracy in rare cases) + */ + switch (type) { + case IPO_FLOAT: + val= *((float *)poin); + break; - /* 2nd field */ - calc_ipo(seq->ipo, (ctime+0.5f)/div); - execute_ipo((ID *)seq, seq->ipo); - seq->facf1= seq->facf0; - - /* 1st field */ - calc_ipo(seq->ipo, ctime/div); - execute_ipo((ID *)seq, seq->ipo); - - } - else seq->facf1= seq->facf0= 1.0f; -} - -int has_ipo_code(Ipo *ipo, int code) -{ - IpoCurve *icu; - - if(ipo==NULL) return 0; - - for(icu= ipo->curve.first; icu; icu= icu->next) { - if(icu->adrcode==code) return 1; - } - return 0; -} - -void do_all_data_ipos() -{ - Material *ma; - Tex *tex; - World *wo; - Ipo *ipo; - Lamp *la; - Key *key; - Camera *ca; - bSound *snd; - Sequence *seq; - Editing *ed; - Base *base; - float ctime; - - ctime= frame_to_float(G.scene->r.cfra); - - /* this exception cannot be depgraphed yet... what todo with objects in other layers?... */ - for(base= G.scene->base.first; base; base= base->next) { - /* only update layer when an ipo */ - if( has_ipo_code(base->object->ipo, OB_LAY) ) { - do_ob_ipo(base->object); - base->lay= base->object->lay; - } - } - - /* layers for the set...*/ - if(G.scene->set) { - for(base= G.scene->set->base.first; base; base= base->next) { - if( has_ipo_code(base->object->ipo, OB_LAY) ) { - do_ob_ipo(base->object); - base->lay= base->object->lay; - } - } - } - - - ipo= G.main->ipo.first; - while(ipo) { - if(ipo->id.us && ipo->blocktype!=ID_OB) { - calc_ipo(ipo, ctime); - } - ipo= ipo->id.next; - } - - for(tex= G.main->tex.first; tex; tex= tex->id.next) { - if(tex->ipo) execute_ipo((ID *)tex, tex->ipo); - } - - for(ma= G.main->mat.first; ma; ma= ma->id.next) { - if(ma->ipo) execute_ipo((ID *)ma, ma->ipo); - } - - for(wo= G.main->world.first; wo; wo= wo->id.next) { - if(wo->ipo) execute_ipo((ID *)wo, wo->ipo); - } - - for(key= G.main->key.first; key; key= key->id.next) { - if(key->ipo) execute_ipo((ID *)key, key->ipo); - } - - la= G.main->lamp.first; - while(la) { - if(la->ipo) execute_ipo((ID *)la, la->ipo); - la= la->id.next; - } - - ca= G.main->camera.first; - while(ca) { - if(ca->ipo) execute_ipo((ID *)ca, ca->ipo); - ca= ca->id.next; - } - - snd= G.main->sound.first; - while(snd) { - if(snd->ipo) execute_ipo((ID *)snd, snd->ipo); - snd= snd->id.next; - } - - /* process FAC Ipos used as volume envelopes */ - ed= G.scene->ed; - if (ed) { - seq= ed->seqbasep->first; - while(seq) { - if ((seq->type == SEQ_RAM_SOUND - || seq->type == SEQ_HD_SOUND) && (seq->ipo) && - (seq->startdisp<=G.scene->r.cfra+2) && - (seq->enddisp>G.scene->r.cfra)) - do_seq_ipo(seq, G.scene->r.cfra); - seq= seq->next; - } - } - -} - - -int calc_ipo_spec(Ipo *ipo, int adrcode, float *ctime) -{ - IpoCurve *icu; - - if(ipo==NULL) return 0; - - for(icu= ipo->curve.first; icu; icu= icu->next) { - if(icu->adrcode == adrcode) { - if(icu->flag & IPO_LOCK); - else calc_icu(icu, *ctime); - - *ctime= icu->curval; - return 1; - } - } - - return 0; -} - - -/* ************************** */ - -void clear_delta_obipo(Ipo *ipo) -{ - Object *ob; + case IPO_FLOAT_DEGR: /* special hack for rotation so that it fits on same axis as other transforms */ + val= *( (float *)poin); + val = (float)(val / (M_PI_2/9.0)); + break; - if(ipo==NULL) return; + case IPO_INT: + case IPO_INT_BIT: // fixme... directly revealing bitflag combinations is evil! + case IPO_LONG: + val= (float)( *((int *)poin) ); + break; + + case IPO_SHORT: + case IPO_SHORT_BIT: // fixme... directly revealing bitflag combinations is evil! + val= *((short *)poin); + break; - ob= G.main->object.first; - while(ob) { - if(ob->id.lib==NULL) { - if(ob->ipo==ipo) { - memset(&ob->dloc, 0, 12); - memset(&ob->drot, 0, 12); - memset(&ob->dsize, 0, 12); - } - } - ob= ob->id.next; + case IPO_CHAR: + case IPO_CHAR_BIT: // fixme... directly revealing bitflag combinations is evil + val= *((char *)poin); + break; } -} - -void add_to_cfra_elem(ListBase *lb, BezTriple *bezt) -{ - CfraElem *ce, *cen; - - ce= lb->first; - while(ce) { - - if( ce->cfra==bezt->vec[1][0] ) { - /* do because of double keys */ - if(bezt->f2 & SELECT) ce->sel= bezt->f2; - return; - } - else if(ce->cfra > bezt->vec[1][0]) break; - - ce= ce->next; - } - cen= MEM_callocN(sizeof(CfraElem), "add_to_cfra_elem"); - if(ce) BLI_insertlinkbefore(lb, ce, cen); - else BLI_addtail(lb, cen); - - cen->cfra= bezt->vec[1][0]; - cen->sel= bezt->f2; + /* return value */ + return val; } +// !!!!!!!!!!!!!!!!!!!!!!!!!!!! FIXME - BAD CRUFT WARNING !!!!!!!!!!!!!!!!!!!!!!! -void make_cfra_list(Ipo *ipo, ListBase *elems) -{ - IpoCurve *icu; - BezTriple *bezt; - int a; - - if(ipo->blocktype==ID_OB) { - for(icu= ipo->curve.first; icu; icu= icu->next) { - if(icu->flag & IPO_VISIBLE) { - switch(icu->adrcode) { - case OB_DLOC_X: - case OB_DLOC_Y: - case OB_DLOC_Z: - case OB_DROT_X: - case OB_DROT_Y: - case OB_DROT_Z: - case OB_DSIZE_X: - case OB_DSIZE_Y: - case OB_DSIZE_Z: - - case OB_LOC_X: - case OB_LOC_Y: - case OB_LOC_Z: - case OB_ROT_X: - case OB_ROT_Y: - case OB_ROT_Z: - case OB_SIZE_X: - case OB_SIZE_Y: - case OB_SIZE_Z: - case OB_PD_FSTR: - case OB_PD_FFALL: - case OB_PD_SDAMP: - case OB_PD_RDAMP: - case OB_PD_PERM: - case OB_PD_FMAXD: - bezt= icu->bezt; - if(bezt) { - a= icu->totvert; - while(a--) { - add_to_cfra_elem(elems, bezt); - bezt++; - } - } - break; - } - } - } - } - else if(ipo->blocktype==ID_AC) { - for(icu= ipo->curve.first; icu; icu= icu->next) { - if(icu->flag & IPO_VISIBLE) { - switch(icu->adrcode) { - case AC_LOC_X: - case AC_LOC_Y: - case AC_LOC_Z: - case AC_SIZE_X: - case AC_SIZE_Y: - case AC_SIZE_Z: - case AC_QUAT_W: - case AC_QUAT_X: - case AC_QUAT_Y: - case AC_QUAT_Z: - bezt= icu->bezt; - if(bezt) { - a= icu->totvert; - while(a--) { - add_to_cfra_elem(elems, bezt); - bezt++; - } - } - break; - } - } - } - } - else { - for(icu= ipo->curve.first; icu; icu= icu->next) { - if(icu->flag & IPO_VISIBLE) { - bezt= icu->bezt; - if(bezt) { - a= icu->totvert; - while(a--) { - add_to_cfra_elem(elems, bezt); - bezt++; - } - } - } - } - } - - /* what's the point of this little block of code? */ -#if 0 - if(ipo->showkey==0) { - /* deselect all keys */ - ce= elems->first; - while(ce) { - ce->sel= 0; - ce= ce->next; - } - } -#endif -} - -/* *********************** INTERFACE FOR KETSJI ********** */ - +/* ***************************** IPO <--> GameEngine Interface ********************************* */ -int IPO_GetChannels(Ipo *ipo, IPO_Channel *channels) +/* channels is max 32 items, allocated by calling function */ +short IPO_GetChannels (Ipo *ipo, IPO_Channel *channels) { - /* channels is max 32 items, allocated by calling function */ - IpoCurve *icu; - int total=0; + int total = 0; - if(ipo==NULL) return 0; + /* don't do anything with no IPO-block */ + if (ipo == NULL) + return 0; - for(icu= ipo->curve.first; icu; icu= icu->next) { + /* store the IPO-curve's adrcode in the relevant channel slot */ + for (icu=ipo->curve.first; (icu) && (total < 31); icu=icu->next, total++) channels[total]= icu->adrcode; - total++; - if(total>31) break; - } + /* return the number of channels stored */ return total; } - - /* Get the float value for channel 'channel' at time 'ctime' */ - -float IPO_GetFloatValue(Ipo *ipo, IPO_Channel channel, float ctime) +float IPO_GetFloatValue (Ipo *ipo, IPO_Channel channel, float ctime) { - if(ipo==NULL) return 0; + /* don't evaluate if no IPO to use */ + if (ipo == NULL) + return 0; + /* only calculate the specified channel */ calc_ipo_spec(ipo, channel, &ctime); - if (OB_ROT_X <= channel && channel <= OB_DROT_Z) { + /* unapply rotation hack, as gameengine doesn't use it */ + if ((OB_ROT_X <= channel) && (channel <= OB_DROT_Z)) ctime *= (float)(M_PI_2/9.0); - } + /* return the value of this channel */ return ctime; } diff --git a/source/blender/blenkernel/intern/material.c b/source/blender/blenkernel/intern/material.c index 3983eecdaba..f05b84f6e90 100644 --- a/source/blender/blenkernel/intern/material.c +++ b/source/blender/blenkernel/intern/material.c @@ -61,6 +61,8 @@ #include "BPY_extern.h" +#include "GPU_material.h" + /* used in UI and render */ Material defmaterial; @@ -96,6 +98,9 @@ void free_material(Material *ma) ntreeFreeTree(ma->nodetree); MEM_freeN(ma->nodetree); } + + if(ma->gpumaterial.first) + GPU_material_free(ma); } void init_material(Material *ma) @@ -207,6 +212,8 @@ Material *copy_material(Material *ma) if(ma->nodetree) { man->nodetree= ntreeCopyTree(ma->nodetree, 0); /* 0 == full new tree */ } + + man->gpumaterial.first= man->gpumaterial.last= NULL; return man; } diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c index 43e48c3bacd..77b9ea4fa2d 100644 --- a/source/blender/blenkernel/intern/mesh.c +++ b/source/blender/blenkernel/intern/mesh.c @@ -79,45 +79,6 @@ #include "BLI_editVert.h" #include "BLI_arithb.h" -int update_realtime_texture(MTFace *tface, double time) -{ - Image *ima; - int inc = 0; - float diff; - int newframe; - - ima = tface->tpage; - - if (!ima) - return 0; - - if (ima->lastupdate<0) - ima->lastupdate = 0; - - if (ima->lastupdate>time) - ima->lastupdate=(float)time; - - if(ima->tpageflag & IMA_TWINANIM) { - if(ima->twend >= ima->xrep*ima->yrep) ima->twend= ima->xrep*ima->yrep-1; - - /* check: is the bindcode not in the array? Then free. (still to do) */ - - diff = (float)(time-ima->lastupdate); - - inc = (int)(diff*(float)ima->animspeed); - - ima->lastupdate+=((float)inc/(float)ima->animspeed); - - newframe = ima->lastframe+inc; - - if (newframe > (int)ima->twend) - newframe = (int)ima->twsta-1 + (newframe-ima->twend)%(ima->twend-ima->twsta); - - ima->lastframe = newframe; - } - return inc; -} - void mesh_update_customdata_pointers(Mesh *me) { me->mvert = CustomData_get_layer(&me->vdata, CD_MVERT); diff --git a/source/blender/blenkernel/intern/modifier.c b/source/blender/blenkernel/intern/modifier.c index 1dc76296c28..b9a60ca9329 100644 --- a/source/blender/blenkernel/intern/modifier.c +++ b/source/blender/blenkernel/intern/modifier.c @@ -54,8 +54,11 @@ #include "MEM_guardedalloc.h" +#include "DNA_action_types.h" #include "DNA_armature_types.h" +#include "DNA_camera_types.h" #include "DNA_cloth_types.h" +#include "DNA_curve_types.h" #include "DNA_effect_types.h" #include "DNA_material_types.h" #include "DNA_mesh_types.h" @@ -66,8 +69,6 @@ #include "DNA_particle_types.h" #include "DNA_scene_types.h" #include "DNA_texture_types.h" -#include "DNA_curve_types.h" -#include "DNA_camera_types.h" #include "BLI_editVert.h" @@ -77,30 +78,34 @@ #include "BKE_main.h" #include "BKE_anim.h" #include "BKE_bad_level_calls.h" +#include "BKE_bmesh.h" +#include "BKE_booleanops.h" #include "BKE_cloth.h" #include "BKE_collision.h" +#include "BKE_cdderivedmesh.h" #include "BKE_curve.h" #include "BKE_customdata.h" -#include "BKE_global.h" -#include "BKE_cdderivedmesh.h" #include "BKE_DerivedMesh.h" -#include "BKE_booleanops.h" #include "BKE_displist.h" -#include "BKE_modifier.h" +#include "BKE_fluidsim.h" +#include "BKE_global.h" #include "BKE_lattice.h" #include "BKE_library.h" -#include "BKE_subsurf.h" -#include "BKE_object.h" -#include "BKE_mesh.h" -#include "BKE_softbody.h" -#include "BKE_cloth.h" #include "BKE_material.h" +#include "BKE_mesh.h" +#include "BKE_modifier.h" +#include "BKE_object.h" #include "BKE_particle.h" #include "BKE_pointcache.h" +#include "BKE_softbody.h" +#include "BKE_subsurf.h" #include "BKE_texture.h" #include "BKE_utildefines.h" + #include "depsgraph_private.h" -#include "BKE_bmesh.h" +#include "BKE_deform.h" +#include "BKE_shrinkwrap.h" +#include "BKE_simple_deform.h" #include "LOD_DependKludge.h" #include "LOD_decimation.h" @@ -584,6 +589,361 @@ static DerivedMesh *buildModifier_applyModifier(ModifierData *md, Object *ob, return result; } +/* Mask */ + +static void maskModifier_copyData(ModifierData *md, ModifierData *target) +{ + MaskModifierData *mmd = (MaskModifierData*) md; + MaskModifierData *tmmd = (MaskModifierData*) target; + + strcpy(tmmd->vgroup, mmd->vgroup); +} + +static CustomDataMask maskModifier_requiredDataMask(ModifierData *md) +{ + return (1 << CD_MDEFORMVERT); +} + +static void maskModifier_foreachObjectLink( + ModifierData *md, Object *ob, + void (*walk)(void *userData, Object *ob, Object **obpoin), + void *userData) +{ + MaskModifierData *mmd = (MaskModifierData *)md; + walk(userData, ob, &mmd->ob_arm); +} + +static void maskModifier_updateDepgraph(ModifierData *md, DagForest *forest, + Object *ob, DagNode *obNode) +{ + MaskModifierData *mmd = (MaskModifierData *)md; + + if (mmd->ob_arm) + { + DagNode *armNode = dag_get_node(forest, mmd->ob_arm); + + dag_add_relation(forest, armNode, obNode, + DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Mask Modifier"); + } +} + +static DerivedMesh *maskModifier_applyModifier(ModifierData *md, Object *ob, + DerivedMesh *derivedData, + int useRenderParams, int isFinalCalc) +{ + MaskModifierData *mmd= (MaskModifierData *)md; + DerivedMesh *dm= derivedData, *result= NULL; + GHash *vertHash=NULL, *edgeHash, *faceHash; + GHashIterator *hashIter; + MDeformVert *dvert= NULL; + int numFaces=0, numEdges=0, numVerts=0; + int maxVerts, maxEdges, maxFaces; + int i; + + /* Overview of Method: + * 1. Get the vertices that are in the vertexgroup of interest + * 2. Filter out unwanted geometry (i.e. not in vertexgroup), by populating mappings with new vs old indices + * 3. Make a new mesh containing only the mapping data + */ + + /* get original number of verts, edges, and faces */ + maxVerts= dm->getNumVerts(dm); + maxEdges= dm->getNumEdges(dm); + maxFaces= dm->getNumFaces(dm); + + /* check if we can just return the original mesh + * - must have verts and therefore verts assigned to vgroups to do anything useful + */ + if ( !(ELEM(mmd->mode, MOD_MASK_MODE_ARM, MOD_MASK_MODE_VGROUP)) || + (maxVerts == 0) || (ob->defbase.first == NULL) ) + { + return derivedData; + } + + /* if mode is to use selected armature bones, aggregate the bone groups */ + if (mmd->mode == MOD_MASK_MODE_ARM) /* --- using selected bones --- */ + { + GHash *vgroupHash, *boneHash; + Object *oba= mmd->ob_arm; + bPoseChannel *pchan; + bDeformGroup *def; + + /* check that there is armature object with bones to use, otherwise return original mesh */ + if (ELEM(NULL, mmd->ob_arm, mmd->ob_arm->pose)) + return derivedData; + + /* hashes for finding mapping of: + * - vgroups to indicies -> vgroupHash (string, int) + * - bones to vgroup indices -> boneHash (index of vgroup, dummy) + */ + vgroupHash= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp); + boneHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp); + + /* build mapping of names of vertex groups to indices */ + for (i = 0, def = ob->defbase.first; def; def = def->next, i++) + BLI_ghash_insert(vgroupHash, def->name, SET_INT_IN_POINTER(i)); + + /* get selected-posechannel <-> vertexgroup index mapping */ + for (pchan= oba->pose->chanbase.first; pchan; pchan= pchan->next) + { + /* check if bone is selected */ + // TODO: include checks for visibility too? + // FIXME: the depsgraph needs extensions to make this work in realtime... + if ( (pchan->bone) && (pchan->bone->flag & BONE_SELECTED) ) + { + /* check if hash has group for this bone */ + if (BLI_ghash_haskey(vgroupHash, pchan->name)) + { + int defgrp_index= GET_INT_FROM_POINTER(BLI_ghash_lookup(vgroupHash, pchan->name)); + + /* add index to hash (store under key only) */ + BLI_ghash_insert(boneHash, SET_INT_IN_POINTER(defgrp_index), pchan); + } + } + } + + /* if no bones selected, free hashes and return original mesh */ + if (BLI_ghash_size(boneHash) == 0) + { + BLI_ghash_free(vgroupHash, NULL, NULL); + BLI_ghash_free(boneHash, NULL, NULL); + + return derivedData; + } + + /* repeat the previous check, but for dverts */ + dvert= dm->getVertDataArray(dm, CD_MDEFORMVERT); + if (dvert == NULL) + { + BLI_ghash_free(vgroupHash, NULL, NULL); + BLI_ghash_free(boneHash, NULL, NULL); + + return derivedData; + } + + /* hashes for quickly providing a mapping from old to new - use key=oldindex, value=newindex */ + vertHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp); + + /* add vertices which exist in vertexgroups into vertHash for filtering */ + for (i = 0; i < maxVerts; i++) + { + MDeformWeight *def_weight = NULL; + int j; + + for (j= 0; j < dvert[i].totweight; j++) + { + if (BLI_ghash_haskey(boneHash, SET_INT_IN_POINTER(dvert[i].dw[j].def_nr))) + { + def_weight = &dvert[i].dw[j]; + break; + } + } + + /* check if include vert in vertHash */ + if (mmd->flag & MOD_MASK_INV) { + /* if this vert is in the vgroup, don't include it in vertHash */ + if (def_weight) continue; + } + else { + /* if this vert isn't in the vgroup, don't include it in vertHash */ + if (!def_weight) continue; + } + + /* add to ghash for verts (numVerts acts as counter for mapping) */ + BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(numVerts)); + numVerts++; + } + + /* free temp hashes */ + BLI_ghash_free(vgroupHash, NULL, NULL); + BLI_ghash_free(boneHash, NULL, NULL); + } + else /* --- Using Nominated VertexGroup only --- */ + { + int defgrp_index = -1; + + /* get index of vertex group */ + if (mmd->vgroup[0]) + { + bDeformGroup *def; + + /* find index by comparing names - SLOW... */ + for (i = 0, def = ob->defbase.first; def; def = def->next, i++) + { + if (!strcmp(def->name, mmd->vgroup)) + { + defgrp_index = i; + break; + } + } + } + + /* get dverts */ + if (defgrp_index >= 0) + dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT); + + /* if no vgroup (i.e. dverts) found, return the initial mesh */ + if ((defgrp_index < 0) || (dvert == NULL)) + return dm; + + /* hashes for quickly providing a mapping from old to new - use key=oldindex, value=newindex */ + vertHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp); + + /* add vertices which exist in vertexgroup into ghash for filtering */ + for (i = 0; i < maxVerts; i++) + { + MDeformWeight *def_weight = NULL; + int j; + + for (j= 0; j < dvert[i].totweight; j++) + { + if (dvert[i].dw[j].def_nr == defgrp_index) + { + def_weight = &dvert[i].dw[j]; + break; + } + } + + /* check if include vert in vertHash */ + if (mmd->flag & MOD_MASK_INV) { + /* if this vert is in the vgroup, don't include it in vertHash */ + if (def_weight) continue; + } + else { + /* if this vert isn't in the vgroup, don't include it in vertHash */ + if (!def_weight) continue; + } + + /* add to ghash for verts (numVerts acts as counter for mapping) */ + BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(numVerts)); + numVerts++; + } + } + + /* hashes for quickly providing a mapping from old to new - use key=oldindex, value=newindex */ + edgeHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp); + faceHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp); + + /* loop over edges and faces, and do the same thing to + * ensure that they only reference existing verts + */ + for (i = 0; i < maxEdges; i++) + { + MEdge me; + dm->getEdge(dm, i, &me); + + /* only add if both verts will be in new mesh */ + if ( BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v1)) && + BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v2)) ) + { + BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(numEdges)); + numEdges++; + } + } + for (i = 0; i < maxFaces; i++) + { + MFace mf; + dm->getFace(dm, i, &mf); + + /* all verts must be available */ + if ( BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v1)) && + BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v2)) && + BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v3)) && + (mf.v4==0 || BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v4))) ) + { + BLI_ghash_insert(faceHash, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(numFaces)); + numFaces++; + } + } + + + /* now we know the number of verts, edges and faces, + * we can create the new (reduced) mesh + */ + result = CDDM_from_template(dm, numVerts, numEdges, numFaces); + + + /* using ghash-iterators, map data into new mesh */ + /* vertices */ + for ( hashIter = BLI_ghashIterator_new(vertHash); + !BLI_ghashIterator_isDone(hashIter); + BLI_ghashIterator_step(hashIter) ) + { + MVert source; + MVert *dest; + int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter)); + int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter)); + + dm->getVert(dm, oldIndex, &source); + dest = CDDM_get_vert(result, newIndex); + + DM_copy_vert_data(dm, result, oldIndex, newIndex, 1); + *dest = source; + } + BLI_ghashIterator_free(hashIter); + + /* edges */ + for ( hashIter = BLI_ghashIterator_new(edgeHash); + !BLI_ghashIterator_isDone(hashIter); + BLI_ghashIterator_step(hashIter) ) + { + MEdge source; + MEdge *dest; + int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter)); + int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter)); + + dm->getEdge(dm, oldIndex, &source); + dest = CDDM_get_edge(result, newIndex); + + source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1))); + source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2))); + + DM_copy_edge_data(dm, result, oldIndex, newIndex, 1); + *dest = source; + } + BLI_ghashIterator_free(hashIter); + + /* faces */ + for ( hashIter = BLI_ghashIterator_new(faceHash); + !BLI_ghashIterator_isDone(hashIter); + BLI_ghashIterator_step(hashIter) ) + { + MFace source; + MFace *dest; + int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter)); + int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter)); + int orig_v4; + + dm->getFace(dm, oldIndex, &source); + dest = CDDM_get_face(result, newIndex); + + orig_v4 = source.v4; + + source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1))); + source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2))); + source.v3 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v3))); + if (source.v4) + source.v4 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v4))); + + DM_copy_face_data(dm, result, oldIndex, newIndex, 1); + *dest = source; + + test_index_face(dest, &result->faceData, newIndex, (orig_v4 ? 4 : 3)); + } + BLI_ghashIterator_free(hashIter); + + /* recalculate normals */ + CDDM_calc_normals(result); + + /* free hashes */ + BLI_ghash_free(vertHash, NULL, NULL); + BLI_ghash_free(edgeHash, NULL, NULL); + BLI_ghash_free(faceHash, NULL, NULL); + + /* return the new mesh */ + return result; +} + /* Array */ /* Array modifier: duplicates the object multiple times along an axis */ @@ -791,12 +1151,18 @@ static DerivedMesh *arrayModifier_doArray(ArrayModifierData *amd, if(amd->fit_type == MOD_ARR_FITCURVE && amd->curve_ob) { Curve *cu = amd->curve_ob->data; if(cu) { + float tmp_mat[3][3]; + float scale; + + object_to_mat3(amd->curve_ob, tmp_mat); + scale = Mat3ToScalef(tmp_mat); + if(!cu->path) { cu->flag |= CU_PATH; // needed for path & bevlist makeDispListCurveTypes(amd->curve_ob, 0); } if(cu->path) - length = cu->path->totdist; + length = scale*cu->path->totdist; } } @@ -1773,6 +2139,8 @@ typedef struct SmoothMesh { DerivedMesh *dm; float threshold; /* the cosine of the smoothing angle */ int flags; + MemArena *arena; + ListBase propagatestack, reusestack; } SmoothMesh; static SmoothVert *smoothvert_copy(SmoothVert *vert, SmoothMesh *mesh) @@ -1855,6 +2223,9 @@ static void smoothmesh_free(SmoothMesh *mesh) for(i = 0; i < mesh->num_edges; ++i) BLI_linklist_free(mesh->edges[i].faces, NULL); + + if(mesh->arena) + BLI_memarena_free(mesh->arena); MEM_freeN(mesh->verts); MEM_freeN(mesh->edges); @@ -2506,6 +2877,49 @@ static void split_single_vert(SmoothVert *vert, SmoothFace *face, face_replace_vert(face, &repdata); } +typedef struct PropagateEdge { + struct PropagateEdge *next, *prev; + SmoothEdge *edge; + SmoothVert *vert; +} PropagateEdge; + +static void push_propagate_stack(SmoothEdge *edge, SmoothVert *vert, SmoothMesh *mesh) +{ + PropagateEdge *pedge = mesh->reusestack.first; + + if(pedge) { + BLI_remlink(&mesh->reusestack, pedge); + } + else { + if(!mesh->arena) { + mesh->arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE); + BLI_memarena_use_calloc(mesh->arena); + } + + pedge = BLI_memarena_alloc(mesh->arena, sizeof(PropagateEdge)); + } + + pedge->edge = edge; + pedge->vert = vert; + BLI_addhead(&mesh->propagatestack, pedge); +} + +static void pop_propagate_stack(SmoothEdge **edge, SmoothVert **vert, SmoothMesh *mesh) +{ + PropagateEdge *pedge = mesh->propagatestack.first; + + if(pedge) { + *edge = pedge->edge; + *vert = pedge->vert; + BLI_remlink(&mesh->propagatestack, pedge); + BLI_addhead(&mesh->reusestack, pedge); + } + else { + *edge = NULL; + *vert = NULL; + } +} + static void split_edge(SmoothEdge *edge, SmoothVert *vert, SmoothMesh *mesh); static void propagate_split(SmoothEdge *edge, SmoothVert *vert, @@ -2583,7 +2997,7 @@ static void split_edge(SmoothEdge *edge, SmoothVert *vert, SmoothMesh *mesh) if(!edge2) { /* didn't find a sharp or loose edge, so try the other vert */ vert2 = other_vert(edge, vert); - propagate_split(edge, vert2, mesh); + push_propagate_stack(edge, vert2, mesh); } else if(!edge_is_loose(edge2)) { /* edge2 is not loose, so it must be sharp */ SmoothEdge *copy_edge = smoothedge_copy(edge, mesh); @@ -2612,11 +3026,11 @@ static void split_edge(SmoothEdge *edge, SmoothVert *vert, SmoothMesh *mesh) /* all copying and replacing is done; the mesh should be consistent. * now propagate the split to the vertices at either end */ - propagate_split(copy_edge, other_vert(copy_edge, vert2), mesh); - propagate_split(copy_edge2, other_vert(copy_edge2, vert2), mesh); + push_propagate_stack(copy_edge, other_vert(copy_edge, vert2), mesh); + push_propagate_stack(copy_edge2, other_vert(copy_edge2, vert2), mesh); if(smoothedge_has_vert(edge, vert)) - propagate_split(edge, vert, mesh); + push_propagate_stack(edge, vert, mesh); } else { /* edge2 is loose */ SmoothEdge *copy_edge = smoothedge_copy(edge, mesh); @@ -2639,10 +3053,10 @@ static void split_edge(SmoothEdge *edge, SmoothVert *vert, SmoothMesh *mesh) /* copying and replacing is done; the mesh should be consistent. * now propagate the split to the vertex at the other end */ - propagate_split(copy_edge, other_vert(copy_edge, vert2), mesh); + push_propagate_stack(copy_edge, other_vert(copy_edge, vert2), mesh); if(smoothedge_has_vert(edge, vert)) - propagate_split(edge, vert, mesh); + push_propagate_stack(edge, vert, mesh); } BLI_linklist_free(visited_faces, NULL); @@ -2714,6 +3128,7 @@ static void tag_and_count_extra_edges(SmoothMesh *mesh, float split_angle, static void split_sharp_edges(SmoothMesh *mesh, float split_angle, int flags) { + SmoothVert *vert; int i; /* if normal1 dot normal2 < threshold, angle is greater, so split */ /* FIXME not sure if this always works */ @@ -2726,10 +3141,16 @@ static void split_sharp_edges(SmoothMesh *mesh, float split_angle, int flags) for(i = 0; i < mesh->num_edges; i++) { SmoothEdge *edge = &mesh->edges[i]; - if(edge_is_sharp(edge, flags, mesh->threshold)) + if(edge_is_sharp(edge, flags, mesh->threshold)) { split_edge(edge, edge->verts[0], mesh); - } + do { + pop_propagate_stack(&edge, &vert, mesh); + if(edge && smoothedge_has_vert(edge, vert)) + propagate_split(edge, vert, mesh); + } while(edge); + } + } } static int count_bridge_verts(SmoothMesh *mesh) @@ -5285,6 +5706,11 @@ static void softbodyModifier_deformVerts( sbObjectStep(ob, (float)G.scene->r.cfra, vertexCos, numVerts); } +static int softbodyModifier_dependsOnTime(ModifierData *md) +{ + return 1; +} + /* Cloth */ @@ -5493,7 +5919,7 @@ static void collisionModifier_deformVerts( numverts = dm->getNumVerts ( dm ); - if(current_time > collmd->time) + if((current_time > collmd->time)|| (BKE_ptcache_get_continue_physics())) { // check if mesh has changed if(collmd->x && (numverts != collmd->numverts)) @@ -6120,22 +6546,6 @@ CustomDataMask explodeModifier_requiredDataMask(ModifierData *md) return dataMask; } -/* this should really be put somewhere permanently */ -static float vert_weight(MDeformVert *dvert, int group) -{ - MDeformWeight *dw; - int i; - - if(dvert) { - dw= dvert->dw; - for(i= dvert->totweight; i>0; i--, dw++) { - if(dw->def_nr == group) return dw->weight; - if(i==1) break; /*otherwise dw will point to somewhere it shouldn't*/ - } - } - return 0.0; -} - static void explodeModifier_createFacepa(ExplodeModifierData *emd, ParticleSystemModifierData *psmd, Object *ob, DerivedMesh *dm) @@ -6179,7 +6589,7 @@ static void explodeModifier_createFacepa(ExplodeModifierData *emd, for(i=0; i<totvert; i++){ val = BLI_frand(); val = (1.0f-emd->protect)*val + emd->protect*0.5f; - if(val < vert_weight(dvert+i,emd->vgroup-1)) + if(val < deformvert_get_weight(dvert+i,emd->vgroup-1)) vertpa[i] = -1; } } @@ -6188,7 +6598,7 @@ static void explodeModifier_createFacepa(ExplodeModifierData *emd, /* make tree of emitter locations */ tree=BLI_kdtree_new(totpart); for(p=0,pa=psys->particles; p<totpart; p++,pa++){ - psys_particle_on_dm(ob,psmd->dm,psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,0,0,0,0,0); + psys_particle_on_dm(psmd->dm,psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,0,0,0,0,0); BLI_kdtree_insert(tree, p, co, NULL); } BLI_kdtree_balance(tree); @@ -6708,7 +7118,6 @@ static DerivedMesh * explodeModifier_explodeMesh(ExplodeModifierData *emd, { DerivedMesh *explode, *dm=to_explode; MFace *mf=0; - MVert *dupvert=0; ParticleSettings *part=psmd->psys->part; ParticleData *pa=NULL, *pars=psmd->psys->particles; ParticleKey state; @@ -6763,7 +7172,7 @@ static DerivedMesh * explodeModifier_explodeMesh(ExplodeModifierData *emd, /* the final duplicated vertices */ explode= CDDM_from_template(dm, totdup, 0,totface); - dupvert= CDDM_get_verts(explode); + /*dupvert= CDDM_get_verts(explode);*/ /* getting back to object space */ Mat4Invert(imat,ob->obmat); @@ -6792,7 +7201,7 @@ static DerivedMesh * explodeModifier_explodeMesh(ExplodeModifierData *emd, pa= pars+i; /* get particle state */ - psys_particle_on_emitter(ob, psmd,part->from,pa->num,-1,pa->fuv,pa->foffset,loc0,nor,0,0,0,0); + psys_particle_on_emitter(psmd,part->from,pa->num,-1,pa->fuv,pa->foffset,loc0,nor,0,0,0,0); Mat4MulVecfl(ob->obmat,loc0); state.time=cfra; @@ -6924,6 +7333,93 @@ static DerivedMesh * explodeModifier_applyModifier( } return derivedData; } + +/* Fluidsim */ +static void fluidsimModifier_initData(ModifierData *md) +{ + FluidsimModifierData *fluidmd= (FluidsimModifierData*) md; + + fluidsim_init(fluidmd); +} +static void fluidsimModifier_freeData(ModifierData *md) +{ + FluidsimModifierData *fluidmd= (FluidsimModifierData*) md; + + fluidsim_free(fluidmd); +} + +static void fluidsimModifier_copyData(ModifierData *md, ModifierData *target) +{ + FluidsimModifierData *fluidmd= (FluidsimModifierData*) md; + FluidsimModifierData *tfluidmd= (FluidsimModifierData*) target; + + if(tfluidmd->fss) + MEM_freeN(tfluidmd->fss); + + tfluidmd->fss = MEM_dupallocN(fluidmd->fss); +} + +static DerivedMesh * fluidsimModifier_applyModifier( + ModifierData *md, Object *ob, DerivedMesh *derivedData, + int useRenderParams, int isFinalCalc) +{ + FluidsimModifierData *fluidmd= (FluidsimModifierData*) md; + DerivedMesh *result = NULL; + + /* check for alloc failing */ + if(!fluidmd->fss) + { + fluidsimModifier_initData(md); + + if(!fluidmd->fss) + return derivedData; + } + + result = fluidsimModifier_do(fluidmd, ob, derivedData, useRenderParams, isFinalCalc); + + if(result) + { + return result; + } + + return derivedData; +} + +static void fluidsimModifier_updateDepgraph( + ModifierData *md, DagForest *forest, + Object *ob, DagNode *obNode) +{ + FluidsimModifierData *fluidmd= (FluidsimModifierData*) md; + Base *base; + + if(fluidmd && fluidmd->fss) + { + if(fluidmd->fss->type == OB_FLUIDSIM_DOMAIN) + { + for(base = G.scene->base.first; base; base= base->next) + { + Object *ob1= base->object; + if(ob1 != ob) + { + FluidsimModifierData *fluidmdtmp = (FluidsimModifierData *)modifiers_findByType(ob1, eModifierType_Fluidsim); + + // only put dependancies from NON-DOMAIN fluids in here + if(fluidmdtmp && fluidmdtmp->fss && (fluidmdtmp->fss->type!=OB_FLUIDSIM_DOMAIN)) + { + DagNode *curNode = dag_get_node(forest, ob1); + dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Fluidsim Object"); + } + } + } + } + } +} + +static int fluidsimModifier_dependsOnTime(ModifierData *md) +{ + return 1; +} + /* MeshDeform */ static void meshdeformModifier_initData(ModifierData *md) @@ -7236,6 +7732,229 @@ static void meshdeformModifier_deformVertsEM( dm->release(dm); } + +/* Shrinkwrap */ + +static void shrinkwrapModifier_initData(ModifierData *md) +{ + ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md; + smd->shrinkType = MOD_SHRINKWRAP_NEAREST_SURFACE; + smd->shrinkOpts = MOD_SHRINKWRAP_PROJECT_ALLOW_POS_DIR; + smd->keepDist = 0.0f; + + smd->target = NULL; + smd->auxTarget = NULL; +} + +static void shrinkwrapModifier_copyData(ModifierData *md, ModifierData *target) +{ + ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*)md; + ShrinkwrapModifierData *tsmd = (ShrinkwrapModifierData*)target; + + tsmd->target = smd->target; + tsmd->auxTarget = smd->auxTarget; + + strcpy(tsmd->vgroup_name, smd->vgroup_name); + + tsmd->keepDist = smd->keepDist; + tsmd->shrinkType= smd->shrinkType; + tsmd->shrinkOpts= smd->shrinkOpts; +} + +CustomDataMask shrinkwrapModifier_requiredDataMask(ModifierData *md) +{ + ShrinkwrapModifierData *smd = (ShrinkwrapModifierData *)md; + CustomDataMask dataMask = 0; + + /* ask for vertexgroups if we need them */ + if(smd->vgroup_name[0]) + dataMask |= (1 << CD_MDEFORMVERT); + + if(smd->shrinkType == MOD_SHRINKWRAP_PROJECT + && smd->projAxis == MOD_SHRINKWRAP_PROJECT_OVER_NORMAL) + dataMask |= (1 << CD_MVERT); + + return dataMask; +} + +static int shrinkwrapModifier_isDisabled(ModifierData *md) +{ + ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md; + return !smd->target; +} + + +static void shrinkwrapModifier_foreachObjectLink(ModifierData *md, Object *ob, ObjectWalkFunc walk, void *userData) +{ + ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md; + + walk(userData, ob, &smd->target); + walk(userData, ob, &smd->auxTarget); +} + +static void shrinkwrapModifier_deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts) +{ + DerivedMesh *dm = NULL; + CustomDataMask dataMask = shrinkwrapModifier_requiredDataMask(md); + + /* We implement requiredDataMask but thats not really usefull since mesh_calc_modifiers pass a NULL derivedData or without the modified vertexs applied */ + if(dataMask) + { + if(derivedData) dm = CDDM_copy(derivedData); + else if(ob->type==OB_MESH) dm = CDDM_from_mesh(ob->data, ob); + else return; + + if(dataMask & CD_MVERT) + { + CDDM_apply_vert_coords(dm, vertexCos); + CDDM_calc_normals(dm); + } + } + + shrinkwrapModifier_deform((ShrinkwrapModifierData*)md, ob, dm, vertexCos, numVerts); + + if(dm) + dm->release(dm); +} + +static void shrinkwrapModifier_deformVertsEM(ModifierData *md, Object *ob, EditMesh *editData, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts) +{ + DerivedMesh *dm = NULL; + CustomDataMask dataMask = shrinkwrapModifier_requiredDataMask(md); + + if(dataMask) + { + if(derivedData) dm = CDDM_copy(derivedData); + else if(ob->type==OB_MESH) dm = CDDM_from_editmesh(editData, ob->data); + else return; + + if(dataMask & CD_MVERT) + { + CDDM_apply_vert_coords(dm, vertexCos); + CDDM_calc_normals(dm); + } + } + + shrinkwrapModifier_deform((ShrinkwrapModifierData*)md, ob, dm, vertexCos, numVerts); + + if(dm) + dm->release(dm); +} + +static void shrinkwrapModifier_updateDepgraph(ModifierData *md, DagForest *forest, Object *ob, DagNode *obNode) +{ + ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md; + + if (smd->target) + dag_add_relation(forest, dag_get_node(forest, smd->target), obNode, DAG_RL_OB_DATA | DAG_RL_DATA_DATA, "Shrinkwrap Modifier"); + + if (smd->auxTarget) + dag_add_relation(forest, dag_get_node(forest, smd->auxTarget), obNode, DAG_RL_OB_DATA | DAG_RL_DATA_DATA, "Shrinkwrap Modifier"); +} + +/* SimpleDeform */ +static void simpledeformModifier_initData(ModifierData *md) +{ + SimpleDeformModifierData *smd = (SimpleDeformModifierData*) md; + + smd->mode = MOD_SIMPLEDEFORM_MODE_TWIST; + smd->axis = 0; + + smd->origin = NULL; + smd->factor = 0.35f; + smd->limit[0] = 0.0f; + smd->limit[1] = 1.0f; +} + +static void simpledeformModifier_copyData(ModifierData *md, ModifierData *target) +{ + SimpleDeformModifierData *smd = (SimpleDeformModifierData*)md; + SimpleDeformModifierData *tsmd = (SimpleDeformModifierData*)target; + + tsmd->mode = smd->mode; + tsmd->axis = smd->axis; + tsmd->origin= smd->origin; + tsmd->factor= smd->factor; + memcpy(tsmd->limit, smd->limit, sizeof(tsmd->limit)); +} + +static CustomDataMask simpledeformModifier_requiredDataMask(ModifierData *md) +{ + SimpleDeformModifierData *smd = (SimpleDeformModifierData *)md; + CustomDataMask dataMask = 0; + + /* ask for vertexgroups if we need them */ + if(smd->vgroup_name[0]) + dataMask |= (1 << CD_MDEFORMVERT); + + return dataMask; +} + +static void simpledeformModifier_foreachObjectLink(ModifierData *md, Object *ob, void (*walk)(void *userData, Object *ob, Object **obpoin), void *userData) +{ + SimpleDeformModifierData *smd = (SimpleDeformModifierData*)md; + walk(userData, ob, &smd->origin); +} + +static void simpledeformModifier_updateDepgraph(ModifierData *md, DagForest *forest, Object *ob, DagNode *obNode) +{ + SimpleDeformModifierData *smd = (SimpleDeformModifierData*)md; + + if (smd->origin) + dag_add_relation(forest, dag_get_node(forest, smd->origin), obNode, DAG_RL_OB_DATA, "SimpleDeform Modifier"); +} + +static void simpledeformModifier_deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts) +{ + DerivedMesh *dm = NULL; + CustomDataMask dataMask = simpledeformModifier_requiredDataMask(md); + + /* We implement requiredDataMask but thats not really usefull since mesh_calc_modifiers pass a NULL derivedData or without the modified vertexs applied */ + if(dataMask) + { + if(derivedData) dm = CDDM_copy(derivedData); + else if(ob->type==OB_MESH) dm = CDDM_from_mesh(ob->data, ob); + else return; + + if(dataMask & CD_MVERT) + { + CDDM_apply_vert_coords(dm, vertexCos); + CDDM_calc_normals(dm); + } + } + + SimpleDeformModifier_do((SimpleDeformModifierData*)md, ob, dm, vertexCos, numVerts); + + if(dm) + dm->release(dm); + +} + +static void simpledeformModifier_deformVertsEM(ModifierData *md, Object *ob, EditMesh *editData, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts) +{ + DerivedMesh *dm = NULL; + CustomDataMask dataMask = simpledeformModifier_requiredDataMask(md); + + /* We implement requiredDataMask but thats not really usefull since mesh_calc_modifiers pass a NULL derivedData or without the modified vertexs applied */ + if(dataMask) + { + if(derivedData) dm = CDDM_copy(derivedData); + else if(ob->type==OB_MESH) dm = CDDM_from_editmesh(editData, ob->data); + else return; + + if(dataMask & CD_MVERT) + { + CDDM_apply_vert_coords(dm, vertexCos); + CDDM_calc_normals(dm); + } + } + + SimpleDeformModifier_do((SimpleDeformModifierData*)md, ob, dm, vertexCos, numVerts); + + if(dm) + dm->release(dm); +} + /***/ static ModifierTypeInfo typeArr[NUM_MODIFIER_TYPES]; @@ -7314,6 +8033,15 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type) mti->copyData = buildModifier_copyData; mti->dependsOnTime = buildModifier_dependsOnTime; mti->applyModifier = buildModifier_applyModifier; + + mti = INIT_TYPE(Mask); + mti->type = eModifierTypeType_Nonconstructive; + mti->flags = eModifierTypeFlag_AcceptsMesh; + mti->copyData = maskModifier_copyData; + mti->requiredDataMask= maskModifier_requiredDataMask; + mti->foreachObjectLink = maskModifier_foreachObjectLink; + mti->updateDepgraph = maskModifier_updateDepgraph; + mti->applyModifier = maskModifier_applyModifier; mti = INIT_TYPE(Array); mti->type = eModifierTypeType_Constructive; @@ -7468,6 +8196,7 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type) mti->flags = eModifierTypeFlag_AcceptsCVs | eModifierTypeFlag_RequiresOriginalData; mti->deformVerts = softbodyModifier_deformVerts; + mti->dependsOnTime = softbodyModifier_dependsOnTime; mti = INIT_TYPE(Cloth); mti->type = eModifierTypeType_Nonconstructive; @@ -7556,6 +8285,46 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type) mti->dependsOnTime = explodeModifier_dependsOnTime; mti->requiredDataMask = explodeModifier_requiredDataMask; mti->applyModifier = explodeModifier_applyModifier; + + mti = INIT_TYPE(Fluidsim); + mti->type = eModifierTypeType_Nonconstructive + | eModifierTypeFlag_RequiresOriginalData; + mti->flags = eModifierTypeFlag_AcceptsMesh; + mti->initData = fluidsimModifier_initData; + mti->freeData = fluidsimModifier_freeData; + mti->copyData = fluidsimModifier_copyData; + mti->dependsOnTime = fluidsimModifier_dependsOnTime; + mti->applyModifier = fluidsimModifier_applyModifier; + mti->updateDepgraph = fluidsimModifier_updateDepgraph; + + mti = INIT_TYPE(Shrinkwrap); + mti->type = eModifierTypeType_OnlyDeform; + mti->flags = eModifierTypeFlag_AcceptsMesh + | eModifierTypeFlag_AcceptsCVs + | eModifierTypeFlag_SupportsEditmode + | eModifierTypeFlag_EnableInEditmode; + mti->initData = shrinkwrapModifier_initData; + mti->copyData = shrinkwrapModifier_copyData; + mti->requiredDataMask = shrinkwrapModifier_requiredDataMask; + mti->isDisabled = shrinkwrapModifier_isDisabled; + mti->foreachObjectLink = shrinkwrapModifier_foreachObjectLink; + mti->deformVerts = shrinkwrapModifier_deformVerts; + mti->deformVertsEM = shrinkwrapModifier_deformVertsEM; + mti->updateDepgraph = shrinkwrapModifier_updateDepgraph; + + mti = INIT_TYPE(SimpleDeform); + mti->type = eModifierTypeType_OnlyDeform; + mti->flags = eModifierTypeFlag_AcceptsMesh + | eModifierTypeFlag_AcceptsCVs + | eModifierTypeFlag_SupportsEditmode + | eModifierTypeFlag_EnableInEditmode; + mti->initData = simpledeformModifier_initData; + mti->copyData = simpledeformModifier_copyData; + mti->requiredDataMask = simpledeformModifier_requiredDataMask; + mti->deformVerts = simpledeformModifier_deformVerts; + mti->deformVertsEM = simpledeformModifier_deformVertsEM; + mti->foreachObjectLink = simpledeformModifier_foreachObjectLink; + mti->updateDepgraph = simpledeformModifier_updateDepgraph; typeArrInit = 0; #undef INIT_TYPE @@ -7979,3 +8748,4 @@ void modifier_freeTemporaryData(ModifierData *md) } + diff --git a/source/blender/blenkernel/intern/multires.c b/source/blender/blenkernel/intern/multires.c index 9bc7fe28f44..259a4e0de04 100644 --- a/source/blender/blenkernel/intern/multires.c +++ b/source/blender/blenkernel/intern/multires.c @@ -208,7 +208,7 @@ static void multires_get_edge(MultiresEdge *e, EditEdge *eed, MEdge *m, short *f } } -static void multires_get_face(MultiresFace *f, EditFace *efa, MFace *m) +static void multires_get_face(MultiresFace *f, CustomData *fdata, int findex, EditFace *efa, MFace *m) { if(efa) { MFace tmp; @@ -218,7 +218,7 @@ static void multires_get_face(MultiresFace *f, EditFace *efa, MFace *m) tmp.v3= efa->v3->tmp.l; tmp.v4= 0; if(efa->v4) tmp.v4= efa->v4->tmp.l; - test_index_face(&tmp, NULL, 0, efa->v4?4:3); + test_index_face(&tmp, fdata, findex, efa->v4?4:3); for(j=0; j<4; ++j) f->v[j]= (&tmp.v1)[j]; /* Flags */ @@ -388,7 +388,7 @@ void multires_create(Object *ob, Mesh *me) &me->mr->fdata, CD_MTFACE); if(em) efa= em->faces.first; for(i=0; i<lvl->totface; ++i) { - multires_get_face(&lvl->faces[i], efa, &me->mface[i]); + multires_get_face(&lvl->faces[i], &me->mr->fdata, i, efa, &me->mface[i]); if(em) efa= efa->next; } @@ -765,7 +765,7 @@ static void multires_update_faces(Mesh *me, EditMesh *em) if(em) efa= em->faces.first; for(i=0; i<cr_lvl->totface; ++i) { MultiresFace mftmp; - multires_get_face(&mftmp, efa, &me->mface[i]); + multires_get_face(&mftmp, &me->mr->fdata, i, efa, &me->mface[i]); if(cr_lvl->faces[i].flag != mftmp.flag) cr_flag_damaged[i]= 1; if(cr_lvl->faces[i].mat_nr != mftmp.mat_nr) diff --git a/source/blender/blenkernel/intern/node.c b/source/blender/blenkernel/intern/node.c index 4e36df35a14..94a21cdd5df 100644 --- a/source/blender/blenkernel/intern/node.c +++ b/source/blender/blenkernel/intern/node.c @@ -68,6 +68,9 @@ #include "SHD_node.h" +#include "GPU_extensions.h" +#include "GPU_material.h" + static ListBase empty_list = {NULL, NULL}; ListBase node_all_composit = {NULL, NULL}; ListBase node_all_shaders = {NULL, NULL}; @@ -2352,6 +2355,117 @@ void ntreeCompositExecTree(bNodeTree *ntree, RenderData *rd, int do_preview) ntreeEndExecTree(ntree); } +/* GPU material from shader nodes */ + +static void gpu_from_node_stack(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs) +{ + bNodeSocket *sock; + int i; + + for (sock=sockets->first, i=0; sock; sock=sock->next, i++) { + memset(&gs[i], 0, sizeof(gs[i])); + + QUATCOPY(gs[i].vec, ns[i]->vec); + gs[i].link= ns[i]->data; + + if (sock->type == SOCK_VALUE) + gs[i].type= GPU_FLOAT; + else if (sock->type == SOCK_VECTOR) + gs[i].type= GPU_VEC3; + else if (sock->type == SOCK_RGBA) + gs[i].type= GPU_VEC4; + else + gs[i].type= GPU_NONE; + + gs[i].name = ""; + gs[i].hasinput= ns[i]->hasinput && ns[i]->data; + gs[i].hasoutput= ns[i]->hasinput && ns[i]->data; + gs[i].sockettype= ns[i]->sockettype; + } + + gs[i].type= GPU_NONE; +} + +static void data_from_gpu_stack(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs) +{ + bNodeSocket *sock; + int i; + + for (sock=sockets->first, i=0; sock; sock=sock->next, i++) { + ns[i]->data= gs[i].link; + ns[i]->hasinput= gs[i].hasinput && gs[i].link; + ns[i]->hasoutput= gs[i].hasoutput; + ns[i]->sockettype= gs[i].sockettype; + } +} + +static void gpu_node_group_execute(bNodeStack *stack, GPUMaterial *mat, bNode *gnode, bNodeStack **in, bNodeStack **out) +{ + bNode *node; + bNodeTree *ntree= (bNodeTree *)gnode->id; + bNodeStack *nsin[MAX_SOCKET]; /* arbitrary... watch this */ + bNodeStack *nsout[MAX_SOCKET]; /* arbitrary... watch this */ + GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1]; + int doit = 0; + + if(ntree==NULL) return; + + stack+= gnode->stack_index; + + for(node= ntree->nodes.first; node; node= node->next) { + if(node->typeinfo->gpufunc) { + group_node_get_stack(node, stack, nsin, nsout, in, out); + + doit = 0; + + /* for groups, only execute outputs for edited group */ + if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) { + if(gnode->flag & NODE_GROUP_EDIT) + if(node->flag & NODE_DO_OUTPUT) + doit = 1; + } + else + doit = 1; + + if(doit) { + gpu_from_node_stack(&node->inputs, nsin, gpuin); + gpu_from_node_stack(&node->outputs, nsout, gpuout); + if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout)) + data_from_gpu_stack(&node->outputs, nsout, gpuout); + } + } + } +} + +void ntreeGPUMaterialNodes(bNodeTree *ntree, GPUMaterial *mat) +{ + bNode *node; + bNodeStack *stack; + bNodeStack *nsin[MAX_SOCKET]; /* arbitrary... watch this */ + bNodeStack *nsout[MAX_SOCKET]; /* arbitrary... watch this */ + GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1]; + + if((ntree->init & NTREE_EXEC_INIT)==0) + ntreeBeginExecTree(ntree); + + stack= ntree->stack; + + for(node= ntree->nodes.first; node; node= node->next) { + if(node->typeinfo->gpufunc) { + node_get_stack(node, stack, nsin, nsout); + gpu_from_node_stack(&node->inputs, nsin, gpuin); + gpu_from_node_stack(&node->outputs, nsout, gpuout); + if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout)) + data_from_gpu_stack(&node->outputs, nsout, gpuout); + } + else if(node->type==NODE_GROUP && node->id) { + node_get_stack(node, stack, nsin, nsout); + gpu_node_group_execute(stack, mat, node, nsin, nsout); + } + } + + ntreeEndExecTree(ntree); +} /* **************** call to switch lamploop for material node ************ */ diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c index 7b36e46d45e..b6f6820ca73 100644 --- a/source/blender/blenkernel/intern/object.c +++ b/source/blender/blenkernel/intern/object.c @@ -70,6 +70,7 @@ #include "BKE_armature.h" #include "BKE_action.h" +#include "BKE_bullet.h" #include "BKE_colortools.h" #include "BKE_deform.h" #include "BKE_DerivedMesh.h" @@ -112,6 +113,8 @@ #include "BPY_extern.h" +#include "GPU_material.h" + #include "blendef.h" /* Local function protos */ @@ -177,6 +180,14 @@ void object_free_softbody(Object *ob) } } +void object_free_bulletsoftbody(Object *ob) +{ + if(ob->bsoft) { + bsbFree(ob->bsoft); + ob->bsoft= NULL; + } +} + void object_free_modifiers(Object *ob) { while (ob->modifiers.first) { @@ -267,7 +278,8 @@ void free_object(Object *ob) MEM_freeN(ob->pd); } if(ob->soft) sbFree(ob->soft); - if(ob->fluidsimSettings) fluidsimSettingsFree(ob->fluidsimSettings); + if(ob->bsoft) bsbFree(ob->bsoft); + if(ob->gpulamp.first) GPU_lamp_free(ob); } static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Object **obpoin) @@ -694,9 +706,11 @@ float dof_camera(Object *ob) if (cam->dof_ob) { /* too simple, better to return the distance on the view axis only * return VecLenf(ob->obmat[3], cam->dof_ob->obmat[3]); */ + float mat[4][4], obmat[4][4]; - float mat[4][4]; - Mat4Invert(ob->imat, ob->obmat); + Mat4CpyMat4(obmat, ob->obmat); + Mat4Ortho(obmat); + Mat4Invert(ob->imat, obmat); Mat4MulMat4(mat, cam->dof_ob->obmat, ob->imat); return fabs(mat[3][2]); } @@ -743,6 +757,11 @@ void *add_lamp(char *name) la->atm_extinction_factor = 1.0; la->atm_distance_factor = 1.0; la->sun_intensity = 1.0; + la->skyblendtype= MA_RAMP_ADD; + la->skyblendfac= 1.0f; + la->sky_colorspace= BLI_CS_CIE; + la->sky_exposure= 1.0f; + curvemapping_initialize(la->curfalloff); return la; } @@ -918,7 +937,7 @@ Object *add_only_object(int type, char *name) QuatOne(ob->dquat); #endif - ob->col[0]= ob->col[1]= ob->col[2]= 0.0; + ob->col[0]= ob->col[1]= ob->col[2]= 1.0; ob->col[3]= 1.0; ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0; @@ -955,8 +974,9 @@ Object *add_only_object(int type, char *name) ob->anisotropicFriction[0] = 1.0f; ob->anisotropicFriction[1] = 1.0f; ob->anisotropicFriction[2] = 1.0f; - ob->gameflag= OB_PROP; - + ob->gameflag= OB_PROP|OB_COLLISION; + ob->margin = 0.0; + /* NT fluid sim defaults */ ob->fluidsimFlag = 0; ob->fluidsimSettings = NULL; @@ -1039,6 +1059,17 @@ SoftBody *copy_softbody(SoftBody *sb) return sbn; } +BulletSoftBody *copy_bulletsoftbody(BulletSoftBody *bsb) +{ + BulletSoftBody *bsbn; + + if (bsb == NULL) + return NULL; + bsbn = MEM_dupallocN(bsb); + /* no pointer in this structure yet */ + return bsbn; +} + ParticleSystem *copy_particlesystem(ParticleSystem *psys) { ParticleSystem *psysn; @@ -1174,7 +1205,9 @@ Object *copy_object(Object *ob) BPY_copy_scriptlink(&ob->scriptlink); + obn->prop.first = obn->prop.last = NULL; copy_properties(&obn->prop, &ob->prop); + copy_sensors(&obn->sensors, &ob->sensors); copy_controllers(&obn->controllers, &ob->controllers); copy_actuators(&obn->actuators, &ob->actuators); @@ -1207,15 +1240,7 @@ Object *copy_object(Object *ob) id_us_plus(&(obn->pd->tex->id)); } obn->soft= copy_softbody(ob->soft); - - /* NT copy fluid sim setting memory */ - if(obn->fluidsimSettings) { - obn->fluidsimSettings = fluidsimSettingsCopy(ob->fluidsimSettings); - /* copying might fail... */ - if(obn->fluidsimSettings) { - obn->fluidsimSettings->orgMesh = (Mesh *)obn->data; - } - } + obn->bsoft = copy_bulletsoftbody(ob->bsoft); copy_object_particlesystems(obn, ob); @@ -1226,6 +1251,7 @@ Object *copy_object(Object *ob) obn->vnode = NULL; #endif + obn->gpulamp.first = obn->gpulamp.last = NULL; return obn; } @@ -1454,22 +1480,42 @@ float bsystem_time(Object *ob, float cfra, float ofs) return cfra; } -void object_to_mat3(Object *ob, float mat[][3]) /* no parent */ +void object_scale_to_mat3(Object *ob, float mat[][3]) { - float smat[3][3], vec[3]; - float rmat[3][3]; - /*float q1[4];*/ - - /* size */ + float vec[3]; if(ob->ipo) { vec[0]= ob->size[0]+ob->dsize[0]; vec[1]= ob->size[1]+ob->dsize[1]; vec[2]= ob->size[2]+ob->dsize[2]; - SizeToMat3(vec, smat); + SizeToMat3(vec, mat); } else { - SizeToMat3(ob->size, smat); + SizeToMat3(ob->size, mat); } +} + +void object_rot_to_mat3(Object *ob, float mat[][3]) +{ + float vec[3]; + if(ob->ipo) { + vec[0]= ob->rot[0]+ob->drot[0]; + vec[1]= ob->rot[1]+ob->drot[1]; + vec[2]= ob->rot[2]+ob->drot[2]; + EulToMat3(vec, mat); + } + else { + EulToMat3(ob->rot, mat); + } +} + +void object_to_mat3(Object *ob, float mat[][3]) /* no parent */ +{ + float smat[3][3]; + float rmat[3][3]; + /*float q1[4];*/ + + /* size */ + object_scale_to_mat3(ob, smat); /* rot */ /* Quats arnt used yet */ @@ -1483,15 +1529,7 @@ void object_to_mat3(Object *ob, float mat[][3]) /* no parent */ } } else {*/ - if(ob->ipo) { - vec[0]= ob->rot[0]+ob->drot[0]; - vec[1]= ob->rot[1]+ob->drot[1]; - vec[2]= ob->rot[2]+ob->drot[2]; - EulToMat3(vec, rmat); - } - else { - EulToMat3(ob->rot, rmat); - } + object_rot_to_mat3(ob, rmat); /*}*/ Mat3MulMat3(mat, rmat, smat); } diff --git a/source/blender/blenkernel/intern/particle.c b/source/blender/blenkernel/intern/particle.c index 643f90637ad..44ee5c236fa 100644 --- a/source/blender/blenkernel/intern/particle.c +++ b/source/blender/blenkernel/intern/particle.c @@ -46,6 +46,7 @@ #include "DNA_object_types.h" #include "DNA_curve_types.h" #include "DNA_key_types.h" +#include "DNA_ipo_types.h" #include "BLI_arithb.h" #include "BLI_blenlib.h" @@ -295,7 +296,7 @@ int psys_check_enabled(Object *ob, ParticleSystem *psys) ParticleSystemModifierData *psmd; Mesh *me; - if(psys->flag & PSYS_DISABLED) + if(psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE) return 0; if(ob->type == OB_MESH) { @@ -320,8 +321,14 @@ int psys_check_enabled(Object *ob, ParticleSystem *psys) /************************************************/ void psys_free_settings(ParticleSettings *part) { - if(part->pd) + if(part->pd) { MEM_freeN(part->pd); + part->pd = NULL; + } + if(part->pd2) { + MEM_freeN(part->pd2); + part->pd2 = NULL; + } } void free_hair(ParticleSystem *psys, int softbody) @@ -373,8 +380,11 @@ void psys_free_children(ParticleSystem *psys) } /* free everything */ void psys_free(Object *ob, ParticleSystem * psys) -{ +{ if(psys){ + int nr = 0; + ParticleSystem * tpsys; + if(ob->particlesystem.first == NULL && G.f & G_PARTICLEEDIT) G.f &= ~G_PARTICLEEDIT; @@ -400,6 +410,21 @@ void psys_free(Object *ob, ParticleSystem * psys) if(psys->effectors.first) psys_end_effectors(psys); + + // check if we are last non-visible particle system + for(tpsys=ob->particlesystem.first; tpsys; tpsys=tpsys->next){ + if(tpsys->part) + { + if(ELEM(tpsys->part->draw_as,PART_DRAW_OB,PART_DRAW_GR)) + { + nr++; + break; + } + } + } + // clear do-not-draw-flag + if(!nr) + ob->transflag &= ~OB_DUPLIPARTS; if(psys->part){ psys->part->id.us--; @@ -411,7 +436,7 @@ void psys_free(Object *ob, ParticleSystem * psys) if(psys->pointcache) BKE_ptcache_free(psys->pointcache); - + MEM_freeN(psys); } } @@ -1092,8 +1117,8 @@ static void psys_origspace_to_w(OrigSpaceFace *osface, int quad, float *w, float } } -/* find the derived mesh face for a particle, set the mf passed. -This is slow, can be optimized but only for many lookups, return the face lookup index*/ +/* find the derived mesh face for a particle, set the mf passed. this is slow + * and can be optimized but only for many lookups. returns the face index. */ int psys_particle_dm_face_lookup(Object *ob, DerivedMesh *dm, int index, float *fw, struct LinkNode *node) { Mesh *me= (Mesh*)ob->data; @@ -1161,168 +1186,142 @@ int psys_particle_dm_face_lookup(Object *ob, DerivedMesh *dm, int index, float * return DMCACHE_NOTFOUND; } -/* interprets particle data to get a point on a mesh in object space */ -#define PARTICLE_ON_DM_ERROR \ - { if(vec) { vec[0]=vec[1]=vec[2]=0.0; } \ - if(nor) { nor[0]=nor[1]=0.0; nor[2]=1.0; } \ - if(orco) { orco[0]=orco[1]=orco[2]=0.0; } \ - if(ornor) { ornor[0]=ornor[1]=0.0; ornor[2]=1.0; } \ - if(utan) { utan[0]=utan[1]=utan[2]=0.0; } \ - if(vtan) { vtan[0]=vtan[1]=vtan[2]=0.0; } } - -void psys_particle_on_dm(Object *ob, DerivedMesh *dm, int from, int index, int index_dmcache, float *fw, float foffset, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor) +static int psys_map_index_on_dm(DerivedMesh *dm, int from, int index, int index_dmcache, float *fw, float foffset, int *mapindex, float *mapfw) { - float temp1[3]; - float (*orcodata)[3]; - - if(index < 0) { /* 'no dm' error has happened! */ - PARTICLE_ON_DM_ERROR; - return; - } - orcodata= dm->getVertDataArray(dm, CD_ORCO); + if(index < 0) + return 0; if (dm->deformedOnly || index_dmcache == DMCACHE_ISCHILD) { - /* this works for meshes with deform verts only - constructive modifiers wont work properly*/ + /* for meshes that are either only defined or for child particles, the + * index and fw do not require any mapping, so we can directly use it */ if(from == PART_FROM_VERT) { - if(index >= dm->getNumVerts(dm)) { - PARTICLE_ON_DM_ERROR; - return; - } - - dm->getVertCo(dm,index,vec); - if(nor){ - dm->getVertNo(dm,index,nor); - Normalize(nor); - } - if(orco) - VECCOPY(orco, orcodata[index]) - if(ornor) { - dm->getVertNo(dm,index,nor); - Normalize(nor); - } - } - else { /* PART_FROM_FACE / PART_FROM_VOLUME */ - MFace *mface; - MTFace *mtface=0; - MVert *mvert; - int uv_index; - - if(index >= dm->getNumFaces(dm)) { - PARTICLE_ON_DM_ERROR; - return; - } - - mface=dm->getFaceData(dm,index,CD_MFACE); - mvert=dm->getVertDataArray(dm,CD_MVERT); - uv_index=CustomData_get_active_layer_index(&dm->faceData,CD_MTFACE); + if(index >= dm->getNumVerts(dm)) + return 0; - if(uv_index>=0){ - CustomDataLayer *layer=&dm->faceData.layers[uv_index]; - mtface= &((MTFace*)layer->data)[index]; - } + *mapindex = index; + } + else { /* FROM_FACE/FROM_VOLUME */ + if(index >= dm->getNumFaces(dm)) + return 0; - if(from==PART_FROM_VOLUME){ - psys_interpolate_face(mvert,mface,mtface,orcodata,fw,vec,temp1,utan,vtan,orco,ornor); - if(nor) - VECCOPY(nor,temp1); - Normalize(temp1); - VecMulf(temp1,-foffset); - VECADD(vec,vec,temp1); - } - else - psys_interpolate_face(mvert,mface,mtface,orcodata,fw,vec,nor,utan,vtan,orco,ornor); + *mapindex = index; + QUATCOPY(mapfw, fw); } } else { - /* Need to support constructive modifiers, this is a bit more tricky - we need a customdata layer like UV's so we can position the particle */ - - /* Only face supported at the moment */ - if(ELEM(from, PART_FROM_FACE, PART_FROM_VOLUME)) { + /* for other meshes that have been modified, we try to map the particle + * to their new location, which means a different index, and for faces + * also a new face interpolation weights */ + if(from == PART_FROM_VERT) { + if (index_dmcache == DMCACHE_NOTFOUND || index_dmcache > dm->getNumVerts(dm)) + return 0; + + *mapindex = index_dmcache; + } + else { /* FROM_FACE/FROM_VOLUME */ /* find a face on the derived mesh that uses this face */ - Mesh *me= (Mesh*)ob->data; - MVert *mvert; MFace *mface; - MTFace *mtface; OrigSpaceFace *osface; - int *origindex; - float fw_mod[4]; - int i, totface; - - mvert= dm->getVertDataArray(dm,CD_MVERT); + int i; + + i = index_dmcache; + + if(i== DMCACHE_NOTFOUND || i >= dm->getNumFaces(dm)) + return 0; + + *mapindex = i; + /* modify the original weights to become + * weights for the derived mesh face */ osface= dm->getFaceDataArray(dm, CD_ORIGSPACE); - origindex= dm->getFaceDataArray(dm, CD_ORIGINDEX); + mface= dm->getFaceData(dm, i, CD_MFACE); - /* For this to work we need origindex and OrigSpace coords */ - if(origindex==NULL || osface==NULL || index>=me->totface) { - PARTICLE_ON_DM_ERROR; - return; - } - - if (index_dmcache == DMCACHE_NOTFOUND) - i = psys_particle_dm_face_lookup(ob, dm, index, fw, (LinkNode*)NULL); + if(osface == NULL) + mapfw[0]= mapfw[1]= mapfw[2]= mapfw[3]= 0.0f; else - i = index_dmcache; + psys_origspace_to_w(&osface[i], mface->v4, fw, mapfw); + } + } - totface = dm->getNumFaces(dm); + return 1; +} - /* Any time this happens, and the face has not been removed, - * its a BUG watch out for this error! */ - if (i==-1) { - printf("Cannot find original face %i\n", index); - PARTICLE_ON_DM_ERROR; - return; - } - else if(i >= totface) - return; +/* interprets particle data to get a point on a mesh in object space */ +void psys_particle_on_dm(DerivedMesh *dm, int from, int index, int index_dmcache, float *fw, float foffset, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor) +{ + float tmpnor[3], mapfw[4]; + float (*orcodata)[3]; + int mapindex; - mface= dm->getFaceData(dm, i, CD_MFACE); - mtface= dm->getFaceData(dm, i, CD_MTFACE); - osface += i; - - /* we need to modify the original weights to become weights for - * the derived mesh face */ - psys_origspace_to_w(osface, mface->v4, fw, fw_mod); - - if(from==PART_FROM_VOLUME){ - psys_interpolate_face(mvert,mface,mtface,orcodata,fw_mod,vec,temp1,utan,vtan,orco,ornor); - if(nor) - VECCOPY(nor,temp1); - Normalize(temp1); - VecMulf(temp1,-foffset); - VECADD(vec,vec,temp1); - } - else - psys_interpolate_face(mvert,mface,mtface,orcodata,fw_mod,vec,nor,utan,vtan,orco,ornor); + if(!psys_map_index_on_dm(dm, from, index, index_dmcache, fw, foffset, &mapindex, mapfw)) { + if(vec) { vec[0]=vec[1]=vec[2]=0.0; } + if(nor) { nor[0]=nor[1]=0.0; nor[2]=1.0; } + if(orco) { orco[0]=orco[1]=orco[2]=0.0; } + if(ornor) { ornor[0]=ornor[1]=0.0; ornor[2]=1.0; } + if(utan) { utan[0]=utan[1]=utan[2]=0.0; } + if(vtan) { vtan[0]=vtan[1]=vtan[2]=0.0; } + + return; + } + + orcodata= dm->getVertDataArray(dm, CD_ORCO); + + if(from == PART_FROM_VERT) { + dm->getVertCo(dm,mapindex,vec); + + if(nor) { + dm->getVertNo(dm,mapindex,nor); + Normalize(nor); } - else if(from == PART_FROM_VERT) { - if (index_dmcache == DMCACHE_NOTFOUND || index_dmcache > dm->getNumVerts(dm)) { - PARTICLE_ON_DM_ERROR; - return; - } - dm->getVertCo(dm,index_dmcache,vec); - if(nor) { - dm->getVertNo(dm,index_dmcache,nor); - Normalize(nor); - } - if(orco) - VECCOPY(orco, orcodata[index]) - if(ornor) { - dm->getVertNo(dm,index_dmcache,nor); - Normalize(nor); - } - if(utan && vtan) { - utan[0]= utan[1]= utan[2]= 0.0f; - vtan[0]= vtan[1]= vtan[2]= 0.0f; - } + if(orco) + VECCOPY(orco, orcodata[mapindex]) + + if(ornor) { + dm->getVertNo(dm,mapindex,nor); + Normalize(nor); } - else { - PARTICLE_ON_DM_ERROR; + + if(utan && vtan) { + utan[0]= utan[1]= utan[2]= 0.0f; + vtan[0]= vtan[1]= vtan[2]= 0.0f; } } + else { /* PART_FROM_FACE / PART_FROM_VOLUME */ + MFace *mface; + MTFace *mtface; + MVert *mvert; + + mface=dm->getFaceData(dm,mapindex,CD_MFACE); + mvert=dm->getVertDataArray(dm,CD_MVERT); + mtface=CustomData_get_layer(&dm->faceData,CD_MTFACE); + + if(mtface) + mtface += mapindex; + + if(from==PART_FROM_VOLUME) { + psys_interpolate_face(mvert,mface,mtface,orcodata,mapfw,vec,tmpnor,utan,vtan,orco,ornor); + if(nor) + VECCOPY(nor,tmpnor); + + Normalize(tmpnor); + VecMulf(tmpnor,-foffset); + VECADD(vec,vec,tmpnor); + } + else + psys_interpolate_face(mvert,mface,mtface,orcodata,mapfw,vec,nor,utan,vtan,orco,ornor); + } +} + +float psys_particle_value_from_verts(DerivedMesh *dm, short from, ParticleData *pa, float *values) +{ + float mapfw[4]; + int mapindex; + + if(!psys_map_index_on_dm(dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, &mapindex, mapfw)) + return 0.0f; + + return psys_interpolate_value_from_verts(dm, from, mapindex, mapfw, values); } -#undef PARTICLE_ON_DM_ERROR ParticleSystemModifierData *psys_get_modifier(Object *ob, ParticleSystem *psys) { @@ -1343,7 +1342,7 @@ ParticleSystemModifierData *psys_get_modifier(Object *ob, ParticleSystem *psys) /* Particles on a shape */ /************************************************/ /* ready for future use */ -void psys_particle_on_shape(int distr, int index, float *fuv, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor) +static void psys_particle_on_shape(int distr, int index, float *fuv, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor) { /* TODO */ float zerovec[3]={0.0f,0.0f,0.0f}; @@ -1369,7 +1368,7 @@ void psys_particle_on_shape(int distr, int index, float *fuv, float *vec, float /************************************************/ /* Particles on emitter */ /************************************************/ -void psys_particle_on_emitter(Object *ob, ParticleSystemModifierData *psmd, int from, int index, int index_dmcache, float *fuv, float foffset, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor){ +void psys_particle_on_emitter(ParticleSystemModifierData *psmd, int from, int index, int index_dmcache, float *fuv, float foffset, float *vec, float *nor, float *utan, float *vtan, float *orco, float *ornor){ if(psmd){ if(psmd->psys->part->distr==PART_DISTR_GRID && psmd->psys->part->from != PART_FROM_VERT){ if(vec){ @@ -1378,7 +1377,7 @@ void psys_particle_on_emitter(Object *ob, ParticleSystemModifierData *psmd, int return; } /* we cant use the num_dmcache */ - psys_particle_on_dm(ob, psmd->dm,from,index,index_dmcache,fuv,foffset,vec,nor,utan,vtan,orco,ornor); + psys_particle_on_dm(psmd->dm,from,index,index_dmcache,fuv,foffset,vec,nor,utan,vtan,orco,ornor); } else psys_particle_on_shape(from,index,fuv,vec,nor,utan,vtan,orco,ornor); @@ -1649,7 +1648,6 @@ int do_guide(ParticleKey *state, int pa_num, float time, ListBase *lb) /* TODO */ //else{ ///* curve size*/ - // calc_curve_subdiv_radius(cu,cu->nurb.first,((Nurb*)cu->nurb.first)-> //} par.co[0]=par.co[1]=par.co[2]=0.0f; VECCOPY(key.co,pa_loc); @@ -1750,37 +1748,17 @@ static void do_path_effectors(Object *ob, ParticleSystem *psys, int i, ParticleC static int check_path_length(int k, ParticleCacheKey *keys, ParticleCacheKey *state, float max_length, float *cur_length, float length, float *dvec) { if(*cur_length + length > max_length){ - //if(p<totparent){ - // if(k<=(int)cache[totpart+p]->time){ - // /* parents need to be calculated fully first so that they don't mess up their children */ - // /* we'll make a note of where we got to though so that they're easy to finish later */ - // state->time=(max_length-*cur_length)/length; - // cache[totpart+p]->time=(float)k; - // } - //} - //else{ VecMulf(dvec, (max_length - *cur_length) / length); VECADD(state->co, (state - 1)->co, dvec); keys->steps = k; /* something over the maximum step value */ return k=100000; - //} } else { *cur_length+=length; return k; } } -static void finalize_path_length(ParticleCacheKey *keys) -{ - ParticleCacheKey *state = keys; - float dvec[3]; - state += state->steps; - - VECSUB(dvec, state->co, (state - 1)->co); - VecMulf(dvec, state->steps); - VECADD(state->co, (state - 1)->co, dvec); -} static void offset_child(ChildParticle *cpa, ParticleKey *par, ParticleKey *child, float flat, float radius) { VECCOPY(child->co,cpa->fuv); @@ -1830,14 +1808,14 @@ void psys_find_parents(Object *ob, ParticleSystemModifierData *psmd, ParticleSys tree=BLI_kdtree_new(totparent); for(p=0,cpa=psys->child; p<totparent; p++,cpa++){ - psys_particle_on_emitter(ob,psmd,from,cpa->num,-1,cpa->fuv,cpa->foffset,co,0,0,0,orco,0); + psys_particle_on_emitter(psmd,from,cpa->num,-1,cpa->fuv,cpa->foffset,co,0,0,0,orco,0); BLI_kdtree_insert(tree, p, orco, NULL); } BLI_kdtree_balance(tree); for(; p<totchild; p++,cpa++){ - psys_particle_on_emitter(ob,psmd,from,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co,0,0,0,orco,0); + psys_particle_on_emitter(psmd,from,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co,0,0,0,orco,0); cpa->parent=BLI_kdtree_find_nearest(tree, orco, NULL, NULL); } @@ -1961,7 +1939,7 @@ void psys_thread_create_path(ParticleThread *thread, struct ChildParticle *cpa, ParticleCacheKey **cache= psys->childcache; ParticleCacheKey **pcache= psys->pathcache; ParticleCacheKey *state, *par = NULL, *key[4]; - ParticleData *pa; + ParticleData *pa=NULL; ParticleTexture ptex; float *cpa_fuv=0; float co[3], orco[3], ornor[3], t, rough_t, cpa_1st[3], dvec[3]; @@ -2029,7 +2007,7 @@ void psys_thread_create_path(ParticleThread *thread, struct ChildParticle *cpa, cpa_fuv = cpa->fuv; cpa_from = PART_FROM_FACE; - psys_particle_on_emitter(ob,ctx->psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa->fuv,foffset,co,ornor,0,0,orco,0); + psys_particle_on_emitter(ctx->psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa->fuv,foffset,co,ornor,0,0,orco,0); /* we need to save the actual root position of the child for positioning it accurately to the surface of the emitter */ VECCOPY(cpa_1st,co); @@ -2055,7 +2033,7 @@ void psys_thread_create_path(ParticleThread *thread, struct ChildParticle *cpa, cpa_num=pa->num; cpa_fuv=pa->fuv; - psys_particle_on_emitter(ob,ctx->psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa_fuv,pa->foffset,co,ornor,0,0,orco,0); + psys_particle_on_emitter(ctx->psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa_fuv,pa->foffset,co,ornor,0,0,orco,0); } keys->steps = ctx->steps; @@ -2262,13 +2240,9 @@ void psys_thread_create_path(ParticleThread *thread, struct ChildParticle *cpa, get_strand_normal(ctx->ma, ornor, cur_length, state->vel); } } - - /* now let's finalise the interpolated parents that we might have left half done before */ - if(i<ctx->totparent) - finalize_path_length(keys); } -void *exec_child_path_cache(void *data) +static void *exec_child_path_cache(void *data) { ParticleThread *thread= (ParticleThread*)data; ParticleThreadContext *ctx= thread->ctx; @@ -2339,6 +2313,7 @@ void psys_cache_paths(Object *ob, ParticleSystem *psys, float cfra, int editupda ParticleCacheKey *ca, **cache=psys->pathcache; ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys); ParticleEditSettings *pset = &G.scene->toolsettings->particle; + ParticleSettings *part = psys->part; ParticleData *pa; ParticleKey keys[4], result, *kkey[2] = {NULL, NULL}; @@ -2364,6 +2339,8 @@ void psys_cache_paths(Object *ob, ParticleSystem *psys, float cfra, int editupda float nosel_col[3]; float length, vec[3]; float *vg_effector= NULL, effector=0.0f; + float *vg_length= NULL, pa_length=1.0f, max_length=1.0f, cur_length=0.0f; + float len, dvec[3]; /* we don't have anything valid to create paths from so let's quit here */ if((psys->flag & PSYS_HAIR_DONE)==0 && (psys->flag & PSYS_KEYED)==0) @@ -2415,6 +2392,9 @@ void psys_cache_paths(Object *ob, ParticleSystem *psys, float cfra, int editupda if(psys->part->from!=PART_FROM_PARTICLE) { if(!(psys->part->flag & PART_CHILD_EFFECT)) vg_effector = psys_cache_vgroup(psmd->dm, psys, PSYS_VG_EFFECTOR); + + if(!edit && !psys->totchild) + vg_length = psys_cache_vgroup(psmd->dm, psys, PSYS_VG_LENGTH); } /*---first main loop: create all actual particles' paths---*/ @@ -2428,6 +2408,12 @@ void psys_cache_paths(Object *ob, ParticleSystem *psys, float cfra, int editupda if(editupdate && !(pa->flag & PARS_EDIT_RECALC)) continue; else memset(cache[i], 0, sizeof(*cache[i])*(steps+1)); + if(!edit && !psys->totchild) { + pa_length = part->length * (1.0f - part->randlength*pa->r_ave[0]); + if(vg_length) + pa_length *= psys_particle_value_from_verts(psmd->dm,part->from,pa,vg_length); + } + cache[i]->steps = steps; if(edit) @@ -2586,7 +2572,7 @@ void psys_cache_paths(Object *ob, ParticleSystem *psys, float cfra, int editupda effector= 1.0f; if(vg_effector) - effector*= psys_interpolate_value_from_verts(psmd->dm,psys->part->from,pa->num,pa->fuv,vg_effector); + effector*= psys_particle_value_from_verts(psmd->dm,psys->part->from,pa,vg_effector); for(k=0, ca=cache[i]; k<=steps; k++, ca++) { /* apply effectors */ @@ -2653,6 +2639,27 @@ void psys_cache_paths(Object *ob, ParticleSystem *psys, float cfra, int editupda } + if(!edit && !psys->totchild) { + /* check if path needs to be cut before actual end of data points */ + if(k){ + VECSUB(dvec,ca->co,(ca-1)->co); + if(part->flag&PART_ABS_LENGTH) + len=VecLength(dvec); + else + len=1.0f/(float)steps; + + k=check_path_length(k,cache[i],ca,max_length,&cur_length,len,dvec); + } + else{ + /* initialize length calculation */ + if(part->flag&PART_ABS_LENGTH) + max_length= part->abslength*pa_length; + else + max_length= pa_length; + + cur_length= 0.0f; + } + } } } @@ -2665,6 +2672,9 @@ void psys_cache_paths(Object *ob, ParticleSystem *psys, float cfra, int editupda if(vg_effector) MEM_freeN(vg_effector); + + if(vg_length) + MEM_freeN(vg_length); } /************************************************/ /* Particle Key handling */ @@ -2803,7 +2813,7 @@ void psys_mat_hair_to_object(Object *ob, DerivedMesh *dm, short from, ParticleDa float vec[3]; psys_face_mat(0, dm, pa, hairmat, 0); - psys_particle_on_dm(ob, dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, vec, 0, 0, 0, 0, 0); + psys_particle_on_dm(dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, vec, 0, 0, 0, 0, 0); VECCOPY(hairmat[3],vec); } @@ -2812,62 +2822,14 @@ void psys_mat_hair_to_orco(Object *ob, DerivedMesh *dm, short from, ParticleData float vec[3], orco[3]; psys_face_mat(ob, dm, pa, hairmat, 1); - psys_particle_on_dm(ob, dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, vec, 0, 0, 0, orco, 0); + psys_particle_on_dm(dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, vec, 0, 0, 0, orco, 0); /* see psys_face_mat for why this function is called */ transform_mesh_orco_verts(ob->data, &orco, 1, 1); VECCOPY(hairmat[3],orco); } -/* -void psys_key_to_geometry(DerivedMesh *dm, ParticleData *pa, ParticleKey *key) -{ - float q[4], v1[3], v2[3], v3[3]; - - dm->getVertCo(dm,pa->verts[0],v1); - dm->getVertCo(dm,pa->verts[1],v2); - dm->getVertCo(dm,pa->verts[2],v3); - - triatoquat(v1, v2, v3, q); - - QuatInv(q); - - VECSUB(key->co,key->co,v1); - - VECADD(key->vel,key->vel,key->co); - - QuatMulVecf(q, key->co); - QuatMulVecf(q, key->vel); - - VECSUB(key->vel,key->vel,key->co); - - QuatMul(key->rot,q,key->rot); -} - -void psys_key_from_geometry(DerivedMesh *dm, ParticleData *pa, ParticleKey *key) -{ - float q[4], v1[3], v2[3], v3[3]; - - dm->getVertCo(dm,pa->verts[0],v1); - dm->getVertCo(dm,pa->verts[1],v2); - dm->getVertCo(dm,pa->verts[2],v3); - - triatoquat(v1, v2, v3, q); - - VECADD(key->vel,key->vel,key->co); - - QuatMulVecf(q, key->co); - QuatMulVecf(q, key->vel); - - VECSUB(key->vel,key->vel,key->co); - - VECADD(key->co,key->co,v1); - - QuatMul(key->rot,q,key->rot); -} -*/ - -void psys_vec_rot_to_face(DerivedMesh *dm, ParticleData *pa, float *vec)//to_geometry(DerivedMesh *dm, ParticleData *pa, float *vec) +void psys_vec_rot_to_face(DerivedMesh *dm, ParticleData *pa, float *vec) { float mat[4][4]; @@ -2876,36 +2838,6 @@ void psys_vec_rot_to_face(DerivedMesh *dm, ParticleData *pa, float *vec)//to_geo Mat4Mul3Vecfl(mat, vec); } -/* unused */ -#if 0 -static void psys_vec_rot_from_face(DerivedMesh *dm, ParticleData *pa, float *vec)//from_geometry(DerivedMesh *dm, ParticleData *pa, float *vec) -{ - float q[4], v1[3], v2[3], v3[3]; - /* - dm->getVertCo(dm,pa->verts[0],v1); - dm->getVertCo(dm,pa->verts[1],v2); - dm->getVertCo(dm,pa->verts[2],v3); - */ - /* replace with this */ - MFace *mface; - int i; // = psys_particle_dm_face_lookup(dm, pa->num, pa->fuv, pa->foffset, (LinkNode*)NULL); - i = pa->num_dmcache==DMCACHE_NOTFOUND ? pa->num : pa->num_dmcache; - if (i==-1 || i >= dm->getNumFaces(dm)) { vec[0] = vec[1] = 0; vec[2] = 1; return; } - mface=dm->getFaceData(dm,i,CD_MFACE); - - dm->getVertCo(dm,mface->v1,v1); - dm->getVertCo(dm,mface->v2,v2); - dm->getVertCo(dm,mface->v3,v3); - /* done */ - - triatoquat(v1, v2, v3, q); - - QuatMulVecf(q, vec); - - //VECADD(vec,vec,v1); -} -#endif - void psys_mat_hair_to_global(Object *ob, DerivedMesh *dm, short from, ParticleData *pa, float hairmat[][4]) { float facemat[4][4]; @@ -3015,6 +2947,7 @@ ParticleSettings *psys_copy_settings(ParticleSettings *part) partn= copy_libblock(part); if(partn->pd) partn->pd= MEM_dupallocN(part->pd); + if(partn->pd2) partn->pd2= MEM_dupallocN(part->pd2); return partn; } @@ -3154,6 +3087,47 @@ LinkNode *psys_using_settings(ParticleSettings *part, int flush_update) /************************************************/ /* Textures */ /************************************************/ + +static int get_particle_uv(DerivedMesh *dm, ParticleData *pa, int face_index, float *fuv, char *name, float *texco) +{ + MFace *mf; + MTFace *tf; + int i; + + tf= CustomData_get_layer_named(&dm->faceData, CD_MTFACE, name); + + if(tf == NULL) + tf= CustomData_get_layer(&dm->faceData, CD_MTFACE); + + if(tf == NULL) + return 0; + + if(pa) { + i= (pa->num_dmcache==DMCACHE_NOTFOUND)? pa->num: pa->num_dmcache; + if(i >= dm->getNumFaces(dm)) + i = -1; + } + else + i= face_index; + + if (i==-1) { + texco[0]= 0.0f; + texco[1]= 0.0f; + texco[2]= 0.0f; + } + else { + mf= dm->getFaceData(dm, i, CD_MFACE); + + psys_interpolate_uvs(&tf[i], mf->v4, fuv, texco); + + texco[0]= texco[0]*2.0f - 1.0f; + texco[1]= texco[1]*2.0f - 1.0f; + texco[2]= 0.0f; + } + + return 1; +} + static void get_cpa_texture(DerivedMesh *dm, Material *ma, int face_index, float *fw, float *orco, ParticleTexture *ptex, int event) { MTex *mtex; @@ -3168,27 +3142,13 @@ static void get_cpa_texture(DerivedMesh *dm, Material *ma, int face_index, float short blend=mtex->blendtype; short neg=mtex->pmaptoneg; - if(mtex->texco & TEXCO_UV && fw){ - int uv_index=CustomData_get_named_layer_index(&dm->faceData,CD_MTFACE,mtex->uvname); - if(uv_index<0){ - uv_index=CustomData_get_active_layer_index(&dm->faceData,CD_MTFACE); - } - if(uv_index>=0){ - CustomDataLayer *layer=&dm->faceData.layers[uv_index]; - MTFace *mtface= &((MTFace*)layer->data)[face_index]; - MFace *mf=dm->getFaceData(dm,face_index,CD_MFACE); - psys_interpolate_uvs(mtface,mf->v4,fw,texco); - texco[0]*=2.0; - texco[1]*=2.0; - texco[0]-=1.0; - texco[1]-=1.0; - } - else + if((mtex->texco & TEXCO_UV) && fw) { + if(!get_particle_uv(dm, NULL, face_index, fw, mtex->uvname, texco)) VECCOPY(texco,orco); } - else{ + else VECCOPY(texco,orco); - } + externtex(mtex, texco, &value, rgba, rgba+1, rgba+2, rgba+3); if((event & mtex->pmapto) & MAP_PA_TIME){ if((setvars&MAP_PA_TIME)==0){ @@ -3228,33 +3188,16 @@ void psys_get_texture(Object *ob, Material *ma, ParticleSystemModifierData *psmd short blend=mtex->blendtype; short neg=mtex->pmaptoneg; - if(mtex->texco & TEXCO_UV){ - int uv_index=CustomData_get_named_layer_index(&psmd->dm->faceData,CD_MTFACE,mtex->uvname); - if(uv_index<0){ - uv_index=CustomData_get_active_layer_index(&psmd->dm->faceData,CD_MTFACE); - } - if(uv_index>=0){ - CustomDataLayer *layer=&psmd->dm->faceData.layers[uv_index]; - MTFace *mtface= &((MTFace*)layer->data)[pa->num]; - MFace *mf=psmd->dm->getFaceData(psmd->dm,pa->num,CD_MFACE); - psys_interpolate_uvs(mtface,mf->v4,pa->fuv,texco); - texco[0]*=2.0; - texco[1]*=2.0; - texco[0]-=1.0; - texco[1]-=1.0; + if((mtex->texco & TEXCO_UV) && ELEM(psys->part->from, PART_FROM_FACE, PART_FROM_VOLUME)) { + if(!get_particle_uv(psmd->dm, pa, 0, pa->fuv, mtex->uvname, texco)) { + /* failed to get uv's, let's try orco's */ + psys_particle_on_emitter(psmd,psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,0,0,0,texco, 0); } - else - //psys_particle_on_emitter(ob,psmd,psys->part->from,pa->num,pa->fuv,pa->foffset,texco,0,0,0); - /* <jahka> anyways I think it will be too small a difference to notice, so psys_get_texture should only know about the original mesh structure.. no dm needed anywhere */ - /* <brecht> the code only does dm based lookup now, so passing num_dmcache anyway to avoid^ - * massive slowdown here */ - psys_particle_on_emitter(ob,psmd,psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,0,0,0,texco, 0); } - else{ - //psys_particle_on_emitter(ob,psmd,psys->part->from,pa->num,pa->fuv,pa->offset,texco,0,0,0); - /* ditto above */ - psys_particle_on_emitter(ob,psmd,psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,0,0,0,texco, 0); + else { + psys_particle_on_emitter(psmd,psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,0,0,0,texco, 0); } + externtex(mtex, texco, &value, rgba, rgba+1, rgba+2, rgba+3); if((event & mtex->pmapto) & MAP_PA_TIME){ @@ -3319,7 +3262,7 @@ float psys_get_size(Object *ob, Material *ma, ParticleSystemModifierData *psmd, } if(vg_size) - size*=psys_interpolate_value_from_verts(psmd->dm,part->from,pa->num,pa->fuv,vg_size); + size*=psys_particle_value_from_verts(psmd->dm,part->from,pa,vg_size); if(part->randsize!=0.0) size*= 1.0f - part->randsize*pa->sizemul; @@ -3352,16 +3295,24 @@ float psys_get_child_size(ParticleSystem *psys, ChildParticle *cpa, float cfra, float size, time; if(part->childtype==PART_CHILD_FACES){ - if(pa_time) - time=*pa_time; - else - time=psys_get_child_time(psys,cpa,cfra); + size=part->size; if((part->flag&PART_ABS_TIME)==0 && part->ipo){ + IpoCurve *icu; + + if(pa_time) + time=*pa_time; + else + time=psys_get_child_time(psys,cpa,cfra); + + /* correction for lifetime */ calc_ipo(part->ipo, 100*time); - execute_ipo((ID *)part, part->ipo); + + for(icu = part->ipo->curve.first; icu; icu=icu->next) { + if(icu->adrcode == PART_SIZE) + size = icu->curval; + } } - size=part->size; } else size=psys->particles[cpa->parent].size; @@ -3558,7 +3509,7 @@ void psys_get_particle_on_path(Object *ob, ParticleSystem *psys, int p, Particle cpa_fuv = cpa->fuv; cpa_from = PART_FROM_FACE; - psys_particle_on_emitter(ob,psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa->fuv,foffset,co,0,0,0,orco,0); + psys_particle_on_emitter(psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa->fuv,foffset,co,0,0,0,orco,0); /* we need to save the actual root position of the child for positioning it accurately to the surface of the emitter */ //VECCOPY(cpa_1st,co); @@ -3580,7 +3531,7 @@ void psys_get_particle_on_path(Object *ob, ParticleSystem *psys, int p, Particle cpa_num=pa->num; cpa_fuv=pa->fuv; - psys_particle_on_emitter(ob,psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa_fuv,pa->foffset,co,0,0,0,orco,0); + psys_particle_on_emitter(psmd,cpa_from,cpa_num,DMCACHE_ISCHILD,cpa_fuv,pa->foffset,co,0,0,0,orco,0); } /* correct child ipo timing */ @@ -3675,6 +3626,7 @@ int psys_get_particle_state(Object *ob, ParticleSystem *psys, int p, ParticleKey float cfra; int totpart=psys->totpart, between=0; + /* negative time means "use current time" */ if(state->time>0) cfra=state->time; else @@ -3750,7 +3702,48 @@ int psys_get_particle_state(Object *ob, ParticleSystem *psys, int p, ParticleKey } else{ if (pa) { /* TODO PARTICLE - should this ever be NULL? - Campbell */ - copy_particle_key(state,&pa->state,0); + if(pa->state.time==state->time || ELEM(part->phystype,PART_PHYS_NO,PART_PHYS_KEYED)) + copy_particle_key(state, &pa->state, 1); + else if(pa->prev_state.time==state->time) + copy_particle_key(state, &pa->prev_state, 1); + else { + /* let's interpolate to try to be as accurate as possible */ + if(pa->state.time + 1.0f > state->time && pa->prev_state.time - 1.0f < state->time) { + ParticleKey keys[4]; + float dfra, keytime, frs_sec = G.scene->r.frs_sec; + + if(pa->prev_state.time >= pa->state.time) { + /* prev_state is wrong so let's not use it, this can happen at frame 1 or particle birth */ + copy_particle_key(state, &pa->state, 1); + + VECADDFAC(state->co, state->co, state->vel, (state->time-pa->state.time)/frs_sec); + } + else { + copy_particle_key(keys+1, &pa->prev_state, 1); + copy_particle_key(keys+2, &pa->state, 1); + + dfra = keys[2].time - keys[1].time; + + keytime = (state->time - keys[1].time) / dfra; + + /* convert velocity to timestep size */ + VecMulf(keys[1].vel, dfra / frs_sec); + VecMulf(keys[2].vel, dfra / frs_sec); + + interpolate_particle(-1, keys, keytime, state, 1); + + /* convert back to real velocity */ + VecMulf(state->vel, frs_sec / dfra); + + VecLerpf(state->ave, keys[1].ave, keys[2].ave, keytime); + QuatInterpol(state->rot, keys[1].rot, keys[2].rot, keytime); + } + } + else { + /* extrapolating over big ranges is not accurate so let's just give something close to reasonable back */ + copy_particle_key(state, &pa->state, 0); + } + } if(pa->alive==PARS_DEAD && part->flag&PART_STICKY && pa->flag&PARS_STICKY && pa->stick_ob){ key_from_object(pa->stick_ob,state); @@ -3786,7 +3779,7 @@ void psys_get_dupli_texture(Object *ob, ParticleSettings *part, ParticleSystemMo else uv[0]= uv[1]= 0.0f; - psys_particle_on_emitter(ob, psmd, + psys_particle_on_emitter(psmd, (part->childtype == PART_CHILD_FACES)? PART_FROM_FACE: PART_FROM_PARTICLE, cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,loc,0,0,0,orco,0); } @@ -3810,7 +3803,7 @@ void psys_get_dupli_texture(Object *ob, ParticleSettings *part, ParticleSystemMo else uv[0]= uv[1]= 0.0f; - psys_particle_on_emitter(ob,psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc,0,0,0,orco,0); + psys_particle_on_emitter(psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc,0,0,0,orco,0); } } @@ -3823,9 +3816,9 @@ void psys_get_dupli_path_transform(Object *ob, ParticleSystem *psys, ParticleSys len= Normalize(vec); if(pa) - psys_particle_on_emitter(ob,psmd,psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc,nor,0,0,0,0); + psys_particle_on_emitter(psmd,psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc,nor,0,0,0,0); else - psys_particle_on_emitter(ob, psmd, + psys_particle_on_emitter(psmd, (psys->part->childtype == PART_CHILD_FACES)? PART_FROM_FACE: PART_FROM_PARTICLE, cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,loc,nor,0,0,0,0); @@ -3841,11 +3834,24 @@ void psys_get_dupli_path_transform(Object *ob, ParticleSystem *psys, ParticleSys Mat4MulMat4(mat, obrotmat, qmat); } else { + /* make sure that we get a proper side vector */ + if(fabs(Inpf(nor,vec))>0.999999) { + if(fabs(Inpf(nor,xvec))>0.999999) { + nor[0] = 0.0f; + nor[1] = 1.0f; + nor[2] = 0.0f; + } + else { + nor[0] = 1.0f; + nor[1] = 0.0f; + nor[2] = 0.0f; + } + } Crossf(side, nor, vec); Normalize(side); Crossf(nor, vec, side); - Mat4One(mat); + Mat4One(mat); VECCOPY(mat[0], vec); VECCOPY(mat[1], side); VECCOPY(mat[2], nor); diff --git a/source/blender/blenkernel/intern/particle_system.c b/source/blender/blenkernel/intern/particle_system.c index d1c0cdec71d..8f7a90625ba 100644 --- a/source/blender/blenkernel/intern/particle_system.c +++ b/source/blender/blenkernel/intern/particle_system.c @@ -53,13 +53,16 @@ #include "BLI_arithb.h" #include "BLI_blenlib.h" #include "BLI_kdtree.h" +#include "BLI_kdopbvh.h" #include "BLI_linklist.h" #include "BLI_threads.h" #include "BKE_anim.h" #include "BKE_bad_level_calls.h" #include "BKE_cdderivedmesh.h" +#include "BKE_collision.h" #include "BKE_displist.h" +#include "BKE_effect.h" #include "BKE_particle.h" #include "BKE_global.h" #include "BKE_utildefines.h" @@ -75,6 +78,8 @@ #include "BKE_modifier.h" #include "BKE_scene.h" +#include "PIL_time.h" + #include "BSE_headerbuttons.h" #include "blendef.h" @@ -145,6 +150,14 @@ void psys_reset(ParticleSystem *psys, int mode) BLI_freelistN(&psys->reactevents); } } + else if(mode == PSYS_RESET_CACHE_MISS) { + /* set all particles to be skipped */ + ParticleData *pa = psys->particles; + int p=0; + + for(; p<psys->totpart; p++, pa++) + pa->flag |= PARS_NO_DISP; + } /* reset children */ if(psys->child) { @@ -609,7 +622,7 @@ void psys_thread_distribute_particle(ParticleThread *thread, ParticleData *pa, C KDTreeNearest ptn[3]; int w, maxw; - psys_particle_on_dm(ctx->ob,ctx->dm,from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co1,0,0,0,orco1,0); + psys_particle_on_dm(ctx->dm,from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co1,0,0,0,orco1,0); transform_mesh_orco_verts((Mesh*)ob->data, &orco1, 1, 1); maxw = BLI_kdtree_find_n_nearest(ctx->tree,3,orco1,NULL,ptn); @@ -753,7 +766,7 @@ void psys_thread_distribute_particle(ParticleThread *thread, ParticleData *pa, C do_seams= (part->flag&PART_CHILD_SEAMS && ctx->seams); - psys_particle_on_dm(ob,dm,cfrom,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co1,nor1,0,0,orco1,ornor1); + psys_particle_on_dm(dm,cfrom,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co1,nor1,0,0,orco1,ornor1); transform_mesh_orco_verts((Mesh*)ob->data, &orco1, 1, 1); maxw = BLI_kdtree_find_n_nearest(ctx->tree,(do_seams)?10:4,orco1,ornor1,ptn); @@ -851,7 +864,7 @@ void psys_thread_distribute_particle(ParticleThread *thread, ParticleData *pa, C } } -void *exec_distribution(void *data) +static void *exec_distribution(void *data) { ParticleThread *thread= (ParticleThread*)data; ParticleSystem *psys= thread->ctx->psys; @@ -967,7 +980,7 @@ int psys_threads_init_distribution(ParticleThread *threads, DerivedMesh *finaldm tree=BLI_kdtree_new(totpart); for(p=0,pa=psys->particles; p<totpart; p++,pa++){ - psys_particle_on_dm(ob,dm,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,nor,0,0,orco,ornor); + psys_particle_on_dm(dm,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,nor,0,0,orco,ornor); transform_mesh_orco_verts((Mesh*)ob->data, &orco, 1, 1); BLI_kdtree_insert(tree, p, orco, ornor); } @@ -1115,12 +1128,14 @@ int psys_threads_init_distribution(ParticleThread *threads, DerivedMesh *finaldm if(children){ if(G.f & G_DEBUG) fprintf(stderr,"Particle child distribution error: Nothing to emit from!\n"); - for(p=0,cpa=psys->child; p<totpart; p++,cpa++){ - cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3]= 0.0; - cpa->foffset= 0.0f; - cpa->parent=0; - cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0; - cpa->num= -1; + if(psys->child) { + for(p=0,cpa=psys->child; p<totpart; p++,cpa++){ + cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3]= 0.0; + cpa->foffset= 0.0f; + cpa->parent=0; + cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0; + cpa->num= -1; + } } } else { @@ -1699,10 +1714,8 @@ void reset_particle(ParticleData *pa, ParticleSystem *psys, ParticleSystemModifi tob=ob; tpsys=BLI_findlink(&tob->particlesystem,psys->target_psys-1); - - /*TODO: get precise location of particle at birth*/ - state.time=cfra; + state.time = pa->time; if(pa->num == -1) memset(&state, 0, sizeof(state)); else @@ -1728,7 +1741,7 @@ void reset_particle(ParticleData *pa, ParticleSystem *psys, ParticleSystemModifi where_is_object_time(ob,pa->time); /* get birth location from object */ - psys_particle_on_emitter(ob,psmd,part->from,pa->num, pa->num_dmcache, pa->fuv,pa->foffset,loc,nor,utan,vtan,0,0); + psys_particle_on_emitter(psmd,part->from,pa->num, pa->num_dmcache, pa->fuv,pa->foffset,loc,nor,utan,vtan,0,0); /* save local coordinates for later */ VECCOPY(tloc,loc); @@ -1737,7 +1750,7 @@ void reset_particle(ParticleData *pa, ParticleSystem *psys, ParticleSystemModifi psys_get_texture(ob,give_current_material(ob,part->omat),psmd,psys,pa,&ptex,MAP_PA_IVEL); if(vg_vel && pa->num != -1) - ptex.ivel*=psys_interpolate_value_from_verts(psmd->dm,part->from,pa->num,pa->fuv,vg_vel); + ptex.ivel*=psys_particle_value_from_verts(psmd->dm,part->from,pa,vg_vel); /* particles live in global space so */ /* let's convert: */ @@ -1752,7 +1765,7 @@ void reset_particle(ParticleData *pa, ParticleSystem *psys, ParticleSystemModifi /* -tangent */ if(part->tanfac!=0.0){ - float phase=vg_rot?2.0f*(psys_interpolate_value_from_verts(psmd->dm,part->from,pa->num,pa->fuv,vg_rot)-0.5f):0.0f; + float phase=vg_rot?2.0f*(psys_particle_value_from_verts(psmd->dm,part->from,pa,vg_rot)-0.5f):0.0f; VecMulf(vtan,-(float)cos(M_PI*(part->tanphase+phase))); fac=-(float)sin(M_PI*(part->tanphase+phase)); VECADDFAC(vtan,vtan,utan,fac); @@ -1794,6 +1807,12 @@ void reset_particle(ParticleData *pa, ParticleSystem *psys, ParticleSystemModifi } /* conversion done so now we apply new: */ /* -velocity from: */ + + /* *reactions */ + if(dtime>0.0f){ + VECSUB(vel,pa->state.vel,pa->prev_state.vel); + } + /* *emitter velocity */ if(dtime!=0.0 && part->obfac!=0.0){ VECSUB(vel,loc,pa->state.co); @@ -1806,7 +1825,7 @@ void reset_particle(ParticleData *pa, ParticleSystem *psys, ParticleSystemModifi /* *emitter tangent */ if(part->tanfac!=0.0) - VECADDFAC(vel,vel,vtan,part->tanfac*(vg_tan?psys_interpolate_value_from_verts(psmd->dm,part->from,pa->num,pa->fuv,vg_tan):1.0f)); + VECADDFAC(vel,vel,vtan,part->tanfac*(vg_tan?psys_particle_value_from_verts(psmd->dm,part->from,pa,vg_tan):1.0f)); /* *texture */ /* TODO */ @@ -2095,22 +2114,18 @@ static void react_to_events(ParticleSystem *psys, int pa_num) for(re=psys->reactevents.first; re; re=re->next){ birth=0; if(part->from==PART_FROM_PARTICLE){ - if(pa->num==re->pa_num){ + if(pa->num==re->pa_num && pa->alive==PARS_UNBORN){ if(re->event==PART_EVENT_NEAR){ ParticleData *tpa = re->psys->particles+re->pa_num; float pa_time=tpa->time + pa->foffset*tpa->lifetime; - if(re->time > pa_time){ - pa->alive=PARS_ALIVE; + if(re->time >= pa_time){ pa->time=pa_time; pa->dietime=pa->time+pa->lifetime; } } else{ - if(pa->alive==PARS_UNBORN){ - pa->alive=PARS_ALIVE; - pa->time=re->time; - pa->dietime=pa->time+pa->lifetime; - } + pa->time=re->time; + pa->dietime=pa->time+pa->lifetime; } } } @@ -2118,7 +2133,6 @@ static void react_to_events(ParticleSystem *psys, int pa_num) dist=VecLenf(pa->state.co, re->state.co); if(dist <= re->size){ if(pa->alive==PARS_UNBORN){ - pa->alive=PARS_ALIVE; pa->time=re->time; pa->dietime=pa->time+pa->lifetime; birth=1; @@ -2194,6 +2208,8 @@ static int get_particles_from_cache(Object *ob, ParticleSystem *psys, int cfra) /* assuming struct consists of tightly packed floats */ for(i=0, pa=psys->particles; i<totpart; i++, pa++) { + if(cfra!=pa->state.time) + copy_particle_key(&pa->prev_state,&pa->state,1); if(!BKE_ptcache_file_read_floats(pf, (float*)&pa->state, sizeof(ParticleKey)/sizeof(float))) { BKE_ptcache_file_close(pf); return 0; @@ -2208,174 +2224,6 @@ static int get_particles_from_cache(Object *ob, ParticleSystem *psys, int cfra) /************************************************/ /* Effectors */ /************************************************/ -static float falloff_func(float fac, int usemin, float mindist, int usemax, float maxdist, float power) -{ - if(!usemin) - mindist= 0.0f; - - if(fac < mindist) { - return 1.0f; - } - else if(usemax) { - if(fac>maxdist || (maxdist-mindist)<=0.0f) - return 0.0f; - - fac= (fac-mindist)/(maxdist-mindist); - return 1.0f - (float)pow((double)fac, (double)power); - } - else - return pow((double)1.0f+fac-mindist, (double)-power); -} - -static float falloff_func_dist(PartDeflect *pd, float fac) -{ - return falloff_func(fac, pd->flag&PFIELD_USEMIN, pd->mindist, pd->flag&PFIELD_USEMAX, pd->maxdist, pd->f_power); -} - -static float falloff_func_rad(PartDeflect *pd, float fac) -{ - return falloff_func(fac, pd->flag&PFIELD_USEMINR, pd->minrad, pd->flag&PFIELD_USEMAXR, pd->maxrad, pd->f_power_r); -} - -static float effector_falloff(PartDeflect *pd, float *eff_velocity, float *vec_to_part) -{ - float eff_dir[3], temp[3]; - float falloff=1.0, fac, r_fac; - - VecCopyf(eff_dir,eff_velocity); - Normalize(eff_dir); - - if(pd->flag & PFIELD_POSZ && Inpf(eff_dir,vec_to_part)<0.0f) - falloff=0.0f; - else switch(pd->falloff){ - case PFIELD_FALL_SPHERE: - fac=VecLength(vec_to_part); - falloff= falloff_func_dist(pd, fac); - break; - - case PFIELD_FALL_TUBE: - fac=Inpf(vec_to_part,eff_dir); - falloff= falloff_func_dist(pd, ABS(fac)); - if(falloff == 0.0f) - break; - - VECADDFAC(temp,vec_to_part,eff_dir,-fac); - r_fac=VecLength(temp); - falloff*= falloff_func_rad(pd, r_fac); - break; - case PFIELD_FALL_CONE: - fac=Inpf(vec_to_part,eff_dir); - falloff= falloff_func_dist(pd, ABS(fac)); - if(falloff == 0.0f) - break; - - r_fac=saacos(fac/VecLength(vec_to_part))*180.0f/(float)M_PI; - falloff*= falloff_func_rad(pd, r_fac); - - break; -// case PFIELD_FALL_INSIDE: - //for(i=0; i<totface; i++,mface++){ - // VECCOPY(v1,mvert[mface->v1].co); - // VECCOPY(v2,mvert[mface->v2].co); - // VECCOPY(v3,mvert[mface->v3].co); - - // if(AxialLineIntersectsTriangle(a,co1, co2, v2, v3, v1, &lambda)){ - // if(from==PART_FROM_FACE) - // (pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST; - // else /* store number of intersections */ - // (pa+(int)(lambda*size[a])*a0mul)->loop++; - // } - // - // if(mface->v4){ - // VECCOPY(v4,mvert[mface->v4].co); - - // if(AxialLineIntersectsTriangle(a,co1, co2, v4, v1, v3, &lambda)){ - // if(from==PART_FROM_FACE) - // (pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST; - // else - // (pa+(int)(lambda*size[a])*a0mul)->loop++; - // } - // } - //} - -// break; - } - - return falloff; -} -static void do_physical_effector(short type, float force_val, float distance, float falloff, float size, float damp, - float *eff_velocity, float *vec_to_part, float *velocity, float *field, int planar) -{ - float mag_vec[3]={0,0,0}; - float temp[3], temp2[3]; - float eff_vel[3]; - - VecCopyf(eff_vel,eff_velocity); - Normalize(eff_vel); - - switch(type){ - case PFIELD_WIND: - VECCOPY(mag_vec,eff_vel); - - VecMulf(mag_vec,force_val*falloff); - VecAddf(field,field,mag_vec); - break; - - case PFIELD_FORCE: - if(planar) - Projf(mag_vec,vec_to_part,eff_vel); - else - VecCopyf(mag_vec,vec_to_part); - - VecMulf(mag_vec,force_val*falloff); - VecAddf(field,field,mag_vec); - break; - - case PFIELD_VORTEX: - Crossf(mag_vec,eff_vel,vec_to_part); - Normalize(mag_vec); - - VecMulf(mag_vec,force_val*distance*falloff); - VecAddf(field,field,mag_vec); - - break; - case PFIELD_MAGNET: - if(planar) - VecCopyf(temp,eff_vel); - else - /* magnetic field of a moving charge */ - Crossf(temp,eff_vel,vec_to_part); - - Crossf(temp2,velocity,temp); - VecAddf(mag_vec,mag_vec,temp2); - - VecMulf(mag_vec,force_val*falloff); - VecAddf(field,field,mag_vec); - break; - case PFIELD_HARMONIC: - if(planar) - Projf(mag_vec,vec_to_part,eff_vel); - else - VecCopyf(mag_vec,vec_to_part); - - VecMulf(mag_vec,force_val*falloff); - VecSubf(field,field,mag_vec); - - VecCopyf(mag_vec,velocity); - /* 1.9 is an experimental value to get critical damping at damp=1.0 */ - VecMulf(mag_vec,damp*1.9f*(float)sqrt(force_val)); - VecSubf(field,field,mag_vec); - break; - case PFIELD_NUCLEAR: - /*pow here is root of cosine expression below*/ - //rad=(float)pow(2.0,-1.0/power)*distance/size; - //VECCOPY(mag_vec,vec_to_part); - //Normalize(mag_vec); - //VecMulf(mag_vec,(float)cos(3.0*M_PI/2.0*(1.0-1.0/(pow(rad,power)+1.0)))/(rad+0.2f)); - //VECADDFAC(field,field,mag_vec,force_val); - break; - } -} static void do_texture_effector(Tex *tex, short mode, short is_2d, float nabla, short object, float *pa_co, float obmat[4][4], float force_val, float falloff, float *field) { TexResult result[4]; @@ -2468,7 +2316,9 @@ static void add_to_effectors(ListBase *lb, Object *ob, Object *obsrc, ParticleSy } } else if(pd->forcefield) + { type |= PSYS_EC_EFFECTOR; + } } if(pd && pd->deflect) @@ -2480,6 +2330,9 @@ static void add_to_effectors(ListBase *lb, Object *ob, Object *obsrc, ParticleSy ec->type=type; ec->distances=0; ec->locations=0; + ec->rng = rng_new(1); + rng_srandom(ec->rng, (unsigned int)(ceil(PIL_check_seconds_timer()))); // use better seed + BLI_addtail(lb, ec); } @@ -2493,11 +2346,14 @@ static void add_to_effectors(ListBase *lb, Object *ob, Object *obsrc, ParticleSy for(i=0; epsys; epsys=epsys->next,i++){ type=0; - if(epsys!=psys){ + if(epsys!=psys || (psys->part->flag & PART_SELF_EFFECT)){ epart=epsys->part; - if(epsys->part->pd && epsys->part->pd->forcefield) + if((epsys->part->pd && epsys->part->pd->forcefield) + || (epsys->part->pd2 && epsys->part->pd2->forcefield)) + { type=PSYS_EC_PARTICLE; + } if(epart->type==PART_REACTOR) { tob=epsys->target_ob; @@ -2512,6 +2368,9 @@ static void add_to_effectors(ListBase *lb, Object *ob, Object *obsrc, ParticleSy ec->ob= ob; ec->type=type; ec->psys_nbr=i; + ec->rng = rng_new(1); + rng_srandom(ec->rng, (unsigned int)(ceil(PIL_check_seconds_timer()))); + BLI_addtail(lb, ec); } } @@ -2561,6 +2420,9 @@ void psys_init_effectors(Object *obsrc, Group *group, ParticleSystem *psys) void psys_end_effectors(ParticleSystem *psys) { + /* NOTE: + ec->ob is not valid in here anymore! - dg + */ ListBase *lb=&psys->effectors; if(lb->first) { ParticleEffectorCache *ec; @@ -2579,20 +2441,24 @@ void psys_end_effectors(ParticleSystem *psys) if(ec->tree) BLI_kdtree_free(ec->tree); + + if(ec->rng) + rng_free(ec->rng); + } BLI_freelistN(lb); } } -static void precalc_effectors(Object *ob, ParticleSystem *psys, ParticleSystemModifierData *psmd) +static void precalc_effectors(Object *ob, ParticleSystem *psys, ParticleSystemModifierData *psmd, float cfra) { ListBase *lb=&psys->effectors; ParticleEffectorCache *ec; ParticleSettings *part=psys->part; ParticleData *pa; float vec2[3],loc[3],*co=0; - int p,totpart,totvert; + int p,totpart; for(ec= lb->first; ec; ec= ec->next) { PartDeflect *pd= ec->ob->pd; @@ -2617,7 +2483,7 @@ static void precalc_effectors(Object *ob, ParticleSystem *psys, ParticleSystemMo ec->locations=MEM_callocN(totpart*3*sizeof(float),"particle locations"); for(p=0,pa=psys->particles; p<totpart; p++, pa++){ - psys_particle_on_emitter(ob,psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc,0,0,0,0,0); + psys_particle_on_emitter(psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc,0,0,0,0,0); Mat4MulVecfl(ob->obmat,loc); ec->distances[p]=VecLenf(loc,vec); VECSUB(loc,loc,vec); @@ -2625,96 +2491,18 @@ static void precalc_effectors(Object *ob, ParticleSystem *psys, ParticleSystemMo } } } - else if(ec->type==PSYS_EC_DEFLECT){ - DerivedMesh *dm; - MFace *mface=0; - MVert *mvert=0; - int i, totface; - float v1[3],v2[3],v3[3],v4[4], *min, *max; - - if(ob==ec->ob) - dm=psmd->dm; - else{ - psys_disable_all(ec->ob); - - dm=mesh_get_derived_final(ec->ob,0); - - psys_enable_all(ec->ob); - } - - if(dm){ - totvert=dm->getNumVerts(dm); - totface=dm->getNumFaces(dm); - mface=dm->getFaceDataArray(dm,CD_MFACE); - mvert=dm->getVertDataArray(dm,CD_MVERT); - - /* Decide which is faster to calculate by the amount of*/ - /* matrice multiplications needed to convert spaces. */ - /* With size deflect we have to convert allways because */ - /* the object can be scaled nonuniformly (sphere->ellipsoid). */ - if(totvert<2*psys->totpart || part->flag & PART_SIZE_DEFL){ - co=ec->vert_cos=MEM_callocN(sizeof(float)*3*totvert,"Particle deflection vert cos"); - /* convert vert coordinates to global (particle) coordinates */ - for(i=0; i<totvert; i++, co+=3){ - VECCOPY(co,mvert[i].co); - Mat4MulVecfl(ec->ob->obmat,co); - } - co=ec->vert_cos; - } - else - ec->vert_cos=0; - - INIT_MINMAX(ec->ob_minmax,ec->ob_minmax+3); - - min=ec->face_minmax=MEM_callocN(sizeof(float)*6*totface,"Particle deflection face minmax"); - max=min+3; - - for(i=0; i<totface; i++,mface++,min+=6,max+=6){ - if(co){ - VECCOPY(v1,co+3*mface->v1); - VECCOPY(v2,co+3*mface->v2); - VECCOPY(v3,co+3*mface->v3); - } - else{ - VECCOPY(v1,mvert[mface->v1].co); - VECCOPY(v2,mvert[mface->v2].co); - VECCOPY(v3,mvert[mface->v3].co); - } - INIT_MINMAX(min,max); - DO_MINMAX(v1,min,max); - DO_MINMAX(v2,min,max); - DO_MINMAX(v3,min,max); - - if(mface->v4){ - if(co){ - VECCOPY(v4,co+3*mface->v4); - } - else{ - VECCOPY(v4,mvert[mface->v4].co); - } - DO_MINMAX(v4,min,max); - } - - DO_MINMAX(min,ec->ob_minmax,ec->ob_minmax+3); - DO_MINMAX(max,ec->ob_minmax,ec->ob_minmax+3); - } - } - else - ec->face_minmax=0; - } else if(ec->type==PSYS_EC_PARTICLE){ + Object *eob = ec->ob; + ParticleSystem *epsys = BLI_findlink(&eob->particlesystem,ec->psys_nbr); + ParticleSettings *epart = epsys->part; + ParticleData *epa; + int p, totepart = epsys->totpart; + if(psys->part->phystype==PART_PHYS_BOIDS){ - Object *eob = ec->ob; - ParticleSystem *epsys; - ParticleSettings *epart; - ParticleData *epa; ParticleKey state; PartDeflect *pd; - int totepart, p; - epsys= BLI_findlink(&eob->particlesystem,ec->psys_nbr); - epart= epsys->part; + pd= epart->pd; - totepart= epsys->totpart; if(pd->forcefield==PFIELD_FORCE && totepart){ KDTree *tree; @@ -2728,6 +2516,12 @@ static void precalc_effectors(Object *ob, ParticleSystem *psys, ParticleSystemMo BLI_kdtree_balance(tree); } } + + } + else if(ec->type==PSYS_EC_DEFLECT) { + CollisionModifierData *collmd = ( CollisionModifierData * ) ( modifiers_findByType ( ec->ob, eModifierType_Collision ) ); + if(collmd) + collision_move_object(collmd, 1.0, 0.0); } } } @@ -2745,35 +2539,31 @@ void do_effectors(int pa_no, ParticleData *pa, ParticleKey *state, Object *ob, P ListBase *lb=&psys->effectors; ParticleEffectorCache *ec; float distance, vec_to_part[3]; - float falloff; + float falloff, charge = 0.0f; int p; /* check all effector objects for interaction */ if(lb->first){ + if(psys->part->pd && psys->part->pd->forcefield==PFIELD_CHARGE){ + /* Only the charge of the effected particle is used for + interaction, not fall-offs. If the fall-offs aren't the + same this will be unphysical, but for animation this + could be the wanted behavior. If you want physical + correctness the fall-off should be spherical 2.0 anyways. + */ + charge = psys->part->pd->f_strength; + } + if(psys->part->pd2 && psys->part->pd2->forcefield==PFIELD_CHARGE){ + charge += psys->part->pd2->f_strength; + } for(ec = lb->first; ec; ec= ec->next){ eob= ec->ob; if(ec->type & PSYS_EC_EFFECTOR){ pd=eob->pd; if(psys->part->type!=PART_HAIR && psys->part->integrator) where_is_object_time(eob,cfra); - /* Get IPO force strength and fall off values here */ - //if (has_ipo_code(eob->ipo, OB_PD_FSTR)) - // force_val = IPO_GetFloatValue(eob->ipo, OB_PD_FSTR, cfra); - //else - // force_val = pd->f_strength; - - //if (has_ipo_code(eob->ipo, OB_PD_FFALL)) - // ffall_val = IPO_GetFloatValue(eob->ipo, OB_PD_FFALL, cfra); - //else - // ffall_val = pd->f_power; - - //if (has_ipo_code(eob->ipo, OB_PD_FMAXD)) - // maxdist = IPO_GetFloatValue(eob->ipo, OB_PD_FMAXD, cfra); - //else - // maxdist = pd->maxdist; /* use center of object for distance calculus */ - //obloc= eob->obmat[3]; VecSubf(vec_to_part, state->co, eob->obmat[3]); distance = VecLength(vec_to_part); @@ -2786,22 +2576,22 @@ void do_effectors(int pa_no, ParticleData *pa, ParticleKey *state, Object *ob, P pd->flag & PFIELD_TEX_OBJECT, (pd->flag & PFIELD_TEX_ROOTCO) ? rootco : state->co, eob->obmat, pd->f_strength, falloff, force_field); } else { - do_physical_effector(pd->forcefield,pd->f_strength,distance, - falloff,pd->f_dist,pd->f_damp,eob->obmat[2],vec_to_part, - pa->state.vel,force_field,pd->flag&PFIELD_PLANAR); + do_physical_effector(eob, state->co, pd->forcefield,pd->f_strength,distance, + falloff,0.0,pd->f_damp,eob->obmat[2],vec_to_part, + state->vel,force_field,pd->flag&PFIELD_PLANAR,ec->rng,pd->f_noise,charge,pa->size); } } if(ec->type & PSYS_EC_PARTICLE){ - int totepart; + int totepart, i; epsys= BLI_findlink(&eob->particlesystem,ec->psys_nbr); epart= epsys->part; - pd= epart->pd; + pd=epart->pd; totepart= epsys->totpart; if(totepart <= 0) continue; - if(pd->forcefield==PFIELD_HARMONIC){ + if(pd && pd->forcefield==PFIELD_HARMONIC){ /* every particle is mapped to only one harmonic effector particle */ p= pa_no%epsys->totpart; totepart= p+1; @@ -2813,33 +2603,29 @@ void do_effectors(int pa_no, ParticleData *pa, ParticleKey *state, Object *ob, P epsys->lattice=psys_get_lattice(ob,psys); for(; p<totepart; p++){ + /* particle skips itself as effector */ + if(epsys==psys && p == pa_no) continue; + epa = epsys->particles + p; - estate.time=-1.0; + estate.time=cfra; if(psys_get_particle_state(eob,epsys,p,&estate,0)){ VECSUB(vec_to_part, state->co, estate.co); distance = VecLength(vec_to_part); - - //if(pd->forcefield==PFIELD_HARMONIC){ - // //if(cfra < epa->time + radius){ /* radius is fade-in in ui */ - // // eforce*=(cfra-epa->time)/radius; - // //} - //} - //else{ - // /* Limit minimum distance to effector particle so that */ - // /* the force is not too big */ - // if (distance < 0.001) distance = 0.001f; - //} - falloff=effector_falloff(pd,estate.vel,vec_to_part); + for(i=0, pd = epart->pd; i<2; i++,pd = epart->pd2) { + if(pd==NULL || pd->forcefield==0) continue; - if(falloff<=0.0f) - ; /* don't do anything */ - else - do_physical_effector(pd->forcefield,pd->f_strength,distance, - falloff,epart->size,pd->f_damp,estate.vel,vec_to_part, - state->vel,force_field,0); + falloff=effector_falloff(pd,estate.vel,vec_to_part); + + if(falloff<=0.0f) + ; /* don't do anything */ + else + do_physical_effector(eob, state->co, pd->forcefield,pd->f_strength,distance, + falloff,epart->size,pd->f_damp,estate.vel,vec_to_part, + state->vel,force_field,0, ec->rng, pd->f_noise,charge,pa->size); + } } - else if(pd->forcefield==PFIELD_HARMONIC && cfra-framestep <= epa->dietime && cfra>epa->dietime){ + else if(pd && pd->forcefield==PFIELD_HARMONIC && cfra-framestep <= epa->dietime && cfra>epa->dietime){ /* first step after key release */ psys_get_particle_state(eob,epsys,p,&estate,1); VECADD(vel,vel,estate.vel); @@ -2860,7 +2646,7 @@ void do_effectors(int pa_no, ParticleData *pa, ParticleKey *state, Object *ob, P /* Newtonian physics */ /************************************************/ /* gathers all forces that effect particles and calculates a new state for the particle */ -static void apply_particle_forces(int pa_no, ParticleData *pa, Object *ob, ParticleSystem *psys, ParticleSettings *part, float timestep, float dfra, float cfra, ParticleKey *state) +static void apply_particle_forces(int pa_no, ParticleData *pa, Object *ob, ParticleSystem *psys, ParticleSettings *part, float timestep, float dfra, float cfra) { ParticleKey states[5], tkey; float force[3],tvel[3],dx[4][3],dv[4][3]; @@ -2868,7 +2654,7 @@ static void apply_particle_forces(int pa_no, ParticleData *pa, Object *ob, Parti int i, steps=1; /* maintain angular velocity */ - VECCOPY(state->ave,pa->state.ave); + VECCOPY(pa->state.ave,pa->prev_state.ave); if(part->flag & PART_SIZEMASS) pa_mass*=pa->size; @@ -2918,8 +2704,8 @@ static void apply_particle_forces(int pa_no, ParticleData *pa, Object *ob, Parti switch(part->integrator){ case PART_INT_EULER: - VECADDFAC(state->co,states->co,states->vel,dtime); - VECADDFAC(state->vel,states->vel,force,dtime); + VECADDFAC(pa->state.co,states->co,states->vel,dtime); + VECADDFAC(pa->state.vel,states->vel,force,dtime); break; case PART_INT_MIDPOINT: if(i==0){ @@ -2928,8 +2714,8 @@ static void apply_particle_forces(int pa_no, ParticleData *pa, Object *ob, Parti fra=psys->cfra+0.5f*dfra; } else{ - VECADDFAC(state->co,states->co,states[1].vel,dtime); - VECADDFAC(state->vel,states->vel,force,dtime); + VECADDFAC(pa->state.co,states->co,states[1].vel,dtime); + VECADDFAC(pa->state.vel,states->vel,force,dtime); } break; case PART_INT_RK4: @@ -2969,15 +2755,15 @@ static void apply_particle_forces(int pa_no, ParticleData *pa, Object *ob, Parti VECCOPY(dv[3],force); VecMulf(dv[3],dtime); - VECADDFAC(state->co,states->co,dx[0],1.0f/6.0f); - VECADDFAC(state->co,state->co,dx[1],1.0f/3.0f); - VECADDFAC(state->co,state->co,dx[2],1.0f/3.0f); - VECADDFAC(state->co,state->co,dx[3],1.0f/6.0f); + VECADDFAC(pa->state.co,states->co,dx[0],1.0f/6.0f); + VECADDFAC(pa->state.co,pa->state.co,dx[1],1.0f/3.0f); + VECADDFAC(pa->state.co,pa->state.co,dx[2],1.0f/3.0f); + VECADDFAC(pa->state.co,pa->state.co,dx[3],1.0f/6.0f); - VECADDFAC(state->vel,states->vel,dv[0],1.0f/6.0f); - VECADDFAC(state->vel,state->vel,dv[1],1.0f/3.0f); - VECADDFAC(state->vel,state->vel,dv[2],1.0f/3.0f); - VECADDFAC(state->vel,state->vel,dv[3],1.0f/6.0f); + VECADDFAC(pa->state.vel,states->vel,dv[0],1.0f/6.0f); + VECADDFAC(pa->state.vel,pa->state.vel,dv[1],1.0f/3.0f); + VECADDFAC(pa->state.vel,pa->state.vel,dv[2],1.0f/3.0f); + VECADDFAC(pa->state.vel,pa->state.vel,dv[3],1.0f/6.0f); } break; } @@ -2985,62 +2771,62 @@ static void apply_particle_forces(int pa_no, ParticleData *pa, Object *ob, Parti /* damp affects final velocity */ if(part->dampfac!=0.0) - VecMulf(state->vel,1.0f-part->dampfac); + VecMulf(pa->state.vel,1.0f-part->dampfac); /* finally we do guides */ time=(cfra-pa->time)/pa->lifetime; CLAMP(time,0.0,1.0); - VECCOPY(tkey.co,state->co); - VECCOPY(tkey.vel,state->vel); - tkey.time=state->time; + VECCOPY(tkey.co,pa->state.co); + VECCOPY(tkey.vel,pa->state.vel); + tkey.time=pa->state.time; if(part->type != PART_HAIR) { if(do_guide(&tkey,pa_no,time,&psys->effectors)) { - VECCOPY(state->co,tkey.co); + VECCOPY(pa->state.co,tkey.co); /* guides don't produce valid velocity */ - VECSUB(state->vel,tkey.co,pa->state.co); - VecMulf(state->vel,1.0f/dtime); - state->time=tkey.time; + VECSUB(pa->state.vel,tkey.co,pa->prev_state.co); + VecMulf(pa->state.vel,1.0f/dtime); + pa->state.time=tkey.time; } } } -static void rotate_particle(ParticleSettings *part, ParticleData *pa, float dfra, float timestep, ParticleKey *state) +static void rotate_particle(ParticleSettings *part, ParticleData *pa, float dfra, float timestep) { float rotfac, rot1[4], rot2[4]={1.0,0.0,0.0,0.0}, dtime=dfra*timestep; if((part->flag & PART_ROT_DYN)==0){ if(part->avemode==PART_AVE_SPIN){ float angle; - float len1 = VecLength(pa->state.vel); - float len2 = VecLength(state->vel); + float len1 = VecLength(pa->prev_state.vel); + float len2 = VecLength(pa->state.vel); if(len1==0.0f || len2==0.0f) - state->ave[0]=state->ave[1]=state->ave[2]=0.0f; + pa->state.ave[0]=pa->state.ave[1]=pa->state.ave[2]=0.0f; else{ - Crossf(state->ave,pa->state.vel,state->vel); - Normalize(state->ave); - angle=Inpf(pa->state.vel,state->vel)/(len1*len2); - VecMulf(state->ave,saacos(angle)/dtime); + Crossf(pa->state.ave,pa->prev_state.vel,pa->state.vel); + Normalize(pa->state.ave); + angle=Inpf(pa->prev_state.vel,pa->state.vel)/(len1*len2); + VecMulf(pa->state.ave,saacos(angle)/dtime); } - VecRotToQuat(state->vel,dtime*part->avefac,rot2); + VecRotToQuat(pa->state.vel,dtime*part->avefac,rot2); } } - rotfac=VecLength(state->ave); + rotfac=VecLength(pa->state.ave); if(rotfac==0.0){ /* QuatOne (in VecRotToQuat) doesn't give unit quat [1,0,0,0]?? */ rot1[0]=1.0; rot1[1]=rot1[2]=rot1[3]=0; } else{ - VecRotToQuat(state->ave,rotfac*dtime,rot1); + VecRotToQuat(pa->state.ave,rotfac*dtime,rot1); } - QuatMul(state->rot,rot1,pa->state.rot); - QuatMul(state->rot,rot2,state->rot); + QuatMul(pa->state.rot,rot1,pa->prev_state.rot); + QuatMul(pa->state.rot,rot2,pa->state.rot); /* keep rotation quat in good health */ - NormalQuat(state->rot); + NormalQuat(pa->state.rot); } /* convert from triangle barycentric weights to quad mean value weights */ @@ -3075,7 +2861,7 @@ int psys_intersect_dm(Object *ob, DerivedMesh *dm, float *vert_cos, float *co1, dm=mesh_get_derived_final(ob,0); if(dm==0) - mesh_get_derived_deform(ob,0); + dm=mesh_get_derived_deform(ob,0); psys_enable_all(ob); @@ -3185,37 +2971,122 @@ int psys_intersect_dm(Object *ob, DerivedMesh *dm, float *vert_cos, float *co1, } return intersect; } + +/* container for moving data between deflet_particle and particle_intersect_face */ +typedef struct ParticleCollision +{ + struct Object *ob, *ob_t; // collided and current objects + struct CollisionModifierData *md; // collision modifier for ob_t; + float nor[3]; // normal at collision point + float vel[3]; // velocity of collision point + float co1[3], co2[3]; // ray start and end points + float ray_len; // original length of co2-co1, needed for collision time evaluation + float t; // time of previous collision, needed for substracting face velocity +} +ParticleCollision; + +static void particle_intersect_face(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit) +{ + ParticleCollision *col = (ParticleCollision *) userdata; + MFace *face = col->md->mfaces + index; + MVert *x = col->md->x; + MVert *v = col->md->current_v; + float vel[3], co1[3], co2[3], uv[2], ipoint[3], temp[3], t; + + float *t0, *t1, *t2, *t3; + t0 = x[ face->v1 ].co; + t1 = x[ face->v2 ].co; + t2 = x[ face->v3 ].co; + t3 = face->v4 ? x[ face->v4].co : NULL; + + /* calculate average velocity of face */ + VECCOPY(vel, v[ face->v1 ].co); + VECADD(vel, vel, v[ face->v2 ].co); + VECADD(vel, vel, v[ face->v3 ].co); + VecMulf(vel, 0.33334f); + + /* substract face velocity, in other words convert to + a coordinate system where only the particle moves */ + VECADDFAC(co1, col->co1, vel, -col->t); + VECSUB(co2, col->co2, vel); + + do + { + if(ray->radius == 0.0f) { + if(LineIntersectsTriangle(co1, co2, t0, t1, t2, &t, uv)) { + if(t >= 0.0f && t < hit->dist/col->ray_len) { + hit->dist = col->ray_len * t; + hit->index = index; + + /* calculate normal that's facing the particle */ + CalcNormFloat(t0, t1, t2, col->nor); + VECSUB(temp, co2, co1); + if(Inpf(col->nor, temp) > 0.0f) + VecMulf(col->nor, -1.0f); + + VECCOPY(col->vel,vel); + + col->ob = col->ob_t; + } + } + } + else { + if(SweepingSphereIntersectsTriangleUV(co1, co2, ray->radius, t0, t1, t2, &t, ipoint)) { + if(t >=0.0f && t < hit->dist/col->ray_len) { + hit->dist = col->ray_len * t; + hit->index = index; + + VecLerpf(temp, co1, co2, t); + + VECSUB(col->nor, temp, ipoint); + Normalize(col->nor); + + VECCOPY(col->vel,vel); + + col->ob = col->ob_t; + } + } + } + + t1 = t2; + t2 = t3; + t3 = NULL; + + } while(t2); +} /* particle - mesh collision code */ /* in addition to basic point to surface collisions handles friction & damping,*/ /* angular momentum <-> linear momentum and swept sphere - mesh collisions */ /* 1. check for all possible deflectors for closest intersection on particle path */ /* 2. if deflection was found kill the particle or calculate new coordinates */ -static void deflect_particle(Object *pob, ParticleSystemModifierData *psmd, ParticleSystem *psys, ParticleSettings *part, ParticleData *pa, int p, float dfra, float cfra, ParticleKey *state, int *pa_die){ - Object *ob, *min_ob; - MFace *mface; - MVert *mvert; - DerivedMesh *dm; +static void deflect_particle(Object *pob, ParticleSystemModifierData *psmd, ParticleSystem *psys, ParticleSettings *part, ParticleData *pa, int p, float timestep, float dfra, float cfra){ + Object *ob = NULL; ListBase *lb=&psys->effectors; ParticleEffectorCache *ec; - ParticleKey cstate; - float imat[4][4]; - float co1[3],co2[3],def_loc[3],def_nor[3],unit_nor[3],def_tan[3],dvec[3],def_vel[3],dave[3],dvel[3]; - float pa_minmax[6]; - float min_w[4], zerovec[3]={0.0,0.0,0.0}, ipoint[3]; - float min_d,dotprod,damp,frict,o_len,d_len,radius=-1.0f; - int min_face=0, intersect=1, through=0; - short deflections=0, global=0; + ParticleKey reaction_state; + ParticleCollision col; + BVHTreeRayHit hit; + float ray_dir[3], zerovec[3]={0.0,0.0,0.0}; + float radius = ((part->flag & PART_SIZE_DEFL)?pa->size:0.0f); + int deflections=0, max_deflections=10; - VECCOPY(def_loc,pa->state.co); - VECCOPY(def_vel,pa->state.vel); + VECCOPY(col.co1, pa->prev_state.co); + VECCOPY(col.co2, pa->state.co); + col.t = 0.0f; /* 10 iterations to catch multiple deflections */ - if(lb->first) while(deflections<10){ - intersect=0; - global=0; - min_d=20000.0; - min_ob=NULL; + if(lb->first) while(deflections < max_deflections){ /* 1. */ + + VECSUB(ray_dir, col.co2, col.co1); + hit.index = -1; + hit.dist = col.ray_len = VecLength(ray_dir); + + /* even if particle is stationary we want to check for moving colliders */ + /* if hit.dist is zero the bvhtree_ray_cast will just ignore everything */ + if(hit.dist == 0.0f) + hit.dist = col.ray_len = 0.000001f; + for(ec=lb->first; ec; ec=ec->next){ if(ec->type & PSYS_EC_DEFLECT){ ob= ec->ob; @@ -3223,263 +3094,168 @@ static void deflect_particle(Object *pob, ParticleSystemModifierData *psmd, Part if(part->type!=PART_HAIR) where_is_object_time(ob,cfra); - if(ob==pob){ - dm=psmd->dm; - /* particles should not collide with emitter at birth */ - if(pa->time < cfra && pa->time >= psys->cfra) - continue; - } - else - dm=0; - - VECCOPY(co1,def_loc); - VECCOPY(co2,state->co); - - if(ec->vert_cos==0){ - /* convert particle coordinates to object coordinates */ - Mat4Invert(imat,ob->obmat); - Mat4MulVecfl(imat,co1); - Mat4MulVecfl(imat,co2); - } - - INIT_MINMAX(pa_minmax,pa_minmax+3); - DO_MINMAX(co1,pa_minmax,pa_minmax+3); - DO_MINMAX(co2,pa_minmax,pa_minmax+3); - if(part->flag&PART_SIZE_DEFL){ - pa_minmax[0]-=pa->size; - pa_minmax[1]-=pa->size; - pa_minmax[2]-=pa->size; - pa_minmax[3]+=pa->size; - pa_minmax[4]+=pa->size; - pa_minmax[5]+=pa->size; - - radius=pa->size; - } - - if(ec->face_minmax==0 || AabbIntersectAabb(pa_minmax,pa_minmax+3,ec->ob_minmax,ec->ob_minmax+3)) { - if(psys_intersect_dm(ob,dm,ec->vert_cos,co1,co2,&min_d,&min_face,min_w, - ec->face_minmax,pa_minmax,radius,ipoint)){ + /* particles should not collide with emitter at birth */ + if(ob==pob && pa->time < cfra && pa->time >= psys->cfra) + continue; - min_ob=ob; + col.md = ( CollisionModifierData * ) ( modifiers_findByType ( ec->ob, eModifierType_Collision ) ); + col.ob_t = ob; - if(ec->vert_cos) - global=1; - else - global=0; - } - } + if(col.md->bvhtree) + BLI_bvhtree_ray_cast(col.md->bvhtree, col.co1, ray_dir, radius, &hit, particle_intersect_face, &col); } } /* 2. */ - if(min_ob){ - BLI_srandom((int)cfra+p); - ob=min_ob; - - if(ob==pob){ - dm=psmd->dm; - } - else{ - psys_disable_all(ob); - - dm=mesh_get_derived_final(ob,0); - - psys_enable_all(ob); - } - - mface=dm->getFaceDataArray(dm,CD_MFACE); - mface+=min_face; - mvert=dm->getVertDataArray(dm,CD_MVERT); - - /* permeability check */ - if(BLI_frand()<ob->pd->pdef_perm) - through=1; - else - through=0; - - if(through==0 && (part->flag & PART_DIE_ON_COL || ob->pd->flag & PDEFLE_KILL_PART)){ - pa->dietime = cfra-(1.0f-min_d)*dfra; - VecLerpf(def_loc,def_loc,state->co,min_d); - - VECCOPY(state->co,def_loc); - VecLerpf(state->vel,pa->state.vel,state->vel,min_d); - QuatInterpol(state->rot,pa->state.rot,state->rot,min_d); - VecLerpf(state->ave,pa->state.ave,state->ave,min_d); + if(hit.index>=0) { + PartDeflect *pd = col.ob->pd; + int through = (BLI_frand() < pd->pdef_perm) ? 1 : 0; + float co[3]; /* point of collision */ + float vec[3]; /* movement through collision */ + float t = hit.dist/col.ray_len; /* time of collision between this iteration */ + float dt = col.t + t * (1.0f - col.t); /* time of collision between frame change*/ + + VecLerpf(co, col.co1, col.co2, t); + VECSUB(vec, col.co2, col.co1); + + VecMulf(col.vel, 1.0f-col.t); + + /* particle dies in collision */ + if(through == 0 && (part->flag & PART_DIE_ON_COL || pd->flag & PDEFLE_KILL_PART)) { + pa->alive = PARS_DYING; + pa->dietime = pa->state.time + (cfra - pa->state.time) * dt; + + /* we have to add this for dying particles too so that reactors work correctly */ + VECADDFAC(co, co, col.nor, (through ? -0.0001f : 0.0001f)); - *pa_die=1; + VECCOPY(pa->state.co, co); + VecLerpf(pa->state.vel, pa->prev_state.vel, pa->state.vel, dt); + QuatInterpol(pa->state.rot, pa->prev_state.rot, pa->state.rot, dt); + VecLerpf(pa->state.ave, pa->prev_state.ave, pa->state.ave, dt); /* particle is dead so we don't need to calculate further */ - deflections=10; + deflections=max_deflections; /* store for reactors */ - copy_particle_key(&cstate,state,0); + copy_particle_key(&reaction_state,&pa->state,0); if(part->flag & PART_STICKY){ pa->stick_ob=ob; pa->flag |= PARS_STICKY; } } - else{ - VECCOPY(co1,def_loc); - VECCOPY(co2,state->co); + else { + float nor_vec[3], tan_vec[3], tan_vel[3], vel[3]; + float damp, frict; + float inp, inp_v; + + /* get damping & friction factors */ + damp = pd->pdef_damp + pd->pdef_rdamp * 2 * (BLI_frand() - 0.5f); + CLAMP(damp,0.0,1.0); - if(global==0){ - /* convert particle coordinates to object coordinates */ - Mat4Invert(imat,ob->obmat); - Mat4MulVecfl(imat,co1); - Mat4MulVecfl(imat,co2); - } + frict = pd->pdef_frict + pd->pdef_rfrict * 2 * (BLI_frand() - 0.5f); + CLAMP(frict,0.0,1.0); - VecLerpf(def_loc,co1,co2,min_d); + /* treat normal & tangent components separately */ + inp = Inpf(col.nor, vec); + inp_v = Inpf(col.nor, col.vel); - if(radius>0.0f){ - VECSUB(unit_nor,def_loc,ipoint); - } - else{ - /* get deflection point & normal */ - psys_interpolate_face(mvert,mface,0,0,min_w,ipoint,unit_nor,0,0,0,0); - if(global){ - Mat4Mul3Vecfl(ob->obmat,unit_nor); - Mat4MulVecfl(ob->obmat,ipoint); - } - } + VECADDFAC(tan_vec, vec, col.nor, -inp); + VECADDFAC(tan_vel, col.vel, col.nor, -inp_v); + if((part->flag & PART_ROT_DYN)==0) + VecLerpf(tan_vec, tan_vec, tan_vel, frict); - Normalize(unit_nor); + VECCOPY(nor_vec, col.nor); + inp *= 1.0f - damp; - VECSUB(dvec,co1,co2); - /* scale to remaining length after deflection */ - VecMulf(dvec,1.0f-min_d); + if(through) + inp_v *= damp; - /* flip normal to face particle */ - if(Inpf(unit_nor,dvec)<0.0f) - VecMulf(unit_nor,-1.0f); + /* special case for object hitting the particle from behind */ + if(through==0 && ((inp_v>0 && inp>0 && inp_v>inp) || (inp_v<0 && inp<0 && inp_v<inp))) + VecMulf(nor_vec, inp_v); + else + VecMulf(nor_vec, inp_v + (through ? 1.0f : -1.0f) * inp); - /* store for easy velocity calculation */ - o_len=VecLength(dvec); + /* angular <-> linear velocity - slightly more physical and looks even nicer than before */ + if(part->flag & PART_ROT_DYN) { + float surface_vel[3], rot_vel[3], friction[3], dave[3], dvel[3]; - /* project particle movement to normal & create tangent */ - dotprod=Inpf(dvec,unit_nor); - VECCOPY(def_nor,unit_nor); - VecMulf(def_nor,dotprod); - VECSUB(def_tan,def_nor,dvec); + /* apparent velocity along collision surface */ + VECSUB(surface_vel, tan_vec, tan_vel); - damp=ob->pd->pdef_damp+ob->pd->pdef_rdamp*2*(BLI_frand()-0.5f); + /* direction of rolling friction */ + Crossf(rot_vel, pa->state.ave, col.nor); + /* convert to current dt */ + VecMulf(rot_vel, (timestep*dfra) * (1.0f - col.t)); + VecMulf(rot_vel, pa->size); - /* create location after deflection */ - VECCOPY(dvec,def_nor); - damp=ob->pd->pdef_damp+ob->pd->pdef_rdamp*2*(BLI_frand()-0.5f); - CLAMP(damp,0.0,1.0); - VecMulf(dvec,1.0f-damp); - if(through) - VecMulf(dvec,-1.0); - - frict=ob->pd->pdef_frict+ob->pd->pdef_rfrict*2.0f*(BLI_frand()-0.5f); - CLAMP(frict,0.0,1.0); - VECADDFAC(dvec,dvec,def_tan,1.0f-frict); + /* apply sliding friction */ + VECSUB(surface_vel, surface_vel, rot_vel); + VECCOPY(friction, surface_vel); - /* store for easy velocity calculation */ - d_len=VecLength(dvec); + VecMulf(surface_vel, 1.0 - frict); + VecMulf(friction, frict); - /* just to be sure we don't hit the current face again */ - if(through){ - VECADDFAC(ipoint,ipoint,unit_nor,-0.0001f); - VECADDFAC(def_loc,def_loc,unit_nor,-0.0001f); + /* sliding changes angular velocity */ + Crossf(dave, col.nor, friction); + VecMulf(dave, 1.0f/MAX2(pa->size, 0.001)); - if(part->flag & PART_ROT_DYN){ - VECADDFAC(def_tan,def_tan,unit_nor,-0.0001f); - VECADDFAC(def_nor,def_nor,unit_nor,-0.0001f); - } - } - else{ - VECADDFAC(ipoint,ipoint,unit_nor,0.0001f); - VECADDFAC(def_loc,def_loc,unit_nor,0.0001f); + /* we assume rolling friction is around 0.01 of sliding friction */ + VecMulf(rot_vel, 1.0 - frict*0.01); - if(part->flag & PART_ROT_DYN){ - VECADDFAC(def_tan,def_tan,unit_nor,0.0001f); - VECADDFAC(def_nor,def_nor,unit_nor,0.0001f); - } - } + /* change in angular velocity has to be added to the linear velocity too */ + Crossf(dvel, dave, col.nor); + VecMulf(dvel, pa->size); + VECADD(rot_vel, rot_vel, dvel); - /* lets get back to global space */ - if(global==0){ - Mat4Mul3Vecfl(ob->obmat,dvec); - Mat4MulVecfl(ob->obmat,ipoint); - Mat4MulVecfl(ob->obmat,def_loc);/* def_loc remains as intersection point for next iteration */ - } + VECADD(surface_vel, surface_vel, rot_vel); + VECADD(tan_vec, surface_vel, tan_vel); - /* store for reactors */ - VECCOPY(cstate.co,ipoint); - VecLerpf(cstate.vel,pa->state.vel,state->vel,min_d); - QuatInterpol(cstate.rot,pa->state.rot,state->rot,min_d); - - /* slightly unphysical but looks nice enough */ - if(part->flag & PART_ROT_DYN){ - if(global==0){ - Mat4Mul3Vecfl(ob->obmat,def_nor); - Mat4Mul3Vecfl(ob->obmat,def_tan); - } + /* convert back to normal time */ + VecMulf(dave, 1.0f/MAX2((timestep*dfra) * (1.0f - col.t), 0.00001)); - Normalize(def_tan); - Normalize(def_nor); - VECCOPY(unit_nor,def_nor); + VecMulf(pa->state.ave, 1.0 - frict*0.01); + VECADD(pa->state.ave, pa->state.ave, dave); + } - /* create normal velocity */ - VecMulf(def_nor,Inpf(pa->state.vel,def_nor)); + /* combine components together again */ + VECADD(vec, nor_vec, tan_vec); - /* create tangential velocity */ - VecMulf(def_tan,Inpf(pa->state.vel,def_tan)); - - /* angular velocity change due to tangential velocity */ - Crossf(dave,unit_nor,def_tan); - VecMulf(dave,1.0f/pa->size); + /* calculate velocity from collision vector */ + VECCOPY(vel, vec); + VecMulf(vel, 1.0f/MAX2((timestep*dfra) * (1.0f - col.t), 0.00001)); - /* linear velocity change due to angular velocity */ - VecMulf(unit_nor,pa->size); /* point of impact from particle center */ - Crossf(dvel,pa->state.ave,unit_nor); + /* make sure we don't hit the current face again */ + VECADDFAC(co, co, col.nor, (through ? -0.0001f : 0.0001f)); - if(through) - VecMulf(def_nor,-1.0); + /* store state for reactors */ + VECCOPY(reaction_state.co, co); + VecLerpf(reaction_state.vel, pa->prev_state.vel, pa->state.vel, dt); + QuatInterpol(reaction_state.rot, pa->prev_state.rot, pa->state.rot, dt); - VecMulf(def_nor,1.0f-damp); - VECSUB(dvel,dvel,def_nor); + /* set coordinates for next iteration */ + VECCOPY(col.co1, co); + VECADDFAC(col.co2, co, vec, 1.0f - t); + col.t = dt; - VecMulf(dvel,1.0f-frict); - VecMulf(dave,1.0f-frict); - } - - if(d_len<0.001 && VecLength(pa->state.vel)<0.001){ + if(VecLength(vec) < 0.001 && VecLength(pa->state.vel) < 0.001) { /* kill speed to stop slipping */ - VECCOPY(state->vel,zerovec); - VECCOPY(state->co,def_loc); - if(part->flag & PART_ROT_DYN) - VECCOPY(state->ave,zerovec); - deflections=10; - } - else{ - - /* apply new coordinates */ - VECADD(state->co,def_loc,dvec); - - Normalize(dvec); - - /* we have to use original velocity because otherwise we get slipping */ - /* when forces like gravity balance out damping & friction */ - VecMulf(dvec,VecLength(pa->state.vel)*(d_len/o_len)); - VECCOPY(state->vel,dvec); - - if(part->flag & PART_ROT_DYN){ - VECADD(state->vel,state->vel,dvel); - VecMulf(state->vel,0.5); - VECADD(state->ave,state->ave,dave); - VecMulf(state->ave,0.5); + VECCOPY(pa->state.vel,zerovec); + VECCOPY(pa->state.co, co); + if(part->flag & PART_ROT_DYN) { + VECCOPY(pa->state.ave,zerovec); } } + else { + VECCOPY(pa->state.co, col.co2); + VECCOPY(pa->state.vel, vel); + } } deflections++; - cstate.time=cfra-(1.0f-min_d)*dfra; - //particle_react_to_collision(min_ob,pob,psys,pa,p,&cstate); - push_reaction(pob,psys,p,PART_EVENT_COLLIDE,&cstate); + reaction_state.time = cfra - (1.0f - dt) * dfra; + push_reaction(col.ob, psys, p, PART_EVENT_COLLIDE, &reaction_state); } else return; @@ -3540,7 +3316,9 @@ static int boid_see_mesh(ListBase *lb, Object *pob, ParticleSystem *psys, float else{ psys_disable_all(ob); - dm=mesh_get_derived_deform(ob,0); + dm=mesh_get_derived_final(ob,0); + if(dm==0) + dm=mesh_get_derived_deform(ob,0); psys_enable_all(ob); } @@ -3643,7 +3421,7 @@ static int add_boid_acc(BoidVecFunc *bvf, float lat_max, float tan_max, float *l } } /* determines the acceleration that the boid tries to acchieve */ -static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleSystem *psys, ParticleSettings *part, KDTree *tree, float timestep, float cfra, float *acc, int *pa_die) +static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleSystem *psys, ParticleSettings *part, KDTree *tree, float timestep, float cfra, float *acc) { ParticleData *pars=psys->particles; KDTreeNearest ptn[MAX_BOIDNEIGHBOURS+1]; @@ -3674,18 +3452,18 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS switch(part->boidrule[i]){ case BOID_COLLIDE: /* collision avoidance */ - bvf->Copyf(dvec,pa->state.vel); + bvf->Copyf(dvec,pa->prev_state.vel); bvf->Mulf(dvec,5.0f); - bvf->Addf(dvec,dvec,pa->state.co); - if(boid_see_mesh(&psys->effectors,ob,psys,pa->state.co,dvec,ob_co,ob_nor,cfra)){ + bvf->Addf(dvec,dvec,pa->prev_state.co); + if(boid_see_mesh(&psys->effectors,ob,psys,pa->prev_state.co,dvec,ob_co,ob_nor,cfra)){ float probelen = bvf->Length(dvec); float proj; float oblen; Normalize(ob_nor); - proj = bvf->Inpf(ob_nor,pa->state.vel); + proj = bvf->Inpf(ob_nor,pa->prev_state.vel); - bvf->Subf(dvec,pa->state.co,ob_co); + bvf->Subf(dvec,pa->prev_state.co,ob_co); oblen=bvf->Length(dvec); bvf->Copyf(dvec,ob_nor); @@ -3705,12 +3483,12 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS if(pd->forcefield==PFIELD_FORCE && pd->f_strength<0.0){ float distance; - VECSUB(dvec,eob->obmat[3],pa->state.co); + VECSUB(dvec,eob->obmat[3],pa->prev_state.co); distance=Normalize(dvec); if(part->flag & PART_DIE_ON_COL && distance < pd->mindist){ - *pa_die=1; + pa->alive = PARS_DYING; pa->dietime=cfra; i=BOID_TOT_RULES; break; @@ -3739,17 +3517,17 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS pd= epart->pd; totepart= epsys->totpart; - if(pd->forcefield==PFIELD_FORCE && pd->f_strength<0.0){ - count=BLI_kdtree_find_n_nearest(ec->tree,epart->boidneighbours,pa->state.co,NULL,ptn2); + if(pd->forcefield==PFIELD_FORCE && pd->f_strength<0.0 && ec->tree){ + count=BLI_kdtree_find_n_nearest(ec->tree,epart->boidneighbours,pa->prev_state.co,NULL,ptn2); for(p=0; p<count; p++){ state.time=-1.0; if(psys_get_particle_state(eob,epsys,ptn2[p].index,&state,0)){ - VECSUB(dvec, state.co, pa->state.co); + VECSUB(dvec, state.co, pa->prev_state.co); distance = Normalize(dvec); if(part->flag & PART_DIE_ON_COL && distance < (epsys->particles+ptn2[p].index)->size){ - *pa_die=1; + pa->alive = PARS_DYING; pa->dietime=cfra; i=BOID_TOT_RULES; break; @@ -3775,7 +3553,7 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS for(n=1; n<neighbours; n++){ if(ptn[n].dist<2.0f*pa->size){ if(ptn[n].dist!=0.0f) { - bvf->Subf(dvec,pa->state.co,pars[ptn[n].index].state.co); + bvf->Subf(dvec,pa->prev_state.co,pars[ptn[n].index].state.co); bvf->Mulf(dvec,(2.0f*pa->size-ptn[n].dist)/ptn[n].dist); bvf->Addf(avoid,avoid,dvec); near++; @@ -3798,7 +3576,7 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS } bvf->Mulf(center,1.0f/((float)neighbours-1.0f)); - bvf->Subf(dvec,center,pa->state.co); + bvf->Subf(dvec,center,pa->prev_state.co); bvf->Mulf(dvec,part->boidfac[BOID_CENTER]*2.0f); @@ -3807,9 +3585,9 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS break; case BOID_AV_VEL: /* average velocity */ - cur_vel=bvf->Length(pa->state.vel); + cur_vel=bvf->Length(pa->prev_state.vel); if(cur_vel>0.0){ - bvf->Copyf(dvec,pa->state.vel); + bvf->Copyf(dvec,pa->prev_state.vel); bvf->Mulf(dvec,part->boidfac[BOID_AV_VEL]*(avg_vel-cur_vel)/cur_vel); not_finished=add_boid_acc(bvf,max_lat_acc,max_tan_acc,&lat_accu,&tan_accu,acc,dvec,0); } @@ -3824,7 +3602,7 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS } bvf->Mulf(velocity,1.0f/((float)neighbours-1.0f)); - bvf->Subf(dvec,velocity,pa->state.vel); + bvf->Subf(dvec,velocity,pa->prev_state.vel); bvf->Mulf(dvec,part->boidfac[BOID_VEL_MATCH]); @@ -3842,7 +3620,7 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS if(pd->forcefield==PFIELD_FORCE && pd->f_strength>0.0){ float distance; - VECSUB(dvec,eob->obmat[3],pa->state.co); + VECSUB(dvec,eob->obmat[3],pa->prev_state.co); distance=Normalize(dvec); @@ -3859,7 +3637,7 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS where_on_path(eob, (cfra-pa->time)/pa->lifetime, temp, dvec); - VECSUB(dvec,temp,pa->state.co); + VECSUB(dvec,temp,pa->prev_state.co); distance=Normalize(dvec); @@ -3886,12 +3664,12 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS pd= epart->pd; totepart= epsys->totpart; - if(pd->forcefield==PFIELD_FORCE && pd->f_strength>0.0){ - count=BLI_kdtree_find_n_nearest(ec->tree,epart->boidneighbours,pa->state.co,NULL,ptn2); + if(pd->forcefield==PFIELD_FORCE && pd->f_strength>0.0 && ec->tree){ + count=BLI_kdtree_find_n_nearest(ec->tree,epart->boidneighbours,pa->prev_state.co,NULL,ptn2); for(p=0; p<count; p++){ state.time=-1.0; if(psys_get_particle_state(eob,epsys,ptn2[p].index,&state,0)){ - VECSUB(dvec, state.co, pa->state.co); + VECSUB(dvec, state.co, pa->prev_state.co); distance = Normalize(dvec); @@ -3913,7 +3691,7 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS /* level flight */ if((part->flag & PART_BOIDS_2D)==0){ dvec[0]=dvec[1]=0.0; - dvec[2]=-pa->state.vel[2]; + dvec[2]=-pa->prev_state.vel[2]; VecMulf(dvec,part->boidfac[BOID_LEVEL]); not_finished=add_boid_acc(bvf,max_lat_acc,max_tan_acc,&lat_accu,&tan_accu,acc,dvec,0); @@ -3923,7 +3701,7 @@ static void boid_brain(BoidVecFunc *bvf, ParticleData *pa, Object *ob, ParticleS } } /* tries to realize the wanted acceleration */ -static void boid_body(BoidVecFunc *bvf, ParticleData *pa, ParticleSystem *psys, ParticleSettings *part, float timestep, float *acc, ParticleKey *state) +static void boid_body(BoidVecFunc *bvf, ParticleData *pa, ParticleSystem *psys, ParticleSettings *part, float timestep, float *acc) { float dvec[3], bvec[3], length, max_vel=part->max_vel; float q2[4], q[4]; @@ -3931,7 +3709,7 @@ static void boid_body(BoidVecFunc *bvf, ParticleData *pa, ParticleSystem *psys, float yvec[3]={0.0,1.0,0.0}, zvec[3]={0.0,0.0,-1.0}, bank; /* apply new velocity, location & rotation */ - copy_particle_key(state,&pa->state,0); + copy_particle_key(&pa->state,&pa->prev_state,0); if(part->flag & PART_SIZEMASS) pa_mass*=pa->size; @@ -3943,32 +3721,49 @@ static void boid_body(BoidVecFunc *bvf, ParticleData *pa, ParticleSystem *psys, bvf->Copyf(dvec,acc); bvf->Mulf(dvec,timestep*timestep*0.5f); - bvf->Copyf(bvec,state->vel); + bvf->Copyf(bvec,pa->state.vel); bvf->Mulf(bvec,timestep); bvf->Addf(dvec,dvec,bvec); - bvf->Addf(state->co,state->co,dvec); + bvf->Addf(pa->state.co,pa->state.co,dvec); - /* air speed from wind effectors */ - if(psys->effectors.first){ + /* air speed from wind and vortex effectors */ + if(psys->effectors.first) { ParticleEffectorCache *ec; - for(ec=psys->effectors.first; ec; ec=ec->next){ - if(ec->type & PSYS_EC_EFFECTOR){ + for(ec=psys->effectors.first; ec; ec=ec->next) { + if(ec->type & PSYS_EC_EFFECTOR) { Object *eob = ec->ob; PartDeflect *pd = eob->pd; + float direction[3], vec_to_part[3]; + float falloff; + + if(pd->f_strength != 0.0f) { + VecCopyf(direction, eob->obmat[2]); + VecSubf(vec_to_part, pa->state.co, eob->obmat[3]); + + falloff=effector_falloff(pd, direction, vec_to_part); + + switch(pd->forcefield) { + case PFIELD_WIND: + if(falloff <= 0.0f) + ; /* don't do anything */ + else { + Normalize(direction); + VecMulf(direction, pd->f_strength * falloff); + bvf->Addf(pa->state.co, pa->state.co, direction); + } + break; + case PFIELD_VORTEX: + { + float distance, mag_vec[3]; + Crossf(mag_vec, direction, vec_to_part); + Normalize(mag_vec); - if(pd->forcefield==PFIELD_WIND && pd->f_strength!=0.0){ - float distance, wind[3]; - VecCopyf(wind,eob->obmat[2]); - distance=VecLenf(state->co,eob->obmat[3]); - - if (distance < 0.001) distance = 0.001f; + distance = VecLength(vec_to_part); - if(pd->flag&PFIELD_USEMAX && distance > pd->maxdist) - ; - else{ - Normalize(wind); - VecMulf(wind,pd->f_strength/(float)pow((double)distance,(double)pd->f_power)); - bvf->Addf(state->co,state->co,wind); + VecMulf(mag_vec, pd->f_strength * distance * falloff); + bvf->Addf(pa->state.co, pa->state.co, mag_vec); + break; + } } } } @@ -3976,8 +3771,8 @@ static void boid_body(BoidVecFunc *bvf, ParticleData *pa, ParticleSystem *psys, } - if((part->flag & PART_BOIDS_2D)==0 && pa->state.vel[0]!=0.0 && pa->state.vel[0]!=0.0 && pa->state.vel[0]!=0.0){ - Crossf(yvec,state->vel,zvec); + if((part->flag & PART_BOIDS_2D)==0 && pa->prev_state.vel[0]!=0.0 && pa->prev_state.vel[0]!=0.0 && pa->prev_state.vel[0]!=0.0){ + Crossf(yvec,pa->state.vel,zvec); Normalize(yvec); @@ -4004,27 +3799,27 @@ static void boid_body(BoidVecFunc *bvf, ParticleData *pa, ParticleSystem *psys, } - VecRotToQuat(state->vel,bank,q); + VecRotToQuat(pa->state.vel,bank,q); - VECCOPY(dvec,state->vel); + VECCOPY(dvec,pa->state.vel); VecMulf(dvec,-1.0f); vectoquat(dvec, OB_POSX, OB_POSZ, q2); - QuatMul(state->rot,q,q2); + QuatMul(pa->state.rot,q,q2); bvf->Mulf(acc,timestep); - bvf->Addf(state->vel,state->vel,acc); + bvf->Addf(pa->state.vel,pa->state.vel,acc); if(part->flag & PART_BOIDS_2D){ - state->vel[2]=0.0; - state->co[2]=part->groundz; + pa->state.vel[2]=0.0; + pa->state.co[2]=part->groundz; - if(psys->keyed_ob){ + if(psys->keyed_ob && (psys->keyed_ob->type == OB_MESH)){ Object *zob=psys->keyed_ob; int min_face; float co1[3],co2[3],min_d=2.0,min_w[4],imat[4][4]; - VECCOPY(co1,state->co); - VECCOPY(co2,state->co); + VECCOPY(co1,pa->state.co); + VECCOPY(co2,pa->state.co); co1[2]=1000.0f; co2[2]=-1000.0f; @@ -4055,7 +3850,7 @@ static void boid_body(BoidVecFunc *bvf, ParticleData *pa, ParticleSystem *psys, Normalize(nor); - VECCOPY(state->co,loc); + VECCOPY(pa->state.co,loc); zvec[2]=1.0; @@ -4067,22 +3862,22 @@ static void boid_body(BoidVecFunc *bvf, ParticleData *pa, ParticleSystem *psys, VecRotToQuat(loc,bank,q); - QUATCOPY(q1,state->rot); + QUATCOPY(q1,pa->state.rot); - QuatMul(state->rot,q,q1); + QuatMul(pa->state.rot,q,q1); } } } } - length=bvf->Length(state->vel); + length=bvf->Length(pa->state.vel); if(length > max_vel) - bvf->Mulf(state->vel,max_vel/length); + bvf->Mulf(pa->state.vel,max_vel/length); } /************************************************/ /* Hair */ /************************************************/ -void save_hair(Object *ob, ParticleSystem *psys, ParticleSystemModifierData *psmd, float cfra){ +static void save_hair(Object *ob, ParticleSystem *psys, ParticleSystemModifierData *psmd, float cfra){ ParticleData *pa; HairKey *key; int totpart; @@ -4134,14 +3929,13 @@ static void dynamics_step(Object *ob, ParticleSystem *psys, ParticleSystemModifi float *vg_vel, float *vg_tan, float *vg_rot, float *vg_size) { ParticleData *pa; - ParticleKey *outstate, *key; ParticleSettings *part=psys->part; KDTree *tree=0; BoidVecFunc bvf; IpoCurve *icu_esize=find_ipocurve(part->ipo,PART_EMIT_SIZE); Material *ma=give_current_material(ob,part->omat); float timestep; - int p, totpart, pa_die; + int p, totpart; /* current time */ float ctime, ipotime; /* frame & time changes */ @@ -4204,15 +3998,9 @@ static void dynamics_step(Object *ob, ParticleSystem *psys, ParticleSystemModifi if(vg_size) MEM_freeN(vg_size); - - //if(part->phystype==PART_PHYS_SOLID) - // reset_to_first_fragment(psys); } else{ BLI_srandom(31415926 + (int)cfra + psys->seed); - - /* outstate is used so that particles are updated in parallel */ - outstate=MEM_callocN(totpart*sizeof(ParticleKey),"Particle Outstates"); /* update effectors */ if(psys->effectors.first) @@ -4221,7 +4009,7 @@ static void dynamics_step(Object *ob, ParticleSystem *psys, ParticleSystemModifi psys_init_effectors(ob,part->eff_group,psys); if(psys->effectors.first) - precalc_effectors(ob,psys,psmd); + precalc_effectors(ob,psys,psmd,cfra); if(part->phystype==PART_PHYS_BOIDS){ /* create particle tree for fast inter-particle comparisons */ @@ -4237,10 +4025,10 @@ static void dynamics_step(Object *ob, ParticleSystem *psys, ParticleSystemModifi } /* main loop: calculate physics for all particles */ - for(p=0, pa=psys->particles, key=outstate; p<totpart; p++,pa++,key++){ + for(p=0, pa=psys->particles; p<totpart; p++,pa++){ if(pa->flag & PARS_UNEXIST) continue; - copy_particle_key(key,&pa->state,1); + copy_particle_key(&pa->prev_state,&pa->state,1); /* set correct ipo timing */ if((part->flag&PART_ABS_TIME)==0 && part->ipo){ @@ -4250,99 +4038,85 @@ static void dynamics_step(Object *ob, ParticleSystem *psys, ParticleSystemModifi } pa->size=psys_get_size(ob,ma,psmd,icu_esize,psys,part,pa,vg_size); - pa_die=0; + /* reactions can change birth time so they need to be checked first */ + if(psys->reactevents.first && ELEM(pa->alive,PARS_DEAD,PARS_KILLED)==0) + react_to_events(psys,p); birthtime = pa->time + pa->loop * pa->lifetime; + dietime = birthtime + pa->lifetime; + /* allways reset particles to emitter before birth */ if(pa->alive==PARS_UNBORN || pa->alive==PARS_KILLED || ELEM(part->phystype,PART_PHYS_NO,PART_PHYS_KEYED) || birthtime >= cfra){ - /* allways reset particles to emitter before birth */ reset_particle(pa,psys,psmd,ob,dtime,cfra,vg_vel,vg_tan,vg_rot); - copy_particle_key(key,&pa->state,1); } - if(1) { - - if(psys->reactevents.first && ELEM(pa->alive,PARS_DEAD,PARS_KILLED)==0) - react_to_events(psys,p); - - pa_dfra = dfra; - pa_dtime = dtime; - - dietime = birthtime + pa->lifetime; - - if(birthtime < cfra && birthtime >= psys->cfra){ - /* particle is born some time between this and last step*/ - pa->alive = PARS_ALIVE; - pa_dfra = cfra - birthtime; - pa_dtime = pa_dfra*timestep; - } - else if(dietime <= cfra && psys->cfra < dietime){ - /* particle dies some time between this and last step */ - pa_dfra = dietime - psys->cfra; - pa_dtime = pa_dfra * timestep; - pa_die = 1; - } - else if(dietime < cfra){ - /* TODO: figure out if there's something to be done when particle is dead */ - } - - copy_particle_key(key,&pa->state,1); + pa_dfra = dfra; + pa_dtime = dtime; - if(dfra>0.0 && pa->alive==PARS_ALIVE){ - switch(part->phystype){ - case PART_PHYS_NEWTON: - /* do global forces & effectors */ - apply_particle_forces(p,pa,ob,psys,part,timestep,pa_dfra,cfra,key); + if(birthtime <= cfra && birthtime >= psys->cfra){ + /* particle is born some time between this and last step*/ + pa->alive = PARS_ALIVE; + pa_dfra = cfra - birthtime; + pa_dtime = pa_dfra*timestep; + } + else if(dietime <= cfra && psys->cfra < dietime){ + /* particle dies some time between this and last step */ + pa_dfra = dietime - psys->cfra; + pa_dtime = pa_dfra * timestep; + pa->alive = PARS_DYING; + } + else if(dietime < cfra){ + /* nothing to be done when particle is dead */ + } - /* deflection */ - deflect_particle(ob,psmd,psys,part,pa,p,pa_dfra,cfra,key,&pa_die); - /* rotations */ - rotate_particle(part,pa,pa_dfra,timestep,key); + if(dfra>0.0 && ELEM(pa->alive,PARS_ALIVE,PARS_DYING)){ + switch(part->phystype){ + case PART_PHYS_NEWTON: + /* do global forces & effectors */ + apply_particle_forces(p,pa,ob,psys,part,timestep,pa_dfra,cfra); + + /* deflection */ + deflect_particle(ob,psmd,psys,part,pa,p,timestep,pa_dfra,cfra); - break; - case PART_PHYS_BOIDS: - { - float acc[3]; - boid_brain(&bvf,pa,ob,psys,part,tree,timestep,cfra,acc,&pa_die); - if(pa_die==0) - boid_body(&bvf,pa,psys,part,timestep,acc,key); - break; - } + /* rotations */ + rotate_particle(part,pa,pa_dfra,timestep); + break; + case PART_PHYS_BOIDS: + { + float acc[3]; + boid_brain(&bvf,pa,ob,psys,part,tree,timestep,cfra,acc); + if(pa->alive != PARS_DYING) + boid_body(&bvf,pa,psys,part,timestep,acc); + break; } + } - push_reaction(ob,psys,p,PART_EVENT_NEAR,key); - - if(pa_die){ - push_reaction(ob,psys,p,PART_EVENT_DEATH,key); + if(pa->alive == PARS_DYING){ + push_reaction(ob,psys,p,PART_EVENT_DEATH,&pa->state); - if(part->flag & PART_LOOP && part->type!=PART_HAIR){ - pa->loop++; - reset_particle(pa,psys,psmd,ob,0.0,cfra,vg_vel,vg_tan,vg_rot); - copy_particle_key(key,&pa->state,1); - pa->alive=PARS_ALIVE; - } - else{ - pa->alive=PARS_DEAD; - key->time=pa->dietime; + if(part->flag & PART_LOOP && part->type!=PART_HAIR){ + pa->loop++; + reset_particle(pa,psys,psmd,ob,0.0,cfra,vg_vel,vg_tan,vg_rot); + pa->alive=PARS_ALIVE; + } + else{ + pa->alive=PARS_DEAD; + pa->state.time=pa->dietime; - if(pa->flag&PARS_STICKY) - psys_key_to_object(pa->stick_ob,key,0); - } + if(pa->flag&PARS_STICKY) + psys_key_to_object(pa->stick_ob,&pa->state,0); } - else - key->time=cfra; } + else + pa->state.time=cfra; + + push_reaction(ob,psys,p,PART_EVENT_NEAR,&pa->state); } } - /* apply outstates to particles */ - for(p=0, pa=psys->particles, key=outstate; p<totpart; p++,pa++,key++) - copy_particle_key(&pa->state,key,1); - - MEM_freeN(outstate); } if(psys->reactevents.first) BLI_freelistN(&psys->reactevents); @@ -4380,7 +4154,7 @@ static void psys_update_path_cache(Object *ob, ParticleSystemModifierData *psmd, } if((part->type==PART_HAIR || psys->flag&PSYS_KEYED) && (psys_in_edit_mode(psys) - || (part->type==PART_HAIR || part->draw_as==PART_DRAW_PATH) || part->draw&PART_DRAW_KEYS)){ + || (part->type==PART_HAIR || part->draw_as==PART_DRAW_PATH))){ psys_cache_paths(ob, psys, cfra, 0); /* for render, child particle paths are computed on the fly */ @@ -4417,7 +4191,7 @@ static void hair_step(Object *ob, ParticleSystemModifierData *psmd, ParticleSyst psys_init_effectors(ob,part->eff_group,psys); if(psys->effectors.first) - precalc_effectors(ob,psys,psmd); + precalc_effectors(ob,psys,psmd,cfra); if(psys_in_edit_mode(psys)) PE_recalc_world_cos(ob, psys); @@ -4445,7 +4219,7 @@ static void cached_step(Object *ob, ParticleSystemModifierData *psmd, ParticleSy //if(part->flag & (PART_BAKED_GUIDES+PART_BAKED_DEATHS)){ psys_init_effectors(ob,part->eff_group,psys); if(psys->effectors.first) - precalc_effectors(ob,psys,psmd); + precalc_effectors(ob,psys,psmd,cfra); //} disp= (float)get_current_display_percentage(psys)/50.0f-1.0f; @@ -4543,7 +4317,7 @@ void psys_changed_type(ParticleSystem *psys) } static void particles_fluid_step(Object *ob, ParticleSystem *psys, int cfra) -{ +{ if(psys->particles){ MEM_freeN(psys->particles); psys->particles = 0; @@ -4552,94 +4326,98 @@ static void particles_fluid_step(Object *ob, ParticleSystem *psys, int cfra) /* fluid sim particle import handling, actual loading of particles from file */ #ifndef DISABLE_ELBEEM - if( (1) && (ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) && // broken, disabled for now! - (ob->fluidsimSettings)) { - ParticleSettings *part = psys->part; - ParticleData *pa=0; - char *suffix = "fluidsurface_particles_####"; - char *suffix2 = ".gz"; - char filename[256]; - char debugStrBuffer[256]; - int curFrame = G.scene->r.cfra -1; // warning - sync with derived mesh fsmesh loading - int p, j, numFileParts, totpart; - int readMask, activeParts = 0, fileParts = 0; - gzFile gzf; - - if(ob==G.obedit) // off... - return; - - // ok, start loading - strcpy(filename, ob->fluidsimSettings->surfdataPath); - strcat(filename, suffix); - BLI_convertstringcode(filename, G.sce); - BLI_convertstringframe(filename, curFrame); // fixed #frame-no - strcat(filename, suffix2); - - gzf = gzopen(filename, "rb"); - if (!gzf) { - snprintf(debugStrBuffer,256,"readFsPartData::error - Unable to open file for reading '%s' \n", filename); - //elbeemDebugOut(debugStrBuffer); - return; - } - - gzread(gzf, &totpart, sizeof(totpart)); - numFileParts = totpart; - totpart = (G.rendering)?totpart:(part->disp*totpart)/100; - - part->totpart= totpart; - part->sta=part->end = 1.0f; - part->lifetime = G.scene->r.efra + 1; - - /* initialize particles */ - realloc_particles(ob, psys, part->totpart); - initialize_all_particles(ob, psys, 0); - - // set up reading mask - readMask = ob->fluidsimSettings->typeFlags; + { + FluidsimModifierData *fluidmd = (FluidsimModifierData *)modifiers_findByType(ob, eModifierType_Fluidsim); - for(p=0, pa=psys->particles; p<totpart; p++, pa++) { - int ptype=0; - - gzread(gzf, &ptype, sizeof( ptype )); - if(ptype&readMask) { - activeParts++; - - gzread(gzf, &(pa->size), sizeof( float )); - - pa->size /= 10.0f; - - for(j=0; j<3; j++) { - float wrf; - gzread(gzf, &wrf, sizeof( wrf )); - pa->state.co[j] = wrf; - //fprintf(stderr,"Rj%d ",j); - } - for(j=0; j<3; j++) { - float wrf; - gzread(gzf, &wrf, sizeof( wrf )); - pa->state.vel[j] = wrf; - } - - pa->state.ave[0] = pa->state.ave[1] = pa->state.ave[2] = 0.0f; - pa->state.rot[0] = 1.0; - pa->state.rot[1] = pa->state.rot[2] = pa->state.rot[3] = 0.0; - - pa->alive = PARS_ALIVE; - //if(a<25) fprintf(stderr,"FSPARTICLE debug set %s , a%d = %f,%f,%f , life=%f \n", filename, a, pa->co[0],pa->co[1],pa->co[2], pa->lifetime ); - } else { - // skip... - for(j=0; j<2*3+1; j++) { - float wrf; gzread(gzf, &wrf, sizeof( wrf )); + if( fluidmd && fluidmd->fss) { + FluidsimSettings *fss= fluidmd->fss; + ParticleSettings *part = psys->part; + ParticleData *pa=0; + char *suffix = "fluidsurface_particles_####"; + char *suffix2 = ".gz"; + char filename[256]; + char debugStrBuffer[256]; + int curFrame = G.scene->r.cfra -1; // warning - sync with derived mesh fsmesh loading + int p, j, numFileParts, totpart; + int readMask, activeParts = 0, fileParts = 0; + gzFile gzf; + + if(ob==G.obedit) // off... + return; + + // ok, start loading + strcpy(filename, fss->surfdataPath); + strcat(filename, suffix); + BLI_convertstringcode(filename, G.sce); + BLI_convertstringframe(filename, curFrame); // fixed #frame-no + strcat(filename, suffix2); + + gzf = gzopen(filename, "rb"); + if (!gzf) { + snprintf(debugStrBuffer,256,"readFsPartData::error - Unable to open file for reading '%s' \n", filename); + //elbeemDebugOut(debugStrBuffer); + return; + } + + gzread(gzf, &totpart, sizeof(totpart)); + numFileParts = totpart; + totpart = (G.rendering)?totpart:(part->disp*totpart)/100; + + part->totpart= totpart; + part->sta=part->end = 1.0f; + part->lifetime = G.scene->r.efra + 1; + + /* initialize particles */ + realloc_particles(ob, psys, part->totpart); + initialize_all_particles(ob, psys, 0); + + // set up reading mask + readMask = fss->typeFlags; + + for(p=0, pa=psys->particles; p<totpart; p++, pa++) { + int ptype=0; + + gzread(gzf, &ptype, sizeof( ptype )); + if(ptype&readMask) { + activeParts++; + + gzread(gzf, &(pa->size), sizeof( float )); + + pa->size /= 10.0f; + + for(j=0; j<3; j++) { + float wrf; + gzread(gzf, &wrf, sizeof( wrf )); + pa->state.co[j] = wrf; + //fprintf(stderr,"Rj%d ",j); + } + for(j=0; j<3; j++) { + float wrf; + gzread(gzf, &wrf, sizeof( wrf )); + pa->state.vel[j] = wrf; + } + + pa->state.ave[0] = pa->state.ave[1] = pa->state.ave[2] = 0.0f; + pa->state.rot[0] = 1.0; + pa->state.rot[1] = pa->state.rot[2] = pa->state.rot[3] = 0.0; + + pa->alive = PARS_ALIVE; + //if(a<25) fprintf(stderr,"FSPARTICLE debug set %s , a%d = %f,%f,%f , life=%f \n", filename, a, pa->co[0],pa->co[1],pa->co[2], pa->lifetime ); + } else { + // skip... + for(j=0; j<2*3+1; j++) { + float wrf; gzread(gzf, &wrf, sizeof( wrf )); + } } + fileParts++; } - fileParts++; - } - gzclose( gzf ); - - totpart = psys->totpart = activeParts; - snprintf(debugStrBuffer,256,"readFsPartData::done - particles:%d, active:%d, file:%d, mask:%d \n", psys->totpart,activeParts,fileParts,readMask); - elbeemDebugOut(debugStrBuffer); - } // fluid sim particles done + gzclose( gzf ); + + totpart = psys->totpart = activeParts; + snprintf(debugStrBuffer,256,"readFsPartData::done - particles:%d, active:%d, file:%d, mask:%d \n", psys->totpart,activeParts,fileParts,readMask); + elbeemDebugOut(debugStrBuffer); + } // fluid sim particles done + } #endif // DISABLE_ELBEEM } @@ -4686,10 +4464,8 @@ static void system_step(Object *ob, ParticleSystem *psys, ParticleSystemModifier return; } - /* cache shouldn't be used for hair or "none" or "first keyed" physics */ - if(part->type == PART_HAIR || part->phystype == PART_PHYS_NO) - usecache= 0; - else if(part->type == PART_PHYS_KEYED && (psys->flag & PSYS_FIRST_KEYED)) + /* cache shouldn't be used for hair or "none" or "keyed" physics */ + if(part->type == PART_HAIR || ELEM(part->phystype, PART_PHYS_NO, PART_PHYS_KEYED)) usecache= 0; else if(BKE_ptcache_get_continue_physics()) usecache= 0; @@ -4699,7 +4475,7 @@ static void system_step(Object *ob, ParticleSystem *psys, ParticleSystemModifier if(usecache) { /* frame clamping */ if(framenr < startframe) { - psys_reset(psys, PSYS_RESET_DEPSGRAPH); + psys_reset(psys, PSYS_RESET_CACHE_MISS); psys->cfra = cfra; psys->recalc = 0; return; @@ -4779,14 +4555,14 @@ static void system_step(Object *ob, ParticleSystem *psys, ParticleSystemModifier return; } else if(ob->id.lib || (cache->flag & PTCACHE_BAKED)) { - psys_reset(psys, PSYS_RESET_DEPSGRAPH); + psys_reset(psys, PSYS_RESET_CACHE_MISS); psys->cfra=cfra; psys->recalc = 0; return; } if(framenr != startframe && framedelta != 1) { - psys_reset(psys, PSYS_RESET_DEPSGRAPH); + psys_reset(psys, PSYS_RESET_CACHE_MISS); psys->cfra = cfra; psys->recalc = 0; return; @@ -4867,7 +4643,7 @@ static void system_step(Object *ob, ParticleSystem *psys, ParticleSystemModifier } } -void psys_to_softbody(Object *ob, ParticleSystem *psys) +static void psys_to_softbody(Object *ob, ParticleSystem *psys) { SoftBody *sb; short softflag; diff --git a/source/blender/blenkernel/intern/property.c b/source/blender/blenkernel/intern/property.c index f55bdd15279..d2eb058a9a0 100644 --- a/source/blender/blenkernel/intern/property.c +++ b/source/blender/blenkernel/intern/property.c @@ -83,8 +83,7 @@ bProperty *copy_property(bProperty *prop) void copy_properties(ListBase *lbn, ListBase *lbo) { bProperty *prop, *propn; - - lbn->first= lbn->last= 0; + free_properties( lbn ); /* incase we are copying to an object with props */ prop= lbo->first; while(prop) { propn= copy_property(prop); @@ -139,7 +138,7 @@ bProperty *new_property(int type) return prop; } -bProperty *get_property(Object *ob, char *name) +bProperty *get_ob_property(Object *ob, char *name) { bProperty *prop; @@ -151,6 +150,17 @@ bProperty *get_property(Object *ob, char *name) return NULL; } +void set_ob_property(Object *ob, bProperty *propc) +{ + bProperty *prop; + prop= get_ob_property(ob, propc->name); + if(prop) { + free_property(prop); + BLI_remlink(&ob->prop, prop); + } + BLI_addtail(&ob->prop, copy_property(propc)); +} + /* negative: prop is smaller * positive: prop is larger */ diff --git a/source/blender/blenkernel/intern/sca.c b/source/blender/blenkernel/intern/sca.c index fcf1c7ce311..47d11bb9d29 100644 --- a/source/blender/blenkernel/intern/sca.c +++ b/source/blender/blenkernel/intern/sca.c @@ -153,6 +153,9 @@ void init_sensor(bSensor *sens) case SENS_ACTUATOR: sens->data= MEM_callocN(sizeof(bActuatorSensor), "actsens"); break; + case SENS_DELAY: + sens->data= MEM_callocN(sizeof(bDelaySensor), "delaysens"); + break; case SENS_MOUSE: ms=sens->data= MEM_callocN(sizeof(bMouseSensor), "mousesens"); ms->type= LEFTMOUSE; diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c index 6102ee9a5d7..7d864c83d2c 100644 --- a/source/blender/blenkernel/intern/scene.c +++ b/source/blender/blenkernel/intern/scene.c @@ -188,6 +188,7 @@ Scene *add_scene(char *name) sce->lay= 1; sce->selectmode= SCE_SELECT_VERTEX; sce->editbutsize= 0.1; + sce->autokey_mode= U.autokey_mode; sce->r.mode= R_GAMMA; sce->r.cfra= 1; @@ -365,7 +366,7 @@ void set_scene_bg(Scene *sce) /* no full animation update, this to enable render code to work (render code calls own animation updates) */ /* do we need FRAMECHANGED in set_scene? */ -// if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED); +// if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED, 0); } /* called from creator.c */ @@ -585,8 +586,8 @@ void scene_update_for_newframe(Scene *sce, unsigned int lay) /* object ipos are calculated in where_is_object */ do_all_data_ipos(); - if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED); - + if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED, 0); + /* sets first, we allow per definition current scene to have dependencies on sets */ for(sce= sce->set; sce; sce= sce->set) scene_update(sce, lay); diff --git a/source/blender/blenkernel/intern/shrinkwrap.c b/source/blender/blenkernel/intern/shrinkwrap.c new file mode 100644 index 00000000000..76af9a763ee --- /dev/null +++ b/source/blender/blenkernel/intern/shrinkwrap.c @@ -0,0 +1,588 @@ +/** + * shrinkwrap.c + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): André Pinto + * + * ***** END GPL LICENSE BLOCK ***** + */ +#include <string.h> +#include <float.h> +#include <math.h> +#include <memory.h> +#include <stdio.h> +#include <time.h> +#include <assert.h> + +#include "DNA_object_types.h" +#include "DNA_modifier_types.h" +#include "DNA_meshdata_types.h" +#include "DNA_mesh_types.h" + +#include "BKE_shrinkwrap.h" +#include "BKE_DerivedMesh.h" +#include "BKE_utildefines.h" +#include "BKE_deform.h" +#include "BKE_cdderivedmesh.h" +#include "BKE_displist.h" +#include "BKE_global.h" +#include "BKE_subsurf.h" + +#include "BLI_arithb.h" +#include "BLI_kdtree.h" +#include "BLI_kdopbvh.h" + +#include "RE_raytrace.h" +#include "MEM_guardedalloc.h" + + +/* Util macros */ +#define TO_STR(a) #a +#define JOIN(a,b) a##b + +#define OUT_OF_MEMORY() ((void)printf("Shrinkwrap: Out of memory\n")) + +/* Benchmark macros */ +#if !defined(_WIN32) && 0 + +#include <sys/time.h> + +#define BENCH(a) \ + do { \ + double _t1, _t2; \ + struct timeval _tstart, _tend; \ + clock_t _clock_init = clock(); \ + gettimeofday ( &_tstart, NULL); \ + (a); \ + gettimeofday ( &_tend, NULL); \ + _t1 = ( double ) _tstart.tv_sec + ( double ) _tstart.tv_usec/ ( 1000*1000 ); \ + _t2 = ( double ) _tend.tv_sec + ( double ) _tend.tv_usec/ ( 1000*1000 ); \ + printf("%s: %fs (real) %fs (cpu)\n", #a, _t2-_t1, (float)(clock()-_clock_init)/CLOCKS_PER_SEC);\ + } while(0) + +#else + +#define BENCH(a) (a) + +#endif + +typedef void ( *Shrinkwrap_ForeachVertexCallback) (DerivedMesh *target, float *co, float *normal); + +/* get derived mesh */ +//TODO is anyfunction that does this? returning the derivedFinal witouth we caring if its in edit mode or not? +DerivedMesh *object_get_derived_final(Object *ob, CustomDataMask dataMask) +{ + if (ob==G.obedit) + { + DerivedMesh *final = NULL; + editmesh_get_derived_cage_and_final(&final, dataMask); + return final; + } + else + return mesh_get_derived_final(ob, dataMask); +} + +/* Space transform */ +void space_transform_from_matrixs(SpaceTransform *data, float local[4][4], float target[4][4]) +{ + float itarget[4][4]; + Mat4Invert(itarget, target); + Mat4MulSerie(data->local2target, itarget, local, 0, 0, 0, 0, 0, 0); + Mat4Invert(data->target2local, data->local2target); +} + +void space_transform_apply(const SpaceTransform *data, float *co) +{ + VecMat4MulVecfl(co, ((SpaceTransform*)data)->local2target, co); +} + +void space_transform_invert(const SpaceTransform *data, float *co) +{ + VecMat4MulVecfl(co, ((SpaceTransform*)data)->target2local, co); +} + +void space_transform_apply_normal(const SpaceTransform *data, float *no) +{ + Mat4Mul3Vecfl( ((SpaceTransform*)data)->local2target, no); + Normalize(no); // TODO: could we just determine de scale value from the matrix? +} + +void space_transform_invert_normal(const SpaceTransform *data, float *no) +{ + Mat4Mul3Vecfl(((SpaceTransform*)data)->target2local, no); + Normalize(no); // TODO: could we just determine de scale value from the matrix? +} + +/* + * Returns the squared distance between two given points + */ +static float squared_dist(const float *a, const float *b) +{ + float tmp[3]; + VECSUB(tmp, a, b); + return INPR(tmp, tmp); +} + +/* Main shrinkwrap function */ +void shrinkwrapModifier_deform(ShrinkwrapModifierData *smd, Object *ob, DerivedMesh *dm, float (*vertexCos)[3], int numVerts) +{ + + ShrinkwrapCalcData calc = NULL_ShrinkwrapCalcData; + + //remove loop dependencies on derived meshs (TODO should this be done elsewhere?) + if(smd->target == ob) smd->target = NULL; + if(smd->auxTarget == ob) smd->auxTarget = NULL; + + + //Configure Shrinkwrap calc data + calc.smd = smd; + calc.ob = ob; + calc.original = dm; + calc.numVerts = numVerts; + calc.vertexCos = vertexCos; + + if(smd->target) + { + //TODO currently we need a copy in case object_get_derived_final returns an emDM that does not defines getVertArray or getFace array + calc.target = CDDM_copy( object_get_derived_final(smd->target, CD_MASK_BAREMESH) ); + + //TODO there might be several "bugs" on non-uniform scales matrixs.. because it will no longer be nearest surface, not sphere projection + //because space has been deformed + space_transform_setup(&calc.local2target, ob, smd->target); + + calc.keepDist = smd->keepDist; //TODO: smd->keepDist is in global units.. must change to local + } + + + //Projecting target defined - lets work! + if(calc.target) + { + switch(smd->shrinkType) + { + case MOD_SHRINKWRAP_NEAREST_SURFACE: + BENCH(shrinkwrap_calc_nearest_surface_point(&calc)); + break; + + case MOD_SHRINKWRAP_PROJECT: + BENCH(shrinkwrap_calc_normal_projection(&calc)); + break; + + case MOD_SHRINKWRAP_NEAREST_VERTEX: + BENCH(shrinkwrap_calc_nearest_vertex(&calc)); + break; + } + } + + //free memory + if(calc.target) + calc.target->release( calc.target ); +} + +/* + * Shrinkwrap to the nearest vertex + * + * it builds a kdtree of vertexs we can attach to and then + * for each vertex performs a nearest vertex search on the tree + */ +void shrinkwrap_calc_nearest_vertex(ShrinkwrapCalcData *calc) +{ + int i; + const int vgroup = get_named_vertexgroup_num(calc->ob, calc->smd->vgroup_name); + MDeformVert *const dvert = calc->original ? calc->original->getVertDataArray(calc->original, CD_MDEFORMVERT) : NULL; + + BVHTreeFromMesh treeData = NULL_BVHTreeFromMesh; + BVHTreeNearest nearest = NULL_BVHTreeNearest; + + + BENCH(bvhtree_from_mesh_verts(&treeData, calc->target, 0.0, 2, 6)); + if(treeData.tree == NULL) return OUT_OF_MEMORY(); + + //Setup nearest + nearest.index = -1; + nearest.dist = FLT_MAX; + +#pragma omp parallel for default(none) private(i) firstprivate(nearest) shared(treeData,calc) schedule(static) + for(i = 0; i<calc->numVerts; ++i) + { + float *co = calc->vertexCos[i]; + float tmp_co[3]; + float weight = vertexgroup_get_vertex_weight(dvert, i, vgroup); + if(weight == 0.0f) continue; + + VECCOPY(tmp_co, co); + space_transform_apply(&calc->local2target, tmp_co); //Convert the coordinates to the tree coordinates + + //Use local proximity heuristics (to reduce the nearest search) + // + //If we already had an hit before.. we assume this vertex is going to have a close hit to that other vertex + //so we can initiate the "nearest.dist" with the expected value to that last hit. + //This will lead in prunning of the search tree. + if(nearest.index != -1) + nearest.dist = squared_dist(tmp_co, nearest.co); + else + nearest.dist = FLT_MAX; + + BLI_bvhtree_find_nearest(treeData.tree, tmp_co, &nearest, treeData.nearest_callback, &treeData); + + + //Found the nearest vertex + if(nearest.index != -1) + { + //Adjusting the vertex weight, so that after interpolating it keeps a certain distance from the nearest position + float dist = sasqrt(nearest.dist); + if(dist > FLT_EPSILON) weight *= (dist - calc->keepDist)/dist; + + //Convert the coordinates back to mesh coordinates + VECCOPY(tmp_co, nearest.co); + space_transform_invert(&calc->local2target, tmp_co); + + VecLerpf(co, co, tmp_co, weight); //linear interpolation + } + } + + free_bvhtree_from_mesh(&treeData); +} + +/* + * This function raycast a single vertex and updates the hit if the "hit" is considered valid. + * Returns TRUE if "hit" was updated. + * Opts control whether an hit is valid or not + * Supported options are: + * MOD_SHRINKWRAP_CULL_TARGET_FRONTFACE (front faces hits are ignored) + * MOD_SHRINKWRAP_CULL_TARGET_BACKFACE (back faces hits are ignored) + */ +int normal_projection_project_vertex(char options, const float *vert, const float *dir, const SpaceTransform *transf, BVHTree *tree, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata) +{ + float tmp_co[3], tmp_no[3]; + const float *co, *no; + BVHTreeRayHit hit_tmp; + + //Copy from hit (we need to convert hit rays from one space coordinates to the other + memcpy( &hit_tmp, hit, sizeof(hit_tmp) ); + + //Apply space transform (TODO readjust dist) + if(transf) + { + VECCOPY( tmp_co, vert ); + space_transform_apply( transf, tmp_co ); + co = tmp_co; + + VECCOPY( tmp_no, dir ); + space_transform_apply_normal( transf, tmp_no ); + no = tmp_no; + + hit_tmp.dist *= Mat4ToScalef( ((SpaceTransform*)transf)->local2target ); + } + else + { + co = vert; + no = dir; + } + + hit_tmp.index = -1; + + BLI_bvhtree_ray_cast(tree, co, no, 0.0f, &hit_tmp, callback, userdata); + + if(hit_tmp.index != -1) + { + float dot = INPR( dir, hit_tmp.no); + + if(((options & MOD_SHRINKWRAP_CULL_TARGET_FRONTFACE) && dot <= 0.0f) + || ((options & MOD_SHRINKWRAP_CULL_TARGET_BACKFACE) && dot >= 0.0f)) + return FALSE; //Ignore hit + + + //Inverting space transform (TODO make coeherent with the initial dist readjust) + if(transf) + { + space_transform_invert( transf, hit_tmp.co ); + space_transform_invert_normal( transf, hit_tmp.no ); + + hit_tmp.dist = VecLenf( (float*)vert, hit_tmp.co ); + } + + memcpy(hit, &hit_tmp, sizeof(hit_tmp) ); + return TRUE; + } + return FALSE; +} + + +void shrinkwrap_calc_normal_projection(ShrinkwrapCalcData *calc) +{ + int i; + + //Options about projection direction + const char use_normal = calc->smd->shrinkOpts; + float proj_axis[3] = {0.0f, 0.0f, 0.0f}; + MVert *vert = NULL; //Needed in case of vertex normal + DerivedMesh* ss_mesh = NULL; + + //Vertex group data + const int vgroup = get_named_vertexgroup_num(calc->ob, calc->smd->vgroup_name); + const MDeformVert *dvert = calc->original ? calc->original->getVertDataArray(calc->original, CD_MDEFORMVERT) : NULL; + + + //Raycast and tree stuff + BVHTreeRayHit hit; + BVHTreeFromMesh treeData = NULL_BVHTreeFromMesh; //target + + //auxiliar target + DerivedMesh * aux_mesh = NULL; + BVHTreeFromMesh auxData= NULL_BVHTreeFromMesh; + SpaceTransform local2aux; + +do +{ + + //Prepare data to retrieve the direction in which we should project each vertex + if(calc->smd->projAxis == MOD_SHRINKWRAP_PROJECT_OVER_NORMAL) + { + //No Mvert information: jump to "free memory and return" part + if(calc->original == NULL) break; + + if(calc->smd->subsurfLevels) + { + SubsurfModifierData smd; + memset(&smd, 0, sizeof(smd)); + smd.subdivType = ME_CC_SUBSURF; //catmull clark + smd.levels = calc->smd->subsurfLevels; //levels + + ss_mesh = subsurf_make_derived_from_derived(calc->original, &smd, FALSE, NULL, 0, 0); + + if(ss_mesh) + { + vert = ss_mesh->getVertDataArray(ss_mesh, CD_MVERT); + if(vert) + { + //TRICKY: this code assumes subsurface will have the transformed original vertices + //in their original order at the end of the vert array. + vert = vert + + ss_mesh->getNumVerts(ss_mesh) + - calc->original->getNumVerts(calc->original); + } + } + + //To make sure we are not letting any memory behind + assert(smd.emCache == NULL); + assert(smd.mCache == NULL); + } + else + vert = calc->original->getVertDataArray(calc->original, CD_MVERT); + + //Not able to get vert information: jump to "free memory and return" part + if(vert == NULL) break; + } + else + { + //The code supports any axis that is a combination of X,Y,Z.. altought currently UI only allows to set the 3 diferent axis + if(calc->smd->projAxis & MOD_SHRINKWRAP_PROJECT_OVER_X_AXIS) proj_axis[0] = 1.0f; + if(calc->smd->projAxis & MOD_SHRINKWRAP_PROJECT_OVER_Y_AXIS) proj_axis[1] = 1.0f; + if(calc->smd->projAxis & MOD_SHRINKWRAP_PROJECT_OVER_Z_AXIS) proj_axis[2] = 1.0f; + + Normalize(proj_axis); + + //Invalid projection direction: jump to "free memory and return" part + if(INPR(proj_axis, proj_axis) < FLT_EPSILON) break; + } + + //If the user doesn't allows to project in any direction of projection axis... then theres nothing todo. + if((use_normal & (MOD_SHRINKWRAP_PROJECT_ALLOW_POS_DIR | MOD_SHRINKWRAP_PROJECT_ALLOW_NEG_DIR)) == 0) + break; //jump to "free memory and return" part + + + //Build target tree + BENCH(bvhtree_from_mesh_faces(&treeData, calc->target, calc->keepDist, 4, 6)); + if(treeData.tree == NULL) + break; //jump to "free memory and return" part + + + //Build auxiliar target + if(calc->smd->auxTarget) + { + space_transform_setup( &local2aux, calc->ob, calc->smd->auxTarget); + + aux_mesh = CDDM_copy( object_get_derived_final(calc->smd->auxTarget, CD_MASK_BAREMESH) ); //TODO currently we need a copy in case object_get_derived_final returns an emDM that does not defines getVertArray or getFace array + if(aux_mesh) + BENCH(bvhtree_from_mesh_faces(&auxData, aux_mesh, 0.0, 4, 6)); + else + printf("Auxiliar target finalDerived mesh is null\n"); + } + + + //Now, everything is ready to project the vertexs! +#pragma omp parallel for private(i,hit) schedule(static) + for(i = 0; i<calc->numVerts; ++i) + { + float *co = calc->vertexCos[i]; + float tmp_co[3], tmp_no[3]; + float lim = 10000.0f; //TODO: we should use FLT_MAX here, but sweepsphere code isnt prepared for that + float weight = vertexgroup_get_vertex_weight(dvert, i, vgroup); + + if(weight == 0.0f) continue; + + if(ss_mesh) + { + VECCOPY(tmp_co, vert[i].co); + } + else + { + VECCOPY(tmp_co, co); + } + + + if(vert) + NormalShortToFloat(tmp_no, vert[i].no); + else + VECCOPY( tmp_no, proj_axis ); + + + hit.index = -1; + hit.dist = lim; + + + //Project over positive direction of axis + if(use_normal & MOD_SHRINKWRAP_PROJECT_ALLOW_POS_DIR) + { + + if(auxData.tree) + normal_projection_project_vertex(0, tmp_co, tmp_no, &local2aux, auxData.tree, &hit, auxData.raycast_callback, &auxData); + + normal_projection_project_vertex(calc->smd->shrinkOpts, tmp_co, tmp_no, &calc->local2target, treeData.tree, &hit, treeData.raycast_callback, &treeData); + } + + //Project over negative direction of axis + if(use_normal & MOD_SHRINKWRAP_PROJECT_ALLOW_NEG_DIR) + { + float inv_no[3] = { -tmp_no[0], -tmp_no[1], -tmp_no[2] }; + + + if(auxData.tree) + normal_projection_project_vertex(0, tmp_co, inv_no, &local2aux, auxData.tree, &hit, auxData.raycast_callback, &auxData); + + normal_projection_project_vertex(calc->smd->shrinkOpts, tmp_co, inv_no, &calc->local2target, treeData.tree, &hit, treeData.raycast_callback, &treeData); + } + + + if(hit.index != -1) + { + VecLerpf(co, co, hit.co, weight); + } + } + + +//Simple do{} while(0) structure to allow to easily jump to the "free memory and return" part +} while(0); + + //free data structures + + free_bvhtree_from_mesh(&treeData); + free_bvhtree_from_mesh(&auxData); + + if(aux_mesh) + aux_mesh->release(aux_mesh); + + if(ss_mesh) + ss_mesh->release(ss_mesh); +} + +/* + * Shrinkwrap moving vertexs to the nearest surface point on the target + * + * it builds a BVHTree from the target mesh and then performs a + * NN matchs for each vertex + */ +void shrinkwrap_calc_nearest_surface_point(ShrinkwrapCalcData *calc) +{ + int i; + + const int vgroup = get_named_vertexgroup_num(calc->ob, calc->smd->vgroup_name); + const MDeformVert *const dvert = calc->original ? calc->original->getVertDataArray(calc->original, CD_MDEFORMVERT) : NULL; + + BVHTreeFromMesh treeData = NULL_BVHTreeFromMesh; + BVHTreeNearest nearest = NULL_BVHTreeNearest; + + + + //Create a bvh-tree of the given target + BENCH(bvhtree_from_mesh_faces( &treeData, calc->target, 0.0, 2, 6)); + if(treeData.tree == NULL) return OUT_OF_MEMORY(); + + //Setup nearest + nearest.index = -1; + nearest.dist = FLT_MAX; + + + //Find the nearest vertex +#pragma omp parallel for default(none) private(i) firstprivate(nearest) shared(calc,treeData) schedule(static) + for(i = 0; i<calc->numVerts; ++i) + { + float *co = calc->vertexCos[i]; + float tmp_co[3]; + float weight = vertexgroup_get_vertex_weight(dvert, i, vgroup); + if(weight == 0.0f) continue; + + //Convert the vertex to tree coordinates + VECCOPY(tmp_co, co); + space_transform_apply(&calc->local2target, tmp_co); + + //Use local proximity heuristics (to reduce the nearest search) + // + //If we already had an hit before.. we assume this vertex is going to have a close hit to that other vertex + //so we can initiate the "nearest.dist" with the expected value to that last hit. + //This will lead in prunning of the search tree. + if(nearest.index != -1) + nearest.dist = squared_dist(tmp_co, nearest.co); + else + nearest.dist = FLT_MAX; + + BLI_bvhtree_find_nearest(treeData.tree, tmp_co, &nearest, treeData.nearest_callback, &treeData); + + //Found the nearest vertex + if(nearest.index != -1) + { + if(calc->smd->shrinkOpts & MOD_SHRINKWRAP_KEEP_ABOVE_SURFACE) + { + //Make the vertex stay on the front side of the face + VECADDFAC(tmp_co, nearest.co, nearest.no, calc->keepDist); + } + else + { + //Adjusting the vertex weight, so that after interpolating it keeps a certain distance from the nearest position + float dist = sasqrt( nearest.dist ); + if(dist > FLT_EPSILON) + VecLerpf(tmp_co, tmp_co, nearest.co, (dist - calc->keepDist)/dist); //linear interpolation + else + VECCOPY( tmp_co, nearest.co ); + } + + //Convert the coordinates back to mesh coordinates + space_transform_invert(&calc->local2target, tmp_co); + VecLerpf(co, co, tmp_co, weight); //linear interpolation + } + } + + + free_bvhtree_from_mesh(&treeData); +} + diff --git a/source/blender/blenkernel/intern/simple_deform.c b/source/blender/blenkernel/intern/simple_deform.c new file mode 100644 index 00000000000..0eb710fa48e --- /dev/null +++ b/source/blender/blenkernel/intern/simple_deform.c @@ -0,0 +1,248 @@ +/** + * deform_simple.c + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): André Pinto + * + * ***** END GPL LICENSE BLOCK ***** + */ +#include "DNA_object_types.h" +#include "DNA_modifier_types.h" +#include "DNA_meshdata_types.h" + +#include "BKE_simple_deform.h" +#include "BKE_DerivedMesh.h" +#include "BKE_deform.h" +#include "BKE_utildefines.h" +#include "BLI_arithb.h" +#include "BKE_shrinkwrap.h" + +#include <string.h> +#include <math.h> + + +//Clamps/Limits the given coordinate to: limits[0] <= co[axis] <= limits[1] +//The ammount of clamp is saved on dcut +static void axis_limit(int axis, const float limits[2], float co[3], float dcut[3]) +{ + float val = co[axis]; + if(limits[0] > val) val = limits[0]; + if(limits[1] < val) val = limits[1]; + + dcut[axis] = co[axis] - val; + co[axis] = val; +} + +static void simpleDeform_taper(const float factor, const float dcut[3], float *co) +{ + float x = co[0], y = co[1], z = co[2]; + float scale = z*factor; + + co[0] = x + x*scale; + co[1] = y + y*scale; + co[2] = z; + + if(dcut) + { + co[0] += dcut[0]; + co[1] += dcut[1]; + co[2] += dcut[2]; + } +} + +static void simpleDeform_stretch(const float factor, const float dcut[3], float *co) +{ + float x = co[0], y = co[1], z = co[2]; + float scale; + + scale = (z*z*factor-factor + 1.0); + + co[0] = x*scale; + co[1] = y*scale; + co[2] = z*(1.0+factor); + + + if(dcut) + { + co[0] += dcut[0]; + co[1] += dcut[1]; + co[2] += dcut[2]; + } +} + +static void simpleDeform_twist(const float factor, const float *dcut, float *co) +{ + float x = co[0], y = co[1], z = co[2]; + float theta, sint, cost; + + theta = z*factor; + sint = sin(theta); + cost = cos(theta); + + co[0] = x*cost - y*sint; + co[1] = x*sint + y*cost; + co[2] = z; + + if(dcut) + { + co[0] += dcut[0]; + co[1] += dcut[1]; + co[2] += dcut[2]; + } +} + +static void simpleDeform_bend(const float factor, const float dcut[3], float *co) +{ + float x = co[0], y = co[1], z = co[2]; + float theta, sint, cost; + + theta = x*factor; + sint = sin(theta); + cost = cos(theta); + + if(fabs(factor) > 1e-7f) + { + co[0] = -(y-1.0f/factor)*sint; + co[1] = (y-1.0f/factor)*cost + 1.0f/factor; + co[2] = z; + } + + + if(dcut) + { + co[0] += cost*dcut[0]; + co[1] += sint*dcut[0]; + co[2] += dcut[2]; + } + +} + + +/* simple deform modifier */ +void SimpleDeformModifier_do(SimpleDeformModifierData *smd, struct Object *ob, struct DerivedMesh *dm, float (*vertexCos)[3], int numVerts) +{ + static const float lock_axis[2] = {0.0f, 0.0f}; + + int i; + int limit_axis = 0; + float smd_limit[2], smd_factor; + SpaceTransform *transf = NULL, tmp_transf; + void (*simpleDeform_callback)(const float factor, const float dcut[3], float *co) = NULL; //Mode callback + int vgroup = get_named_vertexgroup_num(ob, smd->vgroup_name); + MDeformVert *dvert = NULL; + + //Safe-check + if(smd->origin == ob) smd->origin = NULL; //No self references + + if(smd->limit[0] < 0.0) smd->limit[0] = 0.0f; + if(smd->limit[0] > 1.0) smd->limit[0] = 1.0f; + + smd->limit[0] = MIN2(smd->limit[0], smd->limit[1]); //Upper limit >= than lower limit + + //Calculate matrixs do convert between coordinate spaces + if(smd->origin) + { + transf = &tmp_transf; + + if(smd->originOpts & MOD_SIMPLEDEFORM_ORIGIN_LOCAL) + { + space_transform_from_matrixs(transf, ob->obmat, smd->origin->obmat); + } + else + { + Mat4CpyMat4(transf->local2target, smd->origin->obmat); + Mat4Invert(transf->target2local, transf->local2target); + } + } + + //Setup vars + limit_axis = (smd->mode == MOD_SIMPLEDEFORM_MODE_BEND) ? 0 : 2; //Bend limits on X.. all other modes limit on Z + + //Update limits if needed + { + float lower = FLT_MAX; + float upper = -FLT_MAX; + + for(i=0; i<numVerts; i++) + { + float tmp[3]; + VECCOPY(tmp, vertexCos[i]); + + if(transf) space_transform_apply(transf, tmp); + + lower = MIN2(lower, tmp[limit_axis]); + upper = MAX2(upper, tmp[limit_axis]); + } + + + //SMD values are normalized to the BV, calculate the absolut values + smd_limit[1] = lower + (upper-lower)*smd->limit[1]; + smd_limit[0] = lower + (upper-lower)*smd->limit[0]; + + smd_factor = smd->factor / MAX2(FLT_EPSILON, smd_limit[1]-smd_limit[0]); + } + + + if(dm) + dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT); + + + switch(smd->mode) + { + case MOD_SIMPLEDEFORM_MODE_TWIST: simpleDeform_callback = simpleDeform_twist; break; + case MOD_SIMPLEDEFORM_MODE_BEND: simpleDeform_callback = simpleDeform_bend; break; + case MOD_SIMPLEDEFORM_MODE_TAPER: simpleDeform_callback = simpleDeform_taper; break; + case MOD_SIMPLEDEFORM_MODE_STRETCH: simpleDeform_callback = simpleDeform_stretch; break; + default: + return; //No simpledeform mode? + } + + for(i=0; i<numVerts; i++) + { + float weight = vertexgroup_get_vertex_weight(dvert, i, vgroup); + + if(weight != 0.0f) + { + float co[3], dcut[3] = {0.0f, 0.0f, 0.0f}; + + if(transf) space_transform_apply(transf, vertexCos[i]); + + VECCOPY(co, vertexCos[i]); + + //Apply axis limits + if(smd->mode != MOD_SIMPLEDEFORM_MODE_BEND) //Bend mode shoulnt have any lock axis + { + if(smd->axis & MOD_SIMPLEDEFORM_LOCK_AXIS_X) axis_limit(0, lock_axis, co, dcut); + if(smd->axis & MOD_SIMPLEDEFORM_LOCK_AXIS_Y) axis_limit(1, lock_axis, co, dcut); + } + axis_limit(limit_axis, smd_limit, co, dcut); + + simpleDeform_callback(smd_factor, dcut, co); //Apply deform + VecLerpf(vertexCos[i], vertexCos[i], co, weight); //Use vertex weight has coef of linear interpolation + + if(transf) space_transform_invert(transf, vertexCos[i]); + } + } +} + + diff --git a/source/blender/blenkernel/intern/softbody.c b/source/blender/blenkernel/intern/softbody.c index d465c058d30..5d93f10526a 100644 --- a/source/blender/blenkernel/intern/softbody.c +++ b/source/blender/blenkernel/intern/softbody.c @@ -236,7 +236,7 @@ typedef struct ccd_Mesh { -ccd_Mesh *ccd_mesh_make(Object *ob, DerivedMesh *dm) +static ccd_Mesh *ccd_mesh_make(Object *ob, DerivedMesh *dm) { ccd_Mesh *pccd_M = NULL; ccdf_minmax *mima =NULL; @@ -333,7 +333,7 @@ ccd_Mesh *ccd_mesh_make(Object *ob, DerivedMesh *dm) } return pccd_M; } -void ccd_mesh_update(Object *ob,ccd_Mesh *pccd_M, DerivedMesh *dm) +static void ccd_mesh_update(Object *ob,ccd_Mesh *pccd_M, DerivedMesh *dm) { ccdf_minmax *mima =NULL; MFace *mface=NULL; @@ -472,7 +472,7 @@ void ccd_mesh_update(Object *ob,ccd_Mesh *pccd_M, DerivedMesh *dm) return ; } -void ccd_mesh_free(ccd_Mesh *ccdm) +static void ccd_mesh_free(ccd_Mesh *ccdm) { if(ccdm && (ccdm->savety == CCD_SAVETY )){ /*make sure we're not nuking objects we don't know*/ MEM_freeN(ccdm->mface); @@ -484,7 +484,7 @@ void ccd_mesh_free(ccd_Mesh *ccdm) } } -void ccd_build_deflector_hache(Object *vertexowner,GHash *hash) +static void ccd_build_deflector_hache(Object *vertexowner,GHash *hash) { Base *base; Object *ob; @@ -536,7 +536,7 @@ void ccd_build_deflector_hache(Object *vertexowner,GHash *hash) } /* while (base) */ } -void ccd_update_deflector_hache(Object *vertexowner,GHash *hash) +static void ccd_update_deflector_hache(Object *vertexowner,GHash *hash) { Base *base; Object *ob; @@ -991,7 +991,7 @@ static int query_external_colliders(Object *me) /* +++ the aabb "force" section*/ -int sb_detect_aabb_collisionCached( float force[3], unsigned int par_layer,struct Object *vertexowner,float time) +static int sb_detect_aabb_collisionCached( float force[3], unsigned int par_layer,struct Object *vertexowner,float time) { Object *ob; SoftBody *sb=vertexowner->soft; @@ -1055,7 +1055,7 @@ int sb_detect_aabb_collisionCached( float force[3], unsigned int par_layer,struc /* +++ the face external section*/ -int sb_detect_face_pointCached(float face_v1[3],float face_v2[3],float face_v3[3],float *damp, +static int sb_detect_face_pointCached(float face_v1[3],float face_v2[3],float face_v3[3],float *damp, float force[3], unsigned int par_layer,struct Object *vertexowner,float time) { Object *ob; @@ -1153,7 +1153,7 @@ int sb_detect_face_pointCached(float face_v1[3],float face_v2[3],float face_v3[3 } -int sb_detect_face_collisionCached(float face_v1[3],float face_v2[3],float face_v3[3],float *damp, +static int sb_detect_face_collisionCached(float face_v1[3],float face_v2[3],float face_v3[3],float *damp, float force[3], unsigned int par_layer,struct Object *vertexowner,float time) { Object *ob; @@ -1291,7 +1291,7 @@ int sb_detect_face_collisionCached(float face_v1[3],float face_v2[3],float face_ -void scan_for_ext_face_forces(Object *ob,float timenow) +static void scan_for_ext_face_forces(Object *ob,float timenow) { SoftBody *sb = ob->soft; BodyFace *bf; @@ -1379,7 +1379,7 @@ void scan_for_ext_face_forces(Object *ob,float timenow) /* +++ the spring external section*/ -int sb_detect_edge_collisionCached(float edge_v1[3],float edge_v2[3],float *damp, +static int sb_detect_edge_collisionCached(float edge_v1[3],float edge_v2[3],float *damp, float force[3], unsigned int par_layer,struct Object *vertexowner,float time) { Object *ob; @@ -1530,7 +1530,7 @@ int sb_detect_edge_collisionCached(float edge_v1[3],float edge_v2[3],float *damp -void _scan_for_ext_spring_forces(Object *ob,float timenow,int ifirst,int ilast, struct ListBase *do_effector) +static void _scan_for_ext_spring_forces(Object *ob,float timenow,int ifirst,int ilast, struct ListBase *do_effector) { SoftBody *sb = ob->soft; int a; @@ -1601,7 +1601,7 @@ void _scan_for_ext_spring_forces(Object *ob,float timenow,int ifirst,int ilast, } -void scan_for_ext_spring_forces(Object *ob,float timenow) +static void scan_for_ext_spring_forces(Object *ob,float timenow) { SoftBody *sb = ob->soft; ListBase *do_effector= NULL; @@ -1613,32 +1613,32 @@ void scan_for_ext_spring_forces(Object *ob,float timenow) pdEndEffectors(do_effector); } -void *exec_scan_for_ext_spring_forces(void *data) +static void *exec_scan_for_ext_spring_forces(void *data) { SB_thread_context *pctx = (SB_thread_context*)data; _scan_for_ext_spring_forces(pctx->ob,pctx->timenow,pctx->ifirst,pctx->ilast,pctx->do_effector); return 0; } -void sb_sfesf_threads_run(struct Object *ob, float timenow,int totsprings,int *ptr_to_break_func()) +static void sb_sfesf_threads_run(struct Object *ob, float timenow,int totsprings,int *ptr_to_break_func()) { - ListBase *do_effector = NULL; + ListBase *do_effector = NULL; ListBase threads; SB_thread_context *sb_threads; int i, totthread,left,dec; - int lowsprings =10; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */ + int lowsprings =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */ do_effector= pdInitEffectors(ob,NULL); /* figure the number of threads while preventing pretty pointless threading overhead */ - if(totsprings < lowsprings) {totthread=1;} - else{ - if(G.scene->r.mode & R_FIXED_THREADS) - totthread= G.scene->r.threads; - else - totthread= BLI_system_thread_count(); + if(G.scene->r.mode & R_FIXED_THREADS) + totthread= G.scene->r.threads; + else + totthread= BLI_system_thread_count(); + /* what if we got zillions of CPUs running but less to spread*/ + while ((totsprings/totthread < lowsprings) && (totthread > 1)) { + totthread--; } - /*left to do--> what if we got zillions of CPUs running but 'totsprings' tasks to spread*/ sb_threads= MEM_callocN(sizeof(SB_thread_context)*totthread, "SBSpringsThread"); memset(sb_threads, 0, sizeof(SB_thread_context)*totthread); @@ -1682,7 +1682,7 @@ void sb_sfesf_threads_run(struct Object *ob, float timenow,int totsprings,int *p /* --- the spring external section*/ -int choose_winner(float*w, float* pos,float*a,float*b,float*c,float*ca,float*cb,float*cc) +static int choose_winner(float*w, float* pos,float*a,float*b,float*c,float*ca,float*cb,float*cc) { float mindist,cp; int winner =1; @@ -1709,7 +1709,7 @@ int choose_winner(float*w, float* pos,float*a,float*b,float*c,float*ca,float*cb, -int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3], float *damp, +static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3], float *damp, float force[3], unsigned int par_layer,struct Object *vertexowner, float time,float vel[3], float *intrusion) { @@ -2119,7 +2119,7 @@ static void sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float fo /* since this is definitely the most CPU consuming task here .. try to spread it */ /* core function _softbody_calc_forces_slice_in_a_thread */ /* result is int to be able to flag user break */ -int _softbody_calc_forces_slice_in_a_thread(Object *ob, float forcetime, float timenow,int ifirst,int ilast,int *ptr_to_break_func(),ListBase *do_effector,int do_deflector,float fieldfactor, float windfactor) +static int _softbody_calc_forces_slice_in_a_thread(Object *ob, float forcetime, float timenow,int ifirst,int ilast,int *ptr_to_break_func(),ListBase *do_effector,int do_deflector,float fieldfactor, float windfactor) { float iks; int bb,do_selfcollision,do_springcollision,do_aero; @@ -2279,6 +2279,11 @@ int _softbody_calc_forces_slice_in_a_thread(Object *ob, float forcetime, float t float kd = 1.0f; if (sb_deflect_face(ob,bp->pos,facenormal,defforce,&cf,timenow,vel,&intrusion)){ + if (intrusion < 0.0f){ + sb->scratch->flag |= SBF_DOFUZZY; + bp->flag |= SBF_DOFUZZY; + bp->choke = sb->choke*0.01f; + } VECSUB(cfforce,bp->vec,vel); Vec3PlusStVec(bp->force,-cf*50.0f,cfforce); @@ -2314,29 +2319,31 @@ int _softbody_calc_forces_slice_in_a_thread(Object *ob, float forcetime, float t return 0; /*done fine*/ } -void *exec_softbody_calc_forces(void *data) +static void *exec_softbody_calc_forces(void *data) { SB_thread_context *pctx = (SB_thread_context*)data; _softbody_calc_forces_slice_in_a_thread(pctx->ob,pctx->forcetime,pctx->timenow,pctx->ifirst,pctx->ilast,NULL,pctx->do_effector,pctx->do_deflector,pctx->fieldfactor,pctx->windfactor); return 0; } -void sb_cf_threads_run(struct Object *ob, float forcetime, float timenow,int totpoint,int *ptr_to_break_func(),struct ListBase *do_effector,int do_deflector,float fieldfactor, float windfactor) +static void sb_cf_threads_run(struct Object *ob, float forcetime, float timenow,int totpoint,int *ptr_to_break_func(),struct ListBase *do_effector,int do_deflector,float fieldfactor, float windfactor) { ListBase threads; SB_thread_context *sb_threads; int i, totthread,left,dec; - int lowpoints =10; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */ + int lowpoints =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */ /* figure the number of threads while preventing pretty pointless threading overhead */ - if(totpoint < lowpoints) {totthread=1;} - else{ - if(G.scene->r.mode & R_FIXED_THREADS) - totthread= G.scene->r.threads; - else - totthread= BLI_system_thread_count(); + if(G.scene->r.mode & R_FIXED_THREADS) + totthread= G.scene->r.threads; + else + totthread= BLI_system_thread_count(); + /* what if we got zillions of CPUs running but less to spread*/ + while ((totpoint/totthread < lowpoints) && (totthread > 1)) { + totthread--; } - /*left to do--> what if we got zillions of CPUs running but 'totpoint' tasks to spread*/ + + /* printf("sb_cf_threads_run spawning %d threads \n",totthread); */ sb_threads= MEM_callocN(sizeof(SB_thread_context)*totthread, "SBThread"); memset(sb_threads, 0, sizeof(SB_thread_context)*totthread); @@ -2425,7 +2432,7 @@ static void softbody_calc_forcesEx(Object *ob, float forcetime, float timenow, i static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int nl_flags) { /* redirection to the new threaded Version */ - if (G.rt !=16){ + if (!(G.rt & 0x10)){ // 16 softbody_calc_forcesEx(ob, forcetime, timenow, nl_flags); return; } @@ -2433,6 +2440,10 @@ static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int /* so the following will die */ /* |||||||||||||||||||||||||| */ /* VVVVVVVVVVVVVVVVVVVVVVVVVV */ + /*backward compatibility note: + fixing bug [17428] which forces adaptive step size to tiny steps + in some situations + .. keeping G.rt==17 0x11 option for old files 'needing' the bug*/ /* rule we never alter free variables :bp->vec bp->pos in here ! * this will ruin adaptive stepsize AKA heun! (BM) @@ -2681,14 +2692,25 @@ static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int if (sb_deflect_face(ob,bp->pos,facenormal,defforce,&cf,timenow,vel,&intrusion)){ if ((!nl_flags)&&(intrusion < 0.0f)){ - /*bjornmose: uugh.. what an evil hack - violation of the 'don't touch bp->pos in here' rule - but works nice, like this--> - we predict the solution beeing out of the collider - in heun step No1 and leave the heun step No2 adapt to it - so we kind of introduced a implicit solver for this case - */ - Vec3PlusStVec(bp->pos,-intrusion,facenormal); + if(G.rt & 0x01){ // 17 we did check for bit 0x10 before + /*fixing bug [17428] this forces adaptive step size to tiny steps + in some situations .. keeping G.rt==17 option for old files 'needing' the bug + */ + /*bjornmose: uugh.. what an evil hack + violation of the 'don't touch bp->pos in here' rule + but works nice, like this--> + we predict the solution beeing out of the collider + in heun step No1 and leave the heun step No2 adapt to it + so we kind of introduced a implicit solver for this case + */ + Vec3PlusStVec(bp->pos,-intrusion,facenormal); + } + else{ + + VECSUB(cfforce,bp->vec,vel); + Vec3PlusStVec(bp->force,-cf*50.0f,cfforce); + } + sb->scratch->flag |= SBF_DOFUZZY; bp->flag |= SBF_DOFUZZY; @@ -3045,7 +3067,7 @@ static void softbody_apply_goalsnap(Object *ob) } -void apply_spring_memory(Object *ob) +static void apply_spring_memory(Object *ob) { SoftBody *sb = ob->soft; BodySpring *bs; @@ -3271,7 +3293,7 @@ static void mesh_faces_to_scratch(Object *ob) helper function to get proper spring length when object is rescaled */ -float globallen(float *v1,float *v2,Object *ob) +static float globallen(float *v1,float *v2,Object *ob) { float p1[3],p2[3]; VECCOPY(p1,v1); @@ -3689,7 +3711,7 @@ static int softbody_read_cache(Object *ob, float framenr) } /* +++ ************ maintaining scratch *************** */ -void sb_new_scratch(SoftBody *sb) +static void sb_new_scratch(SoftBody *sb) { if (!sb) return; sb->scratch = MEM_callocN(sizeof(SBScratch), "SBScratch"); @@ -4040,7 +4062,7 @@ static void softbody_step(Object *ob, SoftBody *sb, float dtime) if(sb->solverflags & SBSO_MONITOR ){ sct=PIL_check_seconds_timer(); - if (sct-sst > 0.5f) printf(" solver time %f sec %s \n",sct-sst,ob->id.name); + if ((sct-sst > 0.5f) || (G.f & G_DEBUG)) printf(" solver time %f sec %s \n",sct-sst,ob->id.name); } } diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c index bba79bd6d28..f79009e673d 100644 --- a/source/blender/blenkernel/intern/subsurf_ccg.c +++ b/source/blender/blenkernel/intern/subsurf_ccg.c @@ -61,6 +61,10 @@ #include "BIF_gl.h" +#include "GPU_draw.h" +#include "GPU_extensions.h" +#include "GPU_material.h" + #include "CCGSubSurf.h" typedef struct _VertData { @@ -82,9 +86,9 @@ struct CCGDerivedMesh { typedef struct CCGDerivedMesh CCGDerivedMesh; -static int ccgDM_getVertMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGVert *v); -static int ccgDM_getEdgeMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGEdge *e); -static int ccgDM_getFaceMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGFace *f); +static int ccgDM_getVertMapIndex(CCGSubSurf *ss, CCGVert *v); +static int ccgDM_getEdgeMapIndex(CCGSubSurf *ss, CCGEdge *e); +static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f); /// @@ -157,8 +161,8 @@ static CCGSubSurf *_getSubSurf(CCGSubSurf *prevSS, int subdivLevels, int useAgin } static int getEdgeIndex(CCGSubSurf *ss, CCGEdge *e, int x, int edgeSize) { - CCGVert *v0 = ccgSubSurf_getEdgeVert0(ss, e); - CCGVert *v1 = ccgSubSurf_getEdgeVert1(ss, e); + CCGVert *v0 = ccgSubSurf_getEdgeVert0(e); + CCGVert *v1 = ccgSubSurf_getEdgeVert1(e); int v0idx = *((int*) ccgSubSurf_getVertUserData(ss, v0)); int v1idx = *((int*) ccgSubSurf_getVertUserData(ss, v1)); int edgeBase = *((int*) ccgSubSurf_getEdgeUserData(ss, e)); @@ -173,7 +177,7 @@ static int getEdgeIndex(CCGSubSurf *ss, CCGEdge *e, int x, int edgeSize) { } static int getFaceIndex(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edgeSize, int gridSize) { int faceBase = *((int*) ccgSubSurf_getFaceUserData(ss, f)); - int numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int numVerts = ccgSubSurf_getFaceNumVerts(f); if (x==gridSize-1 && y==gridSize-1) { CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S); @@ -182,7 +186,7 @@ static int getFaceIndex(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edg CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S); CCGEdge *e = ccgSubSurf_getFaceEdge(ss, f, S); int edgeBase = *((int*) ccgSubSurf_getEdgeUserData(ss, e)); - if (v==ccgSubSurf_getEdgeVert0(ss, e)) { + if (v==ccgSubSurf_getEdgeVert0(e)) { return edgeBase + (gridSize-1-y)-1; } else { return edgeBase + (edgeSize-2-1)-((gridSize-1-y)-1); @@ -191,7 +195,7 @@ static int getFaceIndex(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edg CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S); CCGEdge *e = ccgSubSurf_getFaceEdge(ss, f, (S+numVerts-1)%numVerts); int edgeBase = *((int*) ccgSubSurf_getEdgeUserData(ss, e)); - if (v==ccgSubSurf_getEdgeVert0(ss, e)) { + if (v==ccgSubSurf_getEdgeVert0(e)) { return edgeBase + (gridSize-1-x)-1; } else { return edgeBase + (edgeSize-2-1)-((gridSize-1-x)-1); @@ -296,7 +300,7 @@ static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm, if ((mv0->flag&mv1->flag) & ME_VERT_MERGED) crease = creaseFactor; else - crease = ccgSubSurf_getEdgeCrease(origss, orige); + crease = ccgSubSurf_getEdgeCrease(orige); ccgSubSurf_syncEdge(ss, ehdl, fverts[j], fverts[(j+1)%nverts], crease, &e); BLI_edgehash_insert(ehash, v0, v1, NULL); @@ -364,7 +368,7 @@ static void set_subsurf_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh *result, for(index = 0; index < totface; index++) { CCGFace *f = faceMap[index]; - int numVerts = ccgSubSurf_getFaceNumVerts(uvss, f); + int numVerts = ccgSubSurf_getFaceNumVerts(f); for (S=0; S<numVerts; S++) { VertData *faceGridData= ccgSubSurf_getFaceGridDataArray(uvss, f, S); @@ -395,12 +399,12 @@ static void set_subsurf_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh *result, static unsigned int ss_getEdgeFlags(CCGSubSurf *ss, CCGEdge *e, int ssFromEditmesh, DispListMesh *dlm, MEdge *medge, MTFace *tface) { unsigned int flags = 0; - int N = ccgSubSurf_getEdgeNumFaces(ss, e); + int N = ccgSubSurf_getEdgeNumFaces(e); if (!N) flags |= ME_LOOSEEDGE; if (ssFromEditmesh) { - EditEdge *eed = ccgSubSurf_getEdgeEdgeHandle(ss, e); + EditEdge *eed = ccgSubSurf_getEdgeEdgeHandle(e); flags |= ME_EDGEDRAW|ME_EDGERENDER; if (eed->seam) { @@ -466,7 +470,7 @@ static void calc_ss_weights(int gridFaces, } } -DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, +static DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, int drawInteriorEdges, int useSubsurfUv, DerivedMesh *dm) { @@ -476,7 +480,6 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, int gridFaces = gridSize - 1; int edgeBase, faceBase; int i, j, k, S, x, y, index; - int vertBase = 0; CCGVertIterator *vi; CCGEdgeIterator *ei; CCGFaceIterator *fi; @@ -499,7 +502,7 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, for(; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) { CCGVert *v = ccgVertIterator_getCurrent(vi); - vertMap2[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v))] = v; + vertMap2[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))] = v; } ccgVertIterator_free(vi); @@ -509,7 +512,7 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, for(; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - edgeMap2[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(ss, e))] = e; + edgeMap2[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))] = e; } totface = ccgSubSurf_getNumFaces(ss); @@ -539,18 +542,18 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, for(index = 0; index < totface; index++) { CCGFace *f = faceMap2[index]; - int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f); FaceVertWeight *weight = (numVerts == 4) ? qweight : tweight; int vertIdx[4]; for(S = 0; S < numVerts; S++) { CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S); - vertIdx[S] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v)); + vertIdx[S] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); } DM_interp_vert_data(dm, result, vertIdx, weight[0][0], numVerts, i); - VecCopyf(mvert->co, ccgSubSurf_getFaceCenterData(ss, f)); + VecCopyf(mvert->co, ccgSubSurf_getFaceCenterData(f)); *origIndex = ORIGINDEX_NONE; ++mvert; ++origIndex; @@ -609,10 +612,10 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, int vertIdx[2]; CCGVert *v; - v = ccgSubSurf_getEdgeVert0(ss, e); - vertIdx[0] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v)); - v = ccgSubSurf_getEdgeVert1(ss, e); - vertIdx[1] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v)); + v = ccgSubSurf_getEdgeVert0(e); + vertIdx[0] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); + v = ccgSubSurf_getEdgeVert1(e); + vertIdx[1] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); for(x = 1; x < edgeSize - 1; x++) { float w[2]; @@ -630,18 +633,17 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, edgeBase += edgeSize-2; } - vertBase = i; for(index = 0; index < totvert; index++) { CCGVert *v = vertMap2[index]; int vertIdx; - vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v)); + vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); DM_copy_vert_data(dm, result, vertIdx, i, 1); VecCopyf(mvert->co, ccgSubSurf_getVertData(ss, v)); *((int*)ccgSubSurf_getVertUserData(ss, v)) = i; - *origIndex = ccgDM_getVertMapIndex(NULL, ss, v); + *origIndex = ccgDM_getVertMapIndex(ss, v); ++mvert; ++origIndex; i++; @@ -654,7 +656,7 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, for(index = 0; index < totface; index++) { CCGFace *f = faceMap2[index]; - int numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int numVerts = ccgSubSurf_getFaceNumVerts(f); for(k = 0; k < numVerts; k++) { for(x = 0; x < gridFaces; x++) { @@ -696,9 +698,10 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, for(index = 0; index < totedge; index++) { CCGEdge *e = edgeMap2[index]; unsigned int flags = 0; - int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(ss, e)); + char bweight = 0; + int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e)); - if(!ccgSubSurf_getEdgeNumFaces(ss, e)) flags |= ME_LOOSEEDGE; + if(!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE; if(edgeIdx != -1 && dm) { @@ -706,13 +709,15 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, dm->getEdge(dm, edgeIdx, &origMed); flags |= origMed.flag; + bweight = origMed.bweight; } for(x = 0; x < edgeSize - 1; x++) { med->v1 = getEdgeIndex(ss, e, x, edgeSize); med->v2 = getEdgeIndex(ss, e, x + 1, edgeSize); med->flag = flags; - *origIndex = ccgDM_getEdgeMapIndex(NULL, ss, e); + med->bweight = bweight; + *origIndex = ccgDM_getEdgeMapIndex(ss, e); ++med; ++origIndex; i++; @@ -726,10 +731,10 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh, for(index = 0; index < totface; index++) { CCGFace *f = faceMap2[index]; - int numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int numVerts = ccgSubSurf_getFaceNumVerts(f); int mat_nr; int flag; - int mapIndex = ccgDM_getFaceMapIndex(NULL, ss, f); + int mapIndex = ccgDM_getFaceMapIndex(ss, f); int faceIdx = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f)); if(!ssFromEditmesh) { @@ -893,15 +898,15 @@ static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm, /***/ -static int ccgDM_getVertMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGVert *v) { +static int ccgDM_getVertMapIndex(CCGSubSurf *ss, CCGVert *v) { return ((int*) ccgSubSurf_getVertUserData(ss, v))[1]; } -static int ccgDM_getEdgeMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGEdge *e) { +static int ccgDM_getEdgeMapIndex(CCGSubSurf *ss, CCGEdge *e) { return ((int*) ccgSubSurf_getEdgeUserData(ss, e))[1]; } -static int ccgDM_getFaceMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGFace *f) { +static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f) { return ((int*) ccgSubSurf_getFaceUserData(ss, f))[1]; } @@ -934,7 +939,7 @@ static void ccgDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3]) { for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) { CCGFace *f = ccgFaceIterator_getCurrent(fi); - int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); for (S=0; S<numVerts; S++) { VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); @@ -973,7 +978,7 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv) memset(mv, 0, sizeof(*mv)); - if(vertNum < ccgdm->edgeMap[0].startVert) { + if((vertNum < ccgdm->edgeMap[0].startVert) && (ccgSubSurf_getNumFaces(ss) > 0)) { /* this vert comes from face data */ int lastface = ccgSubSurf_getNumFaces(ss) - 1; CCGFace *f; @@ -990,7 +995,7 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv) ++i; f = ccgdm->faceMap[i].face; - numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + numVerts = ccgSubSurf_getFaceNumVerts(f); gridSideVerts = gridSize - 2; gridInternalVerts = gridSideVerts * gridSideVerts; @@ -1000,7 +1005,7 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv) offset = vertNum - ccgdm->faceMap[i].startVert; if(offset < 1) { - VecCopyf(mv->co, ccgSubSurf_getFaceCenterData(ss, f)); + VecCopyf(mv->co, ccgSubSurf_getFaceCenterData(f)); } else if(offset < gridSideEnd) { offset -= 1; grid = offset / gridSideVerts; @@ -1014,7 +1019,7 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv) x = offset % gridSideVerts + 1; VecCopyf(mv->co, ccgSubSurf_getFaceGridData(ss, f, grid, x, y)); } - } else if(vertNum < ccgdm->vertMap[0].startVert) { + } else if((vertNum < ccgdm->vertMap[0].startVert) && (ccgSubSurf_getNumEdges(ss) > 0)) { /* this vert comes from edge data */ CCGEdge *e; int lastedge = ccgSubSurf_getNumEdges(ss) - 1; @@ -1062,7 +1067,7 @@ static void ccgDM_getFinalEdge(DerivedMesh *dm, int edgeNum, MEdge *med) ++i; f = ccgdm->faceMap[i].face; - numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + numVerts = ccgSubSurf_getFaceNumVerts(f); gridSideEdges = gridSize - 1; gridInternalEdges = (gridSideEdges - 1) * gridSideEdges * 2; @@ -1098,7 +1103,7 @@ static void ccgDM_getFinalEdge(DerivedMesh *dm, int edgeNum, MEdge *med) e = ccgdm->edgeMap[i].edge; - if(!ccgSubSurf_getEdgeNumFaces(ss, e)) flags |= ME_LOOSEEDGE; + if(!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE; x = edgeNum - ccgdm->edgeMap[i].startEdge; @@ -1140,7 +1145,7 @@ static void ccgDM_getFinalFace(DerivedMesh *dm, int faceNum, MFace *mf) ++i; f = ccgdm->faceMap[i].face; - numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + numVerts = ccgSubSurf_getFaceNumVerts(f); offset = faceNum - ccgdm->faceMap[i].startFace; grid = offset / gridFaces; @@ -1170,9 +1175,9 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert) totface = ccgSubSurf_getNumFaces(ss); for(index = 0; index < totface; index++) { CCGFace *f = ccgdm->faceMap[index].face; - int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f); - VecCopyf(mvert[i++].co, ccgSubSurf_getFaceCenterData(ss, f)); + VecCopyf(mvert[i++].co, ccgSubSurf_getFaceCenterData(f)); for(S = 0; S < numVerts; S++) { for(x = 1; x < gridSize - 1; x++) { @@ -1225,7 +1230,7 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge) totface = ccgSubSurf_getNumFaces(ss); for(index = 0; index < totface; index++) { CCGFace *f = ccgdm->faceMap[index].face; - int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f); for(S = 0; S < numVerts; S++) { for(x = 0; x < gridSize - 1; x++) { @@ -1269,9 +1274,9 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge) CCGEdge *e = ccgdm->edgeMap[index].edge; unsigned int flags = 0; int x; - int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(ss, e)); + int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e)); - if(!ccgSubSurf_getEdgeNumFaces(ss, e)) flags |= ME_LOOSEEDGE; + if(!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE; if(edgeFlags) { if(edgeIdx != -1) { @@ -1306,7 +1311,7 @@ static void ccgDM_copyFinalFaceArray(DerivedMesh *dm, MFace *mface) totface = ccgSubSurf_getNumFaces(ss); for(index = 0; index < totface; index++) { CCGFace *f = ccgdm->faceMap[index].face; - int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f); int mat_nr = 0; int flag = ME_SMOOTH; /* assume face is smooth by default */ @@ -1353,7 +1358,7 @@ static void ccgdm_getVertCos(DerivedMesh *dm, float (*cos)[3]) { for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) { CCGVert *v = ccgVertIterator_getCurrent(vi); - vertMap2[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v))] = v; + vertMap2[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))] = v; } ccgVertIterator_free(vi); @@ -1363,7 +1368,7 @@ static void ccgdm_getVertCos(DerivedMesh *dm, float (*cos)[3]) { for (i=0; !ccgEdgeIterator_isStopped(ei); i++,ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - edgeMap2[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(ss, e))] = e; + edgeMap2[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))] = e; } totface = ccgSubSurf_getNumFaces(ss); @@ -1379,9 +1384,9 @@ static void ccgdm_getVertCos(DerivedMesh *dm, float (*cos)[3]) { i = 0; for (index=0; index<totface; index++) { CCGFace *f = faceMap2[index]; - int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f); - VecCopyf(cos[i++], ccgSubSurf_getFaceCenterData(ss, f)); + VecCopyf(cos[i++], ccgSubSurf_getFaceCenterData(f)); for (S=0; S<numVerts; S++) { for (x=1; x<gridSize-1; x++) { @@ -1423,7 +1428,7 @@ static void ccgDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) { CCGVert *v = ccgVertIterator_getCurrent(vi); VertData *vd = ccgSubSurf_getVertData(ccgdm->ss, v); - int index = ccgDM_getVertMapIndex(ccgdm, ccgdm->ss, v); + int index = ccgDM_getVertMapIndex(ccgdm->ss, v); if (index!=-1) func(userData, index, vd->co, vd->no, NULL); @@ -1440,7 +1445,7 @@ static void ccgDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); - int index = ccgDM_getEdgeMapIndex(ccgdm, ss, e); + int index = ccgDM_getEdgeMapIndex(ss, e); if (index!=-1) { for (i=0; i<edgeSize-1; i++) @@ -1481,9 +1486,9 @@ static void ccgDM_drawVerts(DerivedMesh *dm) { fi = ccgSubSurf_getFaceIterator(ss); for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) { CCGFace *f = ccgFaceIterator_getCurrent(fi); - int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f); - glVertex3fv(ccgSubSurf_getFaceCenterData(ss, f)); + glVertex3fv(ccgSubSurf_getFaceCenterData(f)); for (S=0; S<numVerts; S++) for (x=1; x<gridSize-1; x++) glVertex3fv(ccgSubSurf_getFaceGridEdgeData(ss, f, S, x)); @@ -1510,7 +1515,7 @@ static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); - if (!drawLooseEdges && !ccgSubSurf_getEdgeNumFaces(ss, e)) + if (!drawLooseEdges && !ccgSubSurf_getEdgeNumFaces(e)) continue; if (useAging && !(G.f&G_BACKBUFSEL)) { @@ -1533,7 +1538,7 @@ static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges) { if (ccgdm->drawInteriorEdges) { for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) { CCGFace *f = ccgFaceIterator_getCurrent(fi); - int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); for (S=0; S<numVerts; S++) { VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); @@ -1571,7 +1576,7 @@ static void ccgDM_drawLooseEdges(DerivedMesh *dm) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); - if (!ccgSubSurf_getEdgeNumFaces(ss, e)) { + if (!ccgSubSurf_getEdgeNumFaces(e)) { glBegin(GL_LINE_STRIP); for (i=0; i<edgeSize-1; i++) { glVertex3fv(edgeData[i].co); @@ -1599,7 +1604,7 @@ static void ccgDM_glNormalFast(float *a, float *b, float *c, float *d) } /* Only used by non-editmesh types */ -static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int)) { +static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm; CCGSubSurf *ss = ccgdm->ss; CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss); @@ -1608,7 +1613,7 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int)) { for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) { CCGFace *f = ccgFaceIterator_getCurrent(fi); - int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f)); int drawSmooth, mat_nr; @@ -1621,7 +1626,7 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int)) { mat_nr= 0; } - if (!setMaterial(mat_nr+1)) + if (!setMaterial(mat_nr+1, NULL)) continue; glShadeModel(drawSmooth? GL_SMOOTH: GL_FLAT); @@ -1666,6 +1671,168 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int)) { ccgFaceIterator_free(fi); } + + /* Only used by non-editmesh types */ +static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs), int (*setDrawOptions)(void *userData, int index), void *userData) { + CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm; + CCGSubSurf *ss = ccgdm->ss; + CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss); + GPUVertexAttribs gattribs; + DMVertexAttribs attribs; + MTFace *tf = dm->getFaceDataArray(dm, CD_MTFACE); + int gridSize = ccgSubSurf_getGridSize(ss); + int gridFaces = gridSize - 1; + int edgeSize = ccgSubSurf_getEdgeSize(ss); + int transp, orig_transp, new_transp; + char *faceFlags = DM_get_face_data_layer(dm, CD_FLAGS); + int a, b, i, doDraw, numVerts, matnr, new_matnr, totface; + + doDraw = 0; + numVerts = 0; + matnr = -1; + transp = GPU_get_material_blend_mode(); + orig_transp = transp; + + memset(&attribs, 0, sizeof(attribs)); + +#define PASSATTRIB(dx, dy, vert) { \ + if(attribs.totorco) { \ + index = getFaceIndex(ss, f, S, x+dx, y+dy, edgeSize, gridSize); \ + glVertexAttrib3fvARB(attribs.orco.glIndex, attribs.orco.array[index]); \ + } \ + for(b = 0; b < attribs.tottface; b++) { \ + MTFace *tf = &attribs.tface[b].array[a]; \ + glVertexAttrib2fvARB(attribs.tface[b].glIndex, tf->uv[vert]); \ + } \ + for(b = 0; b < attribs.totmcol; b++) { \ + MCol *cp = &attribs.mcol[b].array[a*4 + vert]; \ + GLubyte col[4]; \ + col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a; \ + glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col); \ + } \ + if(attribs.tottang) { \ + float *tang = attribs.tang.array[a*4 + vert]; \ + glVertexAttrib3fvARB(attribs.tang.glIndex, tang); \ + } \ +} + + totface = ccgSubSurf_getNumFaces(ss); + for(a = 0, i = 0; i < totface; i++) { + CCGFace *f = ccgdm->faceMap[i].face; + int S, x, y, drawSmooth; + int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f)); + int origIndex = ccgDM_getFaceMapIndex(ss, f); + + numVerts = ccgSubSurf_getFaceNumVerts(f); + + if(faceFlags) { + drawSmooth = (faceFlags[index*4] & ME_SMOOTH); + new_matnr= faceFlags[index*4 + 1] + 1; + } + else { + drawSmooth = 1; + new_matnr= 1; + } + + if(new_matnr != matnr) { + doDraw = setMaterial(matnr = new_matnr, &gattribs); + if(doDraw) + DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs); + } + + if(!doDraw || (setDrawOptions && !setDrawOptions(userData, origIndex))) { + a += gridFaces*gridFaces*numVerts; + continue; + } + + if(tf) { + new_transp = tf[i].transp; + + if(new_transp != transp) { + if(new_transp == GPU_BLEND_SOLID && orig_transp != GPU_BLEND_SOLID) + GPU_set_material_blend_mode(orig_transp); + else + GPU_set_material_blend_mode(new_transp); + transp = new_transp; + } + } + + glShadeModel(drawSmooth? GL_SMOOTH: GL_FLAT); + for (S=0; S<numVerts; S++) { + VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); + VertData *vda, *vdb; + + if (drawSmooth) { + for (y=0; y<gridFaces; y++) { + glBegin(GL_QUAD_STRIP); + for (x=0; x<gridFaces; x++) { + vda = &faceGridData[(y+0)*gridSize + x]; + vdb = &faceGridData[(y+1)*gridSize + x]; + + PASSATTRIB(0, 0, 0); + glNormal3fv(vda->no); + glVertex3fv(vda->co); + + PASSATTRIB(0, 1, 1); + glNormal3fv(vdb->no); + glVertex3fv(vdb->co); + + if(x != gridFaces-1) + a++; + } + + vda = &faceGridData[(y+0)*gridSize + x]; + vdb = &faceGridData[(y+1)*gridSize + x]; + + PASSATTRIB(0, 0, 3); + glNormal3fv(vda->no); + glVertex3fv(vda->co); + + PASSATTRIB(0, 1, 2); + glNormal3fv(vdb->no); + glVertex3fv(vdb->co); + + glEnd(); + + a++; + } + } else { + glBegin(GL_QUADS); + for (y=0; y<gridFaces; y++) { + for (x=0; x<gridFaces; x++) { + float *aco = faceGridData[(y+0)*gridSize + x].co; + float *bco = faceGridData[(y+0)*gridSize + x + 1].co; + float *cco = faceGridData[(y+1)*gridSize + x + 1].co; + float *dco = faceGridData[(y+1)*gridSize + x].co; + + ccgDM_glNormalFast(aco, bco, cco, dco); + + PASSATTRIB(0, 1, 1); + glVertex3fv(dco); + PASSATTRIB(1, 1, 2); + glVertex3fv(cco); + PASSATTRIB(1, 0, 3); + glVertex3fv(bco); + PASSATTRIB(0, 0, 0); + glVertex3fv(aco); + + a++; + } + } + glEnd(); + } + } + } + +#undef PASSATTRIB + + ccgFaceIterator_free(fi); +} + +static void ccgDM_drawFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) { + dm->drawMappedFacesGLSL(dm, setMaterial, NULL, NULL); +} + static void ccgDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *col1, unsigned char *col2) { CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm; CCGSubSurf *ss = ccgdm->ss; @@ -1689,7 +1856,7 @@ static void ccgDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned ch glBegin(GL_QUADS); for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) { CCGFace *f = ccgFaceIterator_getCurrent(fi); - int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); for (S=0; S<numVerts; S++) { VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S); @@ -1747,8 +1914,8 @@ static void ccgDM_drawFacesTex_common(DerivedMesh *dm, totface = ccgSubSurf_getNumFaces(ss); for(i = 0; i < totface; i++) { CCGFace *f = ccgdm->faceMap[i].face; - int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); - int drawSmooth, index = ccgDM_getFaceMapIndex(ccgdm, ss, f); + int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); + int drawSmooth, index = ccgDM_getFaceMapIndex(ss, f); int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f)); unsigned char *cp= NULL; int mat_nr; @@ -1830,28 +1997,28 @@ static void ccgDM_drawFacesTex_common(DerivedMesh *dm, glBegin(GL_QUADS); for (y=0; y<gridFaces; y++) { for (x=0; x<gridFaces; x++) { - float *a = faceGridData[(y+0)*gridSize + x].co; - float *b = faceGridData[(y+0)*gridSize + x + 1].co; - float *c = faceGridData[(y+1)*gridSize + x + 1].co; - float *d = faceGridData[(y+1)*gridSize + x].co; + float *a_co = faceGridData[(y+0)*gridSize + x].co; + float *b_co = faceGridData[(y+0)*gridSize + x + 1].co; + float *c_co = faceGridData[(y+1)*gridSize + x + 1].co; + float *d_co = faceGridData[(y+1)*gridSize + x].co; - ccgDM_glNormalFast(a, b, c, d); + ccgDM_glNormalFast(a_co, b_co, c_co, d_co); if(tf) glTexCoord2fv(tf->uv[1]); if(cp) glColor3ub(cp[7], cp[6], cp[5]); - glVertex3fv(d); + glVertex3fv(d_co); if(tf) glTexCoord2fv(tf->uv[2]); if(cp) glColor3ub(cp[11], cp[10], cp[9]); - glVertex3fv(c); + glVertex3fv(c_co); if(tf) glTexCoord2fv(tf->uv[3]); if(cp) glColor3ub(cp[15], cp[14], cp[13]); - glVertex3fv(b); + glVertex3fv(b_co); if(tf) glTexCoord2fv(tf->uv[0]); if(cp) glColor3ub(cp[3], cp[2], cp[1]); - glVertex3fv(a); + glVertex3fv(a_co); if(tf) tf++; if(cp) cp += 16; @@ -1915,8 +2082,8 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *u for (i=0; !ccgFaceIterator_isStopped(fi); i++,ccgFaceIterator_next(fi)) { CCGFace *f = ccgFaceIterator_getCurrent(fi); - int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f); - int drawSmooth, index = ccgDM_getFaceMapIndex(ccgdm, ss, f); + int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f); + int drawSmooth, index = ccgDM_getFaceMapIndex(ss, f); int origIndex; origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f)); @@ -1997,7 +2164,7 @@ static void ccgDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *u for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); - int index = ccgDM_getEdgeMapIndex(ccgdm, ss, e); + int index = ccgDM_getEdgeMapIndex(ss, e); glBegin(GL_LINE_STRIP); if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index))) { @@ -2027,7 +2194,7 @@ static void ccgDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(v for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e); - int index = ccgDM_getEdgeMapIndex(ccgdm, ss, e); + int index = ccgDM_getEdgeMapIndex(ss, e); glBegin(GL_LINE_STRIP); if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index))) { @@ -2054,7 +2221,7 @@ static void ccgDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *us for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) { CCGFace *f = ccgFaceIterator_getCurrent(fi); - int index = ccgDM_getFaceMapIndex(ccgdm, ss, f); + int index = ccgDM_getFaceMapIndex(ss, f); if (index!=-1) { /* Face center data normal isn't updated atm. */ @@ -2090,17 +2257,17 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, int index, totvert, totedge, totface; int i; int vertNum, edgeNum, faceNum; - int *vertOrigIndex, *edgeOrigIndex, *faceOrigIndex; + int *vertOrigIndex, *faceOrigIndex; /* *edgeOrigIndex - as yet, unused */ int *edgeFlags; char *faceFlags; int edgeSize; int gridSize; int gridFaces; int gridSideVerts; - int gridInternalVerts; + /*int gridInternalVerts; - as yet unused */ int gridSideEdges; int gridInternalEdges; - MVert *mvert = NULL; + /* MVert *mvert = NULL; - as yet unused */ MEdge *medge = NULL; MFace *mface = NULL; FaceVertWeight *qweight, *tweight; @@ -2143,8 +2310,10 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, ccgdm->dm.drawFacesSolid = ccgDM_drawFacesSolid; ccgdm->dm.drawFacesColored = ccgDM_drawFacesColored; ccgdm->dm.drawFacesTex = ccgDM_drawFacesTex; + ccgdm->dm.drawFacesGLSL = ccgDM_drawFacesGLSL; ccgdm->dm.drawMappedFaces = ccgDM_drawMappedFaces; ccgdm->dm.drawMappedFacesTex = ccgDM_drawMappedFacesTex; + ccgdm->dm.drawMappedFacesGLSL = ccgDM_drawMappedFacesGLSL; ccgdm->dm.drawUVEdges = ccgDM_drawUVEdges; ccgdm->dm.drawMappedEdgesInterp = ccgDM_drawMappedEdgesInterp; @@ -2162,7 +2331,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, for(; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) { CCGVert *v = ccgVertIterator_getCurrent(vi); - ccgdm->vertMap[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v))].vert = v; + ccgdm->vertMap[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))].vert = v; } ccgVertIterator_free(vi); @@ -2172,7 +2341,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, for(; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) { CCGEdge *e = ccgEdgeIterator_getCurrent(ei); - ccgdm->edgeMap[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(ss, e))].edge = e; + ccgdm->edgeMap[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))].edge = e; } totface = ccgSubSurf_getNumFaces(ss); @@ -2189,7 +2358,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, gridSize = ccgSubSurf_getGridSize(ss); gridFaces = gridSize - 1; gridSideVerts = gridSize - 2; - gridInternalVerts = gridSideVerts * gridSideVerts; + /*gridInternalVerts = gridSideVerts * gridSideVerts; - as yet, unused */ gridSideEdges = gridSize - 1; gridInternalEdges = (gridSideEdges - 1) * gridSideEdges * 2; @@ -2199,21 +2368,21 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, edgeNum = 0; faceNum = 0; - mvert = dm->getVertArray(dm); + /* mvert = dm->getVertArray(dm); - as yet unused */ medge = dm->getEdgeArray(dm); mface = dm->getFaceArray(dm); vertOrigIndex = DM_get_vert_data_layer(&ccgdm->dm, CD_ORIGINDEX); - edgeOrigIndex = DM_get_edge_data_layer(&ccgdm->dm, CD_ORIGINDEX); + /*edgeOrigIndex = DM_get_edge_data_layer(&ccgdm->dm, CD_ORIGINDEX);*/ faceOrigIndex = DM_get_face_data_layer(&ccgdm->dm, CD_ORIGINDEX); faceFlags = DM_get_face_data_layer(&ccgdm->dm, CD_FLAGS); for(index = 0; index < totface; ++index) { CCGFace *f = ccgdm->faceMap[index].face; - int numVerts = ccgSubSurf_getFaceNumVerts(ss, f); + int numVerts = ccgSubSurf_getFaceNumVerts(f); int numFinalEdges = numVerts * (gridSideEdges + gridInternalEdges); - int mapIndex = ccgDM_getFaceMapIndex(ccgdm, ss, f); + int mapIndex = ccgDM_getFaceMapIndex(ss, f); int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f)); FaceVertWeight *weight = (numVerts == 4) ? qweight : tweight; int S, x, y; @@ -2229,7 +2398,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, for(S = 0; S < numVerts; S++) { CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S); - vertIdx[S] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v)); + vertIdx[S] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); } DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, weight[0][0], @@ -2332,16 +2501,16 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, for(index = 0; index < totedge; ++index) { CCGEdge *e = ccgdm->edgeMap[index].edge; int numFinalEdges = edgeSize - 1; - int mapIndex = ccgDM_getEdgeMapIndex(ccgdm, ss, e); + int mapIndex = ccgDM_getEdgeMapIndex(ss, e); int x; int vertIdx[2]; - int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(ss, e)); + int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e)); CCGVert *v; - v = ccgSubSurf_getEdgeVert0(ss, e); - vertIdx[0] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v)); - v = ccgSubSurf_getEdgeVert1(ss, e); - vertIdx[1] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v)); + v = ccgSubSurf_getEdgeVert0(e); + vertIdx[0] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); + v = ccgSubSurf_getEdgeVert1(e); + vertIdx[1] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); ccgdm->edgeMap[index].startVert = vertNum; ccgdm->edgeMap[index].startEdge = edgeNum; @@ -2372,10 +2541,10 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss, for(index = 0; index < totvert; ++index) { CCGVert *v = ccgdm->vertMap[index].vert; - int mapIndex = ccgDM_getVertMapIndex(ccgdm, ccgdm->ss, v); + int mapIndex = ccgDM_getVertMapIndex(ccgdm->ss, v); int vertIdx; - vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v)); + vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); ccgdm->vertMap[index].startVert = vertNum; @@ -2508,9 +2677,9 @@ void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3]) vi = ccgSubSurf_getVertIterator(ss); for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) { CCGVert *v = ccgVertIterator_getCurrent(vi); - int idx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(ss, v)); - int N = ccgSubSurf_getVertNumEdges(ss, v); - int numFaces = ccgSubSurf_getVertNumFaces(ss, v); + int idx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v)); + int N = ccgSubSurf_getVertNumEdges(v); + int numFaces = ccgSubSurf_getVertNumFaces(v); float *co; int i; @@ -2518,12 +2687,12 @@ void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3]) face_sum[0]= face_sum[1]= face_sum[2]= 0.0; for (i=0; i<N; i++) { - CCGEdge *e = ccgSubSurf_getVertEdge(ss, v, i); + CCGEdge *e = ccgSubSurf_getVertEdge(v, i); VecAddf(edge_sum, edge_sum, ccgSubSurf_getEdgeData(ss, e, 1)); } for (i=0; i<numFaces; i++) { - CCGFace *f = ccgSubSurf_getVertFace(ss, v, i); - VecAddf(face_sum, face_sum, ccgSubSurf_getFaceCenterData(ss, f)); + CCGFace *f = ccgSubSurf_getVertFace(v, i); + VecAddf(face_sum, face_sum, ccgSubSurf_getFaceCenterData(f)); } /* ad-hoc correction for boundary vertices, to at least avoid them diff --git a/source/blender/blenkernel/intern/suggestions.c b/source/blender/blenkernel/intern/suggestions.c new file mode 100644 index 00000000000..6530909336c --- /dev/null +++ b/source/blender/blenkernel/intern/suggestions.c @@ -0,0 +1,254 @@ +/** + * $Id: $ + * + * ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2008, Blender Foundation + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): Ian Thompson. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +#include "MEM_guardedalloc.h" +#include "BLI_blenlib.h" +#include "DNA_text_types.h" +#include "BKE_text.h" +#include "BKE_suggestions.h" + +/**********************/ +/* Static definitions */ +/**********************/ + +static Text *activeToolText = NULL; +static SuggList suggestions = {NULL, NULL, NULL, NULL, NULL}; +static char *documentation = NULL; +//static int doc_lines = 0; + +static int txttl_cmp(const char *first, const char *second, int len) { + int cmp, i; + for (cmp=0, i=0; i<len; i++) { + if ( (cmp= toupper(first[i])-toupper(second[i])) ) { + break; + } + } + return cmp; +} + +static void txttl_free_suggest() { + SuggItem *item, *prev; + for (item = suggestions.last; item; item=prev) { + prev = item->prev; + MEM_freeN(item); + } + suggestions.first = suggestions.last = NULL; + suggestions.firstmatch = suggestions.lastmatch = NULL; + suggestions.selected = NULL; + suggestions.top = 0; +} + +static void txttl_free_docs() { + if (documentation) { + MEM_freeN(documentation); + documentation = NULL; + } +} + +/**************************/ +/* General tool functions */ +/**************************/ + +void free_texttools() { + txttl_free_suggest(); + txttl_free_docs(); +} + +void texttool_text_set_active(Text *text) { + if (activeToolText == text) return; + texttool_text_clear(); + activeToolText = text; +} + +void texttool_text_clear() { + free_texttools(); + activeToolText = NULL; +} + +short texttool_text_is_active(Text *text) { + return activeToolText==text ? 1 : 0; +} + +/***************************/ +/* Suggestion list methods */ +/***************************/ + +void texttool_suggest_add(const char *name, char type) { + SuggItem *newitem, *item; + int len, cmp; + + newitem = MEM_mallocN(sizeof(SuggItem) + strlen(name) + 1, "SuggestionItem"); + if (!newitem) { + printf("Failed to allocate memory for suggestion.\n"); + return; + } + + newitem->name = (char *) (newitem + 1); + len = strlen(name); + strncpy(newitem->name, name, len); + newitem->name[len] = '\0'; + newitem->type = type; + newitem->prev = newitem->next = NULL; + + /* Perform simple linear search for ordered storage */ + if (!suggestions.first || !suggestions.last) { + suggestions.first = suggestions.last = newitem; + } else { + cmp = -1; + for (item=suggestions.last; item; item=item->prev) { + cmp = txttl_cmp(name, item->name, len); + + /* Newitem comes after this item, insert here */ + if (cmp >= 0) { + newitem->prev = item; + if (item->next) + item->next->prev = newitem; + newitem->next = item->next; + item->next = newitem; + + /* At last item, set last pointer here */ + if (item == suggestions.last) + suggestions.last = newitem; + break; + } + } + /* Reached beginning of list, insert before first */ + if (cmp < 0) { + newitem->next = suggestions.first; + suggestions.first->prev = newitem; + suggestions.first = newitem; + } + } + suggestions.firstmatch = suggestions.lastmatch = suggestions.selected = NULL; + suggestions.top= 0; +} + +void texttool_suggest_prefix(const char *prefix) { + SuggItem *match, *first, *last; + int cmp, len = strlen(prefix), top = 0; + + if (!suggestions.first) return; + if (len==0) { + suggestions.selected = suggestions.firstmatch = suggestions.first; + suggestions.lastmatch = suggestions.last; + return; + } + + first = last = NULL; + for (match=suggestions.first; match; match=match->next) { + cmp = txttl_cmp(prefix, match->name, len); + if (cmp==0) { + if (!first) { + first = match; + suggestions.top = top; + } + } else if (cmp<0) { + if (!last) { + last = match->prev; + break; + } + } + top++; + } + if (first) { + if (!last) last = suggestions.last; + suggestions.firstmatch = first; + suggestions.lastmatch = last; + suggestions.selected = first; + } else { + suggestions.firstmatch = NULL; + suggestions.lastmatch = NULL; + suggestions.selected = NULL; + suggestions.top = 0; + } +} + +void texttool_suggest_clear() { + txttl_free_suggest(); +} + +SuggItem *texttool_suggest_first() { + return suggestions.firstmatch; +} + +SuggItem *texttool_suggest_last() { + return suggestions.lastmatch; +} + +void texttool_suggest_select(SuggItem *sel) { + suggestions.selected = sel; +} + +SuggItem *texttool_suggest_selected() { + return suggestions.selected; +} + +int *texttool_suggest_top() { + return &suggestions.top; +} + +/*************************/ +/* Documentation methods */ +/*************************/ + +void texttool_docs_show(const char *docs) { + int len; + + if (!docs) return; + + len = strlen(docs); + + if (documentation) { + MEM_freeN(documentation); + documentation = NULL; + } + + /* Ensure documentation ends with a '\n' */ + if (docs[len-1] != '\n') { + documentation = MEM_mallocN(len+2, "Documentation"); + strncpy(documentation, docs, len); + documentation[len++] = '\n'; + } else { + documentation = MEM_mallocN(len+1, "Documentation"); + strncpy(documentation, docs, len); + } + documentation[len] = '\0'; +} + +char *texttool_docs_get() { + return documentation; +} + +void texttool_docs_clear() { + txttl_free_docs(); +} diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index 53b7bb975a3..9e6efa59d71 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -30,6 +30,8 @@ */ #include <string.h> /* strstr */ +#include <sys/types.h> +#include <sys/stat.h> #include "MEM_guardedalloc.h" @@ -81,12 +83,19 @@ The st->top determines at what line the top of the text is displayed. If the user moves the cursor the st containing that cursor should be popped ... other st's retain their own top location. -*/ /***************/ - +Markers +-- +The mrk->flags define the behaviour and relationships between markers. The +upper two bytes are used to hold a group ID, the lower two are normal flags. If +TMARK_EDITALL is set the group ID defines which other markers should be edited. -/****************/ /* - Undo +The mrk->clr field is used to visually group markers where the flags may not +match. A template system, for example, may allow editing of repeating tokens +(in one group) but include other marked positions (in another group) all in the +same template with the same colour. +Undo +-- Undo/Redo works by storing events in a queue, and a pointer to the current position in the @@ -145,6 +154,7 @@ void free_text(Text *text) } BLI_freelistN(&text->lines); + BLI_freelistN(&text->markers); if(text->name) MEM_freeN(text->name); MEM_freeN(text->undo_buf); @@ -169,10 +179,11 @@ Text *add_empty_text(char *name) ta->flags= TXT_ISDIRTY | TXT_ISTMP | TXT_ISMEM; ta->lines.first= ta->lines.last= NULL; + ta->markers.first= ta->markers.last= NULL; tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline"); tmp->line= (char*) MEM_mallocN(1, "textline_string"); - tmp->format= (char*) MEM_mallocN(2, "Syntax_format"); + tmp->format= NULL; tmp->line[0]=0; tmp->len= 0; @@ -209,11 +220,12 @@ static void cleanup_textline(TextLine * tl) int reopen_text(Text *text) { FILE *fp; - int i, llen, len; + int i, llen, len, res; unsigned char *buffer; TextLine *tmp; char sfile[FILE_MAXFILE]; char str[FILE_MAXDIR+FILE_MAXFILE]; + struct stat st; if (!text || !text->name) return 0; @@ -242,7 +254,7 @@ int reopen_text(Text *text) text->undo_len= TXT_INIT_UNDO; text->undo_buf= MEM_mallocN(text->undo_len, "undo buf"); - text->flags= TXT_ISDIRTY | TXT_ISTMP; + text->flags= TXT_ISTMP; fseek(fp, 0L, SEEK_END); len= ftell(fp); @@ -256,6 +268,9 @@ int reopen_text(Text *text) len = fread(buffer, 1, len, fp); fclose(fp); + + res= stat(str, &st); + text->mtime= st.st_mtime; text->nlines=0; i=0; @@ -264,7 +279,7 @@ int reopen_text(Text *text) if (buffer[i]=='\n') { tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline"); tmp->line= (char*) MEM_mallocN(llen+1, "textline_string"); - tmp->format= (char*) MEM_mallocN(llen+2, "Syntax_format"); + tmp->format= NULL; if(llen) memcpy(tmp->line, &buffer[i-llen], llen); tmp->line[llen]=0; @@ -284,7 +299,7 @@ int reopen_text(Text *text) if (llen!=0 || text->nlines==0) { tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline"); tmp->line= (char*) MEM_mallocN(llen+1, "textline_string"); - tmp->format= (char*) MEM_mallocN(llen+2, "Syntax_format"); + tmp->format= NULL; if(llen) memcpy(tmp->line, &buffer[i-llen], llen); @@ -307,12 +322,13 @@ int reopen_text(Text *text) Text *add_text(char *file) { FILE *fp; - int i, llen, len; + int i, llen, len, res; unsigned char *buffer; TextLine *tmp; Text *ta; char sfile[FILE_MAXFILE]; char str[FILE_MAXDIR+FILE_MAXFILE]; + struct stat st; BLI_strncpy(str, file, FILE_MAXDIR+FILE_MAXFILE); if (G.scene) /* can be NULL (bg mode) */ @@ -326,6 +342,7 @@ Text *add_text(char *file) ta->id.us= 1; ta->lines.first= ta->lines.last= NULL; + ta->markers.first= ta->markers.last= NULL; ta->curl= ta->sell= NULL; /* ta->flags= TXT_ISTMP | TXT_ISEXT; */ @@ -348,6 +365,9 @@ Text *add_text(char *file) len = fread(buffer, 1, len, fp); fclose(fp); + + res= stat(str, &st); + ta->mtime= st.st_mtime; ta->nlines=0; i=0; @@ -356,7 +376,7 @@ Text *add_text(char *file) if (buffer[i]=='\n') { tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline"); tmp->line= (char*) MEM_mallocN(llen+1, "textline_string"); - tmp->format= (char*) MEM_mallocN(llen+2, "Syntax_format"); + tmp->format= NULL; if(llen) memcpy(tmp->line, &buffer[i-llen], llen); tmp->line[llen]=0; @@ -376,7 +396,7 @@ Text *add_text(char *file) if (llen!=0 || ta->nlines==0) { tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline"); tmp->line= (char*) MEM_mallocN(llen+1, "textline_string"); - tmp->format= (char*) MEM_mallocN(llen+2, "Syntax_format"); + tmp->format= NULL; if(llen) memcpy(tmp->line, &buffer[i-llen], llen); @@ -410,6 +430,7 @@ Text *copy_text(Text *ta) tan->flags = ta->flags | TXT_ISDIRTY | TXT_ISTMP; tan->lines.first= tan->lines.last= NULL; + tan->markers.first= tan->markers.last= NULL; tan->curl= tan->sell= NULL; tan->nlines= ta->nlines; @@ -419,7 +440,7 @@ Text *copy_text(Text *ta) while (line) { tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline"); tmp->line= MEM_mallocN(line->len+1, "textline_string"); - tmp->format= MEM_mallocN(line->len+2, "Syntax_format"); + tmp->format= NULL; strcpy(tmp->line, line->line); @@ -440,14 +461,14 @@ Text *copy_text(Text *ta) /* Editing utility functions */ /*****************************/ -static void make_new_line (TextLine *line, char *newline, char *newformat) +static void make_new_line (TextLine *line, char *newline) { if (line->line) MEM_freeN(line->line); if (line->format) MEM_freeN(line->format); line->line= newline; line->len= strlen(newline); - line->format= newformat; + line->format= NULL; } static TextLine *txt_new_line(char *str) @@ -458,7 +479,7 @@ static TextLine *txt_new_line(char *str) tmp= (TextLine *) MEM_mallocN(sizeof(TextLine), "textline"); tmp->line= MEM_mallocN(strlen(str)+1, "textline_string"); - tmp->format= MEM_mallocN(strlen(str)+2, "Syntax_format"); + tmp->format= NULL; strcpy(tmp->line, str); @@ -476,7 +497,7 @@ static TextLine *txt_new_linen(char *str, int n) tmp= (TextLine *) MEM_mallocN(sizeof(TextLine), "textline"); tmp->line= MEM_mallocN(n+1, "textline_string"); - tmp->format= MEM_mallocN(n+2, "Syntax_format"); + tmp->format= NULL; BLI_strncpy(tmp->line, str, n+1); @@ -553,6 +574,20 @@ static void txt_make_dirty (Text *text) if (text->compiled) BPY_free_compiled_text(text); } +/* 0:whitespace, 1:punct, 2:alphanumeric */ +static short txt_char_type (char ch) +{ + if (ch <= ' ') return 0; /* 32 */ + if (ch <= '/') return 1; /* 47 */ + if (ch <= '9') return 2; /* 57 */ + if (ch <= '@') return 1; /* 64 */ + if (ch <= 'Z') return 2; /* 90 */ + if (ch == '_') return 2; /* 95, dont delimit '_' */ + if (ch <= '`') return 1; /* 96 */ + if (ch <= 'z') return 2; /* 122 */ + return 1; +} + /****************************/ /* Cursor utility functions */ /****************************/ @@ -606,8 +641,7 @@ void txt_move_up(Text *text, short sel) if(!undoing) txt_undo_add_op(text, sel?UNDO_SUP:UNDO_CUP); } } else { - *charp= 0; - if(!undoing) txt_undo_add_op(text, sel?UNDO_SUP:UNDO_CUP); + txt_move_bol(text, sel); } if(!sel) txt_pop_sel(text); @@ -632,8 +666,7 @@ void txt_move_down(Text *text, short sel) } else if(!undoing) txt_undo_add_op(text, sel?UNDO_SDOWN:UNDO_CDOWN); } else { - *charp= (*linep)->len; - if(!undoing) txt_undo_add_op(text, sel?UNDO_SDOWN:UNDO_CDOWN); + txt_move_eol(text, sel); } if(!sel) txt_pop_sel(text); @@ -689,6 +722,68 @@ void txt_move_right(Text *text, short sel) if(!sel) txt_pop_sel(text); } +void txt_jump_left(Text *text, short sel) +{ + TextLine **linep, *oldl; + int *charp, oldc, count, i; + unsigned char oldu; + + if (!text) return; + if(sel) txt_curs_sel(text, &linep, &charp); + else { txt_pop_first(text); txt_curs_cur(text, &linep, &charp); } + if (!*linep) return; + + oldl= *linep; + oldc= *charp; + oldu= undoing; + undoing= 1; /* Don't push individual moves to undo stack */ + + count= 0; + for (i=0; i<3; i++) { + if (count < 2) { + while (*charp>0 && txt_char_type((*linep)->line[*charp-1])==i) { + txt_move_left(text, sel); + count++; + } + } + } + if (count==0) txt_move_left(text, sel); + + undoing= oldu; + if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, oldl), oldc, txt_get_span(text->lines.first, *linep), (unsigned short)*charp); +} + +void txt_jump_right(Text *text, short sel) +{ + TextLine **linep, *oldl; + int *charp, oldc, count, i; + unsigned char oldu; + + if (!text) return; + if(sel) txt_curs_sel(text, &linep, &charp); + else { txt_pop_last(text); txt_curs_cur(text, &linep, &charp); } + if (!*linep) return; + + oldl= *linep; + oldc= *charp; + oldu= undoing; + undoing= 1; /* Don't push individual moves to undo stack */ + + count= 0; + for (i=0; i<3; i++) { + if (count < 2) { + while (*charp<(*linep)->len && txt_char_type((*linep)->line[*charp])==i) { + txt_move_right(text, sel); + count++; + } + } + } + if (count==0) txt_move_right(text, sel); + + undoing= oldu; + if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, oldl), oldc, txt_get_span(text->lines.first, *linep), (unsigned short)*charp); +} + void txt_move_bol (Text *text, short sel) { TextLine **linep; @@ -761,6 +856,11 @@ void txt_move_eof (Text *text, short sel) void txt_move_toline (Text *text, unsigned int line, short sel) { + txt_move_to(text, line, 0, sel); +} + +void txt_move_to (Text *text, unsigned int line, unsigned int ch, short sel) +{ TextLine **linep, *oldl; int *charp, oldc; unsigned int i; @@ -777,10 +877,12 @@ void txt_move_toline (Text *text, unsigned int line, short sel) if ((*linep)->next) *linep= (*linep)->next; else break; } - *charp= 0; + if (ch>(*linep)->len) + ch= (*linep)->len; + *charp= ch; if(!sel) txt_pop_sel(text); - if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, oldl), oldc, txt_get_span(text->lines.first, *linep), (unsigned short)*charp); + if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, oldl), oldc, txt_get_span(text->lines.first, *linep), (unsigned short)*charp); } /****************************/ @@ -865,7 +967,9 @@ int txt_has_sel(Text *text) static void txt_delete_sel (Text *text) { TextLine *tmpl; - char *buf, *format; + TextMarker *mrk; + char *buf; + int move, lineno; if (!text) return; if (!text->curl) return; @@ -882,13 +986,33 @@ static void txt_delete_sel (Text *text) } buf= MEM_mallocN(text->curc+(text->sell->len - text->selc)+1, "textline_string"); - format= MEM_mallocN(text->curc+(text->sell->len - text->selc)+2, "Syntax_format"); + if (text->curl != text->sell) { + txt_clear_marker_region(text, text->curl, text->curc, text->curl->len, 0, 0); + move= txt_get_span(text->curl, text->sell); + } else { + mrk= txt_find_marker_region(text, text->curl, text->curc, text->selc, 0, 0); + if (mrk && (mrk->start > text->curc || mrk->end < text->selc)) + txt_clear_marker_region(text, text->curl, text->curc, text->selc, 0, 0); + move= 0; + } + + mrk= txt_find_marker_region(text, text->sell, text->selc-1, text->sell->len, 0, 0); + if (mrk) { + lineno= mrk->lineno; + do { + mrk->lineno -= move; + if (mrk->start > text->curc) mrk->start -= text->selc - text->curc; + mrk->end -= text->selc - text->curc; + mrk= mrk->next; + } while (mrk && mrk->lineno==lineno); + } + strncpy(buf, text->curl->line, text->curc); strcpy(buf+text->curc, text->sell->line + text->selc); buf[text->curc+(text->sell->len - text->selc)]=0; - make_new_line(text->curl, buf, format); + make_new_line(text->curl, buf); tmpl= text->sell; while (tmpl != text->curl) { @@ -995,22 +1119,31 @@ char *txt_to_buf (Text *text) return buf; } -int txt_find_string(Text *text, char *findstr) +int txt_find_string(Text *text, char *findstr, int wrap) { TextLine *tl, *startl; char *s= NULL; + int oldcl, oldsl, oldcc, oldsc; if (!text || !text->curl || !text->sell) return 0; txt_order_cursors(text); + oldcl= txt_get_span(text->lines.first, text->curl); + oldsl= txt_get_span(text->lines.first, text->sell); tl= startl= text->sell; + oldcc= text->curc; + oldsc= text->selc; s= strstr(&tl->line[text->selc], findstr); while (!s) { tl= tl->next; - if (!tl) - tl= text->lines.first; + if (!tl) { + if (wrap) + tl= text->lines.first; + else + break; + } s= strstr(tl->line, findstr); if (tl==startl) @@ -1018,10 +1151,10 @@ int txt_find_string(Text *text, char *findstr) } if (s) { - text->curl= text->sell= tl; - text->curc= (int) (s-tl->line); - text->selc= text->curc + strlen(findstr); - + int newl= txt_get_span(text->lines.first, tl); + int newc= (int)(s-tl->line); + txt_move_to(text, newl, newc, 0); + txt_move_to(text, newl, newc + strlen(findstr), 1); return 1; } else return 0; @@ -1621,7 +1754,6 @@ void txt_do_undo(Text *text) case UNDO_SWAP: txt_curs_swap(text); - txt_do_undo(text); /* swaps should appear transparent */ break; case UNDO_DBLOCK: @@ -1736,6 +1868,19 @@ void txt_do_undo(Text *text) break; } + + /* next undo step may need evaluating */ + if (text->undo_pos>=0) { + switch (text->undo_buf[text->undo_pos]) { + case UNDO_STO: + txt_do_undo(text); + txt_do_redo(text); /* selections need restoring */ + break; + case UNDO_SWAP: + txt_do_undo(text); /* swaps should appear transparent */ + break; + } + } undoing= 0; } @@ -1810,7 +1955,7 @@ void txt_do_redo(Text *text) case UNDO_SWAP: txt_curs_swap(text); - txt_do_undo(text); /* swaps should appear transparent a*/ + txt_do_redo(text); /* swaps should appear transparent a*/ break; case UNDO_CTO: @@ -1947,22 +2092,37 @@ void txt_do_redo(Text *text) void txt_split_curline (Text *text) { TextLine *ins; - char *left, *right, *fleft, *fright; + TextMarker *mrk; + char *left, *right; + int lineno= -1; if (!text) return; if (!text->curl) return; - txt_delete_sel(text); + txt_delete_sel(text); + + /* Move markers */ + + lineno= txt_get_span(text->lines.first, text->curl); + mrk= text->markers.first; + while (mrk) { + if (mrk->lineno==lineno && mrk->start>text->curc) { + mrk->lineno++; + mrk->start -= text->curc; + mrk->end -= text->curc; + } else if (mrk->lineno > lineno) { + mrk->lineno++; + } + mrk= mrk->next; + } /* Make the two half strings */ left= MEM_mallocN(text->curc+1, "textline_string"); - fleft= MEM_mallocN(text->curc+2, "Syntax_format"); if (text->curc) memcpy(left, text->curl->line, text->curc); left[text->curc]=0; right= MEM_mallocN(text->curl->len - text->curc+1, "textline_string"); - fright= MEM_mallocN(text->curl->len - text->curc+2, "Syntax_format"); if (text->curl->len - text->curc) memcpy(right, text->curl->line+text->curc, text->curl->len-text->curc); right[text->curl->len - text->curc]=0; @@ -1973,11 +2133,11 @@ void txt_split_curline (Text *text) ins= MEM_mallocN(sizeof(TextLine), "textline"); ins->line= left; - ins->format= fleft; + ins->format= NULL; ins->len= text->curc; text->curl->line= right; - text->curl->format= fright; + text->curl->format= NULL; text->curl->len= text->curl->len - text->curc; BLI_insertlinkbefore(&text->lines, text->curl, ins); @@ -1993,9 +2153,23 @@ void txt_split_curline (Text *text) static void txt_delete_line (Text *text, TextLine *line) { + TextMarker *mrk=NULL, *nxt; + int lineno= -1; + if (!text) return; if (!text->curl) return; + lineno= txt_get_span(text->lines.first, line); + mrk= text->markers.first; + while (mrk) { + nxt= mrk->next; + if (mrk->lineno==lineno) + BLI_freelinkN(&text->markers, mrk); + else if (mrk->lineno > lineno) + mrk->lineno--; + mrk= nxt; + } + BLI_remlink (&text->lines, line); if (line->line) MEM_freeN(line->line); @@ -2009,21 +2183,35 @@ static void txt_delete_line (Text *text, TextLine *line) static void txt_combine_lines (Text *text, TextLine *linea, TextLine *lineb) { - char *tmp, *format; + char *tmp; + TextMarker *mrk= NULL; + int lineno=-1; if (!text) return; if(!linea || !lineb) return; + + mrk= txt_find_marker_region(text, lineb, 0, lineb->len, 0, 0); + if (mrk) { + lineno= mrk->lineno; + do { + mrk->lineno--; + mrk->start += linea->len; + mrk->end += linea->len; + mrk= mrk->next; + } while (mrk && mrk->lineno==lineno); + } + if (lineno==-1) lineno= txt_get_span(text->lines.first, lineb); + if (!mrk) mrk= text->markers.first; tmp= MEM_mallocN(linea->len+lineb->len+1, "textline_string"); - format= MEM_mallocN(linea->len+lineb->len+1, "Syntax_format"); strcpy(tmp, linea->line); strcat(tmp, lineb->line); - make_new_line(linea, tmp, format); + make_new_line(linea, tmp); - txt_delete_line(text, lineb); + txt_delete_line(text, lineb); txt_make_dirty(text); txt_clean_text(text); @@ -2037,8 +2225,9 @@ void txt_delete_char (Text *text) if (!text->curl) return; if (txt_has_sel(text)) { /* deleting a selection */ - txt_delete_sel(text); - return; + txt_delete_sel(text); + txt_make_dirty(text); + return; } else if (text->curc== text->curl->len) { /* Appending two lines */ if (text->curl->next) { @@ -2047,6 +2236,24 @@ void txt_delete_char (Text *text) } } else { /* Just deleting a char */ int i= text->curc; + + TextMarker *mrk= txt_find_marker_region(text, text->curl, i-1, text->curl->len, 0, 0); + if (mrk) { + int lineno= mrk->lineno; + if (mrk->end==i) { + if ((mrk->flags & TMARK_TEMP) && !(mrk->flags & TMARK_EDITALL)) { + txt_clear_markers(text, mrk->group, TMARK_TEMP); + } else { + BLI_freelinkN(&text->markers, mrk); + } + return; + } + do { + if (mrk->start>i) mrk->start--; + mrk->end--; + mrk= mrk->next; + } while (mrk && mrk->lineno==lineno); + } c= text->curl->line[i]; while(i< text->curl->len) { @@ -2064,6 +2271,12 @@ void txt_delete_char (Text *text) if(!undoing) txt_undo_add_charop(text, UNDO_DEL, c); } +void txt_delete_word (Text *text) +{ + txt_jump_right(text, 1); + txt_delete_sel(text); +} + void txt_backspace_char (Text *text) { char c='\n'; @@ -2072,8 +2285,9 @@ void txt_backspace_char (Text *text) if (!text->curl) return; if (txt_has_sel(text)) { /* deleting a selection */ - txt_delete_sel(text); - return; + txt_delete_sel(text); + txt_make_dirty(text); + return; } else if (text->curc==0) { /* Appending two lines */ if (!text->curl->prev) return; @@ -2083,19 +2297,37 @@ void txt_backspace_char (Text *text) txt_combine_lines(text, text->curl, text->curl->next); txt_pop_sel(text); - } + } else { /* Just backspacing a char */ - int i= text->curc-1; - - c= text->curl->line[i]; - while(i< text->curl->len) { - text->curl->line[i]= text->curl->line[i+1]; - i++; - } - text->curl->len--; - text->curc--; + int i= text->curc-1; + + TextMarker *mrk= txt_find_marker_region(text, text->curl, i, text->curl->len, 0, 0); + if (mrk) { + int lineno= mrk->lineno; + if (mrk->start==i+1) { + if ((mrk->flags & TMARK_TEMP) && !(mrk->flags & TMARK_EDITALL)) { + txt_clear_markers(text, mrk->group, TMARK_TEMP); + } else { + BLI_freelinkN(&text->markers, mrk); + } + return; + } + do { + if (mrk->start>i) mrk->start--; + mrk->end--; + mrk= mrk->next; + } while (mrk && mrk->lineno==lineno); + } - txt_pop_sel(text); + c= text->curl->line[i]; + while(i< text->curl->len) { + text->curl->line[i]= text->curl->line[i+1]; + i++; + } + text->curl->len--; + text->curc--; + + txt_pop_sel(text); } txt_make_dirty(text); @@ -2104,10 +2336,17 @@ void txt_backspace_char (Text *text) if(!undoing) txt_undo_add_charop(text, UNDO_BS, c); } +void txt_backspace_word (Text *text) +{ + txt_jump_left(text, 1); + txt_delete_sel(text); +} + int txt_add_char (Text *text, char add) { - int len; - char *tmp, *format; + int len, lineno; + char *tmp; + TextMarker *mrk; if (!text) return 0; if (!text->curl) return 0; @@ -2119,8 +2358,17 @@ int txt_add_char (Text *text, char add) txt_delete_sel(text); + mrk= txt_find_marker_region(text, text->curl, text->curc-1, text->curl->len, 0, 0); + if (mrk) { + lineno= mrk->lineno; + do { + if (mrk->start>text->curc) mrk->start++; + mrk->end++; + mrk= mrk->next; + } while (mrk && mrk->lineno==lineno); + } + tmp= MEM_mallocN(text->curl->len+2, "textline_string"); - format= MEM_mallocN(text->curl->len+4, "Syntax_format"); if(text->curc) memcpy(tmp, text->curl->line, text->curc); tmp[text->curc]= add; @@ -2128,7 +2376,7 @@ int txt_add_char (Text *text, char add) len= text->curl->len - text->curc; if(len>0) memcpy(tmp+text->curc+1, text->curl->line+text->curc, len); tmp[text->curl->len+1]=0; - make_new_line(text->curl, tmp, format); + make_new_line(text->curl, tmp); text->curc++; @@ -2141,10 +2389,42 @@ int txt_add_char (Text *text, char add) return 1; } +int txt_replace_char (Text *text, char add) +{ + char del; + + if (!text) return 0; + if (!text->curl) return 0; + + /* If text is selected or we're at the end of the line just use txt_add_char */ + if (text->curc==text->curl->len || txt_has_sel(text) || add=='\n') { + TextMarker *mrk; + int i= txt_add_char(text, add); + mrk= txt_find_marker(text, text->curl, text->curc, 0, 0); + if (mrk && mrk->end==text->curc) mrk->end--; + return i; + } + + del= text->curl->line[text->curc]; + text->curl->line[text->curc]= (unsigned char) add; + text->curc++; + txt_pop_sel(text); + + txt_make_dirty(text); + txt_clean_text(text); + + /* Should probably create a new op for this */ + if(!undoing) { + txt_undo_add_charop(text, UNDO_DEL, del); + txt_undo_add_charop(text, UNDO_INSERT, add); + } + return 1; +} + void indent(Text *text) { int len, num; - char *tmp, *format; + char *tmp; char add = '\t'; if (!text) return; @@ -2155,7 +2435,6 @@ void indent(Text *text) while (TRUE) { tmp= MEM_mallocN(text->curl->len+2, "textline_string"); - format= MEM_mallocN(text->curl->len+3, "Syntax_format"); text->curc = 0; if(text->curc) memcpy(tmp, text->curl->line, text->curc); @@ -2165,7 +2444,7 @@ void indent(Text *text) if(len>0) memcpy(tmp+text->curc+1, text->curl->line+text->curc, len); tmp[text->curl->len+1]=0; - make_new_line(text->curl, tmp, format); + make_new_line(text->curl, tmp); text->curc++; @@ -2246,7 +2525,7 @@ void unindent(Text *text) void comment(Text *text) { int len, num; - char *tmp, *format; + char *tmp; char add = '#'; if (!text) return; @@ -2257,7 +2536,6 @@ void comment(Text *text) while (TRUE) { tmp= MEM_mallocN(text->curl->len+2, "textline_string"); - format = MEM_mallocN(text->curl->len+3, "Syntax_format"); text->curc = 0; if(text->curc) memcpy(tmp, text->curl->line, text->curc); @@ -2267,7 +2545,7 @@ void comment(Text *text) if(len>0) memcpy(tmp+text->curc+1, text->curl->line+text->curc, len); tmp[text->curl->len+1]=0; - make_new_line(text->curl, tmp, format); + make_new_line(text->curl, tmp); text->curc++; @@ -2398,3 +2676,141 @@ int setcurr_tab (Text *text) return i; } +/*********************************/ +/* Text marker utility functions */ +/*********************************/ + +/* Creates and adds a marker to the list maintaining sorted order */ +void txt_add_marker(Text *text, TextLine *line, int start, int end, char color[4], int group, int flags) { + TextMarker *tmp, *marker; + + marker= MEM_mallocN(sizeof(TextMarker), "text_marker"); + + marker->lineno= txt_get_span(text->lines.first, line); + marker->start= MIN2(start, end); + marker->end= MAX2(start, end); + marker->group= group; + marker->flags= flags; + + marker->color[0]= color[0]; + marker->color[1]= color[1]; + marker->color[2]= color[2]; + marker->color[3]= color[3]; + + for (tmp=text->markers.last; tmp; tmp=tmp->prev) + if (tmp->lineno < marker->lineno || (tmp->lineno==marker->lineno && tmp->start < marker->start)) + break; + + if (tmp) BLI_insertlinkafter(&text->markers, tmp, marker); + else BLI_addhead(&text->markers, marker); +} + +/* Returns the first matching marker on the specified line between two points. + If the group or flags fields are non-zero the returned flag must be in the + specified group and have at least the specified flags set. */ +TextMarker *txt_find_marker_region(Text *text, TextLine *line, int start, int end, int group, int flags) { + TextMarker *marker, *next; + int lineno= txt_get_span(text->lines.first, line); + + for (marker=text->markers.first; marker; marker=next) { + next= marker->next; + + if (group && marker->group != group) continue; + else if ((marker->flags & flags) != flags) continue; + else if (marker->lineno < lineno) continue; + else if (marker->lineno > lineno) break; + + if ((marker->start==marker->end && start<=marker->start && marker->start<=end) || + (marker->start<end && marker->end>start)) + return marker; + } + return NULL; +} + +/* Clears all markers on the specified line between two points. If the group or + flags fields are non-zero the returned flag must be in the specified group + and have at least the specified flags set. */ +short txt_clear_marker_region(Text *text, TextLine *line, int start, int end, int group, int flags) { + TextMarker *marker, *next; + int lineno= txt_get_span(text->lines.first, line); + short cleared= 0; + + for (marker=text->markers.first; marker; marker=next) { + next= marker->next; + + if (group && marker->group != group) continue; + else if ((marker->flags & flags) != flags) continue; + else if (marker->lineno < lineno) continue; + else if (marker->lineno > lineno) break; + + if ((marker->start==marker->end && start<=marker->start && marker->start<=end) || + (marker->start<end && marker->end>start)) { + BLI_freelinkN(&text->markers, marker); + cleared= 1; + } + } + return cleared; +} + +/* Clears all markers in the specified group (if given) with at least the + specified flags set. Useful for clearing temporary markers (group=0, + flags=TMARK_TEMP) */ +short txt_clear_markers(Text *text, int group, int flags) { + TextMarker *marker, *next; + short cleared= 0; + + for (marker=text->markers.first; marker; marker=next) { + next= marker->next; + + if ((!group || marker->group==group) && + (marker->flags & flags) == flags) { + BLI_freelinkN(&text->markers, marker); + cleared= 1; + } + } + return cleared; +} + +/* Finds the marker at the specified line and cursor position with at least the + specified flags set in the given group (if non-zero). */ +TextMarker *txt_find_marker(Text *text, TextLine *line, int curs, int group, int flags) { + TextMarker *marker; + int lineno= txt_get_span(text->lines.first, line); + + for (marker=text->markers.first; marker; marker=marker->next) { + if (group && marker->group != group) continue; + else if ((marker->flags & flags) != flags) continue; + else if (marker->lineno < lineno) continue; + else if (marker->lineno > lineno) break; + + if (marker->start <= curs && curs <= marker->end) + return marker; + } + return NULL; +} + +/* Finds the previous marker in the same group. If no other is found, the same + marker will be returned */ +TextMarker *txt_prev_marker(Text *text, TextMarker *marker) { + TextMarker *tmp= marker; + while (tmp) { + if (tmp->prev) tmp= tmp->prev; + else tmp= text->markers.last; + if (tmp->group == marker->group) + return tmp; + } + return NULL; /* Only if marker==NULL */ +} + +/* Finds the next marker in the same group. If no other is found, the same + marker will be returned */ +TextMarker *txt_next_marker(Text *text, TextMarker *marker) { + TextMarker *tmp= marker; + while (tmp) { + if (tmp->next) tmp= tmp->next; + else tmp= text->markers.first; + if (tmp->group == marker->group) + return tmp; + } + return NULL; /* Only if marker==NULL */ +} diff --git a/source/blender/blenkernel/intern/texture.c b/source/blender/blenkernel/intern/texture.c index 936381c85cc..bb726887d32 100644 --- a/source/blender/blenkernel/intern/texture.c +++ b/source/blender/blenkernel/intern/texture.c @@ -53,6 +53,7 @@ #include "DNA_world_types.h" #include "DNA_brush_types.h" #include "DNA_node_types.h" +#include "DNA_color_types.h" #include "DNA_scene_types.h" #include "IMB_imbuf_types.h" @@ -390,6 +391,17 @@ int do_colorband(ColorBand *coba, float in, float out[4]) return 1; /* OK */ } +void colorband_table_RGBA(ColorBand *coba, float **array, int *size) +{ + int a; + + *size = CM_TABLE+1; + *array = MEM_callocN(sizeof(float)*(*size)*4, "ColorBand"); + + for(a=0; a<*size; a++) + do_colorband(coba, (float)a/(float)CM_TABLE, &(*array)[a*4]); +} + /* ******************* TEX ************************ */ void free_texture(Tex *tex) diff --git a/source/blender/blenkernel/intern/writeframeserver.c b/source/blender/blenkernel/intern/writeframeserver.c index 6b38d7deadb..40e1dc1bb03 100644 --- a/source/blender/blenkernel/intern/writeframeserver.c +++ b/source/blender/blenkernel/intern/writeframeserver.c @@ -251,7 +251,7 @@ static int handle_request(char * req) return -1; } -int frameserver_loop() +int frameserver_loop(void) { fd_set readfds; struct timeval tv; |