0

我正在尝试用opengl(3.0)渲染一个球体。下面的算法计算顶点和相应的索引。总而言之,它工作得很好,但是,矩阵中似乎有一个小故障,因为我在球体内得到了一个难看的锥体。

在此处输入图像描述

vertices.resize(rings * segments * 3);
colors.resize(rings * segments * 3);
indices.resize(6 * rings * segments);

auto v = vertices.begin();
auto c = colors.begin();
auto i = indices.begin();

auto dTheta = M_PI / (f32)rings;
auto dPhi   = 2 * M_PI / (f32)segments;

for ( u32 ring = 0; ring < rings; ++ring ) {
    auto r0 = radius * sinf(ring * dTheta);
    auto y0 = radius * cosf(ring * dTheta);
    for ( u32 segment = 0; segment < segments; ++segment ) {
        auto x0 = r0 * sinf(segment * dPhi);
        auto z0 = r0 * cosf(segment * dPhi);

        *v++ = x0;  *c++ = color.r;
        *v++ = y0;  *c++ = color.g;
        *v++ = z0;  *c++ = color.b;

        if (ring < rings) {
            *i++ = ( (ring  ) * segments ) + segment;
            *i++ = ( (ring+1) * segments ) + segment;
            *i++ = ( (ring+1) * segments ) + segment + 1;
            *i++ = ( (ring+1) * segments ) + segment + 1;
            *i++ = ( (ring  ) * segments ) + segment + 1;
            *i++ = ( (ring  ) * segments ) + segment;
        }
    }
}

知道我缺少什么吗?

4

1 回答 1

3
*i++ = ( (ring+1) * segments ) + segment + 1;
*i++ = ( (ring+1) * segments ) + segment + 1;
*i++ = ( (ring  ) * segments ) + segment + 1;

segment如果等于 ,你计算什么指数segments - 1?是的,您会从一段获得索引。

如果没有一段?然后,您会在点列表之外获得一个索引。

您的段增量需要环绕到 0:

*i++ = ( (ring+1) * segments ) + (segment + 1) % segments;
*i++ = ( (ring+1) * segments ) + (segment + 1) % segments;
*i++ = ( (ring  ) * segments ) + (segment + 1) % segments;

另外,考虑如果ring等于会发生什么rings - 1。同样的问题,但您需要不同的解决方案。你的if说法是错误的。它应该是:

if((ring + 1) < rings)
于 2013-05-04T09:17:03.220 回答