ofSpherePrimitive has an opposite winding order

Hi, I’ve been playing with normals of 3d objects and I found out ofSpherePrimitive has a counter-clockwise winding order which is an opposite direction of all other 3d primitives.

I found this out not only by manually checking the order of indices but also have done a testing to prove this.
The test I’ve done was to copy all the vertices from original 3dPrimitives to ofMesh and then apply new normals to the mesh based on its vertices.
I used surface normal calculation found from https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal

And this is the screenshot of the test which shows 4 original 3d Primitives at the top and 4 meshes that use surface normals at the bottom.

And as you can see, the sphere at the bottom doesn’t reflect light because it has an opposite winding order so it’s back faced.

So I changed all normal winding order to opposite, then I got this result.

As you can see, only sphere has an opposite winding order.
I think the original ofSpherePrimitive has opposite order of indices and also opposite order of normals so it looks like it is working correctly, but it’s not.

Please find the attached source files below.

ofApp.h (945 Bytes)
ofApp.cpp (5.6 KB)

I found a way to fix this bug. (if it is a bug)

in ofMesh.cpp file at line 1631, you will find the below part

ofMesh ofMesh::sphere( float radius, int res, ofPrimitiveMode mode ) {

 //more codes here...

 //line 1631
if (mode == OF_PRIMITIVE_TRIANGLES) {

	int index1, index2, index3;

	for(float iy = 0; iy < res; iy++) {
		for(float ix = 0; ix < doubleRes; ix++) {

			// first tri //
			if(iy > 0) {
				index1 = (iy+0) * (nr) + (ix+0);
				index2 = (iy+0) * (nr) + (ix+1);
				index3 = (iy+1) * (nr) + (ix+0);

				mesh.addIndex(index1);
				mesh.addIndex(index3);
				mesh.addIndex(index2);
			}

			if(iy < res-1 ) {
				// second tri //
				index1 = (iy+0) * (nr) + (ix+1);
				index2 = (iy+1) * (nr) + (ix+1);
				index3 = (iy+1) * (nr) + (ix+0);

				mesh.addIndex(index1);
				mesh.addIndex(index3);
				mesh.addIndex(index2);

			}
		}
	}
}

}

Switch “mesh.addIndex(index1);” with “mesh.addIndex(index2);” so it becomes

				mesh.addIndex(index2);
				mesh.addIndex(index3);
				mesh.addIndex(index1);

There are two of this. switch them all.
And then it works correctly just like other primitives.

Can someone please confirm that this is a bug ?

yes this is a known issue, there’s an open bug report here: https://github.com/openframeworks/openFrameworks/issues/4626

if you can send a pull request referring that issue that would be really useful

1 Like

Thanks!

https://github.com/openframeworks/openFrameworks/pull/5414