Added paging example and reverted basic example back to it's previous state.

This commit is contained in:
David Williams 2011-03-20 22:42:05 +00:00
parent 708b142702
commit 40295c84b1
12 changed files with 27957 additions and 542 deletions

View File

@ -40,8 +40,10 @@ ADD_SUBDIRECTORY(library)
OPTION(ENABLE_EXAMPLES "Should the examples be built" ON)
IF(ENABLE_EXAMPLES)
ADD_SUBDIRECTORY(examples/Basic)
ADD_SUBDIRECTORY(examples/Paging)
ADD_SUBDIRECTORY(examples/OpenGL)
ADD_DEPENDENCIES(BasicExample PolyVoxCore PolyVoxUtil)
ADD_DEPENDENCIES(PagingExample PolyVoxCore PolyVoxUtil)
ADD_DEPENDENCIES(OpenGLExample PolyVoxCore PolyVoxUtil)
ENDIF(ENABLE_EXAMPLES)

View File

@ -7,9 +7,6 @@ using namespace std;
OpenGLWidget::OpenGLWidget(QWidget *parent)
:QGLWidget(parent)
,m_uBeginIndex(0)
,m_uEndIndex(0)
,noOfIndices(0)
,m_xRotation(0)
,m_yRotation(0)
{
@ -17,12 +14,6 @@ OpenGLWidget::OpenGLWidget(QWidget *parent)
void OpenGLWidget::setSurfaceMeshToRender(const PolyVox::SurfaceMesh<PositionMaterialNormal>& surfaceMesh)
{
if((surfaceMesh.getNoOfIndices() == 0) || (surfaceMesh.getNoOfVertices() == 0))
{
//We don't have a valid mesh
return;
}
//Convienient access to the vertices and indices
const vector<uint32_t>& vecIndices = surfaceMesh.getIndices();
const vector<PositionMaterialNormal>& vecVertices = surfaceMesh.getVertices();
@ -41,7 +32,6 @@ void OpenGLWidget::setSurfaceMeshToRender(const PolyVox::SurfaceMesh<PositionMat
m_uBeginIndex = 0;
m_uEndIndex = vecIndices.size();
noOfIndices = surfaceMesh.getNoOfIndices();
}
void OpenGLWidget::initializeGL()
@ -80,29 +70,23 @@ void OpenGLWidget::resizeGL(int w, int h)
//Set up the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float frustumSize = 128.0f * 1.7f; //Half the volume diagonal
float frustumSize = 32.0f; //Half the volume size
float aspect = static_cast<float>(width()) / static_cast<float>(height());
glOrtho(frustumSize*aspect, -frustumSize*aspect, frustumSize, -frustumSize, 10.0, 10000);
glOrtho(frustumSize*aspect, -frustumSize*aspect, frustumSize, -frustumSize, 1.0, 1000);
}
void OpenGLWidget::paintGL()
{
if(noOfIndices == 0)
{
//Nothing to render
return;
}
//Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Set up the viewing transformation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5000.0f); //Centre volume and move back
glTranslatef(0.0f,0.0f,-100.0f); //Centre volume and move back
glRotatef(m_xRotation, 1.0f, 0.0f, 0.0f);
glRotatef(m_yRotation, 0.0f, 1.0f, 0.0f);
glTranslatef(-128.0f,-128.0f,-128.0f); //Centre volume and move back
glTranslatef(-32.0f,-32.0f,-32.0f); //Centre volume and move back
//Bind the index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

View File

@ -25,7 +25,6 @@ freely, subject to the following restrictions:
#include "MaterialDensityPair.h"
#include "CubicSurfaceExtractorWithNormals.h"
#include "SurfaceExtractor.h"
#include "SurfaceMesh.h"
#include "Volume.h"
@ -34,452 +33,17 @@ freely, subject to the following restrictions:
//Use the PolyVox namespace
using namespace PolyVox;
#include <stdlib.h>
#define SAMPLE_SIZE 1024
class Perlin
{
public:
Perlin(int octaves,float freq,float amp,int seed);
float Get(float x,float y)
{
float vec[2];
vec[0] = x;
vec[1] = y;
return perlin_noise_2D(vec);
};
float Get3D(float x,float y,float z)
{
float vec[3];
vec[0] = x;
vec[1] = y;
vec[2] = z;
return perlin_noise_3D(vec);
};
private:
void init_perlin(int n,float p);
float perlin_noise_2D(float vec[2]);
float perlin_noise_3D(float vec[3]);
float noise1(float arg);
float noise2(float vec[2]);
float noise3(float vec[3]);
void normalize2(float v[2]);
void normalize3(float v[3]);
void init(void);
int mOctaves;
float mFrequency;
float mAmplitude;
int mSeed;
int p[SAMPLE_SIZE + SAMPLE_SIZE + 2];
float g3[SAMPLE_SIZE + SAMPLE_SIZE + 2][3];
float g2[SAMPLE_SIZE + SAMPLE_SIZE + 2][2];
float g1[SAMPLE_SIZE + SAMPLE_SIZE + 2];
bool mStart;
};
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define B SAMPLE_SIZE
#define BM (SAMPLE_SIZE-1)
#define N 0x1000
#define NP 12 /* 2^N */
#define NM 0xfff
#define s_curve(t) ( t * t * (3.0f - 2.0f * t) )
#define lerp(t, a, b) ( a + t * (b - a) )
#define setup(i,b0,b1,r0,r1)\
t = vec[i] + N;\
b0 = ((int)t) & BM;\
b1 = (b0+1) & BM;\
r0 = t - (int)t;\
r1 = r0 - 1.0f;
float Perlin::noise1(float arg)
{
int bx0, bx1;
float rx0, rx1, sx, t, u, v, vec[1];
vec[0] = arg;
if (mStart)
{
srand(mSeed);
mStart = false;
init();
}
setup(0, bx0,bx1, rx0,rx1);
sx = s_curve(rx0);
u = rx0 * g1[ p[ bx0 ] ];
v = rx1 * g1[ p[ bx1 ] ];
return lerp(sx, u, v);
}
float Perlin::noise2(float vec[2])
{
int bx0, bx1, by0, by1, b00, b10, b01, b11;
float rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v;
int i, j;
if (mStart)
{
srand(mSeed);
mStart = false;
init();
}
setup(0,bx0,bx1,rx0,rx1);
setup(1,by0,by1,ry0,ry1);
i = p[bx0];
j = p[bx1];
b00 = p[i + by0];
b10 = p[j + by0];
b01 = p[i + by1];
b11 = p[j + by1];
sx = s_curve(rx0);
sy = s_curve(ry0);
#define at2(rx,ry) ( rx * q[0] + ry * q[1] )
q = g2[b00];
u = at2(rx0,ry0);
q = g2[b10];
v = at2(rx1,ry0);
a = lerp(sx, u, v);
q = g2[b01];
u = at2(rx0,ry1);
q = g2[b11];
v = at2(rx1,ry1);
b = lerp(sx, u, v);
return lerp(sy, a, b);
}
float Perlin::noise3(float vec[3])
{
int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
float rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
int i, j;
if (mStart)
{
srand(mSeed);
mStart = false;
init();
}
setup(0, bx0,bx1, rx0,rx1);
setup(1, by0,by1, ry0,ry1);
setup(2, bz0,bz1, rz0,rz1);
i = p[ bx0 ];
j = p[ bx1 ];
b00 = p[ i + by0 ];
b10 = p[ j + by0 ];
b01 = p[ i + by1 ];
b11 = p[ j + by1 ];
t = s_curve(rx0);
sy = s_curve(ry0);
sz = s_curve(rz0);
#define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
a = lerp(t, u, v);
q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
b = lerp(t, u, v);
c = lerp(sy, a, b);
q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
a = lerp(t, u, v);
q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
b = lerp(t, u, v);
d = lerp(sy, a, b);
return lerp(sz, c, d);
}
void Perlin::normalize2(float v[2])
{
float s;
s = (float)sqrt(v[0] * v[0] + v[1] * v[1]);
s = 1.0f/s;
v[0] = v[0] * s;
v[1] = v[1] * s;
}
void Perlin::normalize3(float v[3])
{
float s;
s = (float)sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
s = 1.0f/s;
v[0] = v[0] * s;
v[1] = v[1] * s;
v[2] = v[2] * s;
}
void Perlin::init(void)
{
int i, j, k;
for (i = 0 ; i < B ; i++)
{
p[i] = i;
g1[i] = (float)((rand() % (B + B)) - B) / B;
for (j = 0 ; j < 2 ; j++)
g2[i][j] = (float)((rand() % (B + B)) - B) / B;
normalize2(g2[i]);
for (j = 0 ; j < 3 ; j++)
g3[i][j] = (float)((rand() % (B + B)) - B) / B;
normalize3(g3[i]);
}
while (--i)
{
k = p[i];
p[i] = p[j = rand() % B];
p[j] = k;
}
for (i = 0 ; i < B + 2 ; i++)
{
p[B + i] = p[i];
g1[B + i] = g1[i];
for (j = 0 ; j < 2 ; j++)
g2[B + i][j] = g2[i][j];
for (j = 0 ; j < 3 ; j++)
g3[B + i][j] = g3[i][j];
}
}
float Perlin::perlin_noise_2D(float vec[2])
{
int terms = mOctaves;
float freq = mFrequency;
float result = 0.0f;
float amp = mAmplitude;
vec[0]*=mFrequency;
vec[1]*=mFrequency;
for( int i=0; i<terms; i++ )
{
result += noise2(vec)*amp;
vec[0] *= 2.0f;
vec[1] *= 2.0f;
amp*=0.5f;
}
return result;
}
float Perlin::perlin_noise_3D(float vec[3])
{
int terms = mOctaves;
float freq = mFrequency;
float result = 0.0f;
float amp = mAmplitude;
vec[0]*=mFrequency;
vec[1]*=mFrequency;
vec[2]*=mFrequency;
for( int i=0; i<terms; i++ )
{
result += noise3(vec)*amp;
vec[0] *= 2.0f;
vec[1] *= 2.0f;
vec[2] *= 2.0f;
amp*=0.5f;
}
return result;
}
Perlin::Perlin(int octaves,float freq,float amp,int seed)
{
mOctaves = octaves;
mFrequency = freq;
mAmplitude = amp;
mSeed = seed;
mStart = true;
}
void createPerlinVolumeSlow(Volume<MaterialDensityPair44>& volData)
{
Perlin perlin(2,8,1,234);
for(int z = 1; z < 256-1; z++)
{
std::cout << z << std::endl;
for(int y = 1; y < 256-1; y++)
{
for(int x = 1; x < 256-1; x++)
{
float perlinVal = perlin.Get3D(x /static_cast<float>(256-1), (y) / static_cast<float>(256-1), z / static_cast<float>(256-1));
perlinVal += 1.0f;
perlinVal *= 0.5f;
perlinVal *= MaterialDensityPair44::getMaxDensity();
MaterialDensityPair44 voxel;
voxel.setMaterial(245);
voxel.setDensity(perlinVal);
/*if(perlinVal < 0.0f)
{
voxel.setMaterial(245);
voxel.setDensity(MaterialDensityPair44::getMaxDensity());
}
else
{
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
}*/
volData.setVoxelAt(x, y, z, voxel);
}
}
}
}
/*void createPerlinVolumeFast(Volume<MaterialDensityPair44>& volData)
{
Perlin perlin(2,8,1,234);
for(int blockZ = 0; blockZ < volData.m_uDepthInBlocks; blockZ++)
{
std::cout << blockZ << std::endl;
for(int blockY = 0; blockY < volData.m_uHeightInBlocks; blockY++)
{
for(int blockX = 0; blockX < volData.m_uWidthInBlocks; blockX++)
{
for(int offsetz = 0; offsetz < volData.m_uBlockSideLength; offsetz++)
{
for(int offsety = 0; offsety < volData.m_uBlockSideLength; offsety++)
{
for(int offsetx = 0; offsetx < volData.m_uBlockSideLength; offsetx++)
{
int x = blockX * volData.m_uBlockSideLength + offsetx;
int y = blockY * volData.m_uBlockSideLength + offsety;
int z = blockZ * volData.m_uBlockSideLength + offsetz;
if((x == 0) || (x == volData.getWidth()-1)) continue;
if((y == 0) || (y == volData.getHeight()-1)) continue;
if((z == 0) || (z == volData.getDepth()-1)) continue;
float perlinVal = perlin.Get3D(x /static_cast<float>(volData.getWidth()-1), (y) / static_cast<float>(volData.getHeight()-1), z / static_cast<float>(volData.getDepth()-1));
MaterialDensityPair44 voxel;
if(perlinVal < 0.0f)
{
voxel.setMaterial(245);
voxel.setDensity(MaterialDensityPair44::getMaxDensity());
}
else
{
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
}
volData.setVoxelAt(x, y, z, voxel);
}
}
}
}
}
}
}*/
void createPerlinTerrain(Volume<MaterialDensityPair44>& volData)
{
Perlin perlin(2,2,1,234);
for(int x = 1; x < 255-1; x++)
{
if(x%(255/100) == 0) {
std::cout << "." << std::flush;
}
for(int y = 1; y < 255-1; y++)
{
float perlinVal = perlin.Get(x / static_cast<float>(255-1), y / static_cast<float>(255-1));
perlinVal += 1.0f;
perlinVal *= 0.5f;
perlinVal *= 255;
for(int z = 1; z < 255-1; z++)
{
MaterialDensityPair44 voxel;
if(z < perlinVal)
{
voxel.setMaterial(245);
voxel.setDensity(MaterialDensityPair44::getMaxDensity());
}
else
{
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
}
volData.setVoxelAt(x, y, z, voxel);
}
}
}
std::cout << std::endl;
}
void createSphereInVolume(Volume<MaterialDensityPair44>& volData, Vector3DFloat v3dVolCenter, float fRadius)
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);
int iRadius = fRadius;
Vector3DFloat v3dVolCenter(volData.getWidth() / 2, volData.getHeight() / 2, volData.getDepth() / 2);
//This three-level for loop iterates over every voxel in the volume
for (int z = v3dVolCenter.getZ() - iRadius; z <= v3dVolCenter.getZ() + iRadius; z++)
for (int z = 0; z < volData.getWidth(); z++)
{
for (int y = v3dVolCenter.getY() - iRadius; y <= v3dVolCenter.getY() + iRadius; y++)
for (int y = 0; y < volData.getHeight(); y++)
{
for (int x = v3dVolCenter.getX() - iRadius; x <= v3dVolCenter.getX() + iRadius; x++)
for (int x = 0; x < volData.getDepth(); x++)
{
//Store our current position as a vector...
Vector3DFloat v3dCurrentPos(x,y,z);
@ -506,51 +70,6 @@ void createSphereInVolume(Volume<MaterialDensityPair44>& volData, Vector3DFloat
}
}
void load(const ConstVolumeProxy<MaterialDensityPair44>& volume, const PolyVox::Region& reg)
{
Perlin perlin(2,2,1,234);
for(int x = reg.getLowerCorner().getX(); x <= reg.getUpperCorner().getX(); x++)
{
for(int y = reg.getLowerCorner().getY(); y <= reg.getUpperCorner().getY(); y++)
{
float perlinVal = perlin.Get(x / static_cast<float>(255-1), y / static_cast<float>(255-1));
perlinVal += 1.0f;
perlinVal *= 0.5f;
perlinVal *= 255;
for(int z = reg.getLowerCorner().getZ(); z <= reg.getUpperCorner().getZ(); z++)
{
MaterialDensityPair44 voxel;
if(z < perlinVal)
{
const int xpos = 50;
const int zpos = 100;
if((x-xpos)*(x-xpos) + (z-zpos)*(z-zpos) < 200) {
// tunnel
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
} else {
// solid
voxel.setMaterial(245);
voxel.setDensity(MaterialDensityPair44::getMaxDensity());
}
}
else
{
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
}
volume.setVoxelAt(x, y, z, voxel);
}
}
}
}
void unload(const ConstVolumeProxy<MaterialDensityPair44>& vol, const PolyVox::Region& reg)
{
std::cout << "warning unloading region: " << reg.getLowerCorner() << " -> " << reg.getUpperCorner() << std::endl;
}
int main(int argc, char *argv[])
{
//Create and show the Qt OpenGL window
@ -559,46 +78,13 @@ int main(int argc, char *argv[])
openGLWidget.show();
//Create an empty volume and then place a sphere in it
Volume<MaterialDensityPair44> volData(&load, &unload, 128);
//If these two lines don't compile, please try commenting them out and using the two lines after
//(you will need Boost for this). If you have to do this then please let us know in the forums as
//we rely on community feedback to keep the Boost version running.
//volData.dataRequiredHandler = &load;
//volData.dataOverflowHandler = &unload;
//volData.dataRequiredHandler = polyvox_bind(&load, polyvox_placeholder_1, polyvox_placeholder_2);
//volData.dataOverflowHandler = polyvox_bind(&unload, polyvox_placeholder_1, polyvox_placeholder_2);
//volData.setMaxNumberOfUncompressedBlocks(4096);
//createSphereInVolume(volData, 30);
//createPerlinTerrain(volData);
//createPerlinVolumeSlow(volData);
std::cout << "Memory usage: " << (volData.calculateSizeInBytes()/1024.0/1024.0) << "MB" << std::endl;
//volData.setBlockCacheSize(64);
std::cout << "Memory usage: " << (volData.calculateSizeInBytes()/1024.0/1024.0) << "MB" << std::endl;
std::cout << "Compression ratio: 1 to " << (1.0/(volData.calculateCompressionRatio())) << std::endl;
/*srand(12345);
for(int ct = 0; ct < 1000; ct++)
{
std::cout << ct << std::endl;
int x = rand() % volData.getWidth();
int y = rand() % volData.getHeight();
int z = rand() % volData.getDepth();
int r = rand() % 20;
createSphereInVolume(volData, Vector3DFloat(x,y,z), r);
}*/
Volume<MaterialDensityPair44> volData(64, 64, 64);
createSphereInVolume(volData, 30);
//Extract the surface
SurfaceMesh<PositionMaterialNormal> mesh;
//CubicSurfaceExtractorWithNormals<MaterialDensityPair44> surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh);
PolyVox::Region reg(Vector3DInt32(-255,0,0), Vector3DInt32(255,1024,255));
SurfaceExtractor<MaterialDensityPair44> surfaceExtractor(&volData, reg, &mesh);
//CubicSurfaceExtractorWithNormals<MaterialDensityPair44> surfaceExtractor(&volData, reg, &mesh);
CubicSurfaceExtractorWithNormals<MaterialDensityPair44> surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh);
surfaceExtractor.execute();
std::cout << "#vertices: " << mesh.getNoOfVertices() << std::endl;
//Pass the surface to the OpenGL window
openGLWidget.setSurfaceMeshToRender(mesh);

View File

@ -0,0 +1,54 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(PagingExample)
#Projects source files
SET(SRC_FILES
glew/glew.c
main.cpp
OpenGLWidget.cpp
)
#Projects headers files
SET(INC_FILES
glew/glew.h
glew/glxew.h
glew/wglew.h
OpenGLWidget.h
)
ADD_DEFINITIONS(-DGLEW_STATIC)
#"Sources" and "Headers" are the group names in Visual Studio.
#They may have other uses too...
SOURCE_GROUP("Sources" FILES ${SRC_FILES})
SOURCE_GROUP("Headers" FILES ${INC_FILES})
FIND_PACKAGE(OpenGL REQUIRED)
#Tell CMake the paths for OpenGL and for PolyVox (which is just relative to our current location)
INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR} ${PolyVoxCore_SOURCE_DIR}/include)
#There has to be a better way!
LINK_DIRECTORIES(${PolyVoxCore_BINARY_DIR}/debug ${PolyVoxCore_BINARY_DIR}/release)
#Build
ADD_EXECUTABLE(PagingExample ${SRC_FILES})
TARGET_LINK_LIBRARIES(PagingExample ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore)
#Install - Only install the example in Windows
IF(WIN32)
INSTALL(TARGETS PagingExample
RUNTIME DESTINATION Examples/OpenGL/bin
LIBRARY DESTINATION Examples/OpenGL/lib
ARCHIVE DESTINATION Examples/OpenGL/lib
COMPONENT example
)
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/../../release/PolyVoxCore.dll DESTINATION Examples/OpenGL/bin CONFIGURATIONS Release)
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/../../release/PolyVoxUtil.dll DESTINATION Examples/OpenGL/bin CONFIGURATIONS Release)
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/../../debug/PolyVoxCore.dll DESTINATION Examples/OpenGL/bin CONFIGURATIONS Debug)
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/../../debug/PolyVoxUtil.dll DESTINATION Examples/OpenGL/bin CONFIGURATIONS Debug)
ENDIF(WIN32)

