More work adding support for multiple meshes.
This commit is contained in:
parent
51e93fdabc
commit
7d55e00f76
@ -19,19 +19,23 @@ void OpenGLWidget::setSurfaceMeshToRender(const PolyVox::SurfaceMesh<CubicVertex
|
|||||||
//Convienient access to the vertices and indices
|
//Convienient access to the vertices and indices
|
||||||
const auto& vecIndices = surfaceMesh.getIndices();
|
const auto& vecIndices = surfaceMesh.getIndices();
|
||||||
const auto& vecVertices = surfaceMesh.getVertices();
|
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
|
//Create the VAO for the mesh
|
||||||
glGenVertexArrays(1, &(mMeshData.vertexArrayObject));
|
glGenVertexArrays(1, &(meshData.vertexArrayObject));
|
||||||
glBindVertexArray(mMeshData.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, &(mMeshData.vertexBuffer));
|
glGenBuffers(1, &(meshData.vertexBuffer));
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, mMeshData.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, &(mMeshData.indexBuffer));
|
glGenBuffers(1, &(meshData.indexBuffer));
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mMeshData.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
|
||||||
@ -40,7 +44,9 @@ void OpenGLWidget::setSurfaceMeshToRender(const PolyVox::SurfaceMesh<CubicVertex
|
|||||||
|
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
mMeshData.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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLWidget::initializeGL()
|
void OpenGLWidget::initializeGL()
|
||||||
@ -155,21 +161,24 @@ void OpenGLWidget::paintGL()
|
|||||||
{
|
{
|
||||||
//Clear the screen
|
//Clear the screen
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
QMatrix4x4 modelToWorldMatrix{};
|
QMatrix4x4 modelToWorldMatrix{};
|
||||||
modelToWorldMatrix.rotate(m_xRotation, 0, 1, 0); //rotate around y-axis
|
modelToWorldMatrix.rotate(m_xRotation, 0, 1, 0); //rotate around y-axis
|
||||||
modelToWorldMatrix.rotate(m_yRotation, 1, 0, 0); //rotate around x-axis
|
modelToWorldMatrix.rotate(m_yRotation, 1, 0, 0); //rotate around x-axis
|
||||||
modelToWorldMatrix.translate(-32, -32, -32); //centre the model on the origin
|
modelToWorldMatrix.translate(-32, -32, -32); //centre the model on the origin
|
||||||
|
|
||||||
shader.bind();
|
shader.bind();
|
||||||
|
|
||||||
shader.setUniformValue("modelToWorldMatrix", modelToWorldMatrix); //Update to the latest camera matrix
|
shader.setUniformValue("modelToWorldMatrix", modelToWorldMatrix); //Update to the latest camera matrix
|
||||||
|
|
||||||
glBindVertexArray(mMeshData.vertexArrayObject);
|
for (OpenGLMeshData meshData : mMeshData)
|
||||||
|
{
|
||||||
glDrawElements(GL_TRIANGLES, mMeshData.noOfIndices, GL_UNSIGNED_INT, 0);
|
glBindVertexArray(meshData.vertexArrayObject);
|
||||||
|
|
||||||
glBindVertexArray(0);
|
glDrawElements(GL_TRIANGLES, meshData.noOfIndices, GL_UNSIGNED_INT, 0);
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
shader.release();
|
shader.release();
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
//Index/vertex buffer data
|
//Index/vertex buffer data
|
||||||
OpenGLMeshData mMeshData;
|
std::vector<OpenGLMeshData> mMeshData;
|
||||||
|
|
||||||
QGLShaderProgram shader;
|
QGLShaderProgram shader;
|
||||||
|
|
||||||
|
@ -78,11 +78,15 @@ int main(int argc, char *argv[])
|
|||||||
createSphereInVolume(volData, 30);
|
createSphereInVolume(volData, 30);
|
||||||
|
|
||||||
// Extract the surface for the specified region of the volume. Uncomment the line for the kind of surface extraction you want to see.
|
// Extract the surface for the specified region of the volume. Uncomment the line for the kind of surface extraction you want to see.
|
||||||
auto mesh = extractCubicSurface(&volData, volData.getEnclosingRegion());
|
//auto mesh = extractCubicSurface(&volData, volData.getEnclosingRegion());
|
||||||
//auto mesh = extractMarchingCubesSurface(&volData, volData.getEnclosingRegion());
|
//auto mesh = extractMarchingCubesSurface(&volData, volData.getEnclosingRegion());
|
||||||
|
|
||||||
|
auto mesh = extractCubicSurface(&volData, PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(31, 31, 31)));
|
||||||
|
auto mesh2 = extractCubicSurface(&volData, PolyVox::Region(Vector3DInt32(32, 32, 32), Vector3DInt32(63, 63, 63)));
|
||||||
|
|
||||||
//Pass the surface to the OpenGL window
|
//Pass the surface to the OpenGL window
|
||||||
openGLWidget.setSurfaceMeshToRender(mesh);
|
openGLWidget.setSurfaceMeshToRender(mesh);
|
||||||
|
openGLWidget.setSurfaceMeshToRender(mesh2);
|
||||||
|
|
||||||
//Run the message pump.
|
//Run the message pump.
|
||||||
return app.exec();
|
return app.exec();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user