[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [49664] branches/soc-2012-swiss_cheese/ source/blender/gpu: New gpuMatrix functions:

Alexander Kuznetsov kuzsasha at gmail.com
Tue Aug 7 18:37:54 CEST 2012


Revision: 49664
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=49664
Author:   alexk
Date:     2012-08-07 16:37:54 +0000 (Tue, 07 Aug 2012)
Log Message:
-----------
New gpuMatrix functions:
gpuGetSpecificMatrix returns/writes matrix of specified type.
gpuRotateVector rotates current matrix by 3d vector.
gpuRotateRight rotates current matrix by right angle

gpuProject is simular to gluProject, but uses vectors instead. Probably we should move it to BLI_math or use current matrix instead?
gpuUnProject is simular to gluUnProject, but uses vectors instead. There is a bug, so we relay on gluUnProject for now. Should be fix soon.

Modified Paths:
--------------
    branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_matrix.h
    branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_matrix.c

Modified: branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_matrix.h
===================================================================
--- branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_matrix.h	2012-08-07 16:30:34 UTC (rev 49663)
+++ branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_matrix.h	2012-08-07 16:37:54 UTC (rev 49664)
@@ -25,6 +25,7 @@
 
 void gpuLoadMatrix(const GLfloat * m);
 GLfloat * gpuGetMatrix(GLfloat * m);
+GLfloat * gpuGetSpecificMatrix(GLenum type, GLfloat * m);
 
 void gpuLoadIdentity(void);
 
@@ -33,7 +34,9 @@
 
 void gpuTranslate(GLfloat x, GLfloat y, GLfloat z);
 void gpuScale(GLfloat x, GLfloat y, GLfloat z);
+void gpuRotateVector(GLfloat angle, GLfloat * vector);
 void gpuRotateAxis(GLfloat angle, char axis);
+void gpuRotateRight(char type);
 
 void gpuOrtho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearVal, GLfloat farVal);
 void gpuFrustum(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearVal, GLfloat farVal);
@@ -43,6 +46,8 @@
 
 void gpuLookAt(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat upX, GLfloat upY, GLfloat upZ);
 
+void gpuProject(const GLfloat obj[3], const GLfloat model[4][4], const GLfloat proj[4][4], const GLint view[4], GLfloat win[3]);
+int gpuUnProject(const GLfloat win[3], const GLfloat model[4][4], const GLfloat proj[4][4], const GLint view[4], GLfloat obj[3]);
 #ifndef GPU_MAT_CAST_ANY
 #define GPU_MAT_CAST_ANY
 

Modified: branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_matrix.c
===================================================================
--- branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_matrix.c	2012-08-07 16:30:34 UTC (rev 49663)
+++ branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_matrix.c	2012-08-07 16:37:54 UTC (rev 49664)
@@ -75,7 +75,6 @@
 #define CHECKMAT
 #endif
 
-
 static void ms_init(GPU_matrix_stack * ms, GLint initsize)
 {
 	if(initsize == 0)
@@ -108,10 +107,8 @@
 	ms_current = &ms_modelview;
 	ms_current_mode = GL_MODELVIEW;
 
-	printf("Stack init\n");
 
 
-
 }
 
 void GPU_ms_exit(void)
@@ -119,11 +116,6 @@
 	ms_free(&ms_modelview);
 	ms_free(&ms_projection);
 	ms_free(&ms_texture);
-
-	printf("Stack exit\n");
-
-
-
 }
 
 void gpuMatrixLock(void)
@@ -259,7 +251,7 @@
 
 
 #endif
-CHECKMAT
+//CHECKMAT
 }
 
 
@@ -342,14 +334,43 @@
 GLfloat * gpuGetMatrix(GLfloat * m)
 {
 	if(m)
-		copy_m4_m4((GLfloat (*)[4])m,CURMATRIX);
+		copy_m4_m4((GLfloat (*)[4])m, CURMATRIX);
 	else
 		return (GLfloat*)(CURMATRIX);
 	ms_current->changed = 1;
 	return 0;
 }
 
