Trying to write a small class that will draw a 3D sphere primitive.
What I'm doing now is working, however, I cannot get the sphere to rotate around itself unless it is very close to the origin of my world.
This code is not in a speed-critical section - all spheres are generated in the constructor - all objects are created prior to rendering the world. This will be called once for every sphere (planet) in the world (universe) during the load process.
Here is the code to create the base circle:
Code:
class Vector
{
public:
double x;
double y;
double z;
....
....
};
struct Vertex
{
Vector Local;
Vector World;
Vector Aligned;
};
void Sphere::Sphere(Vector center,double lx,double ly,double lz,short NumFacets,short radius)
{
double lx=center.x;
double ly=center.y;
double lz=center.z;
SphereCenter.Local=center; //SphereCenter is type vertex in
//class
double anginc=6.28f/(double)NumFacets;
short NumXCircles=(short)(6.28f/angleinc);
short NumYCircles=NumXCircles;
short TotalVertexes=(NumXCircles*NumYCircles)<<1;
Vtxs=new Vertex[TotalVertexes];
Vertex *BaseCircle=new Vertex[NumFacets];
for (int i=0;i<NumFacets;i++)
{
double localx=(double)radius*cos(curangle);
double localy=(double)radius*sin(curangle);
double localz=0;
Vector temp(localx,localy,localz);
BaseCircle[i].Local=temp; //operator handled by vector class
curangle+=anginc;
}
//Now translate lx,ly,lz on x,y,z
//rotate this circle ax,ay, az radians
//Transform using local space to get world space
//Append result to vertexes for sphere
....
....
//Too lengthy to post here
}
If you find errors in the code, I posted wrong. There are no warnings or errors when I compile the project.
The translation to world coords causes them to rotate around lx-origin, ly-origin, lz-origin, not around their actual centers.
I think the problem is that my sphere is not centered around the x,y,z axes. This would cause the sphere to rotate around that point, rather than it's center. However, I've also tried to create a sphere using x,z and y,z, but the same thing happens.
How do I rotate these spheres around their central point, rather than around a point in space? I don't want them to orbit, they need to rotate.
An algorithm to create the approx of a sphere around point lx,ly,lz at radius r and facets f is what I'm looking for.