PDA

View Full Version : 'Holes' with Procedurally Generated Sphere for OpenGL



tmason
09-01-2014, 06:23 AM
Hello,

Hopefully this is a simple question; I have the following code that I am using to generate a sphere object for OpenGL. The problem is, as the picture will show, is that there are "holes" in the spheres. I placed a few boxes in the scene to show that overall my buffers/shaders seem to be OK and I believe the problem may be the indices.

Anyway, here is the picture:

http://s30.postimg.org/410igmj8h/Terrible_Sphere.png

And here is how I am generating the sphere:


float ShapeRadius = 3.0f;

GLuint NumberOfRings = 50;
GLuint NumberOfSectors = 50;

float const R = 1. / (GLfloat)(NumberOfRings - 1);
float const S = 1. / (GLfloat)(NumberOfSectors - 1);

int r, s;

Temp_Vertices = new std::vector<GLfloat>;
Temp_Normals = new std::vector<GLfloat>;
Temp_TextCoords = new std::vector<GLfloat>;

Temp_Indices = new std::vector<GLuint>;

Temp_Vertices->resize(NumberOfRings * NumberOfSectors * 3);
Temp_Normals->resize(NumberOfRings * NumberOfSectors * 3);
Temp_TextCoords->resize(NumberOfRings * NumberOfSectors * 2);

std::vector<GLfloat>::iterator v = Temp_Vertices->begin();
std::vector<GLfloat>::iterator n = Temp_Normals->begin();
std::vector<GLfloat>::iterator t = Temp_TextCoords->begin();

for (r = 0; r < NumberOfRings; r++) for (s = 0; s < NumberOfSectors; s++) {

float const y = sin(-M_PI_2 + M_PI * r * R);
float const x = cos(2 * M_PI * s * S) * sin(M_PI * r * R);
float const z = sin(2 * M_PI * s * S) * sin(M_PI * r * R);

*t++ = s*S;
*t++ = r*R;

*v++ = x * ShapeRadius;
*v++ = y * ShapeRadius;
*v++ = z * ShapeRadius;

*n++ = x;
*n++ = y;
*n++ = z;
}

Temp_Indices->resize(NumberOfRings * NumberOfSectors * 4);

std::vector<GLuint>::iterator i = Temp_Indices->begin();

for (r = 0; r < NumberOfRings; r++) for (s = 0; s < NumberOfSectors; s++) {
*i++ = r * NumberOfSectors + s;
*i++ = r * NumberOfSectors + (s + 1);
*i++ = (r + 1) * NumberOfSectors + (s + 1);
*i++ = (r + 1) * NumberOfSectors + s;
}
Vertices = Temp_Vertices->data();
NumOfVertices = Temp_Vertices->size();

Indices = Temp_Indices->data();
NumOfIndices = Temp_Indices->size();

Normals = Temp_Normals->data();
NumOfNormals = Temp_Normals->size();

TextureCoordinates = Temp_TextCoords->data();
NumOfTextureCoordinates = Temp_TextCoords->size();

What can I do to get rid of the holes?

Thank you.

GClements
09-01-2014, 07:39 PM
I believe the problem may be the indices

Probably.

In particular, (s + 1) should probably be (s + 1) % NumberOfSectors, so that the last vertex on each ring is connected to the first vertex on that ring, rather than to the first vertex on the subsequent ring.

Also, you should have (NumberOfRings+1)*NumberOfSectors vertices for NumberOfRings*NumberOfSectors quads (assuming that the poles are actually rings of coincident vertices). This is because the topology "wraps" in longitude but not in latitude.

tmason
09-03-2014, 01:42 PM
Probably.

In particular, (s + 1) should probably be (s + 1) % NumberOfSectors, so that the last vertex on each ring is connected to the first vertex on that ring, rather than to the first vertex on the subsequent ring.

Also, you should have (NumberOfRings+1)*NumberOfSectors vertices for NumberOfRings*NumberOfSectors quads (assuming that the poles are actually rings of coincident vertices). This is because the topology "wraps" in longitude but not in latitude.

This and changing my drawing to GL_QUADS worked! Thanks.

One problem though; on Intel-based systems I get the sphere with no problems but on AMD based graphics cards nothing shows up.

I haven't tried the code on nVidia based systems.

Any reason why this may be?

carsten neumann
09-03-2014, 02:28 PM
Are you using the core profile? Quads are not a supported primitive type in core and AMD is (was?) known to be fairly strict about adherence to the spec while NV lets you get away with pretty much anything ;) If that's the problem you should get a GL error though.

tmason
09-03-2014, 02:37 PM
That would make sense; I am using a core profile.

This sucks, just when I got a darn sphere working I now need to change it up.

dam.