Skip to content

Commit

Permalink
Sphere Drawing Skeleton: add vertex positions and normals to a single…
Browse files Browse the repository at this point in the history
… std::vector and defined two attribues, but this seems not to work properly (debug this)
  • Loading branch information
denizdiktas committed Jun 7, 2023
1 parent 52f4c7e commit 9369e15
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ void MainWidget::initShaderProgram()
uniformMVP = glGetUniformLocation(shader, "MVP");
cout << "uniform loc = " << uniformMVP << endl;
}



void MainWidget::initGeometry()
{
const float c = 0.5;
Expand Down Expand Up @@ -249,18 +252,18 @@ void MainWidget::initGeometry()

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
int MainWidget::createSphere(int numSlices, int numStacks, float r)
void MainWidget::createSphere(int numSlices, int numStacks, float r)
{
numStacks = std::max<int>(2, numStacks);
vector<QVector3D> vertices, normals;
vector<QVector3D> vertices;

// NORTH POLE
vertices.push_back(QVector3D(0, 0, r));
normals.push_back(QVector3D(0, 0, 1));
vertices.push_back(QVector3D(0, 0, 1));

// SOUTH POLE
vertices.push_back(QVector3D(0, 0, -r));
normals.push_back(QVector3D(0, 0, -1));
vertices.push_back(QVector3D(0, 0, -1));
int startingIndexOfMiddleVertices = vertices.size();

for (int j = 1; j < numStacks; j++)
Expand All @@ -282,11 +285,12 @@ int MainWidget::createSphere(int numSlices, int numStacks, float r)
auto p = QVector3D(x, y, z);
auto n = p / p.length();
vertices.push_back(p);
normals.push_back(n);
vertices.push_back(n);
}
}

// add the indices for all trinagles

// add the indices for all triangles
vector<GLuint> indices;

// NORTH CAP
Expand Down Expand Up @@ -362,18 +366,23 @@ int MainWidget::createSphere(int numSlices, int numStacks, float r)
{
glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D) * vertices.size(), reinterpret_cast<const void*>(vertices.data()), GL_STATIC_DRAW);

GLint index = 0;
glVertexAttribPointer(index, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(index);
// positions
GLint positionAttribIndex = 0;
GLsizei stride = 6 * sizeof(float);
glVertexAttribPointer(positionAttribIndex, 3, GL_FLOAT, GL_FALSE, stride, 0);
glEnableVertexAttribArray(positionAttribIndex);
//normals
GLint normalAttribIndex = 1;
auto* normal_offset = reinterpret_cast<const void*>(3 * sizeof(float));
glVertexAttribPointer(normalAttribIndex, 3, GL_FLOAT, GL_FALSE, stride, normal_offset);
glEnableVertexAttribArray(normalAttribIndex);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);

}
glBindVertexArray(0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

return numIndices;
}
//! [3]

Expand Down
2 changes: 1 addition & 1 deletion Arrangement_on_surface_2/demo/earth/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MainWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
void initShaderProgram();

void initGeometry();
int createSphere(int numSlices, int numStacks, float r);
void createSphere(int numSlices, int numStacks, float r);


private:
Expand Down

0 comments on commit 9369e15

Please sign in to comment.