Gammadon (timv) wrote in 3dgraphics,
Gammadon
timv
3dgraphics

YAY!!!

I am sure there's an easier way, but it was still fun working through it...
Image Hosted by ImageShack.us

Mesh CreateUnitSphere(const int &step)
{
     int i,j,p;
     Mesh ret;
     Vector4 v;
     Matrix4x4 mx = M44RotateX(-PI/step);
     Matrix4x4 my = M44RotateY(-PI2/step);


     int pts = 0;
     v = Vector4(0,1,0,1);
     ret.LPoints.push_back(v);


     for (i=0; i<step-1; i++)
     {
          v = V4Multiply(mx,v);
          for (j=0; j<step; j++)
          {
               v = V4Multiply(my,v);
               ret.LPoints.push_back(v);
               pts++;
          }
     }
     v = Vector4(0,-1,0,1);
     ret.LPoints.push_back(v);
    
     for (p=1; p<step; p++)
     {
          ret.faces.push_back(Vector4(p+1,p,0,0));
     }
     ret.faces.push_back(Vector4(1,p,0,0));


     for (i=0; i<step-2; i++)
     {
          for (j=1; j<step; j++)
          {
               p=(i*step)+j;
               ret.faces.push_back(Vector4(p,p+1,p+step,0));
               ret.faces.push_back(Vector4(p+1,p+step+1,p+step,0));
          }
          p=(i*step)+j;
          ret.faces.push_back(Vector4(p,p-j+1,p+step,0));
          ret.faces.push_back(Vector4(p-j+1,p-j+step+1,p+step,0));
     }


     for (p=pts-step+1; p<pts; p++)
     {
          ret.faces.push_back(Vector4(pts,p-1,p,0));
     }
     ret.faces.push_back(Vector4(pts,p-1,pts-step,0));


     return ret;


}

  • Post a new comment

    Error

    default userpic
  • 1 comment