#### Archived

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

# Coding a sphere?

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

## Recommended Posts

I''m coding a this game and I need to code a sphere in format: X,Y,Z X,Y,Z X,Y,Z (in textfile) but how to create this ball?

##### Share on other sites
Here''s the function that I use. It''s optimized for triangle strips. It generates normals and texture coordinates.

  // draws a sphere with tex coords// radius - radius// lat_step - number of horizontal sections// long_step - number of vertical sections// draw_type - how the sphere is to be renderedvoid DrawSphere(double radius, int lat_step, int long_step, bool lighting, int draw_type){ double DEGTORAD = 0.0174532925199432; glPushMatrix(); glBegin(draw_type); for (double i = 0; i < 180; i += 180/lat_step) { glTexCoord2d(0, i/180); glVertex3d(radius * sin(i * DEGTORAD), cos(i * DEGTORAD) * radius, 0.0f); glTexCoord2d(0, (i + 180/lat_step)/180); glVertex3d(radius * sin((i + 180/lat_step) * DEGTORAD), cos((i + 180/lat_step) * DEGTORAD) * radius, 0.0f); for (double j = 0; j < 360; j += 360/long_step) { if (lighting) glNormal3d(cos(DEGTORAD * (j + 360/(long_step * 2))) * sin((i + 180/(lat_step * 2)) * DEGTORAD), cos((i + 180/(lat_step * 2)) * DEGTORAD), -sin(DEGTORAD * (j + 360/(long_step * 2))) * sin((i + 180/(lat_step * 2)) * DEGTORAD)); glTexCoord2d((j + 360/long_step)/360, i/180); glVertex3d(cos(DEGTORAD * (j + 360/long_step)) * radius * sin(i * DEGTORAD), cos(i * DEGTORAD) * radius, -sin(DEGTORAD * (j + 360/long_step)) * radius * sin(i * DEGTORAD)); glTexCoord2d((j + 360/long_step)/360, (i + 180/lat_step)/180); glVertex3d(cos(DEGTORAD * (j + 360/long_step)) * radius * sin((i + 180/lat_step) * DEGTORAD), cos((i + 180/lat_step) * DEGTORAD) * radius, -sin(DEGTORAD * (j + 360/long_step)) * radius * sin((i + 180/lat_step) * DEGTORAD)); } } glEnd(); glPopMatrix();}

Thanks!

1. 1
Rutin
26
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002086
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!