diff options
| -rw-r--r-- | MM_2015_Base_Project.pro | 6 | ||||
| -rw-r--r-- | src/glsphere.cpp | 52 | ||||
| -rw-r--r-- | src/glsphere.h | 29 |
3 files changed, 85 insertions, 2 deletions
diff --git a/MM_2015_Base_Project.pro b/MM_2015_Base_Project.pro index e7215bc..8a4cee2 100644 --- a/MM_2015_Base_Project.pro +++ b/MM_2015_Base_Project.pro @@ -11,7 +11,8 @@ SOURCES += \ src/shaderdebugger.cpp \ src/mmscene.cpp \ src/glbody.cpp \ - src/glcube.cpp + src/glcube.cpp \ + src/glsphere.cpp HEADERS += \ src/glcolorrgba.h \ @@ -22,7 +23,8 @@ HEADERS += \ src/shaderdebugger.h \ src/mmscene.h \ src/glbody.h \ - src/glcube.h + src/glcube.h \ + src/glsphere.h RESOURCES += qml.qrc \ shaders.qrc diff --git a/src/glsphere.cpp b/src/glsphere.cpp new file mode 100644 index 0000000..d77ef95 --- /dev/null +++ b/src/glsphere.cpp @@ -0,0 +1,52 @@ +#include "glsphere.h" + +GLSphere::GLSphere(double radius, int stacks, int slices) + : GLBody(radius) +{ + m_stacks = stacks; + m_slices = slices; +} + +void GLSphere::makeSurface(QVector<GLPoint> *pointContainer, + QVector<GLshort> *indexContainer) +{ + GLBody::makeSurface(pointContainer, indexContainer); + + QVector3D n0 = v_XYZ; //dummy normal + QVector3D t0 = QVector3D(0.0, 0.0, 0.0); //dummy texture + + QVector3D northpol = v_Y * m_radius; + QVector3D southpol = -v_Y * m_radius; + + m_firstPoint = m_points->size(); + + for (int slice = 1; slice < m_slices; slice += 2) + { + m_points->append(GLPoint(northpol, n0, t0, GLColorRgba::clMagenta)); + + for (int stack = 1; stack < m_stacks; stack++) + { + m_points->append(GLPoint(calculatePoint(2 * M_PI * slice / m_slices, + stack * M_PI / m_stacks), n0, t0, + m_color)); + + m_points->append(GLPoint(calculatePoint(2 * M_PI * (slice - 1) / m_slices, + stack * M_PI / m_stacks), n0, t0, + m_color)); + + } + + m_points->append(GLPoint(southpol, n0, t0, GLColorRgba::clMagenta)); + } + + m_nextPoint = m_points->size(); +} + +QVector3D GLSphere::calculatePoint(double sliceRotation, double stackRotation) +{ + double x = m_radius * sin(stackRotation) * sin(sliceRotation); + double y = m_radius * cos(stackRotation); + double z = m_radius * sin(stackRotation) * cos(sliceRotation); + + return QVector3D(x, y, z); +} diff --git a/src/glsphere.h b/src/glsphere.h new file mode 100644 index 0000000..9aae032 --- /dev/null +++ b/src/glsphere.h @@ -0,0 +1,29 @@ +#ifndef GLSPHERE_H +#define GLSPHERE_H + +#include <cmath> + +#include "glbody.h" + + +class GLSphere : public GLBody +{ +public: + GLSphere(double radius = 1.0, int stacks = 10, int slices = 10); + virtual void makeSurface(QVector<GLPoint> *pointContainer, + QVector<GLshort> *indexContainer); + +private: + /** + * @brief calculatePoint + * @param zRotation + * @param yRotation + * @return + */ + QVector3D calculatePoint(double sliceRotation, double stackRotation); + + int m_stacks; + int m_slices; +}; + +#endif // GLSPHERE_H |
