繁体   English   中英

GL_TRIANGLES而不是GL_QUADS

[英]GL_TRIANGLES instead of GL_QUADS

我想修改用于生成3D球体的代码,因此它使用三角形而不是四边形进行绘制。 问题是,像往常一样,我遇到了一些错误-“向量迭代器不可递增”。 它出什么问题了?

SolidSphere(float radius, unsigned int rings, unsigned int sectors)
{
    float const R = 1.0f / (float)(rings-1);
    float const S = 1.0f / (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 x = sinf(M_PI * r * R) * cosf(2 * M_PI * s * S);
            float const y = sinf(-M_PI_2 + M_PI * r * R );                
            float const z = sinf(2.0f * M_PI * s * S) * sinf( M_PI * r * R );

            *t++ = s*S;
            *t++ = r*R;

            *v++ = x * radius;
            *v++ = y * radius;
            *v++ = z * radius;

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

    indices.resize(rings * sectors * 4);
    std::vector<GLushort>::iterator i = indices.begin();
    for(r = 0; r < rings-1; r++) for(s = 0; s < sectors-1; s++) {
        /* triangles -- not working!
        *i++ = r * sectors + s;
        *i++ = (r + 1) * sectors + (s + 1);
        *i++ = r * sectors + (s + 1);

        *i++ = r * sectors + s;
        *i++ = (r + 1) * sectors + s;
        *i++ = (r + 1) * sectors + (s + 1); */

        /* quads */
        *i++ = r * sectors + s;
        *i++ = r * sectors + (s+1);
        *i++ = (r+1) * sectors + (s+1);
        *i++ = (r+1) * sectors + s;

    }
}

貌似循环的三角形生成器版本做(sectors-1)(rings-1)迭代,每一个增加i的六倍,但您调整矢量i通过刚才迭代(rings * sectors * 4) ,对于循环的四发电机版本就足够了。


假设三角形版本可以,则此调整应将其修复:

indices.resize(rings * sectors * 6)

当您在没有喝足够咖啡的情况下编码时,通常会发生这种疏忽。 还是太多 (由于实际分配的空间超出了迭代所需的空间,该图显示了代码实际失败时的双曲线边界(将环和扇区号映射到x和y))。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM