Modifying the OpenGLWidget so that it can render multiple meshes.

This commit is contained in:
David Williams
2014-05-22 16:25:36 +02:00
parent 7fc954a6bf
commit 51e93fdabc
2 changed files with 18 additions and 13 deletions

View File

@ -21,17 +21,17 @@ void OpenGLWidget::setSurfaceMeshToRender(const PolyVox::SurfaceMesh<CubicVertex
const auto& vecVertices = surfaceMesh.getVertices(); const auto& vecVertices = surfaceMesh.getVertices();
//Create the VAO for the mesh //Create the VAO for the mesh
glGenVertexArrays(1, &vertexArrayObject); glGenVertexArrays(1, &(mMeshData.vertexArrayObject));
glBindVertexArray(vertexArrayObject); glBindVertexArray(mMeshData.vertexArrayObject);
//The GL_ARRAY_BUFFER will contain the list of vertex positions //The GL_ARRAY_BUFFER will contain the list of vertex positions
glGenBuffers(1, &vertexBuffer); glGenBuffers(1, &(mMeshData.vertexBuffer));
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, mMeshData.vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(CubicVertex<uint8_t>), vecVertices.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(CubicVertex<uint8_t>), vecVertices.data(), GL_STATIC_DRAW);
//and GL_ELEMENT_ARRAY_BUFFER will contain the indices //and GL_ELEMENT_ARRAY_BUFFER will contain the indices
glGenBuffers(1, &indexBuffer); glGenBuffers(1, &(mMeshData.indexBuffer));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mMeshData.indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vecIndices.size() * sizeof(uint32_t), vecIndices.data(), GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, vecIndices.size() * sizeof(uint32_t), vecIndices.data(), GL_STATIC_DRAW);
//We need to tell OpenGL how to understand the format of the vertex data //We need to tell OpenGL how to understand the format of the vertex data
@ -40,7 +40,7 @@ void OpenGLWidget::setSurfaceMeshToRender(const PolyVox::SurfaceMesh<CubicVertex
glBindVertexArray(0); glBindVertexArray(0);
noOfIndices = vecIndices.size(); //Save this for the call to glDrawElements later mMeshData.noOfIndices = vecIndices.size(); //Save this for the call to glDrawElements later
} }
void OpenGLWidget::initializeGL() void OpenGLWidget::initializeGL()
@ -165,9 +165,9 @@ void OpenGLWidget::paintGL()
shader.setUniformValue("modelToWorldMatrix", modelToWorldMatrix); //Update to the latest camera matrix shader.setUniformValue("modelToWorldMatrix", modelToWorldMatrix); //Update to the latest camera matrix
glBindVertexArray(vertexArrayObject); glBindVertexArray(mMeshData.vertexArrayObject);
glDrawElements(GL_TRIANGLES, noOfIndices, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, mMeshData.noOfIndices, GL_UNSIGNED_INT, 0);
glBindVertexArray(0); glBindVertexArray(0);

View File

@ -31,6 +31,14 @@ distribution.
#include <QGLWidget> #include <QGLWidget>
#include <QGLShaderProgram> #include <QGLShaderProgram>
struct OpenGLMeshData
{
GLuint noOfIndices;
GLuint indexBuffer;
GLuint vertexBuffer;
GLuint vertexArrayObject;
};
class OpenGLWidget : public QGLWidget class OpenGLWidget : public QGLWidget
{ {
public: public:
@ -52,10 +60,7 @@ protected:
private: private:
//Index/vertex buffer data //Index/vertex buffer data
GLuint noOfIndices; OpenGLMeshData mMeshData;
GLuint indexBuffer;
GLuint vertexBuffer;
GLuint vertexArrayObject;
QGLShaderProgram shader; QGLShaderProgram shader;