c++ - OpenGL Vertex Array Sphere C 的问题

标签 c++ opengl

这是问题的图片:

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());

代码取自(Creating a 3D sphere in Opengl using Visual C++)

最佳答案

索引将以顶点之外的索引结束。 索引中的最后四个值将是:

*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 可能会在某些实现中包装索引。

关于c++ - OpenGL Vertex Array Sphere C 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15934637/

相关文章:

android - 对于哪些 ABI,我可以使用 NDK v10e 构建 apk?

c++ - 客户端-服务器程序,使用 TCP 从客户端向服务器发送消息时出现问题,反之亦然

c++ - Unreal 使用什么样的反射?

javascript - WebGL 显示加载的没有矩阵的模型

java - 获取 float[] 作为 FloatBuffer 类

将三角形带的顶点转换为多边形的算法

c++ - 获取成员函数的内存地址?

c++ - 查找唯一分解的总数

c++ - 在特定时间淡入和淡出对象

java - OpenGL - 特定深度的像素颜色