Added sphere files
authorDmitriy Morozov <morozov@cs.duke.edu>
Mon, 07 Aug 2006 09:36:23 -0400
changeset 9 b16a476ef46d
parent 8 2d94ef3aab03
child 10 240ec9bb3d92
Added sphere files
sphere.cpp
sphere.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sphere.cpp	Mon Aug 07 09:36:23 2006 -0400
@@ -0,0 +1,53 @@
+#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();
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sphere.h	Mon Aug 07 09:36:23 2006 -0400
@@ -0,0 +1,22 @@
+/*
+	Create a sphere centered at c, with radius r, and precision n
+	Draw a point for zero radius spheres
+
+	Code by Paul Bourke	<http://astronomy.swin.edu.au/~pbourke/opengl/sphere/>
+*/
+
+#include <GL/gl.h>
+
+const double TWOPI = 2*3.14;
+const double PID2 = 3.14/2;
+
+struct XYZ
+{
+	XYZ(GLfloat xx = 0, GLfloat yy = 0, GLfloat zz = 0):
+		x(xx), y(yy), z(zz)
+	{}
+	
+	GLfloat x,y,z;
+};
+
+void CreateSphere(XYZ c,double r,int n);