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

Retrieving vertices, normals, and indices from SimpleVolume
http://www.volumesoffun.com/phpBB3/viewtopic.php?f=14&t=630
Page 1 of 2

Author:  RisingForce [ Sun Mar 01, 2015 3:49 pm ]
Post subject:  Retrieving vertices, normals, and indices from SimpleVolume

Hello, all. My 3D game programming experience would be very novice. So, any and all help would be greatly appreciated. My first plan with the PolyVox API is to create a terrain generator/ model loader. I'm using OpenGL and SDL at the moment.

I've created a SimpleModel class and I've used it draw various simple models like a cube for instance. I can even apply a texture to it.

The constructor looks like this:
Code:
SimpleModel(GLuint textureID, GLfloat scale, glm::vec3 position, const std::vector<GLfloat>& vertices, const std::vector<GLfloat>& normals, const std::vector<GLuint>& indices);


I wanted to try to create a small mesh with PolyVox::SimpleVolume to use with it. How could I retrieve the vertices, normals, and indices from the generated mesh? Below is my attempt at doing this with data from a greyscale image. It didn't work and so I thought I'd ask here. I know this won't scale well or probably look very good. It was just an idea I wanted to try for a proof of concept with PolyVox.

Code:
Renderable* VoxelEngine::GenerateTerrain(const std::string& greyScaleImgPath, GLfloat cubeSize)
{
    SDL_Surface* surface = IMG_Load(greyScaleImgPath.c_str());

    Uint8 pixel;

    if (!surface->pixels)
    {
        throw ("Failed to read terrain file.");
    }

    PolyVox::SimpleVolume<Uint8> terrainVolume(PolyVox::Region(PolyVox::Vector3DInt32(0, 0, 0), PolyVox::Vector3DInt32(surface->w, surface->h, 32)));

    for (int i(0); i < surface->w; i++)
    {
        for (int j(0); j < surface->h; j++)
        {
            pixel = *((Uint8*)surface->pixels + (i * surface->pitch) + (j * surface->format->BytesPerPixel));
            pixel = (pixel + 1) / 8;

            for (Uint8 k(0); k < 32; k++)
            {
                if (k < pixel)
                {
                    terrainVolume.setVoxelAt(i, j, k, 255);
                }
                else
                {
                    terrainVolume.setVoxelAt(i, j, k, 0);
                }
            }
        }
    }

    SDL_FreeSurface(surface);

    PolyVox::SurfaceMesh<PolyVox::PositionMaterialNormal> terrainMesh;
    PolyVox::CubicSurfaceExtractorWithNormals<PolyVox::SimpleVolume<Uint8>> surfaceExtractor(&terrainVolume, terrainVolume.getEnclosingRegion(), &terrainMesh);

    surfaceExtractor.execute();

    std::vector<GLfloat> vertices;
    std::vector<GLfloat> normals;
    std::vector<GLuint> indicies(terrainMesh.getIndices());

    const std::vector<PolyVox::PositionMaterialNormal>& positionMaterialNormals = terrainMesh.getVertices();
    for (std::vector<PolyVox::PositionMaterialNormal>::size_type i(0); i < positionMaterialNormals.size(); i++)
    {
        PolyVox::Vector3DFloat position = positionMaterialNormals[i].getPosition();
        PolyVox::Vector3DFloat normal = positionMaterialNormals[i].getNormal();

        vertices.push_back(position.getX());
        vertices.push_back(position.getY());
        vertices.push_back(position.getZ());

        normals.push_back(normal.getX());
        normals.push_back(normal.getY());
        normals.push_back(normal.getZ());
    }

    return new SimpleModel(TextureManager::GetTexture("GRASS"), cubeSize, glm::vec3(0, 0, 0), vertices, normals, indicies);

Author:  David Williams [ Mon Mar 02, 2015 9:37 am ]
Post subject:  Re: Retrieving vertices, normals, and indices from SimpleVol

Hi, I can note a couple of minor problems with your code:
Code:
PolyVox::SimpleVolume<Uint8> terrainVolume(PolyVox::Region(PolyVox::Vector3DInt32(0, 0, 0), PolyVox::Vector3DInt32(surface->w, surface->h, 32)));

Should be (note the '- 1's):
Code:
PolyVox::SimpleVolume<Uint8> terrainVolume(PolyVox::Region(PolyVox::Vector3DInt32(0, 0, 0), PolyVox::Vector3DInt32(surface->w - 1, surface->h - 1, 32 - 1)));

Also the height of your volume is only 32, but your heightmap goes up to 255... does it contain values below 32?

However, you have the basic idea correct, so what is the behaviour you see? Can you try printing out the length of the vertex and index arrays, and perhaps printing the first few vertex positions and array indices to ensure they make sense?

Author:  RisingForce [ Tue Mar 03, 2015 4:15 am ]
Post subject:  Re: Retrieving vertices, normals, and indices from SimpleVol

Hello, David. Thanks for replying!

I only used 32 because I'm adding 1 to each pixel value and then dividing by 8. So, highest value would be (255 + 1) / 8 = 32 and lowest value would be (0 + 1) / 8 = 0. Just for a quick and dirty test.

I really just wanted to make sure I was pulling the data correctly from that PositionMaterialNormal object. I'll try with a volume of size 1 and log the data like you suggested. That should probably be all I need to figure this out.

Thanks again. :)

Author:  David Williams [ Tue Mar 03, 2015 9:36 am ]
Post subject:  Re: Retrieving vertices, normals, and indices from SimpleVol

