From 9f5b2e165926ebeadffb799cfb62dfaad887af41 Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 23 May 2014 17:02:46 +0200 Subject: [PATCH] Better templatization of addMesh. --- examples/Basic/OpenGLWidget.cpp | 46 --------------------------------- examples/Basic/OpenGLWidget.h | 43 +++++++++++++++++++++++++++--- examples/Basic/main.cpp | 4 +-- 3 files changed, 41 insertions(+), 52 deletions(-) diff --git a/examples/Basic/OpenGLWidget.cpp b/examples/Basic/OpenGLWidget.cpp index badf1cba..edd75b55 100644 --- a/examples/Basic/OpenGLWidget.cpp +++ b/examples/Basic/OpenGLWidget.cpp @@ -15,52 +15,6 @@ OpenGLWidget::OpenGLWidget(QWidget *parent) { } -void OpenGLWidget::setMeshToRender(const PolyVox::Mesh >& surfaceMesh) -{ - setMeshToRenderImpl(surfaceMesh); -} - -void OpenGLWidget::setMeshToRender(const PolyVox::Mesh >& surfaceMesh) -{ - setMeshToRenderImpl(surfaceMesh); -} - -template -void OpenGLWidget::setMeshToRenderImpl(const MeshType& surfaceMesh) -{ - //Convienient access to the vertices and indices - const auto& vecIndices = surfaceMesh.getIndices(); - const auto& vecVertices = surfaceMesh.getVertices(); - - // This struct holds the OpenGL properties (buffer handles, etc) which will be used - // to render our mesh. We copy the data from the PolyVox mesh into this structure. - OpenGLMeshData meshData; - - //Create the VAO for the mesh - glGenVertexArrays(1, &(meshData.vertexArrayObject)); - glBindVertexArray(meshData.vertexArrayObject); - - //The GL_ARRAY_BUFFER will contain the list of vertex positions - glGenBuffers(1, &(meshData.vertexBuffer)); - glBindBuffer(GL_ARRAY_BUFFER, meshData.vertexBuffer); - glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(CubicVertex), vecVertices.data(), GL_STATIC_DRAW); - - //and GL_ELEMENT_ARRAY_BUFFER will contain the indices - glGenBuffers(1, &(meshData.indexBuffer)); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshData.indexBuffer); - 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 - glEnableVertexAttribArray(0); //We're talking about shader attribute '0' - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(CubicVertex), 0); //take the first 3 floats from every sizeof(decltype(vecVertices)::value_type) - - glBindVertexArray(0); - - meshData.noOfIndices = vecIndices.size(); //Save this for the call to glDrawElements later - - mMeshData.push_back(meshData); -} - void OpenGLWidget::setViewableRegion(Region viewableRegion) { m_viewableRegion = viewableRegion; diff --git a/examples/Basic/OpenGLWidget.h b/examples/Basic/OpenGLWidget.h index 8e6f048c..7ac39928 100644 --- a/examples/Basic/OpenGLWidget.h +++ b/examples/Basic/OpenGLWidget.h @@ -50,15 +50,14 @@ public: void mousePressEvent(QMouseEvent* event); // Convert a SurfaceMesh to OpenGL index/vertex buffers - void setMeshToRender(const PolyVox::Mesh >& surfaceMesh); - void setMeshToRender(const PolyVox::Mesh >& surfaceMesh); + template + void addMesh(const MeshType& surfaceMesh); // The viewable region can be adjusted so that this example framework can be use for different volume sizes. void setViewableRegion(PolyVox::Region viewableRegion); protected: - template - void setMeshToRenderImpl(const MeshType& surfaceMesh); + // Qt OpenGL functions void initializeGL(); @@ -84,4 +83,40 @@ private: int m_yRotation; }; +template +void OpenGLWidget::addMesh(const MeshType& surfaceMesh) +{ + //Convienient access to the vertices and indices + const auto& vecIndices = surfaceMesh.getIndices(); + const auto& vecVertices = surfaceMesh.getVertices(); + + // This struct holds the OpenGL properties (buffer handles, etc) which will be used + // to render our mesh. We copy the data from the PolyVox mesh into this structure. + OpenGLMeshData meshData; + + //Create the VAO for the mesh + glGenVertexArrays(1, &(meshData.vertexArrayObject)); + glBindVertexArray(meshData.vertexArrayObject); + + //The GL_ARRAY_BUFFER will contain the list of vertex positions + glGenBuffers(1, &(meshData.vertexBuffer)); + glBindBuffer(GL_ARRAY_BUFFER, meshData.vertexBuffer); + glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(CubicVertex), vecVertices.data(), GL_STATIC_DRAW); + + //and GL_ELEMENT_ARRAY_BUFFER will contain the indices + glGenBuffers(1, &(meshData.indexBuffer)); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshData.indexBuffer); + 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 + glEnableVertexAttribArray(0); //We're talking about shader attribute '0' + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(CubicVertex), 0); //take the first 3 floats from every sizeof(decltype(vecVertices)::value_type) + + glBindVertexArray(0); + + meshData.noOfIndices = vecIndices.size(); //Save this for the call to glDrawElements later + + mMeshData.push_back(meshData); +} + #endif //__BasicExample_OpenGLWidget_H__ diff --git a/examples/Basic/main.cpp b/examples/Basic/main.cpp index c3a53f59..b422f1c4 100644 --- a/examples/Basic/main.cpp +++ b/examples/Basic/main.cpp @@ -85,9 +85,9 @@ int main(int argc, char *argv[]) //auto mesh2 = extractCubicMesh(&volData, PolyVox::Region(Vector3DInt32(32, 32, 32), Vector3DInt32(63, 63, 63))); //Pass the surface to the OpenGL window - openGLWidget.setMeshToRender(mesh); + openGLWidget.addMesh(mesh); openGLWidget.setViewableRegion(volData.getEnclosingRegion()); - //openGLWidget.setMeshToRender(mesh2); + //openGLWidget.addMesh(mesh2); //Run the message pump. return app.exec();