View File

@ -0,0 +1,135 @@
#include "OpenGLWidget.h"
#include <QMouseEvent>
using namespace PolyVox;
using namespace std;
OpenGLWidget::OpenGLWidget(QWidget *parent)
:QGLWidget(parent)
,m_uBeginIndex(0)
,m_uEndIndex(0)
,noOfIndices(0)
,m_xRotation(0)
,m_yRotation(0)
{
}
void OpenGLWidget::setSurfaceMeshToRender(const PolyVox::SurfaceMesh<PositionMaterialNormal>& surfaceMesh)
{
if((surfaceMesh.getNoOfIndices() == 0) || (surfaceMesh.getNoOfVertices() == 0))
{
//We don't have a valid mesh
return;
}
//Convienient access to the vertices and indices
const vector<uint32_t>& vecIndices = surfaceMesh.getIndices();
const vector<PositionMaterialNormal>& vecVertices = surfaceMesh.getVertices();
//Build an OpenGL index buffer
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
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);
const GLvoid* pVertices = static_cast<const GLvoid*>(&(vecVertices[0]));
glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(PositionMaterialNormal), pVertices, GL_STATIC_DRAW);
m_uBeginIndex = 0;
m_uEndIndex = vecIndices.size();
noOfIndices = surfaceMesh.getNoOfIndices();
}
void OpenGLWidget::initializeGL()
{
//We need GLEW to access recent OpenGL functionality
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
std::cout << "GLEW Error: " << glewGetErrorString(err) << std::endl;
}
//Set up the clear colour
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
//Enable the depth buffer
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
//Anable smooth lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
//We'll be rendering with index/vertex arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
}
void OpenGLWidget::resizeGL(int w, int h)
{
//Setup the viewport
glViewport(0, 0, w, h);
//Set up the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float frustumSize = 128.0f * 1.7f; //Half the volume diagonal
float aspect = static_cast<float>(width()) / static_cast<float>(height());
glOrtho(frustumSize*aspect, -frustumSize*aspect, frustumSize, -frustumSize, 10.0, 10000);
}
void OpenGLWidget::paintGL()
{
if(noOfIndices == 0)
{
//Nothing to render
return;
}
//Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Set up the viewing transformation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5000.0f); //Centre volume and move back
glRotatef(m_xRotation, 1.0f, 0.0f, 0.0f);
glRotatef(m_yRotation, 0.0f, 1.0f, 0.0f);
glTranslatef(-128.0f,-128.0f,-128.0f); //Centre volume and move back
//Bind the index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
//Bind the vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexPointer(3, GL_FLOAT, sizeof(PositionMaterialNormal), 0);
glNormalPointer(GL_FLOAT, sizeof(PositionMaterialNormal), (GLvoid*)12);
glDrawRangeElements(GL_TRIANGLES, m_uBeginIndex, m_uEndIndex-1, m_uEndIndex - m_uBeginIndex, GL_UNSIGNED_INT, 0);
}
void OpenGLWidget::mousePressEvent(QMouseEvent* event)
{
m_CurrentMousePos = event->pos();
m_LastFrameMousePos = m_CurrentMousePos;
update();
}
void OpenGLWidget::mouseMoveEvent(QMouseEvent* event)
{
m_CurrentMousePos = event->pos();
QPoint diff = m_CurrentMousePos - m_LastFrameMousePos;
m_xRotation += diff.x();
m_yRotation += diff.y();
m_LastFrameMousePos = m_CurrentMousePos;
update();
}

View File

@ -0,0 +1,67 @@
/*******************************************************************************
Copyright (c) 2005-2009 David Williams
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*******************************************************************************/
#ifndef __BasicExample_OpenGLWidget_H__
#define __BasicExample_OpenGLWidget_H__
#include "SurfaceMesh.h"
#include "glew/glew.h"
#include <QGLWidget>
class OpenGLWidget : public QGLWidget
{
public:
//Constructor
OpenGLWidget(QWidget *parent);
//Mouse handling
void mouseMoveEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
//Convert a SrfaceMesh to OpenGL index/vertex buffers
void setSurfaceMeshToRender(const PolyVox::SurfaceMesh<PolyVox::PositionMaterialNormal>& surfaceMesh);
protected:
//Qt OpenGL functions
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
private:
//Index/vertex buffer data
GLuint m_uBeginIndex;
GLuint m_uEndIndex;
GLuint noOfIndices;
GLuint indexBuffer;
GLuint vertexBuffer;
//Mouse data
QPoint m_LastFrameMousePos;
QPoint m_CurrentMousePos;
int m_xRotation;
int m_yRotation;
};
#endif //__BasicExample_OpenGLWidget_H__

View File

@ -0,0 +1,73 @@
The OpenGL Extension Wrangler Library
Copyright (C) 2002-2007, Milan Ikits <milan ikits[]ieee org>
Copyright (C) 2002-2007, Marcelo E. Magallon <mmagallo[]debian org>
Copyright (C) 2002, Lev Povalahev
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name of the author may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
Mesa 3-D graphics library
Version: 7.0
Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copyright (c) 2007 The Khronos Group Inc.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and/or associated documentation files (the
"Materials"), to deal in the Materials without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Materials, and to
permit persons to whom the Materials are furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Materials.
THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.

