[Bf-blender-cvs] [1decd82] master: Code Cleanup: use math functions and reduce View3d axis drawing into a loop

Campbell Barton noreply at git.blender.org
Sun Nov 24 11:28:39 CET 2013


Commit: 1decd824f3edcb612acd7251ebaa3b2884398b19
Author: Campbell Barton
Date:   Sun Nov 24 21:25:05 2013 +1100
http://developer.blender.org/rB1decd824f3edcb612acd7251ebaa3b2884398b19

Code Cleanup: use math functions and reduce View3d axis drawing into a loop

===================================================================

M	source/blender/bmesh/operators/bmo_primitive.c
M	source/blender/editors/curve/editcurve.c
M	source/blender/editors/space_view3d/drawobject.c
M	source/blender/editors/space_view3d/view3d_draw.c
M	source/blender/editors/space_view3d/view3d_edit.c

===================================================================

diff --git a/source/blender/bmesh/operators/bmo_primitive.c b/source/blender/bmesh/operators/bmo_primitive.c
index 6a54092..0194840 100644
--- a/source/blender/bmesh/operators/bmo_primitive.c
+++ b/source/blender/bmesh/operators/bmo_primitive.c
@@ -301,8 +301,9 @@ void bmo_create_uvsphere_exec(BMesh *bm, BMOperator *op)
 	BMVert *eve, *preveve;
 	BMEdge *e;
 	BMIter iter;
-	float vec[3], mat[4][4], cmat[3][3], phi, q[4];
-	float phid;
+	const float axis[3] = {0, 0, 1};
+	float vec[3], mat[4][4], cmat[3][3];
+	float phi, phid;
 	int a;
 
 	BMO_slot_mat4_get(op->slots_in, "matrix", mat);
@@ -331,11 +332,7 @@ void bmo_create_uvsphere_exec(BMesh *bm, BMOperator *op)
 	}
 
 	/* extrude and rotate; negative phi to make normals face outward */
