From 029290cd81e8fe8d0cd48e45682ac9a31ef732ce Mon Sep 17 00:00:00 2001
From: Gundalf <gundalf01@users.sourceforge.net>
Date: Thu, 22 Jun 2006 21:19:17 +0000
Subject: [PATCH] Vertex Colors

---
 NifExport/Config.cpp   |  4 ++--
 NifExport/Exporter.h   |  2 +-
 NifExport/Mesh.cpp     | 28 ++++++++++++++++++++++++++--
 NifExport/NifExport.rc |  2 +-
 4 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/NifExport/Config.cpp b/NifExport/Config.cpp
index 5e3ac3e..3c4493a 100755
--- a/NifExport/Config.cpp
+++ b/NifExport/Config.cpp
@@ -29,7 +29,7 @@ void Exporter::writeConfig()
 	regSet(hKey, "npx_furn", mExportFurn);
 	regSet(hKey, "npx_lights", mExportLights);
 	regSet(hKey, "npx_vcolors", mVertexColors);
-	regSet(hKey, "npx_wthresh", mWeldThresh);
+//	regSet(hKey, "npx_wthresh", mWeldThresh);
 	regSet(hKey, "npx_tprefix", mTexPrefix);
 	regSet(hKey, "npx_coll", mExportCollision);
 }
@@ -52,7 +52,7 @@ void Exporter::readConfig()
 	regGet(hKey, "npx_furn", mExportFurn);
 	regGet(hKey, "npx_lights", mExportLights);
 	regGet(hKey, "npx_vcolors", mVertexColors);
-	regGet(hKey, "npx_wthresh", mWeldThresh);
+//	regGet(hKey, "npx_wthresh", mWeldThresh);
 	regGet(hKey, "npx_tprefix", mTexPrefix);
 	regGet(hKey, "npx_coll", mExportCollision);
 }
diff --git a/NifExport/Exporter.h b/NifExport/Exporter.h
index cbf293a..46b1120 100755
--- a/NifExport/Exporter.h
+++ b/NifExport/Exporter.h
@@ -90,7 +90,7 @@ private:
 	/* mesh export */
 	// adds a vertex to a face group if it doesn't exist yet. returns new or previous index into the
 	// vertex array.
-	int 				addVertex(FaceGroup &grp, const Point3 &pt, const Point3 &uv, const Point3 &norm);
+	int 				addVertex(FaceGroup &grp, int face, int vi, Mesh *mesh);
 	// adds a face to a face group
 	void				addFace(FaceGroup &grp, int face, const int vi[3], Mesh *mesh);
 	// returns true if at least one of the colors in the group has a value != (1, 1, 1, 1)
diff --git a/NifExport/Mesh.cpp b/NifExport/Mesh.cpp
index 789ad1d..5d07d3e 100755
--- a/NifExport/Mesh.cpp
+++ b/NifExport/Mesh.cpp
@@ -97,6 +97,9 @@ bool Exporter::makeMesh(NiNodeRef &parent, Mtl *mtl, FaceGroup &grp)
 	data->SetUVSetCount(1);
 	data->SetUVSet(0, grp.uvs);
 
+	if (grp.vcolors.size() > 0)
+		data->SetVertexColors(grp.vcolors);
+
 	shape->SetData(data);
 
 	NiAVObjectRef av(DynamicCast<NiAVObject>(shape));
@@ -108,20 +111,39 @@ bool Exporter::makeMesh(NiNodeRef &parent, Mtl *mtl, FaceGroup &grp)
 	return true;
 }
 
-int Exporter::addVertex(FaceGroup &grp, const Point3 &pt, const Point3 &uv, const Point3 &norm)
+int Exporter::addVertex(FaceGroup &grp, int face, int vi, Mesh *mesh)
 {
+	Point3 pt = mesh->verts[ mesh->faces[ face ].v[ vi ] ];
+	Point3 uv = mesh->tVerts[ mesh->tvFace[ face ].t[ vi ]];
+	Point3 norm = getVertexNormal(mesh, face, mesh->getRVertPtr(mesh->faces[ face ].v[ vi ]));
+
+	VertColor col;
+	if (mVertexColors && mesh->vertCol)
+		col = mesh->vertCol[ mesh->vcFace[ face ].t[ vi ] ];
+
 	for (int i=0; i<grp.verts.size(); i++)
 	{
 		if (equal(grp.verts[i], pt, mWeldThresh) &&
 			grp.uvs[i].u==uv.x && grp.uvs[i].v==uv.y &&
 			equal(grp.vnorms[i], norm, 0))
+		{
+			if (mVertexColors && mesh->vertCol &&
+				(grp.vcolors[i].r!=col.x ||
+				 grp.vcolors[i].g!=col.y ||
+				 grp.vcolors[i].b!=col.z))
+				continue;
+
 			return i;
+		}
 	}
 	
 	grp.verts.push_back(Vector3(pt.x, pt.y, pt.z));
 	grp.uvs.push_back(TexCoord(uv.x, uv.y));
 	grp.vnorms.push_back(Vector3(norm.x, norm.y, norm.z));
 
+	if (mVertexColors && mesh->vertCol)
+		grp.vcolors.push_back(Color4(col.x, col.y, col.z, 1));
+
 	return grp.verts.size()-1;
 }
 
@@ -130,11 +152,13 @@ void Exporter::addFace(FaceGroup &grp, int face, const int vi[3], Mesh *mesh)
 	Triangle tri;
 	for (int i=0; i<3; i++)
 	{
-		Point3 tv = mesh->verts[ mesh->faces[ face ].v[ vi[i] ] ]; // * tm;
+/*		Point3 tv = mesh->verts[ mesh->faces[ face ].v[ vi[i] ] ]; // * tm;
 		tri[i] = addVertex(grp, 
 			               tv, 
 						   mesh->tVerts[ mesh->tvFace[ face ].t[ vi[i] ]], 
 						   getVertexNormal(mesh, face, mesh->getRVertPtr(mesh->faces[ face ].v[ vi[i] ])));
+*/
+		tri[i] = addVertex(grp, face, vi[i], mesh);
 	}
 
 	grp.faces.push_back(tri);
diff --git a/NifExport/NifExport.rc b/NifExport/NifExport.rc
index 8ef5ce1..c878de8 100755
--- a/NifExport/NifExport.rc
+++ b/NifExport/NifExport.rc
@@ -102,7 +102,7 @@ BEGIN
     CONTROL         "Export Co&llision",IDC_CHK_COLL,"Button",
                     BS_AUTOCHECKBOX | WS_TABSTOP,85,18,68,10
     CONTROL         "&Vertex Colors",IDC_CHK_VCOLORS,"Button",
-                    BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,85,31,68,10
+                    BS_AUTOCHECKBOX | WS_TABSTOP,85,31,68,10
     LTEXT           "http://niftools.sourceforge.net",IDC_LBL_LINK,98,87,95,
                     14,SS_NOTIFY | SS_CENTERIMAGE
 END
-- 
GitLab