summaryrefslogtreecommitdiffstats
path: root/src/glsphere.cpp
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2015-11-23 20:10:12 +0100
committerStefan Suhren <suhren.stefan@fh-swf.de>2015-11-23 20:10:12 +0100
commit709e2363edcebdcf5d0185db77dc2944df7004dd (patch)
treed0c9863ca29ad5bc981ea867d2ef13594176e43c /src/glsphere.cpp
parent781b2563ea559243de28ebc357b1a3f0be1e9cb2 (diff)
downloadMultimedia-709e2363edcebdcf5d0185db77dc2944df7004dd.tar.gz
Multimedia-709e2363edcebdcf5d0185db77dc2944df7004dd.zip
Add caching to GLSphere
Diffstat (limited to 'src/glsphere.cpp')
-rw-r--r--src/glsphere.cpp60
1 files changed, 44 insertions, 16 deletions
diff --git a/src/glsphere.cpp b/src/glsphere.cpp
index 56144f1..77cb7f7 100644
--- a/src/glsphere.cpp
+++ b/src/glsphere.cpp
@@ -3,8 +3,9 @@
GLSphere::GLSphere(double radius, int stacks, int slices)
: GLBody(radius)
{
- m_stacks = stacks;
- m_slices = slices;
+ // Force an even number
+ m_stacks = stacks / 2 * 2;
+ m_slices = slices / 2 * 2;
}
void GLSphere::makeSurface(QVector<GLPoint> *pointContainer,
@@ -17,35 +18,39 @@ void GLSphere::makeSurface(QVector<GLPoint> *pointContainer,
QVector3D northpol = v_Y;
QVector3D southpol = -v_Y;
- QVector3D point1;
- QVector3D point2;
+ QVector<QVector3D> firstPointRow = calculateRow(0);
+
+ QVector<QVector3D> sliceA = firstPointRow;
+ QVector<QVector3D> sliceB;
m_firstPoint = m_points->size();
for (int slice = 1; slice < m_slices; slice += 2)
{
+ sliceB = calculateRow(slice);
+
m_points->append(GLPoint(m_radius * northpol, northpol, t0, m_color));
- for (int stack = 1; stack < m_stacks; stack++)
+ for (int i = 0; i < m_stacks - 1; i++)
{
- point1 = calculatePoint(2 * M_PI * slice / m_slices, stack * M_PI / m_stacks);
- m_points->append(GLPoint(m_radius * point1, point1, t0, m_color));
+ m_points->append(GLPoint(m_radius * sliceA[i], sliceA[i],
+ t0, m_color));
- point2 = calculatePoint(2 * M_PI * (slice - 1) / m_slices,
- stack * M_PI / m_stacks);
- m_points->append(GLPoint(m_radius * point2, point2, t0, m_color));
+ m_points->append(GLPoint(m_radius * sliceB[i], sliceB[i],
+ t0, m_color));
}
m_points->append(GLPoint(m_radius * southpol, southpol, t0, m_color));
- for (int stack = m_stacks - 1; stack >= 1; stack--)
+ sliceA = calculateRow(slice + 1);
+
+ for (int i = m_stacks - 2; i >= 0; i--)
{
- point1 = calculatePoint(2 * M_PI * slice / m_slices, stack * M_PI / m_stacks);
- m_points->append(GLPoint(m_radius * point1, point1, t0, m_color));
+ m_points->append(GLPoint(m_radius * sliceA[i], sliceA[i],
+ t0, m_color));
- point2 = calculatePoint(2 * M_PI * (slice + 1) / m_slices,
- stack * M_PI / m_stacks);
- m_points->append(GLPoint(m_radius * point2, point2, t0, m_color));
+ m_points->append(GLPoint(m_radius * sliceB[i], sliceB[i],
+ t0, m_color));
}
m_points->append(GLPoint(m_radius * northpol, northpol, t0, m_color));
@@ -54,6 +59,29 @@ void GLSphere::makeSurface(QVector<GLPoint> *pointContainer,
m_nextPoint = m_points->size();
}
+QVector<QVector3D> GLSphere::calculateRow(int slice)
+{
+ if(slice == m_stacks)
+ {
+ return firstRow;
+ }
+
+ QVector<QVector3D> longitude;
+
+ for (int stack = 1; stack < m_stacks; stack++)
+ {
+ longitude.append(calculatePoint(2 * M_PI * slice / m_slices,
+ stack * M_PI / m_stacks));
+ }
+
+ if (slice == 0)
+ {
+ firstRow = longitude;
+ }
+
+ return longitude;
+}
+
QVector3D GLSphere::calculatePoint(double sliceRotation, double stackRotation)
{
double x = sin(stackRotation) * sin(sliceRotation);