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.

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.