Updated basic example and associated documentation.

This commit is contained in:
David Williams 2010-08-21 11:55:15 +00:00
parent fdf21f7846
commit 6d58348297
3 changed files with 20 additions and 53 deletions

View File

@ -7,35 +7,7 @@ Contents:
:maxdepth: 1
install
Introduction
------------
This tutorial covers the basic use of the PolyVox API. After reading this tutorial you should have a good idea how to create a PolyVox volume and fill it with data, extract a triangle mesh representing the surface, and render the result. This tutorial assumes you are already familiar with the basic concepts behind PolyVox (see the :doc:`principles of polyvox <principles>` document if not), and are reasonably confident with 3D graphics and C++. It also assumes you have already got PolyVox installed on your system, if this is not the case then please consult :doc:`installation guide <install>`.
The code samples and text in this tutorial correspond directly to the basic OpenGL example. This example uses the Qt toolkit for window and input handling, and for providing an OpenGL context to render into. In this tutorial we will omit code which performs these tasks and will instead focus on on the PolyVox code. You can consult the `Qt documentation <http://doc.qt.nokia.com/latest/>`_ if you want more information about these other aspects of the system.
Creating a volume
-----------------
*Mention required headers*
The most fundamental construct when working with PolyVox is that of the volume. This is represented by the :polyvox:`Volume` class and stores a 3D grid of voxels. Our basic example application creates a volume with the following line of code:
.. code-block:: c++
Volume<MaterialDensityPair44> volData(64, 64, 64);
As can be seen, the Volume class is templated upon the voxel type. This means it is straight forward to create a volume of integers, floats, or a custom voxel type (see the :polyvox:`Volume documentation <PolyVox::Volume>` for more details). In this particular case we have created a volume in which each voxel is an instance of :polyvox:`MaterialDensityPair44`. Each instance of MaterialDensityPair44 holds both a material and a density and uses four bits of data for each. This means that both the material and the density have a range of 0-15, and each voxel requires one byte of storage. For more information about materials and densities please consult the :doc:`principles of polyvox <principles>` document.
Each voxel is initialised using its default constructor, which in the case of MaterialDensityPair44 will mean that both the material and the density are set to zero. This corresponds to a volume full of empty space because the density of each voxel is below the threshold.
Extracting the surface
----------------------
Rendering the surface
---------------------
basicexample
Indices and tables

View File

@ -14,30 +14,24 @@ OpenGLWidget::OpenGLWidget(QWidget *parent)
void OpenGLWidget::setSurfaceMeshToRender(const PolyVox::SurfaceMesh& surfaceMesh)
{
//Sanity check that we have something to render.
if(surfaceMesh.getVertices().empty() || surfaceMesh.getIndices().empty())
{
return;
}
//Convienient access to the vertices and indices
vector<uint32_t>& vecIndices = const_cast<vector<uint32_t>&>(surfaceMesh.getIndices());
vector<SurfaceVertex>& vecVertices = const_cast<vector<SurfaceVertex>&>(surfaceMesh.getVertices());
const vector<uint32_t>& vecIndices = surfaceMesh.getIndices();
const vector<SurfaceVertex>& vecVertices = surfaceMesh.getVertices();
//Build an OpenGL index buffer
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
GLvoid* pIndices = static_cast<GLvoid*>(&(vecIndices[0]));
const GLvoid* pIndices = static_cast<const GLvoid*>(&(vecIndices[0]));
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vecIndices.size() * sizeof(uint32_t), pIndices, GL_STATIC_DRAW);
//Build an OpenGL vertex buffer
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
GLvoid* pVertices = static_cast<GLvoid*>(&(vecVertices[0]));
const GLvoid* pVertices = static_cast<const GLvoid*>(&(vecVertices[0]));
glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(SurfaceVertex), pVertices, GL_STATIC_DRAW);
m_uBeginIndex = surfaceMesh.m_vecLodRecords[0].beginIndex;
m_uEndIndex = surfaceMesh.m_vecLodRecords[0].endIndex;
m_uBeginIndex = 0;
m_uEndIndex = vecIndices.size();
}
void OpenGLWidget::initializeGL()

View File

@ -33,7 +33,7 @@ freely, subject to the following restrictions:
//Use the PolyVox namespace
using namespace PolyVox;
void createSphereInVolume(Volume<MaterialDensityPair44>& volData, float fRadius, uint8_t uValue)
void createSphereInVolume(Volume<MaterialDensityPair44>& volData, float fRadius)
{
//This vector hold the position of the center of the volume
Vector3DFloat v3dVolCenter(volData.getWidth() / 2, volData.getHeight() / 2, volData.getDepth() / 2);
@ -50,20 +50,21 @@ void createSphereInVolume(Volume<MaterialDensityPair44>& volData, float fRadius,
//And compute how far the current position is from the center of the volume
float fDistToCenter = (v3dCurrentPos - v3dVolCenter).length();
//Default to values representing empty space.
uint8_t uMaterial = 0;
uint8_t uDensity = MaterialDensityPair44::getMinDensity();
//If the current voxel is less than 'radius' units from the center then we make it solid.
if(fDistToCenter <= fRadius)
{
uMaterial = 1;
uDensity = MaterialDensityPair44::getMaxDensity();
}
//Our new density value
uint8_t uDensity = MaterialDensityPair44::getMaxDensity();
//Wrte the voxel value into the volume
MaterialDensityPair44 voxel(uMaterial, uDensity);
volData.setVoxelAt(x, y, z, voxel);
//Get the old voxel
MaterialDensityPair44 voxel = volData.getVoxelAt(x,y,z);
//Modify the density
voxel.setDensity(uDensity);
//Wrte the voxel value into the volume
volData.setVoxelAt(x, y, z, voxel);
}
}
}
}
@ -78,7 +79,7 @@ int main(int argc, char *argv[])
//Create an empty volume and then place a sphere in it
Volume<MaterialDensityPair44> volData(64, 64, 64);
createSphereInVolume(volData, 30, 1);
createSphereInVolume(volData, 30);
//Extract the surface
SurfaceExtractor<MaterialDensityPair44> surfaceExtractor(volData);