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

Trouble creating a Irrlicht mesh from CubicSurfaceExtractor
http://www.volumesoffun.com/phpBB3/viewtopic.php?f=14&t=275
Page 1 of 2

Author:  gamegezer [ Mon Oct 10, 2011 7:42 pm ]
Post subject:  Trouble creating a Irrlicht mesh from CubicSurfaceExtractor

I'm using CubicSurfaceExtractorWithNormals to extract the vertices and indices. Then converting to an irrlicht SMesh with the following code
Code:
SMesh* TerrainManager::generateCubicMesh(SimpleVolume<MaterialDensityPair44>& volData)
{
      SMesh* sMesh = new SMesh();
      SurfaceMesh<PositionMaterial> vMesh;

//get the vertices and indices
     PolyVox::CubicSurfaceExtractor<SimpleVolume, MaterialDensityPair44 > surfaceEx(&volData, volData.getEnclosingRegion(), &vMesh,true);
     surfaceEx.execute();

     const std::vector<uint32_t>& vecIndices = vMesh.getIndices();
          const std::vector<PositionMaterial>& vecVertices = vMesh.getVertices();

//put the indices into an array

     u16 *arrIndices = new u16[vecIndices.size()];
     for(int i =0; i < vMesh.getNoOfIndices();i++)
     {
      arrIndices[i] =(u16)vecIndices[i];
     }

//put the vertices into an array of 3dvectors

     irr::core::vector3df *arrVertices = new irr::core::vector3df[vecVertices.size() ];
     for(int i =0; i < vecVertices.size();i++)
     {
       
        arrVertices[i] = irr::core::vector3df((f32)vecVertices[i].getPosition().getX(),
        (f32)vecVertices[i].getPosition().getY(),
        (f32)vecVertices[i].getPosition().getZ());
      
     }

    //add the vertices and indices to a buffer and add them to the mesh
     SMeshBuffer *buf = new SMeshBuffer();
     buf->append(arrVertices,vecVertices.size(),arrIndices,vecIndices.size());
     sMesh->addMeshBuffer(buf);
     return sMesh;
}


The odd thing is that I create a spherical shape(using create sphere in volume code), the polygons are just completely messed up(jagged and definitely not cubic) and invisible from certain angles.

If noone feels like dissecting my code( I wouldn't), How would I properly load the indices and vertexes? I feel like I'm doing it correctly, but I'm obviously not.

Author:  beyzend [ Tue Oct 11, 2011 1:39 am ]
Post subject:  Re: Trouble creating a Irrlicht mesh from CubicSurfaceExtrac

can you post a screenshot also? One thing I noticed is that you are using SurfaceExtractor and not SurfaceExtractorWithNormal as you indicated in your post. Other than that I don't see anything wrong.

Author:  gamegezer [ Tue Oct 11, 2011 2:13 am ]
Post subject:  Re: Trouble creating a Irrlicht mesh from CubicSurfaceExtrac

http://www.youtube.com/watch?v=-_QzyOaLF0c
that is supposed to be a sphere with a radius of 4. Sorry for the low quality btw.

Author:  Gnurfos [ Tue Oct 11, 2011 5:20 am ]
Post subject:  Re: Trouble creating a Irrlicht mesh from CubicSurfaceExtrac

I don't see anything shocking either, but Irrlicht's API is not protecting you from doing bad things so much, imho.

You could try doing this way, it works at least here:
http://thermite3d.org/phpBB3/viewtopic.php?f=14&t=185&start=10

Author:  beyzend [ Tue Oct 11, 2011 5:52 pm ]
Post subject:  Re: Trouble creating a Irrlicht mesh from CubicSurfaceExtrac

can you post the volume creation code? From that video alone it looks like you have the vertex and index buffer wrong, especially since the mesh disappears viewed from certain angles. Or you have incorrect world transforms so everything is bunched up together. I don't think it's the latter, though. Also make sure you mesh appending code is correct.

Author:  gamegezer [ Tue Oct 11, 2011 7:39 pm ]
Post subject:  Re: Trouble creating a Irrlicht mesh from CubicSurfaceExtrac

This is the code I'm using to generate the volume(same as in the example). Thanks Gnu I'll be sure to check out that link tonight.

Code:
void TerrainManager::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);

        //This three-level for loop iterates over every voxel in the volume
        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);
                                }
                        }
                }
        }
}

Author:  David Williams [ Tue Oct 11, 2011 9:34 pm ]
Post subject:  Re: Trouble creating a Irrlicht mesh from CubicSurfaceExtrac

I don't know anything about Irrlicht but I notice you don't appear to define a vertex format anywhere. How does irrlicht know that each vertex has a position but no normal? Maybe it thinks that half the values are normals and therefore you only have half as many vertices as you think?

This could also explain why the indicies appear strange as beyzend observed? Note that Gnurfos's code references 'EVT_STANDARD' which I guess is a vertex type.

Also, i doubt if it the problem but watch out for casting the 32 bit indices to 16 bits.

Author:  gamegezer [ Tue Oct 11, 2011 10:26 pm ]
Post subject:  Re: Trouble creating a Irrlicht mesh from CubicSurfaceExtrac

Alright! I took the vertices and indices convert code gnu linked to and the mesh generated properly! I'm betting that the error was with the Vertex array like david said. I thought by vertex type they meant that you could pass vertices as different data types, I'll take the time to look at EVT_STANDARD tonight. The mesh still isn't drawn at certain angles, could this be because of a culling issue?

Author:  gamegezer [ Wed Oct 12, 2011 2:59 am ]
Post subject:  Re: Trouble creating a Irrlicht mesh from CubicSurfaceExtrac

Awesome guys It's working perfectly, thanks so much! It was a culling issue btw, if anyone is experiencing bizarre clipping with irrlicht turn off the culling for the node.

Author:  Gnurfos [ Wed Oct 12, 2011 4:48 am ]
Post subject:  Re: Trouble creating a Irrlicht mesh from CubicSurfaceExtrac

gamegezer wrote:
Awesome guys It's working perfectly, thanks so much! It was a culling issue btw, if anyone is experiencing bizarre clipping with irrlicht turn off the culling for the node.

This is not a solution, but a workaround that has drawbacks.
I had the same problem, and it was solved by calling recalculateBoundingBox() on the created mesh (doing it on the meshbuffer is not enough, I'm not sure it is even needed). You can display bounding boxes to confirm this is the problem (call setDebugDataVisible(irr::scene::EDS_BBOX) on the scene node).

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