RisingForce wrote:
I only used 32 because I'm adding 1 to each pixel value and then dividing by 8. So, highest value would be (255 + 1) / 8 = 32 and lowest value would be (0 + 1) / 8 = 0. Just for a quick and dirty test.


Right, sorry, I missed that line.

RisingForce wrote:
I'll try with a volume of size 1 and log the data like you suggested.


Logging the data is a good idea but I wasn't saying you should use a volume of size 1. The '- 1' which I added in my previous code snippet was simply setting the region to the correct size to match your heightmap. If your heightmap has a width of 256 pixels then you were creating a volume covering the range 0 to 256 (which is 257 voxels), but you actually want to cover the range 0 to 255 (which is the required 256 voxels).

Author:  RisingForce [ Tue Mar 03, 2015 11:54 pm ]
Post subject:  Re: Retrieving vertices, normals, and indices from SimpleVol

Sorry, that was a poorly worded sentence. I meant to type size - 1.

Author:  RisingForce [ Wed Mar 04, 2015 1:54 am ]
Post subject:  Re: Retrieving vertices, normals, and indices from SimpleVol

Alrighty. So, I commented out everything in my fragment shader and just set it to color everything white. I got stuff to draw but it appeared a bit funny.

So, I tried the same exact process of obtaining the verticies, normals, and indicies with this volume:
Code:
PolyVox::SimpleVolume<Uint8> terrainVolume(PolyVox::Region(PolyVox::Vector3DInt32(0, 0, 0), PolyVox::Vector3DInt32(1, 1, 1)));
terrainVolume.setVoxelAt(0, 0, 0, 255);


When i launched the application I expected to see a whole cube but I only see 3 faces the cube. Which would explain why it didn't apply the texturing.

I expected 24 verticies and 36 indicies (2 verticies per face re-used). The log counts 12 verticies and 54 indicies being generated from that PolyVox::SurfaceMesh<PolyVox::PositionMaterialNormal> instance.

Author:  David Williams [ Wed Mar 04, 2015 9:17 am ]
Post subject:  Re: Retrieving vertices, normals, and indices from SimpleVol

RisingForce wrote:
When i launched the application I expected to see a whole cube but I only see 3 faces the cube.


Yes, this is expected. Have a look at the diagrams in this documentation: http://www.volumesoffun.com/polyvox/doc ... actor.html

The easiest solution is to leave the edge voxels of your volume empty (obviously this doesn't apply in the case of a 1x1x1 volume). It is also possible to extract a region which is larger than the volume.

RisingForce wrote:
I expected 24 verticies and 36 indicies (2 verticies per face re-used). The log counts 12 verticies and 54 indicies being generated from that PolyVox::SurfaceMesh<PolyVox::PositionMaterialNormal> instance.


12 vertices of ok for 3 quads, but 54 indices sounds strange. That's 18 triangles whereas there should only be 6 I think. Are there duplicates? Can you post them here?

Author:  RisingForce [ Thu Mar 05, 2015 12:15 am ]
Post subject:  Re: Retrieving vertices, normals, and indices from SimpleVol

Ah I see. I should have read that... My bad...

As far as the indicies they would appear to be triplicated:
    0
    0
    0
    2
    2
    2
    1
    1
    1
    1
    1
    1
    2
    2
    2
    3
    3
    3
    4
    4
    4
    5
    5
    5
    6
    6
    6
    5
    5
    5
    7
    7
    7
    6
    6
    6
    8
    8
    8
    10
    10
    10
    9
    9
    9
    9
    9
    9
    10
    10
    10
    11
    11
    11

And verticies:
    0.5 , -0.5 , -0.5
    0.5 , -0.5 , 0.5
    0.5 , 0.5 , -0.5
    0.5 , 0.5 , 0.5
    -0.5 , 0.5 , -0.5
    -0.5 , 0.5 , 0.5
    0.5 , 0.5 , -0.5
    0.5 , 0.5 , 0.5
    -0.5 , -0.5 , 0.5
    -0.5 , 0.5 , 0.5
    0.5 , -0.5 , 0.5
    0.5 , 0.5 , 0.5

Author:  RisingForce [ Thu Mar 05, 2015 1:18 am ]
Post subject:  Re: Retrieving vertices, normals, and indices from SimpleVol

Still not sure why there are 54 indicies for half of a cube but on the bright side I'm now able to render a textured cube using the verticies, normals, and indicies extracted with PolyVox::CubicSurfaceExtractorWithNormals
:D 8-)

The main issue with nothing showing up was a problem with my TextureManager class I've created to manage texture bindings. Silly me never inserted the id into the map and therefore couldn't retrieve it later... The other issue was me not reading that link you posted about PolyVox::CubicSurfaceExtractorWithNormals David.

Thanks for all your help!

Author:  David Williams [ Thu Mar 05, 2015 2:14 pm ]
Post subject:  Re: Retrieving vertices, normals, and indices from SimpleVol

RisingForce wrote:
As far as the indicies they would appear to be triplicated:
    0
    0
    0
    2
    2
    2
    1
    1
    1
    1
    .
    .
    .


That's very strange... does this seem to occur in the PolyVox examples? The CubicSurfaceExtractorWithNormals is removed in recent versions of PolyVox (just CubicSurfaceExtractor is present) so I can't easily test this at the moment. But this will completely break the rendering of the mesh because sets of three points no longer define valid triangles, so I would have expected this to show up before now.

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