Volumes Of Fun
http://www.volumesoffun.com/phpBB3/

How do I setup a simple modifiable terrain in an Ogre scene?
http://www.volumesoffun.com/phpBB3/viewtopic.php?f=14&t=427
Page 2 of 2

Author:  MirceaKitsune [ Mon Oct 08, 2012 3:12 pm ]
Post subject:  Re: How do I setup a simple modifiable terrain in an Ogre sc

Ok. I followed the example you mentioned, and added the code that should create a sphere in the volume. It compiles fine but still doesn't render anything. Here is the relevant part of the code again:

Code:
#include "TutorialApplication.h"
#include <PolyVoxCore/SimpleInterface.h>
#include <PolyVoxCore/Material.h>
#include <PolyVoxCore/MeshDecimator.h>
#include <PolyVoxCore/Vector.h>

//Use the PolyVox namespace
using namespace PolyVox;
using namespace std;

void createSphereInVolume(SimpleVolume<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);

   for (int z = 0; z < volData.getWidth(); z++)
   {
      for (int y = 0; y < volData.getHeight(); y++)
      {
         for (int x = 0; x < volData.getDepth(); 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);
            }
         }
      }
   }
}

//-------------------------------------------------------------------------------------
TutorialApplication::TutorialApplication(void)
{
}
//-------------------------------------------------------------------------------------
TutorialApplication::~TutorialApplication(void)
{
}

//-------------------------------------------------------------------------------------
void TutorialApplication::createScene(void)
{
   typedef PolyVox::PositionMaterialNormal VertexType;
   SurfaceMesh <VertexType> mesh;
   SurfaceMesh <VertexType> mesh2;
   Region regValid(Vector3DInt32(0,0,0), Vector3DInt32(128,128,128));

   Ogre::ManualObject* ogreMesh;
   // create something to draw the PolyVox stuff to
   ogreMesh = mSceneMgr->createManualObject("PolyVox Mesh");
   // YES we do intend to change the mesh later -.-
   /*
   ogreMesh->setDynamic(true);
   ogreMesh->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST);
   {
      // do nothing, this will be updated
   }
   ogreMesh->end();
   */
   Ogre::SceneNode* ogreNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("testnode1", Ogre::Vector3(20, 0, 0));
   ogreNode->attachObject(ogreMesh);

   SimpleVolume<MaterialDensityPair44> volume(regValid);

   // now add some data to it
   createSphereInVolume(volume, 30);

   std::cout << "smoothing volume" << std::endl;
   // smoothRegion<MaterialDensityPair44>(volume, volume.getEnclosingRegion());
   std::cout << "updating volume surface" << std::endl;
   {
      PolyVox::SurfaceExtractor<SimpleVolume,MaterialDensityPair44> suf(&volume, volume.getEnclosingRegion(), &mesh);
      suf.execute();
   }
   std::cout << "decimating meshes" << std::endl;
   {
      PolyVox::MeshDecimator<PolyVox::PositionMaterialNormal> decim(&mesh, &mesh2);
      decim.execute();
   }
   // std::cout << "drawing mesh: " << mesh2.getNoOfVertices() << std::endl;
   ogreMesh->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST);
   {
      const vector<PolyVox::PositionMaterialNormal>& vecVertices = mesh2.getVertices();
      const vector<uint32_t>& vecIndices = mesh2.getIndices();
      unsigned int uLodLevel = 0;
      int beginIndex = mesh2.m_vecLodRecords[uLodLevel].beginIndex;
      int endIndex = mesh2.m_vecLodRecords[uLodLevel].endIndex;
      for(int index = beginIndex; index < endIndex; ++index) {
         const PolyVox::PositionMaterialNormal& vertex = vecVertices[vecIndices[index]];
         const PolyVox::Vector3DFloat& v3dVertexPos = vertex.getPosition();
         const PolyVox::Vector3DFloat& v3dVertexNormal = vertex.getNormal();
         //const Vector3DFloat v3dRegionOffset(uRegionX * g_uRegionSideLength, uRegionY * g_uRegionSideLength, uRegionZ * g_uRegionSideLength);
         const PolyVox::Vector3DFloat v3dFinalVertexPos = v3dVertexPos + static_cast<PolyVox::Vector3DFloat>(mesh2.m_Region.getLowerCorner());
         ogreMesh->position(v3dFinalVertexPos.getX(), v3dFinalVertexPos.getY(), v3dFinalVertexPos.getZ());
         ogreMesh->normal(v3dVertexNormal.getX(), v3dVertexNormal.getY(), v3dVertexNormal.getZ());
         uint8_t mat = vertex.getMaterial() + 0.5;
         uint8_t red = mat & 0xF0;
         uint8_t green = mat & 0x03;
         uint8_t blue = mat & 0x0C;
         ogreMesh->colour(red*2, green*4, blue*4);// just some random colors, I'm too lazy for hsv
      }
   }
   ogreMesh->end();

   mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
}

