我正在尝试用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;
}
}
}
知道我缺少什么吗?