diff --git a/cube_tables.cpp b/cube_tables.cpp index 3f6025f55..14019c5a6 100644 --- a/cube_tables.cpp +++ b/cube_tables.cpp @@ -9,9 +9,9 @@ namespace Cube { // / | / | Corners // 4-------5 | // | 3----|--2 -// | / | / y z -// |/ |/ |/ -// 0-------1 o--x +// | / | / y z +// |/ |/ |/ OpenGL axis convention +// 0-------1 x--o // // // o---10----o @@ -29,47 +29,37 @@ namespace Cube { // Edges are ordered according to the Voxel::Edge enum (only g_edge_inormals!). // -//static const unsigned int CORNER_COUNT = 8; -//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 +// Ordered as per the cube corners diagram const Vector3 g_corner_position[CORNER_COUNT] = { - Vector3(0, 0, 0), Vector3(1, 0, 0), - Vector3(1, 0, 1), + Vector3(0, 0, 0), Vector3(0, 0, 1), - Vector3(0, 1, 0), + Vector3(1, 0, 1), Vector3(1, 1, 0), - Vector3(1, 1, 1), - Vector3(0, 1, 1) + Vector3(0, 1, 0), + Vector3(0, 1, 1), + Vector3(1, 1, 1) }; const int g_side_quad_triangles[SIDE_COUNT][6] = { - // LEFT - { 0, 1, 2, 0, 2, 3 }, - // RIGHT - { 0, 1, 2, 0, 2, 3 }, - // BOTTOM - { 0, 1, 2, 0, 2, 3 }, - // TOP - { 0, 1, 2, 0, 2, 3 }, - // BACK - { 0, 1, 2, 0, 2, 3 }, - // FRONT - { 0, 1, 2, 0, 2, 3 }, + { 0, 2, 1, 0, 3, 2 }, // LEFT (+x) + { 0, 2, 1, 0, 3, 2 }, // RIGHT (-x) + { 0, 2, 1, 0, 3, 2 }, // BOTTOM (-y) + { 0, 2, 1, 0, 3, 2 }, // TOP (+y) + { 0, 2, 1, 0, 3, 2 }, // BACK (-z) + { 0, 2, 1, 0, 3, 2 }, // FRONT (+z) }; -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_coord[SIDE_COUNT] = { 0, 0, 1, 1, 2, 2 }; +//const unsigned int g_side_sign[SIDE_COUNT] = { 0, 1, 0, 1, 0, 1 }; const Vector3i g_side_normals[SIDE_COUNT] = { - Vector3i(-1, 0, 0), - Vector3i(1, 0, 0), - Vector3i(0, -1, 0), - Vector3i(0, 1, 0), - Vector3i(0, 0, -1), - Vector3i(0, 0, 1), + Vector3i(1, 0, 0), // LEFT + Vector3i(-1, 0, 0), // RIGHT + Vector3i(0, -1, 0), // BOTTOM + Vector3i(0, 1, 0), // TOP + Vector3i(0, 0, -1), // BACK + Vector3i(0, 0, 1), // FRONT }; // 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] = { - 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] = { Vector3i(0, -1, -1), - Vector3i(1, -1, 0), - Vector3i(0, -1, 1), 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(0, 1, -1), - Vector3i(1, 1, 0), + Vector3i(-1, 1, 0), Vector3i(0, 1, 1), - Vector3i(-1, 1, 0) + Vector3i(1, 1, 0) }; const unsigned int g_edge_corners[EDGE_COUNT][2] = { diff --git a/cube_tables.h b/cube_tables.h index 35fb377d7..6a7a6dff1 100644 --- a/cube_tables.h +++ b/cube_tables.h @@ -54,8 +54,8 @@ extern const Vector3 g_corner_position[CORNER_COUNT]; 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_sign[SIDE_COUNT]; +//extern const unsigned int g_side_coord[SIDE_COUNT]; +//extern const unsigned int g_side_sign[SIDE_COUNT]; extern const Vector3i g_side_normals[SIDE_COUNT]; diff --git a/voxel.cpp b/voxel.cpp index ad4002218..1725de94f 100644 --- a/voxel.cpp +++ b/voxel.cpp @@ -220,14 +220,19 @@ void Voxel::update_cube_uv_sides() { } 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] = { - Vector2(e, e), - Vector2(1.f - e, e), - Vector2(1.f - 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) { _model_side_uvs[side].resize(4); diff --git a/voxel_mesher.cpp b/voxel_mesher.cpp index 58fa6ed20..0cc4fbfed 100644 --- a/voxel_mesher.cpp +++ b/voxel_mesher.cpp @@ -131,8 +131,8 @@ Array VoxelMesher::build(const VoxelBuffer &buffer, unsigned int channel, Vector int deck_size = buffer.get_size().x * row_size; int side_neighbor_lut[Cube::SIDE_COUNT]; - side_neighbor_lut[Cube::SIDE_LEFT] = -row_size; - side_neighbor_lut[Cube::SIDE_RIGHT] = row_size; + side_neighbor_lut[Cube::SIDE_LEFT] = row_size; + side_neighbor_lut[Cube::SIDE_RIGHT] = -row_size; side_neighbor_lut[Cube::SIDE_BACK] = -deck_size; side_neighbor_lut[Cube::SIDE_FRONT] = deck_size; 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]; 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; mesh_arrays.resize(Mesh::ARRAY_MAX);