Skip to content

Commit

Permalink
Merge branch 'brushbsp' of https://github.com/ericwa/ericw-tools into…
Browse files Browse the repository at this point in the history
… brushbsp
  • Loading branch information
Paril committed Nov 2, 2023
2 parents 7186f41 + 5229cd6 commit 24e0cb0
Show file tree
Hide file tree
Showing 31 changed files with 667 additions and 90 deletions.
35 changes: 35 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ endif ()

add_definitions(-DERICWTOOLS_VERSION="${GIT_DESCRIBE}")

# MINGW stuff
if(MINGW)
find_file(LIB_GCC_S_SEH_1_DLL NAMES "libgcc_s_seh-1.dll")
find_file(LIB_STDCPP_6_DLL NAMES "libstdc++-6.dll")
find_file(LIB_WINPTHREAD_1_DLL NAMES "libwinpthread-1.dll")

if(LIB_GCC_S_SEH_1_DLL)
install(FILES ${LIB_GCC_S_SEH_1_DLL} DESTINATION bin)
endif()
if(LIB_STDCPP_6_DLL)
install(FILES ${LIB_STDCPP_6_DLL} DESTINATION bin)
endif()
if(LIB_WINPTHREAD_1_DLL)
install(FILES ${LIB_WINPTHREAD_1_DLL} DESTINATION bin)
endif()
endif()

