Blender  V2.59
graph_draw.c
Go to the documentation of this file.
00001 /*
00002  * $Id: graph_draw.c 36786 2011-05-20 01:02:00Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) Blender Foundation
00021  *
00022  * Contributor(s): Joshua Leung (2009 Recode)
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00032 #include <stdio.h>
00033 #include <math.h>
00034 #include <string.h>
00035 #include <float.h>
00036 
00037 #ifndef _WIN32
00038 #include <unistd.h>
00039 #else
00040 #include <io.h>
00041 #endif
00042 
00043 
00044 #include "BLI_blenlib.h"
00045 #include "BLI_math.h"
00046 #include "BLI_utildefines.h"
00047 
00048 #include "DNA_anim_types.h"
00049 #include "DNA_object_types.h"
00050 #include "DNA_screen_types.h"
00051 #include "DNA_space_types.h"
00052 #include "DNA_windowmanager_types.h"
00053 
00054 #include "BKE_context.h"
00055 #include "BKE_curve.h"
00056 #include "BKE_fcurve.h"
00057 
00058 
00059 #include "BIF_gl.h"
00060 #include "BIF_glutil.h"
00061 
00062 #include "ED_anim_api.h"
00063 
00064 #include "graph_intern.h"
00065 
00066 #include "UI_interface.h"
00067 #include "UI_resources.h"
00068 #include "UI_view2d.h"
00069 
00070 /* *************************** */
00071 /* Utility Drawing Defines */
00072 
00073 /* determine the alpha value that should be used when 
00074  * drawing components for some F-Curve (fcu)
00075  *      - selected F-Curves should be more visible than partially visible ones
00076  */
00077 #define drawFCurveFade(fcu) ( ((fcu)->flag & FCURVE_SELECTED)? 1.0f : 0.25f )
00078 
00079 /* set the color for some point from some value given packed into an int 
00080  *      - intV: integer value containing color info packed into an int
00081  *      - alpha: float value describing the 
00082  */
00083 #define cpackA(intVC, alpha) \
00084         { \
00085                 float _cpackCol[3]; \
00086                 cpack_to_rgb(intVC, &_cpackCol[0], &_cpackCol[1], &_cpackCol[2]); \
00087                 glColor4f(_cpackCol[0], _cpackCol[1], _cpackCol[2], alpha); \
00088         }
00089 
00090 /* *************************** */
00091 /* F-Curve Modifier Drawing */
00092 
00093 /* Envelope -------------- */
00094 
00095 // TODO: draw a shaded poly showing the region of influence too!!!
00096 static void draw_fcurve_modifier_controls_envelope (FModifier *fcm, View2D *v2d)
00097 {
00098         FMod_Envelope *env= (FMod_Envelope *)fcm->data;
00099         FCM_EnvelopeData *fed;
00100         const float fac= 0.05f * (v2d->cur.xmax - v2d->cur.xmin);
00101         int i;
00102         
00103         /* draw two black lines showing the standard reference levels */
00104         glColor3f(0.0f, 0.0f, 0.0f);
00105         setlinestyle(5);
00106         
00107         glBegin(GL_LINES);
00108                 glVertex2f(v2d->cur.xmin, env->midval+env->min);
00109                 glVertex2f(v2d->cur.xmax, env->midval+env->min);
00110                 
00111                 glVertex2f(v2d->cur.xmin, env->midval+env->max);
00112                 glVertex2f(v2d->cur.xmax, env->midval+env->max);
00113         glEnd(); // GL_LINES
00114         setlinestyle(0);
00115         
00116         /* set size of vertices (non-adjustable for now) */
00117         glPointSize(2.0f);
00118         
00119         // for now, point color is fixed, and is white
00120         glColor3f(1.0f, 1.0f, 1.0f);
00121         
00122         /* we use bgl points not standard gl points, to workaround vertex 
00123          * drawing bugs that some drivers have (probably legacy ones only though)
00124          */
00125         bglBegin(GL_POINTS);
00126         for (i=0, fed=env->data; i < env->totvert; i++, fed++) {
00127                 /* only draw if visible
00128                  *      - min/max here are fixed, not relative
00129                  */
00130                 if IN_RANGE(fed->time, (v2d->cur.xmin - fac), (v2d->cur.xmax + fac)) {
00131                         glVertex2f(fed->time, fed->min);
00132                         glVertex2f(fed->time, fed->max);
00133                 }
00134         }
00135         bglEnd(); // GL_POINTS
00136         
00137         glPointSize(1.0f);
00138 }
00139 
00140 /* *************************** */
00141 /* F-Curve Drawing */
00142 
00143 /* Points ---------------- */
00144 
00145 /* helper func - draw keyframe vertices only for an F-Curve */
00146 static void draw_fcurve_vertices_keyframes (FCurve *fcu, SpaceIpo *UNUSED(sipo), View2D *v2d, short edit, short sel)
00147 {
00148         BezTriple *bezt= fcu->bezt;
00149         const float fac= 0.05f * (v2d->cur.xmax - v2d->cur.xmin);
00150         int i;
00151         
00152         /* we use bgl points not standard gl points, to workaround vertex 
00153          * drawing bugs that some drivers have (probably legacy ones only though)
00154          */
00155         bglBegin(GL_POINTS);
00156         
00157         for (i = 0; i < fcu->totvert; i++, bezt++) {
00158                 /* as an optimisation step, only draw those in view 
00159                  *      - we apply a correction factor to ensure that points don't pop in/out due to slight twitches of view size
00160                  */
00161                 if IN_RANGE(bezt->vec[1][0], (v2d->cur.xmin - fac), (v2d->cur.xmax + fac)) {
00162                         if (edit) {
00163                                 /* 'Keyframe' vertex only, as handle lines and handles have already been drawn
00164                                  *      - only draw those with correct selection state for the current drawing color
00165                                  *      - 
00166                                  */
00167                                 if ((bezt->f2 & SELECT) == sel)
00168                                         bglVertex3fv(bezt->vec[1]);
00169                         }
00170                         else {
00171                                 /* no check for selection here, as curve is not editable... */
00172                                 // XXX perhaps we don't want to even draw points?   maybe add an option for that later
00173                                 bglVertex3fv(bezt->vec[1]);
00174                         }
00175                 }
00176         }
00177         
00178         bglEnd(); // GL_POINTS
00179 }
00180 
00181 
00182 /* helper func - draw handle vertex for an F-Curve as a round unfilled circle 
00183  * NOTE: the caller MUST HAVE GL_LINE_SMOOTH & GL_BLEND ENABLED, otherwise, the controls don't 
00184  * have a consistent appearance (due to off-pixel alignments)...
00185  */
00186 static void draw_fcurve_handle_control (float x, float y, float xscale, float yscale, float hsize)
00187 {
00188         static GLuint displist=0;
00189         
00190         /* initialise round circle shape */
00191         if (displist == 0) {
00192                 GLUquadricObj *qobj;
00193                 
00194                 displist= glGenLists(1);
00195                 glNewList(displist, GL_COMPILE);
00196                 
00197                 qobj    = gluNewQuadric(); 
00198                 gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
00199                 gluDisk(qobj, 0,  0.7, 8, 1);
00200                 gluDeleteQuadric(qobj);  
00201                 
00202                 glEndList();
00203         }
00204         
00205         /* adjust view transform before starting */
00206         glTranslatef(x, y, 0.0f);
00207         glScalef(1.0f/xscale*hsize, 1.0f/yscale*hsize, 1.0f);
00208         
00209         /* draw! */
00210         glCallList(displist);
00211         
00212         /* restore view transform */
00213         glScalef(xscale/hsize, yscale/hsize, 1.0);
00214         glTranslatef(-x, -y, 0.0f);
00215 }
00216 
00217 /* helper func - draw handle vertices only for an F-Curve (if it is not protected) */
00218 static void draw_fcurve_vertices_handles (FCurve *fcu, SpaceIpo *sipo, View2D *v2d, short sel, short sel_handle_only)
00219 {
00220         BezTriple *bezt= fcu->bezt;
00221         BezTriple *prevbezt = NULL;
00222         float hsize, xscale, yscale;
00223         int i;
00224         
00225         /* get view settings */
00226         hsize= UI_GetThemeValuef(TH_HANDLE_VERTEX_SIZE);
00227         UI_view2d_getscale(v2d, &xscale, &yscale);
00228         
00229         /* set handle color */
00230         if (sel) UI_ThemeColor(TH_HANDLE_VERTEX_SELECT);
00231         else UI_ThemeColor(TH_HANDLE_VERTEX);
00232         
00233         /* anti-aliased lines for more consistent appearance */
00234         if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
00235         glEnable(GL_BLEND);
00236         
00237         for (i=0; i < fcu->totvert; i++, prevbezt=bezt, bezt++) {
00238                 /* Draw the editmode handles for a bezier curve (others don't have handles) 
00239                  * if their selection status matches the selection status we're drawing for
00240                  *      - first handle only if previous beztriple was bezier-mode
00241                  *      - second handle only if current beztriple is bezier-mode
00242                  *
00243                  * Also, need to take into account whether the keyframe was selected
00244                  * if a Graph Editor option to only show handles of selected keys is on.
00245                  */
00246                 if ( !sel_handle_only || BEZSELECTED(bezt) ) {
00247                         if ( (!prevbezt && (bezt->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) ) {
00248                                 if ((bezt->f1 & SELECT) == sel)/* && v2d->cur.xmin < bezt->vec[0][0] < v2d->cur.xmax)*/
00249                                         draw_fcurve_handle_control(bezt->vec[0][0], bezt->vec[0][1], xscale, yscale, hsize);
00250                         }
00251                         
00252                         if (bezt->ipo==BEZT_IPO_BEZ) {
00253                                 if ((bezt->f3 & SELECT) == sel)/* && v2d->cur.xmin < bezt->vec[2][0] < v2d->cur.xmax)*/
00254                                         draw_fcurve_handle_control(bezt->vec[2][0], bezt->vec[2][1], xscale, yscale, hsize);
00255                         }
00256                 }
00257         }
00258         
00259         if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
00260         glDisable(GL_BLEND);
00261 }
00262 
00263 /* helper func - set color to draw F-Curve data with */
00264 static void set_fcurve_vertex_color (FCurve *fcu, short sel)
00265 {
00266         /* Fade the 'intensity' of the vertices based on the selection of the curves too */
00267         int alphaOffset= (int)((drawFCurveFade(fcu) - 1.0f) * 255);
00268         
00269         /* Set color of curve vertex based on state of curve (i.e. 'Edit' Mode) */
00270         if ((fcu->flag & FCURVE_PROTECTED)==0) {
00271                 /* Curve's points ARE BEING edited */
00272                 if (sel) UI_ThemeColorShadeAlpha(TH_VERTEX_SELECT, 0, alphaOffset); 
00273                 else UI_ThemeColorShadeAlpha(TH_VERTEX, 0, alphaOffset);
00274         } 
00275         else {
00276                 /* Curve's points CANNOT BE edited */
00277                 if (sel) UI_ThemeColorShadeAlpha(TH_TEXT_HI, 0, alphaOffset);
00278                 else UI_ThemeColorShadeAlpha(TH_TEXT, 0, alphaOffset);
00279         }
00280 }
00281 
00282 
00283 static void draw_fcurve_vertices (SpaceIpo *sipo, ARegion *ar, FCurve *fcu, short do_handles, short sel_handle_only)
00284 {
00285         View2D *v2d= &ar->v2d;
00286         
00287         /* only draw points if curve is visible 
00288          *      - draw unselected points before selected points as separate passes to minimise color-changing overhead
00289          *         (XXX dunno if this is faster than drawing all in one pass though) 
00290          *         and also to make sure in the case of overlapping points that the selected is always visible
00291          *      - draw handles before keyframes, so that keyframes will overlap handles (keyframes are more important for users)
00292          */
00293         
00294         glPointSize(UI_GetThemeValuef(TH_VERTEX_SIZE));
00295         
00296         /* draw the two handles first (if they're shown, the curve doesn't have just a single keyframe, and the curve is being edited) */
00297         if (do_handles)
00298         {
00299                 set_fcurve_vertex_color(fcu, 0);
00300                 draw_fcurve_vertices_handles(fcu, sipo, v2d, 0, sel_handle_only);
00301                 
00302                 set_fcurve_vertex_color(fcu, 1);
00303                 draw_fcurve_vertices_handles(fcu, sipo, v2d, 1, sel_handle_only);
00304         }
00305                 
00306         /* draw keyframes over the handles */
00307         set_fcurve_vertex_color(fcu, 0);
00308         draw_fcurve_vertices_keyframes(fcu, sipo, v2d, !(fcu->flag & FCURVE_PROTECTED), 0);
00309         
00310         set_fcurve_vertex_color(fcu, 1);
00311         draw_fcurve_vertices_keyframes(fcu, sipo, v2d, !(fcu->flag & FCURVE_PROTECTED), 1);
00312         
00313         glPointSize(1.0f);
00314 }
00315 
00316 /* Handles ---------------- */
00317 
00318 static int draw_fcurve_handles_check(SpaceIpo *sipo, FCurve *fcu)
00319 {
00320         /* don't draw handle lines if handles are not to be shown */
00321         if (    (sipo->flag & SIPO_NOHANDLES) || /* handles shouldn't be shown anywhere */
00322                         (fcu->flag & FCURVE_PROTECTED) || /* keyframes aren't editable */
00323 #if 0           /* handles can still be selected and handle types set, better draw - campbell */
00324                         (fcu->flag & FCURVE_INT_VALUES) || /* editing the handles here will cause weird/incorrect interpolation issues */
00325 #endif
00326                         ((fcu->grp) && (fcu->grp->flag & AGRP_PROTECTED)) || /* group that curve belongs to is not editable */
00327                         (fcu->totvert <= 1) /* do not show handles if there is only 1 keyframe, otherwise they all clump together in an ugly ball */
00328                 ) 
00329         {
00330                 return 0;
00331         } 
00332         else 
00333         {
00334                 return 1;
00335         }
00336 }
00337 
00338 /* draw lines for F-Curve handles only (this is only done in EditMode)
00339  * note: draw_fcurve_handles_check must be checked before running this. */
00340 static void draw_fcurve_handles (SpaceIpo *sipo, FCurve *fcu)
00341 {
00342         int sel, b;
00343         
00344         /* a single call to GL_LINES here around these calls should be sufficient to still
00345          * get separate line segments, but which aren't wrapped with GL_LINE_STRIP everytime we
00346          * want a single line
00347          */
00348         glBegin(GL_LINES);
00349         
00350         /* slightly hacky, but we want to draw unselected points before selected ones 
00351          * so that selected points are clearly visible
00352          */
00353         for (sel= 0; sel < 2; sel++) {
00354                 BezTriple *bezt=fcu->bezt, *prevbezt=NULL;
00355                 int basecol= (sel)? TH_HANDLE_SEL_FREE : TH_HANDLE_FREE;
00356                 float *fp;
00357                 unsigned char col[4];
00358                 
00359                 /* if only selected keyframes have handles shown, skip the first round */
00360                 if ((sel == 0) && (sipo->flag & SIPO_SELVHANDLESONLY))
00361                         continue;
00362                 
00363                 for (b= 0; b < fcu->totvert; b++, prevbezt=bezt, bezt++) {
00364                         /* if only selected keyframes can get their handles shown, 
00365                          * check that keyframe is selected
00366                          */
00367                         if (sipo->flag & SIPO_SELVHANDLESONLY) {
00368                                 if (BEZSELECTED(bezt) == 0)
00369                                         continue;
00370                         }
00371                         
00372                         /* draw handle with appropriate set of colors if selection is ok */
00373                         if ((bezt->f2 & SELECT)==sel) {
00374                                 fp= bezt->vec[0];
00375 
00376                                 /* only draw first handle if previous segment had handles */
00377                                 if ( (!prevbezt && (bezt->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) ) 
00378                                 {
00379                                         UI_GetThemeColor3ubv(basecol + bezt->h1, col);
00380                                         col[3]= drawFCurveFade(fcu) * 255;
00381                                         glColor4ubv((GLubyte *)col);
00382                                         
00383                                         glVertex2fv(fp); glVertex2fv(fp+3); 
00384                                 }
00385 
00386                                 /* only draw second handle if this segment is bezier */
00387                                 if (bezt->ipo == BEZT_IPO_BEZ) 
00388                                 {
00389                                         UI_GetThemeColor3ubv(basecol + bezt->h2, col);
00390                                         col[3]= drawFCurveFade(fcu) * 255;
00391                                         glColor4ubv((GLubyte *)col);
00392 
00393                                         glVertex2fv(fp+3); glVertex2fv(fp+6); 
00394                                 }
00395                         }
00396                         else {
00397                                 /* only draw first handle if previous segment was had handles, and selection is ok */
00398                                 if ( ((bezt->f1 & SELECT)==sel) && 
00399                                          ( (!prevbezt && (bezt->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) ) ) 
00400                                 {
00401                                         fp= bezt->vec[0];
00402                                         UI_GetThemeColor3ubv(basecol + bezt->h1, col);
00403                                         col[3]= drawFCurveFade(fcu) * 255;
00404                                         glColor4ubv((GLubyte *)col);
00405 
00406                                         glVertex2fv(fp); glVertex2fv(fp+3); 
00407                                 }
00408                                 
00409                                 /* only draw second handle if this segment is bezier, and selection is ok */
00410                                 if ( ((bezt->f3 & SELECT)==sel) &&
00411                                          (bezt->ipo == BEZT_IPO_BEZ) )
00412                                 {
00413                                         fp= bezt->vec[1];
00414                                         UI_GetThemeColor3ubv(basecol + bezt->h2, col);
00415                                         col[3]= drawFCurveFade(fcu) * 255;
00416                                         glColor4ubv((GLubyte *)col);
00417                                         
00418                                         glVertex2fv(fp); glVertex2fv(fp+3); 
00419                                 }
00420                         }
00421                 }
00422         }
00423         
00424         glEnd(); // GL_LINES 
00425 }
00426 
00427 /* Samples ---------------- */
00428 
00429 /* helper func - draw sample-range marker for an F-Curve as a cross 
00430  * NOTE: the caller MUST HAVE GL_LINE_SMOOTH & GL_BLEND ENABLED, otherwise, the controls don't 
00431  * have a consistent appearance (due to off-pixel alignments)...
00432  */
00433 static void draw_fcurve_sample_control (float x, float y, float xscale, float yscale, float hsize)
00434 {
00435         static GLuint displist=0;
00436         
00437         /* initialise X shape */
00438         if (displist == 0) {
00439                 displist= glGenLists(1);
00440                 glNewList(displist, GL_COMPILE);
00441                 
00442                 glBegin(GL_LINES);
00443                         glVertex2f(-0.7f, -0.7f);
00444                         glVertex2f(+0.7f, +0.7f);
00445                         
00446                         glVertex2f(-0.7f, +0.7f);
00447                         glVertex2f(+0.7f, -0.7f);
00448                 glEnd(); // GL_LINES
00449                 
00450                 glEndList();
00451         }
00452         
00453         /* adjust view transform before starting */
00454         glTranslatef(x, y, 0.0f);
00455         glScalef(1.0f/xscale*hsize, 1.0f/yscale*hsize, 1.0f);
00456         
00457         /* draw! */
00458         glCallList(displist);
00459         
00460         /* restore view transform */
00461         glScalef(xscale/hsize, yscale/hsize, 1.0);
00462         glTranslatef(-x, -y, 0.0f);
00463 }
00464 
00465 /* helper func - draw keyframe vertices only for an F-Curve */
00466 static void draw_fcurve_samples (SpaceIpo *sipo, ARegion *ar, FCurve *fcu)
00467 {
00468         FPoint *first, *last;
00469         float hsize, xscale, yscale;
00470         
00471         /* get view settings */
00472         hsize= UI_GetThemeValuef(TH_VERTEX_SIZE);
00473         UI_view2d_getscale(&ar->v2d, &xscale, &yscale);
00474         
00475         /* set vertex color */
00476         if (fcu->flag & (FCURVE_ACTIVE|FCURVE_SELECTED)) UI_ThemeColor(TH_TEXT_HI);
00477         else UI_ThemeColor(TH_TEXT);
00478         
00479         /* get verts */
00480         first= fcu->fpt;
00481         last= (first) ? (first + (fcu->totvert-1)) : (NULL);
00482         
00483         /* draw */
00484         if (first && last) {
00485                 /* anti-aliased lines for more consistent appearance */
00486                 if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
00487                 glEnable(GL_BLEND);
00488                 
00489                 draw_fcurve_sample_control(first->vec[0], first->vec[1], xscale, yscale, hsize);
00490                 draw_fcurve_sample_control(last->vec[0], last->vec[1], xscale, yscale, hsize);
00491                 
00492                 glDisable(GL_BLEND);
00493                 if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
00494         }
00495 }
00496 
00497 /* Curve ---------------- */
00498 
00499 /* helper func - just draw the F-Curve by sampling the visible region (for drawing curves with modifiers) */
00500 static void draw_fcurve_curve (bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d, View2DGrid *grid)
00501 {
00502         ChannelDriver *driver;
00503         float samplefreq, ctime;
00504         float stime, etime;
00505         float unitFac;
00506         float dx, dy;
00507 
00508         /* when opening a blend file on a different sized screen or while dragging the toolbar this can happen
00509          * best just bail out in this case */
00510         UI_view2d_grid_size(grid, &dx, &dy);
00511         if(dx <= 0.0f)
00512                 return;
00513 
00514 
00515         /* disable any drivers temporarily */
00516         driver= fcu->driver;
00517         fcu->driver= NULL;
00518         
00519         /* compute unit correction factor */
00520         unitFac= ANIM_unit_mapping_get_factor(ac->scene, id, fcu, 0);
00521         
00522         /* Note about sampling frequency:
00523          *      Ideally, this is chosen such that we have 1-2 pixels = 1 segment
00524          *      which means that our curves can be as smooth as possible. However,
00525          *      this does mean that curves may not be fully accurate (i.e. if they have
00526          *      sudden spikes which happen at the sampling point, we may have problems).
00527          *      Also, this may introduce lower performance on less densely detailed curves,'
00528          *      though it is impossible to predict this from the modifiers!
00529          *
00530          *      If the automatically determined sampling frequency is likely to cause an infinite
00531          *      loop (i.e. too close to 0), then clamp it to a determined "safe" value. The value
00532          *      chosen here is just the coarsest value which still looks reasonable...
00533          */
00534                 /* grid->dx represents the number of 'frames' between gridlines, but we divide by U.v2d_min_gridsize to get pixels-steps */
00535                 // TODO: perhaps we should have 1.0 frames as upper limit so that curves don't get too distorted?
00536         samplefreq= dx / U.v2d_min_gridsize;
00537         if (samplefreq < 0.00001f) samplefreq= 0.00001f;
00538         
00539         
00540         /* the start/end times are simply the horizontal extents of the 'cur' rect */
00541         stime= v2d->cur.xmin;
00542         etime= v2d->cur.xmax + samplefreq; /* + samplefreq here so that last item gets included... */
00543         
00544         
00545         /* at each sampling interval, add a new vertex 
00546          *      - apply the unit correction factor to the calculated values so that 
00547          *        the displayed values appear correctly in the viewport
00548          */
00549         glBegin(GL_LINE_STRIP);
00550         
00551         for (ctime= stime; ctime <= etime; ctime += samplefreq)
00552                 glVertex2f( ctime, evaluate_fcurve(fcu, ctime)*unitFac );
00553         
00554         glEnd();
00555         
00556         /* restore driver */
00557         fcu->driver= driver;
00558 }
00559 
00560 /* helper func - draw a samples-based F-Curve */
00561 static void draw_fcurve_curve_samples (bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d)
00562 {
00563         FPoint *prevfpt= fcu->fpt;
00564         FPoint *fpt= prevfpt + 1;
00565         float fac, v[2];
00566         int b= fcu->totvert-1;
00567         
00568         glBegin(GL_LINE_STRIP);
00569         
00570         /* apply unit mapping */
00571         ANIM_unit_mapping_apply_fcurve(ac->scene, id, fcu, 0);
00572         
00573         /* extrapolate to left? - left-side of view comes before first keyframe? */
00574         if (prevfpt->vec[0] > v2d->cur.xmin) {
00575                 v[0]= v2d->cur.xmin;
00576                 
00577                 /* y-value depends on the interpolation */
00578                 if ((fcu->extend==FCURVE_EXTRAPOLATE_CONSTANT) || (fcu->flag & FCURVE_INT_VALUES) || (fcu->totvert==1)) {
00579                         /* just extend across the first keyframe's value */
00580                         v[1]= prevfpt->vec[1];
00581                 } 
00582                 else {
00583                         /* extrapolate linear dosnt use the handle, use the next points center instead */
00584                         fac= (prevfpt->vec[0]-fpt->vec[0])/(prevfpt->vec[0]-v[0]);
00585                         if (fac) fac= 1.0f/fac;
00586                         v[1]= prevfpt->vec[1]-fac*(prevfpt->vec[1]-fpt->vec[1]);
00587                 }
00588                 
00589                 glVertex2fv(v);
00590         }
00591         
00592         /* if only one sample, add it now */
00593         if (fcu->totvert == 1)
00594                 glVertex2fv(prevfpt->vec);
00595         
00596         /* loop over samples, drawing segments */
00597         /* draw curve between first and last keyframe (if there are enough to do so) */
00598         while (b--) {
00599                 /* Linear interpolation: just add one point (which should add a new line segment) */
00600                 glVertex2fv(prevfpt->vec);
00601                 
00602                 /* get next pointers */
00603                 prevfpt= fpt; 
00604                 fpt++;
00605                 
00606                 /* last point? */
00607                 if (b == 0)
00608                         glVertex2fv(prevfpt->vec);
00609         }
00610         
00611         /* extrapolate to right? (see code for left-extrapolation above too) */
00612         if (prevfpt->vec[0] < v2d->cur.xmax) {
00613                 v[0]= v2d->cur.xmax;
00614                 
00615                 /* y-value depends on the interpolation */
00616                 if ((fcu->extend==FCURVE_EXTRAPOLATE_CONSTANT) || (fcu->flag & FCURVE_INT_VALUES) || (fcu->totvert==1)) {
00617                         /* based on last keyframe's value */
00618                         v[1]= prevfpt->vec[1];
00619                 } 
00620                 else {
00621                         /* extrapolate linear dosnt use the handle, use the previous points center instead */
00622                         fpt = prevfpt-1;
00623                         fac= (prevfpt->vec[0]-fpt->vec[0])/(prevfpt->vec[0]-v[0]);
00624                         if (fac) fac= 1.0f/fac;
00625                         v[1]= prevfpt->vec[1]-fac*(prevfpt->vec[1]-fpt->vec[1]);
00626                 }
00627                 
00628                 glVertex2fv(v);
00629         }
00630         
00631         /* unapply unit mapping */
00632         ANIM_unit_mapping_apply_fcurve(ac->scene, id, fcu, ANIM_UNITCONV_RESTORE);
00633         
00634         glEnd();
00635 }
00636 
00637 /* helper func - draw one repeat of an F-Curve */
00638 static void draw_fcurve_curve_bezts (bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d)
00639 {
00640         BezTriple *prevbezt= fcu->bezt;
00641         BezTriple *bezt= prevbezt+1;
00642         float v1[2], v2[2], v3[2], v4[2];
00643         float *fp, data[120];
00644         float fac= 0.0f;
00645         int b= fcu->totvert-1;
00646         int resol;
00647         
00648         glBegin(GL_LINE_STRIP);
00649         
00650         /* apply unit mapping */
00651         ANIM_unit_mapping_apply_fcurve(ac->scene, id, fcu, 0);
00652         
00653         /* extrapolate to left? */
00654         if (prevbezt->vec[1][0] > v2d->cur.xmin) {
00655                 /* left-side of view comes before first keyframe, so need to extend as not cyclic */
00656                 v1[0]= v2d->cur.xmin;
00657                 
00658                 /* y-value depends on the interpolation */
00659                 if ((fcu->extend==FCURVE_EXTRAPOLATE_CONSTANT) || (prevbezt->ipo==BEZT_IPO_CONST) || (fcu->totvert==1)) {
00660                         /* just extend across the first keyframe's value */
00661                         v1[1]= prevbezt->vec[1][1];
00662                 } 
00663                 else if (prevbezt->ipo==BEZT_IPO_LIN) {
00664                         /* extrapolate linear dosnt use the handle, use the next points center instead */
00665                         fac= (prevbezt->vec[1][0]-bezt->vec[1][0])/(prevbezt->vec[1][0]-v1[0]);
00666                         if (fac) fac= 1.0f/fac;
00667                         v1[1]= prevbezt->vec[1][1]-fac*(prevbezt->vec[1][1]-bezt->vec[1][1]);
00668                 } 
00669                 else {
00670                         /* based on angle of handle 1 (relative to keyframe) */
00671                         fac= (prevbezt->vec[0][0]-prevbezt->vec[1][0])/(prevbezt->vec[1][0]-v1[0]);
00672                         if (fac) fac= 1.0f/fac;
00673                         v1[1]= prevbezt->vec[1][1]-fac*(prevbezt->vec[0][1]-prevbezt->vec[1][1]);
00674                 }
00675                 
00676                 glVertex2fv(v1);
00677         }
00678         
00679         /* if only one keyframe, add it now */
00680         if (fcu->totvert == 1) {
00681                 v1[0]= prevbezt->vec[1][0];
00682                 v1[1]= prevbezt->vec[1][1];
00683                 glVertex2fv(v1);
00684         }
00685         
00686         /* draw curve between first and last keyframe (if there are enough to do so) */
00687         while (b--) {
00688                 if (prevbezt->ipo==BEZT_IPO_CONST) {
00689                         /* Constant-Interpolation: draw segment between previous keyframe and next, but holding same value */
00690                         v1[0]= prevbezt->vec[1][0];
00691                         v1[1]= prevbezt->vec[1][1];
00692                         glVertex2fv(v1);
00693                         
00694                         v1[0]= bezt->vec[1][0];
00695                         v1[1]= prevbezt->vec[1][1];
00696                         glVertex2fv(v1);
00697                 }
00698                 else if (prevbezt->ipo==BEZT_IPO_LIN) {
00699                         /* Linear interpolation: just add one point (which should add a new line segment) */
00700                         v1[0]= prevbezt->vec[1][0];
00701                         v1[1]= prevbezt->vec[1][1];
00702                         glVertex2fv(v1);
00703                 }
00704                 else {
00705                         /* Bezier-Interpolation: draw curve as series of segments between keyframes 
00706                          *      - resol determines number of points to sample in between keyframes
00707                          */
00708                         
00709                         /* resol not depending on horizontal resolution anymore, drivers for example... */
00710                         // TODO: would be nice to make this depend on the scale of the graph too...
00711                         if (fcu->driver) 
00712                                 resol= 32;
00713                         else 
00714                                 resol= (int)(3.0*sqrt(bezt->vec[1][0] - prevbezt->vec[1][0]));
00715                         
00716                         if (resol < 2) {
00717                                 /* only draw one */
00718                                 v1[0]= prevbezt->vec[1][0];
00719                                 v1[1]= prevbezt->vec[1][1];
00720                                 glVertex2fv(v1);
00721                         }
00722                         else {
00723                                 /* clamp resolution to max of 32 */
00724                                 if (resol > 32) resol= 32;
00725                                 
00726                                 v1[0]= prevbezt->vec[1][0];
00727                                 v1[1]= prevbezt->vec[1][1];
00728                                 v2[0]= prevbezt->vec[2][0];
00729                                 v2[1]= prevbezt->vec[2][1];
00730                                 
00731                                 v3[0]= bezt->vec[0][0];
00732                                 v3[1]= bezt->vec[0][1];
00733                                 v4[0]= bezt->vec[1][0];
00734                                 v4[1]= bezt->vec[1][1];
00735                                 
00736                                 correct_bezpart(v1, v2, v3, v4);
00737                                 
00738                                 forward_diff_bezier(v1[0], v2[0], v3[0], v4[0], data, resol, sizeof(float)*3);
00739                                 forward_diff_bezier(v1[1], v2[1], v3[1], v4[1], data+1, resol, sizeof(float)*3);
00740                                 
00741                                 for (fp= data; resol; resol--, fp+= 3)
00742                                         glVertex2fv(fp);
00743                         }
00744                 }
00745                 
00746                 /* get next pointers */
00747                 prevbezt= bezt; 
00748                 bezt++;
00749                 
00750                 /* last point? */
00751                 if (b == 0) {
00752                         v1[0]= prevbezt->vec[1][0];
00753                         v1[1]= prevbezt->vec[1][1];
00754                         glVertex2fv(v1);
00755                 }
00756         }
00757         
00758         /* extrapolate to right? (see code for left-extrapolation above too) */
00759         if (prevbezt->vec[1][0] < v2d->cur.xmax) {
00760                 v1[0]= v2d->cur.xmax;
00761                 
00762                 /* y-value depends on the interpolation */
00763                 if ((fcu->extend==FCURVE_EXTRAPOLATE_CONSTANT) || (fcu->flag & FCURVE_INT_VALUES) || (prevbezt->ipo==BEZT_IPO_CONST) || (fcu->totvert==1)) {
00764                         /* based on last keyframe's value */
00765                         v1[1]= prevbezt->vec[1][1];
00766                 } 
00767                 else if (prevbezt->ipo==BEZT_IPO_LIN) {
00768                         /* extrapolate linear dosnt use the handle, use the previous points center instead */
00769                         bezt = prevbezt-1;
00770                         fac= (prevbezt->vec[1][0]-bezt->vec[1][0])/(prevbezt->vec[1][0]-v1[0]);
00771                         if (fac) fac= 1.0f/fac;
00772                         v1[1]= prevbezt->vec[1][1]-fac*(prevbezt->vec[1][1]-bezt->vec[1][1]);
00773                 } 
00774                 else {
00775                         /* based on angle of handle 1 (relative to keyframe) */
00776                         fac= (prevbezt->vec[2][0]-prevbezt->vec[1][0])/(prevbezt->vec[1][0]-v1[0]);
00777                         if (fac) fac= 1.0f/fac;
00778                         v1[1]= prevbezt->vec[1][1]-fac*(prevbezt->vec[2][1]-prevbezt->vec[1][1]);
00779                 }
00780                 
00781                 glVertex2fv(v1);
00782         }
00783         
00784         /* unapply unit mapping */
00785         ANIM_unit_mapping_apply_fcurve(ac->scene, id, fcu, ANIM_UNITCONV_RESTORE);
00786         
00787         glEnd();
00788 } 
00789 
00790 /* Public Curve-Drawing API  ---------------- */
00791 
00792 /* Draw the 'ghost' F-Curves (i.e. snapshots of the curve) 
00793  * NOTE: unit mapping has already been applied to the values, so do not try and apply again
00794  */
00795 void graph_draw_ghost_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar)
00796 {
00797         FCurve *fcu;
00798         
00799         /* draw with thick dotted lines */
00800         setlinestyle(10);
00801         glLineWidth(3.0f);
00802         
00803         /* anti-aliased lines for less jagged appearance */
00804         if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
00805         glEnable(GL_BLEND);
00806         
00807         /* the ghost curves are simply sampled F-Curves stored in sipo->ghostCurves */
00808         for (fcu= sipo->ghostCurves.first; fcu; fcu= fcu->next) {
00809                 /* set whatever color the curve has set 
00810                  *      - this is set by the function which creates these
00811                  *      - draw with a fixed opacity of 2
00812                  */
00813                 glColor4f(fcu->color[0], fcu->color[1], fcu->color[2], 0.5f);
00814                 
00815                 /* simply draw the stored samples */
00816                 draw_fcurve_curve_samples(ac, NULL, fcu, &ar->v2d);
00817         }
00818         
00819         /* restore settings */
00820         setlinestyle(0);
00821         glLineWidth(1.0f);
00822         
00823         if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
00824         glDisable(GL_BLEND);
00825 }
00826 
00827 /* This is called twice from space_graph.c -> graph_main_area_draw()
00828  * Unselected then selected F-Curves are drawn so that they do not occlude each other.
00829  */
00830 void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGrid *grid, short sel)
00831 {
00832         ListBase anim_data = {NULL, NULL};
00833         bAnimListElem *ale;
00834         int filter;
00835         
00836         /* build list of curves to draw */
00837         filter= (ANIMFILTER_VISIBLE|ANIMFILTER_CURVESONLY|ANIMFILTER_CURVEVISIBLE);
00838         filter |= ((sel) ? (ANIMFILTER_SEL) : (ANIMFILTER_UNSEL));
00839         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
00840                 
00841         /* for each curve:
00842          *      draw curve, then handle-lines, and finally vertices in this order so that 
00843          *      the data will be layered correctly
00844          */
00845         for (ale=anim_data.first; ale; ale=ale->next) {
00846                 FCurve *fcu= (FCurve *)ale->key_data;
00847                 FModifier *fcm= find_active_fmodifier(&fcu->modifiers);
00848                 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
00849                 
00850                 /* map keyframes for drawing if scaled F-Curve */
00851                 if (adt)
00852                         ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 0); 
00853                 
00854                 /* draw curve:
00855                  *      - curve line may be result of one or more destructive modifiers or just the raw data,
00856                  *        so we need to check which method should be used
00857                  *      - controls from active modifier take precidence over keyframes
00858                  *        (XXX! editing tools need to take this into account!)
00859                  */
00860                  
00861                 /* 1) draw curve line */
00862                 {
00863                         /* set color/drawing style for curve itself */
00864                         if ( ((fcu->grp) && (fcu->grp->flag & AGRP_PROTECTED)) || (fcu->flag & FCURVE_PROTECTED) ) {
00865                                 /* protected curves (non editable) are drawn with dotted lines */
00866                                 setlinestyle(2);
00867                         }
00868                         if ( ((fcu->grp) && (fcu->grp->flag & AGRP_MUTED)) || (fcu->flag & FCURVE_MUTED) ) {
00869                                 /* muted curves are drawn in a greyish hue */
00870                                 // XXX should we have some variations?
00871                                 UI_ThemeColorShade(TH_HEADER, 50);
00872                         }
00873                         else {
00874                                 /* set whatever color the curve has set 
00875                                  *      - unselected curves draw less opaque to help distinguish the selected ones
00876                                  */
00877                                 glColor4f(fcu->color[0], fcu->color[1], fcu->color[2], drawFCurveFade(fcu));
00878                         }
00879                         
00880                         /* anti-aliased lines for less jagged appearance */
00881                         if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
00882                         glEnable(GL_BLEND);
00883                         
00884                         /* draw F-Curve */
00885                         if ((fcu->modifiers.first) || (fcu->flag & FCURVE_INT_VALUES)) {
00886                                 /* draw a curve affected by modifiers or only allowed to have integer values 
00887                                  * by sampling it at various small-intervals over the visible region 
00888                                  */
00889                                 draw_fcurve_curve(ac, ale->id, fcu, &ar->v2d, grid);
00890                         }
00891                         else if ( ((fcu->bezt) || (fcu->fpt)) && (fcu->totvert) ) { 
00892                                 /* just draw curve based on defined data (i.e. no modifiers) */
00893                                 if (fcu->bezt)
00894                                         draw_fcurve_curve_bezts(ac, ale->id, fcu, &ar->v2d);
00895                                 else if (fcu->fpt)
00896                                         draw_fcurve_curve_samples(ac, ale->id, fcu, &ar->v2d);
00897                         }
00898                         
00899                         /* restore settings */
00900                         setlinestyle(0);
00901                         
00902                         if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
00903                         glDisable(GL_BLEND);
00904                 }
00905                 
00906                 /* 2) draw handles and vertices as appropriate based on active 
00907                  *      - if the option to only show controls if the F-Curve is selected is enabled, we must obey this
00908                  */
00909                 if (!(sipo->flag & SIPO_SELCUVERTSONLY) || (fcu->flag & FCURVE_SELECTED)) {
00910                         if (fcurve_are_keyframes_usable(fcu) == 0) {
00911                                 /* only draw controls if this is the active modifier */
00912                                 if ((fcu->flag & FCURVE_ACTIVE) && (fcm)) {
00913                                         switch (fcm->type) {
00914                                                 case FMODIFIER_TYPE_ENVELOPE: /* envelope */
00915                                                         draw_fcurve_modifier_controls_envelope(fcm, &ar->v2d);
00916                                                         break;
00917                                         }
00918                                 }
00919                         }
00920                         else if ( ((fcu->bezt) || (fcu->fpt)) && (fcu->totvert) ) { 
00921                                 /* apply unit mapping */
00922                                 ANIM_unit_mapping_apply_fcurve(ac->scene, ale->id, fcu, 0);
00923                                 
00924                                 if (fcu->bezt) {
00925                                         int do_handles = draw_fcurve_handles_check(sipo, fcu);
00926                                         
00927                                         if (do_handles) {
00928                                                 /* only draw handles/vertices on keyframes */
00929                                                 glEnable(GL_BLEND);
00930                                                 draw_fcurve_handles(sipo, fcu);
00931                                                 glDisable(GL_BLEND);
00932                                         }
00933                                         
00934                                         draw_fcurve_vertices(sipo, ar, fcu, do_handles, (sipo->flag & SIPO_SELVHANDLESONLY));
00935                                 }
00936                                 else {
00937                                         /* samples: only draw two indicators at either end as indicators */
00938                                         draw_fcurve_samples(sipo, ar, fcu);
00939                                 }
00940                                 
00941                                 /* unapply unit mapping */
00942                                 ANIM_unit_mapping_apply_fcurve(ac->scene, ale->id, fcu, ANIM_UNITCONV_RESTORE);
00943                         }
00944                 }
00945                 
00946                 /* undo mapping of keyframes for drawing if scaled F-Curve */
00947                 if (adt)
00948                         ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 0); 
00949         }
00950         
00951         /* free list of curves */
00952         BLI_freelistN(&anim_data);
00953 }
00954 
00955 /* ************************************************************************* */
00956 /* Channel List */
00957 
00958 /* left hand part */
00959 void graph_draw_channel_names(bContext *C, bAnimContext *ac, ARegion *ar) 
00960 {
00961         ListBase anim_data = {NULL, NULL};
00962         bAnimListElem *ale;
00963         int filter;
00964         
00965         View2D *v2d= &ar->v2d;
00966         float y= 0.0f, height;
00967         int items, i=0;
00968         
00969         /* build list of channels to draw */
00970         filter= (ANIMFILTER_VISIBLE|ANIMFILTER_CHANNELS);
00971         items= ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
00972         
00973         /* Update max-extent of channels here (taking into account scrollers):
00974          *      - this is done to allow the channel list to be scrollable, but must be done here
00975          *        to avoid regenerating the list again and/or also because channels list is drawn first
00976          *      - offset of ACHANNEL_HEIGHT*2 is added to the height of the channels, as first is for 
00977          *        start of list offset, and the second is as a correction for the scrollers.
00978          */
00979         height= (float)((items*ACHANNEL_STEP) + (ACHANNEL_HEIGHT*2));
00980         UI_view2d_totRect_set(v2d, ar->winx, height);
00981         
00982         /* loop through channels, and set up drawing depending on their type  */        
00983         {       /* first pass: just the standard GL-drawing for backdrop + text */
00984                 y= (float)ACHANNEL_FIRST;
00985                 
00986                 for (ale= anim_data.first, i=0; ale; ale= ale->next, i++) {
00987                         const float yminc= (float)(y - ACHANNEL_HEIGHT_HALF);
00988                         const float ymaxc= (float)(y + ACHANNEL_HEIGHT_HALF);
00989                         
00990                         /* check if visible */
00991                         if ( IN_RANGE(yminc, v2d->cur.ymin, v2d->cur.ymax) ||
00992                                  IN_RANGE(ymaxc, v2d->cur.ymin, v2d->cur.ymax) ) 
00993                         {
00994                                 /* draw all channels using standard channel-drawing API */
00995                                 ANIM_channel_draw(ac, ale, yminc, ymaxc);
00996                         }
00997                         
00998                         /* adjust y-position for next one */
00999                         y -= ACHANNEL_STEP;
01000                 }
01001         }
01002         {       /* second pass: widgets */
01003                 uiBlock *block= uiBeginBlock(C, ar, "graph channel buttons", UI_EMBOSS);
01004                 
01005                 y= (float)ACHANNEL_FIRST;
01006                 
01007                 /* set blending again, as may not be set in previous step */
01008                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
01009                 glEnable(GL_BLEND);
01010                 
01011                 for (ale= anim_data.first, i=0; ale; ale= ale->next, i++) {
01012                         const float yminc= (float)(y - ACHANNEL_HEIGHT_HALF);
01013                         const float ymaxc= (float)(y + ACHANNEL_HEIGHT_HALF);
01014                         
01015                         /* check if visible */
01016                         if ( IN_RANGE(yminc, v2d->cur.ymin, v2d->cur.ymax) ||
01017                                  IN_RANGE(ymaxc, v2d->cur.ymin, v2d->cur.ymax) ) 
01018                         {
01019                                 /* draw all channels using standard channel-drawing API */
01020                                 ANIM_channel_draw_widgets(ac, ale, block, yminc, ymaxc);
01021                         }
01022                         
01023                         /* adjust y-position for next one */
01024                         y -= ACHANNEL_STEP;
01025                 }
01026                 
01027                 uiEndBlock(C, block);
01028                 uiDrawBlock(C, block);
01029                 
01030                 glDisable(GL_BLEND);
01031         }
01032         
01033         /* free tempolary channels */
01034         BLI_freelistN(&anim_data);
01035 }