Sphere Data

This commit is contained in:
unknown 2016-11-10 12:53:19 -05:00
parent d1ed0a00dd
commit a0f82956c9
5 changed files with 169 additions and 18 deletions

View File

@ -3,15 +3,27 @@
#include <QVector4D> #include <QVector4D>
void Cube::accept(Visitor &v) { void Cube::accept(Visitor &v) {
v.visit(*this); v.visit(*this);
}
void Sphere::accept(Visitor &v) {
v.visit(*this);
} }
// *** // ***
void Cube::setColor(QColor& c) { void Cube::setColor(QColor& c) {
color = QColor(c); color = QColor(c);
} }
QColor Cube::getColor(){ QColor Cube::getColor(){
return color; return color;
};
void Sphere::setColor(QColor& c) {
color = QColor(c);
}
QColor Sphere::getColor(){
return color;
}; };

View File

@ -15,9 +15,19 @@ class Cube : public Shape
{ {
public: public:
Cube(){} Cube(){}
QColor color; QColor color;
void accept(Visitor& v) override; void accept(Visitor& v) override;
void setColor(QColor& c); void setColor(QColor& c);
QColor getColor(); QColor getColor();
};
class Sphere : public Shape
{
public:
Sphere(){}
QColor color;
void accept(Visitor& v) override;
void setColor(QColor& c);
QColor getColor();
}; };
#endif // SHAPES_H #endif // SHAPES_H

View File

@ -2,10 +2,12 @@
#define VISITOR_H #define VISITOR_H
class SceneGroup; class SceneGroup;
class Cube; class Cube;
class Sphere;
class Visitor { class Visitor {
public: public:
virtual void visit(SceneGroup &n) = 0; virtual void visit(SceneGroup &n) = 0;
virtual void visit(Cube &s) = 0; virtual void visit(Cube &s) = 0;
virtual void visit(Sphere &s) = 0;
}; };
#endif // VISITOR_H #endif // VISITOR_H

View File

