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);