4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
33 #include "DNA_action_types.h"
34 #include "DNA_armature_types.h"
35 #include "DNA_camera_types.h"
36 #include "DNA_customdata_types.h"
37 #include "DNA_group_types.h"
38 #include "DNA_key_types.h"
39 #include "DNA_lamp_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_space_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_userdef_types.h"
45 #include "DNA_view3d_types.h"
46 #include "DNA_world_types.h"
48 #include "MEM_guardedalloc.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"
55 #include "BKE_context.h"
56 #include "BKE_customdata.h"
57 #include "BKE_image.h"
60 #include "BKE_object.h"
61 #include "BKE_global.h"
62 #include "BKE_scene.h"
63 #include "BKE_screen.h"
64 #include "BKE_utildefines.h"
67 #include "RE_pipeline.h" // make_stars
69 #include "IMB_imbuf_types.h"
70 #include "IMB_imbuf.h"
73 #include "BIF_glutil.h"
78 #include "ED_armature.h"
79 #include "ED_keyframing.h"
81 #include "ED_screen.h"
82 #include "ED_space_api.h"
84 #include "ED_transform.h"
87 #include "UI_interface.h"
88 #include "UI_interface_icons.h"
89 #include "UI_resources.h"
90 #include "UI_view2d.h"
93 #include "GPU_material.h"
95 #include "view3d_intern.h" // own include
99 static void star_stuff_init_func(void)
105 static void star_stuff_vertex_func(float* i)
109 static void star_stuff_term_func(void)
114 void circf(float x, float y, float rad)
116 GLUquadricObj *qobj = gluNewQuadric();
118 gluQuadricDrawStyle(qobj, GLU_FILL);
122 glTranslatef(x, y, 0.);
124 gluDisk( qobj, 0.0, rad, 32, 1);
128 gluDeleteQuadric(qobj);
131 void circ(float x, float y, float rad)
133 GLUquadricObj *qobj = gluNewQuadric();
135 gluQuadricDrawStyle(qobj, GLU_SILHOUETTE);
139 glTranslatef(x, y, 0.);
141 gluDisk( qobj, 0.0, rad, 32, 1);
145 gluDeleteQuadric(qobj);
149 /* ********* custom clipping *********** */
151 static void view3d_draw_clipping(RegionView3D *rv3d)
153 BoundBox *bb= rv3d->clipbb;
156 UI_ThemeColorShade(TH_BACK, -8);
160 glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[3]);
161 glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[1]);
162 glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[5]);
163 glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[3]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[6]);
164 glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[2]);
165 glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[3]);
171 void view3d_set_clipping(RegionView3D *rv3d)
176 if(rv3d->viewlock) tot= 6;
178 for(a=0; a<tot; a++) {
179 QUATCOPY(plane, rv3d->clip[a]);
180 glClipPlane(GL_CLIP_PLANE0+a, plane);
181 glEnable(GL_CLIP_PLANE0+a);
185 void view3d_clr_clipping(void)
190 glDisable(GL_CLIP_PLANE0+a);
194 int view3d_test_clipping(RegionView3D *rv3d, float *vec)
196 /* vec in world coordinates, returns 1 if clipped */
201 if(0.0f < rv3d->clip[0][3] + INPR(view, rv3d->clip[0]))
202 if(0.0f < rv3d->clip[1][3] + INPR(view, rv3d->clip[1]))
203 if(0.0f < rv3d->clip[2][3] + INPR(view, rv3d->clip[2]))
204 if(0.0f < rv3d->clip[3][3] + INPR(view, rv3d->clip[3]))
210 /* ********* end custom clipping *********** */
213 static void drawgrid_draw(ARegion *ar, float wx, float wy, float x, float y, float dx)
222 while(fx< ar->winx) {
223 fdrawline(fx, 0.0, fx, (float)ar->winy);
231 while(fy< ar->winy) {
232 fdrawline(0.0, fy, (float)ar->winx, fy);
238 #define GRID_MIN_PX 6.0f
240 static void drawgrid(UnitSettings *unit, ARegion *ar, View3D *v3d, char **grid_unit)
242 /* extern short bgpicmode; */
243 RegionView3D *rv3d= ar->regiondata;
244 float wx, wy, x, y, fw, fx, fy, dx;
246 char col[3], col2[3];
250 vec4[0]=vec4[1]=vec4[2]=0.0;
252 Mat4MulVec4fl(rv3d->persmat, vec4);
257 wx= (ar->winx/2.0); /* because of rounding errors, grid at wrong location */
263 vec4[0]=vec4[1]= (unit->system) ? 1.0 : v3d->grid;
267 Mat4MulVec4fl(rv3d->persmat, vec4);
272 dx= fabs(x-(wx)*fx/fw);
273 if(dx==0) dx= fabs(y-(wy)*fy/fw);
275 glDepthMask(0); // disable write in zbuffer
278 UI_ThemeColor(TH_GRID);
281 /* Use GRID_MIN_PX*2 for units because very very small grid
282 * items are less useful when dealing with units */
289 bUnit_GetSystem(&usys, &len, unit->system, B_UNIT_LENGTH);
294 scalar= bUnit_GetScaler(usys, i);
296 dx_scalar = dx * scalar * unit->scale_length;
297 if (dx_scalar < (GRID_MIN_PX*2))
300 /* Store the smallest drawn grid size units name so users know how big each grid cell is */
301 if(*grid_unit==NULL) {
302 *grid_unit= bUnit_GetNamePlural(usys, i);
303 v3d->gridview= (scalar * unit->scale_length);
305 blend_fac= 1-((GRID_MIN_PX*2)/dx_scalar);
307 /* tweak to have the fade a bit nicer */
308 blend_fac= (blend_fac * blend_fac) * 2.0f;
309 CLAMP(blend_fac, 0.3f, 1.0f);
312 UI_ThemeColorBlend(TH_BACK, TH_GRID, blend_fac);
314 drawgrid_draw(ar, wx, wy, x, y, dx_scalar);
319 short sublines = v3d->gridsubdiv;
322 v3d->gridview*= sublines;
326 v3d->gridview*= sublines;
330 v3d->gridview*= sublines;
334 UI_ThemeColor(TH_GRID);
335 drawgrid_draw(ar, wx, wy, x, y, dx);
338 else { // start blending out
339 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*10));
340 drawgrid_draw(ar, wx, wy, x, y, dx);
342 UI_ThemeColor(TH_GRID);
343 drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
346 else { // start blending out (GRID_MIN_PX < dx < (GRID_MIN_PX*10))
347 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*10));
348 drawgrid_draw(ar, wx, wy, x, y, dx);
350 UI_ThemeColor(TH_GRID);
351 drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
355 if(dx>(GRID_MIN_PX*10)) { // start blending in
356 v3d->gridview/= sublines;
358 if(dx>(GRID_MIN_PX*10)) { // start blending in
359 v3d->gridview/= sublines;
361 if(dx>(GRID_MIN_PX*10)) {
362 UI_ThemeColor(TH_GRID);
363 drawgrid_draw(ar, wx, wy, x, y, dx);
366 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*10));
367 drawgrid_draw(ar, wx, wy, x, y, dx);
368 UI_ThemeColor(TH_GRID);
369 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
373 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*10));
374 drawgrid_draw(ar, wx, wy, x, y, dx);
375 UI_ThemeColor(TH_GRID);
376 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
380 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*10));
381 drawgrid_draw(ar, wx, wy, x, y, dx);
382 UI_ThemeColor(TH_GRID);
383 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
391 UI_GetThemeColor3ubv(TH_GRID, col);
396 if( ELEM(rv3d->view, V3D_VIEW_RIGHT, V3D_VIEW_LEFT))
397 UI_make_axis_color(col, col2, 'y');
398 else UI_make_axis_color(col, col2, 'x');
399 glColor3ubv((GLubyte *)col2);
401 fdrawline(0.0, y, (float)ar->winx, y);
403 if( ELEM(rv3d->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM))
404 UI_make_axis_color(col, col2, 'y');
405 else UI_make_axis_color(col, col2, 'z');
406 glColor3ubv((GLubyte *)col2);
408 fdrawline(x, 0.0, x, (float)ar->winy);
410 glDepthMask(1); // enable write in zbuffer
414 static void drawfloor(Scene *scene, View3D *v3d)
417 int a, gridlines, emphasise;
418 char col[3], col2[3];
423 if(v3d->gridlines<3) return;
425 if(v3d->zbuf && scene->obedit) glDepthMask(0); // for zbuffer-select
427 gridlines= v3d->gridlines/2;
428 grid= gridlines*v3d->grid;
430 UI_GetThemeColor3ubv(TH_GRID, col);
431 UI_GetThemeColor3ubv(TH_BACK, col2);
433 /* emphasise division lines lighter instead of darker, if background is darker than grid */
434 if ( ((col[0]+col[1]+col[2])/3+10) > (col2[0]+col2[1]+col2[2])/3 )
439 /* draw the Y axis and/or grid lines */
440 for(a= -gridlines;a<=gridlines;a++) {
442 /* check for the 'show Y axis' preference */
443 if (v3d->gridflag & V3D_SHOW_Y) {
444 UI_make_axis_color(col, col2, 'y');
445 glColor3ubv((GLubyte *)col2);
448 } else if (v3d->gridflag & V3D_SHOW_FLOOR) {
449 UI_ThemeColorShade(TH_GRID, emphasise);
454 /* check for the 'show grid floor' preference */
455 if (v3d->gridflag & V3D_SHOW_FLOOR) {
457 UI_ThemeColorShade(TH_GRID, emphasise);
459 else UI_ThemeColorShade(TH_GRID, 10);
468 glBegin(GL_LINE_STRIP);
469 vert[0]= a*v3d->grid;
478 /* draw the X axis and/or grid lines */
479 for(a= -gridlines;a<=gridlines;a++) {
481 /* check for the 'show X axis' preference */
482 if (v3d->gridflag & V3D_SHOW_X) {
483 UI_make_axis_color(col, col2, 'x');
484 glColor3ubv((GLubyte *)col2);
487 } else if (v3d->gridflag & V3D_SHOW_FLOOR) {
488 UI_ThemeColorShade(TH_GRID, emphasise);
493 /* check for the 'show grid floor' preference */
494 if (v3d->gridflag & V3D_SHOW_FLOOR) {
496 UI_ThemeColorShade(TH_GRID, emphasise);
498 else UI_ThemeColorShade(TH_GRID, 10);
507 glBegin(GL_LINE_STRIP);
508 vert[1]= a*v3d->grid;
517 /* draw the Z axis line */
518 /* check for the 'show Z axis' preference */
519 if (v3d->gridflag & V3D_SHOW_Z) {
520 UI_make_axis_color(col, col2, 'z');
521 glColor3ubv((GLubyte *)col2);
523 glBegin(GL_LINE_STRIP);
533 if(v3d->zbuf && scene->obedit) glDepthMask(1);
537 static void drawcursor(Scene *scene, ARegion *ar, View3D *v3d)
542 /* we dont want the clipping for cursor */
545 project_short(ar, give_cursor(scene, v3d), co);
554 circ((float)mx, (float)my, 10.0);
557 circ((float)mx, (float)my, 10.0);
561 sdrawline(mx-20, my, mx-5, my);
562 sdrawline(mx+5, my, mx+20, my);
563 sdrawline(mx, my-20, mx, my-5);
564 sdrawline(mx, my+5, mx, my+20);
568 /* Draw a live substitute of the view icon, which is always shown */
569 static void draw_view_axis(RegionView3D *rv3d)
571 const float k = U.rvisize; /* axis size */
572 const float toll = 0.5; /* used to see when view is quasi-orthogonal */
573 const float start = k + 1.0; /* axis center in screen coordinates, x=y */
574 float ydisp = 0.0; /* vertical displacement to allow obj info text */
576 /* rvibright ranges approx. from original axis icon color to gizmo color */
577 float bright = U.rvibright / 15.0f;
579 unsigned char col[3];
580 unsigned char gridcol[3];
587 /* thickness of lines is proportional to k */
588 /* (log(k)-1) gives a more suitable thickness, but fps decreased by about 3 fps */
590 //glLineWidth(log(k)-1); // a bit slow
592 UI_GetThemeColor3ubv(TH_GRID, (char *)gridcol);
597 QuatMulVecf(rv3d->viewquat, vec);
599 UI_make_axis_color((char *)gridcol, (char *)col, 'x');
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;
603 v = (v<1.0-(bright) ? v+bright : 1.0);
604 hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
609 fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
610 if (fabs(dx) > toll || fabs(dy) > toll) {
611 BLF_draw_default(start + dx + 2, start + dy + ydisp + 2, 0.0f, "x");
617 QuatMulVecf(rv3d->viewquat, vec);
619 UI_make_axis_color((char *)gridcol, (char *)col, 'y');
620 rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
621 s = s<0.5 ? s+0.5 : 1.0;
623 v = (v<1.0-(bright) ? v+bright : 1.0);
624 hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
629 fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
630 if (fabs(dx) > toll || fabs(dy) > toll) {
631 BLF_draw_default(start + dx + 2, start + dy + ydisp + 2, 0.0f, "y");
637 QuatMulVecf(rv3d->viewquat, vec);
639 UI_make_axis_color((char *)gridcol, (char *)col, 'z');
640 rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
641 s = s<0.5 ? s+0.5 : 1.0;
643 v = (v<1.0-(bright) ? v+bright : 1.0);
644 hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
649 fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
650 if (fabs(dx) > toll || fabs(dy) > toll) {
651 BLF_draw_default(start + dx + 2, start + dy + ydisp + 2, 0.0f, "z");
654 /* restore line-width */
659 static void draw_view_icon(RegionView3D *rv3d)
663 if( ELEM(rv3d->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM))
665 else if( ELEM(rv3d->view, V3D_VIEW_FRONT, V3D_VIEW_BACK))
666 icon= ICON_AXIS_FRONT;
667 else if( ELEM(rv3d->view, V3D_VIEW_RIGHT, V3D_VIEW_LEFT))
668 icon= ICON_AXIS_SIDE;
672 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
674 UI_icon_draw(5.0, 5.0, icon);
679 static char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
683 switch (rv3d->view) {
685 if (rv3d->persp == V3D_ORTHO) name = "Front Ortho";
686 else name = "Front Persp";
689 if (rv3d->persp == V3D_ORTHO) name = "Back Ortho";
690 else name = "Back Persp";
693 if (rv3d->persp == V3D_ORTHO) name = "Top Ortho";
694 else name = "Top Persp";
696 case V3D_VIEW_BOTTOM:
697 if (rv3d->persp == V3D_ORTHO) name = "Bottom Ortho";
698 else name = "Bottom Persp";
701 if (rv3d->persp == V3D_ORTHO) name = "Right Ortho";
702 else name = "Right Persp";
705 if (rv3d->persp == V3D_ORTHO) name = "Left Ortho";
706 else name = "Left Persp";
710 if (rv3d->persp==V3D_CAMOB) {
711 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
713 cam = v3d->camera->data;
714 name = (cam->type != CAM_ORTHO) ? "Camera Persp" : "Camera Ortho";
716 name = "Object as Camera";
719 name = (rv3d->persp == V3D_ORTHO) ? "User Ortho" : "User Persp";
727 static void draw_viewport_name(ARegion *ar, View3D *v3d)
729 RegionView3D *rv3d= ar->regiondata;
730 char *name = view3d_get_name(v3d, rv3d);
731 char *printable = NULL;
733 if (v3d->localview) {
734 printable = malloc(strlen(name) + strlen(" (Local)_")); /* '_' gives space for '\0' */
735 strcpy(printable, name);
736 strcat(printable, " (Local)");
742 UI_ThemeColor(TH_TEXT_HI);
743 BLF_draw_default(10, ar->winy-20, 0.0f, printable);
746 if (v3d->localview) {
752 static char *get_cfra_marker_name(Scene *scene)
754 ListBase *markers= &scene->markers;
757 /* search through markers for match */
758 for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
772 /* draw info beside axes in bottom left-corner:
773 * framenum, object name, bone name (if available), marker name (if available)
775 static void draw_selected_name(Scene *scene, Object *ob, View3D *v3d)
777 char info[256], *markern;
780 /* get name of marker on current frame (if available) */
781 markern= get_cfra_marker_name(scene);
783 /* check if there is an object */
785 /* name(s) to display depends on type of object */
786 if(ob->type==OB_ARMATURE) {
787 bArmature *arm= ob->data;
790 /* show name of active bone too (if possible) */
793 for (ebo=arm->edbo->first; ebo; ebo=ebo->next){
794 if ((ebo->flag & BONE_ACTIVE) && (ebo->layer & arm->layer)) {
800 else if(ob->pose && (ob->flag & OB_POSEMODE)) {
802 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
803 if((pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer)) {
810 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, name, markern);
812 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, name);
814 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
816 else if(ELEM3(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
821 /* try to display active shapekey too */
823 key = ob_get_key(ob);
825 kb = BLI_findlink(&key->block, ob->shapenr-1);
827 sprintf(shapes, ": %s ", kb->name);
828 if(ob->shapeflag == OB_SHAPE_LOCK){
829 sprintf(shapes, "%s (Pinned)",shapes);
835 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, shapes, markern);
837 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, shapes);
840 /* standard object */
842 sprintf(info, "(%d) %s <%s>", CFRA, ob->id.name+2, markern);
844 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
847 /* colour depends on whether there is a keyframe */
848 if (id_frame_has_keyframe((ID *)ob, /*frame_to_float(scene, CFRA)*/(float)(CFRA), v3d->keyflags))
849 UI_ThemeColor(TH_VERTEX_SELECT);
851 UI_ThemeColor(TH_TEXT_HI);
856 sprintf(info, "(%d) <%s>", CFRA, markern);
858 sprintf(info, "(%d)", CFRA);
860 /* colour is always white */
861 UI_ThemeColor(TH_TEXT_HI);
864 if (U.uiflag & USER_SHOW_ROTVIEWICON)
865 offset = 14 + (U.rvisize * 2);
867 BLF_draw_default(offset, 10, 0.0f, info);
870 static void view3d_get_viewborder_size(Scene *scene, ARegion *ar, float size_r[2])
872 float winmax= MAX2(ar->winx, ar->winy);
873 float aspect= (float) (scene->r.xsch*scene->r.xasp)/(scene->r.ysch*scene->r.yasp);
877 size_r[1]= winmax/aspect;
879 size_r[0]= winmax*aspect;
884 void calc_viewborder(Scene *scene, ARegion *ar, View3D *v3d, rctf *viewborder_r)
886 RegionView3D *rv3d= ar->regiondata;
887 float zoomfac, size[2];
888 float dx= 0.0f, dy= 0.0f;
890 view3d_get_viewborder_size(scene, ar, size);
892 /* magic zoom calculation, no idea what
893 * it signifies, if you find out, tell me! -zr
895 /* simple, its magic dude!
896 * well, to be honest, this gives a natural feeling zooming
897 * with multiple keypad presses (ton)
900 zoomfac= (M_SQRT2 + rv3d->camzoom/50.0);
901 zoomfac= (zoomfac*zoomfac)*0.25;
903 size[0]= size[0]*zoomfac;
904 size[1]= size[1]*zoomfac;
906 /* center in window */
907 viewborder_r->xmin= 0.5*ar->winx - 0.5*size[0];
908 viewborder_r->ymin= 0.5*ar->winy - 0.5*size[1];
909 viewborder_r->xmax= viewborder_r->xmin + size[0];
910 viewborder_r->ymax= viewborder_r->ymin + size[1];
912 dx= ar->winx*rv3d->camdx*zoomfac*2.0f;
913 dy= ar->winy*rv3d->camdy*zoomfac*2.0f;
916 viewborder_r->xmin-= dx;
917 viewborder_r->ymin-= dy;
918 viewborder_r->xmax-= dx;
919 viewborder_r->ymax-= dy;
921 if(v3d->camera && v3d->camera->type==OB_CAMERA) {
922 Camera *cam= v3d->camera->data;
923 float w = viewborder_r->xmax - viewborder_r->xmin;
924 float h = viewborder_r->ymax - viewborder_r->ymin;
925 float side = MAX2(w, h);
927 viewborder_r->xmin+= cam->shiftx*side;
928 viewborder_r->xmax+= cam->shiftx*side;
929 viewborder_r->ymin+= cam->shifty*side;
930 viewborder_r->ymax+= cam->shifty*side;
934 void view3d_set_1_to_1_viewborder(Scene *scene, ARegion *ar)
936 RegionView3D *rv3d= ar->regiondata;
938 int im_width= (scene->r.size*scene->r.xsch)/100;
940 view3d_get_viewborder_size(scene, ar, size);
942 rv3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
943 rv3d->camzoom= CLAMPIS(rv3d->camzoom, -30, 300);
947 static void drawviewborder_flymode(ARegion *ar)
949 /* draws 4 edge brackets that frame the safe area where the
950 mouse can move during fly mode without spinning the view */
951 float x1, x2, y1, y2;
953 x1= 0.45*(float)ar->winx;
954 y1= 0.45*(float)ar->winy;
955 x2= 0.55*(float)ar->winx;
956 y2= 0.55*(float)ar->winy;
992 static void drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
994 extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad); // interface_panel.c
996 float x1, x2, y1, y2;
997 float x3, y3, x4, y4;
1001 if(v3d->camera==NULL)
1003 if(v3d->camera->type==OB_CAMERA)
1004 ca = v3d->camera->data;
1006 calc_viewborder(scene, ar, v3d, &viewborder);
1007 x1= viewborder.xmin;
1008 y1= viewborder.ymin;
1009 x2= viewborder.xmax;
1010 y2= viewborder.ymax;
1012 /* passepartout, specified in camera edit buttons */
1013 if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001) {
1014 if (ca->passepartalpha == 1.0) {
1017 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1019 glColor4f(0, 0, 0, ca->passepartalpha);
1022 glRectf(0.0, (float)ar->winy, x1, 0.0);
1023 if (x2 < (float)ar->winx)
1024 glRectf(x2, (float)ar->winy, (float)ar->winx, 0.0);
1025 if (y2 < (float)ar->winy)
1026 glRectf(x1, (float)ar->winy, x2, y2);
1028 glRectf(x1, y1, x2, 0.0);
1030 glDisable(GL_BLEND);
1034 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1037 UI_ThemeColor(TH_BACK);
1038 glRectf(x1, y1, x2, y2);
1041 UI_ThemeColor(TH_WIRE);
1042 glRectf(x1, y1, x2, y2);
1044 /* camera name - draw in highlighted text color */
1045 if (ca && (ca->flag & CAM_SHOWNAME)) {
1046 UI_ThemeColor(TH_TEXT_HI);
1047 BLF_draw_default(x1, y1-15, 0.0f, v3d->camera->id.name+2);
1048 UI_ThemeColor(TH_WIRE);
1053 if(scene->r.mode & R_BORDER) {
1056 x3= x1+ scene->r.border.xmin*(x2-x1);
1057 y3= y1+ scene->r.border.ymin*(y2-y1);
1058 x4= x1+ scene->r.border.xmax*(x2-x1);
1059 y4= y1+ scene->r.border.ymax*(y2-y1);
1062 glRectf(x3, y3, x4, y4);
1066 if (ca && (ca->flag & CAM_SHOWTITLESAFE)) {
1077 UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1080 gl_round_box(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
1084 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1088 /* *********************** backdraw for selection *************** */
1090 void backdrawview3d(Scene *scene, ARegion *ar, View3D *v3d)
1092 RegionView3D *rv3d= ar->regiondata;
1095 /*for 2.43 release, don't use glext and just define the constant.
1096 this to avoid possibly breaking platforms before release.*/
1097 #ifndef GL_MULTISAMPLE_ARB
1098 #define GL_MULTISAMPLE_ARB 0x809D
1101 #ifdef GL_MULTISAMPLE_ARB
1105 if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT || (FACESEL_PAINT_TEST));
1106 else if((G.f & G_TEXTUREPAINT) && scene->toolsettings && (scene->toolsettings->imapaint.flag & IMAGEPAINT_PROJECT_DISABLE));
1107 else if((G.f & G_PARTICLEEDIT) && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT));
1108 else if(scene->obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT));
1110 v3d->flag &= ~V3D_NEEDBACKBUFDRAW;
1114 if( !(v3d->flag & V3D_NEEDBACKBUFDRAW) ) return;
1118 // addafterqueue(ar->win, BACKBUFDRAW, 1);
1123 /* Disable FSAA for backbuffer selection.
1125 Only works if GL_MULTISAMPLE_ARB is defined by the header
1126 file, which is should be for every OS that supports FSAA.*/
1128 #ifdef GL_MULTISAMPLE_ARB
1129 m = glIsEnabled(GL_MULTISAMPLE_ARB);
1130 if (m) glDisable(GL_MULTISAMPLE_ARB);
1133 if(v3d->drawtype > OB_WIRE) v3d->zbuf= TRUE;
1135 glDisable(GL_DITHER);
1137 glClearColor(0.0, 0.0, 0.0, 0.0);
1139 glEnable(GL_DEPTH_TEST);
1140 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1143 glClear(GL_COLOR_BUFFER_BIT);
1144 glDisable(GL_DEPTH_TEST);
1147 if(rv3d->rflag & RV3D_CLIPPING)
1148 view3d_set_clipping(rv3d);
1150 G.f |= G_BACKBUFSEL;
1152 base= (scene->basact);
1153 if(base && (base->lay & v3d->lay)) {
1154 draw_object_backbufsel(scene, v3d, rv3d, base->object);
1157 v3d->flag &= ~V3D_NEEDBACKBUFDRAW;
1159 G.f &= ~G_BACKBUFSEL;
1161 glDisable(GL_DEPTH_TEST);
1162 glEnable(GL_DITHER);
1164 if(rv3d->rflag & RV3D_CLIPPING)
1165 view3d_clr_clipping();
1167 #ifdef GL_MULTISAMPLE_ARB
1168 if (m) glEnable(GL_MULTISAMPLE_ARB);
1171 /* it is important to end a view in a transform compatible with buttons */
1172 // persp(PERSP_WIN); // set ortho
1176 void view3d_validate_backbuf(ViewContext *vc)
1178 if(vc->v3d->flag & V3D_NEEDBACKBUFDRAW)
1179 backdrawview3d(vc->scene, vc->ar, vc->v3d);
1182 /* samples a single pixel (copied from vpaint) */
1183 unsigned int view3d_sample_backbuf(ViewContext *vc, int x, int y)
1187 if(x >= vc->ar->winx || y >= vc->ar->winy) return 0;
1188 x+= vc->ar->winrct.xmin;
1189 y+= vc->ar->winrct.ymin;
1191 view3d_validate_backbuf(vc);
1193 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
1194 glReadBuffer(GL_BACK);
1196 if(ENDIAN_ORDER==B_ENDIAN) SWITCH_INT(col);
1198 return WM_framebuffer_to_index(col);
1201 /* reads full rect, converts indices */
1202 ImBuf *view3d_read_backbuf(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
1204 unsigned int *dr, *rd;
1205 struct ImBuf *ibuf, *ibuf1;
1207 short xminc, yminc, xmaxc, ymaxc, xs, ys;
1210 if(xmin<0) xminc= 0; else xminc= xmin;
1211 if(xmax >= vc->ar->winx) xmaxc= vc->ar->winx-1; else xmaxc= xmax;
1212 if(xminc > xmaxc) return NULL;
1214 if(ymin<0) yminc= 0; else yminc= ymin;
1215 if(ymax >= vc->ar->winy) ymaxc= vc->ar->winy-1; else ymaxc= ymax;
1216 if(yminc > ymaxc) return NULL;
1218 ibuf= IMB_allocImBuf((xmaxc-xminc+1), (ymaxc-yminc+1), 32, IB_rect,0);
1220 view3d_validate_backbuf(vc);
1222 glReadPixels(vc->ar->winrct.xmin+xminc, vc->ar->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1223 glReadBuffer(GL_BACK);
1225 if(ENDIAN_ORDER==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1227 a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
1230 if(*dr) *dr= WM_framebuffer_to_index(*dr);
1234 /* put clipped result back, if needed */
1235 if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax)
1238 ibuf1= IMB_allocImBuf( (xmax-xmin+1),(ymax-ymin+1),32,IB_rect,0);
1242 for(ys= ymin; ys<=ymax; ys++) {
1243 for(xs= xmin; xs<=xmax; xs++, dr++) {
1244 if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
1250 IMB_freeImBuf(ibuf);
1254 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1255 unsigned int view3d_sample_backbuf_rect(ViewContext *vc, short mval[2], int size,
1256 unsigned int min, unsigned int max, int *dist, short strict,
1257 void *handle, unsigned int (*indextest)(void *handle, unsigned int index))
1260 unsigned int *bufmin, *bufmax, *tbuf;
1262 int a, b, rc, nr, amount, dirvec[4][2];
1264 unsigned int index = 0;
1269 minx = mval[0]-(amount+1);
1270 miny = mval[1]-(amount+1);
1271 buf = view3d_read_backbuf(vc, minx, miny, minx+size-1, miny+size-1);
1276 dirvec[0][0]= 1; dirvec[0][1]= 0;
1277 dirvec[1][0]= 0; dirvec[1][1]= -size;
1278 dirvec[2][0]= -1; dirvec[2][1]= 0;
1279 dirvec[3][0]= 0; dirvec[3][1]= size;
1283 bufmax = buf->rect + size*size;
1284 tbuf+= amount*size+ amount;
1286 for(nr=1; nr<=size; nr++) {
1288 for(a=0; a<2; a++) {
1289 for(b=0; b<nr; b++, distance++) {
1290 if (*tbuf && *tbuf>=min && *tbuf<max) { //we got a hit
1292 indexok = indextest(handle, *tbuf - min+1);
1294 *dist= (short) sqrt( (float)distance );
1295 index = *tbuf - min+1;
1300 *dist= (short) sqrt( (float)distance ); // XXX, this distance is wrong -
1301 index = *tbuf - min+1; // messy yah, but indices start at 1
1306 tbuf+= (dirvec[rc][0]+dirvec[rc][1]);
1308 if(tbuf<bufmin || tbuf>=bufmax) {
1323 /* ************************************************************* */
1325 static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
1327 RegionView3D *rv3d= ar->regiondata;
1331 float vec[4], fac, asp, zoomx, zoomy;
1332 float x1, y1, x2, y2, cx, cy;
1335 if(bgpic==NULL) return;
1340 ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
1341 if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL) )
1343 if(ibuf->channels!=4)
1345 if(ibuf->rect==NULL)
1346 IMB_rect_from_float(ibuf);
1348 if(rv3d->persp==2) {
1351 calc_viewborder(scene, ar, v3d, &vb);
1361 /* calc window coord */
1362 initgrabz(rv3d, 0.0, 0.0, 0.0);
1363 window_to_3d_delta(ar, vec, 1, 0);
1364 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
1367 asp= ( (float)ibuf->y)/(float)ibuf->x;
1369 vec[0] = vec[1] = vec[2] = 0.0;
1370 view3d_project_float(ar, vec, sco, rv3d->persmat);
1374 x1= cx+ fac*(bgpic->xof-bgpic->size);
1375 y1= cy+ asp*fac*(bgpic->yof-bgpic->size);
1376 x2= cx+ fac*(bgpic->xof+bgpic->size);
1377 y2= cy+ asp*fac*(bgpic->yof+bgpic->size);
1380 /* complete clip? */
1384 if(x1 > ar->winx ) return;
1385 if(y1 > ar->winy ) return;
1387 zoomx= (x2-x1)/ibuf->x;
1388 zoomy= (y2-y1)/ibuf->y;
1390 /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
1391 if(zoomx < 1.0f || zoomy < 1.0f) {
1392 float tzoom= MIN2(zoomx, zoomy);
1395 if(ibuf->mipmap[0]==NULL)
1396 IMB_makemipmap(ibuf, 0, 0);
1398 while(tzoom < 1.0f && mip<8 && ibuf->mipmap[mip]) {
1405 ibuf= ibuf->mipmap[mip-1];
1408 if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
1410 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1412 glMatrixMode(GL_PROJECTION);
1414 glMatrixMode(GL_MODELVIEW);
1417 glaDefine2DArea(&ar->winrct);
1421 glPixelZoom(zoomx, zoomy);
1422 glColor4f(1.0, 1.0, 1.0, 1.0-bgpic->blend);
1423 glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
1425 glPixelZoom(1.0, 1.0);
1426 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1428 glMatrixMode(GL_PROJECTION);
1430 glMatrixMode(GL_MODELVIEW);
1433 glDisable(GL_BLEND);
1434 if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1437 /* ****************** View3d afterdraw *************** */
1439 typedef struct View3DAfter {
1440 struct View3DAfter *next, *prev;
1445 /* temp storage of Objects that need to be drawn as last */
1446 void add_view3d_after(View3D *v3d, Base *base, int type, int flag)
1448 View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
1450 BLI_addtail(&v3d->afterdraw, v3da);
1456 /* clears zbuffer and draws it over */
1457 static void view3d_draw_xray(Scene *scene, ARegion *ar, View3D *v3d, int clear)
1459 View3DAfter *v3da, *next;
1462 for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
1463 if(v3da->type==V3D_XRAY) doit= 1;
1466 if(clear && v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
1469 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1471 if(v3da->type==V3D_XRAY) {
1472 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1473 BLI_remlink(&v3d->afterdraw, v3da);
1481 /* disables write in zbuffer and draws it over */
1482 static void view3d_draw_transp(Scene *scene, ARegion *ar, View3D *v3d)
1484 View3DAfter *v3da, *next;
1489 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1491 if(v3da->type==V3D_TRANSP) {
1492 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1493 BLI_remlink(&v3d->afterdraw, v3da);
1503 /* *********************** */
1506 In most cases call draw_dupli_objects,
1507 draw_dupli_objects_color was added because when drawing set dupli's
1508 we need to force the color
1510 static void draw_dupli_objects_color(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int color)
1512 RegionView3D *rv3d= ar->regiondata;
1518 short transflag, use_displist= -1; /* -1 is initialize */
1521 if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
1523 tbase.flag= OB_FROMDUPLI|base->flag;
1524 lb= object_duplilist(scene, base->object);
1526 for(dob= lb->first; dob; dob= dob->next) {
1529 tbase.object= dob->ob;
1531 /* extra service: draw the duplicator in drawtype of parent */
1532 /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
1533 dt= tbase.object->dt; tbase.object->dt= MIN2(tbase.object->dt, base->object->dt);
1534 dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
1536 /* negative scale flag has to propagate */
1537 transflag= tbase.object->transflag;
1538 if(base->object->transflag & OB_NEG_SCALE)
1539 tbase.object->transflag ^= OB_NEG_SCALE;
1541 UI_ThemeColorBlend(color, TH_BACK, 0.5);
1543 /* generate displist, test for new object */
1544 if(use_displist==1 && dob->prev && dob->prev->ob!=dob->ob) {
1546 glDeleteLists(displist, 1);
1548 /* generate displist */
1549 if(use_displist == -1) {
1551 /* lamp drawing messes with matrices, could be handled smarter... but this works */
1552 if(dob->ob->type==OB_LAMP || dob->type==OB_DUPLIGROUP)
1555 /* disable boundbox check for list creation */
1556 object_boundbox_flag(dob->ob, OB_BB_DISABLED, 1);
1557 /* need this for next part of code */
1558 bb= object_get_boundbox(dob->ob);
1560 Mat4One(dob->ob->obmat); /* obmat gets restored */
1562 displist= glGenLists(1);
1563 glNewList(displist, GL_COMPILE);
1564 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1568 object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
1572 wmMultMatrix(dob->mat);
1573 if(boundbox_clip(rv3d, dob->mat, bb))
1574 glCallList(displist);
1575 wmLoadMatrix(rv3d->viewmat);
1578 Mat4CpyMat4(dob->ob->obmat, dob->mat);
1579 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1582 tbase.object->dt= dt;
1583 tbase.object->dtx= dtx;
1584 tbase.object->transflag= transflag;
1588 /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
1590 free_object_duplilist(lb); /* does restore */
1593 glDeleteLists(displist, 1);
1596 static void draw_dupli_objects(Scene *scene, ARegion *ar, View3D *v3d, Base *base)
1598 /* define the color here so draw_dupli_objects_color can be called
1599 * from the set loop */
1601 int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
1603 if(base->object->dup_group && base->object->dup_group->id.us<1)
1606 draw_dupli_objects_color(scene, ar, v3d, base, color);
1610 void view3d_update_depths(ARegion *ar, View3D *v3d)
1612 RegionView3D *rv3d= ar->regiondata;
1614 /* Create storage for, and, if necessary, copy depth buffer */
1615 if(!rv3d->depths) rv3d->depths= MEM_callocN(sizeof(ViewDepths),"ViewDepths");
1617 ViewDepths *d= rv3d->depths;
1618 if(d->w != ar->winx ||
1624 MEM_freeN(d->depths);
1625 d->depths= MEM_mallocN(sizeof(float)*d->w*d->h,"View depths");
1630 glReadPixels(ar->winrct.xmin,ar->winrct.ymin,d->w,d->h,
1631 GL_DEPTH_COMPONENT,GL_FLOAT, d->depths);
1633 glGetDoublev(GL_DEPTH_RANGE,d->depth_range);
1640 /* Enable sculpting in wireframe mode by drawing sculpt object only to the depth buffer */
1641 static void draw_sculpt_depths(Scene *scene, ARegion *ar, View3D *v3d)
1645 int dt= MIN2(v3d->drawtype, ob->dt);
1646 if(v3d->zbuf==0 && dt>OB_WIRE)
1650 int orig_vdt = v3d->drawtype;
1651 int orig_zbuf = v3d->zbuf;
1652 int orig_odt = ob->dt;
1654 glGetBooleanv(GL_DEPTH_TEST, &depth_on);
1655 v3d->drawtype = ob->dt = OB_SOLID;
1658 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1659 glEnable(GL_DEPTH_TEST);
1660 draw_object(scene, ar, v3d, BASACT, 0);
1661 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1663 glDisable(GL_DEPTH_TEST);
1665 v3d->drawtype = orig_vdt;
1666 v3d->zbuf = orig_zbuf;
1671 void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (* func)(void *))
1673 RegionView3D *rv3d= ar->regiondata;
1678 /* temp set drawtype to solid */
1680 /* Setting these temporarily is not nice */
1683 glalphaclip = U.glalphaclip;
1685 U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
1686 v3d->flag &= ~V3D_SELECT_OUTLINE;
1688 setwinmatrixview3d(ar, v3d, NULL); /* 0= no pick rect */
1689 setviewmatrixview3d(scene, v3d, rv3d); /* note: calls where_is_object for camera... */
1691 Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1692 Mat4Invert(rv3d->persinv, rv3d->persmat);
1693 Mat4Invert(rv3d->viewinv, rv3d->viewmat);
1695 glClear(GL_DEPTH_BUFFER_BIT);
1697 wmLoadMatrix(rv3d->viewmat);
1698 // persp(PERSP_STORE); // store correct view for persp(PERSP_VIEW) calls
1700 if(rv3d->rflag & RV3D_CLIPPING) {
1701 view3d_set_clipping(rv3d);
1705 glEnable(GL_DEPTH_TEST);
1707 /* draw set first */
1709 for(SETLOOPER(scene->set, base)) {
1710 if(v3d->lay & base->lay) {
1711 if (func == NULL || func(base)) {
1712 draw_object(scene, ar, v3d, base, 0);
1713 if(base->object->transflag & OB_DUPLI) {
1714 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
1721 for(base= scene->base.first; base; base= base->next) {
1722 if(v3d->lay & base->lay) {
1723 if (func == NULL || func(base)) {
1725 if(base->object->transflag & OB_DUPLI) {
1726 draw_dupli_objects(scene, ar, v3d, base);
1728 draw_object(scene, ar, v3d, base, 0);
1733 /* this isnt that nice, draw xray objects as if they are normal */
1734 if (v3d->afterdraw.first) {
1735 View3DAfter *v3da, *next;
1739 glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
1740 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1742 if(v3da->type==V3D_XRAY) {
1743 draw_object(scene, ar, v3d, v3da->base, 0);
1746 /* dont remove this time */
1750 glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
1751 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1753 if(v3da->type==V3D_XRAY) {
1754 v3d->xray= TRUE; v3d->transp= FALSE;
1755 } else if (v3da->type==V3D_TRANSP) {
1756 v3d->xray= FALSE; v3d->transp= TRUE;
1759 draw_object(scene, ar, v3d, v3da->base, 0); /* Draw Xray or Transp objects normally */
1760 BLI_remlink(&v3d->afterdraw, v3da);
1768 U.glalphaclip = glalphaclip;
1772 typedef struct View3DShadow {
1773 struct View3DShadow *next, *prev;
1777 static void gpu_render_lamp_update(Scene *scene, View3D *v3d, Object *ob, Object *par, float obmat[][4], ListBase *shadows)
1780 Lamp *la = (Lamp*)ob->data;
1781 View3DShadow *shadow;
1783 lamp = GPU_lamp_from_blender(scene, ob, par);
1786 GPU_lamp_update(lamp, ob->lay, obmat);
1787 GPU_lamp_update_colors(lamp, la->r, la->g, la->b, la->energy);
1789 if((ob->lay & v3d->lay) && GPU_lamp_has_shadow_buffer(lamp)) {
1790 shadow= MEM_callocN(sizeof(View3DShadow), "View3DShadow");
1791 shadow->lamp = lamp;
1792 BLI_addtail(shadows, shadow);
1797 static void gpu_update_lamps_shadows(Scene *scene, View3D *v3d)
1800 View3DShadow *shadow;
1805 shadows.first= shadows.last= NULL;
1807 /* update lamp transform and gather shadow lamps */
1808 for(SETLOOPER(scene, base)) {
1811 if(ob->type == OB_LAMP)
1812 gpu_render_lamp_update(scene, v3d, ob, NULL, ob->obmat, &shadows);
1814 if (ob->transflag & OB_DUPLI) {
1816 ListBase *lb = object_duplilist(scene, ob);
1818 for(dob=lb->first; dob; dob=dob->next)
1819 if(dob->ob->type==OB_LAMP)
1820 gpu_render_lamp_update(scene, v3d, dob->ob, ob, dob->mat, &shadows);
1822 free_object_duplilist(lb);
1826 /* render shadows after updating all lamps, nested object_duplilist
1827 * don't work correct since it's replacing object matrices */
1828 for(shadow=shadows.first; shadow; shadow=shadow->next) {
1829 /* this needs to be done better .. */
1830 float viewmat[4][4], winmat[4][4];
1831 int drawtype, lay, winsize, flag2;
1833 drawtype= v3d->drawtype;
1835 flag2= v3d->flag2 & V3D_SOLID_TEX;
1837 v3d->drawtype = OB_SOLID;
1838 v3d->lay &= GPU_lamp_shadow_layer(shadow->lamp);
1839 v3d->flag2 &= ~V3D_SOLID_TEX;
1841 GPU_lamp_shadow_buffer_bind(shadow->lamp, viewmat, &winsize, winmat);
1842 // XXX drawview3d_render(v3d, viewmat, winsize, winsize, winmat, 1);
1843 GPU_lamp_shadow_buffer_unbind(shadow->lamp);
1845 v3d->drawtype= drawtype;
1847 v3d->flag2 |= flag2;
1850 BLI_freelistN(&shadows);
1853 /* *********************** customdata **************** */
1855 /* goes over all modes and view3d settings */
1856 static CustomDataMask get_viewedit_datamask(bScreen *screen)
1858 CustomDataMask mask = CD_MASK_BAREMESH;
1861 /* check if we need tfaces & mcols due to face select or texture paint */
1862 if(FACESEL_PAINT_TEST || G.f & G_TEXTUREPAINT)
1863 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1865 /* check if we need tfaces & mcols due to view mode */
1866 for(sa = screen->areabase.first; sa; sa = sa->next) {
1867 if(sa->spacetype == SPACE_VIEW3D) {
1868 View3D *view = sa->spacedata.first;
1869 if(view->drawtype == OB_SHADED) {
1870 /* this includes normals for mesh_create_shadedColors */
1871 mask |= CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_NORMAL | CD_MASK_ORCO;
1873 if((view->drawtype == OB_TEXTURE) || ((view->drawtype == OB_SOLID) && (view->flag2 & V3D_SOLID_TEX))) {
1874 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1876 if((G.fileflags & G_FILE_GAME_MAT) &&
1877 (G.fileflags & G_FILE_GAME_MAT_GLSL)) {
1878 mask |= CD_MASK_ORCO;
1884 /* check if we need mcols due to vertex paint or weightpaint */
1885 if(G.f & G_VERTEXPAINT)
1886 mask |= CD_MASK_MCOL;
1887 if(G.f & G_WEIGHTPAINT)
1888 mask |= CD_MASK_WEIGHT_MCOL;
1890 /* XXX: is this even needed?
1891 if(G.f & G_SCULPTMODE)
1892 mask |= CD_MASK_MDISPS;
1899 void view3d_main_area_draw(const bContext *C, ARegion *ar)
1901 Scene *scene= CTX_data_scene(C);
1902 View3D *v3d = CTX_wm_view3d(C);
1903 RegionView3D *rv3d= CTX_wm_region_view3d(C);
1907 int retopo= 0, sculptparticle= 0;
1908 Object *obact = OBACT;
1909 char *grid_unit= NULL;
1911 /* from now on all object derived meshes check this */
1912 v3d->customdata_mask= get_viewedit_datamask(CTX_wm_screen(C));
1914 /* shadow buffers, before we setup matrices */
1915 if(draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
1916 gpu_update_lamps_shadows(scene, v3d);
1918 setwinmatrixview3d(ar, v3d, NULL); /* 0= no pick rect */
1919 setviewmatrixview3d(scene, v3d, rv3d); /* note: calls where_is_object for camera... */
1921 Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1922 Mat4Invert(rv3d->persinv, rv3d->persmat);
1923 Mat4Invert(rv3d->viewinv, rv3d->viewmat);
1925 /* calculate pixelsize factor once, is used for lamps and obcenters */
1927 float len1, len2, vec[3];
1929 VECCOPY(vec, rv3d->persinv[0]);
1930 len1= Normalize(vec);
1931 VECCOPY(vec, rv3d->persinv[1]);
1932 len2= Normalize(vec);
1934 rv3d->pixsize= 2.0f*(len1>len2?len1:len2);
1936 /* correct for window size */
1937 if(ar->winx > ar->winy) rv3d->pixsize/= (float)ar->winx;
1938 else rv3d->pixsize/= (float)ar->winy;
1941 if(v3d->drawtype > OB_WIRE) {
1943 UI_GetThemeColor3fv(TH_BACK, col);
1944 glClearColor(col[0], col[1], col[2], 0.0);
1945 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1951 UI_GetThemeColor3fv(TH_BACK, col);
1952 glClearColor(col[0], col[1], col[2], 0.0);
1953 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1956 wmLoadMatrix(rv3d->viewmat);
1958 if(rv3d->rflag & RV3D_CLIPPING)
1959 view3d_draw_clipping(rv3d);
1961 /* set zbuffer after we draw clipping region */
1962 if(v3d->drawtype > OB_WIRE) {
1964 glEnable(GL_DEPTH_TEST);
1967 // needs to be done always, gridview is adjusted in drawgrid() now
1968 v3d->gridview= v3d->grid;
1970 if(rv3d->view==0 || rv3d->persp!=0) {
1971 drawfloor(scene, v3d);
1972 if(rv3d->persp==2) {
1974 if(scene->world->mode & WO_STARS) {
1975 RE_make_stars(NULL, scene, star_stuff_init_func, star_stuff_vertex_func,
1976 star_stuff_term_func);
1979 if(v3d->flag & V3D_DISPBGPIC) draw_bgpic(scene, ar, v3d);
1983 ED_region_pixelspace(ar);
1984 drawgrid(&scene->unit, ar, v3d, &grid_unit);
1985 /* XXX make function? replaces persp(1) */
1986 glMatrixMode(GL_PROJECTION);
1987 wmLoadMatrix(rv3d->winmat);
1988 glMatrixMode(GL_MODELVIEW);
1989 wmLoadMatrix(rv3d->viewmat);
1991 if(v3d->flag & V3D_DISPBGPIC) {
1992 draw_bgpic(scene, ar, v3d);
1996 if(rv3d->rflag & RV3D_CLIPPING)
1997 view3d_set_clipping(rv3d);
1999 /* draw set first */
2001 for(SETLOOPER(scene->set, base)) {
2003 if(v3d->lay & base->lay) {
2005 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2006 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
2008 if(base->object->transflag & OB_DUPLI) {
2009 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2014 /* Transp and X-ray afterdraw stuff for sets is done later */
2017 /* extra service in layerbuttons, showing used layers */
2020 /* then draw not selected and the duplis, but skip editmode object */
2021 for(base= scene->base.first; base; base= base->next) {
2022 v3d->lay_used |= base->lay;
2024 if(v3d->lay & base->lay) {
2027 if(base->object->transflag & OB_DUPLI) {
2028 draw_dupli_objects(scene, ar, v3d, base);
2030 if((base->flag & SELECT)==0) {
2031 if(base->object!=scene->obedit)
2032 draw_object(scene, ar, v3d, base, 0);
2037 // retopo= retopo_mesh_check() || retopo_curve_check();
2038 sculptparticle= (G.f & G_PARTICLEEDIT || (obact && obact->mode & OB_MODE_SCULPT)) && !scene->obedit;
2040 view3d_update_depths(ar, v3d);
2042 /* draw selected and editmode */
2043 for(base= scene->base.first; base; base= base->next) {
2044 if(v3d->lay & base->lay) {
2045 if (base->object==scene->obedit || ( base->flag & SELECT) )
2046 draw_object(scene, ar, v3d, base, 0);
2050 if(!retopo && sculptparticle && !(obact && (obact->dtx & OB_DRAWXRAY))) {
2051 if(obact && obact->mode & OB_MODE_SCULPT)
2052 draw_sculpt_depths(scene, ar, v3d);
2053 view3d_update_depths(ar, v3d);
2056 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST);
2060 // if(scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
2062 /* Transp and X-ray afterdraw stuff */
2063 view3d_draw_transp(scene, ar, v3d);
2064 view3d_draw_xray(scene, ar, v3d, 1); // clears zbuffer if it is used!
2066 if(!retopo && sculptparticle && (obact && (OBACT->dtx & OB_DRAWXRAY))) {
2067 if(obact && obact->mode & OB_MODE_SCULPT)
2068 draw_sculpt_depths(scene, ar, v3d);
2069 view3d_update_depths(ar, v3d);
2072 if(rv3d->rflag & RV3D_CLIPPING)
2073 view3d_clr_clipping();
2075 BIF_draw_manipulator(C);
2079 glDisable(GL_DEPTH_TEST);
2082 /* draw grease-pencil stuff */
2083 // if (v3d->flag2 & V3D_DISPGP)
2084 // draw_gpencil_3dview(ar, 1);
2088 ED_region_pixelspace(ar);
2090 /* Draw Sculpt Mode brush XXX (removed) */
2092 // retopo_paint_view_update(v3d);
2093 // retopo_draw_paint_lines();
2095 /* Draw particle edit brush XXX (removed) */
2097 if(rv3d->persp>1) drawviewborder(scene, ar, v3d);
2098 if(rv3d->rflag & RV3D_FLYMODE) drawviewborder_flymode(ar);
2100 /* draw grease-pencil stuff */
2101 // if (v3d->flag2 & V3D_DISPGP)
2102 // draw_gpencil_3dview(ar, 0);
2104 drawcursor(scene, ar, v3d);
2106 if(U.uiflag & USER_SHOW_ROTVIEWICON)
2107 draw_view_axis(rv3d);
2109 draw_view_icon(rv3d);
2111 /* XXX removed viewport fps */
2112 if(U.uiflag & USER_SHOW_VIEWPORTNAME) {
2113 draw_viewport_name(ar, v3d);
2115 if (grid_unit) { /* draw below the viewport name */
2116 UI_ThemeColor(TH_TEXT_HI);
2117 BLF_draw_default(10, ar->winy-(USER_SHOW_VIEWPORTNAME?40:20), 0.0f, grid_unit);
2121 if(U.uiflag & USER_DRAWVIEWINFO)
2122 draw_selected_name(scene, ob, v3d);
2124 /* XXX here was the blockhandlers for floating panels */
2126 if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT || G.f & G_TEXTUREPAINT) {
2127 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2128 // XXX addafterqueue(ar->win, BACKBUFDRAW, 1);
2131 if((G.f & G_PARTICLEEDIT) && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT)) {
2132 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2133 // XXX addafterqueue(ar->win, BACKBUFDRAW, 1);
2136 // test for backbuf select
2137 if(scene->obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT)) {
2139 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2140 // XXX if(afterqtest(ar->win, BACKBUFDRAW)==0) {
2141 // addafterqueue(ar->win, BACKBUFDRAW, 1);