# OpenGL Creating Sphere with GL_TRIANGLE_STRIP

## Recommended Posts

I would like to create a sphere with variable tesselation (in both horizontal and vertical aspect), this is my algorithm:

	const int amount = (sectorCount + 1) * (stackCount + 1);
M3DVector3f *verts = (M3DVector3f*)malloc(sizeof(float) * 3 * amount);
M3DVector4f *colors = (M3DVector4f*)malloc(sizeof(float) * 4 * amount);

float sectorStep = 2 * GL_PI / sectorCount;
float stackStep = GL_PI / stackCount;
float sectorAngle, stackAngle;

int vCount = 0;
for (int i = 0; i <= stackCount; ++i)
{
stackAngle = GL_PI / 2 - i * stackStep;        // starting from pi/2 to -pi/2
float xy = radius * cosf(stackAngle);             // r * cos(u)
float z = radius * sinf(stackAngle);              // r * sin(u)

// add (sectorCount+1) vertices per stack
// the first and last vertices have same position and normal, but different tex coords
for (int j = 0; j <= sectorCount; ++j)
{
sectorAngle = j * sectorStep;           // starting from 0 to 2pi

// vertex position (x, y, z)
float x = xy * cosf(sectorAngle);             // r * cos(u) * cos(v)
float y = xy * sinf(sectorAngle);             // r * cos(u) * sin(v)

m3dLoadVector3(verts[vCount], x, y, z);

m3dLoadVector4(colors[vCount], 0, 1, 0, 1);
vCount++;
}
}

This code works fine in terms of correct vertice coordinates.

Unfortunately the vertices aren't procuded in "correct order" so that GL_TRIANGLE_STRIP (or any other kind of triangle placement) produces a smooth sphere, it more looks like this (see attached images).

Unfortunately I also have no clue about how to change the algorithm to make GL_TRIANGLE_STRIP work. Any ideas?

##### Share on other sites

Well ........ It's not a sphere, but it does look kind of cool, like a stylized claw. Maybe you should use it as a logo

##### Share on other sites
Posted (edited)

Seems like a stray order of indices to me ?

Edited by Green_Baron

##### Share on other sites
Posted (edited)
1 hour ago, Gnollrunner said:

Well ........ It's not a sphere, but it does look kind of cool, like a stylized claw. Maybe you should use it as a logo

The vertices are places in the shape of a sphere

(as you can see in attached images but I used GL_POINTS instead of GL_TRIANGLE_STRIP so I don't get a closed surface).

Is there any other way to get a closed surface with the above algorithm and OpenGL?

Edited by IsItSharp

##### Share on other sites
2 hours ago, IsItSharp said:

Unfortunately I also have no clue about how to change the algorithm to make GL_TRIANGLE_STRIP work. Any ideas?

Does it work without triangle strip (I did not paid deep attention to your algo which look correct at first glance) ?

If you know the order in which you generate the vertices of your triangles, and you compare it to what the tri strip ordering expect. Are there any differences ? If there are few (ie due to going to the next parallel of the sphere), try to create some degenerated triangles between these problematic positions.

Finally, are there any reasons why you want to create strips ? From what I know, they are less and less used. I think that they now have more annoyance than good points.

##### Share on other sites
Posted (edited)

Sure.

You need the correct order in which the triangles are generated or generate them directly in the correct order.

The best way would be to fill an element buffer with the indices for the triangles, in drawing order. Try drawing triangles first. Am unable to imagine how to close a strip on a sphere, especially with the singularities at the poles.

I use a different algorithm to generate ellipsoids by subdividing them recursively from an initial tetrahedron and scaling them afterwards. That generates the indices on the fly and avoids the singularities at the poles.

Edited by Green_Baron

##### Share on other sites
Posted (edited)
57 minutes ago, IsItSharp said:

The vertices are places in the shape of a sphere

(as you can see in attached images but I used GL_POINTS instead of GL_TRIANGLE_STRIP ﻿so I don't get a closed surface).

Is there any other way to get a closed surface with the above algorithm and OpenGL?

Well I'm not really an OpenGL programmer ..... but first, I would think you would have repeat each strip twice, except for the top and bottom, because you have to do a zig-zag between them. Also you don't really want to use strips for the poles, but fans instead, because you only have a single point at the poles. I personally don't use strips at all, but prefer to use vertexes with index buffers. That's another option........

Finally a third option is to sub-divide an icosahedron.  This is my goto sphere! It gives you far more even triangulation but it's trickier to generate algorithmically. What do you want to do with it?  BTW if I remember, DirectX9 had a function that just built the sphere for you (the one you are trying to build). I think it was D3DXCreateSphere or some such.

Edited by Gnollrunner

##### Share on other sites

Each "stack" of faces connects an upper ring to a lower ring, so as you go around the circle of sectors, alternately generate a vertex from the upper ring and the lower ring.  This should at least correct the individual stacks; connecting them is trickier.

##### Share on other sites
Posted (edited)
3 hours ago, Gnollrunner said:

I would think you would have repeat each strip twice, except for the top and bottom, because you have to do a zig﻿-zag between them.

Strips don't (or should not) create zigzags that require another strip for filling in.

3 hours ago, Gnollrunner said:

I personally don't use strips at all, but prefer to use vertexes with index buffers.

You can use strips with indices.

Edited by fleabay

##### Share on other sites
Posted (edited)
39 minutes ago, fleabay said:

Strips don't (or should not) create zigzags that require another strip for filling in.

I'm not sure what you mean by "that require another strip for filling in", but generally in DirectX you create some sort of zig-zag, and the extra edges get automatically created to make triangles.  Looking it up for OpenGL it seems like they do the exact same thing.

To create a sphere with triangle strips, I would go around pairs of latitudinal slices and zig-zag between them, except for the poles where you would use fans.  But perhaps you know of a better way to traverse the points. What would you suggest?

Edit: I guess you could also go vertically and do the same thing, but you would still be doing a zig-zag.

Edited by Gnollrunner

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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 56
• 17
• 27