Sign in to follow this  
voldemort62442

Sphere out of Triangle Strips (Please Help)

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Well you are going to need to know trig for sure to do this. If you don't you should study up on it. Or just load a model mesh of a sphere that you either download for free or make yourself in your choice of modeler tools. Or as someone suggested use gluSphere. What is the reason why none of these suggested ideas will not work, if I may ask.

Share this post


Link to post
Share on other sites
If he wants to deform the sphere he will have to have a set of vertices used to render from. Using this trig function supplies that. However, this is pretty much the worst possible way to render any primitive in OpenGL. Simple, flexible, but slow as can be.

There are a few ways to resolve this issue while retaining the flexibility of the sphere.

Assuming you don't want to deform, you can load the vertices on initialization into an array of floats. Then using glGenLists(), glNewList(), glEndList() and glCallList() you may utilize drawing that (static) array. You may use a multitude of functions in a display list, and even call other display lists. However, you are bound to a few rules. You must not create a new display list in a display list, you must use static memory, and you may not call openGL functions that read from the hard disk. Display lists are an easy and fast solution to optimizing static geometry.

Alternatives are vertex arrays and vertex buffer objects.

Vertex arrays are a bit more complicated than display lists, but still fairly simple to implement. For dynamic access to the vertices, vertex arrays are a good choice.

The best optimization, hands down, is to use VBO's. It actually loads all of the primitive data onto the GPU, making it absurdly fast to render. It is possible to load it as either static or dynamic (draw/stream) data, but you typically want to just leave it as static (draw).

There is a trick, have faith in the OGL gods! If you want to have deformation on (nearly) any static object, even in display lists, you may use *gasp* shaders! Using gl_Vertex in shaders, you are able to mutate the vertices on the GPU instead of on software.

In essence, by doing this you load the geometry AND the deformation algorithm onto the GPU, the only thing you're sending through the bus is "HEY YOU, DRAW!" Do this, and you should be able to render a high-poly geometric object with complex mutations essentially for free.

Enjoy!

PS. It's pretty late, so if there are any discrepancies with what I've said, feel free to call me on it. I am pretty tired so it's entirely possible I've made a semantic error somewhere.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this