Skip to content

Instantly share code, notes, and snippets.

@soulslicer
Last active September 4, 2017 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soulslicer/62ea22c6faad381b3e9fe639c93e31ad to your computer and use it in GitHub Desktop.
Save soulslicer/62ea22c6faad381b3e9fe639c93e31ad to your computer and use it in GitHub Desktop.
MVS-Texturing Single Texture
diff --git a/libs/tex/calculate_data_costs.cpp b/libs/tex/calculate_data_costs.cpp
index d56cb3e..2024204 100644
--- a/libs/tex/calculate_data_costs.cpp
+++ b/libs/tex/calculate_data_costs.cpp
@@ -184,7 +184,7 @@ calculate_face_projection_infos(mve::TriangleMesh::ConstPtr mesh,
if (viewing_angle < 0.0f || viewing_direction.dot(view_to_face_vec) < 0.0f)
continue;
- if (std::acos(viewing_angle) > MATH_DEG2RAD(75.0f))
+ if (std::acos(viewing_angle) > MATH_DEG2RAD(89.0f))
continue;
/* Projects into the valid part of the TextureView? */
diff --git a/libs/tex/generate_texture_atlases.cpp b/libs/tex/generate_texture_atlases.cpp
index c4fe6fc..ee1b698 100644
--- a/libs/tex/generate_texture_atlases.cpp
+++ b/libs/tex/generate_texture_atlases.cpp
@@ -21,7 +21,7 @@
#include "texture_patch.h"
#include "texture_atlas.h"
-#define MAX_TEXTURE_SIZE (8 * 1024)
+#define MAX_TEXTURE_SIZE (32 * 1024)
#define PREF_TEXTURE_SIZE (4 * 1024)
#define MIN_TEXTURE_SIZE (256)
@@ -40,7 +40,7 @@ calculate_texture_size(std::list<TexturePatch::ConstPtr> const & texture_patches
unsigned int total_area = 0;
unsigned int max_width = 0;
unsigned int max_height = 0;
- unsigned int padding = size >> 7;
+ unsigned int padding = std::min(size >> 7, 32U);
for (TexturePatch::ConstPtr texture_patch : texture_patches) {
unsigned int width = texture_patch->get_width() + 2 * padding;
@@ -62,22 +62,12 @@ calculate_texture_size(std::list<TexturePatch::ConstPtr> const & texture_patches
total_area += area;
}
- assert(max_width < MAX_TEXTURE_SIZE);
- assert(max_height < MAX_TEXTURE_SIZE);
- if (size > PREF_TEXTURE_SIZE &&
- max_width < PREF_TEXTURE_SIZE &&
- max_height < PREF_TEXTURE_SIZE &&
- total_area / (PREF_TEXTURE_SIZE * PREF_TEXTURE_SIZE) < 8) {
- size = PREF_TEXTURE_SIZE;
- continue;
- }
-
if (size <= MIN_TEXTURE_SIZE) {
return MIN_TEXTURE_SIZE;
}
if (max_height < size / 2 && max_width < size / 2 &&
- static_cast<double>(total_area) / (size * size) < 0.2) {
+ static_cast<double>(total_area) / (double(size) * size) < 0.2) {
size = size / 2;
continue;
}
@@ -114,19 +104,15 @@ generate_texture_atlases(std::vector<TexturePatch::Ptr> * orig_texture_patches,
std::size_t const total_num_patches = texture_patches.size();
std::size_t remaining_patches = texture_patches.size();
+#if !defined(_WIN32)
std::ofstream tty("/dev/tty", std::ios_base::out);
+#else
+ std::ostream &tty = std::cout;
+#endif
- #pragma omp parallel
- {
- #pragma omp single
- {
-
- while (!texture_patches.empty()) {
- unsigned int texture_size = calculate_texture_size(texture_patches);
-
- texture_atlases->push_back(TextureAtlas::create(texture_size));
- TextureAtlas::Ptr texture_atlas = texture_atlases->back();
-
+ unsigned int texture_size = calculate_texture_size(texture_patches);
+ TextureAtlas::Ptr texture_atlas = TextureAtlas::create(texture_size);
+ while (remaining_patches > 0) {
/* Try to insert each of the texture patches into the texture atlas. */
std::list<TexturePatch::ConstPtr>::iterator it = texture_patches.begin();
for (; it != texture_patches.end();) {
@@ -136,33 +122,30 @@ generate_texture_atlases(std::vector<TexturePatch::Ptr> * orig_texture_patches,
if (total_num_patches > 100
&& done_patches % (total_num_patches / 100) == 0) {
- tty << "\r\tWorking on atlas " << texture_atlases->size() << " "
- << precent << "%... " << std::flush;
+ tty << "\r\tWorking on atlas " << precent << "%... " << std::flush;
}
if (texture_atlas->insert(*it)) {
- it = texture_patches.erase(it);
+ ++it;
remaining_patches -= 1;
} else {
- ++it;
+ /* Texture atlas was too small, try again. */
+ texture_size *= 2;
+ if (texture_size > MAX_TEXTURE_SIZE) {
+ std::cerr << "Exceeded maximum texture size (" << MAX_TEXTURE_SIZE << ")." << std::endl;
+ std::exit(EXIT_FAILURE);
+ }
+
+ remaining_patches = texture_patches.size();
+ it = texture_patches.begin();
+ texture_atlas = TextureAtlas::create(texture_size);
+ break;
}
}
-
- #pragma omp task
- texture_atlas->finalize();
}
- std::cout << "\r\tWorking on atlas " << texture_atlases->size()
- << " 100%... done." << std::endl;
- util::WallTimer timer;
- std::cout << "\tFinalizing texture atlases... " << std::flush;
- #pragma omp taskwait
- std::cout << "done. (Took: " << timer.get_elapsed_sec() << "s)" << std::endl;
-
- /* End of single region */
- }
- /* End of parallel region. */
- }
+ texture_atlases->push_back(texture_atlas);
+ texture_atlas->finalize();
}
TEX_NAMESPACE_END
diff --git a/libs/tex/texture_atlas.cpp b/libs/tex/texture_atlas.cpp
index e95f373..842ad45 100644
--- a/libs/tex/texture_atlas.cpp
+++ b/libs/tex/texture_atlas.cpp
@@ -17,7 +17,7 @@
#include "texture_atlas.h"
TextureAtlas::TextureAtlas(unsigned int size) :
- size(size), padding(size >> 7), finalized(false) {
+ size(size), padding(std::min(size >> 7, 32U)), finalized(false) {
bin = RectangularBin::create(size, size);
image = mve::ByteImage::create(size, size, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment