Sphere out of Triangle Strips (Please Help)

Started by
10 comments, last by FSDev 15 years, 10 months ago
I know this makes a circle glBegin(GL_POLYGON); for(int i=0; i<360; i++) glVertex2d((radius*cos((2.0*M_PI/360)*i)), (radius*sin((2.0*M_PI/360)*i))); glEnd(); so how do i turn it into a sphere using Triangle Strips I know it would be something like this but i need help finishing it glBegin(GL_POLYGON); for(int i=0; i<360; i++) glVertex3d((radius*cos((2.0*M_PI/360)*i)), (radius*sin((2.0*M_PI/360)*i))), some sort of z axis coordinates.); glVertex3d(?, ?, ?); glEnd(); Please i really need to understand this and learn how to do it Please Help thanks thanks for the help Tenac parts of the code made sense, but other parts were like greek to me [Edited by - voldemort62442 on May 12, 2008 10:58:40 PM]
Advertisement
on the top of my head i would say you want to divide the sphere into number of vertical slices and horizontal slices. And you can use quads to draw most of the slices but at the poles of the sphere you might need to do triangle strip so that it converge to a point better. Use gluSphere or something like that to draw a sphere in wireframe, that should give you an idea.
See i already know about glutWireSphere/GlutSolidSphere but i need to know how to make glutSolidSphere from scratch

i.e make the object using glBegin.....glEnd
Loading a sphere from a 3D file won't work for you? That would be my best bet even though it isn't what you want to do. It can be scaled with glScale and moved around with glTranslatef of course.

I did find this googling however even though it's in a different programming language. It's really confusing though lol http://www.codeproject.com/KB/WPF/XamlUVSphere.aspx
I wish i could just load it from a 3d file but i really need to have it coded out myself. I have been looking online for a while, but like the one below they all seem pretty complicated i did however find one that works, but it renders really slow and i am not sure why

double user_theta = 0;
double user_height = 0;


void drawSphere(double r, int lats, int longs) {
int i, j;
for(i = 0; i <= 1000; i++) {
double lat0 = M_PI * (-0.5 + (double) (i - 1) / 1000);
double z0 = sin(lat0);
double zr0 = cos(lat0);

double lat1 = M_PI * (-0.5 + (double) i / lats);
double z1 = sin(lat1);
double zr1 = cos(lat1);

glBegin(GL_QUAD_STRIP);
for(j = 0; j <= 1000; j++) {
double lng = 2 * M_PI * (double) (j - 1) / 1000;
double x = cos(lng);
double y = sin(lng);

glNormal3f(x * zr0, y * zr0, z0);
glVertex3f(x * zr0, y * zr0, z0);
glNormal3f(x * zr1, y * zr1, z1);
glVertex3f(x * zr1, y * zr1, z1);
}
glEnd();
}
}

The code sort of makes sense if anyone knows how to make this a lot faster it would also be appreciated.

This code makes a sphere but it runs so slow that it is almost useless if you ran this in a program you would see what i mean.

At first glance, I see this: for(i = 0; i <= 1000; i++) {
1000, yikes, what happens if you lower that number? Is that number the LOD of the sphere or what?
For some reasons, the implementation of QUAD_STRIP in OpenGL is unnecessarily slow. Therefore, you can speed up your program by quite a lot by using GL_TRIANGLES instead of QUAD_STRIP.

And, you can save all the calls to sin and cos function by pre-calculate a list of values. This should save a little more time.

Lastly, please make sure you have set your compiler to release mode. It is meaningless to discuss performance using debug mode.
You're drawing 2 million vertices in immediate mode. Of course it's going to be slow. Lower the constant 1000, like Tenac suggested (not in the for loop only, but at it's every occurence). You should make a constant out of it and experiment with different values.
See i had it to 10, but i needed a very high quality circle so i increased the number to 1000 Even at 4 it ran unbelievably slow. And i previously set it as a constant and it still ran slow, but thanks ma_hty for suggesting triangle strips as an alternative to my sphere
If anyone has any more suggestions i would love to hear it

This topic is closed to new replies.

Advertisement