Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: 'Holes' with Procedurally Generated Sphere for OpenGL

  1. #1
    Intern Contributor
    Join Date
    Apr 2014
    Posts
    67

    'Holes' with Procedurally Generated Sphere for OpenGL

    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:

    Code auto:0:
    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.

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    495
    Quote Originally Posted by tmason View Post
    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.

  3. #3
    Intern Contributor
    Join Date
    Apr 2014
    Posts
    67
    Quote Originally Posted by GClements View Post
    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?

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    787
    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.

  5. #5
    Intern Contributor
    Join Date
    Apr 2014
    Posts
    67
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •