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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_armature_types.h"
34 #include "DNA_camera_types.h"
35 #include "DNA_customdata_types.h"
36 #include "DNA_group_types.h"
37 #include "DNA_key_types.h"
38 #include "DNA_lamp_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_world_types.h"
42 #include "MEM_guardedalloc.h"
44 #include "BLI_blenlib.h"
49 #include "BKE_context.h"
50 #include "BKE_customdata.h"
51 #include "BKE_image.h"
54 #include "BKE_object.h"
55 #include "BKE_global.h"
56 #include "BKE_paint.h"
57 #include "BKE_scene.h"
58 #include "BKE_screen.h"
59 #include "BKE_utildefines.h"
62 #include "RE_pipeline.h" // make_stars
64 #include "IMB_imbuf_types.h"
65 #include "IMB_imbuf.h"
68 #include "BIF_glutil.h"
73 #include "ED_armature.h"
74 #include "ED_keyframing.h"
75 #include "ED_gpencil.h"
76 #include "ED_screen.h"
77 #include "ED_space_api.h"
78 #include "ED_screen_types.h"
79 #include "ED_transform.h"
81 #include "UI_interface.h"
82 #include "UI_interface_icons.h"
83 #include "UI_resources.h"
86 #include "GPU_material.h"
87 #include "GPU_extensions.h"
89 #include "view3d_intern.h" // own include
93 static void star_stuff_init_func(void)
99 static void star_stuff_vertex_func(float* i)
103 static void star_stuff_term_func(void)
108 void circf(float x, float y, float rad)
110 GLUquadricObj *qobj = gluNewQuadric();
112 gluQuadricDrawStyle(qobj, GLU_FILL);
116 glTranslatef(x, y, 0.);
118 gluDisk( qobj, 0.0, rad, 32, 1);
122 gluDeleteQuadric(qobj);
125 void circ(float x, float y, float rad)
127 GLUquadricObj *qobj = gluNewQuadric();
129 gluQuadricDrawStyle(qobj, GLU_SILHOUETTE);
133 glTranslatef(x, y, 0.);
135 gluDisk( qobj, 0.0, rad, 32, 1);
139 gluDeleteQuadric(qobj);
143 /* ********* custom clipping *********** */
145 static void view3d_draw_clipping(RegionView3D *rv3d)
147 BoundBox *bb= rv3d->clipbb;
150 UI_ThemeColorShade(TH_BACK, -8);
154 glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[3]);
155 glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[1]);
156 glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[5]);
157 glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[3]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[6]);
158 glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[2]);
159 glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[3]);
165 void view3d_set_clipping(RegionView3D *rv3d)
170 if(rv3d->viewlock) tot= 6;
172 for(a=0; a<tot; a++) {
173 QUATCOPY(plane, rv3d->clip[a]);
174 glClipPlane(GL_CLIP_PLANE0+a, plane);
175 glEnable(GL_CLIP_PLANE0+a);
179 void view3d_clr_clipping(void)
184 glDisable(GL_CLIP_PLANE0+a);
188 static int test_clipping(float *vec, float clip[][4])
193 if(0.0f < clip[0][3] + INPR(view, clip[0]))
194 if(0.0f < clip[1][3] + INPR(view, clip[1]))
195 if(0.0f < clip[2][3] + INPR(view, clip[2]))
196 if(0.0f < clip[3][3] + INPR(view, clip[3]))
202 /* for 'local' ED_view3d_local_clipping must run first
203 * then all comparisons can be done in localspace */
204 int view3d_test_clipping(RegionView3D *rv3d, float *vec, int local)
206 return test_clipping(vec, local ? rv3d->clip_local : rv3d->clip);
209 /* ********* end custom clipping *********** */
212 static void drawgrid_draw(ARegion *ar, float wx, float wy, float x, float y, float dx)
221 while(fx< ar->winx) {
222 fdrawline(fx, 0.0, fx, (float)ar->winy);
230 while(fy< ar->winy) {
231 fdrawline(0.0, fy, (float)ar->winx, fy);
237 #define GRID_MIN_PX 6.0f
239 static void drawgrid(UnitSettings *unit, ARegion *ar, View3D *v3d, char **grid_unit)
241 /* extern short bgpicmode; */
242 RegionView3D *rv3d= ar->regiondata;
243 float wx, wy, x, y, fw, fx, fy, dx;
245 char col[3], col2[3];
249 vec4[0]=vec4[1]=vec4[2]=0.0;
251 mul_m4_v4(rv3d->persmat, vec4);
256 wx= (ar->winx/2.0); /* because of rounding errors, grid at wrong location */
262 vec4[0]=vec4[1]= (unit->system) ? 1.0 : v3d->grid;
266 mul_m4_v4(rv3d->persmat, vec4);
271 dx= fabs(x-(wx)*fx/fw);
272 if(dx==0) dx= fabs(y-(wy)*fy/fw);
274 glDepthMask(0); // disable write in zbuffer
277 UI_ThemeColor(TH_GRID);
280 /* Use GRID_MIN_PX*2 for units because very very small grid
281 * items are less useful when dealing with units */
288 bUnit_GetSystem(&usys, &len, unit->system, B_UNIT_LENGTH);
293 scalar= bUnit_GetScaler(usys, i);
295 dx_scalar = dx * scalar * unit->scale_length;
296 if (dx_scalar < (GRID_MIN_PX*2))
299 /* Store the smallest drawn grid size units name so users know how big each grid cell is */
300 if(*grid_unit==NULL) {
301 *grid_unit= bUnit_GetNameDisplay(usys, i);
302 rv3d->gridview= (scalar * unit->scale_length);
304 blend_fac= 1-((GRID_MIN_PX*2)/dx_scalar);
306 /* tweak to have the fade a bit nicer */
307 blend_fac= (blend_fac * blend_fac) * 2.0f;
308 CLAMP(blend_fac, 0.3f, 1.0f);
311 UI_ThemeColorBlend(TH_BACK, TH_GRID, blend_fac);
313 drawgrid_draw(ar, wx, wy, x, y, dx_scalar);
318 short sublines = v3d->gridsubdiv;
321 rv3d->gridview*= sublines;
325 rv3d->gridview*= sublines;
329 rv3d->gridview*= sublines;
333 UI_ThemeColor(TH_GRID);
334 drawgrid_draw(ar, wx, wy, x, y, dx);
337 else { // start blending out
338 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
339 drawgrid_draw(ar, wx, wy, x, y, dx);
341 UI_ThemeColor(TH_GRID);
342 drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
345 else { // start blending out (GRID_MIN_PX < dx < (GRID_MIN_PX*10))
346 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
347 drawgrid_draw(ar, wx, wy, x, y, dx);
349 UI_ThemeColor(TH_GRID);
350 drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
354 if(dx>(GRID_MIN_PX*10)) { // start blending in
355 rv3d->gridview/= sublines;
357 if(dx>(GRID_MIN_PX*10)) { // start blending in
358 rv3d->gridview/= sublines;
360 if(dx>(GRID_MIN_PX*10)) {
361 UI_ThemeColor(TH_GRID);
362 drawgrid_draw(ar, wx, wy, x, y, dx);
365 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
366 drawgrid_draw(ar, wx, wy, x, y, dx);
367 UI_ThemeColor(TH_GRID);
368 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
372 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
373 drawgrid_draw(ar, wx, wy, x, y, dx);
374 UI_ThemeColor(TH_GRID);
375 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
379 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
380 drawgrid_draw(ar, wx, wy, x, y, dx);
381 UI_ThemeColor(TH_GRID);
382 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
390 UI_GetThemeColor3ubv(TH_GRID, col);
395 if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
396 UI_make_axis_color(col, col2, 'y');
397 else UI_make_axis_color(col, col2, 'x');
398 glColor3ubv((GLubyte *)col2);
400 fdrawline(0.0, y, (float)ar->winx, y);
402 if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
403 UI_make_axis_color(col, col2, 'y');
404 else UI_make_axis_color(col, col2, 'z');
405 glColor3ubv((GLubyte *)col2);
407 fdrawline(x, 0.0, x, (float)ar->winy);
409 glDepthMask(1); // enable write in zbuffer
413 static void drawfloor(Scene *scene, View3D *v3d)
416 int a, gridlines, emphasise;
417 char col[3], col2[3];
422 if(v3d->gridlines<3) return;
424 if(v3d->zbuf && scene->obedit) glDepthMask(0); // for zbuffer-select
426 gridlines= v3d->gridlines/2;
427 grid= gridlines*v3d->grid;
429 UI_GetThemeColor3ubv(TH_GRID, col);
430 UI_GetThemeColor3ubv(TH_BACK, col2);
432 /* emphasise division lines lighter instead of darker, if background is darker than grid */
433 if ( ((col[0]+col[1]+col[2])/3+10) > (col2[0]+col2[1]+col2[2])/3 )
438 /* draw the Y axis and/or grid lines */
439 for(a= -gridlines;a<=gridlines;a++) {
441 /* check for the 'show Y axis' preference */
442 if (v3d->gridflag & V3D_SHOW_Y) {
443 UI_make_axis_color(col, col2, 'y');
444 glColor3ubv((GLubyte *)col2);
447 } else if (v3d->gridflag & V3D_SHOW_FLOOR) {
448 UI_ThemeColorShade(TH_GRID, emphasise);
453 /* check for the 'show grid floor' preference */
454 if (v3d->gridflag & V3D_SHOW_FLOOR) {
456 UI_ThemeColorShade(TH_GRID, emphasise);
458 else UI_ThemeColorShade(TH_GRID, 10);
467 glBegin(GL_LINE_STRIP);
468 vert[0]= a*v3d->grid;
477 /* draw the X axis and/or grid lines */
478 for(a= -gridlines;a<=gridlines;a++) {
480 /* check for the 'show X axis' preference */
481 if (v3d->gridflag & V3D_SHOW_X) {
482 UI_make_axis_color(col, col2, 'x');
483 glColor3ubv((GLubyte *)col2);
486 } else if (v3d->gridflag & V3D_SHOW_FLOOR) {
487 UI_ThemeColorShade(TH_GRID, emphasise);
492 /* check for the 'show grid floor' preference */
493 if (v3d->gridflag & V3D_SHOW_FLOOR) {
495 UI_ThemeColorShade(TH_GRID, emphasise);
497 else UI_ThemeColorShade(TH_GRID, 10);
506 glBegin(GL_LINE_STRIP);
507 vert[1]= a*v3d->grid;
516 /* draw the Z axis line */
517 /* check for the 'show Z axis' preference */
518 if (v3d->gridflag & V3D_SHOW_Z) {
519 UI_make_axis_color(col, col2, 'z');
520 glColor3ubv((GLubyte *)col2);
522 glBegin(GL_LINE_STRIP);
532 if(v3d->zbuf && scene->obedit) glDepthMask(1);
536 static void drawcursor(Scene *scene, ARegion *ar, View3D *v3d)
541 /* we dont want the clipping for cursor */
544 project_short(ar, give_cursor(scene, v3d), co);
553 circ((float)mx, (float)my, 10.0);
556 circ((float)mx, (float)my, 10.0);
560 sdrawline(mx-20, my, mx-5, my);
561 sdrawline(mx+5, my, mx+20, my);
562 sdrawline(mx, my-20, mx, my-5);
563 sdrawline(mx, my+5, mx, my+20);
567 /* Draw a live substitute of the view icon, which is always shown */
568 static void draw_view_axis(RegionView3D *rv3d)
570 const float k = U.rvisize; /* axis size */
571 const float toll = 0.5; /* used to see when view is quasi-orthogonal */
572 const float start = k + 1.0; /* axis center in screen coordinates, x=y */
573 float ydisp = 0.0; /* vertical displacement to allow obj info text */
575 /* rvibright ranges approx. from original axis icon color to gizmo color */
576 float bright = U.rvibright / 15.0f;
578 unsigned char col[3];
579 unsigned char gridcol[3];
586 /* thickness of lines is proportional to k */
587 /* (log(k)-1) gives a more suitable thickness, but fps decreased by about 3 fps */
589 //glLineWidth(log(k)-1); // a bit slow
591 UI_GetThemeColor3ubv(TH_GRID, (char *)gridcol);
596 mul_qt_v3(rv3d->viewquat, vec);
598 UI_make_axis_color((char *)gridcol, (char *)col, 'x');
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;
602 v = (v<1.0-(bright) ? v+bright : 1.0);
603 hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
608 fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
609 if (fabs(dx) > toll || fabs(dy) > toll) {
610 BLF_draw_default(start + dx + 2, start + dy + ydisp + 2, 0.0f, "x");
616 mul_qt_v3(rv3d->viewquat, vec);
618 UI_make_axis_color((char *)gridcol, (char *)col, 'y');
619 rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
620 s = s<0.5 ? s+0.5 : 1.0;
622 v = (v<1.0-(bright) ? v+bright : 1.0);
623 hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
628 fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
629 if (fabs(dx) > toll || fabs(dy) > toll) {
630 BLF_draw_default(start + dx + 2, start + dy + ydisp + 2, 0.0f, "y");
636 mul_qt_v3(rv3d->viewquat, vec);
638 UI_make_axis_color((char *)gridcol, (char *)col, 'z');
639 rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
640 s = s<0.5 ? s+0.5 : 1.0;
642 v = (v<1.0-(bright) ? v+bright : 1.0);
643 hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
648 fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
649 if (fabs(dx) > toll || fabs(dy) > toll) {
650 BLF_draw_default(start + dx + 2, start + dy + ydisp + 2, 0.0f, "z");
653 /* restore line-width */
658 static void draw_view_icon(RegionView3D *rv3d)
662 if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
664 else if( ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
665 icon= ICON_AXIS_FRONT;
666 else if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
667 icon= ICON_AXIS_SIDE;
671 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
673 UI_icon_draw(5.0, 5.0, icon);
678 static char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
682 switch (rv3d->view) {
683 case RV3D_VIEW_FRONT:
684 if (rv3d->persp == RV3D_ORTHO) name = "Front Ortho";
685 else name = "Front Persp";
688 if (rv3d->persp == RV3D_ORTHO) name = "Back Ortho";
689 else name = "Back Persp";
692 if (rv3d->persp == RV3D_ORTHO) name = "Top Ortho";
693 else name = "Top Persp";
695 case RV3D_VIEW_BOTTOM:
696 if (rv3d->persp == RV3D_ORTHO) name = "Bottom Ortho";
697 else name = "Bottom Persp";
699 case RV3D_VIEW_RIGHT:
700 if (rv3d->persp == RV3D_ORTHO) name = "Right Ortho";
701 else name = "Right Persp";
704 if (rv3d->persp == RV3D_ORTHO) name = "Left Ortho";
705 else name = "Left Persp";
709 if (rv3d->persp==RV3D_CAMOB) {
710 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
712 cam = v3d->camera->data;
713 name = (cam->type != CAM_ORTHO) ? "Camera Persp" : "Camera Ortho";
715 name = "Object as Camera";
718 name = (rv3d->persp == RV3D_ORTHO) ? "User Ortho" : "User Persp";
726 static void draw_viewport_name(ARegion *ar, View3D *v3d)
728 RegionView3D *rv3d= ar->regiondata;
729 char *name = view3d_get_name(v3d, rv3d);
730 char *printable = NULL;
733 printable = MEM_mallocN(strlen(name) + strlen(" (Local)_"), "viewport_name"); /* '_' gives space for '\0' */
734 strcpy(printable, name);
735 strcat(printable, " (Local)");
741 UI_ThemeColor(TH_TEXT_HI);
742 BLF_draw_default(22, ar->winy-17, 0.0f, printable);
746 MEM_freeN(printable);
750 /* draw info beside axes in bottom left-corner:
751 * framenum, object name, bone name (if available), marker name (if available)
753 static void draw_selected_name(Scene *scene, Object *ob, View3D *v3d)
755 char info[256], *markern;
758 /* get name of marker on current frame (if available) */
759 markern= scene_find_marker_name(scene, CFRA);
761 /* check if there is an object */
763 /* name(s) to display depends on type of object */
764 if(ob->type==OB_ARMATURE) {
765 bArmature *arm= ob->data;
768 /* show name of active bone too (if possible) */
772 name= ((EditBone *)arm->act_edbone)->name;
775 else if(ob->mode & OB_MODE_POSE) {
778 if(arm->act_bone->layer & arm->layer)
779 name= arm->act_bone->name;
784 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, name, markern);
786 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, name);
788 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
790 else if(ELEM3(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
795 /* try to display active shapekey too */
797 key = ob_get_key(ob);
799 kb = BLI_findlink(&key->block, ob->shapenr-1);
801 sprintf(shapes, ": %s ", kb->name);
802 if(ob->shapeflag == OB_SHAPE_LOCK){
803 strcat(shapes, " (Pinned)");
809 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, shapes, markern);
811 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, shapes);
814 /* standard object */
816 sprintf(info, "(%d) %s <%s>", CFRA, ob->id.name+2, markern);
818 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
821 /* colour depends on whether there is a keyframe */
822 if (id_frame_has_keyframe((ID *)ob, /*frame_to_float(scene, CFRA)*/(float)(CFRA), v3d->keyflags))
823 UI_ThemeColor(TH_VERTEX_SELECT);
825 UI_ThemeColor(TH_TEXT_HI);
830 sprintf(info, "(%d) <%s>", CFRA, markern);
832 sprintf(info, "(%d)", CFRA);
834 /* colour is always white */
835 UI_ThemeColor(TH_TEXT_HI);
838 if (U.uiflag & USER_SHOW_ROTVIEWICON)
839 offset = 14 + (U.rvisize * 2);
841 BLF_draw_default(offset, 10, 0.0f, info);
844 static void view3d_get_viewborder_size(Scene *scene, ARegion *ar, float size_r[2])
846 float winmax= MAX2(ar->winx, ar->winy);
847 float aspect= (scene->r.xsch*scene->r.xasp) / (scene->r.ysch*scene->r.yasp);
851 size_r[1]= winmax/aspect;
853 size_r[0]= winmax*aspect;
858 void calc_viewborder(Scene *scene, ARegion *ar, View3D *v3d, rctf *viewborder_r)
860 RegionView3D *rv3d= ar->regiondata;
861 float zoomfac, size[2];
862 float dx= 0.0f, dy= 0.0f;
864 view3d_get_viewborder_size(scene, ar, size);
866 /* magic zoom calculation, no idea what
867 * it signifies, if you find out, tell me! -zr
869 /* simple, its magic dude!
870 * well, to be honest, this gives a natural feeling zooming
871 * with multiple keypad presses (ton)
874 zoomfac= (M_SQRT2 + rv3d->camzoom/50.0);
875 zoomfac= (zoomfac*zoomfac)*0.25;
877 size[0]= size[0]*zoomfac;
878 size[1]= size[1]*zoomfac;
880 /* center in window */
881 viewborder_r->xmin= 0.5*ar->winx - 0.5*size[0];
882 viewborder_r->ymin= 0.5*ar->winy - 0.5*size[1];
883 viewborder_r->xmax= viewborder_r->xmin + size[0];
884 viewborder_r->ymax= viewborder_r->ymin + size[1];
886 dx= ar->winx*rv3d->camdx*zoomfac*2.0f;
887 dy= ar->winy*rv3d->camdy*zoomfac*2.0f;
890 viewborder_r->xmin-= dx;
891 viewborder_r->ymin-= dy;
892 viewborder_r->xmax-= dx;
893 viewborder_r->ymax-= dy;
895 if(v3d->camera && v3d->camera->type==OB_CAMERA) {
896 Camera *cam= v3d->camera->data;
897 float w = viewborder_r->xmax - viewborder_r->xmin;
898 float h = viewborder_r->ymax - viewborder_r->ymin;
899 float side = MAX2(w, h);
901 viewborder_r->xmin+= cam->shiftx*side;
902 viewborder_r->xmax+= cam->shiftx*side;
903 viewborder_r->ymin+= cam->shifty*side;
904 viewborder_r->ymax+= cam->shifty*side;
908 void view3d_set_1_to_1_viewborder(Scene *scene, ARegion *ar)
910 RegionView3D *rv3d= ar->regiondata;
912 int im_width= (scene->r.size*scene->r.xsch)/100;
914 view3d_get_viewborder_size(scene, ar, size);
916 rv3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
917 rv3d->camzoom= CLAMPIS(rv3d->camzoom, -30, 300);
921 static void drawviewborder_flymode(ARegion *ar)
923 /* draws 4 edge brackets that frame the safe area where the
924 mouse can move during fly mode without spinning the view */
925 float x1, x2, y1, y2;
927 x1= 0.45*(float)ar->winx;
928 y1= 0.45*(float)ar->winy;
929 x2= 0.55*(float)ar->winx;
930 y2= 0.55*(float)ar->winy;
966 static void drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
968 extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad); // interface_panel.c
970 float x1, x2, y1, y2;
971 float x3, y3, x4, y4;
975 if(v3d->camera==NULL)
977 if(v3d->camera->type==OB_CAMERA)
978 ca = v3d->camera->data;
980 calc_viewborder(scene, ar, v3d, &viewborder);
986 /* passepartout, specified in camera edit buttons */
987 if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001) {
988 if (ca->passepartalpha == 1.0) {
991 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
993 glColor4f(0, 0, 0, ca->passepartalpha);
996 glRectf(0.0, (float)ar->winy, x1, 0.0);
997 if (x2 < (float)ar->winx)
998 glRectf(x2, (float)ar->winy, (float)ar->winx, 0.0);
999 if (y2 < (float)ar->winy)
1000 glRectf(x1, (float)ar->winy, x2, y2);
1002 glRectf(x1, y1, x2, 0.0);
1004 glDisable(GL_BLEND);
1008 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1011 UI_ThemeColor(TH_BACK);
1012 glRectf(x1, y1, x2, y2);
1015 UI_ThemeColor(TH_WIRE);
1016 glRectf(x1, y1, x2, y2);
1019 if(scene->r.mode & R_BORDER) {
1022 x3= x1+ scene->r.border.xmin*(x2-x1);
1023 y3= y1+ scene->r.border.ymin*(y2-y1);
1024 x4= x1+ scene->r.border.xmax*(x2-x1);
1025 y4= y1+ scene->r.border.ymax*(y2-y1);
1028 glRectf(x3, y3, x4, y4);
1032 if (ca && (ca->flag & CAM_SHOWTITLESAFE)) {
1043 UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1046 gl_round_box(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
1050 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1052 /* camera name - draw in highlighted text color */
1053 if (ca && (ca->flag & CAM_SHOWNAME)) {
1054 UI_ThemeColor(TH_TEXT_HI);
1055 BLF_draw_default(x1, y1-15, 0.0f, v3d->camera->id.name+2);
1056 UI_ThemeColor(TH_WIRE);
1060 /* *********************** backdraw for selection *************** */
1062 void backdrawview3d(Scene *scene, ARegion *ar, View3D *v3d)
1064 RegionView3D *rv3d= ar->regiondata;
1065 struct Base *base = scene->basact;
1068 if(base && (base->object->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT) ||
1069 paint_facesel_test(base->object)));
1070 else if((base && (base->object->mode & OB_MODE_TEXTURE_PAINT)) &&
1071 scene->toolsettings && (scene->toolsettings->imapaint.flag & IMAGEPAINT_PROJECT_DISABLE));
1072 else if((base && (base->object->mode & OB_MODE_PARTICLE_EDIT)) && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT));
1073 else if(scene->obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT));
1075 v3d->flag &= ~V3D_INVALID_BACKBUF;
1079 if( !(v3d->flag & V3D_INVALID_BACKBUF) ) return;
1083 // addafterqueue(ar->win, BACKBUFDRAW, 1);
1088 if(v3d->drawtype > OB_WIRE) v3d->zbuf= TRUE;
1090 glDisable(GL_DITHER);
1092 region_scissor_winrct(ar, &winrct);
1093 glScissor(winrct.xmin, winrct.ymin, winrct.xmax - winrct.xmin, winrct.ymax - winrct.ymin);
1095 glClearColor(0.0, 0.0, 0.0, 0.0);
1097 glEnable(GL_DEPTH_TEST);
1098 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1101 glClear(GL_COLOR_BUFFER_BIT);
1102 glDisable(GL_DEPTH_TEST);
1105 if(rv3d->rflag & RV3D_CLIPPING)
1106 view3d_set_clipping(rv3d);
1108 G.f |= G_BACKBUFSEL;
1110 if(base && (base->lay & v3d->lay)) {
1111 draw_object_backbufsel(scene, v3d, rv3d, base->object);
1114 v3d->flag &= ~V3D_INVALID_BACKBUF;
1116 G.f &= ~G_BACKBUFSEL;
1118 glDisable(GL_DEPTH_TEST);
1119 glEnable(GL_DITHER);
1121 if(rv3d->rflag & RV3D_CLIPPING)
1122 view3d_clr_clipping();
1124 /* it is important to end a view in a transform compatible with buttons */
1125 // persp(PERSP_WIN); // set ortho
1129 void view3d_validate_backbuf(ViewContext *vc)
1131 if(vc->v3d->flag & V3D_INVALID_BACKBUF)
1132 backdrawview3d(vc->scene, vc->ar, vc->v3d);
1135 /* samples a single pixel (copied from vpaint) */
1136 unsigned int view3d_sample_backbuf(ViewContext *vc, int x, int y)
1140 if(x >= vc->ar->winx || y >= vc->ar->winy) return 0;
1141 x+= vc->ar->winrct.xmin;
1142 y+= vc->ar->winrct.ymin;
1144 view3d_validate_backbuf(vc);
1146 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
1147 glReadBuffer(GL_BACK);
1149 if(ENDIAN_ORDER==B_ENDIAN) SWITCH_INT(col);
1151 return WM_framebuffer_to_index(col);
1154 /* reads full rect, converts indices */
1155 ImBuf *view3d_read_backbuf(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
1157 unsigned int *dr, *rd;
1158 struct ImBuf *ibuf, *ibuf1;
1160 short xminc, yminc, xmaxc, ymaxc, xs, ys;
1163 if(xmin<0) xminc= 0; else xminc= xmin;
1164 if(xmax >= vc->ar->winx) xmaxc= vc->ar->winx-1; else xmaxc= xmax;
1165 if(xminc > xmaxc) return NULL;
1167 if(ymin<0) yminc= 0; else yminc= ymin;
1168 if(ymax >= vc->ar->winy) ymaxc= vc->ar->winy-1; else ymaxc= ymax;
1169 if(yminc > ymaxc) return NULL;
1171 ibuf= IMB_allocImBuf((xmaxc-xminc+1), (ymaxc-yminc+1), 32, IB_rect,0);
1173 view3d_validate_backbuf(vc);
1175 glReadPixels(vc->ar->winrct.xmin+xminc, vc->ar->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1176 glReadBuffer(GL_BACK);
1178 if(ENDIAN_ORDER==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1180 a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
1183 if(*dr) *dr= WM_framebuffer_to_index(*dr);
1187 /* put clipped result back, if needed */
1188 if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax)
1191 ibuf1= IMB_allocImBuf( (xmax-xmin+1),(ymax-ymin+1),32,IB_rect,0);
1195 for(ys= ymin; ys<=ymax; ys++) {
1196 for(xs= xmin; xs<=xmax; xs++, dr++) {
1197 if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
1203 IMB_freeImBuf(ibuf);
1207 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1208 unsigned int view3d_sample_backbuf_rect(ViewContext *vc, short mval[2], int size,
1209 unsigned int min, unsigned int max, int *dist, short strict,
1210 void *handle, unsigned int (*indextest)(void *handle, unsigned int index))
1213 unsigned int *bufmin, *bufmax, *tbuf;
1215 int a, b, rc, nr, amount, dirvec[4][2];
1217 unsigned int index = 0;
1222 minx = mval[0]-(amount+1);
1223 miny = mval[1]-(amount+1);
1224 buf = view3d_read_backbuf(vc, minx, miny, minx+size-1, miny+size-1);
1229 dirvec[0][0]= 1; dirvec[0][1]= 0;
1230 dirvec[1][0]= 0; dirvec[1][1]= -size;
1231 dirvec[2][0]= -1; dirvec[2][1]= 0;
1232 dirvec[3][0]= 0; dirvec[3][1]= size;
1236 bufmax = buf->rect + size*size;
1237 tbuf+= amount*size+ amount;
1239 for(nr=1; nr<=size; nr++) {
1241 for(a=0; a<2; a++) {
1242 for(b=0; b<nr; b++, distance++) {
1243 if (*tbuf && *tbuf>=min && *tbuf<max) { //we got a hit
1245 indexok = indextest(handle, *tbuf - min+1);
1247 *dist= (short) sqrt( (float)distance );
1248 index = *tbuf - min+1;
1253 *dist= (short) sqrt( (float)distance ); // XXX, this distance is wrong -
1254 index = *tbuf - min+1; // messy yah, but indices start at 1
1259 tbuf+= (dirvec[rc][0]+dirvec[rc][1]);
1261 if(tbuf<bufmin || tbuf>=bufmax) {
1276 /* ************************************************************* */
1278 static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
1280 RegionView3D *rv3d= ar->regiondata;
1284 float vec[4], fac, asp, zoomx, zoomy;
1285 float x1, y1, x2, y2, cx, cy;
1288 for ( bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next ) {
1290 if( (bgpic->view == 0) || /* zero for any */
1291 (bgpic->view & (1<<rv3d->view)) || /* check agaist flags */
1292 (rv3d->persp==RV3D_CAMOB && bgpic->view == (1<<RV3D_VIEW_CAMERA))
1297 BKE_image_user_calc_frame(&bgpic->iuser, CFRA, 0);
1298 ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
1299 if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL) )
1301 if(ibuf->channels!=4)
1303 if(ibuf->rect==NULL)
1304 IMB_rect_from_float(ibuf);
1306 if(rv3d->persp==RV3D_CAMOB) {
1309 calc_viewborder(scene, ar, v3d, &vb);
1319 /* calc window coord */
1320 initgrabz(rv3d, 0.0, 0.0, 0.0);
1321 window_to_3d_delta(ar, vec, 1, 0);
1322 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
1325 asp= ( (float)ibuf->y)/(float)ibuf->x;
1327 vec[0] = vec[1] = vec[2] = 0.0;
1328 view3d_project_float(ar, vec, sco, rv3d->persmat);
1332 x1= cx+ fac*(bgpic->xof-bgpic->size);
1333 y1= cy+ asp*fac*(bgpic->yof-bgpic->size);
1334 x2= cx+ fac*(bgpic->xof+bgpic->size);
1335 y2= cy+ asp*fac*(bgpic->yof+bgpic->size);
1338 /* complete clip? */
1340 if(x2 < 0 ) continue;
1341 if(y2 < 0 ) continue;
1342 if(x1 > ar->winx ) continue;
1343 if(y1 > ar->winy ) continue;
1345 zoomx= (x2-x1)/ibuf->x;
1346 zoomy= (y2-y1)/ibuf->y;
1348 /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
1349 if(zoomx < 1.0f || zoomy < 1.0f) {
1350 float tzoom= MIN2(zoomx, zoomy);
1353 if(ibuf->mipmap[0]==NULL)
1354 IMB_makemipmap(ibuf, 0);
1356 while(tzoom < 1.0f && mip<8 && ibuf->mipmap[mip]) {
1363 ibuf= ibuf->mipmap[mip-1];
1366 if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
1370 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1372 glMatrixMode(GL_PROJECTION);
1374 glMatrixMode(GL_MODELVIEW);
1376 ED_region_pixelspace(ar);
1378 glPixelZoom(zoomx, zoomy);
1379 glColor4f(1.0, 1.0, 1.0, 1.0-bgpic->blend);
1380 glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
1382 glPixelZoom(1.0, 1.0);
1383 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1385 glMatrixMode(GL_PROJECTION);
1387 glMatrixMode(GL_MODELVIEW);
1390 glDisable(GL_BLEND);
1393 if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1398 /* ****************** View3d afterdraw *************** */
1400 typedef struct View3DAfter {
1401 struct View3DAfter *next, *prev;
1406 /* temp storage of Objects that need to be drawn as last */
1407 void add_view3d_after(View3D *v3d, Base *base, int type, int flag)
1409 View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
1411 BLI_addtail(&v3d->afterdraw, v3da);
1417 /* clears zbuffer and draws it over */
1418 static void view3d_draw_xray(Scene *scene, ARegion *ar, View3D *v3d, int clear)
1420 View3DAfter *v3da, *next;
1423 for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
1424 if(v3da->type==V3D_XRAY) doit= 1;
1427 if(clear && v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
1430 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1432 if(v3da->type==V3D_XRAY) {
1433 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1434 BLI_remlink(&v3d->afterdraw, v3da);
1442 /* disables write in zbuffer and draws it over */
1443 static void view3d_draw_transp(Scene *scene, ARegion *ar, View3D *v3d)
1445 View3DAfter *v3da, *next;
1450 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1452 if(v3da->type==V3D_TRANSP) {
1453 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1454 BLI_remlink(&v3d->afterdraw, v3da);
1464 /* *********************** */
1467 In most cases call draw_dupli_objects,
1468 draw_dupli_objects_color was added because when drawing set dupli's
1469 we need to force the color
1473 int dupli_ob_sort(void *arg1, void *arg2)
1475 void *p1= ((DupliObject *)arg1)->ob;
1476 void *p2= ((DupliObject *)arg2)->ob;
1478 if (p1 < p2) val = -1;
1479 else if (p1 > p2) val = 1;
1485 static DupliObject *dupli_step(DupliObject *dob)
1487 while(dob && dob->no_draw)
1492 static void draw_dupli_objects_color(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int color)
1494 RegionView3D *rv3d= ar->regiondata;
1496 DupliObject *dob_prev= NULL, *dob, *dob_next;
1498 BoundBox bb, *bb_tmp; /* use a copy because draw_object, calls clear_mesh_caches */
1500 short transflag, use_displist= -1; /* -1 is initialize */
1503 if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
1505 tbase.flag= OB_FROMDUPLI|base->flag;
1506 lb= object_duplilist(scene, base->object);
1507 // BLI_sortlist(lb, dupli_ob_sort); // might be nice to have if we have a dupli list with mixed objects.
1509 dob=dupli_step(lb->first);
1510 if(dob) dob_next= dupli_step(dob->next);
1512 for( ; dob ; dob_prev= dob, dob= dob_next, dob_next= dob_next ? dupli_step(dob_next->next) : NULL) {
1513 tbase.object= dob->ob;
1515 /* extra service: draw the duplicator in drawtype of parent */
1516 /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
1517 dt= tbase.object->dt; tbase.object->dt= MIN2(tbase.object->dt, base->object->dt);
1518 dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
1520 /* negative scale flag has to propagate */
1521 transflag= tbase.object->transflag;
1522 if(base->object->transflag & OB_NEG_SCALE)
1523 tbase.object->transflag ^= OB_NEG_SCALE;
1525 UI_ThemeColorBlend(color, TH_BACK, 0.5);
1527 /* generate displist, test for new object */
1528 if(dob_prev && dob_prev->ob != dob->ob) {
1530 glDeleteLists(displist, 1);
1535 /* generate displist */
1536 if(use_displist == -1) {
1538 /* note, since this was added, its checked dob->type==OB_DUPLIGROUP
1539 * however this is very slow, it was probably needed for the NLA
1540 * offset feature (used in group-duplicate.blend but no longer works in 2.5)
1541 * so for now it should be ok to - campbell */
1543 if( (dob_next==NULL || dob_next->ob != dob->ob) || /* if this is the last no need to make a displist */
1544 (dob->ob->type == OB_LAMP) || /* lamp drawing messes with matrices, could be handled smarter... but this works */
1545 (dob->type == OB_DUPLIGROUP && dob->animated) ||
1546 !(bb_tmp= object_get_boundbox(dob->ob))
1548 // printf("draw_dupli_objects_color: skipping displist for %s\n", dob->ob->id.name+2);
1552 // printf("draw_dupli_objects_color: using displist for %s\n", dob->ob->id.name+2);
1553 bb= *bb_tmp; /* must make a copy */
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 unit_m4(dob->ob->obmat); /* obmat gets restored */
1560 displist= glGenLists(1);
1561 glNewList(displist, GL_COMPILE);
1562 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1566 object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
1570 glMultMatrixf(dob->mat);
1571 if(boundbox_clip(rv3d, dob->mat, &bb))
1572 glCallList(displist);
1573 glLoadMatrixf(rv3d->viewmat);
1576 copy_m4_m4(dob->ob->obmat, dob->mat);
1577 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1580 tbase.object->dt= dt;
1581 tbase.object->dtx= dtx;
1582 tbase.object->transflag= transflag;
1585 /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
1587 free_object_duplilist(lb); /* does restore */
1590 glDeleteLists(displist, 1);
1593 static void draw_dupli_objects(Scene *scene, ARegion *ar, View3D *v3d, Base *base)
1595 /* define the color here so draw_dupli_objects_color can be called
1596 * from the set loop */
1598 int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
1600 if(base->object->dup_group && base->object->dup_group->id.us<1)
1603 draw_dupli_objects_color(scene, ar, v3d, base, color);
1607 void view3d_update_depths(ARegion *ar, View3D *v3d)
1609 RegionView3D *rv3d= ar->regiondata;
1611 /* Create storage for, and, if necessary, copy depth buffer */
1612 if(!rv3d->depths) rv3d->depths= MEM_callocN(sizeof(ViewDepths),"ViewDepths");
1614 ViewDepths *d= rv3d->depths;
1615 if(d->w != ar->winx ||
1621 MEM_freeN(d->depths);
1622 d->depths= MEM_mallocN(sizeof(float)*d->w*d->h,"View depths");
1627 glReadPixels(ar->winrct.xmin,ar->winrct.ymin,d->w,d->h,
1628 GL_DEPTH_COMPONENT,GL_FLOAT, d->depths);
1630 glGetDoublev(GL_DEPTH_RANGE,d->depth_range);
1637 void draw_depth_gpencil(Scene *scene, ARegion *ar, View3D *v3d)
1639 short zbuf= v3d->zbuf;
1640 RegionView3D *rv3d= ar->regiondata;
1642 setwinmatrixview3d(ar, v3d, NULL); /* 0= no pick rect */
1643 setviewmatrixview3d(scene, v3d, rv3d); /* note: calls where_is_object for camera... */
1645 mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1646 invert_m4_m4(rv3d->persinv, rv3d->persmat);
1647 invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
1649 glClear(GL_DEPTH_BUFFER_BIT);
1651 glLoadMatrixf(rv3d->viewmat);
1654 glEnable(GL_DEPTH_TEST);
1656 draw_gpencil_3dview_ext(scene, ar, 1);
1662 void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (* func)(void *))
1664 RegionView3D *rv3d= ar->regiondata;
1667 short zbuf= v3d->zbuf;
1668 short flag= v3d->flag;
1669 float glalphaclip= U.glalphaclip;
1670 int obcenter_dia= U.obcenter_dia;
1671 /* temp set drawtype to solid */
1673 /* Setting these temporarily is not nice */
1674 v3d->flag &= ~V3D_SELECT_OUTLINE;
1675 U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
1678 setwinmatrixview3d(ar, v3d, NULL); /* 0= no pick rect */
1679 setviewmatrixview3d(scene, v3d, rv3d); /* note: calls where_is_object for camera... */
1681 mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1682 invert_m4_m4(rv3d->persinv, rv3d->persmat);
1683 invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
1685 glClear(GL_DEPTH_BUFFER_BIT);
1687 glLoadMatrixf(rv3d->viewmat);
1688 // persp(PERSP_STORE); // store correct view for persp(PERSP_VIEW) calls
1690 if(rv3d->rflag & RV3D_CLIPPING) {
1691 view3d_set_clipping(rv3d);
1695 glEnable(GL_DEPTH_TEST);
1697 /* draw set first */
1699 for(SETLOOPER(scene->set, base)) {
1700 if(v3d->lay & base->lay) {
1701 if (func == NULL || func(base)) {
1702 draw_object(scene, ar, v3d, base, 0);
1703 if(base->object->transflag & OB_DUPLI) {
1704 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
1711 for(base= scene->base.first; base; base= base->next) {
1712 if(v3d->lay & base->lay) {
1713 if (func == NULL || func(base)) {
1715 if(base->object->transflag & OB_DUPLI) {
1716 draw_dupli_objects(scene, ar, v3d, base);
1718 draw_object(scene, ar, v3d, base, 0);
1723 /* this isnt that nice, draw xray objects as if they are normal */
1724 if (v3d->afterdraw.first) {
1725 View3DAfter *v3da, *next;
1729 glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
1730 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1732 if(v3da->type==V3D_XRAY) {
1733 draw_object(scene, ar, v3d, v3da->base, 0);
1736 /* dont remove this time */
1740 glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
1741 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1743 if(v3da->type==V3D_XRAY) {
1744 v3d->xray= TRUE; v3d->transp= FALSE;
1745 } else if (v3da->type==V3D_TRANSP) {
1746 v3d->xray= FALSE; v3d->transp= TRUE;
1749 draw_object(scene, ar, v3d, v3da->base, 0); /* Draw Xray or Transp objects normally */
1750 BLI_remlink(&v3d->afterdraw, v3da);
1757 if(rv3d->rflag & RV3D_CLIPPING)
1758 view3d_clr_clipping();
1761 if(!v3d->zbuf) glDisable(GL_DEPTH_TEST);
1763 U.glalphaclip = glalphaclip;
1765 U.obcenter_dia= obcenter_dia;
1768 typedef struct View3DShadow {
1769 struct View3DShadow *next, *prev;
1773 static void gpu_render_lamp_update(Scene *scene, View3D *v3d, Object *ob, Object *par, float obmat[][4], ListBase *shadows)
1776 Lamp *la = (Lamp*)ob->data;
1777 View3DShadow *shadow;
1779 lamp = GPU_lamp_from_blender(scene, ob, par);
1782 GPU_lamp_update(lamp, ob->lay, obmat);
1783 GPU_lamp_update_colors(lamp, la->r, la->g, la->b, la->energy);
1785 if((ob->lay & v3d->lay) && GPU_lamp_has_shadow_buffer(lamp)) {
1786 shadow= MEM_callocN(sizeof(View3DShadow), "View3DShadow");
1787 shadow->lamp = lamp;
1788 BLI_addtail(shadows, shadow);
1793 static void gpu_update_lamps_shadows(Scene *scene, View3D *v3d)
1796 View3DShadow *shadow;
1801 shadows.first= shadows.last= NULL;
1803 /* update lamp transform and gather shadow lamps */
1804 for(SETLOOPER(scene, base)) {
1807 if(ob->type == OB_LAMP)
1808 gpu_render_lamp_update(scene, v3d, ob, NULL, ob->obmat, &shadows);
1810 if (ob->transflag & OB_DUPLI) {
1812 ListBase *lb = object_duplilist(scene, ob);
1814 for(dob=lb->first; dob; dob=dob->next)
1815 if(dob->ob->type==OB_LAMP)
1816 gpu_render_lamp_update(scene, v3d, dob->ob, ob, dob->mat, &shadows);
1818 free_object_duplilist(lb);
1822 /* render shadows after updating all lamps, nested object_duplilist
1823 * don't work correct since it's replacing object matrices */
1824 for(shadow=shadows.first; shadow; shadow=shadow->next) {
1825 /* this needs to be done better .. */
1826 float viewmat[4][4], winmat[4][4];
1827 int drawtype, lay, winsize, flag2;
1829 drawtype= v3d->drawtype;
1831 flag2= v3d->flag2 & V3D_SOLID_TEX;
1833 v3d->drawtype = OB_SOLID;
1834 v3d->lay &= GPU_lamp_shadow_layer(shadow->lamp);
1835 v3d->flag2 &= ~V3D_SOLID_TEX;
1837 GPU_lamp_shadow_buffer_bind(shadow->lamp, viewmat, &winsize, winmat);
1838 // XXX drawview3d_render(v3d, viewmat, winsize, winsize, winmat, 1);
1839 GPU_lamp_shadow_buffer_unbind(shadow->lamp);
1841 v3d->drawtype= drawtype;
1843 v3d->flag2 |= flag2;
1846 BLI_freelistN(&shadows);
1849 /* *********************** customdata **************** */
1851 /* goes over all modes and view3d settings */
1852 static CustomDataMask get_viewedit_datamask(bScreen *screen, Scene *scene, Object *ob)
1854 CustomDataMask mask = CD_MASK_BAREMESH;
1857 /* check if we need tfaces & mcols due to face select or texture paint */
1858 if(paint_facesel_test(ob) || (ob && ob->mode & OB_MODE_TEXTURE_PAINT))
1859 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1861 /* check if we need tfaces & mcols due to view mode */
1862 for(sa = screen->areabase.first; sa; sa = sa->next) {
1863 if(sa->spacetype == SPACE_VIEW3D) {
1864 View3D *view = sa->spacedata.first;
1865 if(view->drawtype == OB_SHADED) {
1866 /* this includes normals for mesh_create_shadedColors */
1867 mask |= CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_NORMAL | CD_MASK_ORCO;
1869 if((view->drawtype == OB_TEXTURE) || ((view->drawtype == OB_SOLID) && (view->flag2 & V3D_SOLID_TEX))) {
1870 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1872 if(scene->gm.matmode == GAME_MAT_GLSL)
1873 mask |= CD_MASK_ORCO;
1878 /* check if we need mcols due to vertex paint or weightpaint */
1880 if(ob->mode & OB_MODE_VERTEX_PAINT)
1881 mask |= CD_MASK_MCOL;
1882 if(ob->mode & OB_MODE_WEIGHT_PAINT)
1883 mask |= CD_MASK_WEIGHT_MCOL;
1889 static void view3d_main_area_setup_view(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[][4], float winmat[][4])
1891 RegionView3D *rv3d= ar->regiondata;
1893 /* setup window matrices */
1895 copy_m4_m4(rv3d->winmat, winmat);
1897 setwinmatrixview3d(ar, v3d, NULL); /* NULL= no pickrect */
1899 /* setup view matrix */
1901 copy_m4_m4(rv3d->viewmat, viewmat);
1903 setviewmatrixview3d(scene, v3d, rv3d); /* note: calls where_is_object for camera... */
1905 /* update utilitity matrices */
1906 mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1907 invert_m4_m4(rv3d->persinv, rv3d->persmat);
1908 invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
1910 /* calculate pixelsize factor once, is used for lamps and obcenters */
1912 float len1, len2, vec[3];
1914 VECCOPY(vec, rv3d->persinv[0]);
1915 len1= normalize_v3(vec);
1916 VECCOPY(vec, rv3d->persinv[1]);
1917 len2= normalize_v3(vec);
1919 rv3d->pixsize= 2.0f*(len1>len2?len1:len2);
1921 /* correct for window size */
1922 if(ar->winx > ar->winy) rv3d->pixsize/= (float)ar->winx;
1923 else rv3d->pixsize/= (float)ar->winy;
1926 /* set for opengl */
1927 glMatrixMode(GL_PROJECTION);
1928 glLoadMatrixf(rv3d->winmat);
1929 glMatrixMode(GL_MODELVIEW);
1930 glLoadMatrixf(rv3d->viewmat);
1933 void ED_view3d_draw_offscreen(Scene *scene, View3D *v3d, ARegion *ar, int winx, int winy, float viewmat[][4], float winmat[][4])
1941 /* set temporary new size */
1948 G.f |= G_RENDER_OGL;
1951 /* set background color, fallback on the view background color */
1953 glClearColor(scene->world->horr, scene->world->horg, scene->world->horb, 0.0);
1957 UI_GetThemeColor3fv(TH_BACK, col);
1958 glClearColor(col[0], col[1], col[2], 0.0);
1961 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1963 /* setup view matrices */
1964 view3d_main_area_setup_view(scene, v3d, ar, viewmat, winmat);
1967 if(v3d->drawtype > OB_WIRE) {
1969 glEnable(GL_DEPTH_TEST);
1974 /* draw set first */
1976 for(SETLOOPER(scene->set, base)) {
1977 if(v3d->lay & base->lay) {
1978 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
1979 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
1981 if(base->object->transflag & OB_DUPLI)
1982 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
1987 /* then draw not selected and the duplis, but skip editmode object */
1988 for(base= scene->base.first; base; base= base->next) {
1989 if(v3d->lay & base->lay) {
1991 if(base->object->transflag & OB_DUPLI)
1992 draw_dupli_objects(scene, ar, v3d, base);
1994 draw_object(scene, ar, v3d, base, 0);
1998 /* transp and X-ray afterdraw stuff */
1999 view3d_draw_transp(scene, ar, v3d);
2000 view3d_draw_xray(scene, ar, v3d, 1); // clears zbuffer if it is used!
2005 glDisable(GL_DEPTH_TEST);
2008 /* draw grease-pencil stuff */
2009 draw_gpencil_3dview_ext(scene, ar, 1);
2011 ED_region_pixelspace(ar);
2013 /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
2014 draw_gpencil_3dview_ext(scene, ar, 0);
2024 glColor4ub(255, 255, 255, 255); // XXX, without this the sequencer flickers with opengl draw enabled, need to find out why - campbell
2027 /* utility func for ED_view3d_draw_offscreen */
2028 ImBuf *ED_view3d_draw_offscreen_imbuf(Scene *scene, View3D *v3d, ARegion *ar, int sizex, int sizey)
2030 RegionView3D *rv3d= ar->regiondata;
2035 ofs= GPU_offscreen_create(sizex, sizey);
2039 GPU_offscreen_bind(ofs);
2041 /* render 3d view */
2042 if(rv3d->persp==RV3D_CAMOB && v3d->camera) {
2044 float _clipsta, _clipend, _lens, _yco, _dx, _dy;
2047 object_camera_matrix(&scene->r, v3d->camera, sizex, sizey, 0, winmat, &_viewplane, &_clipsta, &_clipend, &_lens, &_yco, &_dx, &_dy);
2049 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat);
2052 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, NULL);
2055 /* read in pixels & stamp */
2056 ibuf= IMB_allocImBuf(sizex, sizey, 24, IB_rect, 0);
2057 glReadPixels(0, 0, sizex, sizey, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2059 //if((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
2060 // BKE_stamp_buf(scene, NULL, rr->rectf, rr->rectx, rr->recty, 4);
2063 GPU_offscreen_unbind(ofs);
2064 GPU_offscreen_free(ofs);
2069 /* creates own 3d views, used by the sequencer */
2070 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(Scene *scene, int width, int height, int drawtype)
2076 memset(&v3d, 0, sizeof(v3d));
2077 memset(&ar, 0, sizeof(ar));
2078 memset(&rv3d, 0, sizeof(rv3d));
2081 v3d.regionbase.first= v3d.regionbase.last= &ar;
2082 ar.regiondata= &rv3d;
2083 ar.regiontype= RGN_TYPE_WINDOW;
2085 v3d.camera= scene->camera;
2086 v3d.lay= scene->lay;
2087 v3d.drawtype = drawtype;
2088 v3d.flag2 = V3D_RENDER_OVERRIDE;
2090 rv3d.persp= RV3D_CAMOB;
2092 copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
2093 normalize_m4(rv3d.viewinv);
2094 invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
2097 float _yco, _dx, _dy;
2099 object_camera_matrix(&scene->r, v3d.camera, width, height, 0, rv3d.winmat, &_viewplane, &v3d.near, &v3d.far, &v3d.lens, &_yco, &_dx, &_dy);
2102 mul_m4_m4m4(rv3d.persmat, rv3d.viewmat, rv3d.winmat);
2103 invert_m4_m4(rv3d.persinv, rv3d.viewinv);
2105 return ED_view3d_draw_offscreen_imbuf(scene, &v3d, &ar, width, height);
2107 // seq_view3d_cb(scene, cfra, render_size, seqrectx, seqrecty);
2111 /* NOTE: the info that this uses is updated in ED_refresh_viewport_fps(),
2112 * which currently gets called during SCREEN_OT_animation_step.
2114 static void draw_viewport_fps(Scene *scene, ARegion *ar)
2116 ScreenFrameRateInfo *fpsi= scene->fps_info;
2121 if (!fpsi || !fpsi->lredrawtime || !fpsi->redrawtime)
2124 printable[0] = '\0';
2127 /* this is too simple, better do an average */
2128 fps = (float)(1.0/(fpsi->lredrawtime-fpsi->redrawtime))
2130 fpsi->redrawtimes_fps[fpsi->redrawtime_index] = (float)(1.0/(fpsi->lredrawtime-fpsi->redrawtime));
2132 for (i=0, tot=0, fps=0.0f ; i < REDRAW_FRAME_AVERAGE ; i++) {
2133 if (fpsi->redrawtimes_fps[i]) {
2134 fps += fpsi->redrawtimes_fps[i];
2139 fpsi->redrawtime_index = (fpsi->redrawtime_index + 1) % REDRAW_FRAME_AVERAGE;
2141 //fpsi->redrawtime_index++;
2142 //if (fpsi->redrawtime >= REDRAW_FRAME_AVERAGE)
2143 // fpsi->redrawtime = 0;
2149 /* is this more then half a frame behind? */
2150 if (fps+0.5 < FPS) {
2151 UI_ThemeColor(TH_REDALERT);
2152 sprintf(printable, "fps: %.2f", (float)fps);
2155 UI_ThemeColor(TH_TEXT_HI);
2156 sprintf(printable, "fps: %i", (int)(fps+0.5));
2159 BLF_draw_default(22, ar->winy-17, 0.0f, printable);
2162 void view3d_main_area_draw(const bContext *C, ARegion *ar)
2164 Scene *scene= CTX_data_scene(C);
2165 View3D *v3d = CTX_wm_view3d(C);
2166 RegionView3D *rv3d= CTX_wm_region_view3d(C);
2171 int retopo= 0, sculptparticle= 0;
2172 Object *obact = OBACT;
2173 char *grid_unit= NULL;
2175 /* from now on all object derived meshes check this */
2176 v3d->customdata_mask= get_viewedit_datamask(CTX_wm_screen(C), scene, obact);
2178 /* shadow buffers, before we setup matrices */
2179 if(draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
2180 gpu_update_lamps_shadows(scene, v3d);
2182 /* reset default OpenGL lights if needed (i.e. after preferences have been altered) */
2183 if (rv3d->rflag & RV3D_GPULIGHT_UPDATE) {
2184 rv3d->rflag &= ~RV3D_GPULIGHT_UPDATE;
2185 GPU_default_lights();
2188 /* clear background */
2189 UI_GetThemeColor3fv(TH_BACK, col);
2190 glClearColor(col[0], col[1], col[2], 0.0);
2191 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
2193 /* setup view matrices */
2194 view3d_main_area_setup_view(scene, v3d, ar, NULL, NULL);
2196 ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
2198 if(rv3d->rflag & RV3D_CLIPPING)
2199 view3d_draw_clipping(rv3d);
2201 /* set zbuffer after we draw clipping region */
2202 if(v3d->drawtype > OB_WIRE) {
2204 glEnable(GL_DEPTH_TEST);
2209 /* enables anti-aliasing for 3D view drawing */
2210 /*if (!(U.gameflags & USER_DISABLE_AA))
2211 glEnable(GL_MULTISAMPLE_ARB);*/
2213 // needs to be done always, gridview is adjusted in drawgrid() now
2214 rv3d->gridview= v3d->grid;
2216 if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
2218 if(rv3d->view==0 || rv3d->persp != RV3D_ORTHO) {
2219 drawfloor(scene, v3d);
2220 if(rv3d->persp==RV3D_CAMOB) {
2222 if(scene->world->mode & WO_STARS) {
2223 RE_make_stars(NULL, scene, star_stuff_init_func, star_stuff_vertex_func,
2224 star_stuff_term_func);
2227 if(v3d->flag & V3D_DISPBGPICS) draw_bgpic(scene, ar, v3d);
2231 ED_region_pixelspace(ar);
2232 drawgrid(&scene->unit, ar, v3d, &grid_unit);
2233 /* XXX make function? replaces persp(1) */
2234 glMatrixMode(GL_PROJECTION);
2235 glLoadMatrixf(rv3d->winmat);
2236 glMatrixMode(GL_MODELVIEW);
2237 glLoadMatrixf(rv3d->viewmat);
2239 if(v3d->flag & V3D_DISPBGPICS) {
2240 draw_bgpic(scene, ar, v3d);
2245 if(rv3d->rflag & RV3D_CLIPPING)
2246 view3d_set_clipping(rv3d);
2248 /* draw set first */
2250 for(SETLOOPER(scene->set, base)) {
2252 if(v3d->lay & base->lay) {
2254 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2255 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
2257 if(base->object->transflag & OB_DUPLI) {
2258 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2263 /* Transp and X-ray afterdraw stuff for sets is done later */
2266 /* extra service in layerbuttons, showing used layers */
2269 /* then draw not selected and the duplis, but skip editmode object */
2270 for(base= scene->base.first; base; base= base->next) {
2271 v3d->lay_used |= base->lay;
2273 if(v3d->lay & base->lay) {
2276 if(base->object->transflag & OB_DUPLI) {
2277 draw_dupli_objects(scene, ar, v3d, base);
2279 if((base->flag & SELECT)==0) {
2280 if(base->object!=scene->obedit)
2281 draw_object(scene, ar, v3d, base, 0);
2286 // retopo= retopo_mesh_check() || retopo_curve_check();
2287 sculptparticle= (obact && obact->mode & (OB_MODE_PARTICLE_EDIT)) && !scene->obedit;
2289 view3d_update_depths(ar, v3d);
2291 /* draw selected and editmode */
2292 for(base= scene->base.first; base; base= base->next) {
2293 if(v3d->lay & base->lay) {
2294 if (base->object==scene->obedit || ( base->flag & SELECT) )
2295 draw_object(scene, ar, v3d, base, 0);
2299 if(!retopo && sculptparticle && !(obact && (obact->dtx & OB_DRAWXRAY))) {
2300 view3d_update_depths(ar, v3d);
2305 /* Transp and X-ray afterdraw stuff */
2306 view3d_draw_transp(scene, ar, v3d);
2307 view3d_draw_xray(scene, ar, v3d, 1); // clears zbuffer if it is used!
2309 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
2311 if(!retopo && sculptparticle && (obact && (OBACT->dtx & OB_DRAWXRAY))) {
2312 view3d_update_depths(ar, v3d);
2315 if(rv3d->rflag & RV3D_CLIPPING)
2316 view3d_clr_clipping();
2318 BIF_draw_manipulator(C);
2320 /* Disable back anti-aliasing */
2321 /*if (!(U.gameflags & USER_DISABLE_AA))
2322 glDisable(GL_MULTISAMPLE_ARB);*/
2326 glDisable(GL_DEPTH_TEST);
2329 if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
2330 /* draw grease-pencil stuff (3d-space strokes) */
2331 //if (v3d->flag2 & V3D_DISPGP)
2332 draw_gpencil_3dview((bContext *)C, 1);
2337 ED_region_pixelspace(ar);
2339 // retopo_paint_view_update(v3d);
2340 // retopo_draw_paint_lines();
2342 /* Draw particle edit brush XXX (removed) */
2345 if(rv3d->persp==RV3D_CAMOB) drawviewborder(scene, ar, v3d);
2346 if(rv3d->rflag & RV3D_FLYMODE) drawviewborder_flymode(ar);
2348 if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
2349 /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
2350 // if (v3d->flag2 & V3D_DISPGP)
2351 draw_gpencil_3dview((bContext *)C, 0);
2353 drawcursor(scene, ar, v3d);
2356 if(U.uiflag & USER_SHOW_ROTVIEWICON)
2357 draw_view_axis(rv3d);
2359 draw_view_icon(rv3d);
2361 if((U.uiflag & USER_SHOW_FPS) && (CTX_wm_screen(C)->animtimer)) {
2362 draw_viewport_fps(scene, ar);
2364 else if(U.uiflag & USER_SHOW_VIEWPORTNAME) {
2365 draw_viewport_name(ar, v3d);
2367 if (grid_unit) { /* draw below the viewport name */
2368 UI_ThemeColor(TH_TEXT_HI);
2369 BLF_draw_default(10, ar->winy-(USER_SHOW_VIEWPORTNAME?40:20), 0.0f, grid_unit);
2373 if(U.uiflag & USER_DRAWVIEWINFO)
2374 draw_selected_name(scene, ob, v3d);
2376 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
2378 /* XXX here was the blockhandlers for floating panels */
2380 v3d->flag |= V3D_INVALID_BACKBUF;