diff --git a/common/bspinfo.cc b/common/bspinfo.cc index 6fc71100..991aa2e8 100644 --- a/common/bspinfo.cc +++ b/common/bspinfo.cc @@ -235,7 +235,7 @@ static faceextents_t get_face_extents(const mbsp_t &bsp, const bspxentries_t &bs (float)nth_bit(reinterpret_cast(bspx.at("LMSHIFT").data())[&face - bsp.dfaces.data()])}; } -full_atlas_t build_lightmap_atlas(const mbsp_t &bsp, const bspxentries_t &bspx, bool use_bspx, bool use_decoupled) +full_atlas_t build_lightmap_atlas(const mbsp_t &bsp, const bspxentries_t &bspx, const std::vector &litdata, bool use_bspx, bool use_decoupled) { struct face_rect { @@ -248,7 +248,19 @@ full_atlas_t build_lightmap_atlas(const mbsp_t &bsp, const bspxentries_t &bspx, }; constexpr size_t atlas_size = 512; - const bool is_rgb = bsp.loadversion->game->has_rgb_lightmap; + const uint8_t *lightdata_source; + bool is_rgb; + bool is_lit; + + if (!litdata.empty()) { + is_lit = true; + is_rgb = true; + lightdata_source = litdata.data(); + } else { + is_lit = false; + is_rgb = bsp.loadversion->game->has_rgb_lightmap; + lightdata_source = bsp.dlightdata.data(); + } struct atlas { @@ -408,7 +420,7 @@ full_atlas_t build_lightmap_atlas(const mbsp_t &bsp, const bspxentries_t &bspx, } auto in_pixel = - bsp.dlightdata.begin() + rect.lightofs + (rect.extents.numsamples() * (is_rgb ? 3 : 1) * style_index); + lightdata_source + ((is_lit ? 3 : 1) * rect.lightofs) + (rect.extents.numsamples() * (is_rgb ? 3 : 1) * style_index); for (size_t y = 0; y < rect.extents.height(); y++) { for (size_t x = 0; x < rect.extents.width(); x++) { @@ -475,7 +487,8 @@ full_atlas_t build_lightmap_atlas(const mbsp_t &bsp, const bspxentries_t &bspx, static void export_obj_and_lightmaps(const mbsp_t &bsp, const bspxentries_t &bspx, bool use_bspx, bool use_decoupled, fs::path obj_path, fs::path lightmaps_path) { - const auto atlas = build_lightmap_atlas(bsp, bspx, use_bspx, use_decoupled); + // FIXME: pass in .lit + const auto atlas = build_lightmap_atlas(bsp, bspx, {}, use_bspx, use_decoupled); if (atlas.facenum_to_lightmap_uvs.empty()) { return; diff --git a/include/common/bspinfo.hh b/include/common/bspinfo.hh index f34412bf..1fc1267c 100644 --- a/include/common/bspinfo.hh +++ b/include/common/bspinfo.hh @@ -41,6 +41,6 @@ struct full_atlas_t std::map style_to_lightmap_atlas; }; -full_atlas_t build_lightmap_atlas(const mbsp_t &bsp, const bspxentries_t &bspx, bool use_bspx, bool use_decoupled); +full_atlas_t build_lightmap_atlas(const mbsp_t &bsp, const bspxentries_t &bspx, const std::vector &litdata, bool use_bspx, bool use_decoupled); void serialize_bsp(const bspdata_t &bspdata, const mbsp_t &bsp, const fs::path &name); diff --git a/lightpreview/mainwindow.cpp b/lightpreview/mainwindow.cpp index c75afc2a..f46e2aa2 100644 --- a/lightpreview/mainwindow.cpp +++ b/lightpreview/mainwindow.cpp @@ -725,6 +725,17 @@ int MainWindow::compileMap(const QString &file, bool is_reload) return 1; } + // try to load .lit + auto lit_path = fs_path; + lit_path.replace_extension(".lit"); + + try { + m_litdata = LoadLitFile(lit_path); + } catch (const std::runtime_error &error) { + logging::print("error loading lit: {}", error.what()); + m_litdata = {}; + } + return 0; } @@ -746,7 +757,7 @@ void MainWindow::compileThreadExited() auto ents = EntData_Parse(bsp); // build lightmap atlas - auto atlas = build_lightmap_atlas(bsp, m_bspdata.bspx.entries, false, bspx_decoupled_lm->isChecked()); + auto atlas = build_lightmap_atlas(bsp, m_bspdata.bspx.entries, m_litdata, false, bspx_decoupled_lm->isChecked()); glView->renderBSP(m_mapFile, bsp, m_bspdata.bspx.entries, ents, atlas, render_settings, bspx_normals->isChecked()); diff --git a/lightpreview/mainwindow.h b/lightpreview/mainwindow.h index 9de058c7..b8e71bcb 100644 --- a/lightpreview/mainwindow.h +++ b/lightpreview/mainwindow.h @@ -71,6 +71,7 @@ private: bool m_fileWasReload = false; QString m_mapFile; bspdata_t m_bspdata; + std::vector m_litdata; settings::common_settings render_settings; qint64 m_fileSize = -1; ETLogTab m_activeLogTab = ETLogTab::TAB_LIGHTPREVIEW;