Patch for head tracking

Michael Dürig <[email protected]> Mon, 21 Aug 2006 11:27:57 +0200
Newsgroups gmane.comp.graphics.chromium.devel
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------060408090205090506000709
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit


I am currently working on a SPU which facilitates integration of 
tracking devices with Chromium. For usage for example in a CAVE environment.

In order to do so I patched the Chromium code base in a few places. The 
patch is attached. Since I am not sure which parts of the patch are 
desired and will finally make its way into Chromium I do not release my 
tracker SPU yet. I will wait until I see which parts get through and 
will then try to adapt my SPU accordingly.

Regarding the patch, there is only one part which is *really* required 
in order to get head tracking working at all: In tilesortspu_misc.c 
function tilesortspu_ChromiumParametervCR() the case 
GL_SERVER_PROJECTION_MATRIX_CR needs to be handled similar to the case 
GL_SERVER_VIEW_MATRIX_CR otherwise the servers will never see the new 
projection matrix.
The other parts add functionality which might be implemented elsewhere 
but seemed to fit into Chromium for one reason or another.

Michael

--------------060408090205090506000709
Content-Type: text/plain;
 name="cr.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="cr.patch"

Index: crserverlib/server_misc.c
===================================================================
RCS file: /cvsroot/chromium/cr/crserverlib/server_misc.c,v
retrieving revision 1.9
diff -u -r1.9 server_misc.c
--- crserverlib/server_misc.c	13 Dec 2005 18:37:46 -0000	1.9
+++ crserverlib/server_misc.c	18 Jul 2006 23:04:29 -0000
@@ -119,10 +119,18 @@
 			const GLfloat *v = (const GLfloat *) values;
 			const int eye = v[1] == 0.0 ? 0 : 1;
 			crMatrixInitFromFloats(&cr_server.viewMatrix[eye], v + 2);
-			/*
-			printf("Got SERVER_VIEW Matrix %d\n", eye);
-			crMatrixPrint("view", &cr_server.viewMatrix[eye]);
-			*/
+			
+			crDebug(
+        "Got SERVER_VIEW Matrix \n" 
+ 	      "  %f %f %f %f\n"
+	      "  %f %f %f %f\n"
+      	"  %f %f %f %f\n"
+	      "  %f %f %f %f\n",
+        cr_server.viewMatrix[eye].m00, cr_server.viewMatrix[eye].m10, cr_server.viewMatrix[eye].m20, cr_server.viewMatrix[eye].m30,
+        cr_server.viewMatrix[eye].m01, cr_server.viewMatrix[eye].m11, cr_server.viewMatrix[eye].m21, cr_server.viewMatrix[eye].m31,
+        cr_server.viewMatrix[eye].m02, cr_server.viewMatrix[eye].m12, cr_server.viewMatrix[eye].m22, cr_server.viewMatrix[eye].m32,
+        cr_server.viewMatrix[eye].m03, cr_server.viewMatrix[eye].m13, cr_server.viewMatrix[eye].m23, cr_server.viewMatrix[eye].m33);
+			
 		}
 		cr_server.viewOverride = GL_TRUE;
 		break;
@@ -139,10 +147,37 @@
 			const GLfloat *v = (const GLfloat *) values;
 			const int eye = v[1] == 0.0 ? 0 : 1;
 			crMatrixInitFromFloats(&cr_server.projectionMatrix[eye], v + 2);
