summary refs log tree commit diff
path: root/src/Models.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Models.cpp')
-rw-r--r--src/Models.cpp70
1 files changed, 22 insertions, 48 deletions
diff --git a/src/Models.cpp b/src/Models.cpp
index 4c92f93..6632562 100644
--- a/src/Models.cpp
+++ b/src/Models.cpp
@@ -4,10 +4,18 @@
 #include "Models.h"
 #include "misc.h"
 
-//Functions
-void Model::UpdateVertexArray(){
-	int i;
-	for(i=0;i<TriangleNum;i++){
+void Model::CalculateNormals()
+{
+	for (int i = 0; i < TriangleNum; ++i) {
+		CrossProduct(vertex[Triangles[i].vertex[1]]
+			- vertex[Triangles[i].vertex[0]],
+			vertex[Triangles[i].vertex[2]]
+			- vertex[Triangles[i].vertex[0]],
+			normals + i);
+		Normalise(normals + i);
+	}
+
+	for (int i = 0; i < TriangleNum; ++i) {
 		vArray[i*27+0]=vertex[Triangles[i].vertex[0]].x;
 		vArray[i*27+1]=vertex[Triangles[i].vertex[0]].y;
 		vArray[i*27+2]=vertex[Triangles[i].vertex[0]].z;
@@ -39,29 +47,16 @@ void Model::UpdateVertexArray(){
 		vArray[i*27+26]=Triangles[i].b;
 	}
 
-	XYZ average;
-	int howmany;
-	average=0;
-	howmany=0;
-	boundingboxmin=20000;
-	boundingboxmax=-20000;
-	for(int i=0;i<vertexNum;i++){
-		howmany++;
-		average=average+vertex[i];
-		if(vertex[i].x<boundingboxmin.x)boundingboxmin.x=vertex[i].x;
-		if(vertex[i].y<boundingboxmin.y)boundingboxmin.y=vertex[i].y;
-		if(vertex[i].z<boundingboxmin.z)boundingboxmin.z=vertex[i].z;
-		if(vertex[i].x>boundingboxmax.x)boundingboxmax.x=vertex[i].x;
-		if(vertex[i].y>boundingboxmax.y)boundingboxmax.y=vertex[i].y;
-		if(vertex[i].z>boundingboxmax.z)boundingboxmax.z=vertex[i].z;
-	}
-	average=average/howmany;
-	boundingspherecenter=average;
-	boundingsphereradius=0;
-	for(int i=0;i<vertexNum;i++){
-		if(findDistancefast(average,vertex[i])>boundingsphereradius)boundingsphereradius=findDistancefast(average,vertex[i]);
-	}
-	boundingsphereradius=sqrt(boundingsphereradius);
+	boundingspherecenter = {};
+	for (int i = 0; i < vertexNum; ++i)
+		boundingspherecenter += vertex[i];
+	boundingspherecenter /= vertexNum;
+
+	boundingsphereradius = 0;
+	for (int i = 0; i < vertexNum; ++i)
+		boundingsphereradius = max(boundingsphereradius,
+			findDistancefast(boundingspherecenter, vertex[i]));
+	boundingsphereradius = sqrt(boundingsphereradius);
 }
 
 void Model::load(const char* path)
@@ -85,30 +80,9 @@ void Model::load(const char* path)
 		Triangles[i].b = model.faces.ptr[i].b;
 	}
 	free(model.faces.ptr);
-
-	XYZ average {};
-	for (auto&& v : vertex)
-		boundingspherecenter += v;
-	boundingspherecenter /= vertexNum;
-
-	boundingsphereradius = 0;
-	for (auto&& v : vertex)
-		boundingsphereradius = std::max(boundingsphereradius,
-			findDistancefast(average, v));
-	boundingsphereradius = sqrt(boundingsphereradius);
 	CalculateNormals();
 }
 
-void Model::CalculateNormals()
-{
-	int i;
-	for(i=0;i<TriangleNum;i++){
-		CrossProduct(vertex[Triangles[i].vertex[1]]-vertex[Triangles[i].vertex[0]],vertex[Triangles[i].vertex[2]]-vertex[Triangles[i].vertex[0]],&normals[i]);
-		Normalise(&normals[i]);
-	}
-	UpdateVertexArray();
-}
-
 extern int nocolors;
 void Model::draw()
 {