Game Development Reference
In-Depth Information
the top of the quad strip and one for the bottom. The bottom slice uses the
triangle fan method and is effectively the same as the top, only this time
flipped around the x -axis so that the y values are negative.
void CGLView::DrawSphere(int segments, int slices, double radius)
{
//Draw a sphere centred at the origin with radius=radius
//Seqments are the number of polygons around the main axis
//Slices are the number of polygons up and down
int i,j;
double theta,sliceradius, sliceradiusb;
CVector vertex, vertexb, norm;
GLfloat col[3];
GetDocument()->GetColour(col);
//Draw the top
glColor3d(col[0],col[1],col[2]);
glBegin(GL_TRIANGLE_FAN);
//First set vertex 0
vertex.Set(0.0, radius, 0.0);
norm.Set( 0.0, 1.0, 0.0);
glNormal3d(norm.x, norm.y, norm.z);
glVertex3d(vertex.x, vertex.y, vertex.z);
//All subsequent vertices use the samey value
vertex.y = radius*cos(PI/(double)slices);
sliceradius = sqrt(radius*radius-vertex.y*vertex.y);
for(i=0;i<=segments;i++){
theta = PI2*((double)i/(double)segments);
vertex.x = sliceradius*cos(theta);
vertex.z = sliceradius*sin(theta);
norm = vertex;
norm.Normalize();
glNormal3d(norm.x, norm.y, norm.z);
glVertex3d(vertex.x, vertex.y, vertex.z);
}
glEnd();
//Draw the middle
glBegin(GL_QUAD_STRIP);
for(j=1;j<(slices-1);j++){
vertex.y = radius*cos(((double)j/(double)slices)*PI);
sliceradius = sqrt(radius*radius-vertex.y*vertex.y);
Search Nedrilad ::




Custom Search