Templatised code for example mesh conversion.
This commit is contained in:
parent
79acf814db
commit
37bdf8e3ac
@ -16,6 +16,17 @@ OpenGLWidget::OpenGLWidget(QWidget *parent)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLWidget::setMeshToRender(const PolyVox::Mesh<CubicVertex<uint8_t> >& surfaceMesh)
|
void OpenGLWidget::setMeshToRender(const PolyVox::Mesh<CubicVertex<uint8_t> >& surfaceMesh)
|
||||||
|
{
|
||||||
|
setMeshToRenderImpl(surfaceMesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLWidget::setMeshToRender(const PolyVox::Mesh<MarchingCubesVertex<uint8_t> >& surfaceMesh)
|
||||||
|
{
|
||||||
|
setMeshToRenderImpl(surfaceMesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename MeshType>
|
||||||
|
void OpenGLWidget::setMeshToRenderImpl(const MeshType& surfaceMesh)
|
||||||
{
|
{
|
||||||
//Convienient access to the vertices and indices
|
//Convienient access to the vertices and indices
|
||||||
const auto& vecIndices = surfaceMesh.getIndices();
|
const auto& vecIndices = surfaceMesh.getIndices();
|
||||||
@ -24,27 +35,27 @@ void OpenGLWidget::setMeshToRender(const PolyVox::Mesh<CubicVertex<uint8_t> >& s
|
|||||||
// This struct holds the OpenGL properties (buffer handles, etc) which will be used
|
// 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.
|
// to render our mesh. We copy the data from the PolyVox mesh into this structure.
|
||||||
OpenGLMeshData meshData;
|
OpenGLMeshData meshData;
|
||||||
|
|
||||||
//Create the VAO for the mesh
|
//Create the VAO for the mesh
|
||||||
glGenVertexArrays(1, &(meshData.vertexArrayObject));
|
glGenVertexArrays(1, &(meshData.vertexArrayObject));
|
||||||
glBindVertexArray(meshData.vertexArrayObject);
|
glBindVertexArray(meshData.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, &(meshData.vertexBuffer));
|
glGenBuffers(1, &(meshData.vertexBuffer));
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, meshData.vertexBuffer);
|
glBindBuffer(GL_ARRAY_BUFFER, meshData.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, &(meshData.indexBuffer));
|
glGenBuffers(1, &(meshData.indexBuffer));
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshData.indexBuffer);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshData.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
|
||||||
glEnableVertexAttribArray(0); //We're talking about shader attribute '0'
|
glEnableVertexAttribArray(0); //We're talking about shader attribute '0'
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(CubicVertex<uint8_t>), 0); //take the first 3 floats from every sizeof(decltype(vecVertices)::value_type)
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(CubicVertex<uint8_t>), 0); //take the first 3 floats from every sizeof(decltype(vecVertices)::value_type)
|
||||||
|
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
meshData.noOfIndices = vecIndices.size(); //Save this for the call to glDrawElements later
|
meshData.noOfIndices = vecIndices.size(); //Save this for the call to glDrawElements later
|
||||||
|
|
||||||
mMeshData.push_back(meshData);
|
mMeshData.push_back(meshData);
|
||||||
|
@ -51,11 +51,15 @@ public:
|
|||||||
|
|
||||||
// Convert a SurfaceMesh to OpenGL index/vertex buffers
|
// Convert a SurfaceMesh to OpenGL index/vertex buffers
|
||||||
void setMeshToRender(const PolyVox::Mesh<PolyVox::CubicVertex<uint8_t> >& surfaceMesh);
|
void setMeshToRender(const PolyVox::Mesh<PolyVox::CubicVertex<uint8_t> >& surfaceMesh);
|
||||||
|
void setMeshToRender(const PolyVox::Mesh<PolyVox::MarchingCubesVertex<uint8_t> >& surfaceMesh);
|
||||||
|
|
||||||
// The viewable region can be adjusted so that this example framework can be use for different volume sizes.
|
// The viewable region can be adjusted so that this example framework can be use for different volume sizes.
|
||||||
void setViewableRegion(PolyVox::Region viewableRegion);
|
void setViewableRegion(PolyVox::Region viewableRegion);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
template <typename MeshType>
|
||||||
|
void setMeshToRenderImpl(const MeshType& surfaceMesh);
|
||||||
|
|
||||||
// Qt OpenGL functions
|
// Qt OpenGL functions
|
||||||
void initializeGL();
|
void initializeGL();
|
||||||
void resizeGL(int w, int h);
|
void resizeGL(int w, int h);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user