Author:  milliams [ Mon Oct 08, 2012 3:24 pm ]
Post subject:  Re: How do I setup a simple modifiable terrain in an Ogre sc

The first thing I would try is to uncomment the
Code:
std::cout << "drawing mesh: " << mesh2.getNoOfVertices() << std::endl;
line and make sure that the surface extractor is actually generating any vertices. Also, perhaps try doing it without the MeshDecimator for now to reduce the number of things happening at once.

Author:  MirceaKitsune [ Mon Oct 08, 2012 5:52 pm ]
Post subject:  Re: How do I setup a simple modifiable terrain in an Ogre sc

milliams wrote:
The first thing I would try is to uncomment the
Code:
std::cout << "drawing mesh: " << mesh2.getNoOfVertices() << std::endl;
line and make sure that the surface extractor is actually generating any vertices. Also, perhaps try doing it without the MeshDecimator for now to reduce the number of things happening at once.


Not sure why I had that line commented out... I usually only removed what caused compiling to fail. There's too much output in the console to know what it says however. How do I know if it generates any vertices?

I tried to comment out the decimation part ("PolyVox::MeshDecimator<PolyVox::PositionMaterialNormal> decim(&mesh, &mesh2);" and "decim.execute();"), but the application then crashes:

Code:
smoothing volume
updating volume surface
drawing mesh: 0
Segmentation fault

Author:  David Williams [ Tue Oct 09, 2012 8:18 am ]
Post subject:  Re: How do I setup a simple modifiable terrain in an Ogre sc

MirceaKitsune wrote:
There's too much output in the console to know what it says however. How do I know if it generates any vertices?


That line should only be output once. If you don't see it because of other output then try redirecting the output to a log file which you can then search.

MirceaKitsune wrote:
I tried to comment out the decimation part ("PolyVox::MeshDecimator<PolyVox::PositionMaterialNormal> decim(&mesh, &mesh2);" and "decim.execute();"), but the application then crashes:


Matt is correct that the MeshDecimator has some problems and we'll probably remove it in the future. However, in this case it is decimating 'mesh' and storing the result in 'mesh2' and you are then rendering 'mesh2'. If you take out the decimator then 'mesh2' is not getting any data, and so you would need to update the rest of the code to render 'mesh' instead.

Author:  MirceaKitsune [ Tue Oct 09, 2012 2:34 pm ]
Post subject:  Re: How do I setup a simple modifiable terrain in an Ogre sc

David Williams wrote:
That line should only be output once. If you don't see it because of other output then try redirecting the output to a log file which you can then search.

Matt is correct that the MeshDecimator has some problems and we'll probably remove it in the future. However, in this case it is decimating 'mesh' and storing the result in 'mesh2' and you are then rendering 'mesh2'. If you take out the decimator then 'mesh2' is not getting any data, and so you would need to update the rest of the code to render 'mesh' instead.


I removed the mesh decimator part again, and also renamed mesh2 to mesh. This time there was no crash but still nothing renders visibly. What does the mesh decimator do BTW... is it even needed? Also, I got to see the "drawing mesh" output and if I understand right there should be some vertices.

Code:
...
smoothing volume
updating volume surface
drawing mesh: 16926
*** Initializing OIS ***
...

Author:  David Williams [ Wed Oct 10, 2012 10:00 am ]
Post subject:  Re: How do I setup a simple modifiable terrain in an Ogre sc

MirceaKitsune wrote:
What does the mesh decimator do BTW... is it even needed?

It reduces the number of triangles in the mesh. It's basically a performance optimisation, but it has some issues so you should avoid it for now.
MirceaKitsune wrote:
Also, I got to see the "drawing mesh" output and if I understand right there should be some vertices.
Code:
...
smoothing volume
updating volume surface
drawing mesh: 16926
*** Initializing OIS ***
...

Ok, this looks good. The output 'drawing mesh: 16926' indicates that a mesh has indeed been created by PolyVox. I think the most likely reason that your not seeing anything being drawn is simplythat you are facing the wrong direction... have you tried looking around the scene? And are you sure that you haven't placed the camera inside the sphere (in which case you won't see it?

Based on your code, the sphere should be centered at (64, 64, 64) and have a radius of 30. So if you leave the camera at the origin and look at that position then you should see it. You might find it useful to load additional Ogre meshes into your scene to serve as markers... perhaps add an Ogre mesh at the origin and one where the sphere should be centered. If you can see both the markers you should be able to see the sphere.

Author:  MirceaKitsune [ Wed Oct 10, 2012 10:52 am ]
Post subject:  Re: How do I setup a simple modifiable terrain in an Ogre sc

Ok. Performance is important too so I hope the decimator will get improved. And yes, I tried looking and moving around, but no success. The ogre head example I started the code from worked (including moving the camera) so in this case I'll probably position the ogre head at the origin of the voxel sphere and see what I get then.

Page 2 of 2 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/