c++ - opengl indexed drawing issue -
i'm trying render sphere opengl (3.0). algorithm below computes vertices , according indices. in works quite well, however, there seem glitch in matrix, cause ugly cone inside sphere.
vertices.resize(rings * segments * 3); colors.resize(rings * segments * 3); indices.resize(6 * rings * segments); auto v = vertices.begin(); auto c = colors.begin(); auto = indices.begin(); auto dtheta = m_pi / (f32)rings; auto dphi = 2 * m_pi / (f32)segments; ( u32 ring = 0; ring < rings; ++ring ) { auto r0 = radius * sinf(ring * dtheta); auto y0 = radius * cosf(ring * dtheta); ( 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; } } }
any idea i'm missing?
*i++ = ( (ring+1) * segments ) + segment + 1; *i++ = ( (ring+1) * segments ) + segment + 1; *i++ = ( (ring ) * segments ) + segment + 1;
what index compute if segment
equal segments - 1
? yes, index next segment.
and if there is no next segment? index outside of list of points.
your segment increment needs wrap around 0:
*i++ = ( (ring+1) * segments ) + (segment + 1) % segments; *i++ = ( (ring+1) * segments ) + (segment + 1) % segments; *i++ = ( (ring ) * segments ) + (segment + 1) % segments;
also, consider happens if ring
equal rings - 1
. same problem, need different solution. if
statement wrong. should be:
if((ring + 1) < rings)
Comments
Post a Comment