12180
examples/Paging/glew/glew.c Normal file

File diff suppressed because it is too large Load Diff

12262
examples/Paging/glew/glew.h Normal file

File diff suppressed because it is too large Load Diff

1397
examples/Paging/glew/glxew.h Normal file

File diff suppressed because it is too large Load Diff

1165
examples/Paging/glew/wglew.h Normal file

File diff suppressed because it is too large Load Diff

610
examples/Paging/main.cpp Normal file
View File

@ -0,0 +1,610 @@
/*******************************************************************************
Copyright (c) 2005-2009 David Williams
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*******************************************************************************/
#include "OpenGLWidget.h"
#include "MaterialDensityPair.h"
#include "CubicSurfaceExtractorWithNormals.h"
#include "SurfaceExtractor.h"
#include "SurfaceMesh.h"
#include "Volume.h"
#include <QApplication>
//Use the PolyVox namespace
using namespace PolyVox;
#include <stdlib.h>
#define SAMPLE_SIZE 1024
class Perlin
{
public:
Perlin(int octaves,float freq,float amp,int seed);
float Get(float x,float y)
{
float vec[2];
vec[0] = x;
vec[1] = y;
return perlin_noise_2D(vec);
};
float Get3D(float x,float y,float z)
{
float vec[3];
vec[0] = x;
vec[1] = y;
vec[2] = z;
return perlin_noise_3D(vec);
};
private:
void init_perlin(int n,float p);
float perlin_noise_2D(float vec[2]);
float perlin_noise_3D(float vec[3]);
float noise1(float arg);
float noise2(float vec[2]);
float noise3(float vec[3]);
void normalize2(float v[2]);
void normalize3(float v[3]);
void init(void);
int mOctaves;
float mFrequency;
float mAmplitude;
int mSeed;
int p[SAMPLE_SIZE + SAMPLE_SIZE + 2];
float g3[SAMPLE_SIZE + SAMPLE_SIZE + 2][3];
float g2[SAMPLE_SIZE + SAMPLE_SIZE + 2][2];
float g1[SAMPLE_SIZE + SAMPLE_SIZE + 2];
bool mStart;
};
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define B SAMPLE_SIZE
#define BM (SAMPLE_SIZE-1)
#define N 0x1000
#define NP 12 /* 2^N */
#define NM 0xfff
#define s_curve(t) ( t * t * (3.0f - 2.0f * t) )
#define lerp(t, a, b) ( a + t * (b - a) )
#define setup(i,b0,b1,r0,r1)\
t = vec[i] + N;\
b0 = ((int)t) & BM;\
b1 = (b0+1) & BM;\
r0 = t - (int)t;\
r1 = r0 - 1.0f;
float Perlin::noise1(float arg)
{
int bx0, bx1;
float rx0, rx1, sx, t, u, v, vec[1];
vec[0] = arg;
if (mStart)
{
srand(mSeed);
mStart = false;
init();
}
setup(0, bx0,bx1, rx0,rx1);
sx = s_curve(rx0);
u = rx0 * g1[ p[ bx0 ] ];
v = rx1 * g1[ p[ bx1 ] ];
return lerp(sx, u, v);
}
float Perlin::noise2(float vec[2])
{
int bx0, bx1, by0, by1, b00, b10, b01, b11;
float rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v;
int i, j;
if (mStart)
{
srand(mSeed);
mStart = false;
init();
}
setup(0,bx0,bx1,rx0,rx1);
setup(1,by0,by1,ry0,ry1);
i = p[bx0];
j = p[bx1];
b00 = p[i + by0];
b10 = p[j + by0];
b01 = p[i + by1];
b11 = p[j + by1];
sx = s_curve(rx0);
sy = s_curve(ry0);
#define at2(rx,ry) ( rx * q[0] + ry * q[1] )
q = g2[b00];
u = at2(rx0,ry0);
q = g2[b10];
v = at2(rx1,ry0);
a = lerp(sx, u, v);
q = g2[b01];
u = at2(rx0,ry1);
q = g2[b11];
v = at2(rx1,ry1);
b = lerp(sx, u, v);
return lerp(sy, a, b);
}
float Perlin::noise3(float vec[3])
{
int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
float rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
int i, j;
if (mStart)
{
srand(mSeed);
mStart = false;
init();
}
setup(0, bx0,bx1, rx0,rx1);
setup(1, by0,by1, ry0,ry1);
setup(2, bz0,bz1, rz0,rz1);
i = p[ bx0 ];
j = p[ bx1 ];
b00 = p[ i + by0 ];
b10 = p[ j + by0 ];
b01 = p[ i + by1 ];
b11 = p[ j + by1 ];
t = s_curve(rx0);
sy = s_curve(ry0);
sz = s_curve(rz0);
#define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
a = lerp(t, u, v);
q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
b = lerp(t, u, v);
c = lerp(sy, a, b);
q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
a = lerp(t, u, v);
q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
b = lerp(t, u, v);
d = lerp(sy, a, b);
return lerp(sz, c, d);
}
void Perlin::normalize2(float v[2])
{
float s;
s = (float)sqrt(v[0] * v[0] + v[1] * v[1]);
s = 1.0f/s;
v[0] = v[0] * s;
v[1] = v[1] * s;
}
void Perlin::normalize3(float v[3])
{
float s;
s = (float)sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
s = 1.0f/s;
v[0] = v[0] * s;
v[1] = v[1] * s;
v[2] = v[2] * s;
}
void Perlin::init(void)
{
int i, j, k;
for (i = 0 ; i < B ; i++)
{
p[i] = i;
g1[i] = (float)((rand() % (B + B)) - B) / B;
for (j = 0 ; j < 2 ; j++)
g2[i][j] = (float)((rand() % (B + B)) - B) / B;
normalize2(g2[i]);
for (j = 0 ; j < 3 ; j++)
g3[i][j] = (float)((rand() % (B + B)) - B) / B;
normalize3(g3[i]);
}
while (--i)
{
k = p[i];
p[i] = p[j = rand() % B];
p[j] = k;
}
for (i = 0 ; i < B + 2 ; i++)
{
p[B + i] = p[i];
g1[B + i] = g1[i];
for (j = 0 ; j < 2 ; j++)
g2[B + i][j] = g2[i][j];
for (j = 0 ; j < 3 ; j++)
g3[B + i][j] = g3[i][j];
}
}
float Perlin::perlin_noise_2D(float vec[2])
{
int terms = mOctaves;
float freq = mFrequency;
float result = 0.0f;
float amp = mAmplitude;
vec[0]*=mFrequency;
vec[1]*=mFrequency;
for( int i=0; i<terms; i++ )
{
result += noise2(vec)*amp;
vec[0] *= 2.0f;
vec[1] *= 2.0f;
amp*=0.5f;
}
return result;
}
float Perlin::perlin_noise_3D(float vec[3])
{
int terms = mOctaves;
float freq = mFrequency;
float result = 0.0f;
float amp = mAmplitude;
vec[0]*=mFrequency;
vec[1]*=mFrequency;
vec[2]*=mFrequency;
for( int i=0; i<terms; i++ )
{
result += noise3(vec)*amp;
vec[0] *= 2.0f;
vec[1] *= 2.0f;
vec[2] *= 2.0f;
amp*=0.5f;
}
return result;
}
Perlin::Perlin(int octaves,float freq,float amp,int seed)
{
mOctaves = octaves;
mFrequency = freq;
mAmplitude = amp;
mSeed = seed;
mStart = true;
}
void createPerlinVolumeSlow(Volume<MaterialDensityPair44>& volData)
{
Perlin perlin(2,8,1,234);
for(int z = 1; z < 256-1; z++)
{
std::cout << z << std::endl;
for(int y = 1; y < 256-1; y++)
{
for(int x = 1; x < 256-1; x++)
{
float perlinVal = perlin.Get3D(x /static_cast<float>(256-1), (y) / static_cast<float>(256-1), z / static_cast<float>(256-1));
perlinVal += 1.0f;
perlinVal *= 0.5f;
perlinVal *= MaterialDensityPair44::getMaxDensity();
MaterialDensityPair44 voxel;
voxel.setMaterial(245);
voxel.setDensity(perlinVal);
/*if(perlinVal < 0.0f)
{
voxel.setMaterial(245);
voxel.setDensity(MaterialDensityPair44::getMaxDensity());
}
else
{
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
}*/
volData.setVoxelAt(x, y, z, voxel);
}
}
}
}
/*void createPerlinVolumeFast(Volume<MaterialDensityPair44>& volData)
{
Perlin perlin(2,8,1,234);
for(int blockZ = 0; blockZ < volData.m_uDepthInBlocks; blockZ++)
{
std::cout << blockZ << std::endl;
for(int blockY = 0; blockY < volData.m_uHeightInBlocks; blockY++)
{
for(int blockX = 0; blockX < volData.m_uWidthInBlocks; blockX++)
{
for(int offsetz = 0; offsetz < volData.m_uBlockSideLength; offsetz++)
{
for(int offsety = 0; offsety < volData.m_uBlockSideLength; offsety++)
{
for(int offsetx = 0; offsetx < volData.m_uBlockSideLength; offsetx++)
{
int x = blockX * volData.m_uBlockSideLength + offsetx;
int y = blockY * volData.m_uBlockSideLength + offsety;
int z = blockZ * volData.m_uBlockSideLength + offsetz;
if((x == 0) || (x == volData.getWidth()-1)) continue;
if((y == 0) || (y == volData.getHeight()-1)) continue;
if((z == 0) || (z == volData.getDepth()-1)) continue;
float perlinVal = perlin.Get3D(x /static_cast<float>(volData.getWidth()-1), (y) / static_cast<float>(volData.getHeight()-1), z / static_cast<float>(volData.getDepth()-1));
MaterialDensityPair44 voxel;
if(perlinVal < 0.0f)
{
voxel.setMaterial(245);
voxel.setDensity(MaterialDensityPair44::getMaxDensity());
}
else
{
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
}
volData.setVoxelAt(x, y, z, voxel);
}
}
}
}
}
}
}*/
void createPerlinTerrain(Volume<MaterialDensityPair44>& volData)
{
Perlin perlin(2,2,1,234);
for(int x = 1; x < 255-1; x++)
{
if(x%(255/100) == 0) {
std::cout << "." << std::flush;
}
for(int y = 1; y < 255-1; y++)
{
float perlinVal = perlin.Get(x / static_cast<float>(255-1), y / static_cast<float>(255-1));
perlinVal += 1.0f;
perlinVal *= 0.5f;
perlinVal *= 255;
for(int z = 1; z < 255-1; z++)
{
MaterialDensityPair44 voxel;
if(z < perlinVal)
{
voxel.setMaterial(245);
voxel.setDensity(MaterialDensityPair44::getMaxDensity());
}
else
{
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
}
volData.setVoxelAt(x, y, z, voxel);
}
}
}
std::cout << std::endl;
}
void createSphereInVolume(Volume<MaterialDensityPair44>& volData, Vector3DFloat v3dVolCenter, float fRadius)
{
//This vector hold the position of the center of the volume
//Vector3DFloat v3dVolCenter(volData.getWidth() / 2, volData.getHeight() / 2, volData.getDepth() / 2);
int iRadius = fRadius;
//This three-level for loop iterates over every voxel in the volume
for (int z = v3dVolCenter.getZ() - iRadius; z <= v3dVolCenter.getZ() + iRadius; z++)
{
for (int y = v3dVolCenter.getY() - iRadius; y <= v3dVolCenter.getY() + iRadius; y++)
{
for (int x = v3dVolCenter.getX() - iRadius; x <= v3dVolCenter.getX() + iRadius; x++)
{
//Store our current position as a vector...
Vector3DFloat v3dCurrentPos(x,y,z);
//And compute how far the current position is from the center of the volume
float fDistToCenter = (v3dCurrentPos - v3dVolCenter).length();
//If the current voxel is less than 'radius' units from the center then we make it solid.
if(fDistToCenter <= fRadius)
{
//Our new density value
uint8_t uDensity = MaterialDensityPair44::getMaxDensity();
//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);
}
}
}
}
}
void load(const ConstVolumeProxy<MaterialDensityPair44>& volume, const PolyVox::Region& reg)
{
Perlin perlin(2,2,1,234);
for(int x = reg.getLowerCorner().getX(); x <= reg.getUpperCorner().getX(); x++)
{
for(int y = reg.getLowerCorner().getY(); y <= reg.getUpperCorner().getY(); y++)
{
float perlinVal = perlin.Get(x / static_cast<float>(255-1), y / static_cast<float>(255-1));
perlinVal += 1.0f;
perlinVal *= 0.5f;
perlinVal *= 255;
for(int z = reg.getLowerCorner().getZ(); z <= reg.getUpperCorner().getZ(); z++)
{
MaterialDensityPair44 voxel;
if(z < perlinVal)
{
const int xpos = 50;
const int zpos = 100;
if((x-xpos)*(x-xpos) + (z-zpos)*(z-zpos) < 200) {
// tunnel
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
} else {
// solid
voxel.setMaterial(245);
voxel.setDensity(MaterialDensityPair44::getMaxDensity());
}
}
else
{
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
}
volume.setVoxelAt(x, y, z, voxel);
}
}
}
}
void unload(const ConstVolumeProxy<MaterialDensityPair44>& vol, const PolyVox::Region& reg)
{
std::cout << "warning unloading region: " << reg.getLowerCorner() << " -> " << reg.getUpperCorner() << std::endl;
}
int main(int argc, char *argv[])
{
//Create and show the Qt OpenGL window
QApplication app(argc, argv);
OpenGLWidget openGLWidget(0);
openGLWidget.show();
//Create an empty volume and then place a sphere in it
Volume<MaterialDensityPair44> volData(&load, &unload, 128);
volData.setMaxNumberOfBlocksInMemory(4096);
volData.setMaxNumberOfUncompressedBlocks(64);
//If these two lines don't compile, please try commenting them out and using the two lines after
//(you will need Boost for this). If you have to do this then please let us know in the forums as
//we rely on community feedback to keep the Boost version running.
//volData.dataRequiredHandler = &load;
//volData.dataOverflowHandler = &unload;
//volData.dataRequiredHandler = polyvox_bind(&load, polyvox_placeholder_1, polyvox_placeholder_2);
//volData.dataOverflowHandler = polyvox_bind(&unload, polyvox_placeholder_1, polyvox_placeholder_2);
//volData.setMaxNumberOfUncompressedBlocks(4096);
//createSphereInVolume(volData, 30);
//createPerlinTerrain(volData);
//createPerlinVolumeSlow(volData);
std::cout << "Memory usage: " << (volData.calculateSizeInBytes()/1024.0/1024.0) << "MB" << std::endl;
//volData.setBlockCacheSize(64);
std::cout << "Memory usage: " << (volData.calculateSizeInBytes()/1024.0/1024.0) << "MB" << std::endl;
std::cout << "Compression ratio: 1 to " << (1.0/(volData.calculateCompressionRatio())) << std::endl;
/*srand(12345);
for(int ct = 0; ct < 1000; ct++)
{
std::cout << ct << std::endl;
int x = rand() % volData.getWidth();
int y = rand() % volData.getHeight();
int z = rand() % volData.getDepth();
int r = rand() % 20;
createSphereInVolume(volData, Vector3DFloat(x,y,z), r);
}*/
//Extract the surface
SurfaceMesh<PositionMaterialNormal> mesh;
//CubicSurfaceExtractorWithNormals<MaterialDensityPair44> surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh);
PolyVox::Region reg(Vector3DInt32(-255,0,0), Vector3DInt32(255,1024,255));
SurfaceExtractor<MaterialDensityPair44> surfaceExtractor(&volData, reg, &mesh);
//CubicSurfaceExtractorWithNormals<MaterialDensityPair44> surfaceExtractor(&volData, reg, &mesh);
surfaceExtractor.execute();
std::cout << "#vertices: " << mesh.getNoOfVertices() << std::endl;
//Pass the surface to the OpenGL window
openGLWidget.setSurfaceMeshToRender(mesh);
//Run the message pump.
return app.exec();
}