SVN maintenance.
[blender.git] / source / blender / editors / space_view3d / drawanimviz.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2009 by the Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Joshua Leung
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_anim_types.h"
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_ID.h"
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_view3d_types.h"
50 #include "DNA_userdef_types.h"
51
52 #include "BLI_blenlib.h"
53 #include "BLI_math.h"
54 #include "BLI_dlrbTree.h"
55
56 #include "BKE_anim.h"
57 #include "BKE_animsys.h"
58 #include "BKE_action.h"
59 #include "BKE_armature.h"
60 #include "BKE_constraint.h"
61 #include "BKE_context.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_DerivedMesh.h"
64 #include "BKE_global.h"
65 #include "BKE_main.h"
66 #include "BKE_modifier.h"
67 #include "BKE_nla.h"
68 #include "BKE_object.h"
69 #include "BKE_utildefines.h"
70
71 #include "BIF_gl.h"
72 #include "BIF_glutil.h"
73
74 #include "ED_armature.h"
75 #include "ED_anim_api.h"
76 #include "ED_keyframes_draw.h"
77
78 #include "WM_api.h"
79 #include "WM_types.h"
80 #include "BLF_api.h"
81
82 #include "UI_resources.h"
83
84 #include "view3d_intern.h"
85
86 /* ************************************ Motion Paths ************************************* */
87
88 // TODO: 
89 //      - options to draw paths with lines
90 //      - include support for editing the path verts
91
92 /* Set up drawing environment for drawing motion paths */
93 void draw_motion_paths_init(Scene *scene, View3D *v3d, ARegion *ar) 
94 {
95         RegionView3D *rv3d= ar->regiondata;
96         
97         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
98         
99         glPushMatrix();
100         glLoadMatrixf(rv3d->viewmat);
101 }
102
103 /* Draw the given motion path for an Object or a Bone 
104  *      - assumes that the viewport has already been initialised properly
105  *              i.e. draw_motion_paths_init() has been called
106  */
107 void draw_motion_path_instance(Scene *scene, View3D *v3d, ARegion *ar, 
108                         Object *ob, bPoseChannel *pchan, bAnimVizSettings *avs, bMotionPath *mpath)
109 {
110         //RegionView3D *rv3d= ar->regiondata;
111         bMotionPathVert *mpv, *mpv_start;
112         int i, stepsize= avs->path_step;
113         int sfra, efra, len;
114         
115         
116         /* get frame ranges */
117         if (avs->path_type == MOTIONPATH_TYPE_ACFRA) {
118                 int sind;
119                 
120                 /* With "Around Current", we only choose frames from around 
121                  * the current frame to draw. However, this range is still 
122                  * restricted by the limits of the original path.
123                  */
124                 sfra= CFRA - avs->path_bc;
125                 efra= CFRA + avs->path_ac;
126                 if (sfra < mpath->start_frame) sfra= mpath->start_frame;
127                 if (efra > mpath->end_frame) efra= mpath->end_frame;
128                 
129                 len= efra - sfra;
130                 
131                 sind= sfra - mpath->start_frame;
132                 mpv_start= (mpath->points + sind);
133         }
134         else {
135                 sfra= mpath->start_frame;
136                 efra = sfra + mpath->length;
137                 len = mpath->length;
138                 mpv_start= mpath->points;
139         }
140         
141         /* draw curve-line of path */
142         glShadeModel(GL_SMOOTH);
143         
144         glBegin(GL_LINE_STRIP);                                 
145         for (i=0, mpv=mpv_start; i < len; i++, mpv++) {
146                 short sel= (pchan) ? (pchan->bone->flag & BONE_SELECTED) : (ob->flag & SELECT);
147                 float intensity; /* how faint */
148                 
149                 /* set color
150                  *      - more intense for active/selected bones, less intense for unselected bones
151                  *      - black for before current frame, green for current frame, blue for after current frame
152                  *      - intensity decreases as distance from current frame increases
153                  */
154                 #define SET_INTENSITY(A, B, C, min, max) (((1.0f - ((C - B) / (C - A))) * (max-min)) + min) 
155                 if ((sfra+i) < CFRA) {
156                         /* black - before cfra */
157                         if (sel) {
158                                 // intensity= 0.5f;
159                                 intensity = SET_INTENSITY(sfra, i, CFRA, 0.25f, 0.75f);
160                         }
161                         else {
162                                 //intensity= 0.8f;
163                                 intensity = SET_INTENSITY(sfra, i, CFRA, 0.68f, 0.92f);
164                         }
165                         UI_ThemeColorBlend(TH_WIRE, TH_BACK, intensity);
166                 }
167                 else if ((sfra+i) > CFRA) {
168                         /* blue - after cfra */
169                         if (sel) {
170                                 //intensity = 0.5f;
171                                 intensity = SET_INTENSITY(CFRA, i, efra, 0.25f, 0.75f);
172                         }
173                         else {
174                                 //intensity = 0.8f;
175                                 intensity = SET_INTENSITY(CFRA, i, efra, 0.68f, 0.92f);
176                         }
177                         UI_ThemeColorBlend(TH_BONE_POSE, TH_BACK, intensity);
178                 }
179                 else {
180                         /* green - on cfra */
181                         if (sel) {
182                                 intensity= 0.5f;
183                         }
184                         else {
185                                 intensity= 0.99f;
186                         }
187                         UI_ThemeColorBlendShade(TH_CFRAME, TH_BACK, intensity, 10);
188                 }       
189                 
190                 /* draw a vertex with this color */ 
191                 glVertex3fv(mpv->co);
192         }
193         
194         glEnd();
195         glShadeModel(GL_FLAT);
196         
197         glPointSize(1.0);
198         
199         /* draw little black point at each frame
200          * NOTE: this is not really visible/noticable
201          */
202         glBegin(GL_POINTS);
203         for (i=0, mpv=mpv_start; i < len; i++, mpv++) 
204                 glVertex3fv(mpv->co);
205         glEnd();
206         
207         /* Draw little white dots at each framestep value */
208         UI_ThemeColor(TH_TEXT_HI);
209         glBegin(GL_POINTS);
210         for (i=0, mpv=mpv_start; i < len; i+=stepsize, mpv+=stepsize) 
211                 glVertex3fv(mpv->co);
212         glEnd();
213         
214         /* Draw frame numbers at each framestep value */
215         if (avs->path_viewflag & MOTIONPATH_VIEW_FNUMS) {
216                 for (i=0, mpv=mpv_start; i < len; i+=stepsize, mpv+=stepsize) {
217                         char str[32];
218                         
219                         /* only draw framenum if several consecutive highlighted points don't occur on same point */
220                         if (i == 0) {
221                                 sprintf(str, "%d", (i+sfra));
222                                 view3d_cached_text_draw_add(mpv->co[0], mpv->co[1], mpv->co[2], str, 0);
223                         }
224                         else if ((i > stepsize) && (i < len-stepsize)) { 
225                                 bMotionPathVert *mpvP = (mpv - stepsize);
226                                 bMotionPathVert *mpvN = (mpv + stepsize);
227                                 
228                                 if ((equals_v3v3(mpv->co, mpvP->co)==0) || (equals_v3v3(mpv->co, mpvN->co)==0)) {
229                                         sprintf(str, "%d", (sfra+i));
230                                         view3d_cached_text_draw_add(mpv->co[0], mpv->co[1], mpv->co[2], str, 0);
231                                 }
232                         }
233                 }
234         }
235
236         /* Keyframes - dots and numbers */
237         if (avs->path_viewflag & MOTIONPATH_VIEW_KFNOS) {
238                 AnimData *adt= BKE_animdata_from_id(&ob->id);
239                 DLRBT_Tree keys;
240                 
241                 /* build list of all keyframes in active action for object or pchan */
242                 BLI_dlrbTree_init(&keys);
243                 
244                 if (adt) {
245                         /* for now, it is assumed that keyframes for bones are all grouped in a single group */
246                         if (pchan) {
247                                 bActionGroup *agrp= action_groups_find_named(adt->action, pchan->name);
248                                 
249                                 if (agrp) {
250                                         agroup_to_keylist(adt, agrp, &keys, NULL);
251                                         BLI_dlrbTree_linkedlist_sync(&keys);
252                                 }
253                         }
254                         else {
255                                 action_to_keylist(adt, adt->action, &keys, NULL);
256                                 BLI_dlrbTree_linkedlist_sync(&keys);
257                         }
258                 }
259                 
260                 /* Draw slightly-larger yellow dots at each keyframe */
261                 UI_ThemeColor(TH_VERTEX_SELECT);
262                 glPointSize(4.0f); // XXX perhaps a bit too big
263                 
264                 glBegin(GL_POINTS);
265                 for (i=0, mpv=mpv_start; i < len; i++, mpv++) {
266                         float mframe= (float)(sfra + i);
267                         
268                         if (BLI_dlrbTree_search_exact(&keys, compare_ak_cfraPtr, &mframe))
269                                 glVertex3fv(mpv->co);
270                 }
271                 glEnd();
272                 
273                 glPointSize(1.0f);
274                 
275                 /* Draw frame numbers of keyframes  */
276                 if (avs->path_viewflag & (MOTIONPATH_VIEW_FNUMS|MOTIONPATH_VIEW_KFNOS)) {
277                         for (i=0, mpv=mpv_start; i < len; i++, mpv++) {
278                                 float mframe= (float)(sfra + i);
279                                 
280                                 if (BLI_dlrbTree_search_exact(&keys, compare_ak_cfraPtr, &mframe)) {
281                                         char str[32];
282                                         
283                                         sprintf(str, "%d", (sfra+i));
284                                         view3d_cached_text_draw_add(mpv->co[0], mpv->co[1], mpv->co[2], str, 0);
285                                 }
286                         }
287                 }
288                 
289                 BLI_dlrbTree_free(&keys);
290         }
291 }
292
293 /* Clean up drawing environment after drawing motion paths */
294 void draw_motion_paths_cleanup(Scene *scene, View3D *v3d, ARegion *ar)
295 {
296         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
297         glPopMatrix();
298 }
299
300 #if 0 // XXX temp file guards 
301
302 /* ***************************** Onion Skinning (Ghosts) ******************************** */
303
304 #if 0 // XXX only for bones
305 /* helper function for ghost drawing - sets/removes flags for temporarily 
306  * hiding unselected bones while drawing ghosts
307  */
308 static void ghost_poses_tag_unselected(Object *ob, short unset)
309 {
310         bArmature *arm= ob->data;
311         bPose *pose= ob->pose;
312         bPoseChannel *pchan;
313         
314         /* don't do anything if no hiding any bones */
315         if ((arm->flag & ARM_GHOST_ONLYSEL)==0)
316                 return;
317                 
318         /* loop over all pchans, adding/removing tags as appropriate */
319         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
320                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
321                         if (unset) {
322                                 /* remove tags from all pchans if cleaning up */
323                                 pchan->bone->flag &= ~BONE_HIDDEN_PG;
324                         }
325                         else {
326                                 /* set tags on unselected pchans only */
327                                 if ((pchan->bone->flag & BONE_SELECTED)==0)
328                                         pchan->bone->flag |= BONE_HIDDEN_PG;
329                         }
330                 }
331         }
332 }
333 #endif // XXX only for bones
334
335 /* draw ghosts that occur within a frame range 
336  *      note: object should be in posemode 
337  */
338 static void draw_ghost_poses_range(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
339 {
340         Object *ob= base->object;
341         AnimData *adt= BKE_animdata_from_id(&ob->id);
342         bArmature *arm= ob->data;
343         bPose *posen, *poseo;
344         float start, end, stepsize, range, colfac;
345         int cfrao, flago, ipoflago;
346         
347         start = (float)arm->ghostsf;
348         end = (float)arm->ghostef;
349         if (end <= start)
350                 return;
351         
352         stepsize= (float)(arm->ghostsize);
353         range= (float)(end - start);
354         
355         /* store values */
356         ob->mode &= ~OB_MODE_POSE;
357         cfrao= CFRA;
358         flago= arm->flag;
359         arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
360         ipoflago= ob->ipoflag; 
361         ob->ipoflag |= OB_DISABLE_PATH;
362         
363         /* copy the pose */
364         poseo= ob->pose;
365         copy_pose(&posen, ob->pose, 1);
366         ob->pose= posen;
367         armature_rebuild_pose(ob, ob->data);    /* child pointers for IK */
368         ghost_poses_tag_unselected(ob, 0);              /* hide unselected bones if need be */
369         
370         glEnable(GL_BLEND);
371         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
372         
373         /* draw from first frame of range to last */
374         for (CFRA= (int)start; CFRA < end; CFRA += (int)stepsize) {
375                 colfac = (end - (float)CFRA) / range;
376                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
377                 
378                 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
379                 where_is_pose(scene, ob);
380                 draw_pose_bones(scene, v3d, ar, base, OB_WIRE);
381         }
382         glDisable(GL_BLEND);
383         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
384
385         ghost_poses_tag_unselected(ob, 1);              /* unhide unselected bones if need be */
386         free_pose(posen);
387         
388         /* restore */
389         CFRA= cfrao;
390         ob->pose= poseo;
391         arm->flag= flago;
392         armature_rebuild_pose(ob, ob->data);
393         ob->mode |= OB_MODE_POSE;
394         ob->ipoflag= ipoflago; 
395 }
396
397 /* draw ghosts on keyframes in action within range 
398  *      - object should be in posemode 
399  */
400 static void draw_ghost_poses_keys(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
401 {
402         Object *ob= base->object;
403         AnimData *adt= BKE_animdata_from_id(&ob->id);
404         bAction *act= (adt) ? adt->action : NULL;
405         bArmature *arm= ob->data;
406         bPose *posen, *poseo;
407         DLRBT_Tree keys;
408         ActKeyColumn *ak, *akn;
409         float start, end, range, colfac, i;
410         int cfrao, flago;
411         
412         start = (float)arm->ghostsf;
413         end = (float)arm->ghostef;
414         if (end <= start)
415                 return;
416         
417         /* get keyframes - then clip to only within range */
418         BLI_dlrbTree_init(&keys);
419         action_to_keylist(adt, act, &keys, NULL);
420         BLI_dlrbTree_linkedlist_sync(&keys);
421         
422         range= 0;
423         for (ak= keys.first; ak; ak= akn) {
424                 akn= ak->next;
425                 
426                 if ((ak->cfra < start) || (ak->cfra > end))
427                         BLI_freelinkN((ListBase *)&keys, ak);
428                 else
429                         range++;
430         }
431         if (range == 0) return;
432         
433         /* store values */
434         ob->mode &= ~OB_MODE_POSE;
435         cfrao= CFRA;
436         flago= arm->flag;
437         arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
438         ob->ipoflag |= OB_DISABLE_PATH;
439         
440         /* copy the pose */
441         poseo= ob->pose;
442         copy_pose(&posen, ob->pose, 1);
443         ob->pose= posen;
444         armature_rebuild_pose(ob, ob->data);    /* child pointers for IK */
445         ghost_poses_tag_unselected(ob, 0);              /* hide unselected bones if need be */
446         
447         glEnable(GL_BLEND);
448         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
449         
450         /* draw from first frame of range to last */
451         for (ak=keys.first, i=0; ak; ak=ak->next, i++) {
452                 colfac = i/range;
453                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
454                 
455                 CFRA= (int)ak->cfra;
456                 
457                 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
458                 where_is_pose(scene, ob);
459                 draw_pose_bones(scene, v3d, ar, base, OB_WIRE);
460         }
461         glDisable(GL_BLEND);
462         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
463
464         ghost_poses_tag_unselected(ob, 1);              /* unhide unselected bones if need be */
465         BLI_dlrbTree_free(&keys);
466         free_pose(posen);
467         
468         /* restore */
469         CFRA= cfrao;
470         ob->pose= poseo;
471         arm->flag= flago;
472         armature_rebuild_pose(ob, ob->data);
473         ob->mode |= OB_MODE_POSE;
474 }
475
476 /* draw ghosts around current frame
477  *      - object is supposed to be armature in posemode 
478  */
479 static void draw_ghost_poses(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
480 {
481         Object *ob= base->object;
482         AnimData *adt= BKE_animdata_from_id(&ob->id);
483         bArmature *arm= ob->data;
484         bPose *posen, *poseo;
485         float cur, start, end, stepsize, range, colfac, actframe, ctime;
486         int cfrao, flago;
487         
488         /* pre conditions, get an action with sufficient frames */
489         if ELEM(NULL, adt, adt->action)
490                 return;
491
492         calc_action_range(adt->action, &start, &end, 0);
493         if (start == end)
494                 return;
495
496         stepsize= (float)(arm->ghostsize);
497         range= (float)(arm->ghostep)*stepsize + 0.5f;   /* plus half to make the for loop end correct */
498         
499         /* store values */
500         ob->mode &= ~OB_MODE_POSE;
501         cfrao= CFRA;
502         actframe= BKE_nla_tweakedit_remap(adt, (float)CFRA, 0);
503         flago= arm->flag;
504         arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
505         
506         /* copy the pose */
507         poseo= ob->pose;
508         copy_pose(&posen, ob->pose, 1);
509         ob->pose= posen;
510         armature_rebuild_pose(ob, ob->data);    /* child pointers for IK */
511         ghost_poses_tag_unselected(ob, 0);              /* hide unselected bones if need be */
512         
513         glEnable(GL_BLEND);
514         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
515         
516         /* draw from darkest blend to lowest */
517         for(cur= stepsize; cur<range; cur+=stepsize) {
518                 ctime= cur - (float)fmod(cfrao, stepsize);      /* ensures consistant stepping */
519                 colfac= ctime/range;
520                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
521                 
522                 /* only within action range */
523                 if (actframe+ctime >= start && actframe+ctime <= end) {
524                         CFRA= (int)BKE_nla_tweakedit_remap(adt, actframe+ctime, NLATIME_CONVERT_MAP);
525                         
526                         if (CFRA != cfrao) {
527                                 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
528                                 where_is_pose(scene, ob);
529                                 draw_pose_bones(scene, v3d, ar, base, OB_WIRE);
530                         }
531                 }
532                 
533                 ctime= cur + (float)fmod((float)cfrao, stepsize) - stepsize+1.0f;       /* ensures consistant stepping */
534                 colfac= ctime/range;
535                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
536                 
537                 /* only within action range */
538                 if ((actframe-ctime >= start) && (actframe-ctime <= end)) {
539                         CFRA= (int)BKE_nla_tweakedit_remap(adt, actframe-ctime, NLATIME_CONVERT_MAP);
540                         
541                         if (CFRA != cfrao) {
542                                 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
543                                 where_is_pose(scene, ob);
544                                 draw_pose_bones(scene, v3d, ar, base, OB_WIRE);
545                         }
546                 }
547         }
548         glDisable(GL_BLEND);
549         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
550
551         ghost_poses_tag_unselected(ob, 1);              /* unhide unselected bones if need be */
552         free_pose(posen);
553         
554         /* restore */
555         CFRA= cfrao;
556         ob->pose= poseo;
557         arm->flag= flago;
558         armature_rebuild_pose(ob, ob->data);
559         ob->mode |= OB_MODE_POSE;
560 }
561
562
563
564 #endif // XXX temp file guards