Blender V2.61 - r43446

graph_draw.c

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