From 697bdcf078d07a4ecd4b1829015c18efbe87b341 Mon Sep 17 00:00:00 2001 From: Kp Date: Sat, 1 Nov 2014 03:17:48 +0000 Subject: [PATCH] Add vm_vec_normal that returns result --- common/include/vecmat.h | 6 ++++++ similar/editor/segment.cpp | 4 ++-- similar/main/gameseg.cpp | 42 +++++++++++++------------------------- similar/main/terrain.cpp | 3 +-- 4 files changed, 23 insertions(+), 32 deletions(-) diff --git a/common/include/vecmat.h b/common/include/vecmat.h index 1909c3e02..933243618 100644 --- a/common/include/vecmat.h +++ b/common/include/vecmat.h @@ -231,6 +231,12 @@ vms_vector &vm_vec_cross (vms_vector &dest, const vms_vector &src0, const vms_ve //returns ptr to dest //dest CANNOT equal either source vms_vector &vm_vec_normal (vms_vector &dest, const vms_vector &p0, const vms_vector &p1, const vms_vector &p2); +static inline vms_vector vm_vec_normal(const vms_vector &p0, const vms_vector &p1, const vms_vector &p2) __attribute_warn_unused_result; +static inline vms_vector vm_vec_normal(const vms_vector &p0, const vms_vector &p1, const vms_vector &p2) +{ + vms_vector dest; + return vm_vec_normal(dest, p0, p1, p2), dest; +} //computes non-normalized surface normal from three points. //returns ptr to dest diff --git a/similar/editor/segment.cpp b/similar/editor/segment.cpp index 74dd7fb78..92bb772c8 100644 --- a/similar/editor/segment.cpp +++ b/similar/editor/segment.cpp @@ -1549,12 +1549,12 @@ void create_coordinate_axes_from_segment(const vsegptr_t sp,array &vert int check_seg_concavity(const vsegptr_t s) { int sn,vn; - vms_vector n0,n1; + vms_vector n0; for (sn=0;snverts[Side_to_verts[sn][vn%4]]], Vertices[s->verts[Side_to_verts[sn][(vn+1)%4]]], Vertices[s->verts[Side_to_verts[sn][(vn+2)%4]]]); diff --git a/similar/main/gameseg.cpp b/similar/main/gameseg.cpp index a8fa4325b..2dd88914c 100644 --- a/similar/main/gameseg.cpp +++ b/similar/main/gameseg.cpp @@ -1348,7 +1348,6 @@ static void get_verts_for_normal(int va, int vb, int vc, int vd, int *v0, int *v // ------------------------------------------------------------------------------- static void add_side_as_2_triangles(const vsegptr_t sp, int sidenum) { - vms_vector norm; const sbyte *vs = Side_to_verts[sidenum]; fix dot; @@ -1360,7 +1359,7 @@ static void add_side_as_2_triangles(const vsegptr_t sp, int sidenum) // Use Matt's formula: Na . AD > 0, where ABCD are vertices on side, a is face formed by A,B,C, Na is normal from face a. // If not a wall, then triangulate so whatever is on the other side is triangulated the same (ie, between the same absoluate vertices) if (!IS_CHILD(sp->children[sidenum])) { - vm_vec_normal(norm, Vertices[sp->verts[vs[0]]], Vertices[sp->verts[vs[1]]], Vertices[sp->verts[vs[2]]]); + const auto norm = vm_vec_normal(Vertices[sp->verts[vs[0]]], Vertices[sp->verts[vs[1]]], Vertices[sp->verts[vs[2]]]); const auto vec_13 = vm_vec_sub(Vertices[sp->verts[vs[3]]], Vertices[sp->verts[vs[1]]]); // vector from vertex 1 to vertex 3 dot = vm_vec_dot(norm, vec_13); @@ -1372,15 +1371,11 @@ static void add_side_as_2_triangles(const vsegptr_t sp, int sidenum) // Now, based on triangulation type, set the normals. if (sidep->get_type() == SIDE_IS_TRI_02) { - vm_vec_normal(norm, Vertices[sp->verts[vs[0]]], Vertices[sp->verts[vs[1]]], Vertices[sp->verts[vs[2]]]); - sidep->normals[0] = norm; - vm_vec_normal(norm, Vertices[sp->verts[vs[0]]], Vertices[sp->verts[vs[2]]], Vertices[sp->verts[vs[3]]]); - sidep->normals[1] = norm; + vm_vec_normal(sidep->normals[0], Vertices[sp->verts[vs[0]]], Vertices[sp->verts[vs[1]]], Vertices[sp->verts[vs[2]]]); + vm_vec_normal(sidep->normals[1], Vertices[sp->verts[vs[0]]], Vertices[sp->verts[vs[2]]], Vertices[sp->verts[vs[3]]]); } else { - vm_vec_normal(norm, Vertices[sp->verts[vs[0]]], Vertices[sp->verts[vs[1]]], Vertices[sp->verts[vs[3]]]); - sidep->normals[0] = norm; - vm_vec_normal(norm, Vertices[sp->verts[vs[1]]], Vertices[sp->verts[vs[2]]], Vertices[sp->verts[vs[3]]]); - sidep->normals[1] = norm; + vm_vec_normal(sidep->normals[0], Vertices[sp->verts[vs[0]]], Vertices[sp->verts[vs[1]]], Vertices[sp->verts[vs[3]]]); + vm_vec_normal(sidep->normals[1], Vertices[sp->verts[vs[1]]], Vertices[sp->verts[vs[2]]], Vertices[sp->verts[vs[3]]]); } } else { int i,v[4], vsorted[4]; @@ -1395,30 +1390,22 @@ static void add_side_as_2_triangles(const vsegptr_t sp, int sidenum) sidep->set_type(SIDE_IS_TRI_02); // Now, get vertices for normal for each triangle based on triangulation type. get_verts_for_normal(v[0], v[1], v[2], 32767, &vsorted[0], &vsorted[1], &vsorted[2], &vsorted[3], &negate_flag); - vm_vec_normal(norm, Vertices[vsorted[0]], Vertices[vsorted[1]], Vertices[vsorted[2]]); - if (negate_flag) - vm_vec_negate(norm); - sidep->normals[0] = norm; + const auto n0 = vm_vec_normal(Vertices[vsorted[0]], Vertices[vsorted[1]], Vertices[vsorted[2]]); + sidep->normals[0] = negate_flag ? vm_vec_negated(n0) : n0; get_verts_for_normal(v[0], v[2], v[3], 32767, &vsorted[0], &vsorted[1], &vsorted[2], &vsorted[3], &negate_flag); - vm_vec_normal(norm, Vertices[vsorted[0]], Vertices[vsorted[1]], Vertices[vsorted[2]]); - if (negate_flag) - vm_vec_negate(norm); - sidep->normals[1] = norm; + const auto n1 = vm_vec_normal(Vertices[vsorted[0]], Vertices[vsorted[1]], Vertices[vsorted[2]]); + sidep->normals[1] = negate_flag ? vm_vec_negated(n1) : n1; } else { sidep->set_type(SIDE_IS_TRI_13); // Now, get vertices for normal for each triangle based on triangulation type. get_verts_for_normal(v[0], v[1], v[3], 32767, &vsorted[0], &vsorted[1], &vsorted[2], &vsorted[3], &negate_flag); - vm_vec_normal(norm, Vertices[vsorted[0]], Vertices[vsorted[1]], Vertices[vsorted[2]]); - if (negate_flag) - vm_vec_negate(norm); - sidep->normals[0] = norm; + const auto n0 = vm_vec_normal(Vertices[vsorted[0]], Vertices[vsorted[1]], Vertices[vsorted[2]]); + sidep->normals[0] = negate_flag ? vm_vec_negated(n0) : n0; get_verts_for_normal(v[1], v[2], v[3], 32767, &vsorted[0], &vsorted[1], &vsorted[2], &vsorted[3], &negate_flag); - vm_vec_normal(norm, Vertices[vsorted[0]], Vertices[vsorted[1]], Vertices[vsorted[2]]); - if (negate_flag) - vm_vec_negate(norm); - sidep->normals[1] = norm; + const auto n1 = vm_vec_normal(Vertices[vsorted[0]], Vertices[vsorted[1]], Vertices[vsorted[2]]); + sidep->normals[1] = negate_flag ? vm_vec_negated(n1) : n1; } } } @@ -1439,7 +1426,6 @@ void create_walls_on_side(const vsegptridx_t sp, int sidenum) { int vm0, vm1, vm2, vm3, negate_flag; int v0, v1, v2, v3; - vms_vector vn; fix dist_to_plane; v0 = sp->verts[Side_to_verts[sidenum][0]]; @@ -1449,7 +1435,7 @@ void create_walls_on_side(const vsegptridx_t sp, int sidenum) get_verts_for_normal(v0, v1, v2, v3, &vm0, &vm1, &vm2, &vm3, &negate_flag); - vm_vec_normal(vn, Vertices[vm0], Vertices[vm1], Vertices[vm2]); + auto vn = vm_vec_normal(Vertices[vm0], Vertices[vm1], Vertices[vm2]); dist_to_plane = abs(vm_dist_to_plane(Vertices[vm3], vn, Vertices[vm0])); if (negate_flag) diff --git a/similar/main/terrain.cpp b/similar/main/terrain.cpp index 4a9c9c3ee..4b6b6f066 100644 --- a/similar/main/terrain.cpp +++ b/similar/main/terrain.cpp @@ -398,8 +398,7 @@ static const vms_vector light{0x2e14,0xe8f5,0x5eb8}; static fix get_face_light(vms_vector *p0,vms_vector *p1,vms_vector *p2) { - vms_vector norm; - vm_vec_normal(norm,*p0,*p1,*p2); + const auto norm = vm_vec_normal(*p0,*p1,*p2); return -vm_vec_dot(norm,light); }