[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [36793] trunk/blender: simplify window_to_3d_vector() and call it from viewline()

Campbell Barton ideasman42 at gmail.com
Fri May 20 12:28:40 CEST 2011


Revision: 36793
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=36793
Author:   campbellbarton
Date:     2011-05-20 10:28:40 +0000 (Fri, 20 May 2011)
Log Message:
-----------
simplify window_to_3d_vector() and call it from viewline()
also update python view function to match.

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/bpy_extras/view3d_utils.py
    trunk/blender/source/blender/editors/include/ED_view3d.h
    trunk/blender/source/blender/editors/space_view3d/view3d_edit.c
    trunk/blender/source/blender/editors/space_view3d/view3d_view.c

Modified: trunk/blender/release/scripts/modules/bpy_extras/view3d_utils.py
===================================================================
--- trunk/blender/release/scripts/modules/bpy_extras/view3d_utils.py	2011-05-20 10:09:03 UTC (rev 36792)
+++ trunk/blender/release/scripts/modules/bpy_extras/view3d_utils.py	2011-05-20 10:28:40 UTC (rev 36793)
@@ -36,17 +36,19 @@
     """
     from mathutils import Vector
 
-    viewvec = rv3d.view_matrix.inverted()[2].xyz.normalized()
-
     if rv3d.is_perspective:
-        dx = (2.0 * coord[0] / region.width) - 1.0
-        dy = (2.0 * coord[1] / region.height) - 1.0
+        persinv = rv3d.perspective_matrix.inverted()
 
-        persmat = rv3d.perspective_matrix.copy()
-        perspinv_x, perspinv_y = persmat.inverted().to_3x3()[0:2]
-        return ((perspinv_x * dx + perspinv_y * dy) - viewvec).normalized()
+        out = Vector(((2.0 * coord[0] / region.width) - 1.0,
+                      (2.0 * coord[1] / region.height) - 1.0,
+                      -0.5
+                    ))        
+
+        w = (out[0] * persinv[0][3]) + (out[1] * persinv[1][3]) + (out[2] * persinv[2][3]) + persinv[3][3]
+        
+        return ((out * persinv) / w) - rv3d.view_matrix.inverted()[3].xyz
     else:
-        return viewvec
+        return rv3d.view_matrix.inverted()[2].xyz.normalized()
 
 
 def region_2d_to_location_3d(region, rv3d, coord, depth_location):

Modified: trunk/blender/source/blender/editors/include/ED_view3d.h
===================================================================
--- trunk/blender/source/blender/editors/include/ED_view3d.h	2011-05-20 10:09:03 UTC (rev 36792)
+++ trunk/blender/source/blender/editors/include/ED_view3d.h	2011-05-20 10:28:40 UTC (rev 36793)
@@ -104,10 +104,10 @@
 void project_float(struct ARegion *ar, const float vec[3], float adr[2]);
 void project_float_noclip(struct ARegion *ar, const float vec[3], float adr[2]);
 
-void viewvector(struct RegionView3D *rv3d, float coord[3], float vec[3]);
+void viewvector(struct RegionView3D *rv3d, const float coord[3], float vec[3]);
 
-void viewline(struct ARegion *ar, struct View3D *v3d, float mval[2], float ray_start[3], float ray_end[3]);
-void viewray(struct ARegion *ar, struct View3D *v3d, float mval[2], float ray_start[3], float ray_normal[3]);
+void viewline(struct ARegion *ar, struct View3D *v3d, const float mval[2], float ray_start[3], float ray_end[3]);
+void viewray(struct ARegion *ar, struct View3D *v3d, const float mval[2], float ray_start[3], float ray_normal[3]);
 
 void get_object_clip_range(struct Object *ob, float *lens, float *clipsta, float *clipend);
 int get_view3d_cliprange(struct View3D *v3d, struct RegionView3D *rv3d, float *clipsta, float *clipend);
@@ -198,8 +198,4 @@
 /* copy the view to the camera */
 void ED_view3d_camera_lock_sync(struct View3D *v3d, struct RegionView3D *rv3d);
 
-int view3d_is_ortho(struct View3D *v3d, struct RegionView3D *rv3d);
-
-
 #endif /* ED_VIEW3D_H */
-

Modified: trunk/blender/source/blender/editors/space_view3d/view3d_edit.c
===================================================================
--- trunk/blender/source/blender/editors/space_view3d/view3d_edit.c	2011-05-20 10:09:03 UTC (rev 36792)
+++ trunk/blender/source/blender/editors/space_view3d/view3d_edit.c	2011-05-20 10:28:40 UTC (rev 36793)
@@ -419,7 +419,7 @@
 	}
 
 	/* for dolly */
-	window_to_3d_vector(vod->ar, vod->mousevec, vod->oldx - vod->ar->winrct.xmin, vod->oldy - vod->ar->winrct.ymin);
+	window_to_3d_vector(vod->ar, vod->mousevec, event->mval[0], event->mval[1]);
 
 	/* lookup, we dont pass on v3d to prevent confusement */
 	vod->grid= vod->v3d->grid;

Modified: trunk/blender/source/blender/editors/space_view3d/view3d_view.c
===================================================================
--- trunk/blender/source/blender/editors/space_view3d/view3d_view.c	2011-05-20 10:09:03 UTC (rev 36792)
+++ trunk/blender/source/blender/editors/space_view3d/view3d_view.c	2011-05-20 10:28:40 UTC (rev 36793)
@@ -502,29 +502,20 @@
 }
 
 /* create intersection coordinates in view Z direction at mouse coordinates */
-void viewline(ARegion *ar, View3D *v3d, float mval[2], float ray_start[3], float ray_end[3])
+void viewline(ARegion *ar, View3D *v3d, const float mval[2], float ray_start[3], float ray_end[3])
 {
 	RegionView3D *rv3d= ar->regiondata;
-	float vec[4];
-	int a;
 	
 	if(rv3d->is_persp) {
-		vec[0]= 2.0f * mval[0] / ar->winx - 1;
-		vec[1]= 2.0f * mval[1] / ar->winy - 1;
-		vec[2]= -1.0f;
-		vec[3]= 1.0f;
-		
-		mul_m4_v4(rv3d->persinv, vec);
-		mul_v3_fl(vec, 1.0f / vec[3]);
-		
+		float vec[3];
+		window_to_3d_vector(ar, vec, mval[0], mval[1]);
+
 		copy_v3_v3(ray_start, rv3d->viewinv[3]);
-		sub_v3_v3(vec, ray_start);
-		normalize_v3(vec);
-		
 		VECADDFAC(ray_start, rv3d->viewinv[3], vec, v3d->near);
 		VECADDFAC(ray_end, rv3d->viewinv[3], vec, v3d->far);
 	}
 	else {
+		float vec[4];
 		vec[0] = 2.0f * mval[0] / ar->winx - 1;
 		vec[1] = 2.0f * mval[1] / ar->winy - 1;
 		vec[2] = 0.0f;
@@ -537,13 +528,16 @@
 	}
 
 	/* clipping */
-	if(rv3d->rflag & RV3D_CLIPPING)
-		for(a=0; a<4; a++)
+	if(rv3d->rflag & RV3D_CLIPPING) {
+		int a;
+		for(a=0; a<4; a++) {
 			clip_line_plane(ray_start, ray_end, rv3d->clip[a]);
+		}
+	}
 }
 
 /* create intersection ray in view Z direction at mouse coordinates */
-void viewray(ARegion *ar, View3D *v3d, float mval[2], float ray_start[3], float ray_normal[3])
+void viewray(ARegion *ar, View3D *v3d, const float mval[2], float ray_start[3], float ray_normal[3])
 {
 	float ray_end[3];
 	
@@ -552,7 +546,7 @@
 	normalize_v3(ray_normal);
 }
 
-void viewvector(RegionView3D *rv3d, float coord[3], float vec[3])
+void viewvector(RegionView3D *rv3d, const float coord[3], float vec[3])
 {
 	if (rv3d->persp != RV3D_ORTHO)
 	{
@@ -652,18 +646,11 @@
 	RegionView3D *rv3d= ar->regiondata;
 
 	if(rv3d->is_persp) {
-		float dx, dy;
-		float viewvec[3];
-
-		dx= (2.0f * mx / ar->winx) - 1.0f;
-		dy= (2.0f * my / ar->winy) - 1.0f;
-
-		/* normalize here so vecs are proportional to eachother */
-		normalize_v3_v3(viewvec, rv3d->viewinv[2]);
-
-		out[0]= (rv3d->persinv[0][0]*dx + rv3d->persinv[1][0]*dy) - viewvec[0];
-		out[1]= (rv3d->persinv[0][1]*dx + rv3d->persinv[1][1]*dy) - viewvec[1];
-		out[2]= (rv3d->persinv[0][2]*dx + rv3d->persinv[1][2]*dy) - viewvec[2];
+		out[0]= 2.0f * (mx / ar->winx) - 1.0f;
+		out[1]= 2.0f * (my / ar->winy) - 1.0f;
+		out[2]= -0.5f;
+		mul_project_m4_v3(rv3d->persinv, out);
+		sub_v3_v3(out, rv3d->viewinv[3]);
 	}
 	else {
 		copy_v3_v3(out, rv3d->viewinv[2]);
@@ -1887,11 +1874,6 @@
 	}
 }
 
-int view3d_is_ortho(View3D *v3d, RegionView3D *rv3d)
-{
-	return (rv3d->persp == RV3D_ORTHO || (v3d->camera && ((Camera *)v3d->camera->data)->type == CAM_ORTHO));
-}
-
 float view3d_pixel_size(struct RegionView3D *rv3d, const float co[3])
 {
 	return  (rv3d->persmat[3][3] + (




More information about the Bf-blender-cvs mailing list