-			/*
-			printf("Got SERVER_PROJ Matrix %d\n", eye);
-			crMatrixPrint("proj", &cr_server.projectionMatrix[eye]);
-			*/
+      
+			crDebug(
+        "Got SERVER_PROJ Matrix \n" 
+ 	      "  %f %f %f %f\n"
+	      "  %f %f %f %f\n"
+      	"  %f %f %f %f\n"
+	      "  %f %f %f %f\n",
+        cr_server.projectionMatrix[eye].m00, cr_server.projectionMatrix[eye].m10, cr_server.projectionMatrix[eye].m20, cr_server.projectionMatrix[eye].m30,
+        cr_server.projectionMatrix[eye].m01, cr_server.projectionMatrix[eye].m11, cr_server.projectionMatrix[eye].m21, cr_server.projectionMatrix[eye].m31,
+        cr_server.projectionMatrix[eye].m02, cr_server.projectionMatrix[eye].m12, cr_server.projectionMatrix[eye].m22, cr_server.projectionMatrix[eye].m32,
+        cr_server.projectionMatrix[eye].m03, cr_server.projectionMatrix[eye].m13, cr_server.projectionMatrix[eye].m23, cr_server.projectionMatrix[eye].m33);
+
+			if (cr_server.projectionMatrix[eye].m33 == 0.0f) {
+				float x = cr_server.projectionMatrix[eye].m00;
+				float y = cr_server.projectionMatrix[eye].m11;
+				float a = cr_server.projectionMatrix[eye].m20;
+				float b = cr_server.projectionMatrix[eye].m21;
+				float c = cr_server.projectionMatrix[eye].m22;
+				float d = cr_server.projectionMatrix[eye].m32;
+				float znear = -d / (1.0f - c);
+				float zfar = (c - 1.0f) * znear / (c + 1.0f);
+				float left = znear * (a - 1.0f) / x;
+				float right = 2.0f * znear / x + left;
+				float bottom = znear * (b - 1.0f) / y;
+			  float top = 2.0f * znear / y + bottom;
+			  crDebug("Frustum: left, right, bottom, top, near, far: %f, %f, %f, %f, %f, %f", left, right, bottom, top, znear, zfar);	
+			}
+			else {
+				// Todo: Add debug output for orthographic projection
+			}
+
 		}
 		cr_server.projectionOverride = GL_TRUE;
 		break;
Index: include/cr_matrix.h
===================================================================
RCS file: /cvsroot/chromium/cr/include/cr_matrix.h,v
retrieving revision 1.3
diff -u -r1.3 cr_matrix.h
--- include/cr_matrix.h	18 Jan 2005 21:50:12 -0000	1.3
+++ include/cr_matrix.h	18 Jul 2006 23:04:33 -0000
@@ -2,6 +2,8 @@
 #ifndef CR_MATRIX_H
 #define CR_MATRIX_H
 
+#include "chromium.h"
+
 /*
  * Note: m[col][row] matches OpenGL's column-major memory layout
  */
@@ -12,6 +14,9 @@
 	float m30, m31, m32, m33;
 } CRmatrix;
 
+typedef struct { GLfloat x,y,z,w; } GLvectorf;
+typedef struct { GLdouble x,y,z,w; } GLvectord;
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -49,17 +54,18 @@
 extern void
 crMatrixMultiply(CRmatrix *p, const CRmatrix *a, const CRmatrix *b);
 
-#if 0
 extern void
 crMatrixTransformPointf(const CRmatrix *m, GLvectorf *p);
 
 extern void
 crMatrixTransformPointd(const CRmatrix *m, GLvectord *p);
-#endif
 
 extern void
 crMatrixInvertTranspose(CRmatrix *inv, const CRmatrix *mat);
 
+extern void 
+crMatrixTranspose(CRmatrix *t, const CRmatrix *m);
+
 extern void
 crMatrixTranslate(CRmatrix *m, float x, float y, float z);
 
Index: include/state/cr_statetypes.h
===================================================================
RCS file: /cvsroot/chromium/cr/include/state/cr_statetypes.h,v
retrieving revision 1.14
diff -u -r1.14 cr_statetypes.h
--- include/state/cr_statetypes.h	16 Feb 2004 20:40:16 -0000	1.14
+++ include/state/cr_statetypes.h	18 Jul 2006 23:04:31 -0000
@@ -51,8 +51,8 @@
 VECTOR(GLshort,GLvectors);
 VECTOR(GLint,GLvectori);
 VECTOR(GLuint,GLvectorui);
-VECTOR(GLfloat,GLvectorf);
-VECTOR(GLdouble,GLvectord);
+// VECTOR(GLfloat,GLvectorf);    These two are defined in cr_matrix.h
+// VECTOR(GLdouble,GLvectord);
 COLOR(GLdefault,GLcolor);
 COLOR(GLenum,GLcolore);
 COLOR(GLubyte,GLcolorub);
Index: mothership/server/crmatrix.py
===================================================================
RCS file: /cvsroot/chromium/cr/mothership/server/crmatrix.py,v
retrieving revision 1.2
diff -u -r1.2 crmatrix.py
--- mothership/server/crmatrix.py	16 Feb 2004 20:40:17 -0000	1.2
+++ mothership/server/crmatrix.py	18 Jul 2006 23:03:21 -0000
@@ -9,7 +9,10 @@
 
 import math
 
