From 9c820681603aad82e577eb6b4bc6bd4b844adb4e Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sat, 18 Mar 2017 16:43:13 -0600 Subject: [PATCH] light: phong: guard against accessing phong caches before they are built --- light/phong.cc | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/light/phong.cc b/light/phong.cc index 14b51c58..9e73884e 100644 --- a/light/phong.cc +++ b/light/phong.cc @@ -67,6 +67,7 @@ AngleBetweenPoints(const vec3 &p1, const vec3 &p2, const vec3 &p3) return result; } +static bool s_builtPhongCaches; static std::map> vertex_normals; static std::set interior_verts; static map> smoothFaces; @@ -77,12 +78,15 @@ static vector FaceCache; const edgeToFaceMap_t &GetEdgeToFaceMap() { + Q_assert(s_builtPhongCaches); return EdgeToFaceMap; } // Uses `smoothFaces` static var bool FacesSmoothed(const bsp2_dface_t *f1, const bsp2_dface_t *f2) { + Q_assert(s_builtPhongCaches); + const auto &facesIt = smoothFaces.find(f1); if (facesIt == smoothFaces.end()) return false; @@ -94,7 +98,10 @@ bool FacesSmoothed(const bsp2_dface_t *f1, const bsp2_dface_t *f2) return true; } -const std::set &GetSmoothFaces(const bsp2_dface_t *face) { +const std::set &GetSmoothFaces(const bsp2_dface_t *face) +{ + Q_assert(s_builtPhongCaches); + static std::set empty; const auto it = smoothFaces.find(face); @@ -104,7 +111,10 @@ const std::set &GetSmoothFaces(const bsp2_dface_t *face) { return it->second; } -const std::vector &GetPlaneFaces(const bsp2_dface_t *face) { +const std::vector &GetPlaneFaces(const bsp2_dface_t *face) +{ + Q_assert(s_builtPhongCaches); + static std::vector empty; const auto it = planesToFaces.find(face->planenum); @@ -138,6 +148,8 @@ AddTriangleNormals(std::map &smoothed_normals, const vec3 &norm, cons /* access the final phong-shaded vertex normal */ const glm::vec3 GetSurfaceVertexNormal(const bsp2_t *bsp, const bsp2_dface_t *f, const int vertindex) { + Q_assert(s_builtPhongCaches); + const auto &face_normals_vector = vertex_normals.at(f); return face_normals_vector.at(vertindex); } @@ -160,6 +172,8 @@ FacesOnSamePlane(const std::vector &faces) const bsp2_dface_t * Face_EdgeIndexSmoothed(const bsp2_t *bsp, const bsp2_dface_t *f, const int edgeindex) { + Q_assert(s_builtPhongCaches); + const int v0 = Face_VertexAtIndex(bsp, f, edgeindex); const int v1 = Face_VertexAtIndex(bsp, f, (edgeindex + 1) % f->numedges); @@ -257,13 +271,9 @@ static vector MakeFaceCache(const bsp2_t *bsp) void CalcualateVertexNormals(const bsp2_t *bsp) { - EdgeToFaceMap = MakeEdgeToFaceMap(bsp); + Q_assert(!s_builtPhongCaches); - // clear in case we are run twice - vertex_normals.clear(); - interior_verts.clear(); - smoothFaces.clear(); - vertsToFaces.clear(); + EdgeToFaceMap = MakeEdgeToFaceMap(bsp); // read _phong and _phong_angle from entities for compatiblity with other qbsp's, at the expense of no // support on func_detail/func_group @@ -419,8 +429,12 @@ CalcualateVertexNormals(const bsp2_t *bsp) } FaceCache = MakeFaceCache(bsp); + + s_builtPhongCaches = true; } -const face_cache_t &FaceCacheForFNum(int fnum) { +const face_cache_t &FaceCacheForFNum(int fnum) +{ + Q_assert(s_builtPhongCaches); return FaceCache.at(fnum); }