OpenGL顶点数组球体出现问题

3
这是一个问题的图片: enter image description here 这是线框图: wireframe 从上面的图片中可以看出,我有一些奇怪的图形问题,不确定如何解决,我认为代码可能有问题,尽管其他人没有遇到过这个问题。
代码:
    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());

代码摘自(在Visual C++中使用OpenGL创建3D球体


如果我猜的话,也许你没有填写其中一个顶点点,所以它保持着未初始化的数据。旋转相机并查看有多少不良点以确认此问题。 - Michael Dorgan
其他人没有遇到过问题,所以不确定问题出在哪里:S 这是它在线框模式下的样子 http://img687.imageshack.us/img687/5849/globe2.png - Split
1
看起来像是一个 off-by-one 错误或者是驱动程序的漏洞。你确定你的 vector 是否为空? - Bartek Banachewicz
1个回答

4

索引将会超出顶点范围。

最后四个值在索引内为:

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

但是顶点只有288个。我猜其他人为什么可以工作的原因是glDrawElements可能在某些实现中会包装索引。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接