• ### Popular Now

• 9
• 9
• 10
• 10
• 11

#### Archived

This topic is now archived and is closed to further replies.

# rendering a sphere

This topic is 5757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

id like to write my own sphere rendering function (gluSphere() doesnt give me the ability to do what i want or i havnt figured out to do it using it heh). basically im not too sure how to go about rendering it...getting the shperical (sp) shape. if you could point me to any sites or information id be most grateful. thanks in advanced

##### Share on other sites
I did what you are trying to do right now. here is what I did. I made a circle that was lying lat on the z axis (What i mean i i just made y into z), by using the equations
x=cos(a)
and
z = sin(a)

a = a variable that goes from 0 to 360

i stored the points in an array and then took an equation to rotate points around the z axis. I rotated these points and stored the new points in another array. i did this a couple times and got my sphere. hope that helps the z-rotation equation is:

newx=x*cos(a)-y*sin(a)
newy=y*cos(a)-x*sin(a)
newz=z

[edited by - tHiSiSbOb on July 15, 2002 3:55:49 PM]

##### Share on other sites
tHiSiSbOb almost has the correct sphere generation code, but it can be done without forming a 2d sphere and rotating it.

if you search for "sperical coordinates" you should find it.

http://www.playbeing.co.uk/pics/moonhi.png

i think i went overboard with my custom sphere routine

i basically used the technique of splitting the faces of an icosahedron recusively, this gives you a perfect sphere with all the tri's the same size. To do the splitting i used a Quadtree

[edited by - Marvin on July 16, 2002 3:22:30 AM]

##### Share on other sites
Hi Marvin!

Looks good...could you give me some specifics on how you did that?

Thx
Drag0n

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning..." -- Rich Cook

##### Share on other sites
this is what i use:

  void draw_sphere(double r, int n=16){   int i,j;   double theta1,theta2,theta3;   double ex, ey, ez;   if (r < 0)      r = -r;   if (n < 0)      n = -n;   if (n < 1 || r <= 0) {      glBegin(GL_POINTS);      glVertex3f(0,0,0);      glEnd();      return;   }   for (j=0;j<n/2;j++) {      theta1 = j * M_PI*2 / n - M_PI/2;      theta2 = (j + 1) * M_PI*2 / n - M_PI/2;      glBegin(GL_QUAD_STRIP);      for (i=0;i<=n;i++) {         theta3 = i * M_PI*2 / n;         ex = r * cos(theta2) * cos(theta3);         ey = r * sin(theta2);         ez = r * cos(theta2) * sin(theta3);         glNormal3f(ex/r,ey/r,ez/r);         glTexCoord2f(i/(double)n,2*(j+1)/(double)n);         glVertex3f(ex,ey,ez);         ex = r * cos(theta1) * cos(theta3);         ey = r * sin(theta1);         ez = r * cos(theta1) * sin(theta3);         glNormal3f(ex/r,ey/r,ez/r);         glTexCoord2f(i/(double)n,2*j/(double)n);         glVertex3f(ex,ey,ez);      }      glEnd();   }}

The first argument is the radius, second argument is how smooth the sphere should be(the higher the smoother). Make sure that M_PI is defined

#define M_PI 3.14159

This is modified code from someone on one of gamedev's forums I think.

[edited by - wolfman8k on July 17, 2002 1:10:39 PM]