View Full Version : help me with built in function for spehere in openGL

prashantgp

11-05-2002, 11:50 PM

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??

nexusone

11-06-2002, 03:57 AM

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??

mikael_aronsson

11-06-2002, 08:28 AM

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

mm_freak

11-06-2002, 03:31 PM

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);

glBegin(GL_QUADS);

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.

MDrawSphere(<size>,<segments>,<quads-per-segment> );

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

http://freak32.dyndns.org/spheretest.png

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

mikael_aronsson

11-06-2002, 10:54 PM

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

mm_freak

11-07-2002, 07:10 AM

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).]

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.