Creating Sphere with GL_TRIANGLE_STRIP

Started by
11 comments, last by Gnollrunner 4 years, 11 months ago

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?

sphere.png

Advertisement

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 :D

Seems like a stray order of indices to me ?

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 :D

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?

sphere.png

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.

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.

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?

sphere.png

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.

SphereWorld.jpg

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.

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. 

vbo.png

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.

This topic is closed to new replies.

Advertisement