Fix UVs, change tables to use OpenGL convention...

This commit is contained in:
Marc Gilleron 2018-10-05 23:51:55 +01:00
parent b052f7fc2d
commit d0ab02a397
4 changed files with 55 additions and 52 deletions

View File

@ -9,9 +9,9 @@ namespace Cube {
// / | / | Corners // / | / | Corners
// 4-------5 | // 4-------5 |
// | 3----|--2 // | 3----|--2
// | / | / y z // | / | / y z
// |/ |/ |/ // |/ |/ |/ OpenGL axis convention
// 0-------1 o--x // 0-------1 x--o
// //
// //
// o---10----o // o---10----o
@ -29,47 +29,37 @@ namespace Cube {
// Edges are ordered according to the Voxel::Edge enum (only g_edge_inormals!). // Edges are ordered according to the Voxel::Edge enum (only g_edge_inormals!).
// //
//static const unsigned int CORNER_COUNT = 8; // Ordered as per the cube corners diagram
//static const unsigned int EDGE_COUNT = 12;
// Order doesn't technically matter here, they are for reading convenience.
// If you need a special ordering, combine index with other tables
const Vector3 g_corner_position[CORNER_COUNT] = { const Vector3 g_corner_position[CORNER_COUNT] = {
Vector3(0, 0, 0),
Vector3(1, 0, 0), Vector3(1, 0, 0),
Vector3(1, 0, 1), Vector3(0, 0, 0),
Vector3(0, 0, 1), Vector3(0, 0, 1),
Vector3(0, 1, 0), Vector3(1, 0, 1),
Vector3(1, 1, 0), Vector3(1, 1, 0),
Vector3(1, 1, 1), Vector3(0, 1, 0),
Vector3(0, 1, 1) Vector3(0, 1, 1),
Vector3(1, 1, 1)
}; };
const int g_side_quad_triangles[SIDE_COUNT][6] = { const int g_side_quad_triangles[SIDE_COUNT][6] = {
// LEFT { 0, 2, 1, 0, 3, 2 }, // LEFT (+x)
{ 0, 1, 2, 0, 2, 3 }, { 0, 2, 1, 0, 3, 2 }, // RIGHT (-x)
// RIGHT { 0, 2, 1, 0, 3, 2 }, // BOTTOM (-y)
{ 0, 1, 2, 0, 2, 3 }, { 0, 2, 1, 0, 3, 2 }, // TOP (+y)
// BOTTOM { 0, 2, 1, 0, 3, 2 }, // BACK (-z)
{ 0, 1, 2, 0, 2, 3 }, { 0, 2, 1, 0, 3, 2 }, // FRONT (+z)
// TOP
{ 0, 1, 2, 0, 2, 3 },
// BACK
{ 0, 1, 2, 0, 2, 3 },
// FRONT
{ 0, 1, 2, 0, 2, 3 },
}; };
const unsigned int g_side_coord[SIDE_COUNT] = { 0, 0, 1, 1, 2, 2 }; //const unsigned int g_side_coord[SIDE_COUNT] = { 0, 0, 1, 1, 2, 2 };
const unsigned int g_side_sign[SIDE_COUNT] = { 0, 1, 0, 1, 0, 1 }; //const unsigned int g_side_sign[SIDE_COUNT] = { 0, 1, 0, 1, 0, 1 };
const Vector3i g_side_normals[SIDE_COUNT] = { const Vector3i g_side_normals[SIDE_COUNT] = {
Vector3i(-1, 0, 0), Vector3i(1, 0, 0), // LEFT
Vector3i(1, 0, 0), Vector3i(-1, 0, 0), // RIGHT
Vector3i(0, -1, 0), Vector3i(0, -1, 0), // BOTTOM
Vector3i(0, 1, 0), Vector3i(0, 1, 0), // TOP
Vector3i(0, 0, -1), Vector3i(0, 0, -1), // BACK
Vector3i(0, 0, 1), Vector3i(0, 0, 1), // FRONT
}; };
// Corners have same winding, relative to the face's normal // Corners have same winding, relative to the face's normal
@ -104,32 +94,32 @@ const unsigned int g_side_edges[SIDE_COUNT][4] = {
//}; //};
const Vector3i g_corner_inormals[CORNER_COUNT] = { const Vector3i g_corner_inormals[CORNER_COUNT] = {
Vector3i(-1, -1, -1),
Vector3i(1, -1, -1), Vector3i(1, -1, -1),
Vector3i(1, -1, 1), Vector3i(-1, -1, -1),
Vector3i(-1, -1, 1), Vector3i(-1, -1, 1),
Vector3i(1, -1, 1),
Vector3i(-1, 1, -1),
Vector3i(1, 1, -1), Vector3i(1, 1, -1),
Vector3i(1, 1, 1), Vector3i(-1, 1, -1),
Vector3i(-1, 1, 1) Vector3i(-1, 1, 1),
Vector3i(1, 1, 1)
}; };
const Vector3i g_edge_inormals[EDGE_COUNT] = { const Vector3i g_edge_inormals[EDGE_COUNT] = {
Vector3i(0, -1, -1), Vector3i(0, -1, -1),
Vector3i(1, -1, 0),
Vector3i(0, -1, 1),
Vector3i(-1, -1, 0), Vector3i(-1, -1, 0),
Vector3i(0, -1, 1),
Vector3i(1, -1, 0),
Vector3i(-1, 0, -1),
Vector3i(1, 0, -1), Vector3i(1, 0, -1),
Vector3i(1, 0, 1), Vector3i(-1, 0, -1),
Vector3i(-1, 0, 1), Vector3i(-1, 0, 1),
Vector3i(1, 0, 1),
Vector3i(0, 1, -1), Vector3i(0, 1, -1),
Vector3i(1, 1, 0), Vector3i(-1, 1, 0),
Vector3i(0, 1, 1), Vector3i(0, 1, 1),
Vector3i(-1, 1, 0) Vector3i(1, 1, 0)
}; };
const unsigned int g_edge_corners[EDGE_COUNT][2] = { const unsigned int g_edge_corners[EDGE_COUNT][2] = {

View File

@ -54,8 +54,8 @@ extern const Vector3 g_corner_position[CORNER_COUNT];
extern const int g_side_quad_triangles[SIDE_COUNT][6]; extern const int g_side_quad_triangles[SIDE_COUNT][6];
extern const unsigned int g_side_coord[SIDE_COUNT]; //extern const unsigned int g_side_coord[SIDE_COUNT];
extern const unsigned int g_side_sign[SIDE_COUNT]; //extern const unsigned int g_side_sign[SIDE_COUNT];
extern const Vector3i g_side_normals[SIDE_COUNT]; extern const Vector3i g_side_normals[SIDE_COUNT];

View File

@ -220,14 +220,19 @@ void Voxel::update_cube_uv_sides() {
} }
float e = 0.001; float e = 0.001;
// Winding is the same as the one chosen in Cube:: vertices
// I am confused. I read in at least 3 OpenGL tutorials that texture coordinates start at bottom-left (0,0).
// But even though Godot is said to follow OpenGL's convention, the engine starts at top-left!
const Vector2 uv[4] = { const Vector2 uv[4] = {
Vector2(e, e),
Vector2(1.f - e, e),
Vector2(1.f - e, 1.f - e),
Vector2(e, 1.f - e), Vector2(e, 1.f - e),
Vector2(1.f - e, 1.f - e),
Vector2(1.f - e, e),
Vector2(e, e),
}; };
float s = 1.0 / (float)library->get_atlas_size(); float atlas_size = (float)library->get_atlas_size();
CRASH_COND(atlas_size <= 0);
float s = 1.0 / atlas_size;
for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) { for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) {
_model_side_uvs[side].resize(4); _model_side_uvs[side].resize(4);

View File

@ -131,8 +131,8 @@ Array VoxelMesher::build(const VoxelBuffer &buffer, unsigned int channel, Vector
int deck_size = buffer.get_size().x * row_size; int deck_size = buffer.get_size().x * row_size;
int side_neighbor_lut[Cube::SIDE_COUNT]; int side_neighbor_lut[Cube::SIDE_COUNT];
side_neighbor_lut[Cube::SIDE_LEFT] = -row_size; side_neighbor_lut[Cube::SIDE_LEFT] = row_size;
side_neighbor_lut[Cube::SIDE_RIGHT] = row_size; side_neighbor_lut[Cube::SIDE_RIGHT] = -row_size;
side_neighbor_lut[Cube::SIDE_BACK] = -deck_size; side_neighbor_lut[Cube::SIDE_BACK] = -deck_size;
side_neighbor_lut[Cube::SIDE_FRONT] = deck_size; side_neighbor_lut[Cube::SIDE_FRONT] = deck_size;
side_neighbor_lut[Cube::SIDE_BOTTOM] = -1; side_neighbor_lut[Cube::SIDE_BOTTOM] = -1;
@ -363,6 +363,14 @@ Array VoxelMesher::build(const VoxelBuffer &buffer, unsigned int channel, Vector
const Arrays &arrays = _arrays[i]; const Arrays &arrays = _arrays[i];
if (arrays.positions.size() != 0) { if (arrays.positions.size() != 0) {
/*print_line("Arrays:");
for(int i = 0; i < arrays.positions.size(); ++i)
print_line(String(" P {0}").format(varray(arrays.positions[i])));
for(int i = 0; i < arrays.normals.size(); ++i)
print_line(String(" N {0}").format(varray(arrays.normals[i])));
for(int i = 0; i < arrays.uvs.size(); ++i)
print_line(String(" UV {0}").format(varray(arrays.uvs[i])));*/
Array mesh_arrays; Array mesh_arrays;
mesh_arrays.resize(Mesh::ARRAY_MAX); mesh_arrays.resize(Mesh::ARRAY_MAX);