3

Heres a pic of the problem:

enter image description here

And here is the wireframe:

wireframe

As you can see from the pictures above, I have some sort of weird graphical issue and am not sure how to fix, I think somethings wrong with the code although noone else has had trouble with the code.

Code:

    unsigned int rings = 12, sectors = 24;

    float const R = 1./(float)(rings-1);
    float const S = 1./(float)(sectors-1);
    int r, s;

    vertices.resize(rings * sectors * 3);
    normals.resize(rings * sectors * 3);
    texcoords.resize(rings * sectors * 2);
    std::vector<GLfloat>::iterator v = vertices.begin();
    std::vector<GLfloat>::iterator n = normals.begin();
    std::vector<GLfloat>::iterator t = texcoords.begin();
    for(r = 0; r < rings; r++) for(s = 0; s < sectors; 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 * getR();
            *v++ = y * getR();
            *v++ = z * getR();

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

    indices.resize(rings * sectors * 4);
    std:vector<GLushort>::iterator i = indices.begin();
    for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
            *i++ = r * sectors + s;
            *i++ = r * sectors + (s+1);
            *i++ = (r+1) * sectors + (s+1);
            *i++ = (r+1) * sectors + s;
    }

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(3, GL_FLOAT, 0, vertices.data());
    glNormalPointer(GL_FLOAT, 0, normals.data());
    glTexCoordPointer(2, GL_FLOAT, 0, texcoords.data());
    glDrawElements(GL_QUADS, indices.size(), GL_UNSIGNED_SHORT, indices.data());

Code taken from (Creating a 3D sphere in Opengl using Visual C++)

Community
  • 1
  • 1
Split
  • 259
  • 2
  • 5
  • 11
  • If I had to guess, perhaps you are not filling in one of your vertex points so it remanins with uninitialized data. Rotate the camera around and see how many points are bad to confirm this. – Michael Dorgan Apr 10 '13 at 20:35
  • Other people haven't had problems with them so not sure what the problem is :S Heres how it looks in wireframe mode http://img687.imageshack.us/img687/5849/globe2.png – Split Apr 10 '13 at 22:27
  • 1
    It looks like either off-by-one error or a driver bug. Are you sure your `vector`s are empty? – Bartek Banachewicz Apr 11 '13 at 14:46

1 Answers1

4

indices will end up with indexes outside of vertices. The last four values in indices will be:

*i++ = 11 * 24 + 23 = 287;
*i++ = 11 * 24 + (23 + 1) = 288;
*i++ = (11 + 1) * 24 + (23 + 1) = 312;
*i++ = (11 + 1) * 24 + 23 = 311;

but vertices only contains 288 vertexes. I assume why it works for other people is that glDrawElements might wrap the indexes in some implementations.

the_jk
  • 539
  • 2
  • 10