Each mesh now has it's own transform matrix applied, so that our example framework can support breaking a volume into regions.

This commit is contained in:
David Williams
2014-05-23 22:03:57 +02:00
parent 9f5b2e1659
commit 4f7e1e6846
3 changed files with 48 additions and 51 deletions

View File

@ -37,6 +37,7 @@ struct OpenGLMeshData
GLuint indexBuffer;
GLuint vertexBuffer;
GLuint vertexArrayObject;
PolyVox::Vector3DInt32 translation;
};
class OpenGLWidget : public QGLWidget
@ -49,13 +50,48 @@ public:
void mouseMoveEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
// Convert a SurfaceMesh to OpenGL index/vertex buffers
template <typename MeshType>
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);
// Convert a SurfaceMesh to OpenGL index/vertex buffers. Inlined because it's templatised.
template <typename MeshType>
void 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<uint8_t>), 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<uint8_t>), 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
meshData.translation = surfaceMesh.m_Region.getLowerCorner();
mMeshData.push_back(meshData);
}
protected:
@ -83,40 +119,4 @@ private:
int m_yRotation;
};
template <typename MeshType>
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<uint8_t>), 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<uint8_t>), 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__