#include "glsphere.h" GLSphere::GLSphere(double radius, int stacks, int slices) : GLBody(radius) { m_stacks = stacks * 2 / 2; m_slices = slices * 2 / 2; } void GLSphere::makeSurface(QVector *pointContainer, QVector *indexContainer) { GLBody::makeSurface(pointContainer, indexContainer); QVector3D t0 = QVector3D(0.0, 0.0, 0.0); //dummy texture QVector3D northpol = v_Y; QVector3D southpol = -v_Y; QVector3D tmpPoint; m_firstPoint = m_points->size(); m_points->append(GLPoint(m_radius * northpol, northpol, t0, m_color)); for (int slice = 0; slice < m_slices; slice++) { for (int stack = 1; stack < m_stacks; stack++) { tmpPoint = calculatePoint(2 * M_PI * slice / m_slices, stack * M_PI / m_stacks); m_points->append(GLPoint(m_radius * tmpPoint, tmpPoint, t0, m_color)); } } m_points->append(GLPoint(m_radius * southpol, southpol, t0, m_color)); m_nextPoint = m_points->size(); m_firstIndex = m_indices->size(); for (int slice = 1; slice < m_slices - 1; slice += 2) { m_indices->append(m_firstPoint); for (int stack = 0; stack < m_stacks - 1; stack++) { m_indices->append((m_firstPoint + 1) + stack + ((m_stacks - 1) * (slice - 1))); m_indices->append((m_firstPoint + 1) + stack + ((m_stacks - 1) * slice)); } m_indices->append(m_nextPoint - 1); for (int stack = m_stacks - 2; stack >= 0; stack--) { m_indices->append((m_firstPoint + 1) + stack + ((m_stacks - 1) * (slice + 1))); m_indices->append((m_firstPoint + 1) + stack + ((m_stacks - 1) * slice)); } m_indices->append(m_firstPoint); } m_nextIndex = m_indices->size(); } QVector3D GLSphere::calculatePoint(double sliceRotation, double stackRotation) { double x = sin(stackRotation) * sin(sliceRotation); double y = cos(stackRotation); double z = sin(stackRotation) * cos(sliceRotation); return QVector3D(x, y, z); }