@ -47,6 +47,11 @@ namespace
const double inc_mult = 5; const double inc_mult = 5;
const double inc_offset = 1.05; 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_lDirectionLocation;
int m_normalMatrixLoc; 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} { 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<float>(numColSphere);
float phiInc = 3.14159265f / static_cast<float>(numRowSphere+1);
for (int row=0; row<numRowSphere; ++row)
{
float phi = 3.14159265f - (static_cast<float>(row+1) * phiInc);
for (int col=0; col<numColSphere; ++col, ++v)
{
float theta = col*thetaInc;
sphereVertices[v][0] = 0.5*sin(theta)*sin(phi);
sphereVertices[v][1] = 0.5*cos(phi);
sphereVertices[v][2] = 0.5*cos(theta)*sin(phi);
sphereNormals[v][0] = sphereVertices[v][0]*2.0f; // Multiply by 2 because sphere radius is 0.5
sphereNormals[v][1] = sphereVertices[v][1]*2.0f;
sphereNormals[v][2] = sphereVertices[v][2]*2.0f;
}
}
// Generate cap vertices
sphereVertices[numColSphere*numRowSphere+0][0] = 0.0f;
sphereVertices[numColSphere*numRowSphere+0][1] = -0.5f;
sphereVertices[numColSphere*numRowSphere+0][2] = 0.0f;
sphereVertices[numColSphere*numRowSphere+1][0] = 0.0f;
sphereVertices[numColSphere*numRowSphere+1][1] = 0.5f;
sphereVertices[numColSphere*numRowSphere+1][2] = 0.0f;
sphereNormals[numColSphere*numRowSphere+0][0] = 0.0f;
sphereNormals[numColSphere*numRowSphere+0][1] = -1.0f;
sphereNormals[numColSphere*numRowSphere+0][2] = 0.0f;
sphereNormals[numColSphere*numRowSphere+1][0] = 0.0f;
sphereNormals[numColSphere*numRowSphere+1][1] = 1.0f;
sphereNormals[numColSphere*numRowSphere+1][2] = 0.0f;
// Generate surrounding indices (faces)
unsigned int tri = 0;
for (int row=0; row<numRowSphere-1; ++row)
{
unsigned int rowStart = row*numColSphere;
unsigned int topRowStart = rowStart + numColSphere;
for (int col=0; col<numColSphere; ++col, tri += 2)
{
// Compute quad vertices
unsigned int v = rowStart + col;
unsigned int vi = (col<numColSphere-1) ? v+1 : rowStart;
unsigned int vj = topRowStart + col;
unsigned int vji = (col<numColSphere-1) ? vj+1 : topRowStart;
// Add to indices
sphereIndices[tri+0][0] = v;
sphereIndices[tri+0][1] = vi;
sphereIndices[tri+0][2] = vj;
sphereIndices[tri+1][0] = vi;
sphereIndices[tri+1][1] = vji;
sphereIndices[tri+1][2] = vj;
}
}
// Generate cap indices (faces)
for (int col=0; col<numColSphere; ++col, tri += 2)
{
sphereIndices[tri+0][0] = numColSphere*numRowSphere;
sphereIndices[tri+0][1] = (col<numColSphere-1) ? col+1 : 0;
sphereIndices[tri+0][2] = col;
unsigned int rowStart = (numRowSphere-1)*numColSphere;
sphereIndices[tri+1][0] = numColSphere*numRowSphere+1;
sphereIndices[tri+1][1] = rowStart + col;
sphereIndices[tri+1][2] = (col<numColSphere-1) ? (rowStart + col + 1) : rowStart;
}
// Prep buffers
glGenBuffers(NumBuffers, m_Buffers);
// Fill cube vertex VBO
GLsizeiptr offsetVertices = 0; GLsizeiptr offsetVertices = 0;
GLsizeiptr offsetNormals = sizeof(verticesCube); GLsizeiptr offsetNormals = sizeof(verticesCube);
GLsizeiptr offsetUV = offsetNormals + sizeof(normals); GLsizeiptr offsetUV = offsetNormals + sizeof(normals);
GLsizeiptr dataSize = offsetUV + sizeof(uvs); GLsizeiptr dataSize = offsetUV + sizeof(uvs);
glBindVertexArray(m_VAOs[VAO_Cube]); glBindVertexArray(m_VAOs[VAO_Cube]);
// glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
glGenBuffers(NumBuffers, m_Buffers);
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
glBufferData(GL_ARRAY_BUFFER, dataSize, glBufferData(GL_ARRAY_BUFFER, dataSize,
NULL, GL_STATIC_DRAW); NULL, GL_STATIC_DRAW);
@ -469,23 +555,63 @@ void Viewer::initGeometries()
glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offsetUV)); glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offsetUV));
glEnableVertexAttribArray(s_vUvLocation); glEnableVertexAttribArray(s_vUvLocation);
// Fill Sphere VBO
GLsizeiptr offsetSphereV = 0;
GLsizeiptr offsetSphereN = offsetSphereV + sizeof(sphereVertices);
GLsizeiptr sphereDataSize = offsetSphereN + sizeof(sphereNormals);
glBindVertexArray(m_VAOs[VAO_Sphere]);
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Sphere]);
glBufferData(GL_ARRAY_BUFFER, sphereDataSize,
NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, offsetSphereV, sizeof(sphereVertices), sphereVertices);
glBufferSubData(GL_ARRAY_BUFFER, offsetSphereN, sizeof(sphereNormals), sphereNormals);
glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(m_vPositionLocation);
glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, GL_TRUE, 0, BUFFER_OFFSET(offsetNormals));
glEnableVertexAttribArray(m_vNormalLocation);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Buffers[EBO_Sphere]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sphereIndices), sphereIndices, GL_STATIC_DRAW);
} }
void Viewer::visit(Cube &s) void Viewer::visit(Cube &s)
{ {
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
int faces = floor(numVerticesCube/6); int faces = floor(numVerticesCube/6);
for(int i = 0; i < faces; i++){ // 6 vertexes par face for(int i = 0; i < faces; i++){ // 6 vertexes par face
glBindVertexArray(m_VAOs[VAO_Cube]); glBindVertexArray(m_VAOs[VAO_Cube]);
m_program->setUniformValue(m_isSkyLoc, false); m_program->setUniformValue(m_isSkyLoc, false);
m_program->setUniformValue(m_isPhongLoc, true); m_program->setUniformValue(m_isPhongLoc, true);
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix()); 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) void Viewer::visit(SceneGroup &s)

View File

@ -46,8 +46,9 @@ public:
Viewer(); Viewer();
~Viewer(); ~Viewer();
virtual void visit(SceneGroup &s); virtual void visit(SceneGroup &s);
virtual void visit(Cube &s); virtual void visit(Cube &s);
virtual void visit(Sphere &s);
public slots: public slots:
void cleanup(); void cleanup();
@ -107,8 +108,8 @@ private:
QColor* activeColor; QColor* activeColor;
int activeShape; int activeShape;
enum VAO_IDs { VAO_Cube, NumVAOs }; enum VAO_IDs { VAO_Cube, VAO_Sphere, NumVAOs };
enum Buffer_IDs { VBO_Cube, NumBuffers }; enum Buffer_IDs { VBO_Cube, VBO_Sphere, EBO_Sphere, NumBuffers };
GLuint m_VAOs[NumVAOs]; GLuint m_VAOs[NumVAOs];
GLuint m_Buffers[NumBuffers]; GLuint m_Buffers[NumBuffers];