-	phi = -M_PI / seg;
-	q[0] = cosf(phi);
-	q[3] = sinf(phi);
-	q[1] = q[2] = 0.0f;
-	quat_to_mat3(cmat, q);
+	axis_angle_to_mat3(cmat, axis, -(M_PI * 2) / seg);
 
 	for (a = 0; a < seg; a++) {
 		if (a) {
diff --git a/source/blender/editors/curve/editcurve.c b/source/blender/editors/curve/editcurve.c
index f576440..d4be39a 100644
--- a/source/blender/editors/curve/editcurve.c
+++ b/source/blender/editors/curve/editcurve.c
@@ -4364,7 +4364,7 @@ bool ed_editnurb_spin(float viewmat[4][4], Object *obedit, const float axis[3],
 	Curve *cu = (Curve *)obedit->data;
 	ListBase *editnurb = object_editcurve_get(obedit);
 	Nurb *nu;
-	float si, phi, n[3], q[4], cmat[3][3], tmat[3][3], imat[3][3];
+	float cmat[3][3], tmat[3][3], imat[3][3];
 	float bmat[3][3], rotmat[3][3], scalemat1[3][3], scalemat2[3][3];
 	float persmat[3][3], persinv[3][3];
 	bool ok, changed = false;
@@ -4377,15 +4377,7 @@ bool ed_editnurb_spin(float viewmat[4][4], Object *obedit, const float axis[3],
 	copy_m3_m4(bmat, obedit->obmat);
 	invert_m3_m3(imat, bmat);
 	
-	normalize_v3_v3(n, axis);
-	/* TODO - use math func */
-	phi = M_PI / 8.0;
-	q[0] = cos(phi);
-	si = sin(phi);
-	q[1] = n[0] * si;
-	q[2] = n[1] * si;
-	q[3] = n[2] * si;
-	quat_to_mat3(cmat, q);
+	axis_angle_to_mat3(cmat, axis, M_PI / 4.0);
 	mul_m3_m3m3(tmat, cmat, bmat);
 	mul_m3_m3m3(rotmat, imat, tmat);
 
diff --git a/source/blender/editors/space_view3d/drawobject.c b/source/blender/editors/space_view3d/drawobject.c
index ee9ed96..29ee72c 100644
--- a/source/blender/editors/space_view3d/drawobject.c
+++ b/source/blender/editors/space_view3d/drawobject.c
@@ -1210,18 +1210,16 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
 		/* skip drawing extra info */
 	}
 	else if ((la->type == LA_SPOT) || (la->type == LA_YF_PHOTON)) {
-		lvec[0] = lvec[1] = 0.0;
-		lvec[2] = 1.0;
-		x = rv3d->persmat[0][2];
-		y = rv3d->persmat[1][2];
-		z = rv3d->persmat[2][2];
-		vvec[0] = x * ob->obmat[0][0] + y * ob->obmat[0][1] + z * ob->obmat[0][2];
-		vvec[1] = x * ob->obmat[1][0] + y * ob->obmat[1][1] + z * ob->obmat[1][2];
-		vvec[2] = x * ob->obmat[2][0] + y * ob->obmat[2][1] + z * ob->obmat[2][2];
 
+		copy_v3_fl3(lvec, 0.0f, 0.0f, 1.0f);
+		copy_v3_fl3(vvec, rv3d->persmat[0][2], rv3d->persmat[1][2], rv3d->persmat[2][2]);
+		mul_mat3_m4_v3(ob->obmat, vvec);
+
+		x = -la->dist;
 		y = cosf(la->spotsize * (float)(M_PI / 360.0));
+		z = x * sqrtf(1.0f - y * y);
+
 		spotvolume(lvec, vvec, y);
-		x = -la->dist;
 		mul_v3_fl(lvec, x);
 		mul_v3_fl(vvec, x);
 
@@ -1232,7 +1230,6 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
 		glVertex3fv(lvec);
 		glEnd();
 		
-		z = x * sqrtf(1.0f - y * y);
 		x *= y;
 
 		/* draw the circle/square at the end of the cone */
diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c
index dc00e55..3d26037 100644
--- a/source/blender/editors/space_view3d/view3d_draw.c
+++ b/source/blender/editors/space_view3d/view3d_draw.c
@@ -583,7 +583,9 @@ static void draw_view_axis(RegionView3D *rv3d, rcti *rect)
 	float ydisp = 0.0;          /* vertical displacement to allow obj info text */
 	int bright = - 20 * (10 - U.rvibright); /* axis alpha offset (rvibright has range 0-10) */
 	float vec[3];
+	char axis_text[2] = "x";
 	float dx, dy;
+	int i;
 	
 	startx += rect->xmin;
 	starty += rect->ymin;
@@ -594,60 +596,27 @@ static void draw_view_axis(RegionView3D *rv3d, rcti *rect)
 	glEnable(GL_BLEND);
 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-	/* X */
-	vec[0] = 1;
-	vec[1] = vec[2] = 0;
-	mul_qt_v3(rv3d->viewquat, vec);
-	dx = vec[0] * k;
-	dy = vec[1] * k;
-	
-	UI_ThemeColorShadeAlpha(TH_AXIS_X, 0, bright);
-	glBegin(GL_LINES);
-	glVertex2f(startx, starty + ydisp);
-	glVertex2f(startx + dx, starty + dy + ydisp);
-	glEnd();
-
-	if (fabsf(dx) > toll || fabsf(dy) > toll) {
-		BLF_draw_default_ascii(startx + dx + 2, starty + dy + ydisp + 2, 0.0f, "x", 1);
-	}
-	
-	/* BLF_draw_default disables blending */
-	glEnable(GL_BLEND);
+	for (i = 0; i < 3; i++) {
+		zero_v3(vec);
+		vec[i] = 1.0f;
+		mul_qt_v3(rv3d->viewquat, vec);
+		dx = vec[0] * k;
+		dy = vec[1] * k;
 
-	/* Y */
-	vec[1] = 1;
-	vec[0] = vec[2] = 0;
-	mul_qt_v3(rv3d->viewquat, vec);
-	dx = vec[0] * k;
-	dy = vec[1] * k;
-	
-	UI_ThemeColorShadeAlpha(TH_AXIS_Y, 0, bright);
-	glBegin(GL_LINES);
-	glVertex2f(startx, starty + ydisp);
-	glVertex2f(startx + dx, starty + dy + ydisp);
-	glEnd();
-
-	if (fabsf(dx) > toll || fabsf(dy) > toll) {
-		BLF_draw_default_ascii(startx + dx + 2, starty + dy + ydisp + 2, 0.0f, "y", 1);
-	}
+		UI_ThemeColorShadeAlpha(TH_AXIS_X + i, 0, bright);
+		glBegin(GL_LINES);
+		glVertex2f(startx, starty + ydisp);
+		glVertex2f(startx + dx, starty + dy + ydisp);
+		glEnd();
 
-	glEnable(GL_BLEND);
-	
-	/* Z */
-	vec[2] = 1;
-	vec[1] = vec[0] = 0;
-	mul_qt_v3(rv3d->viewquat, vec);
-	dx = vec[0] * k;
-	dy = vec[1] * k;
+		if (fabsf(dx) > toll || fabsf(dy) > toll) {
+			BLF_draw_default_ascii(startx + dx + 2, starty + dy + ydisp + 2, 0.0f, axis_text, 1);
+		}
 
-	UI_ThemeColorShadeAlpha(TH_AXIS_Z, 0, bright);
-	glBegin(GL_LINES);
-	glVertex2f(startx, starty + ydisp);
-	glVertex2f(startx + dx, starty + dy + ydisp);
-	glEnd();
+		axis_text[0]++;
 
-	if (fabsf(dx) > toll || fabsf(dy) > toll) {
-		BLF_draw_default_ascii(startx + dx + 2, starty + dy + ydisp + 2, 0.0f, "z", 1);
+		/* BLF_draw_default disables blending */
+		glEnable(GL_BLEND);
 	}
 
 	/* restore line-width */
diff --git a/source/blender/editors/space_view3d/view3d_edit.c b/source/blender/editors/space_view3d/view3d_edit.c
index 80e5d19..b49abaf 100644
--- a/source/blender/editors/space_view3d/view3d_edit.c
+++ b/source/blender/editors/space_view3d/view3d_edit.c
@@ -4144,16 +4144,17 @@ void ED_view3d_cursor3d_position(bContext *C, float fp[3], const int mval[2])
 		}
 	}
 	else {
-		const float dx = ((float)(mval[0] - (ar->winx / 2))) * zfac / (ar->winx / 2);
-		const float dy = ((float)(mval[1] - (ar->winy / 2))) * zfac / (ar->winy / 2);
-		const float fz = (rv3d->persmat[0][3] * fp[0] +
-		                  rv3d->persmat[1][3] * fp[1] +
-		                  rv3d->persmat[2][3] * fp[2] +
-		                  rv3d->persmat[3][3]) / zfac;
-
-		fp[0] = (rv3d->persinv[0][0] * dx + rv3d->persinv[1][0] * dy + rv3d->persinv[2][0] * fz) - rv3d->ofs[0];
-		fp[1] = (rv3d->persinv[0][1] * dx + rv3d->persinv[1][1] * dy + rv3d->persinv[2][1] * fz) - rv3d->ofs[1];
-		fp[2] = (rv3d->persinv[0][2] * dx + rv3d->persinv[1][2] * dy + rv3d->persinv[2][2] * fz) - rv3d->ofs[2];
+		float tvec[3];
+
+		tvec[0] = ((float)(mval[0] - (ar->winx / 2))) * zfac / (ar->winx / 2);
+		tvec[1] = ((float)(mval[1] - (ar->winy / 2))) * zfac / (ar->winy / 2);
+		tvec[2] = (rv3d->persmat[0][3] * fp[0] +
+		           rv3d->persmat[1][3] * fp[1] +
+		           rv3d->persmat[2][3] * fp[2] +
+		           rv3d->persmat[3][3]) / zfac;
+
+		mul_mat3_m4_v3(rv3d->persinv, tvec);
+		sub_v3_v3v3(fp, tvec, rv3d->ofs);
 	}
 
 }




More information about the Bf-blender-cvs mailing list