Skip to content

Commit

Permalink
Fix UVs, change tables to use OpenGL convention...
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Oct 5, 2018
1 parent b052f7f commit d0ab02a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 52 deletions.
78 changes: 34 additions & 44 deletions cube_tables.cpp
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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] = {
Expand Down
4 changes: 2 additions & 2 deletions cube_tables.h
Expand Up @@ -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];

Expand Down
13 changes: 9 additions & 4 deletions voxel.cpp
Expand Up @@ -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);
Expand Down
12 changes: 10 additions & 2 deletions voxel_mesher.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit d0ab02a

Please sign in to comment.