# sphere algo

## Recommended Posts

with detail 1 it tesselates nicely, but higher detail gives strange results... Can anyone see the problem? Thanks, Marty
cSphere::cSphere(int Detail)
{
numverts = 0; numfaces = 0;
tVec3 v;
v.x = 0.5;	v.y = 0.5;	v.z = 0;
UnitVector(&v);
Verts.push_back(v); numverts++;
v.x = 0.5;	v.y = -0.5;	v.z = 0;
UnitVector(&v);
Verts.push_back(v); numverts++;
v.x = -0.5;	v.y = -0.5;	v.z = 0;
UnitVector(&v);
Verts.push_back(v); numverts++;
v.x = -0.5;	v.y = 0.5;	v.z = 0;
UnitVector(&v);
Verts.push_back(v); numverts++;
v.x = 0;	v.y = 0;	v.z = sqrt(0.5);
UnitVector(&v);
Verts.push_back(v); numverts++;
v.x = 0;	v.y = 0;	v.z = -sqrt(0.5);
UnitVector(&v);
Verts.push_back(v); numverts++;

tFace f;
f.Verts[0] = 0;	f.Verts[1] = 4;	f.Verts[2] = 1;
Faces.push_back(f); numfaces++;
f.Verts[0] = 1;	f.Verts[1] = 4;	f.Verts[2] = 2;
Faces.push_back(f); numfaces++;
f.Verts[0] = 2;	f.Verts[1] = 4;	f.Verts[2] = 3;
Faces.push_back(f); numfaces++;
f.Verts[0] = 3;	f.Verts[1] = 4;	f.Verts[2] = 0;
Faces.push_back(f); numfaces++;
f.Verts[0] = 0;	f.Verts[1] = 1;	f.Verts[2] = 5;
Faces.push_back(f); numfaces++;
f.Verts[0] = 1;	f.Verts[1] = 2;	f.Verts[2] = 5;
Faces.push_back(f); numfaces++;
f.Verts[0] = 2;	f.Verts[1] = 3;	f.Verts[2] = 5;
Faces.push_back(f); numfaces++;
f.Verts[0] = 3;	f.Verts[1] = 0;	f.Verts[2] = 5;
Faces.push_back(f); numfaces++;

// the code above creates two pyramids, i think it's called an octahedron. It's 8 triangles with all the edges 1.
// UnitVector makes the edges a different size, but all the points will be removed 1 from the origin.
// It's tested and works fine

tVec3 average;
int old;
for (int i = 0; i < Detail; i++)
{
int numnewfaces = 0;
for (int j = 0; j < numfaces; j++)
{
// calculate average and make unit vector of it
average = ((Verts[Faces[j].Verts[0]] + Verts[Faces[j].Verts[1]] + Verts[Faces[j].Verts[2]])/3);
// i'll keep this one out just to see if it tesselates nicely UnitVector(&average);
// keep the old index of the 0th vert
old = Faces[j].Verts[0];
// add the average vert to the list and set the 0th vert of this face to that one
Verts.push_back(average);
Faces[j].Verts[0] = numverts;
// create a new face and add it to the list
f.Verts[0] = Faces[j].Verts[2];
f.Verts[1] = old;
f.Verts[2] = numverts;
Faces.push_back(f); numnewfaces++; // keep track of the number of new faces made
// create a new face and add it to the list
f.Verts[0] = old;
f.Verts[1] = Faces[j].Verts[1];
f.Verts[1] = numverts;
Faces.push_back(f); numnewfaces++; // keep track of the number of new faces made
numverts++; // we created a vert, but increment now, because we had to set this index to the new faces
}
numfaces += numnewfaces;
}
}



##### Share on other sites
Ok, this is kind of a guess, since I didn't run the code or anything, but I looked at it for a minute...

I think you chose a bad method for subdividing your triangles. By always creating three tris based around the middle point, you will end up with lots of skinny little things, instead of nice equal sized triangles.

I think what you want to try is to subdivide the edges instead. find the midpoint of each edge. Make a triangle that uses the three midpoints, and three more triangles to fill up the corners. Draw a picture and you'll understand what I mean. This method will keep your triangles a nice uniform shape and size.

##### Share on other sites
Ok, I'm tesselating edges now and it works fine... :D
Thanks

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628383
• Total Posts
2982371

• 10
• 9
• 15
• 24
• 11