diff --git a/examples/SmoothLOD/OpenGLWidget.cpp b/examples/SmoothLOD/OpenGLWidget.cpp index c11a2924..52aaaf65 100644 --- a/examples/SmoothLOD/OpenGLWidget.cpp +++ b/examples/SmoothLOD/OpenGLWidget.cpp @@ -34,6 +34,28 @@ void OpenGLWidget::setSurfaceMeshToRender(const PolyVox::SurfaceMesh& surfaceMesh) +{ + //Convienient access to the vertices and indices + const vector& vecIndices = surfaceMesh.getIndices(); + const vector& vecVertices = surfaceMesh.getVertices(); + + //Build an OpenGL index buffer + glGenBuffers(1, &indexBufferLow); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferLow); + const GLvoid* pIndices = static_cast(&(vecIndices[0])); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, vecIndices.size() * sizeof(uint32_t), pIndices, GL_STATIC_DRAW); + + //Build an OpenGL vertex buffer + glGenBuffers(1, &vertexBufferLow); + glBindBuffer(GL_ARRAY_BUFFER, vertexBufferLow); + const GLvoid* pVertices = static_cast(&(vecVertices[0])); + glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(PositionMaterialNormal), pVertices, GL_STATIC_DRAW); + + m_uBeginIndexLow = 0; + m_uEndIndexLow = vecIndices.size(); +} + void OpenGLWidget::initializeGL() { //We need GLEW to access recent OpenGL functionality @@ -125,6 +147,16 @@ void OpenGLWidget::paintGL() glNormalPointer(GL_FLOAT, sizeof(PositionMaterialNormal), (GLvoid*)12); glDrawRangeElements(GL_TRIANGLES, m_uBeginIndex, m_uEndIndex-1, m_uEndIndex - m_uBeginIndex, GL_UNSIGNED_INT, 0); + + //Bind the index buffer + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferLow); + + //Bind the vertex buffer + glBindBuffer(GL_ARRAY_BUFFER, vertexBufferLow); + glVertexPointer(3, GL_FLOAT, sizeof(PositionMaterialNormal), 0); + glNormalPointer(GL_FLOAT, sizeof(PositionMaterialNormal), (GLvoid*)12); + + glDrawRangeElements(GL_TRIANGLES, m_uBeginIndexLow, m_uEndIndexLow-1, m_uEndIndexLow - m_uBeginIndexLow, GL_UNSIGNED_INT, 0); GLenum errCode = glGetError(); if(errCode != GL_NO_ERROR) diff --git a/examples/SmoothLOD/OpenGLWidget.h b/examples/SmoothLOD/OpenGLWidget.h index 199a33ed..4dc465e0 100644 --- a/examples/SmoothLOD/OpenGLWidget.h +++ b/examples/SmoothLOD/OpenGLWidget.h @@ -42,6 +42,7 @@ public: //Convert a SrfaceMesh to OpenGL index/vertex buffers void setSurfaceMeshToRender(const PolyVox::SurfaceMesh& surfaceMesh); + void setSurfaceMeshToRenderLowLOD(const PolyVox::SurfaceMesh& surfaceMesh); protected: //Qt OpenGL functions @@ -53,10 +54,16 @@ private: //Index/vertex buffer data GLuint m_uBeginIndex; GLuint m_uEndIndex; - GLuint noOfIndices; + //GLuint noOfIndices; GLuint indexBuffer; GLuint vertexBuffer; + GLuint m_uBeginIndexLow; + GLuint m_uEndIndexLow; + //GLuint noOfIndicesLow; + GLuint indexBufferLow; + GLuint vertexBufferLow; + //Mouse data QPoint m_LastFrameMousePos; QPoint m_CurrentMousePos; diff --git a/examples/SmoothLOD/main.cpp b/examples/SmoothLOD/main.cpp index 13726dc1..b1fa7f8d 100644 --- a/examples/SmoothLOD/main.cpp +++ b/examples/SmoothLOD/main.cpp @@ -91,20 +91,26 @@ int main(int argc, char *argv[]) smoothRegion(volData, volData.getEnclosingRegion()); smoothRegion(volData, volData.getEnclosingRegion()); - RawVolume volDataLowLOD(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(31, 31, 31))); + RawVolume volDataLowLOD(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(15, 31, 31))); - VolumeResampler volumeResampler(&volData, volData.getEnclosingRegion(), &volDataLowLOD, volDataLowLOD.getEnclosingRegion()); + VolumeResampler volumeResampler(&volData, PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(31, 63, 63)), &volDataLowLOD, volDataLowLOD.getEnclosingRegion()); volumeResampler.execute(); //Extract the surface - SurfaceMesh mesh; - SurfaceExtractor surfaceExtractor(&volDataLowLOD, volDataLowLOD.getEnclosingRegion(), &mesh); + SurfaceMesh meshLowLOD; + SurfaceExtractor surfaceExtractor(&volDataLowLOD, volDataLowLOD.getEnclosingRegion(), &meshLowLOD); surfaceExtractor.execute(); + meshLowLOD.scaleVertices(2.0f); - mesh.scaleVertices(2.0f); + //Extract the surface + SurfaceMesh meshHighLOD; + SurfaceExtractor surfaceExtractorHigh(&volData, PolyVox::Region(Vector3DInt32(32,0,0), Vector3DInt32(63, 63, 63)), &meshHighLOD); + surfaceExtractorHigh.execute(); + meshHighLOD.translateVertices(Vector3DFloat(32, 0, 0)); //Pass the surface to the OpenGL window - openGLWidget.setSurfaceMeshToRender(mesh); + openGLWidget.setSurfaceMeshToRender(meshHighLOD); + openGLWidget.setSurfaceMeshToRenderLowLOD(meshLowLOD); //Run the message pump. return app.exec(); diff --git a/library/PolyVoxCore/include/PolyVoxCore/SurfaceMesh.h b/library/PolyVoxCore/include/PolyVoxCore/SurfaceMesh.h index a9d80947..d51b399f 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SurfaceMesh.h +++ b/library/PolyVoxCore/include/PolyVoxCore/SurfaceMesh.h @@ -64,6 +64,7 @@ namespace PolyVox bool isEmpty(void) const; void scaleVertices(float amount); + void translateVertices(const Vector3DFloat& amount); //THESE FUNCTIONS TO BE REMOVED IN THE FUTURE. OR AT LEAST MOVED OUT OF THIS CLASS INTO FREE FUNCTIONS. //THEY ARE CAUSING PROBLEMS WITH THE SWIG BINDINGS. THE FUNCTIONS REGARDING NORMALS MAKE NO SENSE WHEN diff --git a/library/PolyVoxCore/include/PolyVoxCore/SurfaceMesh.inl b/library/PolyVoxCore/include/PolyVoxCore/SurfaceMesh.inl index d846bd01..033388c1 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SurfaceMesh.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/SurfaceMesh.inl @@ -475,4 +475,16 @@ namespace PolyVox m_vecVertices[ct].setPosition(position); } } + + template + void SurfaceMesh::translateVertices(const Vector3DFloat& amount) + { + for(uint32_t ct = 0; ct < m_vecVertices.size(); ct++) + { + //TODO: Should rethink accessors here to provide faster access + Vector3DFloat position = m_vecVertices[ct].getPosition(); + position += amount; + m_vecVertices[ct].setPosition(position); + } + } } \ No newline at end of file diff --git a/library/PolyVoxCore/include/PolyVoxCore/VolumeResampler.h b/library/PolyVoxCore/include/PolyVoxCore/VolumeResampler.h index 99167a73..fce4c5e9 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/VolumeResampler.h +++ b/library/PolyVoxCore/include/PolyVoxCore/VolumeResampler.h @@ -39,6 +39,7 @@ namespace PolyVox private: void resampleSameSize(); void resampleHalfSize(); + void resampleArbitrary(); //Source data SrcVolumeType* m_pVolSrc; diff --git a/library/PolyVoxCore/include/PolyVoxCore/VolumeResampler.inl b/library/PolyVoxCore/include/PolyVoxCore/VolumeResampler.inl index 10f551dc..6912581e 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/VolumeResampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/VolumeResampler.inl @@ -35,8 +35,26 @@ namespace PolyVox template< template class SrcVolumeType, template class DestVolumeType, typename VoxelType> void VolumeResampler::execute() { - //resampleSameSize(); - resampleHalfSize(); + int32_t uSrcWidth = m_regSrc.getUpperCorner().getX() - m_regSrc.getLowerCorner().getX() + 1; + int32_t uSrcHeight = m_regSrc.getUpperCorner().getY() - m_regSrc.getLowerCorner().getY() + 1; + int32_t uSrcDepth = m_regSrc.getUpperCorner().getZ() - m_regSrc.getLowerCorner().getZ() + 1; + + int32_t uDstWidth = m_regDst.getUpperCorner().getX() - m_regDst.getLowerCorner().getX() + 1; + int32_t uDstHeight = m_regDst.getUpperCorner().getY() - m_regDst.getLowerCorner().getY() + 1; + int32_t uDstDepth = m_regDst.getUpperCorner().getZ() - m_regDst.getLowerCorner().getZ() + 1; + + if((uSrcWidth == uDstWidth) && (uSrcHeight == uDstHeight) && (uSrcDepth == uDstDepth)) + { + resampleSameSize(); + } + else if((uSrcWidth == uDstWidth * 2) && (uSrcHeight == uDstHeight * 2) && (uSrcDepth == uDstDepth * 2)) + { + resampleHalfSize(); + } + else + { + resampleArbitrary(); + } } template< template class SrcVolumeType, template class DestVolumeType, typename VoxelType> @@ -92,4 +110,11 @@ namespace PolyVox } } } + + template< template class SrcVolumeType, template class DestVolumeType, typename VoxelType> + void VolumeResampler::resampleArbitrary() + { + //ARBITRARY RESAMPLING NOT YET IMPLEMENTED. + assert(false); + } } \ No newline at end of file