From e441294d029dee0c73a80a943936c117fd5eb56c Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sat, 14 Nov 2015 15:06:59 +0100 Subject: First try with GL_TRIANGLE_STRIPS --- src/glsphere.cpp | 38 +++++++++++++++++++++++++------------- src/mmscene.cpp | 2 +- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/glsphere.cpp b/src/glsphere.cpp index d77ef95..8b68b7c 100644 --- a/src/glsphere.cpp +++ b/src/glsphere.cpp @@ -21,23 +21,35 @@ void GLSphere::makeSurface(QVector *pointContainer, 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(northpol, n0, t0, GLColorRgba::clMagenta)); - m_points->append(GLPoint(calculatePoint(2 * M_PI * (slice - 1) / m_slices, - stack * M_PI / m_stacks), n0, t0, - m_color)); + 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_points->append(GLPoint(southpol, n0, t0, GLColorRgba::clMagenta)); - } + for (int stack = m_stacks - 1; stack > 1; 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_nextPoint = m_points->size(); } diff --git a/src/mmscene.cpp b/src/mmscene.cpp index f199563..8de2c87 100644 --- a/src/mmscene.cpp +++ b/src/mmscene.cpp @@ -96,7 +96,7 @@ void MMScene::createSphere() { m_sphere = new GLSphere(); - m_sphere->setDrawingMode(GL_POINTS); + m_sphere->setDrawingMode(GL_TRIANGLE_STRIP); m_sphere->makeSurface(&m_points, &m_indices); } -- cgit v1.2.3-70-g09d2 From dd3049c6cd5dbd70839ceb9f2229fc03cf0e6107 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 16 Nov 2015 21:48:22 +0100 Subject: Add normals and color --- src/glsphere.cpp | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/glsphere.cpp b/src/glsphere.cpp index 8b68b7c..84a2c59 100644 --- a/src/glsphere.cpp +++ b/src/glsphere.cpp @@ -12,45 +12,45 @@ void GLSphere::makeSurface(QVector *pointContainer, { 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; + QVector3D point1; + QVector3D point2; + 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(northpol, northpol.normalized(), t0, m_color)); - } + for (int stack = 1; stack < m_stacks; stack++) + { + point1 = calculatePoint(2 * M_PI * slice / m_slices, stack * M_PI / m_stacks); + m_points->append(GLPoint(point1, point1.normalized(), t0, m_color)); - m_points->append(GLPoint(southpol, n0, t0, GLColorRgba::clMagenta)); + point2 = calculatePoint(2 * M_PI * (slice - 1) / m_slices, + stack * M_PI / m_stacks); + m_points->append(GLPoint(point2, point2.normalized(), t0, m_color)); + } - for (int stack = m_stacks - 1; stack > 1; 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(southpol, 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)); + for (int stack = m_stacks - 1; stack > 1; stack--) + { + point1 = calculatePoint(2 * M_PI * slice / m_slices, stack * M_PI / m_stacks); + m_points->append(GLPoint(point1, point1.normalized(), t0, m_color)); - } + point2 = calculatePoint(2 * M_PI * (slice + 1) / m_slices, + stack * M_PI / m_stacks); + m_points->append(GLPoint(point2, point3.normalized(), t0, m_color)); } + m_points->append(GLPoint(southpol, southpol.normalized(), t0, m_color)); + } + m_nextPoint = m_points->size(); } -- cgit v1.2.3-70-g09d2 From 781b2563ea559243de28ebc357b1a3f0be1e9cb2 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Tue, 17 Nov 2015 10:00:10 +0100 Subject: Get rid of normalized --- src/glsphere.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/glsphere.cpp b/src/glsphere.cpp index 84a2c59..56144f1 100644 --- a/src/glsphere.cpp +++ b/src/glsphere.cpp @@ -14,8 +14,8 @@ void GLSphere::makeSurface(QVector *pointContainer, QVector3D t0 = QVector3D(0.0, 0.0, 0.0); //dummy texture - QVector3D northpol = v_Y * m_radius; - QVector3D southpol = -v_Y * m_radius; + QVector3D northpol = v_Y; + QVector3D southpol = -v_Y; QVector3D point1; QVector3D point2; @@ -24,31 +24,31 @@ void GLSphere::makeSurface(QVector *pointContainer, for (int slice = 1; slice < m_slices; slice += 2) { - m_points->append(GLPoint(northpol, northpol.normalized(), t0, m_color)); + m_points->append(GLPoint(m_radius * northpol, northpol, t0, m_color)); for (int stack = 1; stack < m_stacks; stack++) { point1 = calculatePoint(2 * M_PI * slice / m_slices, stack * M_PI / m_stacks); - m_points->append(GLPoint(point1, point1.normalized(), t0, m_color)); + m_points->append(GLPoint(m_radius * point1, point1, t0, m_color)); point2 = calculatePoint(2 * M_PI * (slice - 1) / m_slices, stack * M_PI / m_stacks); - m_points->append(GLPoint(point2, point2.normalized(), t0, m_color)); + m_points->append(GLPoint(m_radius * point2, point2, t0, m_color)); } - m_points->append(GLPoint(southpol, n0, t0, m_color)); + m_points->append(GLPoint(m_radius * southpol, southpol, t0, m_color)); - for (int stack = m_stacks - 1; stack > 1; stack--) + for (int stack = m_stacks - 1; stack >= 1; stack--) { point1 = calculatePoint(2 * M_PI * slice / m_slices, stack * M_PI / m_stacks); - m_points->append(GLPoint(point1, point1.normalized(), t0, m_color)); + m_points->append(GLPoint(m_radius * point1, point1, t0, m_color)); point2 = calculatePoint(2 * M_PI * (slice + 1) / m_slices, stack * M_PI / m_stacks); - m_points->append(GLPoint(point2, point3.normalized(), t0, m_color)); + m_points->append(GLPoint(m_radius * point2, point2, t0, m_color)); } - m_points->append(GLPoint(southpol, southpol.normalized(), t0, m_color)); + m_points->append(GLPoint(m_radius * northpol, northpol, t0, m_color)); } m_nextPoint = m_points->size(); @@ -56,9 +56,9 @@ void GLSphere::makeSurface(QVector *pointContainer, 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); + double x = sin(stackRotation) * sin(sliceRotation); + double y = cos(stackRotation); + double z = sin(stackRotation) * cos(sliceRotation); return QVector3D(x, y, z); } -- cgit v1.2.3-70-g09d2 From 709e2363edcebdcf5d0185db77dc2944df7004dd Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 23 Nov 2015 20:10:12 +0100 Subject: Add caching to GLSphere --- src/glsphere.cpp | 60 +++++++++++++++++++++++++++++++++++++++++--------------- src/glsphere.h | 3 +++ 2 files changed, 47 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 *pointContainer, @@ -17,35 +18,39 @@ void GLSphere::makeSurface(QVector *pointContainer, QVector3D northpol = v_Y; QVector3D southpol = -v_Y; - QVector3D point1; - QVector3D point2; + QVector firstPointRow = calculateRow(0); + + QVector sliceA = firstPointRow; + QVector 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 *pointContainer, m_nextPoint = m_points->size(); } +QVector GLSphere::calculateRow(int slice) +{ + if(slice == m_stacks) + { + return firstRow; + } + + QVector 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); diff --git a/src/glsphere.h b/src/glsphere.h index 9aae032..f884117 100644 --- a/src/glsphere.h +++ b/src/glsphere.h @@ -22,8 +22,11 @@ private: */ QVector3D calculatePoint(double sliceRotation, double stackRotation); + QVector calculateRow(int slice); + int m_stacks; int m_slices; + QVector firstRow; }; #endif // GLSPHERE_H -- cgit v1.2.3-70-g09d2