r697 - trunk

lordhavoc at icculus.org lordhavoc at icculus.org
Sat Apr 8 23:10:04 EDT 2006


Author: lordhavoc
Date: 2006-04-08 23:10:04 -0400 (Sat, 08 Apr 2006)
New Revision: 697

Modified:
   trunk/model.c
   trunk/model.h
Log:
changed weight format to bytes to save memory (still vertex shader
friendly)


Modified: trunk/model.c
===================================================================
--- trunk/model.c	2006-04-09 02:53:10 UTC (rev 696)
+++ trunk/model.c	2006-04-09 03:10:04 UTC (rev 697)
@@ -377,8 +377,8 @@
 					mesh->data_normal3f = Mem_Alloc(r->memzone, mesh->max_vertices * sizeof(float[3]));
 					mesh->data_texcoord2f = Mem_Alloc(r->memzone, mesh->max_vertices * sizeof(float[2]));
 					mesh->data_lightmaptexcoord2f = Mem_Alloc(r->memzone, mesh->max_vertices * sizeof(float[2]));
-					mesh->data_weightindex4i = Mem_Alloc(r->memzone, mesh->max_vertices * sizeof(NUint32[4]));
-					mesh->data_weightvalue4f = Mem_Alloc(r->memzone, mesh->max_vertices * sizeof(float[4]));
+					mesh->data_weightindex4b = Mem_Alloc(r->memzone, mesh->max_vertices * sizeof(NUint8[4]));
+					mesh->data_weightvalue4b = Mem_Alloc(r->memzone, mesh->max_vertices * sizeof(NUint8[4]));
 					md5weightrange = Mem_Alloc(Global_Zone, mesh->max_vertices * sizeof(NUint32[2]));
 				}
 				//	vert 0 ( 0.142532 0.983877 ) 0 1
@@ -501,6 +501,10 @@
 			{
 				Ndouble sum = 0;
 				NUint32 k;
+				NUint32 wi[4];
+				Nfloat wf[4];
+				Vector4Clear(wi);
+				Vector4Clear(wf);
 				for (j = 0, md5weight = md5weights + md5weightrange[i*2+0];j < md5weightrange[i*2+1];j++, md5weight++)
 				{
 					matrix = &model->data_transforminfo[md5weight->jointnum].basematrix;
@@ -508,35 +512,41 @@
 					mesh->data_vertex3f[i*3+1] += DotProduct4(md5weight->vertex, matrix->m[1]);
 					mesh->data_vertex3f[i*3+2] += DotProduct4(md5weight->vertex, matrix->m[2]);
 					// store the best weights in the vertex's limited number of blend weights
-					for (k = 4;k >= 1 && mesh->data_weightvalue4f[i*4+k-1] < md5weight->vertex[3];k--)
+					for (k = 4;k >= 1 && mesh->data_weightvalue4b[i*4+k-1] < md5weight->vertex[3];k--)
 					{
 						if (k < 4)
 						{
-							mesh->data_weightindex4i[i*4+k] = mesh->data_weightindex4i[i*4+k-1];
-							mesh->data_weightvalue4f[i*4+k] = mesh->data_weightvalue4f[i*4+k-1];
+							wi[k] = wi[k-1];
+							wf[k] = wf[k-1];
 						}
 					}
 					if (k < 4)
 					{
-						mesh->data_weightindex4i[i*4+k] = md5weight->jointnum;
-						mesh->data_weightvalue4f[i*4+k] = md5weight->vertex[3];
+						wi[k] = md5weight->jointnum;
+						wf[k] = md5weight->vertex[3];
 					}
 					sum += md5weight->vertex[3];
 				}
 				if (fabs(sum - 1) > 0.001)
 					Console_Printf("vertex #%i (weights %i-%i) does not sum to 1! (sum = %f)\n", i, md5weightrange[i*2+0], md5weightrange[i*2+0]+md5weightrange[i*2+1], sum);
-				if (mesh->data_weightvalue4f[i*4+3] > mesh->data_weightvalue4f[i*4+2] || mesh->data_weightvalue4f[i*4+2] > mesh->data_weightvalue4f[i*4+1] || mesh->data_weightvalue4f[i*4+1] > mesh->data_weightvalue4f[i*4+0])
-					Console_Printf("vertex sorting error!  vertex #%i has weights %i:%f %i:%f %i:%f %i:%f\n", i, mesh->data_weightindex4i[i*4+0], mesh->data_weightvalue4f[i*4+0], mesh->data_weightindex4i[i*4+1], mesh->data_weightvalue4f[i*4+1], mesh->data_weightindex4i[i*4+2], mesh->data_weightvalue4f[i*4+2], mesh->data_weightindex4i[i*4+3], mesh->data_weightvalue4f[i*4+3]);
+				if (wf[3] > wf[2] || wf[2] > wf[1] || wf[1] > wf[0])
+					Console_Printf("vertex sorting error!  vertex #%i has weights %i:%f %i:%f %i:%f %i:%f\n", i, wi[0], wf[0], wi[1], wf[1], wi[2], wf[2], wi[3], wf[3]);
 				// renormalize the array of strongest weights on this vertex
 				sum = 0;
 				for (k = 0;k < 4;k++)
-					sum += mesh->data_weightvalue4f[i*4+k];
+					sum += wf[k];
 				if (sum)
 				{
 					sum = 1.0f / sum;
 					for (k = 0;k < 4;k++)
-						mesh->data_weightvalue4f[i*4+k] *= sum;
+						wf[k] *= sum;
 				}
+				// now convert to the compact format used for longterm storage
+				for (k = 0;k < 4;k++)
+				{
+					mesh->data_weightindex4b[i*4+k] = (NUint8)wi[k];
+					mesh->data_weightvalue4b[i*4+k] = (NUint8)(wf[k] * 255.0f);
+				}
 			}
 			// compile the base frame of the mesh for static uses
 			Model_MeshVectors(mesh->num_triangles, mesh->data_element3i, mesh->num_vertices, mesh->data_vertex3f, mesh->data_texcoord2f, mesh->data_svector3f, mesh->data_tvector3f, mesh->data_normal3f);
