sphere.cpp
author Dmitriy Morozov <morozov@cs.duke.edu>
Mon, 29 Jan 2007 10:56:09 -0500
changeset 11 b4cd9b9d6ca5
parent 9 b16a476ef46d
permissions -rw-r--r--
Switched to CMake

#include "sphere.h"
#include <cmath>

void CreateSphere(XYZ c,double r,int n)
{
	int i,j;
	double theta1,theta2,theta3;
	XYZ e,p;

	if (r < 0)
		r = -r;
	if (n < 0)
		n = -n;
	if (n < 4 || r <= 0) {
		glBegin(GL_POINTS);
		glVertex3f(c.x,c.y,c.z);
		glEnd();
		return;
	}

	for (j=0;j<n/2;j++) {
		theta1 = j * TWOPI / n - PID2;
		theta2 = (j + 1) * TWOPI / n - PID2;

		glBegin(GL_QUAD_STRIP);
		for (i=0;i<=n;i++) {
			theta3 = i * TWOPI / n;

			e.x = cos(theta2) * cos(theta3);
			e.y = sin(theta2);
			e.z = cos(theta2) * sin(theta3);
			p.x = c.x + r * e.x;
			p.y = c.y + r * e.y;
			p.z = c.z + r * e.z;

			glNormal3f(e.x,e.y,e.z);
			glTexCoord2f(i/(double)n,2*(j+1)/(double)n);
			glVertex3f(p.x,p.y,p.z);

			e.x = cos(theta1) * cos(theta3);
			e.y = sin(theta1);
			e.z = cos(theta1) * sin(theta3);
			p.x = c.x + r * e.x;
			p.y = c.y + r * e.y;
			p.z = c.z + r * e.z;

			glNormal3f(e.x,e.y,e.z);
			glTexCoord2f(i/(double)n,2*j/(double)n);
			glVertex3f(p.x,p.y,p.z);
		}
		glEnd();
	}
}