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