@@ -580,9 +590,9 @@
 					mesh->basecullmaxs[1] = Max(mesh->basecullmaxs[1], mesh->data_vertex3f[i * 3 + 1]);
 					mesh->basecullmaxs[2] = Max(mesh->basecullmaxs[2], mesh->data_vertex3f[i * 3 + 2]);
 				}
-				for (k = 0;k < 4 && mesh->data_weightvalue4f[i*4+k] > 0;k++)
+				for (k = 0;k < 4 && mesh->data_weightvalue4b[i*4+k] > 0;k++)
 				{
-					NUint32 index = mesh->data_weightindex4i[i*4+k];
+					NUint32 index = mesh->data_weightindex4b[i*4+k];
 					Nvec3 v1, v;
 					Nvec r;
 					VectorCopy(mesh->data_vertex3f + i * 3, v1);
@@ -676,9 +686,9 @@
 	for (j = 0, index = 0;j < mesh->num_vertices;j++, index += 3)
 	{
 		float value;
-		for (k = 0;k < 4 && (value = mesh->data_weightvalue4f[j*4+k]);k++)
+		for (k = 0;k < 4 && (value = mesh->data_weightvalue4b[j*4+k] * (1.0f / 255.0f));k++)
 		{
-			matrix = relativematrix + mesh->data_weightindex4i[j*4+k];
+			matrix = relativematrix + mesh->data_weightindex4b[j*4+k];
 			if (outvertex3f)
 			{
 				outvertex3f[index+0] += value * (DotProduct(v + index, matrix->m[0]) + matrix->m[0][3]);

Modified: trunk/model.h
===================================================================
--- trunk/model.h	2006-04-09 02:53:10 UTC (rev 696)
+++ trunk/model.h	2006-04-09 03:10:04 UTC (rev 697)
@@ -58,9 +58,9 @@
 	// lightmap texture coordinates for vertices in this mesh
 	float *data_lightmaptexcoord2f;
 	// indices of up to 4 transforms per vertex
-	int *data_weightindex4i;
+	NUint8 *data_weightindex4b;
 	// blending influence values of up to 4 transforms per vertex
-	float *data_weightvalue4f;
+	NUint8 *data_weightvalue4b;
 
 	Nvec3 basecullmins;
 	Nvec3 basecullmaxs;




More information about the neither-commits mailing list