From 1a0fb22e40afb1e993d7a9fbe67d7766687f9ae7 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 7 Aug 2016 22:55:44 -0600 Subject: [PATCH] light: move some helper functions to common/bsputils.{h,cc} --- CMakeLists.txt | 3 +- common/bsputils.cc | 63 +++++++++++++++++++++++++++++++++++++++ include/common/bsputils.h | 38 +++++++++++++++++++++++ include/light/light.hh | 3 -- light/CMakeLists.txt | 1 + light/light.cc | 42 +------------------------- light/ltface.cc | 2 ++ 7 files changed, 107 insertions(+), 45 deletions(-) create mode 100644 common/bsputils.cc create mode 100644 include/common/bsputils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 85f07b5d..3f9c986d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,8 @@ set(COMMON_INCLUDES ${CMAKE_SOURCE_DIR}/include/common/scriplib.h ${CMAKE_SOURCE_DIR}/include/common/threads.h ${CMAKE_SOURCE_DIR}/include/common/trilib.h - ${CMAKE_SOURCE_DIR}/include/common/wadlib.h) + ${CMAKE_SOURCE_DIR}/include/common/wadlib.h + ${CMAKE_SOURCE_DIR}/include/common/bsputils.h) find_package (Threads) diff --git a/common/bsputils.cc b/common/bsputils.cc new file mode 100644 index 00000000..a0c8d9ae --- /dev/null +++ b/common/bsputils.cc @@ -0,0 +1,63 @@ +/* Copyright (C) 1996-1997 Id Software, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + 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, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + See file, 'COPYING', for details. + */ + +#include +#include + +/* small helper that just retrieves the correct vertex from face->surfedge->edge lookups */ +int GetSurfaceVertex(const bsp2_t *bsp, const bsp2_dface_t *f, int v) +{ + int edge = f->firstedge + v; + edge = bsp->dsurfedges[edge]; + if (edge < 0) + return bsp->dedges[-edge].v[1]; + return bsp->dedges[edge].v[0]; +} + +static void +Vertex_GetPos(const bsp2_t *bsp, int num, vec3_t out) +{ + assert(num >= 0 && num < bsp->numvertexes); + const dvertex_t *v = &bsp->dvertexes[num]; + + for (int i=0; i<3; i++) + out[i] = v->point[i]; +} + +void +Face_Normal(const bsp2_t *bsp, const bsp2_dface_t *f, vec3_t norm) +{ + if (f->side) + VectorSubtract(vec3_origin, bsp->dplanes[f->planenum].normal, norm); + else + VectorCopy(bsp->dplanes[f->planenum].normal, norm); +} + +plane_t +Face_Plane(const bsp2_t *bsp, const bsp2_dface_t *f) +{ + const int vertnum = GetSurfaceVertex(bsp, f, 0); + vec3_t vertpos; + Vertex_GetPos(bsp, vertnum, vertpos); + + plane_t res; + Face_Normal(bsp, f, res.normal); + res.dist = DotProduct(vertpos, res.normal); + return res; +} diff --git a/include/common/bsputils.h b/include/common/bsputils.h new file mode 100644 index 00000000..cf34b4ac --- /dev/null +++ b/include/common/bsputils.h @@ -0,0 +1,38 @@ +/* Copyright (C) 1996-1997 Id Software, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + 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, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + See file, 'COPYING', for details. + */ + +#ifndef __COMMON_BSPUTILS_H__ +#define __COMMON_BSPUTILS_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int GetSurfaceVertex(const bsp2_t *bsp, const bsp2_dface_t *f, int v); +void Face_Normal(const bsp2_t *bsp, const bsp2_dface_t *f, vec3_t norm); +plane_t Face_Plane(const bsp2_t *bsp, const bsp2_dface_t *f); + +#ifdef __cplusplus +} +#endif + +#endif /* __COMMON_BSPUTILS_H__ */ diff --git a/include/light/light.hh b/include/light/light.hh index 758576bd..22f42979 100644 --- a/include/light/light.hh +++ b/include/light/light.hh @@ -655,10 +655,7 @@ lockable_setting_t *FindSetting(std::string name); void SetGlobalSetting(std::string name, std::string value, bool cmdline); void GetFileSpace(byte **lightdata, byte **colordata, byte **deluxdata, int size); const modelinfo_t *ModelInfoForFace(const bsp2_t *bsp, int facenum); -int GetSurfaceVertex(const bsp2_t *bsp, const bsp2_dface_t *f, int v); -void Face_Normal(const bsp2_t *bsp, const bsp2_dface_t *f, vec3_t norm); const vec_t *GetSurfaceVertexNormal(const bsp2_t *bsp, const bsp2_dface_t *f, const int v); -plane_t Face_Plane(const bsp2_t *bsp, const bsp2_dface_t *f); const bsp2_dface_t *Face_EdgeIndexSmoothed(const bsp2_t *bsp, const bsp2_dface_t *f, const int edgeindex); const std::vector &BounceLights(); bool Leaf_HasSky(const bsp2_t *bsp, const bsp2_dleaf_t *leaf); diff --git a/light/CMakeLists.txt b/light/CMakeLists.txt index 2e12be3f..813e7dcf 100644 --- a/light/CMakeLists.txt +++ b/light/CMakeLists.txt @@ -20,6 +20,7 @@ set(LIGHT_SOURCES ${CMAKE_SOURCE_DIR}/common/log.c ${CMAKE_SOURCE_DIR}/common/threads.c ${CMAKE_SOURCE_DIR}/common/polylib.c + ${CMAKE_SOURCE_DIR}/common/bsputils.cc ${COMMON_INCLUDES} ${LIGHT_INCLUDES}) diff --git a/light/light.cc b/light/light.cc index 1bd05a37..d72cd2ae 100644 --- a/light/light.cc +++ b/light/light.cc @@ -26,6 +26,7 @@ #include #include +#include #ifdef HAVE_EMBREE #include @@ -454,24 +455,6 @@ AddTriangleNormals(std::map &smoothed_normals, const vec_t * weight = AngleBetweenPoints(p1, p3, p2); VectorMA(smoothed_normals[v3].v, weight, norm, smoothed_normals[v3].v); } -/* small helper that just retrieves the correct vertex from face->surfedge->edge lookups */ -int GetSurfaceVertex(const bsp2_t *bsp, const bsp2_dface_t *f, int v) -{ - int edge = f->firstedge + v; - edge = bsp->dsurfedges[edge]; - if (edge < 0) - return bsp->dedges[-edge].v[1]; - return bsp->dedges[edge].v[0]; -} - -void -Face_Normal(const bsp2_t *bsp, const bsp2_dface_t *f, vec3_t norm) -{ - if (f->side) - VectorSubtract(vec3_origin, bsp->dplanes[f->planenum].normal, norm); - else - VectorCopy(bsp->dplanes[f->planenum].normal, norm); -} /* access the final phong-shaded vertex normal */ const vec_t *GetSurfaceVertexNormal(const bsp2_t *bsp, const bsp2_dface_t *f, const int vertindex) @@ -495,29 +478,6 @@ FacesOnSamePlane(const std::vector &faces) return true; } -static void -Vertex_GetPos(const bsp2_t *bsp, int num, vec3_t out) -{ - assert(num >= 0 && num < bsp->numvertexes); - const dvertex_t *v = &bsp->dvertexes[num]; - - for (int i=0; i<3; i++) - out[i] = v->point[i]; -} - -plane_t -Face_Plane(const bsp2_t *bsp, const bsp2_dface_t *f) -{ - const int vertnum = GetSurfaceVertex(bsp, f, 0); - vec3_t vertpos; - Vertex_GetPos(bsp, vertnum, vertpos); - - plane_t res; - Face_Normal(bsp, f, res.normal); - res.dist = DotProduct(vertpos, res.normal); - return res; -} - const bsp2_dface_t * Face_EdgeIndexSmoothed(const bsp2_t *bsp, const bsp2_dface_t *f, const int edgeindex) { diff --git a/light/ltface.cc b/light/ltface.cc index 47277696..fae172d0 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -22,6 +22,8 @@ #include #include +#include + #include static void