diff --git a/cube_tables.cpp b/cube_tables.cpp index 44e03234..9e72cf52 100644 --- a/cube_tables.cpp +++ b/cube_tables.cpp @@ -199,4 +199,12 @@ const Vector3i g_ordered_moore_area_3d[MOORE_AREA_3D_COUNT] = { Vector3i(1, 1, 1) }; +const float g_side_tangents[SIDE_COUNT][4] = { + {0.0f, 0.0f, -1.0f, -1.0f}, + {0.0f, 0.0f, 1.0f, -1.0f}, + {1.0f, 0.0f, 0.0f, -1.0f}, + {-1.0f, 0.0f, 0.0f, -1.0f}, + {1.0f, 0.0f, 0.0f, -1.0f}, +}; + } // namespace Cube diff --git a/cube_tables.h b/cube_tables.h index d645c3bc..7ef5d7c6 100644 --- a/cube_tables.h +++ b/cube_tables.h @@ -60,6 +60,8 @@ enum Corner { CORNER_COUNT }; +extern const float g_side_tangents[SIDE_COUNT][4]; + extern const Vector3 g_corner_position[CORNER_COUNT]; extern const int g_side_quad_triangles[SIDE_COUNT][6]; diff --git a/meshers/blocky/voxel.cpp b/meshers/blocky/voxel.cpp index 7f2f2a1f..a94c8231 100644 --- a/meshers/blocky/voxel.cpp +++ b/meshers/blocky/voxel.cpp @@ -190,8 +190,6 @@ void Voxel::set_collision_mask(uint32_t mask) { static void bake_cube_geometry(Voxel &config, Voxel::BakedData &baked_data, int p_atlas_size) { const float sy = 1.0; - std::vector deltaPos; - for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) { std::vector &positions = baked_data.model.side_positions[side]; positions.resize(4); @@ -203,10 +201,6 @@ static void bake_cube_geometry(Voxel &config, Voxel::BakedData &baked_data, int } positions[i] = p; } - //Saving them here so we don't have to loop through and get them later. Probably a better way of doing this. - deltaPos.push_back(Vector3(positions[2] - positions[0])); - deltaPos.push_back(Vector3(positions[1] - positions[0])); - deltaPos.push_back(Vector3(positions[3] - positions[0])); std::vector &indices = baked_data.model.side_indices[side]; indices.resize(6); @@ -230,8 +224,6 @@ static void bake_cube_geometry(Voxel &config, Voxel::BakedData &baked_data, int CRASH_COND(atlas_size <= 0); const float s = 1.0 / atlas_size; - std::vector deltaUV; - for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) { baked_data.model.side_uvs[side].resize(4); std::vector &uvs = baked_data.model.side_uvs[side]; @@ -239,42 +231,10 @@ static void bake_cube_geometry(Voxel &config, Voxel::BakedData &baked_data, int uvs[i] = (config.get_cube_tile(side) + uv[i]) * s; } - //Tangent calculation modeled after http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/#computing-the-tangents-and-bitangents - Vector2 deltaUV1 = uvs[2] - uvs[0]; - Vector2 deltaUV2 = uvs[1] - uvs[0]; - - Vector2 deltaUV3 = uvs[3] - uvs[0]; - - float r1 = 1.0f / (deltaUV1[0] * deltaUV2[1] - deltaUV1[1] * deltaUV2[0]); - float r2 = 1.0f / (deltaUV3[0] * deltaUV1[1] - deltaUV3[1] * deltaUV1[0]); - - Vector3 deltaPos1 = deltaPos[side * 3]; - Vector3 deltaPos2 = deltaPos[side * 3+1]; - Vector3 deltaPos3 = deltaPos[side * 3+2]; - - Vector3 t1 = (deltaPos1 * deltaUV2[1] - deltaPos2 * deltaUV1[1]) * r1; - Vector3 t2 = (deltaPos3 * deltaUV1[1] - deltaPos1 * deltaUV3[1]) * r2; - - Vector3 bt1 = (deltaPos2 * deltaUV1[0] - deltaPos1 * deltaUV2[0]) * r1; - Vector3 bt2 = (deltaPos1 * deltaUV3[0] - deltaPos3 * deltaUV1[0]) * r2; - - Vector3i ni = Cube::g_side_normals[side]; - Vector3 n = Vector3(ni[0], ni[1], ni[2]); - float d1 = bt1.dot(n.cross(t1)) < 0 ? -1.0f : 1.0f; - float d2 = bt2.dot(n.cross(t2)) < 0 ? -1.0f : 1.0f; - std::vector &tangents = baked_data.model.side_tangents[side]; - for (unsigned int i = 0; i < 3; i++) { - tangents.push_back(t1[0]); - tangents.push_back(t1[1]); - tangents.push_back(t1[2]); - tangents.push_back(d1); - } - for (unsigned int i = 0; i < 3; i++) { - tangents.push_back(t2[0]); - tangents.push_back(t2[1]); - tangents.push_back(t2[2]); - tangents.push_back(d2); + for (unsigned int i = 0; i < 12; ++i) { + for (unsigned int j = 0; j < 4; ++j) + tangents.push_back(Cube::g_side_tangents[side][j]); } } @@ -299,7 +259,7 @@ static void bake_mesh_geometry(Voxel &config, Voxel::BakedData &baked_data) { PoolVector3Array positions = arrays[Mesh::ARRAY_VERTEX]; PoolVector3Array normals = arrays[Mesh::ARRAY_NORMAL]; PoolVector2Array uvs = arrays[Mesh::ARRAY_TEX_UV]; - PoolVector tangents = arrays[Mesh::ARRAY_TANGENT]; + PoolVector tangents = arrays[Mesh::ARRAY_TANGENT]; baked_data.empty = positions.size() == 0; @@ -342,11 +302,7 @@ static void bake_mesh_geometry(Voxel &config, Voxel::BakedData &baked_data) { uvs.resize(positions.size()); } - bool tangents_empty = false; - if(tangents.size() == 0) { - tangents = PoolVector(); - tangents_empty = true; - } + bool tangents_empty = tangents.size() == 0 ? false : true; // Separate triangles belonging to faces of the cube @@ -355,7 +311,7 @@ static void bake_mesh_geometry(Voxel &config, Voxel::BakedData &baked_data) { PoolVector3Array::Read positions_read = positions.read(); PoolVector3Array::Read normals_read = normals.read(); PoolVector2Array::Read uvs_read = uvs.read(); - PoolVector::Read tangents_read = tangents.read(); + PoolVector::Read tangents_read = tangents.read(); FixedArray, Cube::SIDE_COUNT> added_side_indices; HashMap added_regular_indices; @@ -374,13 +330,13 @@ static void bake_mesh_geometry(Voxel &config, Voxel::BakedData &baked_data) { if(tangents_empty){ //If tangents are empty then we calculate them - Vector2 deltaUV1 = uvs_read[indices_read[i+1]] - uvs_read[indices_read[i]]; - Vector2 deltaUV2 = uvs_read[indices_read[i+2]] - uvs_read[indices_read[i]]; - Vector3 deltaPos1 = tri_positions[1] - tri_positions[0]; - Vector3 deltaPos2 = tri_positions[2] - tri_positions[0]; - float r = 1.0f / (deltaUV1[0] * deltaUV2[1] - deltaUV1[1] * deltaUV2[0]); - Vector3 t = (deltaPos1 * deltaUV2[1] - deltaPos2 * deltaUV1[1])*r; - Vector3 bt = (deltaPos2 * deltaUV1[0] - deltaPos1 * deltaUV2[0])*r; + Vector2 delta_uv1 = uvs_read[indices_read[i+1]] - uvs_read[indices_read[i]]; + Vector2 delta_uv2 = uvs_read[indices_read[i+2]] - uvs_read[indices_read[i]]; + Vector3 delta_pos1 = tri_positions[1] - tri_positions[0]; + Vector3 delta_pos2 = tri_positions[2] - tri_positions[0]; + float r = 1.0f / (delta_uv1[0] * delta_uv2[1] - delta_uv1[1] * delta_uv2[0]); + Vector3 t = (delta_pos1 * delta_uv2[1] - delta_pos2 * delta_uv1[1])*r; + Vector3 bt = (delta_pos2 * delta_uv1[0] - delta_pos1 * delta_uv2[0])*r; tangent[0] = t[0]; tangent[1] = t[1]; tangent[2] = t[2]; diff --git a/meshers/cubes/voxel_mesher_cubes.cpp b/meshers/cubes/voxel_mesher_cubes.cpp index 0c670e6b..99a67cb3 100644 --- a/meshers/cubes/voxel_mesher_cubes.cpp +++ b/meshers/cubes/voxel_mesher_cubes.cpp @@ -343,11 +343,6 @@ void build_voxel_mesh_as_greedy_cubes( Vector3 n; n[za] = m.side == SIDE_FRONT ? -1 : 1; - Vector3 t = n.cross(v0); - Vector3 bt = t.cross(n); - float dt = bt.dot(n.cross(t)); - float tangent[4] = {t[0], t[1], t[2], dt < 0 ? -1.0f : 1.0f}; - // 2-----3 // | | // | | @@ -368,27 +363,6 @@ void build_voxel_mesh_as_greedy_cubes( arrays.normals.push_back(n); arrays.normals.push_back(n); - // One for each vertex - arrays.tangents.push_back(tangent[0]); - arrays.tangents.push_back(tangent[1]); - arrays.tangents.push_back(tangent[2]); - arrays.tangents.push_back(tangent[3]); - - arrays.tangents.push_back(tangent[0]); - arrays.tangents.push_back(tangent[1]); - arrays.tangents.push_back(tangent[2]); - arrays.tangents.push_back(tangent[3]); - - arrays.tangents.push_back(tangent[0]); - arrays.tangents.push_back(tangent[1]); - arrays.tangents.push_back(tangent[2]); - arrays.tangents.push_back(tangent[3]); - - arrays.tangents.push_back(tangent[0]); - arrays.tangents.push_back(tangent[1]); - arrays.tangents.push_back(tangent[2]); - arrays.tangents.push_back(tangent[3]); - const unsigned int index_offset = index_offsets[material_index]; CRASH_COND(za >= 3 || m.side >= 2); const uint8_t *lut = g_indices_lut[za][m.side]; @@ -421,7 +395,6 @@ void VoxelMesherCubes::build(VoxelMesher::Output &output, const VoxelMesher::Inp a.normals.clear(); a.colors.clear(); a.indices.clear(); - a.tangents.clear(); } const VoxelBuffer &voxels = input.voxels; @@ -629,19 +602,16 @@ void VoxelMesherCubes::build(VoxelMesher::Output &output, const VoxelMesher::Inp PoolVector normals; PoolVector colors; PoolVector indices; - PoolVector tangents; raw_copy_to(positions, arrays.positions); raw_copy_to(normals, arrays.normals); raw_copy_to(colors, arrays.colors); raw_copy_to(indices, arrays.indices); - raw_copy_to(tangents, arrays.tangents); mesh_arrays[Mesh::ARRAY_VERTEX] = positions; mesh_arrays[Mesh::ARRAY_NORMAL] = normals; mesh_arrays[Mesh::ARRAY_COLOR] = colors; mesh_arrays[Mesh::ARRAY_INDEX] = indices; - mesh_arrays[Mesh::ARRAY_TANGENT] = tangents; } output.surfaces.push_back(mesh_arrays); diff --git a/meshers/cubes/voxel_mesher_cubes.h b/meshers/cubes/voxel_mesher_cubes.h index 1cb12c8b..31bd2f9a 100644 --- a/meshers/cubes/voxel_mesher_cubes.h +++ b/meshers/cubes/voxel_mesher_cubes.h @@ -51,7 +51,6 @@ public: std::vector normals; std::vector colors; std::vector indices; - std::vector tangents; }; protected: