diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index 1c61094..b980404 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -106,6 +106,7 @@ target_include_directories(native-lib PRIVATE ../libs/poly2tri/poly2tri ../libs/base64 ../libs/sqlite3 + ../libs/nanort ) # add lib dependencies diff --git a/android/src/main/cpp/main.cpp b/android/src/main/cpp/main.cpp index 840aef0..a2407a8 100755 --- a/android/src/main/cpp/main.cpp +++ b/android/src/main/cpp/main.cpp @@ -390,6 +390,7 @@ static int engine_init_display(struct engine* engine) { EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, + EGL_DEPTH_SIZE, 24, EGL_NONE }; EGLint w, h, dummy, format; diff --git a/data/layout.xml b/data/layout.xml index 36a8908..8eb3ac6 100644 --- a/data/layout.xml +++ b/data/layout.xml @@ -153,7 +153,7 @@ - + @@ -168,7 +168,7 @@ - + @@ -221,7 +221,7 @@ - + @@ -1119,7 +1119,7 @@ Here's a list of what's available in this release. - + diff --git a/src/font.cpp b/src/font.cpp index 8b0a51d..6e3f693 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -17,6 +17,7 @@ bool Font::load(const char* ttf, int font_size) auto bitmap = std::make_unique(w*h); chars.resize(num_chars); stbtt_BakeFontBitmap(file.m_data, 0, (float)font_size*2, bitmap.get(), w, h, start_char, num_chars, chars.data()); + calc_bounds(); font_tex.create(w, h, GL_R8, GL_RED, bitmap.get()); file.close(); size = font_size; @@ -25,6 +26,21 @@ bool Font::load(const char* ttf, int font_size) return false; } +void Font::calc_bounds() +{ + glm::vec2 bbmin(FLT_MAX); + glm::vec2 bbmax(-FLT_MAX); + for (int i = 0; i < num_chars; i++) + { + stbtt_aligned_quad q; + float x = 0, y = 0; + stbtt_GetBakedQuad(chars.data(), w, h, i, &x, &y, &q, true); + bbmin = glm::min(bbmin, { q.x0 / 2.f, q.y0 / 2.f }); + bbmax = glm::max(bbmax, { q.x1 / 2.f, q.y1 / 2.f }); + } + bounds = { glm::vec4(bbmin, bbmax) }; +} + void FontManager::init() { m_sampler.create(); diff --git a/src/font.h b/src/font.h index 2c2b01f..d0f3eac 100644 --- a/src/font.h +++ b/src/font.h @@ -16,12 +16,15 @@ public: const int h = 512; const int num_chars = 96; const int start_char = 32; + // {mix, max} + glm::vec4 bounds{ 0 }; int size = 0; stbtt_fontinfo font; Texture2D font_tex; std::vector chars; bool load(const char* ttf, int sz); + void calc_bounds(); }; class FontManager diff --git a/src/node_panel_grid.cpp b/src/node_panel_grid.cpp index 4d056ae..4e3a3e1 100644 --- a/src/node_panel_grid.cpp +++ b/src/node_panel_grid.cpp @@ -264,6 +264,12 @@ void NodePanelGrid::bake_uvs() fb.unbindFramebuffer(); fb.destroy(); + auto pb = root()->add_child(); + pb->m_progress->SetWidthP(0); + pb->m_title->set_text("Lightmap Rendering"); + pb->btn_cancel->destroy(); + async_update(); + if (m_rt_dirty) { nanort::BVHBuildOptions build_options; // Use default option @@ -281,56 +287,71 @@ void NodePanelGrid::bake_uvs() auto light_pos = glm::vec3(sinf(light_yaw), light_pitch + get_offset(), cosf(light_yaw)); auto light_dir = glm::normalize(light_pos); + std::atomic_int pb_value(0); + __block auto data_out = std::make_unique(fb.getWidth() * fb.getHeight() * 4); -#if _WIN32 - concurrency::parallel_for(int(0), fb.getHeight(), [&](int y) -#elif __IOS__ || __OSX__ - dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); - dispatch_apply((size_t)fb.getHeight(), queue, ^(size_t y) -#else - for (int y = 0; y < fb.getHeight(); y++) -#endif + std::thread worker([&] { - for (int x = 0; x < fb.getWidth(); x++) - { - int i = y * fb.getHeight() + x; - auto nor = glm::make_vec3(&data_nor[i * 4]); - auto pos = glm::make_vec3(&data_pos[i * 4]); - auto& out = *reinterpret_cast(&data_out[i * 4]); - -// if (glm::dot(nor, light_dir) <= 0.f) -// { -// out = { 50, 50, 50, 255 }; -// continue; -// } - - int hit = 0; - for (int s = 0; s < 16; s++) - { - auto dir = glm::normalize(light_dir + glm::sphericalRand(.1f)); - - nanort::Ray ray; - ray.org[0] = pos.x;// + nor.x * 0.005; - ray.org[1] = pos.y;// + nor.y * 0.005; - ray.org[2] = pos.z;// + nor.z * 0.005; - ray.dir[0] = dir.x; - ray.dir[1] = dir.y; - ray.dir[2] = dir.z; - - float kFar = 2000.0; - ray.min_t = 0.005f; - ray.max_t = kFar; - - nanort::TriangleIntersector<> triangle_intersector(reinterpret_cast(m_hm_plane.vertices.data()), m_hm_plane.idx.data(), sizeof(vertex_t)); - nanort::TriangleIntersection<> isect; - hit += m_rt_accel.Traverse(ray, triangle_intersector, &isect); - } - out = glm::lerp(glm::vec4(255, 255, 255, 255), glm::vec4(50, 50, 50, 255), hit / 16.f); - } - } -#if _WIN32 || __IOS__ || __OSX__ - ); +#if _WIN32 + concurrency::parallel_for(int(0), fb.getHeight(), [&](int y) +#elif __IOS__ || __OSX__ + dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); + dispatch_apply((size_t)fb.getHeight(), queue, ^ (size_t y) +#else + for (int y = 0; y < fb.getHeight(); y++) #endif + { + for (int x = 0; x < fb.getWidth(); x++) + { + int i = y * fb.getHeight() + x; + auto nor = glm::make_vec3(&data_nor[i * 4]); + auto pos = glm::make_vec3(&data_pos[i * 4]); + auto& out = *reinterpret_cast(&data_out[i * 4]); + + if (glm::dot(nor, light_dir) <= 0.f) + { + out = { 50, 50, 50, 255 }; + continue; + } + + int hit = 0; + for (int s = 0; s < 16; s++) + { + auto dir = glm::normalize(light_dir + glm::sphericalRand(.1f)); + + nanort::Ray ray; + ray.org[0] = pos.x;// + nor.x * 0.005; + ray.org[1] = pos.y;// + nor.y * 0.005; + ray.org[2] = pos.z;// + nor.z * 0.005; + ray.dir[0] = dir.x; + ray.dir[1] = dir.y; + ray.dir[2] = dir.z; + + float kFar = 2000.0; + ray.min_t = 0.005f; + ray.max_t = kFar; + + nanort::TriangleIntersector<> triangle_intersector(reinterpret_cast(m_hm_plane.vertices.data()), m_hm_plane.idx.data(), sizeof(vertex_t)); + nanort::TriangleIntersection<> isect; + hit += m_rt_accel.Traverse(ray, triangle_intersector, &isect); + } + out = glm::lerp(glm::vec4(255, 255, 255, 255), glm::vec4(50, 50, 50, 255), hit / 16.f); + } + pb_value++; + } +#if _WIN32 || __IOS__ || __OSX__ + ); +#endif + } + ); + while (pb_value < fb.getHeight()) + { + pb->m_progress->SetWidthP((float)pb_value / (float)fb.getHeight() * 100.f); + async_update(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + worker.join(); + pb->destroy(); //stbi_write_jpg("bake-out.jpg", fb.getWidth(), fb.getHeight(), 4, data_out.get(), 75); m_texture.update(data_out.get()); m_texture.create_mipmaps(); diff --git a/src/node_panel_grid.h b/src/node_panel_grid.h index 822a4d5..e2efdcb 100644 --- a/src/node_panel_grid.h +++ b/src/node_panel_grid.h @@ -9,6 +9,9 @@ #include "node_button.h" #include "shape.h" #include "image.h" + +#define NANORT_USE_CPP11_FEATURE +#define NANORT_ENABLE_PARALLEL_BUILD #include "nanort.h" class NodePanelGrid : public Node diff --git a/src/node_panel_layer.cpp b/src/node_panel_layer.cpp index db10d9c..c333f85 100644 --- a/src/node_panel_layer.cpp +++ b/src/node_panel_layer.cpp @@ -132,6 +132,8 @@ void NodePanelLayer::init() add_layer(); if (on_layer_add) on_layer_add(this); + if (on_layer_change) + on_layer_change(this, -1, m_layers_container->get_child_index(m_current_layer)); update_attributes(); }; btn_remove->on_click = [this](Node*) { @@ -189,8 +191,6 @@ NodeLayer* NodePanelLayer::add_layer(const char* name) m_current_layer = l; m_current_layer->m_selected = true; m_layers.push_back(l); - if (on_layer_change) - on_layer_change(this, -1, m_layers_container->get_child_index(m_current_layer)); update_attributes(); return l; }