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