Will Smith wrote:
I'm sorry for asking to be spoonfed a solution, but I don't have a very good understanding of constructing primitives based on vertex data. I assumed each face was constructed in the same order of 4 points, so I don't understand why half the faces are rotated.
This is an interesting question. There's no problem with your code, it's actually the triangles generated by PolyVox which are in an inconsistant order.
Off the top of my head I couldn't tell you what order the vertices are in, and I didn't give it much thought when writing the code, for the simple reason that it's not possible to get all three axis to line up. Think of it like this, even if the arrows on your walls did line up, what would happen if you also drew arrows on the floor? Because your walls are at 90 degrees to each other, the arrows on the floor would not be able to line up with both sets of walls. The best you could do would be to have the arrows on the floor line up with one set of walls.
What I'm really saying is that in the general case there is no 'correct' way to assign the texture coordinates, which is part of the reason PolyVox does not provide texture coordinates. However, your case is more specific than the general case - you know that two of your axes represent walls and the that the textures on these should be aligned, and the the other axis represents floor/ceiling and the alignment here doesn't matter.
Hopefully that justifies why it behaves in the current way. The good news is it shouldn't be hard for you to get the behaviour you want - you simply need to consider each of the six faces seperaty based on the normal. This code is untested but should give you the idea:
Code:
else
{
const PositionMaterialNormal& vertex = *itVertex;
const Vector3DFloat& vertPos = vertex.getPosition();
manual->position(vertPos.getX(), vertPos.getY(), vertPos.getZ());
manual->normal(vertex.getNormal().getX(), vertex.getNormal().getY(), vertex.getNormal().getZ());
if(vertex.getNormal().getX() > 0.5f) //Face points along positive X
{
manual->textureCoord(texCoordsForPositiveX[texIdx % 4]);
}
else if(vertex.getNormal().getX() < -0.5f) //Face points along negative X
{
manual->textureCoord(texCoordsForNegativeX[texIdx % 4]);
}
else if(vertex.getNormal().getY() > 0.5f) //Face points along positive Y
{
manual->textureCoord(texCoordsForPositivey[texIdx % 4]);
}
else if(vertex.getNormal().getY() < -0.5f) //Face points along negative Y
{
manual->textureCoord(texCoordsForNegativeY[texIdx % 4]);
}
else if(vertex.getNormal().getZ() > 0.5f) //Face points along positive Z
{
manual->textureCoord(texCoordsForPositiveZ[texIdx % 4]);
}
else if(vertex.getNormal().getZ() < -0.5f) //Face points along negative Z
{
manual->textureCoord(texCoordsForNegativeZ[texIdx % 4]);
}
texIdx++;
}
Note that I've replaced your 'texCoords' array with six new arrays - one for each face ('texCoordsForPositiveX', etc). Start of with each off these the same as your original texCoords array, and the tweak the values for each face until you are happy.
Let us know how that works out. There may be a better way as I haven't given much thought to doing this on the CPU rather than in shaders, so feel free to experiment.