From a0f82956c907e3be5fc906639293632d0d003bc2 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 10 Nov 2016 12:53:19 -0500 Subject: [PATCH] Sphere Data --- src/glnodes/shapes.cpp | 18 ++++- src/glnodes/shapes.h | 16 +++- src/interfaces/visitor.h | 2 + src/viewer/simpleViewer.cpp | 144 +++++++++++++++++++++++++++++++++--- src/viewer/simpleViewer.h | 7 +- 5 files changed, 169 insertions(+), 18 deletions(-) diff --git a/src/glnodes/shapes.cpp b/src/glnodes/shapes.cpp index 5ea289c..9fa80e2 100644 --- a/src/glnodes/shapes.cpp +++ b/src/glnodes/shapes.cpp @@ -3,15 +3,27 @@ #include void Cube::accept(Visitor &v) { - v.visit(*this); + v.visit(*this); +} + +void Sphere::accept(Visitor &v) { + v.visit(*this); } // *** void Cube::setColor(QColor& c) { - color = QColor(c); + color = QColor(c); } QColor Cube::getColor(){ - return color; + return color; +}; + +void Sphere::setColor(QColor& c) { + color = QColor(c); +} + +QColor Sphere::getColor(){ + return color; }; diff --git a/src/glnodes/shapes.h b/src/glnodes/shapes.h index 5d058ae..21765ec 100644 --- a/src/glnodes/shapes.h +++ b/src/glnodes/shapes.h @@ -15,9 +15,19 @@ class Cube : public Shape { public: Cube(){} - QColor color; + QColor color; void accept(Visitor& v) override; - void setColor(QColor& c); - QColor getColor(); + void setColor(QColor& c); + QColor getColor(); +}; + +class Sphere : public Shape +{ +public: + Sphere(){} + QColor color; + void accept(Visitor& v) override; + void setColor(QColor& c); + QColor getColor(); }; #endif // SHAPES_H diff --git a/src/interfaces/visitor.h b/src/interfaces/visitor.h index 999f5c7..5ac432f 100644 --- a/src/interfaces/visitor.h +++ b/src/interfaces/visitor.h @@ -2,10 +2,12 @@ #define VISITOR_H class SceneGroup; class Cube; +class Sphere; class Visitor { public: virtual void visit(SceneGroup &n) = 0; virtual void visit(Cube &s) = 0; + virtual void visit(Sphere &s) = 0; }; #endif // VISITOR_H diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index c2be734..b8168b5 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -47,6 +47,11 @@ namespace const double inc_mult = 5; const double inc_offset = 1.05; + const int numRowSphere = 20; + const int numColSphere = numRowSphere+2; + const int numVerticesSphere = numColSphere * numRowSphere + 2; + const int numTriSphere = numColSphere*(numRowSphere-1)*2 + 2*numColSphere; + int m_lDirectionLocation; int m_normalMatrixLoc; @@ -441,17 +446,98 @@ void Viewer::initGeometries() { 0.0, -1.0, 0.0 }, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.0} }; + GLfloat sphereVertices[numVerticesSphere][3]; + GLfloat sphereNormals[numVerticesSphere][3]; + GLfloat sphereIndices[numTriSphere * 3][3]; - // Fill vertex VBO + // Create Sphere + // Generate surrounding vertices + unsigned int v = 0; + float thetaInc = 2.0f*3.14159265f / static_cast(numColSphere); + float phiInc = 3.14159265f / static_cast(numRowSphere+1); + for (int row=0; row(row+1) * phiInc); + for (int col=0; colsetUniformValue(m_isSkyLoc, false); m_program->setUniformValue(m_isPhongLoc, true); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix()); - m_program->setUniformValue(m_colorLocation, s.getColor()); + m_program->setUniformValue(m_colorLocation, s.getColor()); - glDrawArrays(GL_TRIANGLES, i*6, 6); - } + glDrawArrays(GL_TRIANGLES, i*6, 6); + } +} + + +void Viewer::visit(Sphere &s) +{ + QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); + + glBindVertexArray(m_VAOs[VAO_Sphere]); + m_program->setUniformValue(m_isSkyLoc, false); + m_program->setUniformValue(m_isPhongLoc, true); + m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); + m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix()); + m_program->setUniformValue(m_colorLocation, s.getColor()); + + glDrawElements(GL_TRIANGLES, numTriSphere * 3, GL_UNSIGNED_INT, 0); } void Viewer::visit(SceneGroup &s) diff --git a/src/viewer/simpleViewer.h b/src/viewer/simpleViewer.h index fc542bd..9d9c058 100644 --- a/src/viewer/simpleViewer.h +++ b/src/viewer/simpleViewer.h @@ -46,8 +46,9 @@ public: Viewer(); ~Viewer(); - virtual void visit(SceneGroup &s); + virtual void visit(SceneGroup &s); virtual void visit(Cube &s); + virtual void visit(Sphere &s); public slots: void cleanup(); @@ -107,8 +108,8 @@ private: QColor* activeColor; int activeShape; - enum VAO_IDs { VAO_Cube, NumVAOs }; - enum Buffer_IDs { VBO_Cube, NumBuffers }; + enum VAO_IDs { VAO_Cube, VAO_Sphere, NumVAOs }; + enum Buffer_IDs { VBO_Cube, VBO_Sphere, EBO_Sphere, NumBuffers }; GLuint m_VAOs[NumVAOs]; GLuint m_Buffers[NumBuffers];