+GLfloat * gpuGetSpecificMatrix(GLenum type, GLfloat *m)
+{
+	GPU_matrix_stack * ms_select;
 
+	switch(type)
+	{
+		case GL_MODELVIEW:
+			ms_select = &ms_modelview;
+			break;
+		case GL_PROJECTION:
+			ms_select = &ms_projection;
+			break;
+		case GL_TEXTURE:
+			ms_select = & ms_texture;
+			break;
+		default:
+			BLI_assert(0);
+			return 0;
+	}
+
+	if(m)
+		copy_m4_m4((GLfloat (*)[4])m, ms_select->dynstack[ms_select->pos]);
+	else
+		return (GLfloat*)(ms_select->dynstack[ms_select->pos]);
+
+	return 0;
+
+
+}
+
 void gpuLoadIdentity(void)
 {
 	unit_m4(CURMATRIX);
@@ -402,13 +423,34 @@
 }
 
 
+void gpuRotateVector(GLfloat angle, GLfloat * vector)
+{
+	float rm[3][3];
+	GPU_matrix cm;
+	copy_m4_m4((GLfloat (*)[4])cm, (GLfloat (*)[4])CURMATRIX);
+
+	axis_angle_to_mat3(rm, vector, angle);
+	mult_m4_m3m4_q(CURMATRIX, cm, rm);
+
+	ms_current->changed = 1;
+
+}
+
 void gpuRotateAxis(GLfloat angle, char axis)
 {
 
-	rotate_m4((GLfloat (*)[4])CURMATRIX, axis, angle*M_PI/180.0f);
+	rotate_m4((GLfloat (*)[4])CURMATRIX, axis, angle);
 	ms_current->changed = 1;
 }
 
+void gpuRotateRight(char type)
+{
+	rotate_m4_right((GLfloat (*)[4])CURMATRIX, type);
+
+	ms_current->changed = 1;
+
+}
+
 void gpuLoadOrtho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearVal, GLfloat farVal)
 {
 	mat4_ortho_set(CURMATRIX, left, right, bottom, top, nearVal, farVal);
@@ -463,3 +505,60 @@
 	CHECKMAT
 
 }
+
+void gpuProject(const GLfloat obj[3], const GLfloat model[4][4], const GLfloat proj[4][4], const GLint view[4], GLfloat win[3])
+{
+	float v[4];
+
+	mul_v4_m4v3(v, model, obj);
+	mul_m4_v4(proj, v);
+
+	win[0]=view[0]+(view[2]*(v[0]+1))*0.5f;
+	win[1]=view[1]+(view[3]*(v[1]+1))*0.5f;
+	win[2]=(v[2]+1)*0.5f;
+}
+
+
+int gpuUnProject(const GLfloat win[3], const GLfloat model[4][4], const GLfloat proj[4][4], const GLint view[4], GLfloat obj[3])
+{
+
+	float v[3];
+	GPU_matrix pm;
+
+	int i;
+	double modeld[16], projd[16];
+	double objd[3] = {0};
+	for(i=0; i<16; i++)
+	{
+		modeld[i] = model[0][i];
+		projd[i] = proj[0][i];
+	}
+	if(!gluUnProject(win[0], win[1], win[2], modeld, projd, view, objd, objd+1, objd+2))
+		BLI_assert(0);
+
+	mult_m4_m4m4_q(pm, proj, model);
+
+	if(!invert_m4(pm))
+		return 0;
+
+
+
+
+	v[0] = 2.0*(win[0]-view[0])/view[2] - 1.0f;
+	v[1] = 2.0*(win[1]-view[1])/view[3] - 1.0f;
+	v[2] = 2.0*(win[2]		   )		 - 1.0f;
+
+
+	mul_v3_m4v3_q(obj, pm, v);
+	/*obj[0]/=10;
+obj[1]/=10;
+obj[2]/=10;
+obj[0]=0;
+obj[1]=0;
+obj[2]=0;*/
+	obj[0] = objd[0];
+	obj[1] = objd[1];
+	obj[2] = objd[2];
+	return 1;
+
+}




More information about the Bf-blender-cvs mailing list