PDA

View Full Version : Spheres



12-07-2004, 04:29 PM
I was wondering if there was an easy way to display spheres in openGL.

IE, for triangles we just go
glBegin(GL_TRIANGLES)
...

is there a way to do this with spheres?

dorbie
12-07-2004, 05:13 PM
You could use GLUT to draw a sphere but basically with OpenGL you have to draw a sphere using a bunch of triangles. That's what GLUT does. With enough triangles you stop noticing the triangles and only see the sphere. Smooth shading helps a lot with this.

12-07-2004, 05:25 PM
Do you have any example code somewhere I can see?

Thanks for the help.

Aeluned
12-08-2004, 06:30 AM
google "gluSphere"

...or if you prefer here's some code that will generate the points for a sphere rendered as a QUAD_STRIP



int i,j;
double dRho1 = 0;
double dRho2 = 0;
double dPhi = -PI/2.;
Vert3D e,p;
double SpherePoints[iNUM_GLOBE_PTS+1][iNUM_GLOBE_PTS/2][6];
double TexPoints[iNUM_GLOBE_PTS+1][iNUM_GLOBE_PTS/2][4];

double dTexCoordX,dTexCoordY;
dTexCoordX = m_pPrimitive->m_dTexCoordX;
dTexCoordY = m_pPrimitive->m_dTexCoordY;
for (j=0;j<iNUM_GLOBE_PTS/2;j++)
{
dRho2 += PI / ((double)iNUM_GLOBE_PTS/2);
for (i=0;i<=iNUM_GLOBE_PTS;i++)
{
//e is the surface normal (in case you need it).
e.x = sin(dRho2)*cos(dPhi);
e.y = cos(dRho2);
e.z = sin(dRho2)*sin(dPhi);

p.x = dRadius * e.x;
p.y = dRadius * e.y;
p.z = dRadius * e.z;


TexPoints[i][j][0] = dTexCoordX*(i/(double)iNUM_GLOBE_PTS);
TexPoints[i][j][1] = dTexCoordY*(2*(j+1)/(double)iNUM_GLOBE_PTS);

//Lower Ring....

SpherePoints[i][j][0] = p.x;
SpherePoints[i][j][1] = p.y;
SpherePoints[i][j][2] = p.z;

e.x = sin(dRho1)*cos(dPhi);
e.y = cos(dRho1);
e.z = sin(dRho1)*sin(dPhi);

p.x = dRadius * e.x;
p.y = dRadius * e.y;
p.z = dRadius * e.z;

TexPoints[i][j][2] = dTexCoordX*(i/(double)iNUM_GLOBE_PTS);
TexPoints[i][j][3] = dTexCoordY*(2*j/(double)iNUM_GLOBE_PTS);

//Upper Ring
SpherePoints[i][j][3] = p.x;
SpherePoints[i][j][4] = p.y;
SpherePoints[i][j][5] = p.z;

dPhi -= 2*PI/(double)iNUM_GLOBE_PTS;
}
pGL->End();
dPhi = -PI/2.;
dRho1 += PI / ((double)iNUM_GLOBE_PTS/2);
}

zukko
12-08-2004, 07:22 PM
use quadratics.



GLUquadricObj *quadratic;

// on your initialization function:
quadratic = gluNewQuadric();
gluQuadricNormals(quadratic,GLU_SMOOTH);
gluQuadricTexture(quadratic,GL_TRUE);

//on your rendering function:
gluSphere(quadratic,radius,subdivisions,subdivisio ns);

//on your deinitialization function:
gluDeleteQuadric(quadratic);

Silkut
12-09-2004, 01:43 AM
yes, use quadratic with GLU