function(copy_mingw_dlls TARGETNAME)
if (LIB_GCC_S_SEH_1_DLL)
add_custom_command(TARGET ${TARGETNAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${LIB_GCC_S_SEH_1_DLL}" "$<TARGET_FILE_DIR:${TARGETNAME}>"
)
endif()
if (LIB_STDCPP_6_DLL)
add_custom_command(TARGET ${TARGETNAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${LIB_STDCPP_6_DLL}" "$<TARGET_FILE_DIR:${TARGETNAME}>"
)
endif()
if (LIB_WINPTHREAD_1_DLL)
add_custom_command(TARGET ${TARGETNAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${LIB_WINPTHREAD_1_DLL}" "$<TARGET_FILE_DIR:${TARGETNAME}>"
)
endif()
endfunction()

if (WIN32)
set("NO_ITERATOR_DEBUG" FALSE CACHE BOOL "Whether to use MSVC iterator debugging or not")

Expand Down
1 change: 1 addition & 0 deletions bspinfo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ add_custom_command(TARGET bspinfo POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:TBB::tbb>" "$<TARGET_FILE_DIR:bspinfo>"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:TBB::tbbmalloc>" "$<TARGET_FILE_DIR:bspinfo>"
)
copy_mingw_dlls(bspinfo)

install(TARGETS bspinfo RUNTIME DESTINATION bin)
1 change: 1 addition & 0 deletions bsputil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ add_custom_command(TARGET bsputil POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:TBB::tbb>" "$<TARGET_FILE_DIR:bsputil>"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:TBB::tbbmalloc>" "$<TARGET_FILE_DIR:bsputil>"
)
copy_mingw_dlls(bsputil)

install(TARGETS bsputil RUNTIME DESTINATION bin)
8 changes: 4 additions & 4 deletions common/cmdlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,8 @@ bool need_swap(std::ios_base &os)

void *q_aligned_malloc(size_t align, size_t size)
{
#ifdef _mm_malloc
return _mm_malloc(size, align);
#ifdef _WIN32
return _aligned_malloc(size, align);
#elif __STDC_VERSION__ >= 201112L
return aligned_alloc(align, size);
#else
Expand All @@ -722,8 +722,8 @@ void *q_aligned_malloc(size_t align, size_t size)

void q_aligned_free(void *ptr)
{
#ifdef _mm_malloc
_mm_free(ptr);
#ifdef _WIN32
_aligned_free(ptr);
#else
free(ptr);
#endif
Expand Down
103 changes: 100 additions & 3 deletions common/decompile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ static void DecompileLeaf(const std::vector<decomp_plane_t> &planestack, const m
}

static std::vector<compiled_brush_t> DecompileLeafTaskGeometryOnly(
const mbsp_t *bsp, const leaf_decompile_task &task, std::optional<qvec3d> &brush_offset)
const mbsp_t *bsp, const leaf_decompile_task &task, const std::optional<qvec3d> &brush_offset)
{
compiled_brush_t brush;
brush.source = task.brush;
Expand All @@ -826,7 +826,7 @@ static std::vector<compiled_brush_t> DecompileLeafTaskGeometryOnly(
}

static std::vector<compiled_brush_t> DecompileLeafTask(
const mbsp_t *bsp, const decomp_options &options, leaf_decompile_task &task, std::optional<qvec3d> &brush_offset)
const mbsp_t *bsp, const decomp_options &options, leaf_decompile_task &task, const std::optional<qvec3d> &brush_offset)
{
std::vector<decomp_brush_t> finalBrushes;
if (bsp->loadversion->game->id == GAME_QUAKE_II && !options.ignoreBrushes) {
Expand Down Expand Up @@ -935,6 +935,45 @@ static std::vector<compiled_brush_t> DecompileLeafTask(
return finalCompiledBrushes;
}

static std::vector<compiled_brush_t> DecompileLeafTaskLeafVisualization(
const mbsp_t *bsp, leaf_decompile_task &task, const std::optional<qvec3d> &brush_offset)
{
std::vector<decomp_brush_t> finalBrushes;

RemoveRedundantPlanes(task.allPlanes);

if (task.allPlanes.empty()) {
printf("warning, skipping empty brush\n");
return {};
}

// fmt::print("before: {} after {}\n", task.allPlanes.size(), reducedPlanes.size());

auto initialBrush = BuildInitialBrush_Q2(bsp, task, task.allPlanes);
// assert(initialBrush.checkPoints());

finalBrushes = {initialBrush};

std::vector<compiled_brush_t> finalCompiledBrushes;
for (decomp_brush_t &finalBrush : finalBrushes) {
compiled_brush_t brush;
brush.source = task.brush;
brush.brush_offset = brush_offset;
brush.contents = task.leaf ? contentflags_t{task.leaf->contents} : contentflags_t{task.contents.value()};

for (auto &finalSide : finalBrush.sides) {
compiled_brush_side_t &side = brush.sides.emplace_back();
side.plane = finalSide.plane;
side.winding = std::move(finalSide.winding);
side.source = finalSide.plane.source;
}

finalCompiledBrushes.push_back(std::move(brush));
}

return finalCompiledBrushes;
}

/**
* @param front whether we are visiting the front side of the node plane
*/
Expand Down Expand Up @@ -1041,7 +1080,7 @@ static void AddMapBoundsToStack(std::vector<decomp_plane_t> &planestack, const m
}

static std::vector<compiled_brush_t> DecompileBrushTask(
const mbsp_t *bsp, const decomp_options &options, leaf_decompile_task &task, std::optional<qvec3d> &brush_offset)
const mbsp_t *bsp, const decomp_options &options, leaf_decompile_task &task, const std::optional<qvec3d> &brush_offset)
{
for (size_t i = 0; i < task.brush->numsides; i++) {
const q2_dbrushside_qbism_t *side = &bsp->dbrushsides[task.brush->firstside + i];
Expand Down Expand Up @@ -1322,3 +1361,61 @@ void DecompileBSP(const mbsp_t *bsp, const decomp_options &options, std::ofstrea
DecompileEntity(bsp, options, file, entdicts[i], i == 0);
}
}

// MARK: - leaf visualization

static std::vector<leaf_visualization_t> CompiledBrushesToLeafVisualization(std::vector<std::vector<compiled_brush_t>> in)
{
std::vector<leaf_visualization_t> result;

for (auto &brush_list : in) {
for (auto &brush : brush_list) {
leaf_visualization_t output_leaf;

// move over windings
for (auto &in_side : brush.sides) {
if (in_side.winding) {
output_leaf.windings.push_back(std::move(*in_side.winding));
}
}
output_leaf.contents = brush.contents;
// FIXME: copy over source leafnum

result.push_back(std::move(output_leaf));
}
}

return result;
}

std::vector<leaf_visualization_t> VisualizeLeafs(const mbsp_t &bsp, int modelnum, int hullnum)
{
const dmodelh2_t *model = &bsp.dmodels[modelnum];

std::vector<std::vector<compiled_brush_t>> compiledBrushes;
std::vector<decomp_plane_t> stack;
std::vector<leaf_decompile_task> tasks;

if (hullnum > 0) {
// recursively visit the clipnodes to gather up a list of clipnode leafs to decompile

AddMapBoundsToStack(stack, &bsp, aabb3d(qvec3d(model->mins), qvec3d(model->maxs)));

DecompileClipNode(stack, &bsp, &bsp.dclipnodes[model->headnode[hullnum]], tasks);
} else {
// recursively visit the nodes to gather up a list of leafs to decompile
auto headnode = BSP_GetNode(&bsp, model->headnode[0]);

AddMapBoundsToStack(stack, &bsp, aabb3d(qvec3d(headnode->mins), qvec3d(headnode->maxs)));

DecompileNode(stack, &bsp, headnode, tasks);
}

// decompile the leafs in parallel
compiledBrushes.resize(tasks.size());
tbb::parallel_for(static_cast<size_t>(0), tasks.size(), [&](const size_t &i) {
compiledBrushes[i] = DecompileLeafTaskLeafVisualization(&bsp, tasks[i], std::nullopt);
});

return CompiledBrushesToLeafVisualization(std::move(compiledBrushes));
}
2 changes: 1 addition & 1 deletion common/threads.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <windows.h>
#endif

static std::unique_ptr<tbb::global_control> tbbGlobalControl;
Expand Down
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Welcome to ericw-tools's documentation!
light
bspinfo
bsputil
maputil
lightpreview
changelog


Expand Down
19 changes: 19 additions & 0 deletions docs/lightpreview.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
============
lightpreview
============

lightpreview - GUI unifying :doc:`qbsp`, :doc:`vis`, and :doc:`light`, with a 3D viewport for visualizing the output.

Controls
========

- :kbd:`WASDQE` fly movement

View modes
----------

- :kbd:`Alt-1` Lightmapped
- :kbd:`Alt-2` Lightmap Only
- :kbd:`Alt-3` Fullbright
- :kbd:`Alt-4` Normals
- :kbd:`Alt-5` Flat shading
78 changes: 78 additions & 0 deletions docs/maputil.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
=======
maputil
=======

maputil - utiltiy for working with Quake MAP files

Synopsis
========

**bsputil** [OPTION]... MAPFILE

Options
=======

.. program:: maputil

.. option:: --script <path to Lua script file>

execute the given Lua script.

.. option:: --query \"<Lua expression>\"

perform a query on entities and print out matching results.
see docs for more details on globals.
note that query has the same access as script
but is more suitable for small read-only operations.

.. option:: --strip_extended_info

removes extended Quake II/III information on faces.

.. option:: --convert <quake | valve | etp | bp>

convert the current map to the given format.

.. option:: --save \"<output path>\"

save the current map to the given output path.

.. option:: --game <quake | quake2 | hexen2 | halflife>

set the current game; used for certain conversions
or operations.

Lua layout
==========

::

entities = table[]
[E].dict = array
[D] = [ key, value ]
[E].brushes = table[]
[S].texture = string
[S].plane_points = [ [ x, y, z ] [ x, y, z ] [ x, y, z ] ]
[S].raw = table (can only contain ONE member:)
.quaked = table
.shift = [ x, y ]
.rotate = number
.scale = [ x, y ]
.valve = table
.axis = [ [ x, y, z ] [ x, y, z ] ]
.shift = [ x, y ]
.rotate = number
.scale = [ x, y ]
.bp = table
.axis = [ [ x, y, z ] [ x, y, z ] ]
.etp = table
.shift = [ x, y ]
.rotate = number
.scale = [ x, y ]
.tx2 = boolean
[S].info = table or nil
.contents = number
.value = number
.flags = number
[S].plane = [ x, y, z, d ] (read-only)
[S].vecs = [ [ x, y, z, d ] [ x, y, z, d ] ] (read-only)
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ sphinxcontrib-qthelp==1.0.3
# via sphinx
sphinxcontrib-serializinghtml==1.1.5
# via sphinx
urllib3==2.0.2
urllib3==2.0.7
# via requests
1 change: 1 addition & 0 deletions include/common/cmdlib.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include <array>
#include <cstdint>
#include <cstring> // for memcpy()
#include <string>
#include <string_view>
Expand Down
18 changes: 18 additions & 0 deletions include/common/decompile.hh
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#pragma once

#include <common/polylib.hh>
#include <common/bspfile.hh>

#include <optional>
#include <vector>
#include <iosfwd>

struct mbsp_t;
Expand All @@ -24,3 +29,16 @@ struct decomp_options
};

void DecompileBSP(const mbsp_t *bsp, const decomp_options &options, std::ofstream &file);

struct leaf_visualization_t
{
std::vector<polylib::winding_t> windings;
contentflags_t contents;

/**
* Index of this leaf in bsp.dleafs. Only valid for hull0 leafs, not for clipnodes.
*/
std::optional<int> leafnum;
};

std::vector<leaf_visualization_t> VisualizeLeafs(const mbsp_t &bsp, int modelnum, int hullnum);
1 change: 1 addition & 0 deletions include/common/mathlib.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <cfloat>
#include <cmath>
#include <cstdint>
#include <vector>
#include <algorithm>

Expand Down
5 changes: 5 additions & 0 deletions include/light/ltface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ struct lightgrid_sample_t
qvec3b round_to_int() const;
float brightness() const;

/**
* - if !used, style and color are ignored for equality
* - if a color component is nan, nan is considered equal to nan for the purposes of this comparison
*/
bool operator==(const lightgrid_sample_t &other) const;
bool operator!=(const lightgrid_sample_t &other) const; //gcc9 workaround
};

struct lightgrid_samples_t
Expand Down
Loading

0 comments on commit 24e0cb0

Please sign in to comment.