-
+def det3x3(a1, a2, a3, b1, b2, b3, c1, c2, c3):
+  return (a1 * (b2 * c3 - b3 * c2) + 
+       	  b1 * (c2 * a3 - a2 * c3) + 
+       	  c1 * (a2 * b3 - a3 * b2))
 
 class CRMatrix:
 	"""4x4 matrix transformation class.
@@ -287,6 +290,118 @@
 			isPerspective = 0
 		return (left, right, bottom, top, near, far, isPerspective)
 
+	def Invert(self):
+		"""Invert this matrix"""
+		m00 = self.Get(0, 0)
+		m01 = self.Get(1, 0)
+		m02 = self.Get(2, 0)
+		m03 = self.Get(3, 0)
+
+		m10 = self.Get(0, 1)
+		m11 = self.Get(1, 1)
+		m12 = self.Get(2, 1)
+		m13 = self.Get(3, 1)
+
+		m20 = self.Get(0, 2)
+		m21 = self.Get(1, 2)
+		m22 = self.Get(2, 2)
+		m23 = self.Get(3, 2)
+		
+		m30 = self.Get(0, 3)
+		m31 = self.Get(1, 3)
+		m32 = self.Get(2, 3)
+		m33 = self.Get(3, 3)
+    
+		cof00 =  det3x3( m11, m12, m13,
+                     m21, m22, m23,
+                     m31, m32, m33 )
+
+		cof01 = -det3x3( m12, m13, m10,
+                     m22, m23, m20,
+                     m32, m33, m30 )
+
+		cof02 =  det3x3( m13, m10, m11,
+                     m23, m20, m21,
+                     m33, m30, m31 )
+
+		cof03 = -det3x3( m10, m11, m12,
+                     m20, m21, m22,
+                     m30, m31, m32 )
+
+
+		inv_det = 1.0 / ( m00 * cof00 + m01 * cof01 + m02 * cof02 + m03 * cof03 )
+
+
+		cof10 = -det3x3( m21, m22, m23,
+                     m31, m32, m33,
+                     m01, m02, m03 )
+
+		cof11 =  det3x3( m22, m23, m20,
+                     m32, m33, m30,
+                     m02, m03, m00 )
+
+		cof12 = -det3x3( m23, m20, m21,
+                     m33, m30, m31,
+                     m03, m00, m01 )
+
+		cof13 =  det3x3( m20, m21, m22,
+                     m30, m31, m32,
+                     m00, m01, m02 )
+
+
+		cof20 =  det3x3( m31, m32, m33,
+                     m01, m02, m03,
+                     m11, m12, m13 )
+
+		cof21 = -det3x3( m32, m33, m30,
+                     m02, m03, m00,
+                     m12, m13, m10 )
+
+		cof22 =  det3x3( m33, m30, m31,
+                     m03, m00, m01,
+                     m13, m10, m11 )
+
+		cof23 = -det3x3( m30, m31, m32,
+                     m00, m01, m02,
+                     m10, m11, m12 )
+
+
+		cof30 = -det3x3( m01, m02, m03,
+                     m11, m12, m13,
+                     m21, m22, m23 )
+
+		cof31 =  det3x3( m02, m03, m00,
+                     m12, m13, m10,
+                     m22, m23, m20 )
+
+		cof32 = -det3x3( m03, m00, m01,
+                     m13, m10, m11,
+                     m23, m20, m21 )
+
+		cof33 =  det3x3( m00, m01, m02,
+                     m10, m11, m12,
+                     m20, m21, m22 )
+
+		self.Set(0, 0, cof00 * inv_det)
+		self.Set(0, 1, cof01 * inv_det)
+		self.Set(0, 2, cof02 * inv_det)
+		self.Set(0, 3, cof03 * inv_det)
+
+		self.Set(1, 0, cof10 * inv_det)
+		self.Set(1, 1, cof11 * inv_det)
+		self.Set(1, 2, cof12 * inv_det)
+		self.Set(1, 3, cof13 * inv_det)
+
+		self.Set(2, 0, cof20 * inv_det)
+		self.Set(2, 1, cof21 * inv_det)
+		self.Set(2, 2, cof22 * inv_det)
+		self.Set(2, 3, cof23 * inv_det)
+
+		self.Set(3, 0, cof30 * inv_det)
+		self.Set(3, 1, cof31 * inv_det)
+		self.Set(3, 2, cof32 * inv_det)
+		self.Set(3, 3, cof33 * inv_det)
+
 
 if __name__ == "__main__":
 	# unit test
@@ -316,3 +431,13 @@
 	print "frustum in:  %f, %f, %f, %f, %f, %f" % (l0, r0, b0, t0, n0, f0)
 	print "frustum out: %f, %f, %f, %f, %f, %f" % (l1, r1, b1, t1, n1, f1)
 
+	p = CRMatrix()
+	p.Load([12, 57, 58, 111, -54, 45, -1544, 554, -415, 121, 4, 55, 1, 44, -77, -1])
+	q = CRMatrix()
+	q.Load(p.ToList())
+	q.Invert();
+	q.Multiply(p.ToList())
+	print "\np*p^-1 = "
+	q.Print()
+	
+
Index: spu/tilesort/tilesortspu_misc.c
===================================================================
RCS file: /cvsroot/chromium/cr/spu/tilesort/tilesortspu_misc.c,v
retrieving revision 1.34
diff -u -r1.34 tilesortspu_misc.c
--- spu/tilesort/tilesortspu_misc.c	18 Feb 2005 23:15:37 -0000	1.34
+++ spu/tilesort/tilesortspu_misc.c	18 Jul 2006 23:04:03 -0000
@@ -308,20 +308,11 @@
 				/* send new frustum to <server> */
 				crPackSetBuffer( thread->packer, &(thread->buffer[server]) );
 
-				/** XXX \todo Perhaps it should be the application's responsibility to
-				 * set the matrix mode, load the identity matrix, etc?
-				 */
 				if (tilesort_spu.swap) {
-					crPackPushAttribSWAP(GL_TRANSFORM_BIT);
-					crPackMatrixModeSWAP(GL_PROJECTION);
-					crPackLoadMatrixfSWAP(v + 2);
-					crPackPopAttribSWAP();
+					crPackChromiumParametervCRSWAP(target, type, count, values);
 				}
 				else {
-					crPackPushAttrib(GL_TRANSFORM_BIT);
-					crPackMatrixMode(GL_PROJECTION);
-					crPackLoadMatrixf(v + 2);
-					crPackPopAttrib();
+					crPackChromiumParametervCR(target, type, count, values);
 				}
 				/* release server buffer */
 				crPackReleaseBuffer( thread->packer );
Index: util/matrix.c
===================================================================
RCS file: /cvsroot/chromium/cr/util/matrix.c,v
retrieving revision 1.3
diff -u -r1.3 matrix.c
--- util/matrix.c	17 Feb 2004 22:20:17 -0000	1.3
+++ util/matrix.c	18 Jul 2006 23:04:40 -0000
@@ -192,7 +192,6 @@
 }
 
 
-#if 0
 void
 crMatrixTransformPointf(const CRmatrix *m, GLvectorf *p) 
 {
@@ -221,7 +220,6 @@
 	p->z = (double) (m->m02*x + m->m12*y + m->m22*z + m->m32*w);
 	p->w = (double) (m->m03*x + m->m13*y + m->m23*z + m->m33*w);
 }
-#endif
 
 
 void
@@ -344,6 +342,18 @@
 	inv->m23 = cof32 * inv_det; inv->m33 = cof33 * inv_det;
 }
 
+void 
+crMatrixTranspose(CRmatrix *t, const CRmatrix *m)
+{
+  CRmatrix c;
+
+	c.m00 = m->m00;  c.m10 = m->m01;  c.m20 = m->m02;  c.m30 = m->m03;
+	c.m01 = m->m10;  c.m11 = m->m11;  c.m21 = m->m12;  c.m31 = m->m13;
+	c.m02 = m->m20;  c.m12 = m->m21;  c.m22 = m->m22;  c.m32 = m->m23;
+	c.m03 = m->m30;  c.m13 = m->m31;  c.m23 = m->m32;  c.m33 = m->m33;
+
+	*t = c;
+}
 
 /*
  * Apply a translation to the given matrix.
Index: util/util.def
===================================================================
RCS file: /cvsroot/chromium/cr/util/util.def,v
retrieving revision 1.56
diff -u -r1.56 util.def
--- util/util.def	3 Feb 2006 22:50:44 -0000	1.56
+++ util/util.def	18 Jul 2006 23:04:41 -0000
@@ -156,8 +156,12 @@
 crMatrixCopy
 crMatrixMultiply
 crMatrixInvertTranspose
+crMatrixTranspose
 crMatrixTranslate
 crMatrixRotate
 crMatrixScale
 crMatrixFrustum
 crMatrixOrtho
+crMatrixTransformPointf
+crMatrixTransformPointd
+

--------------060408090205090506000709
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
--------------060408090205090506000709
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Chromium-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chromium-dev

--------------060408090205090506000709--