# OpenGL 'Holes' with Procedurally Generated Sphere for OpenGL

## Recommended Posts

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:

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;

*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.

##### Share on other sites

the picture is not present so i can just guess.if you are culling front face then the end of your rings are drawn as back face so they're not shown

##### Share on other sites

Start by breaking out of the loop early, first after the first iteration, then after the second, and so on, for both the sectors and the ring.

That should go from no polygons to a single triangle at the top of the sphere, and then if that looks OK then go to a full ring, and when that looks OK go to 2 rings.

It should make it much easier to isolate the problem, than trying to see why the full sphere looks like it does.

In this case I'm reasonably sure your problem is in building the indices. Are you drawing with GL_TRIANGLES?

If so you need 6 indices per quad, as it's built from 2 separate triangles. From your indices you need to draw a separate quad or triangle-strip for each sector.

If it still looks strange, do glDisable(GL_CULL_FACE) and see if it fixes it, which would mean face-winding is inverted.

##### Share on other sites

The problem definitely is the indices (all the points seem to be in the right place)

It looks like you are generating quads, but are drawing it as triangles, as Erik suspects.

Edited by Olof Hedman

##### Share on other sites

After executing your code I think the problem is indeed in drawing 'GL_QUADS' geometry with 'GL_TRIANGLES'. Take a look at the following screenshots of your geometry.

[attachment=23463:Triangles_BackfaceCulling.jpg] Fig.1. Drawing with GL_TRIANGLES and GL_CULL_FACE enabled.

[attachment=23464:Triangles.jpg] Fig.2. Drawing with GL_TRIANGLES and GL_CULL_FACE disabled.

##### Share on other sites

Start by breaking out of the loop early, first after the first iteration, then after the second, and so on, for both the sectors and the ring.
That should go from no polygons to a single triangle at the top of the sphere, and then if that looks OK then go to a full ring, and when that looks OK go to 2 rings.
It should make it much easier to isolate the problem, than trying to see why the full sphere looks like it does.

In this case I'm reasonably sure your problem is in building the indices. Are you drawing with GL_TRIANGLES?
If so you need 6 indices per quad, as it's built from 2 separate triangles. From your indices you need to draw a separate quad or triangle-strip for each sector.
If it still looks strange, do glDisable(GL_CULL_FACE) and see if it fixes it, which would mean face-winding is inverted.

Changing to GL_QUADS per @Rootus's suggestion 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?

##### Share on other sites

After executing your code I think the problem is indeed in drawing 'GL_QUADS' geometry with 'GL_TRIANGLES'. Take a look at the following screenshots of your geometry.

Triangles_BackfaceCulling.jpg Fig.1. Drawing with GL_TRIANGLES and GL_CULL_FACE enabled.

Triangles.jpg Fig.2. Drawing with GL_TRIANGLES and GL_CULL_FACE disabled.

Thank you for trying this out!

Hope to get this fully working on all systems like AMD.

So far I only see stuff on Intel...

##### Share on other sites

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

If you're using 'core' profile, GL_QUADS is deprecated. That might be why AMD driver refuses to draw anything. The solution would be to add the indices for the missing triangle for each quad you're generating (and draw with GL_TRIANGLES).

Edited by TheChubu

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627658
• Total Posts
2978474
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33