diff --git a/src/common/filesystem/filesystem.cpp b/src/common/filesystem/filesystem.cpp index 5af6215fb..21acc4e2f 100644 --- a/src/common/filesystem/filesystem.cpp +++ b/src/common/filesystem/filesystem.cpp @@ -841,7 +841,7 @@ int FileSystem::GetFileFlags (int lump) uint32_t FileSystem::LumpNameHash (const char *s) { - const uint32_t *table = GetCRCTable ();; + const uint32_t *table = GetCRCTable (); uint32_t hash = 0xffffffff; int i; diff --git a/src/common/models/model_obj.h b/src/common/models/model_obj.h index e70679b0a..fa41ec0fc 100644 --- a/src/common/models/model_obj.h +++ b/src/common/models/model_obj.h @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/models/models_obj.cpp b/src/common/models/models_obj.cpp index 65ec72915..5d6892798 100644 --- a/src/common/models/models_obj.cpp +++ b/src/common/models/models_obj.cpp @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/gl/gl_renderstate.cpp b/src/common/rendering/gl/gl_renderstate.cpp index 5d7fd774b..10baa3e66 100644 --- a/src/common/rendering/gl/gl_renderstate.cpp +++ b/src/common/rendering/gl/gl_renderstate.cpp @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/gl/gl_renderstate.h b/src/common/rendering/gl/gl_renderstate.h index 8b9bba25c..b89a02ad8 100644 --- a/src/common/rendering/gl/gl_renderstate.h +++ b/src/common/rendering/gl/gl_renderstate.h @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/gl/gl_shader.cpp b/src/common/rendering/gl/gl_shader.cpp index c4e20f7ba..c69aa79b2 100644 --- a/src/common/rendering/gl/gl_shader.cpp +++ b/src/common/rendering/gl/gl_shader.cpp @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/gl/gl_shader.h b/src/common/rendering/gl/gl_shader.h index 6eb2b2226..b93299e08 100644 --- a/src/common/rendering/gl/gl_shader.h +++ b/src/common/rendering/gl/gl_shader.h @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/gles/gles_renderstate.cpp b/src/common/rendering/gles/gles_renderstate.cpp index 76718f649..451d3a616 100644 --- a/src/common/rendering/gles/gles_renderstate.cpp +++ b/src/common/rendering/gles/gles_renderstate.cpp @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/gles/gles_renderstate.h b/src/common/rendering/gles/gles_renderstate.h index 4dd8516ec..a234e25b6 100644 --- a/src/common/rendering/gles/gles_renderstate.h +++ b/src/common/rendering/gles/gles_renderstate.h @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/gles/gles_shader.cpp b/src/common/rendering/gles/gles_shader.cpp index daba34f6d..74bd426d9 100644 --- a/src/common/rendering/gles/gles_shader.cpp +++ b/src/common/rendering/gles/gles_shader.cpp @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/gles/gles_shader.h b/src/common/rendering/gles/gles_shader.h index 72a242c1b..11b28ac7a 100644 --- a/src/common/rendering/gles/gles_shader.h +++ b/src/common/rendering/gles/gles_shader.h @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/hwrenderer/data/hw_aabbtree.cpp b/src/common/rendering/hwrenderer/data/hw_aabbtree.cpp index 0a8c81900..20a1d39a1 100644 --- a/src/common/rendering/hwrenderer/data/hw_aabbtree.cpp +++ b/src/common/rendering/hwrenderer/data/hw_aabbtree.cpp @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/hwrenderer/data/hw_shadowmap.cpp b/src/common/rendering/hwrenderer/data/hw_shadowmap.cpp index 18f23e1d5..e53609840 100644 --- a/src/common/rendering/hwrenderer/data/hw_shadowmap.cpp +++ b/src/common/rendering/hwrenderer/data/hw_shadowmap.cpp @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/rendering/hwrenderer/data/hw_skydome.cpp b/src/common/rendering/hwrenderer/data/hw_skydome.cpp index eae09ffbb..43e56adec 100644 --- a/src/common/rendering/hwrenderer/data/hw_skydome.cpp +++ b/src/common/rendering/hwrenderer/data/hw_skydome.cpp @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/textures/hw_material.cpp b/src/common/textures/hw_material.cpp index 3f149d728..3abd4fae9 100644 --- a/src/common/textures/hw_material.cpp +++ b/src/common/textures/hw_material.cpp @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/utility/cmdlib.h b/src/common/utility/cmdlib.h index f0eef9b40..392d0f46d 100644 --- a/src/common/utility/cmdlib.h +++ b/src/common/utility/cmdlib.h @@ -80,11 +80,16 @@ bool ScanDirectory(TArray &list, const char *dirpath); bool IsAbsPath(const char*); FString M_ZLibError(int zerrnum); -inline int32_t Scale(int32_t a, int32_t b, int32_t c) +inline constexpr int32_t Scale(int32_t a, int32_t b, int32_t c) { return (int32_t)(((int64_t)a * b) / c); } +inline constexpr double Scale(double a, double b, double c) +{ + return (a * b) / c; +} + class FileReader; struct MD5Context; diff --git a/src/common/utility/geometry.h b/src/common/utility/geometry.h new file mode 100644 index 000000000..ff8d9ddf1 --- /dev/null +++ b/src/common/utility/geometry.h @@ -0,0 +1,177 @@ +#pragma once + +/* +** geometry.h +** basic geometry math routines +** +**--------------------------------------------------------------------------- +** Copyright 2005-2022 Christoph Oelckers +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +*/ + +#include "vectors.h" + +inline DVector2 rotatepoint(const DVector2& pivot, const DVector2& point, DAngle angle) +{ + return (point - pivot).Rotated(angle) + pivot; +} + +//========================================================================== +// +// +// +//========================================================================== + +inline double PointOnLineSide(double x, double y, double linex, double liney, double deltax, double deltay) +{ + return (x - linex) * deltay - (y - liney) * deltax; +} + +template +inline double PointOnLineSide(const TVector2& pos, const TVector2& linestart, const TVector2& lineend) +{ + return (pos.X - linestart.X) * (lineend.Y - linestart.Y) - (pos.Y - linestart.Y) * (lineend.X - linestart.X); +} + +//========================================================================== +// +// +// +//========================================================================== + +inline double SquareDist(double lx1, double ly1, double lx2, double ly2) +{ + double dx = lx2 - lx1; + double dy = ly2 - ly1; + return dx * dx + dy * dy; +} + +// This is for cases where only the factor is needed, and pre-validation was performed. +inline double NearestPointOnLineFast(double px, double py, double lx1, double ly1, double lx2, double ly2) +{ + double wall_length = SquareDist(lx1, ly1, lx2, ly2); + assert(wall_length > 0); + return ((px - lx1) * (lx2 - lx1) + (py - ly1) * (ly2 - ly1)) / wall_length; +} + + +inline DVector2 NearestPointOnLine(double px, double py, double lx1, double ly1, double lx2, double ly2, bool clamp = true) +{ + double wall_length = SquareDist(lx1, ly1, lx2, ly2); + + if (wall_length == 0) + { + return { lx1, ly1 }; + } + + double t = ((px - lx1) * (lx2 - lx1) + (py - ly1) * (ly2 - ly1)) / wall_length; + if (clamp) + { + if (t <= 0) return { lx1, ly1 }; + if (t >= 1) return { lx2, ly2 }; + } + double xx = lx1 + t * (lx2 - lx1); + double yy = ly1 + t * (ly2 - ly1); + return { xx, yy }; +} + +//========================================================================== +// +// +// +//========================================================================== + +inline double SquareDistToLine(double px, double py, double lx1, double ly1, double lx2, double ly2) +{ + double wall_length = SquareDist(lx1, ly1, lx2, ly2); + + if (wall_length == 0) return SquareDist(px, py, lx1, ly1); + + double t = ((px - lx1) * (lx2 - lx1) + (py - ly1) * (ly2 - ly1)) / wall_length; + t = clamp(t, 0., 1.); + double xx = lx1 + t * (lx2 - lx1); + double yy = ly1 + t * (ly2 - ly1); + return SquareDist(px, py, xx, yy); +} + +//========================================================================== +// +// taken from GZDoom with the divline_t parameters removed +// +//========================================================================== + +inline double InterceptVector(double v2x, double v2y, double v2dx, double v2dy, double v1x, double v1y, double v1dx, double v1dy) +{ + double den = v1dy * v2dx - v1dx * v2dy; + + if (den == 0) + return 0; // parallel + + double num = (v1x - v2x) * v1dy + (v2y - v1y) * v1dx; + return num / den; +} + +//========================================================================== +// +// Essentially two InterceptVector calls. We can reduce the calculations +// because the denominators for both calculations only differ by their sign. +// +//========================================================================== + +inline double InterceptLineSegments(double v2x, double v2y, double v2dx, double v2dy, double v1x, double v1y, double v1dx, double v1dy, double* pfactor1 = nullptr, bool forcansee = false) +{ + double den = v1dy * v2dx - v1dx * v2dy; + + if (den == 0 || (forcansee && den < 0)) // cansee does this added check here, aside from that its logic is virtually the same. + return 0; // parallel + + // perform the division first for better parallelization. + den = 1 / den; + + double factor1 = ((v2x - v1x) * v2dy + (v1y - v2y) * v2dx) * -den; + if (factor1 < 0 || factor1 > 1) return -FLT_MAX; // no intersection + if (pfactor1) *pfactor1 = factor1; + + return ((v1x - v2x) * v1dy + (v2y - v1y) * v1dx) * den; // this one's for the line segment where we want to get the intercept factor for so it needs to be last. +} + +//========================================================================== +// +// calculates intersection between a plane and line in 3D +// +//========================================================================== + +inline double LinePlaneIntersect(const DVector3& start, const DVector3& trace, const DVector3& ppoint, const DVector3& pvec1, const DVector3& pvec2) +{ + auto normal = pvec1 ^ pvec2; // we do not need a unit vector here. + double dist = normal.dot(ppoint); + double dotStart = normal.dot(start); + double dotTrace = normal.dot(trace); + if (dotTrace == 0) return -FLT_MAX; + return (dist - dotStart) / dotTrace; // we are only interested in the factor +} + diff --git a/src/common/utility/m_fixed.h b/src/common/utility/m_fixed.h index e4dd17bf9..bf7778e79 100644 --- a/src/common/utility/m_fixed.h +++ b/src/common/utility/m_fixed.h @@ -9,7 +9,6 @@ __forceinline constexpr int32_t MulScale(int32_t a, int32_t b, int32_t shift) { return (int32_t)(((int64_t)a * b) >> shift); } __forceinline constexpr double MulScaleF(double a, double b, int32_t shift) { return (a * b) * (1. / (uint32_t(1) << shift)); } __forceinline constexpr int32_t DMulScale(int32_t a, int32_t b, int32_t c, int32_t d, int32_t shift) { return (int32_t)(((int64_t)a * b + (int64_t)c * d) >> shift); } -__forceinline constexpr int32_t TMulScale(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e, int32_t f, int32_t shift) { return (int32_t)(((int64_t)a * b + (int64_t)c * d + (int64_t)e * f) >> shift); } __forceinline constexpr int32_t DivScale(int32_t a, int32_t b, int shift) { return (int32_t)(((int64_t)a << shift) / b); } __forceinline constexpr int64_t DivScaleL(int64_t a, int64_t b, int shift) { return ((a << shift) / b); } @@ -21,6 +20,11 @@ constexpr fixed_t FloatToFixed(double f) return int(f * (1 << b)); } +constexpr fixed_t FloatToFixed(double f, int b) +{ + return int(f * (1 << b)); +} + template inline constexpr fixed_t IntToFixed(int32_t f) { @@ -33,6 +37,11 @@ inline constexpr double FixedToFloat(fixed_t f) return f * (1. / (1 << b)); } +inline constexpr double FixedToFloat(fixed_t f, int b) +{ + return f * (1. / (1 << b)); +} + template inline constexpr int32_t FixedToInt(fixed_t f) { diff --git a/src/common/utility/parallel_for.h b/src/common/utility/parallel_for.h index d3e0e5de0..7f7ee5b6c 100644 --- a/src/common/utility/parallel_for.h +++ b/src/common/utility/parallel_for.h @@ -6,7 +6,7 @@ // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, diff --git a/src/common/utility/vectors.h b/src/common/utility/vectors.h index 55baafc1a..52cbdadf3 100644 --- a/src/common/utility/vectors.h +++ b/src/common/utility/vectors.h @@ -310,13 +310,13 @@ struct TVector2 } // Returns a vector rotated 90 degrees clockwise. - TVector2 Rotated90CW() + TVector2 Rotated90CW() const { return TVector2(Y, -X); } // Returns a vector rotated 90 degrees counterclockwise. - TVector2 Rotated90CCW() + TVector2 Rotated90CCW() const { return TVector2(-Y, X); } @@ -664,6 +664,11 @@ struct TVector3 return X*other.X + Y*other.Y + Z*other.Z; } + vec_t dot (const TVector3& other) const + { + return X * other.X + Y * other.Y + Z * other.Z; + } + // Cross product TVector3 operator ^ (const TVector3 &other) const { @@ -1624,7 +1629,7 @@ struct TRotator TRotator &operator/= (const Angle &scalar) { Angle mul(1 / scalar.Degrees_); - Pitch *= scalar, Yaw *= scalar, Roll *= scalar; + Pitch *= mul, Yaw *= mul, Roll *= mul; return *this; }