[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [27695] trunk/blender/source/blender: move uv project functions into their own files to be more re-usable.

Campbell Barton ideasman42 at gmail.com
Tue Mar 23 19:28:39 CET 2010


Revision: 27695
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=27695
Author:   campbellbarton
Date:     2010-03-23 19:28:38 +0100 (Tue, 23 Mar 2010)

Log Message:
-----------
move uv project functions into their own files to be more re-usable.

Modified Paths:
--------------
    trunk/blender/source/blender/editors/uvedit/uvedit_unwrap_ops.c

Added Paths:
-----------
    trunk/blender/source/blender/blenlib/BLI_uvproject.h
    trunk/blender/source/blender/blenlib/intern/uvproject.c

Added: trunk/blender/source/blender/blenlib/BLI_uvproject.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_uvproject.h	                        (rev 0)
+++ trunk/blender/source/blender/blenlib/BLI_uvproject.h	2010-03-23 18:28:38 UTC (rev 27695)
@@ -0,0 +1,40 @@
+/**
+ *	
+ * $Id:
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+#ifndef BKE_UVPROJECT_H
+#define BKE_UVPROJECT_H
+
+struct UvCameraInfo;
+
+/* create uv info from the camera, needs to be freed */
+struct UvCameraInfo *project_camera_info(struct Object *ob, float rotmat[4][4], float winx, float winy);
+
+/* apply uv from uvinfo (camera) */
+void project_from_camera(float target[2], float source[3], struct UvCameraInfo *uci);
+
+/* apply uv from perspective matrix */
+void project_from_view(float target[2], float source[3], float persmat[4][4], float rotmat[4][4], float winx, float winy);
+
+/* apply ortho uv's */
+void project_from_view_ortho(float target[2], float source[3], float rotmat[4][4]);
+
+#endif
\ No newline at end of file

Added: trunk/blender/source/blender/blenlib/intern/uvproject.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/uvproject.c	                        (rev 0)
+++ trunk/blender/source/blender/blenlib/intern/uvproject.c	2010-03-23 18:28:38 UTC (rev 27695)
@@ -0,0 +1,174 @@
+/**
+ * $Id:
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <math.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_camera_types.h"
+#include "DNA_object_types.h"
+
+#include "BLI_math.h"
+
+typedef struct UvCameraInfo {
+	float camangle;
+	float camsize;
+	float xasp, yasp;
+	float shiftx, shifty;
+	float rotmat[4][4];
+	float caminv[4][4];
+	short do_persp, do_pano;
+} UvCameraInfo;
+
+void project_from_camera(float target[2], float source[3], UvCameraInfo *uci)
+{
+	float pv4[4];
+
+	copy_v3_v3(pv4, source);
+	pv4[3]= 1.0;
+
+	/* rotmat is the object matrix in this case */
+	mul_m4_v4(uci->rotmat, pv4);
+
+	/* caminv is the inverse camera matrix */
+	mul_m4_v4(uci->caminv, pv4);
+
+	if(uci->do_pano) {
+		float angle= atan2f(pv4[0], -pv4[2]) / (M_PI * 2.0); /* angle around the camera */
+		if (uci->do_persp==0) {
+			target[0] = angle; /* no correct method here, just map to  0-1 */
+			target[1] = pv4[1] / uci->camsize;
+		}
+		else {
+			float vec2d[2]= {pv4[0], pv4[2]}; /* 2D position from the camera */
+			target[0] = angle * (M_PI / uci->camangle);
+			target[1] = pv4[1] / (len_v2(vec2d) * uci->camsize);
+		}
+	}
+	else {
+		if (pv4[2]==0.0f) pv4[2]= 0.00001f; /* don't allow div by 0 */
+
+		if (uci->do_persp==0) {
+			target[0]=(pv4[0]/uci->camsize) * uci->xasp;
+			target[1]=(pv4[1]/uci->camsize) * uci->yasp;
+		}
+		else {
+			target[0]=(-pv4[0]*((1.0f/uci->camsize)/pv4[2])*uci->xasp) / 2.0f;
+			target[1]=(-pv4[1]*((1.0f/uci->camsize)/pv4[2])*uci->yasp) / 2.0f;
+		}
+	}
+
+	/* adds camera shift + 0.5 */
+	target[0] += uci->shiftx;
+	target[1] += uci->shifty;
+}
+
+/* could rv3d->persmat */
+void project_from_view(float target[2], float source[3], float persmat[4][4], float rotmat[4][4], float winx, float winy)
+{
+	float pv[3], pv4[4], x= 0.0, y= 0.0;
+
+	mul_m4_v3(rotmat, pv);
+
+	copy_v3_v3(pv4, source);
+	pv4[3]= 1.0;
+
+	/* rotmat is the object matrix in this case */
+	mul_m4_v4(rotmat, pv4); 
+
+	/* almost project_short */
+	mul_m4_v4(persmat, pv4);
+	if(fabs(pv4[3]) > 0.00001) { /* avoid division by zero */
+		target[0] = winx/2.0 + (winx/2.0) * pv4[0] / pv4[3];
+		target[1] = winy/2.0 + (winy/2.0) * pv4[1] / pv4[3];
+	}
+	else {
+		/* scaling is lost but give a valid result */
+		target[0] = winx/2.0 + (winx/2.0) * pv4[0];
+		target[1] = winy/2.0 + (winy/2.0) * pv4[1];
+	}
+
+	/* v3d->persmat seems to do this funky scaling */ 
+	if(winx > winy) {
+		y= (winx - winy)/2.0;
+		winy = winx;
+	}
+	else {
+		x= (winy - winx)/2.0;
+		winx = winy;
+	}
+
+	target[0]= (x + target[0]) / winx;
+	target[1]= (y + target[1]) / winy;
+}
+
+/* 'rotmat' can be obedit->obmat when uv project is used.
+ * 'winx' and 'winy' can be from scene->r.xsch/ysch */ 
+UvCameraInfo *project_camera_info(Object *ob, float rotmat[4][4], float winx, float winy)
+{
+	UvCameraInfo uci;
+	Camera *camera= ob->data;
+
+	uci.do_pano = (camera->flag & CAM_PANORAMA);
+	uci.do_persp = (camera->type==CAM_PERSP);
+
+	uci.camangle= DEG2RAD(camera->angle)/2.0f;
+	uci.camsize=  uci.do_persp ?  uci.camsize= tanf(uci.camangle) : camera->ortho_scale;
+
+	if (invert_m4_m4(uci.caminv, ob->obmat)) {
+		UvCameraInfo *uci_pt;
+
+		/* normal projection */
+		copy_m4_m4(uci.rotmat, rotmat);
+
+		/* also make aspect ratio adjustment factors */
+		if (winx > winy) {
+			uci.xasp= 1.0f;
+			uci.yasp= winx / winy;
+		}
+		else {
+			uci.xasp= winy / winx;
+			uci.yasp= 1.0f;
+		}
+		
+		/* include 0.5f here to move the UVs into the center */
+		uci.shiftx = 0.5f - camera->shiftx;
+		uci.shifty = 0.5f - camera->shifty;
+		
+		uci_pt= MEM_mallocN(sizeof(UvCameraInfo), "UvCameraInfo");
+		*uci_pt= uci;
+		return uci_pt;
+	}
+
+	return NULL;
+}
+
+void project_from_view_ortho(float target[2], float source[3], float rotmat[4][4])
+{
+	float pv[3];
+
+	mul_m4_v3(rotmat, pv);
+
+	/* ortho projection */
+	target[0] = -pv[0];
+	target[1] = pv[2];
+}

