Add comments and tidying.

This commit is contained in:
David Williams 2014-05-26 16:31:12 +02:00
parent 2a072f9347
commit e9c8daa9cb
2 changed files with 26 additions and 13 deletions

View File

@ -26,27 +26,32 @@ void OpenGLWidget::setShader(QSharedPointer<QGLShaderProgram> shader)
void OpenGLWidget::setViewableRegion(Region viewableRegion) void OpenGLWidget::setViewableRegion(Region viewableRegion)
{ {
m_viewableRegion = viewableRegion; m_viewableRegion = viewableRegion;
// The user has specifed a new viewable region
// so we need to regenerate our camera matrix.
setupWorldToCameraMatrix(); setupWorldToCameraMatrix();
} }
void OpenGLWidget::mousePressEvent(QMouseEvent* event) void OpenGLWidget::mousePressEvent(QMouseEvent* event)
{ {
// Initialise these variables which will be used when the mouse actually moves.
m_CurrentMousePos = event->pos(); m_CurrentMousePos = event->pos();
m_LastFrameMousePos = m_CurrentMousePos; m_LastFrameMousePos = m_CurrentMousePos;
update();
} }
void OpenGLWidget::mouseMoveEvent(QMouseEvent* event) void OpenGLWidget::mouseMoveEvent(QMouseEvent* event)
{ {
// Update the x and y rotations based on the mouse movement.
m_CurrentMousePos = event->pos(); m_CurrentMousePos = event->pos();
QPoint diff = m_CurrentMousePos - m_LastFrameMousePos; QPoint diff = m_CurrentMousePos - m_LastFrameMousePos;
m_xRotation += diff.x(); m_xRotation += diff.x();
m_yRotation += diff.y(); m_yRotation += diff.y();
m_LastFrameMousePos = m_CurrentMousePos; m_LastFrameMousePos = m_CurrentMousePos;
// The camera rotation has changed so we need to regenerate the matrix.
setupWorldToCameraMatrix(); setupWorldToCameraMatrix();
// Re-render.
update(); update();
} }

View File

@ -52,7 +52,7 @@ public:
// Constructor // Constructor
OpenGLWidget(QWidget *parent); OpenGLWidget(QWidget *parent);
// Convert a SurfaceMesh 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 MeshType> template <typename MeshType>
void addMesh(const MeshType& 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)
{ {
@ -78,24 +78,32 @@ public:
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 // Every surface extractor outputs valid positions for the vertices, so tell OpenGL how these are laid out
glEnableVertexAttribArray(0); //We're talking about shader attribute '0' glEnableVertexAttribArray(0); // Attrib '0' is the vertex positions
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) 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)
glEnableVertexAttribArray(1); //We're talking about shader attribute '1' // 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
// 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.
glEnableVertexAttribArray(1); // Attrib '1' is the vertex normals.
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(MeshType::VertexType), (GLvoid*)(offsetof(MeshType::VertexType, 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
// 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(MeshType::VertexType::VoxelType), size_t(4)); GLint size = (std::min)(sizeof(MeshType::VertexType::VoxelType), size_t(4)); // Can't upload more that 4 components (vec4 is GLSL's biggest type)
glVertexAttribIPointer(2, size, GL_UNSIGNED_BYTE, sizeof(MeshType::VertexType), (GLvoid*)(offsetof(MeshType::VertexType, material))); glVertexAttribIPointer(2, size, GL_UNSIGNED_BYTE, sizeof(MeshType::VertexType), (GLvoid*)(offsetof(MeshType::VertexType, material)));
// We're done uploading and can now unbind.
glBindVertexArray(0); glBindVertexArray(0);
meshData.noOfIndices = vecIndices.size(); //Save this for the call to glDrawElements later // A few additional properties can be copied across for use during rendering.
meshData.noOfIndices = vecIndices.size();
meshData.translation = QVector3D(translation.getX(), translation.getY(), translation.getZ()); meshData.translation = QVector3D(translation.getX(), translation.getY(), translation.getZ());
meshData.scale = scale; meshData.scale = scale;
// Now add the mesh to the list of meshes to render.
mMeshData.push_back(meshData); mMeshData.push_back(meshData);
} }