2.5 - Object name display in 3d-view now highlights on frames with keyframes again.
[blender-staging.git] / source / blender / editors / space_view3d / view3d_draw.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <math.h>
32
33 #include "DNA_action_types.h"
34 #include "DNA_armature_types.h"
35 #include "DNA_camera_types.h"
36 #include "DNA_group_types.h"
37 #include "DNA_key_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_userdef_types.h"
43 #include "DNA_view3d_types.h"
44 #include "DNA_world_types.h"
45
46 #include "MEM_guardedalloc.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_arithb.h"
50 #include "BLI_rand.h"
51
52 #include "BKE_anim.h"
53 #include "BKE_image.h"
54 #include "BKE_ipo.h"
55 #include "BKE_key.h"
56 #include "BKE_object.h"
57 #include "BKE_global.h"
58 #include "BKE_scene.h"
59 #include "BKE_screen.h"
60 #include "BKE_utildefines.h"
61
62 #include "RE_pipeline.h"        // make_stars
63
64 #include "IMB_imbuf_types.h"
65 #include "IMB_imbuf.h"
66
67 #include "BIF_gl.h"
68 #include "BIF_glutil.h"
69 #include "BMF_Api.h"
70
71 #include "WM_api.h"
72
73 #include "ED_keyframing.h"
74 #include "ED_screen.h"
75 #include "ED_util.h"
76 #include "ED_types.h"
77
78 #include "UI_interface.h"
79 #include "UI_interface_icons.h"
80 #include "UI_resources.h"
81 #include "UI_view2d.h"
82
83 #include "GPU_draw.h"
84 #include "GPU_material.h"
85
86 #include "view3d_intern.h"      // own include
87
88
89
90 static void star_stuff_init_func(void)
91 {
92         cpack(-1);
93         glPointSize(1.0);
94         glBegin(GL_POINTS);
95 }
96 static void star_stuff_vertex_func(float* i)
97 {
98         glVertex3fv(i);
99 }
100 static void star_stuff_term_func(void)
101 {
102         glEnd();
103 }
104
105 void circf(float x, float y, float rad)
106 {
107         GLUquadricObj *qobj = gluNewQuadric(); 
108         
109         gluQuadricDrawStyle(qobj, GLU_FILL); 
110         
111         glPushMatrix(); 
112         
113         glTranslatef(x,  y, 0.); 
114         
115         gluDisk( qobj, 0.0,  rad, 32, 1); 
116         
117         glPopMatrix(); 
118         
119         gluDeleteQuadric(qobj);
120 }
121
122 void circ(float x, float y, float rad)
123 {
124         GLUquadricObj *qobj = gluNewQuadric(); 
125         
126         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
127         
128         glPushMatrix(); 
129         
130         glTranslatef(x,  y, 0.); 
131         
132         gluDisk( qobj, 0.0,  rad, 32, 1); 
133         
134         glPopMatrix(); 
135         
136         gluDeleteQuadric(qobj);
137 }
138
139
140 /* ********* custom clipping *********** */
141
142 static void view3d_draw_clipping(View3D *v3d)
143 {
144         BoundBox *bb= v3d->clipbb;
145         
146         UI_ThemeColorShade(TH_BACK, -8);
147         
148         glBegin(GL_QUADS);
149         
150         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[3]);
151         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[1]);
152         glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[5]);
153         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[3]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[6]);
154         glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[2]);
155         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[3]);
156         
157         glEnd();
158 }
159
160 void view3d_set_clipping(View3D *v3d)
161 {
162         double plane[4];
163         int a;
164         
165         for(a=0; a<4; a++) {
166                 QUATCOPY(plane, v3d->clip[a]);
167                 glClipPlane(GL_CLIP_PLANE0+a, plane);
168                 glEnable(GL_CLIP_PLANE0+a);
169         }
170 }
171
172 void view3d_clr_clipping(void)
173 {
174         int a;
175         
176         for(a=0; a<4; a++) {
177                 glDisable(GL_CLIP_PLANE0+a);
178         }
179 }
180
181 int view3d_test_clipping(View3D *v3d, float *vec)
182 {
183         /* vec in world coordinates, returns 1 if clipped */
184         float view[3];
185         
186         VECCOPY(view, vec);
187         
188         if(0.0f < v3d->clip[0][3] + INPR(view, v3d->clip[0]))
189                 if(0.0f < v3d->clip[1][3] + INPR(view, v3d->clip[1]))
190                         if(0.0f < v3d->clip[2][3] + INPR(view, v3d->clip[2]))
191                                 if(0.0f < v3d->clip[3][3] + INPR(view, v3d->clip[3]))
192                                         return 0;
193         
194         return 1;
195 }
196
197 /* ********* end custom clipping *********** */
198
199
200 static void drawgrid_draw(ARegion *ar, float wx, float wy, float x, float y, float dx)
201 {
202         float fx, fy;
203         
204         x+= (wx); 
205         y+= (wy);
206         fx= x/dx;
207         fx= x-dx*floor(fx);
208         
209         while(fx< ar->winx) {
210                 fdrawline(fx,  0.0,  fx,  (float)ar->winy); 
211                 fx+= dx; 
212         }
213
214         fy= y/dx;
215         fy= y-dx*floor(fy);
216         
217
218         while(fy< ar->winy) {
219                 fdrawline(0.0,  fy,  (float)ar->winx,  fy); 
220                 fy+= dx;
221         }
222
223 }
224
225 // not intern, called in editobject for constraint axis too
226 void make_axis_color(char *col, char *col2, char axis)
227 {
228         if(axis=='x') {
229                 col2[0]= col[0]>219?255:col[0]+36;
230                 col2[1]= col[1]<26?0:col[1]-26;
231                 col2[2]= col[2]<26?0:col[2]-26;
232         }
233         else if(axis=='y') {
234                 col2[0]= col[0]<46?0:col[0]-36;
235                 col2[1]= col[1]>189?255:col[1]+66;
236                 col2[2]= col[2]<46?0:col[2]-36; 
237         }
238         else {
239                 col2[0]= col[0]<26?0:col[0]-26; 
240                 col2[1]= col[1]<26?0:col[1]-26; 
241                 col2[2]= col[2]>209?255:col[2]+46;
242         }
243         
244 }
245
246 static void drawgrid(ARegion *ar, View3D *v3d)
247 {
248         /* extern short bgpicmode; */
249         float wx, wy, x, y, fw, fx, fy, dx;
250         float vec4[4];
251         char col[3], col2[3];
252         short sublines = v3d->gridsubdiv;
253         
254         vec4[0]=vec4[1]=vec4[2]=0.0; 
255         vec4[3]= 1.0;
256         Mat4MulVec4fl(v3d->persmat, vec4);
257         fx= vec4[0]; 
258         fy= vec4[1]; 
259         fw= vec4[3];
260
261         wx= (ar->winx/2.0);     /* because of rounding errors, grid at wrong location */
262         wy= (ar->winy/2.0);
263
264         x= (wx)*fx/fw;
265         y= (wy)*fy/fw;
266
267         vec4[0]=vec4[1]=v3d->grid; 
268         vec4[2]= 0.0;
269         vec4[3]= 1.0;
270         Mat4MulVec4fl(v3d->persmat, vec4);
271         fx= vec4[0]; 
272         fy= vec4[1]; 
273         fw= vec4[3];
274
275         dx= fabs(x-(wx)*fx/fw);
276         if(dx==0) dx= fabs(y-(wy)*fy/fw);
277         
278         glDepthMask(0);         // disable write in zbuffer
279
280         /* check zoom out */
281         UI_ThemeColor(TH_GRID);
282         
283         if(dx<6.0) {
284                 v3d->gridview*= sublines;
285                 dx*= sublines;
286                 
287                 if(dx<6.0) {    
288                         v3d->gridview*= sublines;
289                         dx*= sublines;
290                         
291                         if(dx<6.0) {
292                                 v3d->gridview*= sublines;
293                                 dx*=sublines;
294                                 if(dx<6.0);
295                                 else {
296                                         UI_ThemeColor(TH_GRID);
297                                         drawgrid_draw(ar, wx, wy, x, y, dx);
298                                 }
299                         }
300                         else {  // start blending out
301                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
302                                 drawgrid_draw(ar, wx, wy, x, y, dx);
303                         
304                                 UI_ThemeColor(TH_GRID);
305                                 drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
306                         }
307                 }
308                 else {  // start blending out (6 < dx < 60)
309                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
310                         drawgrid_draw(ar, wx, wy, x, y, dx);
311                         
312                         UI_ThemeColor(TH_GRID);
313                         drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
314                 }
315         }
316         else {
317                 if(dx>60.0) {           // start blending in
318                         v3d->gridview/= sublines;
319                         dx/= sublines;                  
320                         if(dx>60.0) {           // start blending in
321                                 v3d->gridview/= sublines;
322                                 dx/= sublines;
323                                 if(dx>60.0) {
324                                         UI_ThemeColor(TH_GRID);
325                                         drawgrid_draw(ar, wx, wy, x, y, dx);
326                                 }
327                                 else {
328                                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
329                                         drawgrid_draw(ar, wx, wy, x, y, dx);
330                                         UI_ThemeColor(TH_GRID);
331                                         drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
332                                 }
333                         }
334                         else {
335                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
336                                 drawgrid_draw(ar, wx, wy, x, y, dx);
337                                 UI_ThemeColor(TH_GRID);                         
338                                 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
339                         }
340                 }
341                 else {
342                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
343                         drawgrid_draw(ar, wx, wy, x, y, dx);
344                         UI_ThemeColor(TH_GRID);
345                         drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
346                 }
347         }
348
349         x+= (wx); 
350         y+= (wy);
351         UI_GetThemeColor3ubv(TH_GRID, col);
352
353         setlinestyle(0);
354         
355         /* center cross */
356         if(v3d->view==3) make_axis_color(col, col2, 'y');
357         else make_axis_color(col, col2, 'x');
358         glColor3ubv((GLubyte *)col2);
359         
360         fdrawline(0.0,  y,  (float)ar->winx,  y); 
361         
362         if(v3d->view==7) make_axis_color(col, col2, 'y');
363         else make_axis_color(col, col2, 'z');
364         glColor3ubv((GLubyte *)col2);
365
366         fdrawline(x, 0.0, x, (float)ar->winy); 
367
368         glDepthMask(1);         // enable write in zbuffer
369 }
370
371
372 static void drawfloor(View3D *v3d)
373 {
374         float vert[3], grid;
375         int a, gridlines, emphasise;
376         char col[3], col2[3];
377         short draw_line = 0;
378         
379         vert[2]= 0.0;
380         
381         if(v3d->gridlines<3) return;
382         
383         if(v3d->zbuf && G.obedit) glDepthMask(0);       // for zbuffer-select
384         
385         gridlines= v3d->gridlines/2;
386         grid= gridlines*v3d->grid;
387         
388         UI_GetThemeColor3ubv(TH_GRID, col);
389         UI_GetThemeColor3ubv(TH_BACK, col2);
390         
391         /* emphasise division lines lighter instead of darker, if background is darker than grid */
392         if ( ((col[0]+col[1]+col[2])/3+10) > (col2[0]+col2[1]+col2[2])/3 )
393                 emphasise = 20;
394         else
395                 emphasise = -10;
396         
397         /* draw the Y axis and/or grid lines */
398         for(a= -gridlines;a<=gridlines;a++) {
399                 if(a==0) {
400                         /* check for the 'show Y axis' preference */
401                         if (v3d->gridflag & V3D_SHOW_Y) { 
402                                 make_axis_color(col, col2, 'y');
403                                 glColor3ubv((GLubyte *)col2);
404                                 
405                                 draw_line = 1;
406                         } else if (v3d->gridflag & V3D_SHOW_FLOOR) {
407                                 UI_ThemeColorShade(TH_GRID, emphasise);
408                         } else {
409                                 draw_line = 0;
410                         }
411                 } else {
412                         /* check for the 'show grid floor' preference */
413                         if (v3d->gridflag & V3D_SHOW_FLOOR) {
414                                 if( (a % 10)==0) {
415                                         UI_ThemeColorShade(TH_GRID, emphasise);
416                                 }
417                                 else UI_ThemeColorShade(TH_GRID, 10);
418                                 
419                                 draw_line = 1;
420                         } else {
421                                 draw_line = 0;
422                         }
423                 }
424                 
425                 if (draw_line) {
426                         glBegin(GL_LINE_STRIP);
427                 vert[0]= a*v3d->grid;
428                 vert[1]= grid;
429                 glVertex3fv(vert);
430                 vert[1]= -grid;
431                 glVertex3fv(vert);
432                         glEnd();
433                 }
434         }
435         
436         /* draw the X axis and/or grid lines */
437         for(a= -gridlines;a<=gridlines;a++) {
438                 if(a==0) {
439                         /* check for the 'show X axis' preference */
440                         if (v3d->gridflag & V3D_SHOW_X) { 
441                                 make_axis_color(col, col2, 'x');
442                                 glColor3ubv((GLubyte *)col2);
443                                 
444                                 draw_line = 1;
445                         } else if (v3d->gridflag & V3D_SHOW_FLOOR) {
446                                 UI_ThemeColorShade(TH_GRID, emphasise);
447                         } else {
448                                 draw_line = 0;
449                         }
450                 } else {
451                         /* check for the 'show grid floor' preference */
452                         if (v3d->gridflag & V3D_SHOW_FLOOR) {
453                                 if( (a % 10)==0) {
454                                         UI_ThemeColorShade(TH_GRID, emphasise);
455                                 }
456                                 else UI_ThemeColorShade(TH_GRID, 10);
457                                 
458                                 draw_line = 1;
459                         } else {
460                                 draw_line = 0;
461                         }
462                 }
463                 
464                 if (draw_line) {
465                         glBegin(GL_LINE_STRIP);
466                 vert[1]= a*v3d->grid;
467                 vert[0]= grid;
468                 glVertex3fv(vert );
469                 vert[0]= -grid;
470                 glVertex3fv(vert);
471                         glEnd();
472                 }
473         }
474         
475         /* draw the Z axis line */      
476         /* check for the 'show Z axis' preference */
477         if (v3d->gridflag & V3D_SHOW_Z) {
478                 make_axis_color(col, col2, 'z');
479                 glColor3ubv((GLubyte *)col2);
480                 
481                 glBegin(GL_LINE_STRIP);
482                 vert[0]= 0;
483                 vert[1]= 0;
484                 vert[2]= grid;
485                 glVertex3fv(vert );
486                 vert[2]= -grid;
487                 glVertex3fv(vert);
488                 glEnd();
489         }
490         
491         if(v3d->zbuf && G.obedit) glDepthMask(1);       
492         
493 }
494
495 static void drawcursor(Scene *scene, ARegion *ar, View3D *v3d)
496 {
497         short mx,my,co[2];
498         int flag;
499         
500         /* we dont want the clipping for cursor */
501         flag= v3d->flag;
502         v3d->flag= 0;
503         project_short(ar, v3d, give_cursor(scene, v3d), co);
504         v3d->flag= flag;
505         
506         mx = co[0];
507         my = co[1];
508         
509         if(mx!=IS_CLIPPED) {
510                 setlinestyle(0); 
511                 cpack(0xFF);
512                 circ((float)mx, (float)my, 10.0);
513                 setlinestyle(4); 
514                 cpack(0xFFFFFF);
515                 circ((float)mx, (float)my, 10.0);
516                 setlinestyle(0);
517                 cpack(0x0);
518                 
519                 sdrawline(mx-20, my, mx-5, my);
520                 sdrawline(mx+5, my, mx+20, my);
521                 sdrawline(mx, my-20, mx, my-5);
522                 sdrawline(mx, my+5, mx, my+20);
523         }
524 }
525
526 /* Draw a live substitute of the view icon, which is always shown */
527 static void draw_view_axis(View3D *v3d)
528 {
529         const float k = U.rvisize;   /* axis size */
530         const float toll = 0.5;      /* used to see when view is quasi-orthogonal */
531         const float start = k + 1.0; /* axis center in screen coordinates, x=y */
532         float ydisp = 0.0;          /* vertical displacement to allow obj info text */
533         
534         /* rvibright ranges approx. from original axis icon color to gizmo color */
535         float bright = U.rvibright / 15.0f;
536         
537         unsigned char col[3];
538         unsigned char gridcol[3];
539         float colf[3];
540         
541         float vec[4];
542         float dx, dy;
543         float h, s, v;
544         
545         /* thickness of lines is proportional to k */
546         /*      (log(k)-1) gives a more suitable thickness, but fps decreased by about 3 fps */
547         glLineWidth(k / 10);
548         //glLineWidth(log(k)-1); // a bit slow
549         
550         UI_GetThemeColor3ubv(TH_GRID, (char *)gridcol);
551         
552         /* X */
553         vec[0] = vec[3] = 1;
554         vec[1] = vec[2] = 0;
555         QuatMulVecf(v3d->viewquat, vec);
556         
557         make_axis_color((char *)gridcol, (char *)col, 'x');
558         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
559         s = s<0.5 ? s+0.5 : 1.0;
560         v = 0.3;
561         v = (v<1.0-(bright) ? v+bright : 1.0);
562         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
563         glColor3fv(colf);
564         
565         dx = vec[0] * k;
566         dy = vec[1] * k;
567         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
568         if (fabs(dx) > toll || fabs(dy) > toll) {
569                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
570                 BMF_DrawString(G.fonts, "x");
571         }
572         
573         /* Y */
574         vec[1] = vec[3] = 1;
575         vec[0] = vec[2] = 0;
576         QuatMulVecf(v3d->viewquat, vec);
577         
578         make_axis_color((char *)gridcol, (char *)col, 'y');
579         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
580         s = s<0.5 ? s+0.5 : 1.0;
581         v = 0.3;
582         v = (v<1.0-(bright) ? v+bright : 1.0);
583         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
584         glColor3fv(colf);
585         
586         dx = vec[0] * k;
587         dy = vec[1] * k;
588         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
589         if (fabs(dx) > toll || fabs(dy) > toll) {
590                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
591                 BMF_DrawString(G.fonts, "y");
592         }
593         
594         /* Z */
595         vec[2] = vec[3] = 1;
596         vec[1] = vec[0] = 0;
597         QuatMulVecf(v3d->viewquat, vec);
598         
599         make_axis_color((char *)gridcol, (char *)col, 'z');
600         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
601         s = s<0.5 ? s+0.5 : 1.0;
602         v = 0.5;
603         v = (v<1.0-(bright) ? v+bright : 1.0);
604         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
605         glColor3fv(colf);
606         
607         dx = vec[0] * k;
608         dy = vec[1] * k;
609         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
610         if (fabs(dx) > toll || fabs(dy) > toll) {
611                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
612                 BMF_DrawString(G.fonts, "z");
613         }
614         
615         /* restore line-width */
616         glLineWidth(1.0);
617 }
618
619
620 static void draw_view_icon(View3D *v3d)
621 {
622         BIFIconID icon;
623         
624         if(v3d->view==7) icon= ICON_AXIS_TOP;
625         else if(v3d->view==1) icon= ICON_AXIS_FRONT;
626         else if(v3d->view==3) icon= ICON_AXIS_SIDE;
627         else return ;
628         
629         glEnable(GL_BLEND);
630         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
631         
632         UI_icon_draw(5.0, 5.0, icon);
633         
634         glDisable(GL_BLEND);
635 }
636
637 char *view3d_get_name(View3D *v3d)
638 {
639         char *name = NULL;
640         
641         switch (v3d->view) {
642                 case 1:
643                         if (v3d->persp == V3D_ORTHO)
644                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Back Ortho" : "Front Ortho";
645                         else
646                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Back Persp" : "Front Persp";
647                         break;
648                 case 3:
649                         if (v3d->persp == V3D_ORTHO)
650                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Left Ortho" : "Right Ortho";
651                         else
652                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Left Persp" : "Right Persp";
653                         break;
654                 case 7:
655                         if (v3d->persp == V3D_ORTHO)
656                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Bottom Ortho" : "Top Ortho";
657                         else
658                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Bottom Persp" : "Top Persp";
659                         break;
660                 default:
661                         if (v3d->persp==V3D_CAMOB) {
662                                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
663                                         Camera *cam;
664                                         cam = v3d->camera->data;
665                                         name = (cam->type != CAM_ORTHO) ? "Camera Persp" : "Camera Ortho";
666                                 } else {
667                                         name = "Object as Camera";
668                                 }
669                         } else { 
670                                 name = (v3d->persp == V3D_ORTHO) ? "User Ortho" : "User Persp";
671                         }
672                         break;
673         }
674         
675         return name;
676 }
677
678 static void draw_viewport_name(ARegion *ar, View3D *v3d)
679 {
680         char *name = view3d_get_name(v3d);
681         char *printable = NULL;
682         
683         if (v3d->localview) {
684                 printable = malloc(strlen(name) + strlen(" (Local)_")); /* '_' gives space for '\0' */
685                                                                                                  strcpy(printable, name);
686                                                                                                  strcat(printable, " (Local)");
687         } else {
688                 printable = name;
689         }
690
691         if (printable) {
692                 UI_ThemeColor(TH_TEXT_HI);
693                 glRasterPos2i(10,  ar->winy-20);
694                 BMF_DrawString(G.fonts, printable);
695         }
696
697         if (v3d->localview) {
698                 free(printable);
699         }
700 }
701
702
703 static char *get_cfra_marker_name(Scene *scene)
704 {
705         ListBase *markers= &scene->markers;
706         TimeMarker *m1, *m2;
707         
708         /* search through markers for match */
709         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
710                 if (m1->frame==CFRA)
711                         return m1->name;
712                 
713                 if (m1 == m2)
714                         break;          
715                 
716                 if (m2->frame==CFRA)
717                         return m2->name;
718         }
719         
720         return NULL;
721 }
722
723 /* draw info beside axes in bottom left-corner: 
724 *       framenum, object name, bone name (if available), marker name (if available)
725 */
726 static void draw_selected_name(Scene *scene, Object *ob, View3D *v3d)
727 {
728         char info[256], *markern;
729         short offset=30;
730         
731         /* get name of marker on current frame (if available) */
732         markern= get_cfra_marker_name(scene);
733         
734         /* check if there is an object */
735         if(ob) {
736                 /* name(s) to display depends on type of object */
737                 if(ob->type==OB_ARMATURE) {
738                         bArmature *arm= ob->data;
739                         char *name= NULL;
740                         
741                         /* show name of active bone too (if possible) */
742                         if(ob==G.obedit) {
743 //      XXX                     EditBone *ebo;
744 //                              for (ebo=G.edbo.first; ebo; ebo=ebo->next){
745 //                                      if ((ebo->flag & BONE_ACTIVE) && (ebo->layer & arm->layer)) {
746 //                                              name= ebo->name;
747 //                                              break;
748 //                                      }
749 //                              }
750                         }
751                         else if(ob->pose && (ob->flag & OB_POSEMODE)) {
752                                 bPoseChannel *pchan;
753                                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
754                                         if((pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer)) {
755                                                 name= pchan->name;
756                                                 break;
757                                         }
758                                 }
759                         }
760                         if(name && markern)
761                                 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, name, markern);
762                         else if(name)
763                                 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, name);
764                         else
765                                 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
766                 }
767                 else if(ELEM3(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
768                         Key *key= NULL;
769                         KeyBlock *kb = NULL;
770                         char shapes[75];
771                         
772                         /* try to display active shapekey too */
773                         shapes[0] = 0;
774                         key = ob_get_key(ob);
775                         if(key){
776                                 kb = BLI_findlink(&key->block, ob->shapenr-1);
777                                 if(kb){
778                                         sprintf(shapes, ": %s ", kb->name);             
779                                         if(ob->shapeflag == OB_SHAPE_LOCK){
780                                                 sprintf(shapes, "%s (Pinned)",shapes);
781                                         }
782                                 }
783                         }
784                         
785                         if(markern)
786                                 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, shapes, markern);
787                         else
788                                 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, shapes);
789                 }
790                 else {
791                         /* standard object */
792                         if (markern)
793                                 sprintf(info, "(%d) %s <%s>", CFRA, ob->id.name+2, markern);
794                         else
795                                 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
796                 }
797                 
798                 /* colour depends on whether there is a keyframe */
799         if (id_frame_has_keyframe((ID *)ob, frame_to_float(CFRA), v3d->keyflags))
800                         UI_ThemeColor(TH_VERTEX_SELECT);
801                 else
802                         UI_ThemeColor(TH_TEXT_HI);
803         }
804         else {
805                 /* no object */
806                 if (markern)
807                         sprintf(info, "(%d) <%s>", CFRA, markern);
808                 else
809                         sprintf(info, "(%d)", CFRA);
810                 
811                 /* colour is always white */
812                 UI_ThemeColor(TH_TEXT_HI);
813         }
814         
815         if (U.uiflag & USER_SHOW_ROTVIEWICON)
816                 offset = 14 + (U.rvisize * 2);
817         
818         glRasterPos2i(offset,  10);
819         BMF_DrawString(G.fonts, info);
820 }
821
822 static void view3d_get_viewborder_size(Scene *scene, ARegion *ar, float size_r[2])
823 {
824         float winmax= MAX2(ar->winx, ar->winy);
825         float aspect= (float) (scene->r.xsch*scene->r.xasp)/(scene->r.ysch*scene->r.yasp);
826         
827         if(aspect>1.0) {
828                 size_r[0]= winmax;
829                 size_r[1]= winmax/aspect;
830         } else {
831                 size_r[0]= winmax*aspect;
832                 size_r[1]= winmax;
833         }
834 }
835
836 void calc_viewborder(Scene *scene, ARegion *ar, View3D *v3d, rctf *viewborder_r)
837 {
838         float zoomfac, size[2];
839         float dx= 0.0f, dy= 0.0f;
840         
841         view3d_get_viewborder_size(scene, ar, size);
842         
843         /* magic zoom calculation, no idea what
844                 * it signifies, if you find out, tell me! -zr
845                 */
846         /* simple, its magic dude!
847                 * well, to be honest, this gives a natural feeling zooming
848                 * with multiple keypad presses (ton)
849                 */
850         
851         zoomfac= (M_SQRT2 + v3d->camzoom/50.0);
852         zoomfac= (zoomfac*zoomfac)*0.25;
853         
854         size[0]= size[0]*zoomfac;
855         size[1]= size[1]*zoomfac;
856         
857         /* center in window */
858         viewborder_r->xmin= 0.5*ar->winx - 0.5*size[0];
859         viewborder_r->ymin= 0.5*ar->winy - 0.5*size[1];
860         viewborder_r->xmax= viewborder_r->xmin + size[0];
861         viewborder_r->ymax= viewborder_r->ymin + size[1];
862         
863         dx= ar->winx*v3d->camdx*zoomfac*2.0f;
864         dy= ar->winy*v3d->camdy*zoomfac*2.0f;
865         
866         /* apply offset */
867         viewborder_r->xmin-= dx;
868         viewborder_r->ymin-= dy;
869         viewborder_r->xmax-= dx;
870         viewborder_r->ymax-= dy;
871         
872         if(v3d->camera && v3d->camera->type==OB_CAMERA) {
873                 Camera *cam= v3d->camera->data;
874                 float w = viewborder_r->xmax - viewborder_r->xmin;
875                 float h = viewborder_r->ymax - viewborder_r->ymin;
876                 float side = MAX2(w, h);
877                 
878                 viewborder_r->xmin+= cam->shiftx*side;
879                 viewborder_r->xmax+= cam->shiftx*side;
880                 viewborder_r->ymin+= cam->shifty*side;
881                 viewborder_r->ymax+= cam->shifty*side;
882         }
883 }
884
885 void view3d_set_1_to_1_viewborder(Scene *scene, ARegion *ar, View3D *v3d)
886 {
887         float size[2];
888         int im_width= (scene->r.size*scene->r.xsch)/100;
889         
890         view3d_get_viewborder_size(scene, ar, size);
891         
892         v3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
893         v3d->camzoom= CLAMPIS(v3d->camzoom, -30, 300);
894 }
895
896
897 static void drawviewborder_flymode(ARegion *ar) 
898 {
899         /* draws 4 edge brackets that frame the safe area where the
900         mouse can move during fly mode without spinning the view */
901         float x1, x2, y1, y2;
902         
903         x1= 0.45*(float)ar->winx;
904         y1= 0.45*(float)ar->winy;
905         x2= 0.55*(float)ar->winx;
906         y2= 0.55*(float)ar->winy;
907         cpack(0);
908         
909         
910         glBegin(GL_LINES);
911         /* bottom left */
912         glVertex2f(x1,y1); 
913         glVertex2f(x1,y1+5);
914         
915         glVertex2f(x1,y1); 
916         glVertex2f(x1+5,y1);
917         
918         /* top right */
919         glVertex2f(x2,y2); 
920         glVertex2f(x2,y2-5);
921         
922         glVertex2f(x2,y2); 
923         glVertex2f(x2-5,y2);
924         
925         /* top left */
926         glVertex2f(x1,y2); 
927         glVertex2f(x1,y2-5);
928         
929         glVertex2f(x1,y2); 
930         glVertex2f(x1+5,y2);
931         
932         /* bottom right */
933         glVertex2f(x2,y1); 
934         glVertex2f(x2,y1+5);
935         
936         glVertex2f(x2,y1); 
937         glVertex2f(x2-5,y1);
938         glEnd();        
939 }
940
941
942 static void drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
943 {
944         extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad);          // interface_panel.c
945         float fac, a;
946         float x1, x2, y1, y2;
947         float x3, y3, x4, y4;
948         rctf viewborder;
949         Camera *ca= NULL;
950         
951         if(v3d->camera==NULL)
952                 return;
953         if(v3d->camera->type==OB_CAMERA)
954                 ca = v3d->camera->data;
955         
956         calc_viewborder(scene, ar, v3d, &viewborder);
957         x1= viewborder.xmin;
958         y1= viewborder.ymin;
959         x2= viewborder.xmax;
960         y2= viewborder.ymax;
961         
962         /* passepartout, specified in camera edit buttons */
963         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001) {
964                 if (ca->passepartalpha == 1.0) {
965                         glColor3f(0, 0, 0);
966                 } else {
967                         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
968                         glEnable(GL_BLEND);
969                         glColor4f(0, 0, 0, ca->passepartalpha);
970                 }
971                 if (x1 > 0.0)
972                         glRectf(0.0, (float)ar->winy, x1, 0.0);
973                 if (x2 < (float)ar->winx)
974                         glRectf(x2, (float)ar->winy, (float)ar->winx, 0.0);
975                 if (y2 < (float)ar->winy)
976                         glRectf(x1, (float)ar->winy, x2, y2);
977                 if (y2 > 0.0) 
978                         glRectf(x1, y1, x2, 0.0);
979                 
980                 glDisable(GL_BLEND);
981         }
982         
983         /* edge */
984         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
985         
986         setlinestyle(0);
987         UI_ThemeColor(TH_BACK);
988         glRectf(x1, y1, x2, y2);
989         
990         setlinestyle(3);
991         UI_ThemeColor(TH_WIRE);
992         glRectf(x1, y1, x2, y2);
993         
994         /* camera name - draw in highlighted text color */
995         if (ca && (ca->flag & CAM_SHOWNAME)) {
996                 UI_ThemeColor(TH_TEXT_HI);
997                 glRasterPos2f(x1, y1-15);
998                 
999                 BMF_DrawString(G.font, v3d->camera->id.name+2);
1000                 UI_ThemeColor(TH_WIRE);
1001         }
1002         
1003         
1004         /* border */
1005         if(scene->r.mode & R_BORDER) {
1006                 
1007                 cpack(0);
1008                 x3= x1+ scene->r.border.xmin*(x2-x1);
1009                 y3= y1+ scene->r.border.ymin*(y2-y1);
1010                 x4= x1+ scene->r.border.xmax*(x2-x1);
1011                 y4= y1+ scene->r.border.ymax*(y2-y1);
1012                 
1013                 cpack(0x4040FF);
1014                 glRectf(x3,  y3,  x4,  y4); 
1015         }
1016         
1017         /* safety border */
1018         if (ca && (ca->flag & CAM_SHOWTITLESAFE)) {
1019                 fac= 0.1;
1020                 
1021                 a= fac*(x2-x1);
1022                 x1+= a; 
1023                 x2-= a;
1024                 
1025                 a= fac*(y2-y1);
1026                 y1+= a;
1027                 y2-= a;
1028                 
1029                 UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1030                 
1031                 uiSetRoundBox(15);
1032                 gl_round_box(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
1033         }
1034         
1035         setlinestyle(0);
1036         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1037         
1038 }
1039
1040 static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
1041 {
1042         BGpic *bgpic;
1043         Image *ima;
1044         ImBuf *ibuf= NULL;
1045         float vec[4], fac, asp, zoomx, zoomy;
1046         float x1, y1, x2, y2, cx, cy;
1047         
1048         bgpic= v3d->bgpic;
1049         if(bgpic==NULL) return;
1050         
1051         ima= bgpic->ima;
1052         
1053         if(ima)
1054                 ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
1055         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL) ) 
1056                 return;
1057         if(ibuf->channels!=4)
1058                 return;
1059         if(ibuf->rect==NULL)
1060                 IMB_rect_from_float(ibuf);
1061         
1062         if(v3d->persp==2) {
1063                 rctf vb;
1064                 
1065                 calc_viewborder(scene, ar, v3d, &vb);
1066                 
1067                 x1= vb.xmin;
1068                 y1= vb.ymin;
1069                 x2= vb.xmax;
1070                 y2= vb.ymax;
1071         }
1072         else {
1073                 float sco[2];
1074                 
1075                 /* calc window coord */
1076                 initgrabz(v3d, 0.0, 0.0, 0.0);
1077                 window_to_3d(ar, v3d, vec, 1, 0);
1078                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
1079                 fac= 1.0/fac;
1080                 
1081                 asp= ( (float)ibuf->y)/(float)ibuf->x;
1082                 
1083                 vec[0] = vec[1] = vec[2] = 0.0;
1084                 view3d_project_float(ar, vec, sco, v3d->persmat);
1085                 cx = sco[0];
1086                 cy = sco[1];
1087                 
1088                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
1089                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
1090                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
1091                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
1092         }
1093         
1094         /* complete clip? */
1095         
1096         if(x2 < 0 ) return;
1097         if(y2 < 0 ) return;
1098         if(x1 > ar->winx ) return;
1099         if(y1 > ar->winy ) return;
1100         
1101         zoomx= (x2-x1)/ibuf->x;
1102         zoomy= (y2-y1)/ibuf->y;
1103         
1104         /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
1105         if(zoomx < 1.0f || zoomy < 1.0f) {
1106                 float tzoom= MIN2(zoomx, zoomy);
1107                 int mip= 0;
1108                 
1109                 if(ibuf->mipmap[0]==NULL)
1110                         IMB_makemipmap(ibuf, 0);
1111                 
1112                 while(tzoom < 1.0f && mip<8 && ibuf->mipmap[mip]) {
1113                         tzoom*= 2.0f;
1114                         zoomx*= 2.0f;
1115                         zoomy*= 2.0f;
1116                         mip++;
1117                 }
1118                 if(mip>0)
1119                         ibuf= ibuf->mipmap[mip-1];
1120         }
1121         
1122         if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
1123         
1124         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
1125         
1126         glMatrixMode(GL_PROJECTION);
1127         glPushMatrix();
1128         glMatrixMode(GL_MODELVIEW);
1129         glPushMatrix();
1130         
1131         glaDefine2DArea(&ar->winrct);
1132         
1133         glEnable(GL_BLEND);
1134         
1135         glPixelZoom(zoomx, zoomy);
1136         glColor4f(1.0, 1.0, 1.0, 1.0-bgpic->blend);
1137         glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
1138         
1139         glPixelZoom(1.0, 1.0);
1140         glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1141         
1142         glMatrixMode(GL_PROJECTION);
1143         glPopMatrix();
1144         glMatrixMode(GL_MODELVIEW);
1145         glPopMatrix();
1146         
1147         glDisable(GL_BLEND);
1148         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1149         
1150         // XXX  areawinset(ar->win);    // restore viewport / scissor
1151 }
1152
1153 /* ****************** View3d afterdraw *************** */
1154
1155 typedef struct View3DAfter {
1156         struct View3DAfter *next, *prev;
1157         struct Base *base;
1158         int type, flag;
1159 } View3DAfter;
1160
1161 /* temp storage of Objects that need to be drawn as last */
1162 void add_view3d_after(View3D *v3d, Base *base, int type, int flag)
1163 {
1164         View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
1165         
1166         BLI_addtail(&v3d->afterdraw, v3da);
1167         v3da->base= base;
1168         v3da->type= type;
1169         v3da->flag= flag;
1170 }
1171
1172 /* clears zbuffer and draws it over */
1173 static void view3d_draw_xray(Scene *scene, ARegion *ar, View3D *v3d, int clear)
1174 {
1175         View3DAfter *v3da, *next;
1176         int doit= 0;
1177         
1178         for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
1179                 if(v3da->type==V3D_XRAY) doit= 1;
1180         
1181         if(doit) {
1182                 if(clear && v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
1183                 v3d->xray= TRUE;
1184                 
1185                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1186                         next= v3da->next;
1187                         if(v3da->type==V3D_XRAY) {
1188                                 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1189                                 BLI_remlink(&v3d->afterdraw, v3da);
1190                                 MEM_freeN(v3da);
1191                         }
1192                 }
1193                 v3d->xray= FALSE;
1194         }
1195 }
1196
1197 /* disables write in zbuffer and draws it over */
1198 static void view3d_draw_transp(Scene *scene, ARegion *ar, View3D *v3d)
1199 {
1200         View3DAfter *v3da, *next;
1201         
1202         glDepthMask(0);
1203         v3d->transp= TRUE;
1204         
1205         for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1206                 next= v3da->next;
1207                 if(v3da->type==V3D_TRANSP) {
1208                         draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1209                         BLI_remlink(&v3d->afterdraw, v3da);
1210                         MEM_freeN(v3da);
1211                 }
1212         }
1213         v3d->transp= FALSE;
1214         
1215         glDepthMask(1);
1216         
1217 }
1218
1219 /* *********************** */
1220
1221 /*
1222         In most cases call draw_dupli_objects,
1223         draw_dupli_objects_color was added because when drawing set dupli's
1224         we need to force the color
1225  */
1226 static void draw_dupli_objects_color(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int color)
1227 {       
1228         ListBase *lb;
1229         DupliObject *dob;
1230         Base tbase;
1231         BoundBox *bb= NULL;
1232         GLuint displist=0;
1233         short transflag, use_displist= -1;      /* -1 is initialize */
1234         char dt, dtx;
1235         
1236         if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
1237         
1238         tbase.flag= OB_FROMDUPLI|base->flag;
1239         lb= object_duplilist(scene, base->object);
1240         
1241         for(dob= lb->first; dob; dob= dob->next) {
1242                 if(dob->no_draw);
1243                 else {
1244                         tbase.object= dob->ob;
1245                         
1246                         /* extra service: draw the duplicator in drawtype of parent */
1247                         /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
1248                         dt= tbase.object->dt;   tbase.object->dt= MIN2(tbase.object->dt, base->object->dt);
1249                         dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
1250                         
1251                         /* negative scale flag has to propagate */
1252                         transflag= tbase.object->transflag;
1253                         if(base->object->transflag & OB_NEG_SCALE)
1254                                 tbase.object->transflag ^= OB_NEG_SCALE;
1255                         
1256                         UI_ThemeColorBlend(color, TH_BACK, 0.5);
1257                         
1258                         /* generate displist, test for new object */
1259                         if(use_displist==1 && dob->prev && dob->prev->ob!=dob->ob) {
1260                                 use_displist= -1;
1261                                 glDeleteLists(displist, 1);
1262                         }
1263                         /* generate displist */
1264                         if(use_displist == -1) {
1265                                 
1266                                 /* lamp drawing messes with matrices, could be handled smarter... but this works */
1267                                 if(dob->ob->type==OB_LAMP || dob->type==OB_DUPLIGROUP)
1268                                         use_displist= 0;
1269                                 else {
1270                                         /* disable boundbox check for list creation */
1271                                         object_boundbox_flag(dob->ob, OB_BB_DISABLED, 1);
1272                                         /* need this for next part of code */
1273                                         bb= object_get_boundbox(dob->ob);
1274                                         
1275                                         Mat4One(dob->ob->obmat);        /* obmat gets restored */
1276                                         
1277                                         displist= glGenLists(1);
1278                                         glNewList(displist, GL_COMPILE);
1279                                         draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1280                                         glEndList();
1281                                         
1282                                         use_displist= 1;
1283                                         object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
1284                                 }
1285                         }
1286                         if(use_displist) {
1287                                 wmMultMatrix(dob->mat);
1288                                 if(boundbox_clip(v3d, dob->mat, bb))
1289                                         glCallList(displist);
1290                                 wmLoadMatrix(v3d->viewmat);
1291                         }
1292                         else {
1293                                 Mat4CpyMat4(dob->ob->obmat, dob->mat);
1294                                 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1295                         }
1296                         
1297                         tbase.object->dt= dt;
1298                         tbase.object->dtx= dtx;
1299                         tbase.object->transflag= transflag;
1300                 }
1301         }
1302         
1303         /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
1304         
1305         free_object_duplilist(lb);      /* does restore */
1306         
1307         if(use_displist)
1308                 glDeleteLists(displist, 1);
1309 }
1310
1311 static void draw_dupli_objects(Scene *scene, ARegion *ar, View3D *v3d, Base *base)
1312 {
1313         /* define the color here so draw_dupli_objects_color can be called
1314         * from the set loop */
1315         
1316         int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
1317         /* debug */
1318         if(base->object->dup_group && base->object->dup_group->id.us<1)
1319                 color= TH_REDALERT;
1320         
1321         draw_dupli_objects_color(scene, ar, v3d, base, color);
1322 }
1323
1324
1325 void view3d_update_depths(ARegion *ar, View3D *v3d)
1326 {
1327         /* Create storage for, and, if necessary, copy depth buffer */
1328         if(!v3d->depths) v3d->depths= MEM_callocN(sizeof(ViewDepths),"ViewDepths");
1329         if(v3d->depths) {
1330                 ViewDepths *d= v3d->depths;
1331                 if(d->w != ar->winx ||
1332                    d->h != ar->winy ||
1333                    !d->depths) {
1334                         d->w= ar->winx;
1335                         d->h= ar->winy;
1336                         if(d->depths)
1337                                 MEM_freeN(d->depths);
1338                         d->depths= MEM_mallocN(sizeof(float)*d->w*d->h,"View depths");
1339                         d->damaged= 1;
1340                 }
1341                 
1342                 if(d->damaged) {
1343                         glReadPixels(ar->winrct.xmin,ar->winrct.ymin,d->w,d->h,
1344                                                  GL_DEPTH_COMPONENT,GL_FLOAT, d->depths);
1345                         
1346                         glGetDoublev(GL_DEPTH_RANGE,d->depth_range);
1347                         
1348                         d->damaged= 0;
1349                 }
1350         }
1351 }
1352
1353 /* Enable sculpting in wireframe mode by drawing sculpt object only to the depth buffer */
1354 static void draw_sculpt_depths(Scene *scene, ARegion *ar, View3D *v3d)
1355 {
1356         Object *ob = OBACT;
1357         
1358         int dt= MIN2(v3d->drawtype, ob->dt);
1359         if(v3d->zbuf==0 && dt>OB_WIRE)
1360                 dt= OB_WIRE;
1361         if(dt == OB_WIRE) {
1362                 GLboolean depth_on;
1363                 int orig_vdt = v3d->drawtype;
1364                 int orig_zbuf = v3d->zbuf;
1365                 int orig_odt = ob->dt;
1366                 
1367                 glGetBooleanv(GL_DEPTH_TEST, &depth_on);
1368                 v3d->drawtype = ob->dt = OB_SOLID;
1369                 v3d->zbuf = 1;
1370                 
1371                 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1372                 glEnable(GL_DEPTH_TEST);
1373                 draw_object(scene, ar, v3d, BASACT, 0);
1374                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1375                 if(!depth_on)
1376                         glDisable(GL_DEPTH_TEST);
1377                 
1378                 v3d->drawtype = orig_vdt;
1379                 v3d->zbuf = orig_zbuf;
1380                 ob->dt = orig_odt;
1381         }
1382 }
1383
1384 void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (* func)(void *))
1385 {
1386         Base *base;
1387         Scene *sce;
1388         short zbuf, flag;
1389         float glalphaclip;
1390         /* temp set drawtype to solid */
1391         
1392         /* Setting these temporarily is not nice */
1393         zbuf = v3d->zbuf;
1394         flag = v3d->flag;
1395         glalphaclip = U.glalphaclip;
1396         
1397         U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
1398         v3d->flag &= ~V3D_SELECT_OUTLINE;
1399         
1400         setwinmatrixview3d(v3d, ar->winx, ar->winy, NULL);      /* 0= no pick rect */
1401         setviewmatrixview3d(v3d);       /* note: calls where_is_object for camera... */
1402         
1403         Mat4MulMat4(v3d->persmat, v3d->viewmat, v3d->winmat);
1404         Mat4Invert(v3d->persinv, v3d->persmat);
1405         Mat4Invert(v3d->viewinv, v3d->viewmat);
1406         
1407         glClear(GL_DEPTH_BUFFER_BIT);
1408         
1409         wmLoadMatrix(v3d->viewmat);
1410 //      persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
1411         
1412         if(v3d->flag & V3D_CLIPPING) {
1413                 view3d_set_clipping(v3d);
1414         }
1415         
1416         v3d->zbuf= TRUE;
1417         glEnable(GL_DEPTH_TEST);
1418         
1419         /* draw set first */
1420         if(scene->set) {
1421                 for(SETLOOPER(scene->set, base)) {
1422                         if(v3d->lay & base->lay) {
1423                                 if (func == NULL || func(base)) {
1424                                         draw_object(scene, ar, v3d, base, 0);
1425                                         if(base->object->transflag & OB_DUPLI) {
1426                                                 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
1427                                         }
1428                                 }
1429                         }
1430                 }
1431         }
1432         
1433         for(base= scene->base.first; base; base= base->next) {
1434                 if(v3d->lay & base->lay) {
1435                         if (func == NULL || func(base)) {
1436                                 /* dupli drawing */
1437                                 if(base->object->transflag & OB_DUPLI) {
1438                                         draw_dupli_objects(scene, ar, v3d, base);
1439                                 }
1440                                 draw_object(scene, ar, v3d, base, 0);
1441                         }
1442                 }
1443         }
1444         
1445         /* this isnt that nice, draw xray objects as if they are normal */
1446         if (v3d->afterdraw.first) {
1447                 View3DAfter *v3da, *next;
1448                 int num = 0;
1449                 v3d->xray= TRUE;
1450                 
1451                 glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
1452                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1453                         next= v3da->next;
1454                         if(v3da->type==V3D_XRAY) {
1455                                 draw_object(scene, ar, v3d, v3da->base, 0);
1456                                 num++;
1457                         }
1458                         /* dont remove this time */
1459                 }
1460                 v3d->xray= FALSE;
1461                 
1462                 glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
1463                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1464                         next= v3da->next;
1465                         if(v3da->type==V3D_XRAY) {
1466                                 v3d->xray= TRUE; v3d->transp= FALSE;  
1467                         } else if (v3da->type==V3D_TRANSP) {
1468                                 v3d->xray= FALSE; v3d->transp= TRUE;
1469                         }
1470                         
1471                         draw_object(scene, ar, v3d, v3da->base, 0); /* Draw Xray or Transp objects normally */
1472                         BLI_remlink(&v3d->afterdraw, v3da);
1473                         MEM_freeN(v3da);
1474                 }
1475                 v3d->xray= FALSE;
1476                 v3d->transp= FALSE;
1477         }
1478         
1479         v3d->zbuf = zbuf;
1480         U.glalphaclip = glalphaclip;
1481         v3d->flag = flag;
1482 }
1483
1484 typedef struct View3DShadow {
1485         struct View3DShadow *next, *prev;
1486         GPULamp *lamp;
1487 } View3DShadow;
1488
1489 static void gpu_render_lamp_update(Scene *scene, View3D *v3d, Object *ob, Object *par, float obmat[][4], ListBase *shadows)
1490 {
1491         GPULamp *lamp;
1492         View3DShadow *shadow;
1493         
1494         lamp = GPU_lamp_from_blender(scene, ob, par);
1495         
1496         if(lamp) {
1497                 GPU_lamp_update(lamp, ob->lay, obmat);
1498                 
1499                 if((ob->lay & v3d->lay) && GPU_lamp_has_shadow_buffer(lamp)) {
1500                         shadow= MEM_callocN(sizeof(View3DShadow), "View3DShadow");
1501                         shadow->lamp = lamp;
1502                         BLI_addtail(shadows, shadow);
1503                 }
1504         }
1505 }
1506
1507 static void gpu_update_lamps_shadows(Scene *scene, View3D *v3d)
1508 {
1509         ListBase shadows;
1510         View3DShadow *shadow;
1511         Scene *sce;
1512         Base *base;
1513         Object *ob;
1514         
1515         shadows.first= shadows.last= NULL;
1516         
1517         /* update lamp transform and gather shadow lamps */
1518         for(SETLOOPER(scene, base)) {
1519                 ob= base->object;
1520                 
1521                 if(ob->type == OB_LAMP)
1522                         gpu_render_lamp_update(scene, v3d, ob, NULL, ob->obmat, &shadows);
1523                 
1524                 if (ob->transflag & OB_DUPLI) {
1525                         DupliObject *dob;
1526                         ListBase *lb = object_duplilist(scene, ob);
1527                         
1528                         for(dob=lb->first; dob; dob=dob->next)
1529                                 if(dob->ob->type==OB_LAMP)
1530                                         gpu_render_lamp_update(scene, v3d, dob->ob, ob, dob->mat, &shadows);
1531                         
1532                         free_object_duplilist(lb);
1533                 }
1534         }
1535         
1536         /* render shadows after updating all lamps, nested object_duplilist
1537                 * don't work correct since it's replacing object matrices */
1538         for(shadow=shadows.first; shadow; shadow=shadow->next) {
1539                 /* this needs to be done better .. */
1540                 float viewmat[4][4], winmat[4][4];
1541                 int drawtype, lay, winsize, flag2;
1542                 
1543                 drawtype= v3d->drawtype;
1544                 lay= v3d->lay;
1545                 flag2= v3d->flag2 & V3D_SOLID_TEX;
1546                 
1547                 v3d->drawtype = OB_SOLID;
1548                 v3d->lay &= GPU_lamp_shadow_layer(shadow->lamp);
1549                 v3d->flag2 &= ~V3D_SOLID_TEX;
1550                 
1551                 GPU_lamp_shadow_buffer_bind(shadow->lamp, viewmat, &winsize, winmat);
1552 // XXX          drawview3d_render(v3d, viewmat, winsize, winsize, winmat, 1);
1553                 GPU_lamp_shadow_buffer_unbind(shadow->lamp);
1554                 
1555                 v3d->drawtype= drawtype;
1556                 v3d->lay= lay;
1557                 v3d->flag2 |= flag2;
1558         }
1559         
1560         BLI_freelistN(&shadows);
1561 }
1562
1563
1564 void drawview3dspace(Scene *scene, ARegion *ar, View3D *v3d)
1565 {
1566         Scene *sce;
1567         Base *base;
1568         Object *ob;
1569         char retopo= 0, sculptparticle= 0;
1570         Object *obact = OBACT;
1571         
1572         /* update all objects, ipos, matrices, displists, etc. Flags set by depgraph or manual, 
1573                 no layer check here, gets correct flushed */
1574         /* sets first, we allow per definition current scene to have dependencies on sets */
1575         if(scene->set) {
1576                 for(SETLOOPER(scene->set, base))
1577                         object_handle_update(base->object);   // bke_object.h
1578         }
1579         
1580         v3d->lay_used = 0;
1581         for(base= scene->base.first; base; base= base->next) {
1582                 object_handle_update(base->object);   // bke_object.h
1583                 v3d->lay_used |= base->lay;
1584         }
1585         
1586         /* shadow buffers, before we setup matrices */
1587         if(draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
1588                 gpu_update_lamps_shadows(scene, v3d);
1589         
1590         setwinmatrixview3d(v3d, ar->winx, ar->winy, NULL);      /* 0= no pick rect */
1591         setviewmatrixview3d(v3d);       /* note: calls where_is_object for camera... */
1592         
1593         Mat4MulMat4(v3d->persmat, v3d->viewmat, v3d->winmat);
1594         Mat4Invert(v3d->persinv, v3d->persmat);
1595         Mat4Invert(v3d->viewinv, v3d->viewmat);
1596         
1597         /* calculate pixelsize factor once, is used for lamps and obcenters */
1598         {
1599                 float len1, len2, vec[3];
1600                 
1601                 VECCOPY(vec, v3d->persinv[0]);
1602                 len1= Normalize(vec);
1603                 VECCOPY(vec, v3d->persinv[1]);
1604                 len2= Normalize(vec);
1605                 
1606                 v3d->pixsize= 2.0f*(len1>len2?len1:len2);
1607                 
1608                 /* correct for window size */
1609                 if(ar->winx > ar->winy) v3d->pixsize/= (float)ar->winx;
1610                 else v3d->pixsize/= (float)ar->winy;
1611         }
1612         
1613         if(v3d->drawtype > OB_WIRE) {
1614                 float col[3];
1615                 UI_GetThemeColor3fv(TH_BACK, col);
1616                 glClearColor(col[0], col[1], col[2], 0.0); 
1617                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1618                 
1619                 glLoadIdentity();
1620         }
1621         else {
1622                 float col[3];
1623                 UI_GetThemeColor3fv(TH_BACK, col);
1624                 glClearColor(col[0], col[1], col[2], 0.0);
1625                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1626         }
1627         
1628         wmLoadMatrix(v3d->viewmat);
1629         
1630         if(v3d->flag & V3D_CLIPPING)
1631                 view3d_draw_clipping(v3d);
1632         
1633         /* set zbuffer after we draw clipping region */
1634         if(v3d->drawtype > OB_WIRE) {
1635                 v3d->zbuf= TRUE;
1636                 glEnable(GL_DEPTH_TEST);
1637         }
1638         
1639         // needs to be done always, gridview is adjusted in drawgrid() now
1640         v3d->gridview= v3d->grid;
1641         
1642         if(v3d->view==0 || v3d->persp!=0) {
1643                 drawfloor(v3d);
1644                 if(v3d->persp==2) {
1645                         if(scene->world) {
1646                                 if(scene->world->mode & WO_STARS) {
1647                                         RE_make_stars(NULL, star_stuff_init_func, star_stuff_vertex_func,
1648                                                                   star_stuff_term_func);
1649                                 }
1650                         }
1651                         if(v3d->flag & V3D_DISPBGPIC) draw_bgpic(scene, ar, v3d);
1652                 }
1653         }
1654         else {
1655                 ED_region_pixelspace(ar);
1656                 drawgrid(ar, v3d);
1657                 /* XXX make function? replaces persp(1) */
1658                 glMatrixMode(GL_PROJECTION);
1659                 wmLoadMatrix(v3d->winmat);
1660                 glMatrixMode(GL_MODELVIEW);
1661                 wmLoadMatrix(v3d->viewmat);
1662                 
1663                 if(v3d->flag & V3D_DISPBGPIC) {
1664                         draw_bgpic(scene, ar, v3d);
1665                 }
1666         }
1667         
1668         if(v3d->flag & V3D_CLIPPING)
1669                 view3d_set_clipping(v3d);
1670         
1671         /* draw set first */
1672         if(scene->set) {
1673                 for(SETLOOPER(scene->set, base)) {
1674                         
1675                         if(v3d->lay & base->lay) {
1676                                 
1677                                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
1678                                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
1679                                 
1680                                 if(base->object->transflag & OB_DUPLI) {
1681                                         draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
1682                                 }
1683                         }
1684                 }
1685                 
1686                 /* Transp and X-ray afterdraw stuff for sets is done later */
1687         }
1688         
1689         /* then draw not selected and the duplis, but skip editmode object */
1690         for(base= scene->base.first; base; base= base->next) {
1691                 if(v3d->lay & base->lay) {
1692                         
1693                         /* dupli drawing */
1694                         if(base->object->transflag & OB_DUPLI) {
1695                                 draw_dupli_objects(scene, ar, v3d, base);
1696                         }
1697                         if((base->flag & SELECT)==0) {
1698                                 if(base->object!=G.obedit) 
1699                                         draw_object(scene, ar, v3d, base, 0);
1700                         }
1701                 }
1702         }
1703         
1704 //      retopo= retopo_mesh_check() || retopo_curve_check();
1705 //      sculptparticle= (G.f & (G_SCULPTMODE|G_PARTICLEEDIT)) && !G.obedit;
1706         if(retopo)
1707                 view3d_update_depths(ar, v3d);
1708         
1709         /* draw selected and editmode */
1710         for(base= scene->base.first; base; base= base->next) {
1711                 if(v3d->lay & base->lay) {
1712                         if (base->object==G.obedit || ( base->flag & SELECT) ) 
1713                                 draw_object(scene, ar, v3d, base, 0);
1714                 }
1715         }
1716         
1717         if(!retopo && sculptparticle && !(obact && (obact->dtx & OB_DRAWXRAY))) {
1718                 if(G.f & G_SCULPTMODE)
1719                         draw_sculpt_depths(scene, ar, v3d);
1720                 view3d_update_depths(ar, v3d);
1721         }
1722         
1723         if(G.moving) {
1724 //              BIF_drawConstraint();
1725 //              if(G.obedit || (G.f & G_PARTICLEEDIT))
1726 //                      BIF_drawPropCircle(); // only editmode and particles have proportional edit
1727 //              BIF_drawSnap();
1728         }
1729         
1730 //      REEB_draw();
1731         
1732 //      if(scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
1733         
1734         /* Transp and X-ray afterdraw stuff */
1735         view3d_draw_transp(scene, ar, v3d);
1736         view3d_draw_xray(scene, ar, v3d, 1);    // clears zbuffer if it is used!
1737         
1738         if(!retopo && sculptparticle && (obact && (OBACT->dtx & OB_DRAWXRAY))) {
1739                 if(G.f & G_SCULPTMODE)
1740                         draw_sculpt_depths(scene, ar, v3d);
1741                 view3d_update_depths(ar, v3d);
1742         }
1743         
1744         if(v3d->flag & V3D_CLIPPING)
1745                 view3d_clr_clipping();
1746         
1747 //      BIF_draw_manipulator(ar);
1748         
1749         if(v3d->zbuf) {
1750                 v3d->zbuf= FALSE;
1751                 glDisable(GL_DEPTH_TEST);
1752         }
1753         
1754         /* draw grease-pencil stuff */
1755 //      if (v3d->flag2 & V3D_DISPGP)
1756 //              draw_gpencil_3dview(ar, 1);
1757         
1758         ED_region_pixelspace(ar);
1759         
1760         /* Draw Sculpt Mode brush XXX (removed) */
1761         
1762 //      retopo_paint_view_update(v3d);
1763 //      retopo_draw_paint_lines();
1764         
1765         /* Draw particle edit brush XXX (removed) */
1766         
1767         if(v3d->persp>1) drawviewborder(scene, ar, v3d);
1768         if(v3d->flag2 & V3D_FLYMODE) drawviewborder_flymode(ar);
1769         
1770         /* draw grease-pencil stuff */
1771 //      if (v3d->flag2 & V3D_DISPGP)
1772 //              draw_gpencil_3dview(ar, 0);
1773
1774         if(!(G.f & G_PLAYANIM)) drawcursor(scene, ar, v3d);
1775         if(U.uiflag & USER_SHOW_ROTVIEWICON)
1776                 draw_view_axis(v3d);
1777         else    
1778                 draw_view_icon(v3d);
1779         
1780         /* XXX removed viewport fps */
1781         if(U.uiflag & USER_SHOW_VIEWPORTNAME) {
1782                 draw_viewport_name(ar, v3d);
1783         }
1784         
1785         ob= OBACT;
1786         if(U.uiflag & USER_DRAWVIEWINFO) 
1787                 draw_selected_name(scene, ob, v3d);
1788         
1789 //      draw_area_emboss(ar);
1790         
1791         /* XXX here was the blockhandlers for floating panels */
1792
1793         if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT || G.f & G_TEXTUREPAINT) {
1794                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
1795                 // XXX addafterqueue(ar->win, BACKBUFDRAW, 1);
1796         }
1797         // test for backbuf select
1798         if(G.obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT)) {
1799                 
1800                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
1801                 // XXX if(afterqtest(ar->win, BACKBUFDRAW)==0) {
1802                 //      addafterqueue(ar->win, BACKBUFDRAW, 1);
1803                 //}
1804         }
1805         
1806 #ifndef DISABLE_PYTHON
1807         /* XXX here was scriptlink */
1808 #endif  
1809 }
1810
1811