Modified: trunk/blender/source/blender/editors/uvedit/uvedit_unwrap_ops.c
===================================================================
--- trunk/blender/source/blender/editors/uvedit/uvedit_unwrap_ops.c	2010-03-23 16:40:46 UTC (rev 27694)
+++ trunk/blender/source/blender/editors/uvedit/uvedit_unwrap_ops.c	2010-03-23 18:28:38 UTC (rev 27695)
@@ -49,6 +49,7 @@
 #include "BLI_math.h"
 #include "BLI_edgehash.h"
 #include "BLI_editVert.h"
+#include "BLI_uvproject.h"
 
 #include "PIL_time.h"
 
@@ -865,116 +866,8 @@
 }
 
 /**************** Project From View operator **************/
-
-static void uv_from_view_bounds(float target[2], float source[3], float rotmat[4][4])
+static int uv_from_view_exec(bContext *C, wmOperator *op)
 {
-	float pv[3];
-
-	mul_m4_v3(rotmat, pv);
-
-	/* ortho projection */
-	target[0] = -pv[0];
-	target[1] = pv[2];
-}
-
-typedef struct UvCameraInfo {
-	float camangle;
-	float camsize;
-	float xasp, yasp;
-	float shiftx, shifty;
-	float rotmat[4][4];
-	float caminv[4][4];
-	short do_persp, do_pano;
-} UvCameraInfo;
-
-//static void uv_from_camera(float camsize, float camangle, float xasp, float yasp, float target[2], float source[3], float rotmat[4][4], float caminv[4][4], int persp, int pano)
-static void uv_from_camera(UvCameraInfo *uci, float target[2], float source[3])
-{
-	float pv4[4];
-
-	copy_v3_v3(pv4, source);
-	pv4[3]= 1.0;
-
-	/* rotmat is the object matrix in this case */
-	mul_m4_v4(uci->rotmat, pv4);
-
-	/* caminv is the inverse camera matrix */
-	mul_m4_v4(uci->caminv, pv4);
-
-	if(uci->do_pano) {
-		float angle= atan2f(pv4[0], -pv4[2]) / (M_PI * 2.0); /* angle around the camera */
-		if (uci->do_persp==0) {
-			target[0] = angle; /* no correct method here, just map to  0-1 */
-			target[1] = pv4[1] / uci->camsize;
-		}
-		else {
-			float vec2d[2]= {pv4[0], pv4[2]}; /* 2D position from the camera */
-			target[0] = angle * (M_PI / uci->camangle);
-			target[1] = pv4[1] / (len_v2(vec2d) * uci->camsize);
-		}
-	}
-	else {
-		if (pv4[2]==0.0f) pv4[2]=0.00001f; /* don't allow div by 0 */
-
-		if (uci->do_persp==0) {
-			target[0]=(pv4[0]/uci->camsize) * uci->xasp;
-			target[1]=(pv4[1]/uci->camsize) * uci->yasp;
-		}
-		else {
-			target[0]=(-pv4[0]*((1.0f/uci->camsize)/pv4[2])*uci->xasp) / 2.0f;
-			target[1]=(-pv4[1]*((1.0f/uci->camsize)/pv4[2])*uci->yasp) / 2.0f;
-		}
-	}
-
-	/* adds camera shift + 0.5 */
-	target[0] += uci->shiftx;
-	target[1] += uci->shifty;
-}
-
-static void uv_from_view(ARegion *ar, float target[2], float source[3], float rotmat[4][4])
-{
-	RegionView3D *rv3d= ar->regiondata;
-	float pv[3], pv4[4], dx, dy, x= 0.0, y= 0.0;
-
-	mul_m4_v3(rotmat, pv);
-
-	dx= ar->winx;
-	dy= ar->winy;
-
-	copy_v3_v3(pv4, source);
-	pv4[3]= 1.0;
-
-	/* rotmat is the object matrix in this case */
-	mul_m4_v4(rotmat, pv4); 
-
-	/* almost project_short */
-	mul_m4_v4(rv3d->persmat, pv4);
-	if(fabs(pv4[3]) > 0.00001) { /* avoid division by zero */
-		target[0] = dx/2.0 + (dx/2.0)*pv4[0]/pv4[3];
-		target[1] = dy/2.0 + (dy/2.0)*pv4[1]/pv4[3];
-	}
-	else {
-		/* scaling is lost but give a valid result */
-		target[0] = dx/2.0 + (dx/2.0)*pv4[0];
-		target[1] = dy/2.0 + (dy/2.0)*pv4[1];
-	}
-
-	/* v3d->persmat seems to do this funky scaling */ 
-	if(dx > dy) {
-		y= (dx-dy)/2.0;
-		dy = dx;
-	}
-	else {
-		x= (dy-dx)/2.0;
-		dx = dy;
-	}
-
-	target[0]= (x + target[0])/dx;
-	target[1]= (y + target[1])/dy;
-}
-
-static int from_view_exec(bContext *C, wmOperator *op)
-{
 	Scene *scene= CTX_data_scene(C);
 	Object *obedit= CTX_data_edit_object(C);
 	Camera *camera= NULL;
@@ -1004,53 +897,31 @@
 			if(efa->f & SELECT) {
 				tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
 
-				uv_from_view_bounds(tf->uv[0], efa->v1->co, rotmat);

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list