Examples now support both 16 and 32 bit indices.
This commit is contained in:
parent
d4118a2052
commit
f1ed0c1be2
@ -161,7 +161,7 @@ void OpenGLWidget::paintGL()
|
|||||||
// Bind the vertex array for the current mesh
|
// Bind the vertex array for the current mesh
|
||||||
glBindVertexArray(meshData.vertexArrayObject);
|
glBindVertexArray(meshData.vertexArrayObject);
|
||||||
// Draw the mesh
|
// Draw the mesh
|
||||||
glDrawElements(GL_TRIANGLES, meshData.noOfIndices, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, meshData.noOfIndices, meshData.indexType, 0);
|
||||||
// Unbind the vertex array.
|
// Unbind the vertex array.
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,7 @@ distribution.
|
|||||||
struct OpenGLMeshData
|
struct OpenGLMeshData
|
||||||
{
|
{
|
||||||
GLuint noOfIndices;
|
GLuint noOfIndices;
|
||||||
|
GLenum indexType;
|
||||||
GLuint indexBuffer;
|
GLuint indexBuffer;
|
||||||
GLuint vertexBuffer;
|
GLuint vertexBuffer;
|
||||||
GLuint vertexArrayObject;
|
GLuint vertexArrayObject;
|
||||||
@ -53,8 +54,8 @@ public:
|
|||||||
OpenGLWidget(QWidget *parent);
|
OpenGLWidget(QWidget *parent);
|
||||||
|
|
||||||
// Convert a PolyVox mesh to OpenGL index/vertex buffers. Inlined because it's templatised.
|
// Convert a PolyVox mesh to OpenGL index/vertex buffers. Inlined because it's templatised.
|
||||||
template <typename DataType>
|
template <typename MeshType>
|
||||||
void addMesh(const PolyVox::Mesh< PolyVox::Vertex< DataType > >& surfaceMesh, const PolyVox::Vector3DInt32& translation = PolyVox::Vector3DInt32(0, 0, 0), float scale = 1.0f)
|
void addMesh(const MeshType& surfaceMesh, const PolyVox::Vector3DInt32& translation = PolyVox::Vector3DInt32(0, 0, 0), float scale = 1.0f)
|
||||||
{
|
{
|
||||||
// Convienient access to the vertices and indices
|
// Convienient access to the vertices and indices
|
||||||
const auto& vecIndices = surfaceMesh.getIndices();
|
const auto& vecIndices = surfaceMesh.getIndices();
|
||||||
@ -71,29 +72,29 @@ public:
|
|||||||
// 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(PolyVox::Vertex< DataType >), vecVertices.data(), GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(MeshType::VertexType), 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(MeshType::IndexType), vecIndices.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
// Every surface extractor outputs valid positions for the vertices, so tell OpenGL how these are laid out
|
// Every surface extractor outputs valid positions for the vertices, so tell OpenGL how these are laid out
|
||||||
glEnableVertexAttribArray(0); // Attrib '0' is the vertex positions
|
glEnableVertexAttribArray(0); // Attrib '0' is the vertex positions
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(PolyVox::Vertex< DataType >), (GLvoid*)(offsetof(PolyVox::Vertex< DataType >, position))); //take the first 3 floats from every sizeof(decltype(vecVertices)::value_type)
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(MeshType::VertexType), (GLvoid*)(offsetof(MeshType::VertexType, position))); //take the first 3 floats from every sizeof(decltype(vecVertices)::value_type)
|
||||||
|
|
||||||
// Some surface extractors also generate normals, so tell OpenGL how these are laid out. If a surface extractor
|
// Some surface extractors also generate normals, so tell OpenGL how these are laid out. If a surface extractor
|
||||||
// does not generate normals then nonsense values are written into the buffer here and sghould be ignored by the
|
// does not generate normals then nonsense values are written into the buffer here and sghould be ignored by the
|
||||||
// shader. This is mostly just to simplify this example code - in a real application you will know whether your
|
// shader. This is mostly just to simplify this example code - in a real application you will know whether your
|
||||||
// chosen surface extractor generates normals and can skip uploading them if not.
|
// chosen surface extractor generates normals and can skip uploading them if not.
|
||||||
glEnableVertexAttribArray(1); // Attrib '1' is the vertex normals.
|
glEnableVertexAttribArray(1); // Attrib '1' is the vertex normals.
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(PolyVox::Vertex< DataType >), (GLvoid*)(offsetof(PolyVox::Vertex< DataType >, normal)));
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(MeshType::VertexType), (GLvoid*)(offsetof(MeshType::VertexType, normal)));
|
||||||
|
|
||||||
// Finally a surface extractor will probably output additional data. This is highly application dependant. For this example code
|
// Finally a surface extractor will probably output additional data. This is highly application dependant. For this example code
|
||||||
// we're just uploading it as a set of bytes which we can read individually, but real code will want to do something specialised here.
|
// we're just uploading it as a set of bytes which we can read individually, but real code will want to do something specialised here.
|
||||||
glEnableVertexAttribArray(2); //We're talking about shader attribute '2'
|
glEnableVertexAttribArray(2); //We're talking about shader attribute '2'
|
||||||
GLint size = (std::min)(sizeof(DataType), size_t(4)); // Can't upload more that 4 components (vec4 is GLSL's biggest type)
|
GLint size = (std::min)(sizeof(MeshType::VertexType::DataType), size_t(4)); // Can't upload more that 4 components (vec4 is GLSL's biggest type)
|
||||||
glVertexAttribIPointer(2, size, GL_UNSIGNED_BYTE, sizeof(PolyVox::Vertex< DataType >), (GLvoid*)(offsetof(PolyVox::Vertex< DataType >, data)));
|
glVertexAttribIPointer(2, size, GL_UNSIGNED_BYTE, sizeof(MeshType::VertexType), (GLvoid*)(offsetof(MeshType::VertexType, data)));
|
||||||
|
|
||||||
// We're done uploading and can now unbind.
|
// We're done uploading and can now unbind.
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
@ -103,6 +104,9 @@ public:
|
|||||||
meshData.translation = QVector3D(translation.getX(), translation.getY(), translation.getZ());
|
meshData.translation = QVector3D(translation.getX(), translation.getY(), translation.getZ());
|
||||||
meshData.scale = scale;
|
meshData.scale = scale;
|
||||||
|
|
||||||
|
// Set 16 or 32-bit index buffer size.
|
||||||
|
meshData.indexType = sizeof(MeshType::IndexType) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
|
||||||
|
|
||||||
// Now add the mesh to the list of meshes to render.
|
// Now add the mesh to the list of meshes to render.
|
||||||
addMeshData(meshData);
|
addMeshData(meshData);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user