# help me with built in function for spehere in openGL

Hello all,
pls tell me if openGL has built in function for drawing a sphere.
and also how to chanhe the surface color of that with 2 colors at a time??

openGL does not have any primitive object, they all have to be drawn with librarys like glu or glut.

glutSolidSphere or glutWireSphere but only one color can be use at a time.

with gluSphere you can only use one color at a time but you can apply a texture map with the two diffrent colors.

The other option would to make the sphere yourself and map the colors per triangle or quad. The are examples of building your own sphere’s.

Originally posted by prashantgp:
Hello all,
pls tell me if openGL has built in function for drawing a sphere.
and also how to chanhe the surface color of that with 2 colors at a time??

Hi !

If you want to do it yourself it’s pretty simple, you can also peek at the source code of the OpenGL sample implementation or the Mesa source code (www.sourceforge.net).

Mikael

Here is the algorhythm I’ve coded a view days ago for testing purposes… it’s slow and needs optimization, use display lists with that one:

void MDrawSphere(float size,float segs,float pps) {
float ss = M_PI / segs,ps = M_PI / (pps / 2);

for (float seg = 0;seg < M_PI;seg += ss)
for (float pol = -M_PI;pol < M_PI;pol += ps) {
glColor3f(fabs(cos(pol)),fabs(cos(seg)),0);
glVertex3f(cos(pol) * sin(seg) * size,cos(seg) * size,sin(pol) * sin(seg) * size);
glColor3f(fabs(cos(pol + ps)),fabs(cos(seg)),0);
glVertex3f(cos(pol + ps) * sin(seg) * size,cos(seg) * size,sin(pol + ps) * sin(seg) * size);
glColor3f(fabs(cos(pol + ps)),fabs(cos(seg + ss)),0);
glVertex3f(cos(pol + ps) * sin(seg + ss) * size,cos(seg + ss) * size,sin(pol + ps) * sin(seg + ss) * size);
glColor3f(fabs(cos(pol)),fabs(cos(seg + ss)),0);
glVertex3f(cos(pol) * sin(seg + ss) * size,cos(seg + ss) * size,sin(pol) * sin(seg + ss) * size);
}
glEnd();
}

You should remove/change the glColor3f() calls within the function to use your own coloring/texturing/materialing.

Here is the result of MDrawSphere(1,16,32) with smooth shading (rotated):

[This message has been edited by mm_freak (edited 11-06-2002).]

Hi !

If you just replace all the sin/cos calls with a single call at the top saving the result in local variables should speed it all up.

Mikael

I know, that was just a test and I’m not gonna optimize it. Normally I write such algorhythms in assembler (if they don’t need to be portable).

[This message has been edited by mm_freak (edited 11-08-2002).]