Blender V2.61 - r43446

view3d_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) 2008 Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * 
00022  * Contributor(s): Blender Foundation
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00032 #include <string.h>
00033 #include <stdio.h>
00034 #include <math.h>
00035 
00036 #include "DNA_armature_types.h"
00037 #include "DNA_camera_types.h"
00038 #include "DNA_customdata_types.h"
00039 #include "DNA_object_types.h"
00040 #include "DNA_group_types.h"
00041 #include "DNA_key_types.h"
00042 #include "DNA_lamp_types.h"
00043 #include "DNA_scene_types.h"
00044 #include "DNA_world_types.h"
00045 
00046 #include "MEM_guardedalloc.h"
00047 
00048 #include "BLI_blenlib.h"
00049 #include "BLI_math.h"
00050 #include "BLI_rand.h"
00051 #include "BLI_utildefines.h"
00052 
00053 #include "BKE_anim.h"
00054 #include "BKE_camera.h"
00055 #include "BKE_context.h"
00056 #include "BKE_customdata.h"
00057 #include "BKE_image.h"
00058 #include "BKE_key.h"
00059 #include "BKE_object.h"
00060 #include "BKE_global.h"
00061 #include "BKE_paint.h"
00062 #include "BKE_scene.h"
00063 #include "BKE_screen.h"
00064 #include "BKE_unit.h"
00065 #include "BKE_movieclip.h"
00066 
00067 #include "RE_engine.h"
00068 #include "RE_pipeline.h"    // make_stars
00069 
00070 #include "IMB_imbuf_types.h"
00071 #include "IMB_imbuf.h"
00072 
00073 #include "BIF_gl.h"
00074 #include "BIF_glutil.h"
00075 
00076 #include "WM_api.h"
00077 #include "BLF_api.h"
00078 
00079 #include "ED_armature.h"
00080 #include "ED_keyframing.h"
00081 #include "ED_gpencil.h"
00082 #include "ED_screen.h"
00083 #include "ED_space_api.h"
00084 #include "ED_screen_types.h"
00085 #include "ED_transform.h"
00086 
00087 #include "UI_interface.h"
00088 #include "UI_interface_icons.h"
00089 #include "UI_resources.h"
00090 
00091 #include "GPU_draw.h"
00092 #include "GPU_material.h"
00093 #include "GPU_extensions.h"
00094 
00095 #include "view3d_intern.h"  // own include
00096 
00097 
00098 
00099 static void star_stuff_init_func(void)
00100 {
00101     cpack(-1);
00102     glPointSize(1.0);
00103     glBegin(GL_POINTS);
00104 }
00105 static void star_stuff_vertex_func(float* i)
00106 {
00107     glVertex3fv(i);
00108 }
00109 static void star_stuff_term_func(void)
00110 {
00111     glEnd();
00112 }
00113 
00114 void circf(float x, float y, float rad)
00115 {
00116     GLUquadricObj *qobj = gluNewQuadric(); 
00117     
00118     gluQuadricDrawStyle(qobj, GLU_FILL); 
00119     
00120     glPushMatrix(); 
00121     
00122     glTranslatef(x,  y, 0.); 
00123     
00124     gluDisk( qobj, 0.0,  rad, 32, 1); 
00125     
00126     glPopMatrix(); 
00127     
00128     gluDeleteQuadric(qobj);
00129 }
00130 
00131 void circ(float x, float y, float rad)
00132 {
00133     GLUquadricObj *qobj = gluNewQuadric(); 
00134     
00135     gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
00136     
00137     glPushMatrix(); 
00138     
00139     glTranslatef(x,  y, 0.); 
00140     
00141     gluDisk( qobj, 0.0,  rad, 32, 1); 
00142     
00143     glPopMatrix(); 
00144     
00145     gluDeleteQuadric(qobj);
00146 }
00147 
00148 
00149 /* ********* custom clipping *********** */
00150 
00151 static void view3d_draw_clipping(RegionView3D *rv3d)
00152 {
00153     BoundBox *bb= rv3d->clipbb;
00154 
00155     if(bb) {
00156         static unsigned int clipping_index[6][4]= {{0, 1, 2, 3},
00157                                                    {0, 4, 5, 1},
00158                                                    {4, 7, 6, 5},
00159                                                    {7, 3, 2, 6},
00160                                                    {1, 5, 6, 2},
00161                                                    {7, 4, 0, 3}};
00162 
00163         UI_ThemeColorShade(TH_BACK, -8);
00164 
00165         glEnableClientState(GL_VERTEX_ARRAY);
00166         glVertexPointer(3, GL_FLOAT, 0, bb->vec);
00167         glDrawElements(GL_QUADS, sizeof(clipping_index)/sizeof(unsigned int), GL_UNSIGNED_INT, clipping_index);
00168         glDisableClientState(GL_VERTEX_ARRAY);
00169 
00170     }
00171 }
00172 
00173 void view3d_set_clipping(RegionView3D *rv3d)
00174 {
00175     double plane[4];
00176     int a, tot=4;
00177     
00178     if(rv3d->viewlock) tot= 6;
00179     
00180     for(a=0; a<tot; a++) {
00181         QUATCOPY(plane, rv3d->clip[a]);
00182         glClipPlane(GL_CLIP_PLANE0+a, plane);
00183         glEnable(GL_CLIP_PLANE0+a);
00184     }
00185 }
00186 
00187 void view3d_clr_clipping(void)
00188 {
00189     int a;
00190     
00191     for(a=0; a<6; a++) {
00192         glDisable(GL_CLIP_PLANE0+a);
00193     }
00194 }
00195 
00196 static int test_clipping(const float vec[3], float clip[][4])
00197 {
00198     float view[3];
00199     copy_v3_v3(view, vec);
00200 
00201     if(0.0f < clip[0][3] + dot_v3v3(view, clip[0]))
00202         if(0.0f < clip[1][3] + dot_v3v3(view, clip[1]))
00203             if(0.0f < clip[2][3] + dot_v3v3(view, clip[2]))
00204                 if(0.0f < clip[3][3] + dot_v3v3(view, clip[3]))
00205                     return 0;
00206 
00207     return 1;
00208 }
00209 
00210 /* for 'local' ED_view3d_local_clipping must run first
00211  * then all comparisons can be done in localspace */
00212 int ED_view3d_test_clipping(RegionView3D *rv3d, const float vec[3], const int local)
00213 {
00214     return test_clipping(vec, local ? rv3d->clip_local : rv3d->clip);
00215 }
00216 
00217 /* ********* end custom clipping *********** */
00218 
00219 
00220 static void drawgrid_draw(ARegion *ar, float wx, float wy, float x, float y, float dx)
00221 {   
00222     float verts[2][2];
00223 
00224     x+= (wx); 
00225     y+= (wy);
00226 
00227     /* set fixed 'Y' */
00228     verts[0][1]= 0.0f;
00229     verts[1][1]= (float)ar->winy;
00230 
00231     /* iter over 'X' */
00232     verts[0][0] = verts[1][0] = x-dx*floorf(x/dx);
00233     glEnableClientState(GL_VERTEX_ARRAY);
00234     glVertexPointer(2, GL_FLOAT, 0, verts);
00235 
00236     while(verts[0][0] < ar->winx) {
00237         glDrawArrays(GL_LINES, 0, 2);
00238         verts[0][0] = verts[1][0] = verts[0][0] + dx;
00239     }
00240 
00241     /* set fixed 'X' */
00242     verts[0][0]= 0.0f;
00243     verts[1][0]= (float)ar->winx;
00244 
00245     /* iter over 'Y' */
00246     verts[0][1]= verts[1][1]= y-dx*floorf(y/dx);
00247     while(verts[0][1] < ar->winy) {
00248         glDrawArrays(GL_LINES, 0, 2);
00249         verts[0][1] = verts[1][1] = verts[0][1] + dx;
00250     }
00251 
00252     glDisableClientState(GL_VERTEX_ARRAY);
00253 }
00254 
00255 #define GRID_MIN_PX 6.0f
00256 
00257 static void drawgrid(UnitSettings *unit, ARegion *ar, View3D *v3d, const char **grid_unit)
00258 {
00259     /* extern short bgpicmode; */
00260     RegionView3D *rv3d= ar->regiondata;
00261     float wx, wy, x, y, fw, fx, fy, dx;
00262     float vec4[4];
00263     unsigned char col[3], col2[3];
00264 
00265     vec4[0]=vec4[1]=vec4[2]=0.0; 
00266     vec4[3]= 1.0;
00267     mul_m4_v4(rv3d->persmat, vec4);
00268     fx= vec4[0]; 
00269     fy= vec4[1]; 
00270     fw= vec4[3];
00271 
00272     wx= (ar->winx/2.0); /* because of rounding errors, grid at wrong location */
00273     wy= (ar->winy/2.0);
00274 
00275     x= (wx)*fx/fw;
00276     y= (wy)*fy/fw;
00277 
00278     vec4[0]=vec4[1]= v3d->grid;
00279 
00280     vec4[2]= 0.0;
00281     vec4[3]= 1.0;
00282     mul_m4_v4(rv3d->persmat, vec4);
00283     fx= vec4[0]; 
00284     fy= vec4[1]; 
00285     fw= vec4[3];
00286 
00287     dx= fabs(x-(wx)*fx/fw);
00288     if(dx==0) dx= fabs(y-(wy)*fy/fw);
00289     
00290     glDepthMask(0);     // disable write in zbuffer
00291 
00292     /* check zoom out */
00293     UI_ThemeColor(TH_GRID);
00294     
00295     if(unit->system) {
00296         /* Use GRID_MIN_PX*2 for units because very very small grid
00297          * items are less useful when dealing with units */
00298         void *usys;
00299         int len, i;
00300         float dx_scalar;
00301         float blend_fac;
00302 
00303         bUnit_GetSystem(&usys, &len, unit->system, B_UNIT_LENGTH);
00304 
00305         if(usys) {
00306             i= len;
00307             while(i--) {
00308                 float scalar= bUnit_GetScaler(usys, i);
00309 
00310                 dx_scalar = dx * scalar / unit->scale_length;
00311                 if (dx_scalar < (GRID_MIN_PX*2))
00312                     continue;
00313 
00314                 /* Store the smallest drawn grid size units name so users know how big each grid cell is */
00315                 if(*grid_unit==NULL) {
00316                     *grid_unit= bUnit_GetNameDisplay(usys, i);
00317                     rv3d->gridview= (scalar * v3d->grid) / unit->scale_length;
00318                 }
00319                 blend_fac= 1-((GRID_MIN_PX*2)/dx_scalar);
00320 
00321                 /* tweak to have the fade a bit nicer */
00322                 blend_fac= (blend_fac * blend_fac) * 2.0f;
00323                 CLAMP(blend_fac, 0.3f, 1.0f);
00324 
00325 
00326                 UI_ThemeColorBlend(TH_BACK, TH_GRID, blend_fac);
00327 
00328                 drawgrid_draw(ar, wx, wy, x, y, dx_scalar);
00329             }
00330         }
00331     }
00332     else {
00333         short sublines = v3d->gridsubdiv;
00334 
00335         if(dx<GRID_MIN_PX) {
00336             rv3d->gridview*= sublines;
00337             dx*= sublines;
00338             
00339             if(dx<GRID_MIN_PX) {
00340                 rv3d->gridview*= sublines;
00341                 dx*= sublines;
00342 
00343                 if(dx<GRID_MIN_PX) {
00344                     rv3d->gridview*= sublines;
00345                     dx*=sublines;
00346                     if(dx<GRID_MIN_PX);
00347                     else {
00348                         UI_ThemeColor(TH_GRID);
00349                         drawgrid_draw(ar, wx, wy, x, y, dx);
00350                     }
00351                 }
00352                 else {  // start blending out
00353                     UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
00354                     drawgrid_draw(ar, wx, wy, x, y, dx);
00355 
00356                     UI_ThemeColor(TH_GRID);
00357                     drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
00358                 }
00359             }
00360             else {  // start blending out (GRID_MIN_PX < dx < (GRID_MIN_PX*10))
00361                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
00362                 drawgrid_draw(ar, wx, wy, x, y, dx);
00363 
00364                 UI_ThemeColor(TH_GRID);
00365                 drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
00366             }
00367         }
00368         else {
00369             if(dx>(GRID_MIN_PX*10)) {       // start blending in
00370                 rv3d->gridview/= sublines;
00371                 dx/= sublines;
00372                 if(dx>(GRID_MIN_PX*10)) {       // start blending in
00373                     rv3d->gridview/= sublines;
00374                     dx/= sublines;
00375                     if(dx>(GRID_MIN_PX*10)) {
00376                         UI_ThemeColor(TH_GRID);
00377                         drawgrid_draw(ar, wx, wy, x, y, dx);
00378                     }
00379                     else {
00380                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
00381                         drawgrid_draw(ar, wx, wy, x, y, dx);
00382                         UI_ThemeColor(TH_GRID);
00383                         drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
00384                     }
00385                 }
00386                 else {
00387                     UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
00388                     drawgrid_draw(ar, wx, wy, x, y, dx);
00389                     UI_ThemeColor(TH_GRID);
00390                     drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
00391                 }
00392             }
00393             else {
00394                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
00395                 drawgrid_draw(ar, wx, wy, x, y, dx);
00396                 UI_ThemeColor(TH_GRID);
00397                 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
00398             }
00399         }
00400     }
00401 
00402 
00403     x+= (wx); 
00404     y+= (wy);
00405     UI_GetThemeColor3ubv(TH_GRID, col);
00406 
00407     setlinestyle(0);
00408     
00409     /* center cross */
00410     /* horizontal line */
00411     if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) 
00412         UI_make_axis_color(col, col2, 'Y');
00413     else UI_make_axis_color(col, col2, 'X');
00414     glColor3ubv(col2);
00415     
00416     fdrawline(0.0,  y,  (float)ar->winx,  y); 
00417     
00418     /* vertical line */
00419     if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) 
00420         UI_make_axis_color(col, col2, 'Y');
00421     else UI_make_axis_color(col, col2, 'Z');
00422     glColor3ubv(col2);
00423 
00424     fdrawline(x, 0.0, x, (float)ar->winy); 
00425 
00426     glDepthMask(1);     // enable write in zbuffer
00427 }
00428 #undef GRID_MIN_PX
00429 
00430 static void drawfloor(Scene *scene, View3D *v3d, const char **grid_unit)
00431 {
00432     float grid, grid_scale;
00433     unsigned char col_grid[3];
00434     const int gridlines= v3d->gridlines/2;
00435 
00436     if(v3d->gridlines<3) return;
00437     
00438     grid_scale= v3d->grid;
00439     /* use 'grid_scale' instead of 'v3d->grid' from now on */
00440 
00441     /* apply units */
00442     if(scene->unit.system) {
00443         void *usys;
00444         int len;
00445 
00446         bUnit_GetSystem(&usys, &len, scene->unit.system, B_UNIT_LENGTH);
00447 
00448         if(usys) {
00449             int i= bUnit_GetBaseUnit(usys);
00450             *grid_unit= bUnit_GetNameDisplay(usys, i);
00451             grid_scale = (grid_scale * (float)bUnit_GetScaler(usys, i)) / scene->unit.scale_length;
00452         }
00453     }
00454 
00455     grid= gridlines * grid_scale;
00456 
00457     if(v3d->zbuf && scene->obedit) glDepthMask(0);  // for zbuffer-select
00458 
00459     UI_GetThemeColor3ubv(TH_GRID, col_grid);
00460 
00461     /* draw the Y axis and/or grid lines */
00462     if(v3d->gridflag & V3D_SHOW_FLOOR) {
00463         float vert[4][3]= {{0.0f}};
00464         unsigned char col_bg[3];
00465         unsigned char col_grid_emphasise[3], col_grid_light[3];
00466         int a;
00467         int prev_emphasise= -1;
00468 
00469         UI_GetThemeColor3ubv(TH_BACK, col_bg);
00470 
00471         /* emphasise division lines lighter instead of darker, if background is darker than grid */
00472         UI_GetColorPtrShade3ubv(col_grid, col_grid_light, 10);
00473         UI_GetColorPtrShade3ubv(col_grid, col_grid_emphasise,
00474                                 (((col_grid[0]+col_grid[1]+col_grid[2])+30) > (col_bg[0]+col_bg[1]+col_bg[2])) ? 20 : -10);
00475 
00476         /* set fixed axis */
00477         vert[0][0]= vert[2][1]= grid;
00478         vert[1][0]= vert[3][1]= -grid;
00479 
00480         glEnableClientState(GL_VERTEX_ARRAY);
00481         glVertexPointer(3, GL_FLOAT, 0, vert);
00482 
00483         for(a= -gridlines;a<=gridlines;a++) {
00484             const float line= a * grid_scale;
00485             const int is_emphasise= (a % 10) == 0;
00486 
00487             if(is_emphasise != prev_emphasise) {
00488                 glColor3ubv(is_emphasise ? col_grid_emphasise : col_grid_light);
00489                 prev_emphasise= is_emphasise;
00490             }
00491 
00492             /* set variable axis */
00493             vert[0][1]= vert[1][1]=
00494             vert[2][0]= vert[3][0]= line;
00495 
00496             glDrawArrays(GL_LINES, 0, 4);
00497         }
00498 
00499         glDisableClientState(GL_VERTEX_ARRAY);
00500 
00501         GPU_print_error("sdsd");
00502     }
00503     
00504     /* draw the Z axis line */  
00505     /* check for the 'show Z axis' preference */
00506     if (v3d->gridflag & (V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_Z)) {
00507         int axis;
00508         for(axis= 0; axis < 3; axis++)
00509         if (v3d->gridflag & (V3D_SHOW_X << axis)) {
00510             float vert[3];
00511             unsigned char tcol[3];
00512 
00513             UI_make_axis_color(col_grid, tcol, 'X' + axis);
00514             glColor3ubv(tcol);
00515 
00516             glBegin(GL_LINE_STRIP);
00517             zero_v3(vert);
00518             vert[axis]= grid;
00519             glVertex3fv(vert );
00520             vert[axis]= -grid;
00521             glVertex3fv(vert);
00522             glEnd();
00523         }
00524     }
00525 
00526 
00527 
00528 
00529     if(v3d->zbuf && scene->obedit) glDepthMask(1);  
00530     
00531 }
00532 
00533 static void drawcursor(Scene *scene, ARegion *ar, View3D *v3d)
00534 {
00535     int mx, my, co[2];
00536     int flag;
00537     
00538     /* we dont want the clipping for cursor */
00539     flag= v3d->flag;
00540     v3d->flag= 0;
00541     project_int(ar, give_cursor(scene, v3d), co);
00542     v3d->flag= flag;
00543     
00544     mx = co[0];
00545     my = co[1];
00546     
00547     if(mx!=IS_CLIPPED) {
00548         setlinestyle(0); 
00549         cpack(0xFF);
00550         circ((float)mx, (float)my, 10.0);
00551         setlinestyle(4); 
00552         cpack(0xFFFFFF);
00553         circ((float)mx, (float)my, 10.0);
00554         setlinestyle(0);
00555         cpack(0x0);
00556         
00557         sdrawline(mx-20, my, mx-5, my);
00558         sdrawline(mx+5, my, mx+20, my);
00559         sdrawline(mx, my-20, mx, my-5);
00560         sdrawline(mx, my+5, mx, my+20);
00561     }
00562 }
00563 
00564 /* Draw a live substitute of the view icon, which is always shown
00565  * colors copied from transform_manipulator.c, we should keep these matching. */
00566 static void draw_view_axis(RegionView3D *rv3d)
00567 {
00568     const float k = U.rvisize;   /* axis size */
00569     const float toll = 0.5;      /* used to see when view is quasi-orthogonal */
00570     const float start = k + 1.0f;/* axis center in screen coordinates, x=y */
00571     float ydisp = 0.0;          /* vertical displacement to allow obj info text */
00572     int bright = 25*(float)U.rvibright + 5; /* axis alpha (rvibright has range 0-10) */
00573 
00574     float vec[3];
00575     float dx, dy;
00576     
00577     /* thickness of lines is proportional to k */
00578     glLineWidth(2);
00579 
00580     glEnable(GL_BLEND);
00581     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00582 
00583     /* X */
00584     vec[0] = 1;
00585     vec[1] = vec[2] = 0;
00586     mul_qt_v3(rv3d->viewquat, vec);
00587     dx = vec[0] * k;
00588     dy = vec[1] * k;
00589 
00590     glColor4ub(220, 0, 0, bright);
00591     glBegin(GL_LINES);
00592     glVertex2f(start, start + ydisp);
00593     glVertex2f(start + dx, start + dy + ydisp);
00594     glEnd();
00595 
00596     if (fabsf(dx) > toll || fabsf(dy) > toll) {
00597         BLF_draw_default_ascii(start + dx + 2, start + dy + ydisp + 2, 0.0f, "x", 1);
00598     }
00599     
00600     /* BLF_draw_default disables blending */
00601     glEnable(GL_BLEND);
00602 
00603     /* Y */
00604     vec[1] = 1;
00605     vec[0] = vec[2] = 0;
00606     mul_qt_v3(rv3d->viewquat, vec);
00607     dx = vec[0] * k;
00608     dy = vec[1] * k;
00609 
00610     glColor4ub(0, 220, 0, bright);
00611     glBegin(GL_LINES);
00612     glVertex2f(start, start + ydisp);
00613     glVertex2f(start + dx, start + dy + ydisp);
00614     glEnd();
00615 
00616     if (fabsf(dx) > toll || fabsf(dy) > toll) {
00617         BLF_draw_default_ascii(start + dx + 2, start + dy + ydisp + 2, 0.0f, "y", 1);
00618     }
00619 
00620     glEnable(GL_BLEND);
00621     
00622     /* Z */
00623     vec[2] = 1;
00624     vec[1] = vec[0] = 0;
00625     mul_qt_v3(rv3d->viewquat, vec);
00626     dx = vec[0] * k;
00627     dy = vec[1] * k;
00628 
00629     glColor4ub(30, 30, 220, bright);
00630     glBegin(GL_LINES);
00631     glVertex2f(start, start + ydisp);
00632     glVertex2f(start + dx, start + dy + ydisp);
00633     glEnd();
00634 
00635     if (fabsf(dx) > toll || fabsf(dy) > toll) {
00636         BLF_draw_default_ascii(start + dx + 2, start + dy + ydisp + 2, 0.0f, "z", 1);
00637     }
00638 
00639     /* restore line-width */
00640     
00641     glLineWidth(1.0);
00642     glDisable(GL_BLEND);
00643 }
00644 
00645 /* draw center and axis of rotation for ongoing 3D mouse navigation */
00646 static void draw_rotation_guide(RegionView3D *rv3d)
00647 {
00648     float o[3]; // center of rotation
00649     float end[3]; // endpoints for drawing
00650 
00651     float color[4] = {0.f ,0.4235f, 1.f, 1.f}; // bright blue so it matches device LEDs
00652 
00653     negate_v3_v3(o, rv3d->ofs);
00654 
00655     glEnable(GL_BLEND);
00656     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00657     glShadeModel(GL_SMOOTH);
00658     glPointSize(5);
00659     glEnable(GL_POINT_SMOOTH);
00660     glDepthMask(0); // don't overwrite zbuf
00661 
00662     if (rv3d->rot_angle != 0.f) {
00663         // -- draw rotation axis --
00664         float scaled_axis[3];
00665         const float scale = rv3d->dist;
00666         mul_v3_v3fl(scaled_axis, rv3d->rot_axis, scale);
00667 
00668 
00669         glBegin(GL_LINE_STRIP);
00670         color[3] = 0.f; // more transparent toward the ends
00671         glColor4fv(color);
00672         add_v3_v3v3(end, o, scaled_axis);
00673         glVertex3fv(end);
00674 
00675         // color[3] = 0.2f + fabsf(rv3d->rot_angle); // modulate opacity with angle
00676         // ^^ neat idea, but angle is frame-rate dependent, so it's usually close to 0.2
00677 
00678         color[3] = 0.5f; // more opaque toward the center
00679         glColor4fv(color);
00680         glVertex3fv(o);
00681 
00682         color[3] = 0.f;
00683         glColor4fv(color);
00684         sub_v3_v3v3(end, o, scaled_axis);
00685         glVertex3fv(end);
00686         glEnd();
00687         
00688         // -- draw ring around rotation center --
00689         {
00690 #define     ROT_AXIS_DETAIL 13
00691 
00692             const float s = 0.05f * scale;
00693             const float step = 2.f * (float)(M_PI / ROT_AXIS_DETAIL);
00694             float angle;
00695             int i;
00696 
00697             float q[4]; // rotate ring so it's perpendicular to axis
00698             const int upright = fabsf(rv3d->rot_axis[2]) >= 0.95f;
00699             if (!upright) {
00700                 const float up[3] = {0.f, 0.f, 1.f};
00701                 float vis_angle, vis_axis[3];
00702 
00703                 cross_v3_v3v3(vis_axis, up, rv3d->rot_axis);
00704                 vis_angle = acosf(dot_v3v3(up, rv3d->rot_axis));
00705                 axis_angle_to_quat(q, vis_axis, vis_angle);
00706             }
00707 
00708             color[3] = 0.25f; // somewhat faint
00709             glColor4fv(color);
00710             glBegin(GL_LINE_LOOP);
00711             for (i = 0, angle = 0.f; i < ROT_AXIS_DETAIL; ++i, angle += step) {
00712                 float p[3] = {s * cosf(angle), s * sinf(angle), 0.0f};
00713 
00714                 if (!upright) {
00715                     mul_qt_v3(q, p);
00716                 }
00717 
00718                 add_v3_v3(p, o);
00719                 glVertex3fv(p);
00720             }
00721             glEnd();
00722 
00723 #undef      ROT_AXIS_DETAIL
00724         }
00725 
00726         color[3] = 1.f; // solid dot
00727     }
00728     else
00729         color[3] = 0.5f; // see-through dot
00730 
00731     // -- draw rotation center --
00732     glColor4fv(color);
00733     glBegin(GL_POINTS);
00734         glVertex3fv(o);
00735     glEnd();
00736 
00737     // find screen coordinates for rotation center, then draw pretty icon
00738     // mul_m4_v3(rv3d->persinv, rot_center);
00739     // UI_icon_draw(rot_center[0], rot_center[1], ICON_NDOF_TURN);
00740     // ^^ just playing around, does not work
00741 
00742     glDisable(GL_BLEND);
00743     glDisable(GL_POINT_SMOOTH);
00744     glDepthMask(1);
00745 }
00746 
00747 static void draw_view_icon(RegionView3D *rv3d)
00748 {
00749     BIFIconID icon;
00750     
00751     if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) 
00752         icon= ICON_AXIS_TOP;
00753     else if( ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) 
00754         icon= ICON_AXIS_FRONT;
00755     else if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) 
00756         icon= ICON_AXIS_SIDE;
00757     else return ;
00758     
00759     glEnable(GL_BLEND);
00760     glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
00761     
00762     UI_icon_draw(5.0, 5.0, icon);
00763     
00764     glDisable(GL_BLEND);
00765 }
00766 
00767 static const char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
00768 {
00769     const char *name = NULL;
00770     
00771     switch (rv3d->view) {
00772         case RV3D_VIEW_FRONT:
00773             if (rv3d->persp == RV3D_ORTHO) name = "Front Ortho";
00774             else name = "Front Persp";
00775             break;
00776         case RV3D_VIEW_BACK:
00777             if (rv3d->persp == RV3D_ORTHO) name = "Back Ortho";
00778             else name = "Back Persp";
00779             break;
00780         case RV3D_VIEW_TOP:
00781             if (rv3d->persp == RV3D_ORTHO) name = "Top Ortho";
00782             else name = "Top Persp";
00783             break;
00784         case RV3D_VIEW_BOTTOM:
00785             if (rv3d->persp == RV3D_ORTHO) name = "Bottom Ortho";
00786             else name = "Bottom Persp";
00787             break;
00788         case RV3D_VIEW_RIGHT:
00789             if (rv3d->persp == RV3D_ORTHO) name = "Right Ortho";
00790             else name = "Right Persp";
00791             break;
00792         case RV3D_VIEW_LEFT:
00793             if (rv3d->persp == RV3D_ORTHO) name = "Left Ortho";
00794             else name = "Left Persp";
00795             break;
00796             
00797         default:
00798             if (rv3d->persp==RV3D_CAMOB) {
00799                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
00800                     Camera *cam;
00801                     cam = v3d->camera->data;
00802                     name = (cam->type != CAM_ORTHO) ? "Camera Persp" : "Camera Ortho";
00803                 } else {
00804                     name = "Object as Camera";
00805                 }
00806             } else { 
00807                 name = (rv3d->persp == RV3D_ORTHO) ? "User Ortho" : "User Persp";
00808             }
00809             break;
00810     }
00811     
00812     return name;
00813 }
00814 
00815 static void draw_viewport_name(ARegion *ar, View3D *v3d)
00816 {
00817     RegionView3D *rv3d= ar->regiondata;
00818     const char *name= view3d_get_name(v3d, rv3d);
00819     char tmpstr[24];
00820     
00821     if (v3d->localvd) {
00822         BLI_snprintf(tmpstr, sizeof(tmpstr), "%s (Local)", name);
00823         name= tmpstr;
00824     }
00825 
00826     if (name) {
00827         UI_ThemeColor(TH_TEXT_HI);
00828         BLF_draw_default_ascii(22,  ar->winy-17, 0.0f, name, sizeof(tmpstr));
00829     }
00830 }
00831 
00832 /* draw info beside axes in bottom left-corner: 
00833 *   framenum, object name, bone name (if available), marker name (if available)
00834 */
00835 static void draw_selected_name(Scene *scene, Object *ob)
00836 {
00837     char info[256], *markern;
00838     short offset=30;
00839     
00840     /* get name of marker on current frame (if available) */
00841     markern= scene_find_marker_name(scene, CFRA);
00842     
00843     /* check if there is an object */
00844     if(ob) {
00845         /* name(s) to display depends on type of object */
00846         if(ob->type==OB_ARMATURE) {
00847             bArmature *arm= ob->data;
00848             char *name= NULL;
00849             
00850             /* show name of active bone too (if possible) */
00851             if(arm->edbo) {
00852 
00853                 if(arm->act_edbone)
00854                     name= ((EditBone *)arm->act_edbone)->name;
00855 
00856             }
00857             else if(ob->mode & OB_MODE_POSE) {
00858                 if(arm->act_bone) {
00859 
00860                     if(arm->act_bone->layer & arm->layer)
00861                         name= arm->act_bone->name;
00862 
00863                 }
00864             }
00865             if(name && markern)
00866                 BLI_snprintf(info, sizeof(info), "(%d) %s %s <%s>", CFRA, ob->id.name+2, name, markern);
00867             else if(name)
00868                 BLI_snprintf(info, sizeof(info), "(%d) %s %s", CFRA, ob->id.name+2, name);
00869             else
00870                 BLI_snprintf(info, sizeof(info), "(%d) %s", CFRA, ob->id.name+2);
00871         }
00872         else if(ELEM3(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
00873             Key *key= NULL;
00874             KeyBlock *kb = NULL;
00875             char shapes[MAX_NAME + 10];
00876             
00877             /* try to display active shapekey too */
00878             shapes[0] = '\0';
00879             key = ob_get_key(ob);
00880             if(key){
00881                 kb = BLI_findlink(&key->block, ob->shapenr-1);
00882                 if(kb){
00883                     BLI_snprintf(shapes, sizeof(shapes), ": %s ", kb->name);
00884                     if(ob->shapeflag == OB_SHAPE_LOCK){
00885                         strcat(shapes, " (Pinned)");
00886                     }
00887                 }
00888             }
00889             
00890             if(markern)
00891                 BLI_snprintf(info, sizeof(info), "(%d) %s %s <%s>", CFRA, ob->id.name+2, shapes, markern);
00892             else
00893                 BLI_snprintf(info, sizeof(info), "(%d) %s %s", CFRA, ob->id.name+2, shapes);
00894         }
00895         else {
00896             /* standard object */
00897             if (markern)
00898                 BLI_snprintf(info, sizeof(info), "(%d) %s <%s>", CFRA, ob->id.name+2, markern);
00899             else
00900                 BLI_snprintf(info, sizeof(info), "(%d) %s", CFRA, ob->id.name+2);
00901         }
00902         
00903         /* color depends on whether there is a keyframe */
00904         if (id_frame_has_keyframe((ID *)ob, /*BKE_curframe(scene)*/(float)(CFRA), ANIMFILTER_KEYS_LOCAL))
00905             UI_ThemeColor(TH_VERTEX_SELECT);
00906         else
00907             UI_ThemeColor(TH_TEXT_HI);
00908     }
00909     else {
00910         /* no object */
00911         if (markern)
00912             BLI_snprintf(info, sizeof(info), "(%d) <%s>", CFRA, markern);
00913         else
00914             BLI_snprintf(info, sizeof(info), "(%d)", CFRA);
00915         
00916         /* color is always white */
00917         UI_ThemeColor(TH_TEXT_HI);
00918     }
00919     
00920     if (U.uiflag & USER_SHOW_ROTVIEWICON)
00921         offset = 14 + (U.rvisize * 2);
00922 
00923     BLF_draw_default(offset,  10, 0.0f, info, sizeof(info));
00924 }
00925 
00926 static void view3d_camera_border(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, rctf *viewborder_r, short no_shift, short no_zoom)
00927 {
00928     CameraParams params;
00929     rctf rect_view, rect_camera;
00930 
00931     /* get viewport viewplane */
00932     camera_params_init(&params);
00933     camera_params_from_view3d(&params, v3d, rv3d);
00934     if(no_zoom)
00935         params.zoom= 1.0f;
00936     camera_params_compute_viewplane(&params, ar->winx, ar->winy, 1.0f, 1.0f);
00937     rect_view= params.viewplane;
00938 
00939     /* get camera viewplane */
00940     camera_params_init(&params);
00941     camera_params_from_object(&params, v3d->camera);
00942     if(no_shift) {
00943         params.shiftx= 0.0f;
00944         params.shifty= 0.0f;
00945     }
00946     camera_params_compute_viewplane(&params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
00947     rect_camera= params.viewplane;
00948 
00949     /* get camera border within viewport */
00950     viewborder_r->xmin= ((rect_camera.xmin - rect_view.xmin)/(rect_view.xmax - rect_view.xmin))*ar->winx;
00951     viewborder_r->xmax= ((rect_camera.xmax - rect_view.xmin)/(rect_view.xmax - rect_view.xmin))*ar->winx;
00952     viewborder_r->ymin= ((rect_camera.ymin - rect_view.ymin)/(rect_view.ymax - rect_view.ymin))*ar->winy;
00953     viewborder_r->ymax= ((rect_camera.ymax - rect_view.ymin)/(rect_view.ymax - rect_view.ymin))*ar->winy;
00954 }
00955 
00956 void ED_view3d_calc_camera_border_size(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, float size_r[2])
00957 {
00958     rctf viewborder;
00959 
00960     view3d_camera_border(scene, ar, v3d, rv3d, &viewborder, TRUE, TRUE);
00961     size_r[0]= viewborder.xmax - viewborder.xmin;
00962     size_r[1]= viewborder.ymax - viewborder.ymin;
00963 }
00964 
00965 void ED_view3d_calc_camera_border(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, rctf *viewborder_r, short no_shift)
00966 {
00967     view3d_camera_border(scene, ar, v3d, rv3d, viewborder_r, no_shift, FALSE);
00968 }
00969 
00970 static void drawviewborder_grid3(float x1, float x2, float y1, float y2, float fac)
00971 {
00972     float x3, y3, x4, y4;
00973 
00974     x3= x1 + fac * (x2-x1);
00975     y3= y1 + fac * (y2-y1);
00976     x4= x1 + (1.0f - fac) * (x2-x1);
00977     y4= y1 + (1.0f - fac) * (y2-y1);
00978 
00979     glBegin(GL_LINES);
00980     glVertex2f(x1, y3);
00981     glVertex2f(x2, y3);
00982 
00983     glVertex2f(x1, y4);
00984     glVertex2f(x2, y4);
00985 
00986     glVertex2f(x3, y1);
00987     glVertex2f(x3, y2);
00988 
00989     glVertex2f(x4, y1);
00990     glVertex2f(x4, y2);
00991     glEnd();
00992 }
00993 
00994 /* harmonious triangle */
00995 static void drawviewborder_triangle(float x1, float x2, float y1, float y2, const char golden, const char dir)
00996 {
00997     float ofs;
00998     float w= x2 - x1;
00999     float h= y2 - y1;
01000 
01001     glBegin(GL_LINES);
01002     if(w > h) {
01003         if(golden) {
01004             ofs = w * (1.0f-(1.0f/1.61803399f));
01005         }
01006         else {
01007             ofs = h * (h / w);
01008         }
01009         if(dir == 'B') SWAP(float, y1, y2);
01010 
01011         glVertex2f(x1, y1);
01012         glVertex2f(x2, y2);
01013 
01014         glVertex2f(x2, y1);
01015         glVertex2f(x1 + (w - ofs), y2);
01016 
01017         glVertex2f(x1, y2);
01018         glVertex2f(x1 + ofs, y1);
01019     }
01020     else {
01021         if(golden) {
01022             ofs = h * (1.0f-(1.0f/1.61803399f));
01023         }
01024         else {
01025             ofs = w * (w / h);
01026         }
01027         if(dir == 'B') SWAP(float, x1, x2);
01028 
01029         glVertex2f(x1, y1);
01030         glVertex2f(x2, y2);
01031 
01032         glVertex2f(x2, y1);
01033         glVertex2f(x1, y1 + ofs);
01034 
01035         glVertex2f(x1, y2);
01036         glVertex2f(x2, y1 + (h - ofs));
01037     }
01038     glEnd();
01039 }
01040 
01041 static void drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
01042 {
01043     float fac, a;
01044     float x1, x2, y1, y2;
01045     float x1i, x2i, y1i, y2i;
01046     float x3, y3, x4, y4;
01047     rctf viewborder;
01048     Camera *ca= NULL;
01049     RegionView3D *rv3d= (RegionView3D *)ar->regiondata;
01050     
01051     if(v3d->camera==NULL)
01052         return;
01053     if(v3d->camera->type==OB_CAMERA)
01054         ca = v3d->camera->data;
01055     
01056     ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &viewborder, FALSE);
01057     /* the offsets */
01058     x1= viewborder.xmin;
01059     y1= viewborder.ymin;
01060     x2= viewborder.xmax;
01061     y2= viewborder.ymax;
01062     
01063     /* apply offsets so the real 3D camera shows through */
01064 
01065     /* note: quite un-scientific but without this bit extra
01066      * 0.0001 on the lower left the 2D border sometimes
01067      * obscures the 3D camera border */
01068     /* note: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticable
01069      * but keep it here incase we need to remove the workaround */
01070     x1i= (int)(x1 - 1.0001f);
01071     y1i= (int)(y1 - 1.0001f);
01072     x2i= (int)(x2 + (1.0f-0.0001f));
01073     y2i= (int)(y2 + (1.0f-0.0001f));
01074     
01075     /* passepartout, specified in camera edit buttons */
01076     if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
01077         if (ca->passepartalpha == 1.0f) {
01078             glColor3f(0, 0, 0);
01079         } else {
01080             glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
01081             glEnable(GL_BLEND);
01082             glColor4f(0, 0, 0, ca->passepartalpha);
01083         }
01084         if (x1i > 0.0f)
01085             glRectf(0.0, (float)ar->winy, x1i, 0.0);
01086         if (x2i < (float)ar->winx)
01087             glRectf(x2i, (float)ar->winy, (float)ar->winx, 0.0);
01088         if (y2i < (float)ar->winy)
01089             glRectf(x1i, (float)ar->winy, x2i, y2i);
01090         if (y2i > 0.0f)
01091             glRectf(x1i, y1i, x2i, 0.0);
01092         
01093         glDisable(GL_BLEND);
01094     }
01095 
01096     /* edge */
01097     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);  
01098 
01099     setlinestyle(0);
01100     UI_ThemeColor(TH_BACK);
01101     glRectf(x1i, y1i, x2i, y2i);
01102 
01103 #ifdef VIEW3D_CAMERA_BORDER_HACK
01104     if(view3d_camera_border_hack_test == TRUE) {
01105         glColor4fv(view3d_camera_border_hack_col);
01106         glRectf(x1i+1, y1i+1, x2i-1, y2i-1);
01107         view3d_camera_border_hack_test= FALSE;
01108     }
01109 #endif
01110 
01111     setlinestyle(3);
01112     UI_ThemeColor(TH_WIRE);
01113     glRectf(x1i, y1i, x2i, y2i);
01114 
01115     /* border */
01116     if(scene->r.mode & R_BORDER) {
01117         
01118         cpack(0);
01119         x3= x1+ scene->r.border.xmin*(x2-x1);
01120         y3= y1+ scene->r.border.ymin*(y2-y1);
01121         x4= x1+ scene->r.border.xmax*(x2-x1);
01122         y4= y1+ scene->r.border.ymax*(y2-y1);
01123         
01124         cpack(0x4040FF);
01125         glRectf(x3,  y3,  x4,  y4); 
01126     }
01127 
01128     /* safety border */
01129     if(ca) {
01130         if (ca->dtx & CAM_DTX_CENTER) {
01131             UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
01132 
01133             x3= x1+ 0.5f*(x2-x1);
01134             y3= y1+ 0.5f*(y2-y1);
01135 
01136             glBegin(GL_LINES);
01137             glVertex2f(x1, y3);
01138             glVertex2f(x2, y3);
01139 
01140             glVertex2f(x3, y1);
01141             glVertex2f(x3, y2);
01142             glEnd();
01143         }
01144 
01145         if (ca->dtx & CAM_DTX_CENTER_DIAG) {
01146             UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
01147 
01148             glBegin(GL_LINES);
01149             glVertex2f(x1, y1);
01150             glVertex2f(x2, y2);
01151 
01152             glVertex2f(x1, y2);
01153             glVertex2f(x2, y1);
01154             glEnd();
01155         }
01156 
01157         if (ca->dtx & CAM_DTX_THIRDS) {
01158             UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
01159             drawviewborder_grid3(x1, x2, y1, y2, 1.0f/3.0f);
01160         }
01161 
01162         if (ca->dtx & CAM_DTX_GOLDEN) {
01163             UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
01164             drawviewborder_grid3(x1, x2, y1, y2, 1.0f-(1.0f/1.61803399f));
01165         }
01166 
01167         if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
01168             UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
01169             drawviewborder_triangle(x1, x2, y1, y2, 0, 'A');
01170         }
01171 
01172         if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
01173             UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
01174             drawviewborder_triangle(x1, x2, y1, y2, 0, 'B');
01175         }
01176 
01177         if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
01178             UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
01179             drawviewborder_triangle(x1, x2, y1, y2, 1, 'A');
01180         }
01181 
01182         if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
01183             UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
01184             drawviewborder_triangle(x1, x2, y1, y2, 1, 'B');
01185         }
01186 
01187         if (ca->flag & CAM_SHOWTITLESAFE) {
01188             fac= 0.1;
01189 
01190             a= fac*(x2-x1);
01191             x1+= a;
01192             x2-= a;
01193 
01194             a= fac*(y2-y1);
01195             y1+= a;
01196             y2-= a;
01197 
01198             UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
01199 
01200             uiSetRoundBox(UI_CNR_ALL);
01201             uiDrawBox(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
01202         }
01203         if (ca && (ca->flag & CAM_SHOWSENSOR)) {
01204             /* determine sensor fit, and get sensor x/y, for auto fit we
01205                assume and square sensor and only use sensor_x */
01206             float sizex= scene->r.xsch*scene->r.xasp;
01207             float sizey= scene->r.ysch*scene->r.yasp;
01208             int sensor_fit = camera_sensor_fit(ca->sensor_fit, sizex, sizey);
01209             float sensor_x= ca->sensor_x;
01210             float sensor_y= (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO)? ca->sensor_x: ca->sensor_y;
01211 
01212             /* determine sensor plane */
01213             rctf rect;
01214 
01215             if(sensor_fit == CAMERA_SENSOR_FIT_HOR) {
01216                 float sensor_scale = (x2i-x1i) / sensor_x;
01217                 float sensor_height = sensor_scale * sensor_y;
01218 
01219                 rect.xmin= x1i;
01220                 rect.xmax= x2i;
01221                 rect.ymin= (y1i + y2i)*0.5f - sensor_height*0.5f;
01222                 rect.ymax= rect.ymin + sensor_height;
01223             }
01224             else {
01225                 float sensor_scale = (y2i-y1i) / sensor_y;
01226                 float sensor_width = sensor_scale * sensor_x;
01227 
01228                 rect.xmin= (x1i + x2i)*0.5f - sensor_width*0.5f;
01229                 rect.xmax= rect.xmin + sensor_width;
01230                 rect.ymin= y1i;
01231                 rect.ymax= y2i;
01232             }
01233 
01234             /* draw */
01235             UI_ThemeColorShade(TH_WIRE, 100);
01236             uiDrawBox(GL_LINE_LOOP, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f);
01237         }
01238     }
01239 
01240     setlinestyle(0);
01241     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
01242 
01243     /* camera name - draw in highlighted text color */
01244     if (ca && (ca->flag & CAM_SHOWNAME)) {
01245         UI_ThemeColor(TH_TEXT_HI);
01246         BLF_draw_default(x1i, y1i-15, 0.0f, v3d->camera->id.name+2, sizeof(v3d->camera->id.name)-2);
01247         UI_ThemeColor(TH_WIRE);
01248     }
01249 }
01250 
01251 /* *********************** backdraw for selection *************** */
01252 
01253 static void backdrawview3d(Scene *scene, ARegion *ar, View3D *v3d)
01254 {
01255     RegionView3D *rv3d= ar->regiondata;
01256     struct Base *base = scene->basact;
01257     int multisample_enabled;
01258     rcti winrct;
01259 
01260     BLI_assert(ar->regiontype == RGN_TYPE_WINDOW);
01261 
01262     if(base && (base->object->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT) ||
01263                 paint_facesel_test(base->object)))
01264     {
01265         /* do nothing */
01266     }
01267     else if((base && (base->object->mode & OB_MODE_TEXTURE_PAINT)) &&
01268             scene->toolsettings && (scene->toolsettings->imapaint.flag & IMAGEPAINT_PROJECT_DISABLE))
01269     {
01270         /* do nothing */
01271     }
01272     else if((base && (base->object->mode & OB_MODE_PARTICLE_EDIT)) &&
01273             v3d->drawtype > OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT))
01274     {
01275         /* do nothing */
01276     }
01277     else if(scene->obedit && v3d->drawtype>OB_WIRE &&
01278             (v3d->flag & V3D_ZBUF_SELECT)) {
01279         /* do nothing */
01280     }
01281     else {
01282         v3d->flag &= ~V3D_INVALID_BACKBUF;
01283         return;
01284     }
01285 
01286     if( !(v3d->flag & V3D_INVALID_BACKBUF) ) return;
01287 
01288 //  if(test) {
01289 //      if(qtest()) {
01290 //          addafterqueue(ar->win, BACKBUFDRAW, 1);
01291 //          return;
01292 //      }
01293 //  }
01294 
01295     if(v3d->drawtype > OB_WIRE) v3d->zbuf= TRUE;
01296     
01297     /* dithering and AA break color coding, so disable */
01298     glDisable(GL_DITHER);
01299 
01300     multisample_enabled= glIsEnabled(GL_MULTISAMPLE_ARB);
01301     if(multisample_enabled)
01302         glDisable(GL_MULTISAMPLE_ARB);
01303 
01304     region_scissor_winrct(ar, &winrct);
01305     glScissor(winrct.xmin, winrct.ymin, winrct.xmax - winrct.xmin, winrct.ymax - winrct.ymin);
01306 
01307     glClearColor(0.0, 0.0, 0.0, 0.0); 
01308     if(v3d->zbuf) {
01309         glEnable(GL_DEPTH_TEST);
01310         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
01311     }
01312     else {
01313         glClear(GL_COLOR_BUFFER_BIT);
01314         glDisable(GL_DEPTH_TEST);
01315     }
01316     
01317     if(rv3d->rflag & RV3D_CLIPPING)
01318         view3d_set_clipping(rv3d);
01319     
01320     G.f |= G_BACKBUFSEL;
01321     
01322     if(base && (base->lay & v3d->lay))
01323         draw_object_backbufsel(scene, v3d, rv3d, base->object);
01324 
01325     v3d->flag &= ~V3D_INVALID_BACKBUF;
01326     ar->swap= 0; /* mark invalid backbuf for wm draw */
01327 
01328     G.f &= ~G_BACKBUFSEL;
01329     v3d->zbuf= FALSE; 
01330     glDisable(GL_DEPTH_TEST);
01331     glEnable(GL_DITHER);
01332     if(multisample_enabled)
01333         glEnable(GL_MULTISAMPLE_ARB);
01334 
01335     if(rv3d->rflag & RV3D_CLIPPING)
01336         view3d_clr_clipping();
01337 
01338     /* it is important to end a view in a transform compatible with buttons */
01339 //  persp(PERSP_WIN);  // set ortho
01340 
01341 }
01342 
01343 void view3d_validate_backbuf(ViewContext *vc)
01344 {
01345     if(vc->v3d->flag & V3D_INVALID_BACKBUF)
01346         backdrawview3d(vc->scene, vc->ar, vc->v3d);
01347 }
01348 
01349 /* samples a single pixel (copied from vpaint) */
01350 unsigned int view3d_sample_backbuf(ViewContext *vc, int x, int y)
01351 {
01352     unsigned int col;
01353     
01354     if(x >= vc->ar->winx || y >= vc->ar->winy) return 0;
01355     x+= vc->ar->winrct.xmin;
01356     y+= vc->ar->winrct.ymin;
01357     
01358     view3d_validate_backbuf(vc);
01359 
01360     glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
01361     glReadBuffer(GL_BACK);  
01362     
01363     if(ENDIAN_ORDER==B_ENDIAN) SWITCH_INT(col);
01364     
01365     return WM_framebuffer_to_index(col);
01366 }
01367 
01368 /* reads full rect, converts indices */
01369 ImBuf *view3d_read_backbuf(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
01370 {
01371     unsigned int *dr, *rd;
01372     struct ImBuf *ibuf, *ibuf1;
01373     int a;
01374     short xminc, yminc, xmaxc, ymaxc, xs, ys;
01375     
01376     /* clip */
01377     if(xmin<0) xminc= 0; else xminc= xmin;
01378     if(xmax >= vc->ar->winx) xmaxc= vc->ar->winx-1; else xmaxc= xmax;
01379     if(xminc > xmaxc) return NULL;
01380 
01381     if(ymin<0) yminc= 0; else yminc= ymin;
01382     if(ymax >= vc->ar->winy) ymaxc= vc->ar->winy-1; else ymaxc= ymax;
01383     if(yminc > ymaxc) return NULL;
01384     
01385     ibuf= IMB_allocImBuf((xmaxc-xminc+1), (ymaxc-yminc+1), 32, IB_rect);
01386 
01387     view3d_validate_backbuf(vc); 
01388     
01389     glReadPixels(vc->ar->winrct.xmin+xminc, vc->ar->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
01390     glReadBuffer(GL_BACK);  
01391 
01392     if(ENDIAN_ORDER==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
01393 
01394     a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
01395     dr= ibuf->rect;
01396     while(a--) {
01397         if(*dr) *dr= WM_framebuffer_to_index(*dr);
01398         dr++;
01399     }
01400     
01401     /* put clipped result back, if needed */
01402     if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax) 
01403         return ibuf;
01404     
01405     ibuf1= IMB_allocImBuf( (xmax-xmin+1),(ymax-ymin+1),32,IB_rect);
01406     rd= ibuf->rect;
01407     dr= ibuf1->rect;
01408         
01409     for(ys= ymin; ys<=ymax; ys++) {
01410         for(xs= xmin; xs<=xmax; xs++, dr++) {
01411             if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
01412                 *dr= *rd;
01413                 rd++;
01414             }
01415         }
01416     }
01417     IMB_freeImBuf(ibuf);
01418     return ibuf1;
01419 }
01420 
01421 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
01422 unsigned int view3d_sample_backbuf_rect(ViewContext *vc, const int mval[2], int size,
01423                                         unsigned int min, unsigned int max, int *dist, short strict, 
01424                                         void *handle, unsigned int (*indextest)(void *handle, unsigned int index))
01425 {
01426     struct ImBuf *buf;
01427     unsigned int *bufmin, *bufmax, *tbuf;
01428     int minx, miny;
01429     int a, b, rc, nr, amount, dirvec[4][2];
01430     int distance=0;
01431     unsigned int index = 0;
01432     short indexok = 0;  
01433 
01434     amount= (size-1)/2;
01435 
01436     minx = mval[0]-(amount+1);
01437     miny = mval[1]-(amount+1);
01438     buf = view3d_read_backbuf(vc, minx, miny, minx+size-1, miny+size-1);
01439     if (!buf) return 0;
01440 
01441     rc= 0;
01442     
01443     dirvec[0][0]= 1; dirvec[0][1]= 0;
01444     dirvec[1][0]= 0; dirvec[1][1]= -size;
01445     dirvec[2][0]= -1; dirvec[2][1]= 0;
01446     dirvec[3][0]= 0; dirvec[3][1]= size;
01447     
01448     bufmin = buf->rect;
01449     tbuf = buf->rect;
01450     bufmax = buf->rect + size*size;
01451     tbuf+= amount*size+ amount;
01452     
01453     for(nr=1; nr<=size; nr++) {
01454         
01455         for(a=0; a<2; a++) {
01456             for(b=0; b<nr; b++, distance++) {
01457                 if (*tbuf && *tbuf>=min && *tbuf<max) { //we got a hit
01458                     if(strict){
01459                         indexok =  indextest(handle, *tbuf - min+1);
01460                         if(indexok){
01461                             *dist= (short) sqrt( (float)distance   );
01462                             index = *tbuf - min+1;
01463                             goto exit; 
01464                         }                       
01465                     }
01466                     else{
01467                         *dist= (short) sqrt( (float)distance ); // XXX, this distance is wrong - 
01468                         index = *tbuf - min+1; // messy yah, but indices start at 1
01469                         goto exit;
01470                     }           
01471                 }
01472                 
01473                 tbuf+= (dirvec[rc][0]+dirvec[rc][1]);
01474                 
01475                 if(tbuf<bufmin || tbuf>=bufmax) {
01476                     goto exit;
01477                 }
01478             }
01479             rc++;
01480             rc &= 3;
01481         }
01482     }
01483 
01484 exit:
01485     IMB_freeImBuf(buf);
01486     return index;
01487 }
01488 
01489 
01490 /* ************************************************************* */
01491 
01492 static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
01493 {
01494     RegionView3D *rv3d= ar->regiondata;
01495     BGpic *bgpic;
01496     Image *ima;
01497     MovieClip *clip;
01498     ImBuf *ibuf= NULL, *freeibuf;
01499     float vec[4], fac, asp, zoomx, zoomy;
01500     float x1, y1, x2, y2, cx, cy;
01501 
01502 
01503     for ( bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next ) {
01504 
01505         if( (bgpic->view == 0) || /* zero for any */
01506             (bgpic->view & (1<<rv3d->view)) || /* check agaist flags */
01507             (rv3d->persp==RV3D_CAMOB && bgpic->view == (1<<RV3D_VIEW_CAMERA))
01508         ) {
01509             /* disable individual images */
01510             if((bgpic->flag&V3D_BGPIC_DISABLED))
01511                 continue;
01512 
01513             freeibuf= NULL;
01514             if(bgpic->source==V3D_BGPIC_IMAGE) {
01515                 ima= bgpic->ima;
01516                 if(ima==NULL)
01517                     continue;
01518                 BKE_image_user_calc_frame(&bgpic->iuser, CFRA, 0);
01519                 ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
01520             } else {
01521                 clip= NULL;
01522 
01523                 if(bgpic->flag&V3D_BGPIC_CAMERACLIP) {
01524                     if(scene->camera)
01525                         clip= object_get_movieclip(scene, scene->camera, 1);
01526                 } else clip= bgpic->clip;
01527 
01528                 if(clip==NULL)
01529                     continue;
01530 
01531                 BKE_movieclip_user_set_frame(&bgpic->cuser, CFRA);
01532                 ibuf= BKE_movieclip_get_ibuf(clip, &bgpic->cuser);
01533 
01534                 /* working with ibuf from image and clip has got different workflow now.
01535                    ibuf acquired from clip is referenced by cache system and should
01536                    be dereferenced after usage. */
01537                 freeibuf= ibuf;
01538             }
01539 
01540             if(ibuf==NULL)
01541                 continue;
01542 
01543             if((ibuf->rect==NULL && ibuf->rect_float==NULL) || ibuf->channels!=4) { /* invalid image format */
01544                 if(freeibuf)
01545                     IMB_freeImBuf(freeibuf);
01546 
01547                 continue;
01548             }
01549 
01550             if(ibuf->rect==NULL)
01551                 IMB_rect_from_float(ibuf);
01552 
01553             if(rv3d->persp==RV3D_CAMOB) {
01554                 rctf vb;
01555 
01556                 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &vb, FALSE);
01557 
01558                 x1= vb.xmin;
01559                 y1= vb.ymin;
01560                 x2= vb.xmax;
01561                 y2= vb.ymax;
01562             }
01563             else {
01564                 float sco[2];
01565                 const float mval_f[2]= {1.0f, 0.0f};
01566 
01567                 /* calc window coord */
01568                 initgrabz(rv3d, 0.0, 0.0, 0.0);
01569                 ED_view3d_win_to_delta(ar, mval_f, vec);
01570                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
01571                 fac= 1.0f/fac;
01572 
01573                 asp= ( (float)ibuf->y)/(float)ibuf->x;
01574 
01575                 vec[0] = vec[1] = vec[2] = 0.0;
01576                 ED_view3d_project_float(ar, vec, sco, rv3d->persmat);
01577                 cx = sco[0];
01578                 cy = sco[1];
01579 
01580                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
01581                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
01582                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
01583                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
01584             }
01585 
01586             /* complete clip? */
01587 
01588             if(x2 < 0 || y2 < 0 || x1 > ar->winx || y1 > ar->winy) {
01589                 if(freeibuf)
01590                     IMB_freeImBuf(freeibuf);
01591 
01592                 continue;
01593             }
01594 
01595             zoomx= (x2-x1)/ibuf->x;
01596             zoomy= (y2-y1)/ibuf->y;
01597 
01598             /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
01599             if(zoomx < 1.0f || zoomy < 1.0f) {
01600                 float tzoom= MIN2(zoomx, zoomy);
01601                 int mip= 0;
01602 
01603                 if((ibuf->userflags&IB_MIPMAP_INVALID) != 0) {
01604                     IMB_remakemipmap(ibuf, 0);
01605                     ibuf->userflags&= ~IB_MIPMAP_INVALID;
01606                 }
01607                 else if(ibuf->mipmap[0]==NULL)
01608                     IMB_makemipmap(ibuf, 0);
01609 
01610                 while(tzoom < 1.0f && mip<8 && ibuf->mipmap[mip]) {
01611                     tzoom*= 2.0f;
01612                     zoomx*= 2.0f;
01613                     zoomy*= 2.0f;
01614                     mip++;
01615                 }
01616                 if(mip>0)
01617                     ibuf= ibuf->mipmap[mip-1];
01618             }
01619 
01620             if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
01621             glDepthMask(0);
01622 
01623             glEnable(GL_BLEND);
01624             glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
01625 
01626             glMatrixMode(GL_PROJECTION);
01627             glPushMatrix();
01628             glMatrixMode(GL_MODELVIEW);
01629             glPushMatrix();
01630             ED_region_pixelspace(ar);
01631 
01632             glPixelZoom(zoomx, zoomy);
01633             glColor4f(1.0f, 1.0f, 1.0f, 1.0f-bgpic->blend);
01634             glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
01635 
01636             glPixelZoom(1.0, 1.0);
01637             glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
01638 
01639             glMatrixMode(GL_PROJECTION);
01640             glPopMatrix();
01641             glMatrixMode(GL_MODELVIEW);
01642             glPopMatrix();
01643 
01644             glDisable(GL_BLEND);
01645 
01646             glDepthMask(1);
01647             if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
01648 
01649             if(freeibuf)
01650                 IMB_freeImBuf(freeibuf);
01651         }
01652     }
01653 }
01654 
01655 /* ****************** View3d afterdraw *************** */
01656 
01657 typedef struct View3DAfter {
01658     struct View3DAfter *next, *prev;
01659     struct Base *base;
01660     int flag;
01661 } View3DAfter;
01662 
01663 /* temp storage of Objects that need to be drawn as last */
01664 void add_view3d_after(ListBase *lb, Base *base, int flag)
01665 {
01666     View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
01667     BLI_addtail(lb, v3da);
01668     v3da->base= base;
01669     v3da->flag= flag;
01670 }
01671 
01672 /* disables write in zbuffer and draws it over */
01673 static void view3d_draw_transp(Scene *scene, ARegion *ar, View3D *v3d)
01674 {
01675     View3DAfter *v3da, *next;
01676     
01677     glDepthMask(0);
01678     v3d->transp= TRUE;
01679     
01680     for(v3da= v3d->afterdraw_transp.first; v3da; v3da= next) {
01681         next= v3da->next;
01682         draw_object(scene, ar, v3d, v3da->base, v3da->flag);
01683         BLI_remlink(&v3d->afterdraw_transp, v3da);
01684         MEM_freeN(v3da);
01685     }
01686     v3d->transp= FALSE;
01687     
01688     glDepthMask(1);
01689     
01690 }
01691 
01692 /* clears zbuffer and draws it over */
01693 static void view3d_draw_xray(Scene *scene, ARegion *ar, View3D *v3d, int clear)
01694 {
01695     View3DAfter *v3da, *next;
01696 
01697     if(clear && v3d->zbuf)
01698         glClear(GL_DEPTH_BUFFER_BIT);
01699 
01700     v3d->xray= TRUE;
01701     for(v3da= v3d->afterdraw_xray.first; v3da; v3da= next) {
01702         next= v3da->next;
01703         draw_object(scene, ar, v3d, v3da->base, v3da->flag);
01704         BLI_remlink(&v3d->afterdraw_xray, v3da);
01705         MEM_freeN(v3da);
01706     }
01707     v3d->xray= FALSE;
01708 }
01709 
01710 
01711 /* clears zbuffer and draws it over */
01712 static void view3d_draw_xraytransp(Scene *scene, ARegion *ar, View3D *v3d, int clear)
01713 {
01714     View3DAfter *v3da, *next;
01715 
01716     if(clear && v3d->zbuf)
01717         glClear(GL_DEPTH_BUFFER_BIT);
01718 
01719     v3d->xray= TRUE;
01720     v3d->transp= TRUE;
01721     
01722     for(v3da= v3d->afterdraw_xraytransp.first; v3da; v3da= next) {
01723         next= v3da->next;
01724         draw_object(scene, ar, v3d, v3da->base, v3da->flag);
01725         BLI_remlink(&v3d->afterdraw_xraytransp, v3da);
01726         MEM_freeN(v3da);
01727     }
01728 
01729     v3d->transp= FALSE;
01730     v3d->xray= FALSE;
01731 
01732 }
01733 
01734 /* *********************** */
01735 
01736 /*
01737     In most cases call draw_dupli_objects,
01738     draw_dupli_objects_color was added because when drawing set dupli's
01739     we need to force the color
01740  */
01741 
01742 #if 0
01743 int dupli_ob_sort(void *arg1, void *arg2)
01744 {
01745     void *p1= ((DupliObject *)arg1)->ob;
01746     void *p2= ((DupliObject *)arg2)->ob;
01747     int val = 0;
01748     if (p1 < p2)        val = -1;
01749     else if (p1 > p2)   val = 1;
01750     return val;
01751 }
01752 #endif
01753 
01754 
01755 static DupliObject *dupli_step(DupliObject *dob)
01756 {
01757     while(dob && dob->no_draw)
01758         dob= dob->next;
01759     return dob;
01760 }
01761 
01762 static void draw_dupli_objects_color(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int color)
01763 {
01764     RegionView3D *rv3d= ar->regiondata;
01765     ListBase *lb;
01766     DupliObject *dob_prev= NULL, *dob, *dob_next= NULL;
01767     Base tbase;
01768     BoundBox bb, *bb_tmp; /* use a copy because draw_object, calls clear_mesh_caches */
01769     GLuint displist=0;
01770     short transflag, use_displist= -1;  /* -1 is initialize */
01771     char dt, dtx;
01772     
01773     if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
01774     
01775     tbase.flag= OB_FROMDUPLI|base->flag;
01776     lb= object_duplilist(scene, base->object);
01777     // BLI_sortlist(lb, dupli_ob_sort); // might be nice to have if we have a dupli list with mixed objects.
01778 
01779     dob=dupli_step(lb->first);
01780     if(dob) dob_next= dupli_step(dob->next);
01781 
01782     for( ; dob ; dob_prev= dob, dob= dob_next, dob_next= dob_next ? dupli_step(dob_next->next) : NULL) {
01783         tbase.object= dob->ob;
01784 
01785         /* extra service: draw the duplicator in drawtype of parent */
01786         /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
01787         dt= tbase.object->dt;   tbase.object->dt= MIN2(tbase.object->dt, base->object->dt);
01788         dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
01789 
01790         /* negative scale flag has to propagate */
01791         transflag= tbase.object->transflag;
01792         if(base->object->transflag & OB_NEG_SCALE)
01793             tbase.object->transflag ^= OB_NEG_SCALE;
01794 
01795         UI_ThemeColorBlend(color, TH_BACK, 0.5);
01796 
01797         /* generate displist, test for new object */
01798         if(dob_prev && dob_prev->ob != dob->ob) {
01799             if(use_displist==1)
01800                 glDeleteLists(displist, 1);
01801 
01802             use_displist= -1;
01803         }
01804 
01805         /* generate displist */
01806         if(use_displist == -1) {
01807 
01808             /* note, since this was added, its checked dob->type==OB_DUPLIGROUP
01809              * however this is very slow, it was probably needed for the NLA
01810              * offset feature (used in group-duplicate.blend but no longer works in 2.5)
01811              * so for now it should be ok to - campbell */
01812 
01813             if(     (dob_next==NULL || dob_next->ob != dob->ob) || /* if this is the last no need  to make a displist */
01814                     (dob->ob->type == OB_LAMP) || /* lamp drawing messes with matrices, could be handled smarter... but this works */
01815                     (dob->type == OB_DUPLIGROUP && dob->animated) ||
01816                     !(bb_tmp= object_get_boundbox(dob->ob))
01817             ) {
01818                 // printf("draw_dupli_objects_color: skipping displist for %s\n", dob->ob->id.name+2);
01819                 use_displist= 0;
01820             }
01821             else {
01822                 // printf("draw_dupli_objects_color: using displist for %s\n", dob->ob->id.name+2);
01823                 bb= *bb_tmp; /* must make a copy  */
01824 
01825                 /* disable boundbox check for list creation */
01826                 object_boundbox_flag(dob->ob, OB_BB_DISABLED, 1);
01827                 /* need this for next part of code */
01828                 unit_m4(dob->ob->obmat);    /* obmat gets restored */
01829 
01830                 displist= glGenLists(1);
01831                 glNewList(displist, GL_COMPILE);
01832                 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
01833                 glEndList();
01834 
01835                 use_displist= 1;
01836                 object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
01837             }
01838         }
01839         if(use_displist) {
01840             glMultMatrixf(dob->mat);
01841             if(ED_view3d_boundbox_clip(rv3d, dob->mat, &bb))
01842                 glCallList(displist);
01843             glLoadMatrixf(rv3d->viewmat);
01844         }
01845         else {
01846             copy_m4_m4(dob->ob->obmat, dob->mat);
01847             draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
01848         }
01849 
01850         tbase.object->dt= dt;
01851         tbase.object->dtx= dtx;
01852         tbase.object->transflag= transflag;
01853     }
01854     
01855     /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
01856     
01857     free_object_duplilist(lb);  /* does restore */
01858     
01859     if(use_displist)
01860         glDeleteLists(displist, 1);
01861 }
01862 
01863 static void draw_dupli_objects(Scene *scene, ARegion *ar, View3D *v3d, Base *base)
01864 {
01865     /* define the color here so draw_dupli_objects_color can be called
01866     * from the set loop */
01867     
01868     int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
01869     /* debug */
01870     if(base->object->dup_group && base->object->dup_group->id.us<1)
01871         color= TH_REDALERT;
01872     
01873     draw_dupli_objects_color(scene, ar, v3d, base, color);
01874 }
01875 
01876 void view3d_update_depths_rect(ARegion *ar, ViewDepths *d, rcti *rect)
01877 {
01878     int x, y, w, h; 
01879     rcti r;
01880     /* clamp rect by area */
01881 
01882     r.xmin= 0;
01883     r.xmax= ar->winx-1;
01884     r.ymin= 0;
01885     r.ymax= ar->winy-1;
01886 
01887     /* Constrain rect to depth bounds */
01888     BLI_isect_rcti(&r, rect, rect);
01889 
01890     /* assign values to compare with the ViewDepths */
01891     x= rect->xmin;
01892     y= rect->ymin;
01893 
01894     w= rect->xmax - rect->xmin;
01895     h= rect->ymax - rect->ymin;
01896 
01897     if(w <= 0 || h <= 0) {
01898         if(d->depths)
01899             MEM_freeN(d->depths);
01900         d->depths= NULL;
01901 
01902         d->damaged= FALSE;
01903     }
01904     else if(    d->w != w ||
01905         d->h != h ||
01906         d->x != x ||
01907         d->y != y ||
01908         d->depths==NULL
01909     ) {
01910         d->x= x;
01911         d->y= y;
01912         d->w= w;
01913         d->h= h;
01914 
01915         if(d->depths)
01916             MEM_freeN(d->depths);
01917 
01918         d->depths= MEM_mallocN(sizeof(float)*d->w*d->h,"View depths Subset");
01919         
01920         d->damaged= TRUE;
01921     }
01922 
01923     if(d->damaged) {
01924         glReadPixels(ar->winrct.xmin+d->x,ar->winrct.ymin+d->y, d->w,d->h, GL_DEPTH_COMPONENT,GL_FLOAT, d->depths);
01925         glGetDoublev(GL_DEPTH_RANGE,d->depth_range);
01926         d->damaged= FALSE;
01927     }
01928 }
01929 
01930 /* note, with nouveau drivers the glReadPixels() is very slow. [#24339] */
01931 void ED_view3d_depth_update(ARegion *ar)
01932 {
01933     RegionView3D *rv3d= ar->regiondata;
01934     
01935     /* Create storage for, and, if necessary, copy depth buffer */
01936     if(!rv3d->depths) rv3d->depths= MEM_callocN(sizeof(ViewDepths),"ViewDepths");
01937     if(rv3d->depths) {
01938         ViewDepths *d= rv3d->depths;
01939         if(d->w != ar->winx ||
01940            d->h != ar->winy ||
01941            !d->depths) {
01942             d->w= ar->winx;
01943             d->h= ar->winy;
01944             if(d->depths)
01945                 MEM_freeN(d->depths);
01946             d->depths= MEM_mallocN(sizeof(float)*d->w*d->h,"View depths");
01947             d->damaged= 1;
01948         }
01949         
01950         if(d->damaged) {
01951             glReadPixels(ar->winrct.xmin,ar->winrct.ymin,d->w,d->h,
01952                          GL_DEPTH_COMPONENT,GL_FLOAT, d->depths);
01953             
01954             glGetDoublev(GL_DEPTH_RANGE,d->depth_range);
01955             
01956             d->damaged= 0;
01957         }
01958     }
01959 }
01960 
01961 /* utility function to find the closest Z value, use for autodepth */
01962 float view3d_depth_near(ViewDepths *d)
01963 {
01964     /* convert to float for comparisons */
01965     const float near= (float)d->depth_range[0];
01966     const float far_real= (float)d->depth_range[1];
01967     float far= far_real;
01968 
01969     const float *depths= d->depths;
01970     float depth= FLT_MAX;
01971     int i= (int)d->w * (int)d->h; /* cast to avoid short overflow */
01972 
01973     /* far is both the starting 'far' value
01974      * and the closest value found. */  
01975     while(i--) {
01976         depth= *depths++;
01977         if((depth < far) && (depth > near)) {
01978             far= depth;
01979         }
01980     }
01981 
01982     return far == far_real ? FLT_MAX : far;
01983 }
01984 
01985 void draw_depth_gpencil(Scene *scene, ARegion *ar, View3D *v3d)
01986 {
01987     short zbuf= v3d->zbuf;
01988     RegionView3D *rv3d= ar->regiondata;
01989 
01990     setwinmatrixview3d(ar, v3d, NULL);  /* 0= no pick rect */
01991     setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls where_is_object for camera... */
01992 
01993     mult_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
01994     invert_m4_m4(rv3d->persinv, rv3d->persmat);
01995     invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
01996 
01997     glClear(GL_DEPTH_BUFFER_BIT);
01998 
01999     glLoadMatrixf(rv3d->viewmat);
02000 
02001     v3d->zbuf= TRUE;
02002     glEnable(GL_DEPTH_TEST);
02003 
02004     draw_gpencil_view3d(scene, v3d, ar, 1);
02005     
02006     v3d->zbuf= zbuf;
02007 
02008 }
02009 
02010 void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (* func)(void *))
02011 {
02012     RegionView3D *rv3d= ar->regiondata;
02013     Base *base;
02014     short zbuf= v3d->zbuf;
02015     short flag= v3d->flag;
02016     float glalphaclip= U.glalphaclip;
02017     int obcenter_dia= U.obcenter_dia;
02018     /* temp set drawtype to solid */
02019     
02020     /* Setting these temporarily is not nice */
02021     v3d->flag &= ~V3D_SELECT_OUTLINE;
02022     U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
02023     U.obcenter_dia= 0;
02024     
02025     setwinmatrixview3d(ar, v3d, NULL);  /* 0= no pick rect */
02026     setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls where_is_object for camera... */
02027     
02028     mult_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
02029     invert_m4_m4(rv3d->persinv, rv3d->persmat);
02030     invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
02031     
02032     glClear(GL_DEPTH_BUFFER_BIT);
02033     
02034     glLoadMatrixf(rv3d->viewmat);
02035 //  persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
02036     
02037     if(rv3d->rflag & RV3D_CLIPPING) {
02038         view3d_set_clipping(rv3d);
02039     }
02040     
02041     v3d->zbuf= TRUE;
02042     glEnable(GL_DEPTH_TEST);
02043     
02044     /* draw set first */
02045     if(scene->set) {
02046         Scene *sce_iter;
02047         for(SETLOOPER(scene->set, sce_iter, base)) {
02048             if(v3d->lay & base->lay) {
02049                 if (func == NULL || func(base)) {
02050                     draw_object(scene, ar, v3d, base, 0);
02051                     if(base->object->transflag & OB_DUPLI) {
02052                         draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
02053                     }
02054                 }
02055             }
02056         }
02057     }
02058     
02059     for(base= scene->base.first; base; base= base->next) {
02060         if(v3d->lay & base->lay) {
02061             if (func == NULL || func(base)) {
02062                 /* dupli drawing */
02063                 if(base->object->transflag & OB_DUPLI) {
02064                     draw_dupli_objects(scene, ar, v3d, base);
02065                 }
02066                 draw_object(scene, ar, v3d, base, 0);
02067             }
02068         }
02069     }
02070     
02071     /* this isnt that nice, draw xray objects as if they are normal */
02072     if (    v3d->afterdraw_transp.first ||
02073             v3d->afterdraw_xray.first || 
02074             v3d->afterdraw_xraytransp.first
02075     ) {
02076         View3DAfter *v3da, *next;
02077         int mask_orig;
02078 
02079         v3d->xray= TRUE;
02080         
02081         /* transp materials can change the depth mask, see #21388 */
02082         glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig);
02083 
02084 
02085         if(v3d->afterdraw_xray.first || v3d->afterdraw_xraytransp.first) {
02086             glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
02087             for(v3da= v3d->afterdraw_xray.first; v3da; v3da= next) {
02088                 next= v3da->next;
02089                 draw_object(scene, ar, v3d, v3da->base, 0);
02090             }
02091             glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
02092         }
02093 
02094         /* draw 3 passes, transp/xray/xraytransp */
02095         v3d->xray= FALSE;
02096         v3d->transp= TRUE;
02097         for(v3da= v3d->afterdraw_transp.first; v3da; v3da= next) {
02098             next= v3da->next;
02099             draw_object(scene, ar, v3d, v3da->base, 0);
02100             BLI_remlink(&v3d->afterdraw_transp, v3da);
02101             MEM_freeN(v3da);
02102         }
02103 
02104         v3d->xray= TRUE;
02105         v3d->transp= FALSE;  
02106         for(v3da= v3d->afterdraw_xray.first; v3da; v3da= next) {
02107             next= v3da->next;
02108             draw_object(scene, ar, v3d, v3da->base, 0);
02109             BLI_remlink(&v3d->afterdraw_xray, v3da);
02110             MEM_freeN(v3da);
02111         }
02112 
02113         v3d->xray= TRUE;
02114         v3d->transp= TRUE;
02115         for(v3da= v3d->afterdraw_xraytransp.first; v3da; v3da= next) {
02116             next= v3da->next;
02117             draw_object(scene, ar, v3d, v3da->base, 0);
02118             BLI_remlink(&v3d->afterdraw_xraytransp, v3da);
02119             MEM_freeN(v3da);
02120         }
02121 
02122         
02123         v3d->xray= FALSE;
02124         v3d->transp= FALSE;
02125 
02126         glDepthMask(mask_orig);
02127     }
02128     
02129     if(rv3d->rflag & RV3D_CLIPPING)
02130         view3d_clr_clipping();
02131     
02132     v3d->zbuf = zbuf;
02133     if(!v3d->zbuf) glDisable(GL_DEPTH_TEST);
02134 
02135     U.glalphaclip = glalphaclip;
02136     v3d->flag = flag;
02137     U.obcenter_dia= obcenter_dia;
02138 }
02139 
02140 typedef struct View3DShadow {
02141     struct View3DShadow *next, *prev;
02142     GPULamp *lamp;
02143 } View3DShadow;
02144 
02145 static void gpu_render_lamp_update(Scene *scene, View3D *v3d, Object *ob, Object *par, float obmat[][4], ListBase *shadows)
02146 {
02147     GPULamp *lamp;
02148     Lamp *la = (Lamp*)ob->data;
02149     View3DShadow *shadow;
02150     
02151     lamp = GPU_lamp_from_blender(scene, ob, par);
02152     
02153     if(lamp) {
02154         GPU_lamp_update(lamp, ob->lay, (ob->restrictflag & OB_RESTRICT_RENDER), obmat);
02155         GPU_lamp_update_colors(lamp, la->r, la->g, la->b, la->energy);
02156         
02157         if((ob->lay & v3d->lay) && GPU_lamp_has_shadow_buffer(lamp)) {
02158             shadow= MEM_callocN(sizeof(View3DShadow), "View3DShadow");
02159             shadow->lamp = lamp;
02160             BLI_addtail(shadows, shadow);
02161         }
02162     }
02163 }
02164 
02165 static void gpu_update_lamps_shadows(Scene *scene, View3D *v3d)
02166 {
02167     ListBase shadows;
02168     View3DShadow *shadow;
02169     Scene *sce_iter;
02170     Base *base;
02171     Object *ob;
02172     
02173     shadows.first= shadows.last= NULL;
02174     
02175     /* update lamp transform and gather shadow lamps */
02176     for(SETLOOPER(scene, sce_iter, base)) {
02177         ob= base->object;
02178         
02179         if(ob->type == OB_LAMP)
02180             gpu_render_lamp_update(scene, v3d, ob, NULL, ob->obmat, &shadows);
02181         
02182         if (ob->transflag & OB_DUPLI) {
02183             DupliObject *dob;
02184             ListBase *lb = object_duplilist(scene, ob);
02185             
02186             for(dob=lb->first; dob; dob=dob->next)
02187                 if(dob->ob->type==OB_LAMP)
02188                     gpu_render_lamp_update(scene, v3d, dob->ob, ob, dob->mat, &shadows);
02189             
02190             free_object_duplilist(lb);
02191         }
02192     }
02193     
02194     /* render shadows after updating all lamps, nested object_duplilist
02195         * don't work correct since it's replacing object matrices */
02196     for(shadow=shadows.first; shadow; shadow=shadow->next) {
02197         /* this needs to be done better .. */
02198         float viewmat[4][4], winmat[4][4];
02199         int drawtype, lay, winsize, flag2=v3d->flag2;
02200         ARegion ar= {NULL};
02201         RegionView3D rv3d= {{{0}}};
02202         
02203         drawtype= v3d->drawtype;
02204         lay= v3d->lay;
02205         
02206         v3d->drawtype = OB_SOLID;
02207         v3d->lay &= GPU_lamp_shadow_layer(shadow->lamp);
02208         v3d->flag2 &= ~V3D_SOLID_TEX;
02209         v3d->flag2 |= V3D_RENDER_OVERRIDE | V3D_RENDER_SHADOW;
02210         
02211         GPU_lamp_shadow_buffer_bind(shadow->lamp, viewmat, &winsize, winmat);
02212 
02213         ar.regiondata= &rv3d;
02214         ar.regiontype= RGN_TYPE_WINDOW;
02215         rv3d.persp= RV3D_CAMOB;
02216         copy_m4_m4(rv3d.winmat, winmat);
02217         copy_m4_m4(rv3d.viewmat, viewmat);
02218         invert_m4_m4(rv3d.viewinv, rv3d.viewmat);
02219         mult_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
02220         invert_m4_m4(rv3d.persinv, rv3d.viewinv);
02221 
02222         ED_view3d_draw_offscreen(scene, v3d, &ar, winsize, winsize, viewmat, winmat);
02223         GPU_lamp_shadow_buffer_unbind(shadow->lamp);
02224         
02225         v3d->drawtype= drawtype;
02226         v3d->lay= lay;
02227         v3d->flag2 = flag2;
02228     }
02229     
02230     BLI_freelistN(&shadows);
02231 }
02232 
02233 /* *********************** customdata **************** */
02234 
02235 CustomDataMask ED_view3d_datamask(Scene *scene, View3D *v3d)
02236 {
02237     CustomDataMask mask= 0;
02238 
02239     if(ELEM(v3d->drawtype, OB_TEXTURE, OB_MATERIAL) || ((v3d->drawtype == OB_SOLID) && (v3d->flag2 & V3D_SOLID_TEX))) {
02240         mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
02241 
02242         if(scene_use_new_shading_nodes(scene)) {
02243             if(v3d->drawtype == OB_MATERIAL)
02244                 mask |= CD_MASK_ORCO;
02245         }
02246         else {
02247             if(scene->gm.matmode == GAME_MAT_GLSL)
02248                 mask |= CD_MASK_ORCO;
02249         }
02250     }
02251 
02252     return mask;
02253 }
02254 
02255 CustomDataMask ED_view3d_object_datamask(Scene *scene)
02256 {
02257     Object *ob= scene->basact ? scene->basact->object : NULL;
02258     CustomDataMask mask= 0;
02259 
02260     if (ob) {
02261         /* check if we need tfaces & mcols due to face select or texture paint */
02262         if (paint_facesel_test(ob) || (ob->mode & OB_MODE_TEXTURE_PAINT)) {
02263             mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
02264         }
02265 
02266         /* check if we need mcols due to vertex paint or weightpaint */
02267         if (ob->mode & OB_MODE_VERTEX_PAINT) {
02268             mask |= CD_MASK_MCOL;
02269         }
02270 
02271         if (ob->mode & OB_MODE_WEIGHT_PAINT) {
02272             mask |= CD_MASK_WEIGHT_MCOL;
02273         }
02274     }
02275 
02276     return mask;
02277 }
02278 
02279 /* goes over all modes and view3d settings */
02280 CustomDataMask ED_view3d_screen_datamask(bScreen *screen)
02281 {
02282     Scene *scene= screen->scene;
02283     CustomDataMask mask = CD_MASK_BAREMESH;
02284     ScrArea *sa;
02285     
02286     /* check if we need tfaces & mcols due to view mode */
02287     for(sa = screen->areabase.first; sa; sa = sa->next) {
02288         if(sa->spacetype == SPACE_VIEW3D) {
02289             mask |= ED_view3d_datamask(scene, (View3D *)sa->spacedata.first);
02290         }
02291     }
02292 
02293     mask |= ED_view3d_object_datamask(scene);
02294 
02295     return mask;
02296 }
02297 
02298 static void view3d_main_area_setup_view(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[][4], float winmat[][4])
02299 {
02300     RegionView3D *rv3d= ar->regiondata;
02301 
02302     /* setup window matrices */
02303     if(winmat)
02304         copy_m4_m4(rv3d->winmat, winmat);
02305     else
02306         setwinmatrixview3d(ar, v3d, NULL); /* NULL= no pickrect */
02307     
02308     /* setup view matrix */
02309     if(viewmat)
02310         copy_m4_m4(rv3d->viewmat, viewmat);
02311     else
02312         setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls where_is_object for camera... */
02313     
02314     /* update utilitity matrices */
02315     mult_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
02316     invert_m4_m4(rv3d->persinv, rv3d->persmat);
02317     invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
02318 
02319     /* calculate pixelsize factor once, is used for lamps and obcenters */
02320     {
02321         /* note:  '1.0f / len_v3(v1)'  replaced  'len_v3(rv3d->viewmat[0])'
02322          * because of float point precision problems at large values [#23908] */
02323         float v1[3], v2[3];
02324         float len1, len2;
02325 
02326         v1[0]= rv3d->persmat[0][0];
02327         v1[1]= rv3d->persmat[1][0];
02328         v1[2]= rv3d->persmat[2][0];
02329 
02330         v2[0]= rv3d->persmat[0][1];
02331         v2[1]= rv3d->persmat[1][1];
02332         v2[2]= rv3d->persmat[2][1];
02333         
02334         len1= 1.0f / len_v3(v1);
02335         len2= 1.0f / len_v3(v2);
02336 
02337         rv3d->pixsize = (2.0f * MAX2(len1, len2)) / (float)MAX2(ar->winx, ar->winy);
02338     }
02339 
02340     /* set for opengl */
02341     glMatrixMode(GL_PROJECTION);
02342     glLoadMatrixf(rv3d->winmat);
02343     glMatrixMode(GL_MODELVIEW);
02344     glLoadMatrixf(rv3d->viewmat);
02345 }
02346 
02347 void ED_view3d_draw_offscreen(Scene *scene, View3D *v3d, ARegion *ar, int winx, int winy, float viewmat[][4], float winmat[][4])
02348 {
02349     RegionView3D *rv3d= ar->regiondata;
02350     Base *base;
02351     float backcol[3];
02352     int bwinx, bwiny;
02353     rcti brect;
02354 
02355     glPushMatrix();
02356 
02357     /* set temporary new size */
02358     bwinx= ar->winx;
02359     bwiny= ar->winy;
02360     brect= ar->winrct;
02361     
02362     ar->winx= winx;
02363     ar->winy= winy; 
02364     ar->winrct.xmin= 0;
02365     ar->winrct.ymin= 0;
02366     ar->winrct.xmax= winx;
02367     ar->winrct.ymax= winy;
02368     
02369     
02370     /* set flags */
02371     G.f |= G_RENDER_OGL;
02372 
02373     /* free images which can have changed on frame-change
02374      * warning! can be slow so only free animated images - campbell */
02375     GPU_free_images_anim();
02376     
02377     /* shadow buffers, before we setup matrices */
02378     if(draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
02379         gpu_update_lamps_shadows(scene, v3d);
02380 
02381     /* set background color, fallback on the view background color */
02382     if(scene->world) {
02383         if(scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
02384             linearrgb_to_srgb_v3_v3(backcol, &scene->world->horr);
02385         else
02386             copy_v3_v3(backcol, &scene->world->horr);
02387         glClearColor(backcol[0], backcol[1], backcol[2], 0.0);
02388     }
02389     else {
02390         UI_ThemeClearColor(TH_BACK);    
02391     }
02392 
02393     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
02394 
02395     /* setup view matrices */
02396     view3d_main_area_setup_view(scene, v3d, ar, viewmat, winmat);
02397 
02398     if(rv3d->rflag & RV3D_CLIPPING)
02399         view3d_draw_clipping(rv3d);
02400 
02401     /* set zbuffer */
02402     if(v3d->drawtype > OB_WIRE) {
02403         v3d->zbuf= TRUE;
02404         glEnable(GL_DEPTH_TEST);
02405     }
02406     else
02407         v3d->zbuf= FALSE;
02408 
02409     if(rv3d->rflag & RV3D_CLIPPING)
02410         view3d_set_clipping(rv3d);
02411 
02412     /* draw set first */
02413     if(scene->set) {
02414         Scene *sce_iter;
02415         for(SETLOOPER(scene->set, sce_iter, base)) {
02416             if(v3d->lay & base->lay) {
02417                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
02418                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
02419                 
02420                 if(base->object->transflag & OB_DUPLI)
02421                     draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
02422             }
02423         }
02424     }
02425     
02426     /* then draw not selected and the duplis, but skip editmode object */
02427     for(base= scene->base.first; base; base= base->next) {
02428         if(v3d->lay & base->lay) {
02429             /* dupli drawing */
02430             if(base->object->transflag & OB_DUPLI)
02431                 draw_dupli_objects(scene, ar, v3d, base);
02432 
02433             draw_object(scene, ar, v3d, base, 0);
02434         }
02435     }
02436 
02437     /* must be before xray draw which clears the depth buffer */
02438     if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
02439     draw_gpencil_view3d(scene, v3d, ar, 1);
02440     if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
02441 
02442     /* transp and X-ray afterdraw stuff */
02443     if(v3d->afterdraw_transp.first)     view3d_draw_transp(scene, ar, v3d);
02444     if(v3d->afterdraw_xray.first)       view3d_draw_xray(scene, ar, v3d, 1);    // clears zbuffer if it is used!
02445     if(v3d->afterdraw_xraytransp.first) view3d_draw_xraytransp(scene, ar, v3d, 1);
02446 
02447     if(rv3d->rflag & RV3D_CLIPPING)
02448         view3d_clr_clipping();
02449 
02450     /* cleanup */
02451     if(v3d->zbuf) {
02452         v3d->zbuf= FALSE;
02453         glDisable(GL_DEPTH_TEST);
02454     }
02455 
02456     /* draw grease-pencil stuff */
02457     ED_region_pixelspace(ar);
02458 
02459     /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
02460     draw_gpencil_view3d(scene, v3d, ar, 0);
02461 
02462     /* freeing the images again here could be done after the operator runs, leaving for now */
02463     GPU_free_images_anim();
02464 
02465     /* restore size */
02466     ar->winx= bwinx;
02467     ar->winy= bwiny;
02468     ar->winrct = brect;
02469 
02470     glPopMatrix();
02471 
02472     glColor4ub(255, 255, 255, 255); // XXX, without this the sequencer flickers with opengl draw enabled, need to find out why - campbell
02473 
02474     G.f &= ~G_RENDER_OGL;
02475 }
02476 
02477 /* utility func for ED_view3d_draw_offscreen */
02478 ImBuf *ED_view3d_draw_offscreen_imbuf(Scene *scene, View3D *v3d, ARegion *ar, int sizex, int sizey, unsigned int flag, char err_out[256])
02479 {
02480     RegionView3D *rv3d= ar->regiondata;
02481     ImBuf *ibuf;
02482     GPUOffScreen *ofs;
02483     
02484     /* state changes make normal drawing go weird otherwise */
02485     glPushAttrib(GL_LIGHTING_BIT);
02486 
02487     /* bind */
02488     ofs= GPU_offscreen_create(sizex, sizey, err_out);
02489     if(ofs == NULL)
02490         return NULL;
02491 
02492     GPU_offscreen_bind(ofs);
02493 
02494     /* render 3d view */
02495     if(rv3d->persp==RV3D_CAMOB && v3d->camera) {
02496         CameraParams params;
02497 
02498         camera_params_init(&params);
02499         camera_params_from_object(&params, v3d->camera);
02500         camera_params_compute_viewplane(&params, sizex, sizey, scene->r.xasp, scene->r.yasp);
02501         camera_params_compute_matrix(&params);
02502 
02503         ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, params.winmat);
02504     }
02505     else {
02506         ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, NULL);
02507     }
02508 
02509     /* read in pixels & stamp */
02510     ibuf= IMB_allocImBuf(sizex, sizey, 32, flag);
02511 
02512     if(ibuf->rect_float)
02513         GPU_offscreen_read_pixels(ofs, GL_FLOAT, ibuf->rect_float);
02514     else if(ibuf->rect)
02515         GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, ibuf->rect);
02516     
02517     //if((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
02518     //  BKE_stamp_buf(scene, NULL, rr->rectf, rr->rectx, rr->recty, 4);
02519 
02520     /* unbind */
02521     GPU_offscreen_unbind(ofs);
02522     GPU_offscreen_free(ofs);
02523 
02524     glPopAttrib();
02525     
02526     if(ibuf->rect_float && ibuf->rect)
02527         IMB_rect_from_float(ibuf);
02528     
02529     return ibuf;
02530 }
02531 
02532 /* creates own 3d views, used by the sequencer */
02533 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(Scene *scene, Object *camera, int width, int height, unsigned int flag, int drawtype, char err_out[256])
02534 {
02535     View3D v3d= {NULL};
02536     ARegion ar= {NULL};
02537     RegionView3D rv3d= {{{0}}};
02538 
02539     /* connect data */
02540     v3d.regionbase.first= v3d.regionbase.last= &ar;
02541     ar.regiondata= &rv3d;
02542     ar.regiontype= RGN_TYPE_WINDOW;
02543 
02544     v3d.camera= camera;
02545     v3d.lay= scene->lay;
02546     v3d.drawtype = drawtype;
02547     v3d.flag2 = V3D_RENDER_OVERRIDE;
02548 
02549     rv3d.persp= RV3D_CAMOB;
02550 
02551     copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
02552     normalize_m4(rv3d.viewinv);
02553     invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
02554 
02555     {
02556         CameraParams params;
02557 
02558         camera_params_init(&params);
02559         camera_params_from_object(&params, v3d.camera);
02560         camera_params_compute_viewplane(&params, width, height, scene->r.xasp, scene->r.yasp);
02561         camera_params_compute_matrix(&params);
02562 
02563         copy_m4_m4(rv3d.winmat, params.winmat);
02564         v3d.near= params.clipsta;
02565         v3d.far= params.clipend;
02566         v3d.lens= params.lens;
02567     }
02568 
02569     mult_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
02570     invert_m4_m4(rv3d.persinv, rv3d.viewinv);
02571 
02572     return ED_view3d_draw_offscreen_imbuf(scene, &v3d, &ar, width, height, flag, err_out);
02573 
02574     // seq_view3d_cb(scene, cfra, render_size, seqrectx, seqrecty);
02575 }
02576 
02577 
02578 /* NOTE: the info that this uses is updated in ED_refresh_viewport_fps(), 
02579  * which currently gets called during SCREEN_OT_animation_step.
02580  */
02581 static void draw_viewport_fps(Scene *scene, ARegion *ar)
02582 {
02583     ScreenFrameRateInfo *fpsi= scene->fps_info;
02584     float fps;
02585     char printable[16];
02586     int i, tot;
02587     
02588     if (!fpsi || !fpsi->lredrawtime || !fpsi->redrawtime)
02589         return;
02590     
02591     printable[0] = '\0';
02592     
02593 #if 0
02594     /* this is too simple, better do an average */
02595     fps = (float)(1.0/(fpsi->lredrawtime-fpsi->redrawtime))
02596 #else
02597     fpsi->redrawtimes_fps[fpsi->redrawtime_index] = (float)(1.0/(fpsi->lredrawtime-fpsi->redrawtime));
02598     
02599     for (i=0, tot=0, fps=0.0f ; i < REDRAW_FRAME_AVERAGE ; i++) {
02600         if (fpsi->redrawtimes_fps[i]) {
02601             fps += fpsi->redrawtimes_fps[i];
02602             tot++;
02603         }
02604     }
02605     if (tot) {
02606         fpsi->redrawtime_index = (fpsi->redrawtime_index + 1) % REDRAW_FRAME_AVERAGE;
02607         
02608         //fpsi->redrawtime_index++;
02609         //if (fpsi->redrawtime >= REDRAW_FRAME_AVERAGE)
02610         //  fpsi->redrawtime = 0;
02611         
02612         fps = fps / tot;
02613     }
02614 #endif
02615 
02616     /* is this more then half a frame behind? */
02617     if (fps+0.5f < (float)(FPS)) {
02618         UI_ThemeColor(TH_REDALERT);
02619         BLI_snprintf(printable, sizeof(printable), "fps: %.2f", fps);
02620     } 
02621     else {
02622         UI_ThemeColor(TH_TEXT_HI);
02623         BLI_snprintf(printable, sizeof(printable), "fps: %i", (int)(fps+0.5f));
02624     }
02625     
02626     BLF_draw_default_ascii(22,  ar->winy-17, 0.0f, printable, sizeof(printable));
02627 }
02628 
02629 static int view3d_main_area_draw_engine(const bContext *C, ARegion *ar)
02630 {
02631     Scene *scene= CTX_data_scene(C);
02632     View3D *v3d = CTX_wm_view3d(C);
02633     RegionView3D *rv3d= CTX_wm_region_view3d(C);
02634     RenderEngineType *type;
02635 
02636     if(!rv3d->render_engine) {
02637         type= RE_engines_find(scene->r.engine);
02638 
02639         if(!(type->view_update && type->view_draw))
02640             return 0;
02641 
02642         rv3d->render_engine= RE_engine_create(type);
02643         type->view_update(rv3d->render_engine, C);
02644     }
02645 
02646     view3d_main_area_setup_view(scene, v3d, ar, NULL, NULL);
02647 
02648     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
02649     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
02650 
02651     ED_region_pixelspace(ar);
02652 
02653     type= rv3d->render_engine->type;
02654     type->view_draw(rv3d->render_engine, C);
02655 
02656     return 1;
02657 }
02658 
02659 static void view3d_main_area_draw_engine_info(RegionView3D *rv3d, ARegion *ar)
02660 {
02661     if(!rv3d->render_engine || !rv3d->render_engine->text)
02662         return;
02663 
02664     ED_region_info_draw(ar, rv3d->render_engine->text, 1, 0.25);
02665 }
02666 
02667 /* warning: this function has duplicate drawing in ED_view3d_draw_offscreen() */
02668 static void view3d_main_area_draw_objects(const bContext *C, ARegion *ar, const char **grid_unit)
02669 {
02670     Scene *scene= CTX_data_scene(C);
02671     View3D *v3d = CTX_wm_view3d(C);
02672     RegionView3D *rv3d= CTX_wm_region_view3d(C);
02673     Base *base;
02674     float backcol[3];
02675     unsigned int lay_used;
02676 
02677     /* shadow buffers, before we setup matrices */
02678     if(draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
02679         gpu_update_lamps_shadows(scene, v3d);
02680     
02681     /* reset default OpenGL lights if needed (i.e. after preferences have been altered) */
02682     if (rv3d->rflag & RV3D_GPULIGHT_UPDATE) {
02683         rv3d->rflag &= ~RV3D_GPULIGHT_UPDATE;
02684         GPU_default_lights();
02685     }
02686 
02687     /* clear background */
02688     if((v3d->flag2 & V3D_RENDER_OVERRIDE) && scene->world) {
02689         if(scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
02690             linearrgb_to_srgb_v3_v3(backcol, &scene->world->horr);
02691         else
02692             copy_v3_v3(backcol, &scene->world->horr);
02693         glClearColor(backcol[0], backcol[1], backcol[2], 0.0);
02694     }
02695     else
02696         UI_ThemeClearColor(TH_BACK);
02697 
02698     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
02699     
02700     /* setup view matrices */
02701     view3d_main_area_setup_view(scene, v3d, ar, NULL, NULL);
02702 
02703     ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
02704 
02705     if(rv3d->rflag & RV3D_CLIPPING)
02706         view3d_draw_clipping(rv3d);
02707     
02708     /* set zbuffer after we draw clipping region */
02709     if(v3d->drawtype > OB_WIRE) {
02710         v3d->zbuf= TRUE;
02711         glEnable(GL_DEPTH_TEST);
02712     }
02713     else
02714         v3d->zbuf= FALSE;
02715 
02716     /* enables anti-aliasing for 3D view drawing */
02717     /*if (!(U.gameflags & USER_DISABLE_AA))
02718         glEnable(GL_MULTISAMPLE_ARB);*/
02719     
02720     // needs to be done always, gridview is adjusted in drawgrid() now
02721     rv3d->gridview= v3d->grid;
02722 
02723     if((rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO)) {
02724         if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
02725             drawfloor(scene, v3d, grid_unit);
02726         }
02727         if(rv3d->persp==RV3D_CAMOB) {
02728             if(scene->world) {
02729                 if(scene->world->mode & WO_STARS) {
02730                     RE_make_stars(NULL, scene, star_stuff_init_func, star_stuff_vertex_func,
02731                                   star_stuff_term_func);
02732                 }
02733             }
02734             if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
02735                 if(v3d->flag & V3D_DISPBGPICS) draw_bgpic(scene, ar, v3d);
02736             }
02737         }
02738     }
02739     else {
02740         if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
02741             ED_region_pixelspace(ar);
02742             drawgrid(&scene->unit, ar, v3d, grid_unit);
02743             /* XXX make function? replaces persp(1) */
02744             glMatrixMode(GL_PROJECTION);
02745             glLoadMatrixf(rv3d->winmat);
02746             glMatrixMode(GL_MODELVIEW);
02747             glLoadMatrixf(rv3d->viewmat);
02748 
02749             if(v3d->flag & V3D_DISPBGPICS) {
02750                 draw_bgpic(scene, ar, v3d);
02751             }
02752         }
02753     }
02754     
02755     if(rv3d->rflag & RV3D_CLIPPING)
02756         view3d_set_clipping(rv3d);
02757 
02758     /* draw set first */
02759     if(scene->set) {
02760         Scene *sce_iter;
02761         for(SETLOOPER(scene->set, sce_iter, base)) {
02762             
02763             if(v3d->lay & base->lay) {
02764                 
02765                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
02766                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
02767                 
02768                 if(base->object->transflag & OB_DUPLI) {
02769                     draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
02770                 }
02771             }
02772         }
02773         
02774         /* Transp and X-ray afterdraw stuff for sets is done later */
02775     }
02776 
02777     lay_used= 0;
02778 
02779     /* then draw not selected and the duplis, but skip editmode object */
02780     for(base= scene->base.first; base; base= base->next) {
02781         lay_used |= base->lay & ((1<<20)-1);
02782 
02783         if(v3d->lay & base->lay) {
02784             
02785             /* dupli drawing */
02786             if(base->object->transflag & OB_DUPLI) {
02787                 draw_dupli_objects(scene, ar, v3d, base);
02788             }
02789             if((base->flag & SELECT)==0) {
02790                 if(base->object!=scene->obedit) 
02791                     draw_object(scene, ar, v3d, base, 0);
02792             }
02793         }
02794     }
02795 
02796     if(v3d->lay_used != lay_used) { /* happens when loading old files or loading with UI load */
02797         /* find header and force tag redraw */
02798         ScrArea *sa= CTX_wm_area(C);
02799         ARegion *ar_header= BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
02800         ED_region_tag_redraw(ar_header); /* can be NULL */
02801         v3d->lay_used= lay_used;
02802     }
02803 
02804     /* draw selected and editmode */
02805     for(base= scene->base.first; base; base= base->next) {
02806         if(v3d->lay & base->lay) {
02807             if (base->object==scene->obedit || ( base->flag & SELECT) ) 
02808                 draw_object(scene, ar, v3d, base, 0);
02809         }
02810     }
02811 
02812 //  REEB_draw();
02813 
02814     if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
02815         /* must be before xray draw which clears the depth buffer */
02816         if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
02817         draw_gpencil_view3d(scene, v3d, ar, 1);
02818         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
02819     }
02820 
02821     /* Transp and X-ray afterdraw stuff */
02822     if(v3d->afterdraw_transp.first)     view3d_draw_transp(scene, ar, v3d);
02823     if(v3d->afterdraw_xray.first)       view3d_draw_xray(scene, ar, v3d, 1);    // clears zbuffer if it is used!
02824     if(v3d->afterdraw_xraytransp.first) view3d_draw_xraytransp(scene, ar, v3d, 1);
02825     
02826     ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
02827 
02828     if(rv3d->rflag & RV3D_CLIPPING)
02829         view3d_clr_clipping();
02830     
02831     BIF_draw_manipulator(C);
02832     
02833     /* Disable back anti-aliasing */
02834     /*if (!(U.gameflags & USER_DISABLE_AA))
02835         glDisable(GL_MULTISAMPLE_ARB);*/
02836 
02837     if(v3d->zbuf) {
02838         v3d->zbuf= FALSE;
02839         glDisable(GL_DEPTH_TEST);
02840     }
02841 
02842     if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
02843         BDR_drawSketch(C);
02844     }
02845 
02846     if ((U.ndof_flag & NDOF_SHOW_GUIDE) && (rv3d->viewlock != RV3D_LOCKED) && (rv3d->persp != RV3D_CAMOB))
02847         // TODO: draw something else (but not this) during fly mode
02848         draw_rotation_guide(rv3d);
02849 
02850 }
02851 
02852 static void view3d_main_area_draw_info(const bContext *C, ARegion *ar, const char *grid_unit)
02853 {
02854     Scene *scene= CTX_data_scene(C);
02855     View3D *v3d = CTX_wm_view3d(C);
02856     RegionView3D *rv3d= CTX_wm_region_view3d(C);
02857     bScreen *screen= CTX_wm_screen(C);
02858 
02859     Object *ob;
02860 
02861     if(rv3d->persp==RV3D_CAMOB)
02862         drawviewborder(scene, ar, v3d);
02863 
02864     if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
02865         /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
02866     //  if (v3d->flag2 & V3D_DISPGP)
02867             draw_gpencil_view3d(scene, v3d, ar, 0);
02868 
02869         drawcursor(scene, ar, v3d);
02870     }
02871     
02872     if(U.uiflag & USER_SHOW_ROTVIEWICON)
02873         draw_view_axis(rv3d);
02874     else    
02875         draw_view_icon(rv3d);
02876     
02877     ob= OBACT;
02878     if(U.uiflag & USER_DRAWVIEWINFO) 
02879         draw_selected_name(scene, ob);
02880 
02881     if(rv3d->render_engine) {
02882         view3d_main_area_draw_engine_info(rv3d, ar);
02883         return;
02884     }
02885 
02886     if((U.uiflag & USER_SHOW_FPS) && screen->animtimer) {
02887         draw_viewport_fps(scene, ar);
02888     }
02889     else if(U.uiflag & USER_SHOW_VIEWPORTNAME) {
02890         draw_viewport_name(ar, v3d);
02891     }
02892     if (grid_unit) { /* draw below the viewport name */
02893         char numstr[32]= "";
02894 
02895         UI_ThemeColor(TH_TEXT_HI);
02896         if(v3d->grid != 1.0f) {
02897             BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
02898         }
02899 
02900         BLF_draw_default_ascii(22,  ar->winy-(USER_SHOW_VIEWPORTNAME?40:20), 0.0f,
02901                                numstr[0] ? numstr : grid_unit, sizeof(numstr));
02902     }
02903 }
02904 
02905 void view3d_main_area_draw(const bContext *C, ARegion *ar)
02906 {
02907     View3D *v3d = CTX_wm_view3d(C);
02908     const char *grid_unit= NULL;
02909 
02910     /* draw viewport using external renderer? */
02911     if(!(v3d->drawtype == OB_RENDER && view3d_main_area_draw_engine(C, ar))) {
02912         /* draw viewport using opengl */
02913         view3d_main_area_draw_objects(C, ar, &grid_unit);
02914         ED_region_pixelspace(ar);
02915     }
02916     
02917     view3d_main_area_draw_info(C, ar, grid_unit);
02918 
02919     v3d->flag |= V3D_INVALID_BACKBUF;
02920 }
02921