2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
16 * Interrogation functions for segment data structure.
35 // #include "segment2.h"
54 int Do_duplicate_vertex_check = 0; // Gets set to 1 in med_create_duplicate_vertex, means to check for duplicate vertices in compress_mine
56 #define BOTTOM_STUFF 0
58 // Remap all vertices in polygons in a segment through translation table xlate_verts.
60 void remap_vertices(segment *segp, int *xlate_verts)
62 int sidenum, facenum, polynum, v;
64 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++)
65 for (facenum=0; facenum<segp->sides[sidenum].num_faces; facenum++)
66 for (polynum=0; polynum<segp->sides[sidenum].faces[facenum].num_polys; polynum++) {
67 poly *pp = &segp->sides[sidenum].faces[facenum].polys[polynum];
68 for (v=0; v<pp->num_vertices; v++)
69 pp->verts[v] = xlate_verts[pp->verts[v]];
73 // Copy everything from sourceside to destside except sourceside->faces[xx].polys[xx].verts
74 void copy_side_except_vertex_ids(side *destside, side *sourceside)
76 int facenum, polynum, v;
78 destside->num_faces = sourceside->num_faces;
79 destside->tri_edge = sourceside->tri_edge;
80 destside->wall_num = sourceside->wall_num;
82 for (facenum=0; facenum<sourceside->num_faces; facenum++) {
83 face *destface = &destside->faces[facenum];
84 face *sourceface = &sourceside->faces[facenum];
86 destface->num_polys = sourceface->num_polys;
87 destface->normal = sourceface->normal;
89 for (polynum=0; polynum<sourceface->num_polys; polynum++) {
90 poly *destpoly = &destface->polys[polynum];
91 poly *sourcepoly = &sourceface->polys[polynum];
93 destpoly->num_vertices = sourcepoly->num_vertices;
94 destpoly->face_type = sourcepoly->face_type;
95 destpoly->tmap_num = sourcepoly->tmap_num;
96 destpoly->tmap_num2 = sourcepoly->tmap_num2;
98 for (v=0; v<sourcepoly->num_vertices; v++)
99 destpoly->uvls[v] = sourcepoly->uvls[v];
105 // [side] [index] [cur:next]
106 // To remap the vertices on a side after a forward rotation
107 sbyte xlate_previous[6][4][2] = {
108 { {7, 3}, {3, 2}, {2, 6}, {6, 7} }, // remapping left to left
109 { {5, 4}, {4, 0}, {7, 3}, {6, 7} }, // remapping back to top
110 { {5, 4}, {1, 5}, {0, 1}, {4, 0} }, // remapping right to right
111 { {0, 1}, {1, 5}, {2, 6}, {3, 2} }, // remapping front to bottom
112 { {1, 5}, {5, 4}, {6, 7}, {2, 6} }, // remapping bottom to back
113 { {4, 0}, {0, 1}, {3, 2}, {7, 3} }, // remapping top to front
116 void remap_vertices_previous(segment *segp, int sidenum)
118 int v, w, facenum, polynum;
120 for (facenum=0; facenum<segp->sides[sidenum].num_faces; facenum++) {
121 for (polynum=0; polynum<segp->sides[sidenum].faces[facenum].num_polys; polynum++) {
122 poly *pp = &segp->sides[sidenum].faces[facenum].polys[polynum];
124 for (v=0; v<pp->num_vertices; v++) {
125 for (w=0; w<4; w++) {
126 if (pp->verts[v] == xlate_previous[sidenum][w][0]) {
127 pp->verts[v] = xlate_previous[sidenum][w][1];
131 Assert(w<4); // If w == 4, then didn't find current vertex in list, which means xlate_previous table is bogus
137 sbyte xlate_previous_right[6][4][2] = {
138 { {5, 6}, {6, 7}, {2, 3}, {1, 2} }, // bottom to left
139 { {6, 7}, {7, 4}, {3, 0}, {2, 3} }, // left to top
140 { {7, 4}, {4, 5}, {0, 1}, {3, 0} }, // top to right
141 { {4, 5}, {5, 6}, {1, 2}, {0, 1} }, // right to bottom
142 { {6, 7}, {5, 6}, {4, 5}, {7, 4} }, // back to back
143 { {3, 2}, {0, 3}, {1, 0}, {2, 1} }, // front to front
146 void remap_vertices_previous_right(segment *segp, int sidenum)
148 int v, w, facenum, polynum;
150 for (facenum=0; facenum<segp->sides[sidenum].num_faces; facenum++) {
151 for (polynum=0; polynum<segp->sides[sidenum].faces[facenum].num_polys; polynum++) {
152 poly *pp = &segp->sides[sidenum].faces[facenum].polys[polynum];
154 for (v=0; v<pp->num_vertices; v++) {
155 for (w=0; w<4; w++) {
156 if (pp->verts[v] == xlate_previous_right[sidenum][w][0]) {
157 pp->verts[v] = xlate_previous_right[sidenum][w][1];
161 Assert(w<4); // If w == 4, then didn't find current vertex in list, which means xlate_previous table is bogus
168 // -----------------------------------------------------------------------------------
169 // Takes top to front
170 void med_rotate_segment_forward(segment *segp)
177 seg_copy.verts[0] = segp->verts[4];
178 seg_copy.verts[1] = segp->verts[0];
179 seg_copy.verts[2] = segp->verts[3];
180 seg_copy.verts[3] = segp->verts[7];
181 seg_copy.verts[4] = segp->verts[5];
182 seg_copy.verts[5] = segp->verts[1];
183 seg_copy.verts[6] = segp->verts[2];
184 seg_copy.verts[7] = segp->verts[6];
186 seg_copy.children[WFRONT] = segp->children[WTOP];
187 seg_copy.children[WTOP] = segp->children[WBACK];
188 seg_copy.children[WBACK] = segp->children[WBOTTOM];
189 seg_copy.children[WBOTTOM] = segp->children[WFRONT];
191 seg_copy.sides[WFRONT] = segp->sides[WTOP];
192 seg_copy.sides[WTOP] = segp->sides[WBACK];
193 seg_copy.sides[WBACK] = segp->sides[WBOTTOM];
194 seg_copy.sides[WBOTTOM] = segp->sides[WFRONT];
197 remap_vertices_previous(&seg_copy, i);
202 // -----------------------------------------------------------------------------------
203 // Takes top to right
204 void med_rotate_segment_right(segment *segp)
211 seg_copy.verts[4] = segp->verts[7];
212 seg_copy.verts[5] = segp->verts[4];
213 seg_copy.verts[1] = segp->verts[0];
214 seg_copy.verts[0] = segp->verts[3];
215 seg_copy.verts[3] = segp->verts[2];
216 seg_copy.verts[2] = segp->verts[1];
217 seg_copy.verts[6] = segp->verts[5];
218 seg_copy.verts[7] = segp->verts[6];
220 seg_copy.children[WRIGHT] = segp->children[WTOP];
221 seg_copy.children[WBOTTOM] = segp->children[WRIGHT];
222 seg_copy.children[WLEFT] = segp->children[WBOTTOM];
223 seg_copy.children[WTOP] = segp->children[WLEFT];
225 seg_copy.sides[WRIGHT] = segp->sides[WTOP];
226 seg_copy.sides[WBOTTOM] = segp->sides[WRIGHT];
227 seg_copy.sides[WLEFT] = segp->sides[WBOTTOM];
228 seg_copy.sides[WTOP] = segp->sides[WLEFT];
231 remap_vertices_previous_right(&seg_copy, i);
236 void make_curside_bottom_side(void)
239 case WRIGHT: med_rotate_segment_right(Cursegp); break;
240 case WTOP: med_rotate_segment_right(Cursegp); med_rotate_segment_right(Cursegp); break;
241 case WLEFT: med_rotate_segment_right(Cursegp); med_rotate_segment_right(Cursegp); med_rotate_segment_right(Cursegp); break;
243 case WFRONT: med_rotate_segment_forward(Cursegp); break;
244 case WBACK: med_rotate_segment_forward(Cursegp); med_rotate_segment_forward(Cursegp); med_rotate_segment_forward(Cursegp); break;
246 Update_flags = UF_WORLD_CHANGED;
250 int ToggleBottom(void)
252 Render_only_bottom = !Render_only_bottom;
253 Update_flags = UF_WORLD_CHANGED;
257 // ---------------------------------------------------------------------------------------------
258 // ---------- Segment interrogation functions ----------
259 // ----------------------------------------------------------------------------
260 // Return a pointer to the list of vertex indices for the current segment in vp and
261 // the number of vertices in *nv.
262 void med_get_vertex_list(segment *s,int *nv,short **vp)
265 *nv = MAX_VERTICES_PER_SEGMENT;
268 // -------------------------------------------------------------------------------
269 // Return number of times vertex vi appears in all segments.
270 // This function can be used to determine whether a vertex is used exactly once in
271 // all segments, in which case it can be freely moved because it is not connected
272 // to any other segment.
273 int med_vertex_count(int vi)
281 for (s=0; s<MAX_SEGMENTS; s++) {
283 if (sp->segnum != -1)
284 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++)
285 if (sp->verts[v] == vi)
292 // -------------------------------------------------------------------------------
293 int is_free_vertex(int vi)
295 return med_vertex_count(vi) == 1;
299 // -------------------------------------------------------------------------------
300 // Move a free vertex in the segment by adding the vector *vofs to its coordinates.
302 // If the point is not free then:
303 // If the point is not valid (probably valid = in 0..7) then:
304 // If adding *vofs will cause a degenerate segment then:
305 // Note, pi is the point index relative to the segment, not an absolute point index.
306 // For example, 3 is always the front upper left vertex.
307 void med_move_vertex(segment *sp, int pi, vms_vector *vofs)
311 Assert((pi >= 0) && (pi <= 7)); // check valid range of point indices.
313 abspi = sp->verts[pi];
315 // Make sure vertex abspi is free. If it is free, it appears exactly once in Vertices
316 Assert(med_vertex_count(abspi) == 1);
318 Assert(abspi <= MAX_SEGMENT_VERTICES); // Make sure vertex id is not bogus.
320 vm_vec_add(&Vertices[abspi],&Vertices[abspi],vofs);
322 // Here you need to validate the geometry of the segment, which will be quite tricky.
323 // You need to make sure:
324 // The segment is not concave.
325 // None of the sides are concave.
326 validate_segment(sp);
330 // -------------------------------------------------------------------------------
331 // Move a free wall in the segment by adding the vector *vofs to its coordinates.
332 // Wall indices: 0/1/2/3/4/5 = left/top/right/bottom/back/front
333 void med_move_wall(segment *sp,int wi, vms_vector *vofs)
338 Assert( (wi >= 0) && (wi <= 5) );
340 vp = Side_to_verts[wi];
341 for (i=0; i<4; i++) {
342 med_move_vertex(sp,*vp,vofs);
346 validate_segment(sp);
349 // -------------------------------------------------------------------------------
350 // Return true if one fixed point number is very close to another, else return false.
351 int fnear(fix f1, fix f2)
353 return (abs(f1 - f2) <= FIX_EPSILON);
356 // -------------------------------------------------------------------------------
357 int vnear(vms_vector *vp1, vms_vector *vp2)
359 return fnear(vp1->x, vp2->x) && fnear(vp1->y, vp2->y) && fnear(vp1->z, vp2->z);
362 // -------------------------------------------------------------------------------
363 // Add the vertex *vp to the global list of vertices, return its index.
364 // Search until a matching vertex is found (has nearly the same coordinates) or until Num_vertices
365 // vertices have been looked at without a match. If no match, add a new vertex.
366 int med_add_vertex(vms_vector *vp)
369 int count; // number of used vertices found, for loops exits when count == Num_vertices
371 // set_vertex_counts();
373 Assert(Num_vertices < MAX_SEGMENT_VERTICES);
377 for (v=0; (v < MAX_SEGMENT_VERTICES) && (count < Num_vertices); v++)
378 if (Vertex_active[v]) {
380 if (vnear(vp,&Vertices[v])) {
381 // mprintf((0,"[%4i] ",v));
384 } else if (free_index == -1)
385 free_index = v; // we want free_index to be the first free slot to add a vertex
387 if (free_index == -1)
388 free_index = Num_vertices;
390 while (Vertex_active[free_index] && (free_index < MAX_VERTICES))
393 Assert(free_index < MAX_VERTICES);
395 Vertices[free_index] = *vp;
396 Vertex_active[free_index] = 1;
400 if (free_index > Highest_vertex_index)
401 Highest_vertex_index = free_index;
406 // ------------------------------------------------------------------------------------------
407 // Returns the index of a free segment.
408 // Scans the Segments array.
409 int get_free_segment_number(void)
413 for (segnum=0; segnum<MAX_SEGMENTS; segnum++)
414 if (Segments[segnum].segnum == -1) {
416 if (segnum > Highest_segment_index)
417 Highest_segment_index = segnum;
426 // -------------------------------------------------------------------------------
427 // Create a new segment, duplicating exactly, including vertex ids and children, the passed segment.
428 int med_create_duplicate_segment(segment *sp)
432 segnum = get_free_segment_number();
434 Segments[segnum] = *sp;
439 // -------------------------------------------------------------------------------
440 // Add the vertex *vp to the global list of vertices, return its index.
441 // This is the same as med_add_vertex, except that it does not search for the presence of the vertex.
442 int med_create_duplicate_vertex(vms_vector *vp)
446 Assert(Num_vertices < MAX_SEGMENT_VERTICES);
448 Do_duplicate_vertex_check = 1;
450 free_index = Num_vertices;
452 while (Vertex_active[free_index] && (free_index < MAX_VERTICES))
455 Assert(free_index < MAX_VERTICES);
457 Vertices[free_index] = *vp;
458 Vertex_active[free_index] = 1;
462 if (free_index > Highest_vertex_index)
463 Highest_vertex_index = free_index;
469 // -------------------------------------------------------------------------------
470 // Set the vertex *vp at index vnum in the global list of vertices, return its index (just for compatibility).
471 int med_set_vertex(int vnum,vms_vector *vp)
473 Assert(vnum < MAX_VERTICES);
475 Vertices[vnum] = *vp;
477 // Just in case this vertex wasn't active, mark it as active.
478 if (!Vertex_active[vnum]) {
479 Vertex_active[vnum] = 1;
481 if ((vnum > Highest_vertex_index) && (vnum < NEW_SEGMENT_VERTICES)) {
482 mprintf((0,"Warning -- setting a previously unset vertex, index = %i.\n",vnum));
483 Highest_vertex_index = vnum;
493 // A side is determined to be degenerate if the cross products of 3 consecutive points does not point outward.
494 int check_for_degenerate_side(segment *sp, int sidenum)
496 sbyte *vp = Side_to_verts[sidenum];
497 vms_vector vec1, vec2, cross, vec_to_center;
498 vms_vector segc, sidec;
500 int degeneracy_flag = 0;
502 compute_segment_center(&segc, sp);
503 compute_center_point_on_side(&sidec, sp, sidenum);
504 vm_vec_sub(&vec_to_center, &segc, &sidec);
506 //vm_vec_sub(&vec1, &Vertices[sp->verts[vp[1]]], &Vertices[sp->verts[vp[0]]]);
507 //vm_vec_sub(&vec2, &Vertices[sp->verts[vp[2]]], &Vertices[sp->verts[vp[1]]]);
508 //vm_vec_normalize(&vec1);
509 //vm_vec_normalize(&vec2);
510 vm_vec_normalized_dir(&vec1, &Vertices[sp->verts[(int) vp[1]]], &Vertices[sp->verts[(int) vp[0]]]);
511 vm_vec_normalized_dir(&vec2, &Vertices[sp->verts[(int) vp[2]]], &Vertices[sp->verts[(int) vp[1]]]);
512 vm_vec_cross(&cross, &vec1, &vec2);
514 dot = vm_vec_dot(&vec_to_center, &cross);
516 degeneracy_flag |= 1;
518 //vm_vec_sub(&vec1, &Vertices[sp->verts[vp[2]]], &Vertices[sp->verts[vp[1]]]);
519 //vm_vec_sub(&vec2, &Vertices[sp->verts[vp[3]]], &Vertices[sp->verts[vp[2]]]);
520 //vm_vec_normalize(&vec1);
521 //vm_vec_normalize(&vec2);
522 vm_vec_normalized_dir(&vec1, &Vertices[sp->verts[(int) vp[2]]], &Vertices[sp->verts[(int) vp[1]]]);
523 vm_vec_normalized_dir(&vec2, &Vertices[sp->verts[(int) vp[3]]], &Vertices[sp->verts[(int) vp[2]]]);
524 vm_vec_cross(&cross, &vec1, &vec2);
526 dot = vm_vec_dot(&vec_to_center, &cross);
528 degeneracy_flag |= 1;
530 return degeneracy_flag;
534 // -------------------------------------------------------------------------------
535 void create_removable_wall(segment *sp, int sidenum, int tmap_num)
537 create_walls_on_side(sp, sidenum);
539 sp->sides[sidenum].tmap_num = tmap_num;
541 assign_default_uvs_to_side(sp, sidenum);
542 assign_light_to_side(sp, sidenum);
546 // See if a segment has gotten turned inside out, or something.
547 // If so, set global Degenerate_segment_found and return 1, else return 0.
548 int check_for_degenerate_segment(segment *sp)
550 vms_vector fvec, rvec, uvec, cross;
552 int i, degeneracy_flag = 0; // degeneracy flag for current segment
554 extract_forward_vector_from_segment(sp, &fvec);
555 extract_right_vector_from_segment(sp, &rvec);
556 extract_up_vector_from_segment(sp, &uvec);
558 vm_vec_normalize(&fvec);
559 vm_vec_normalize(&rvec);
560 vm_vec_normalize(&uvec);
562 vm_vec_cross(&cross, &fvec, &rvec);
563 dot = vm_vec_dot(&cross, &uvec);
568 mprintf((0, "segment #%i is degenerate due to cross product check.\n", SEGMENT_NUMBER(sp)));
572 // Now, see if degenerate because of any side.
573 for (i=0; i<MAX_SIDES_PER_SEGMENT; i++)
574 degeneracy_flag |= check_for_degenerate_side(sp, i);
576 Degenerate_segment_found |= degeneracy_flag;
578 return degeneracy_flag;
584 // ---------------------------------------------------------------------------------------------
585 // Orthogonalize matrix smat, returning result in rmat.
586 // Does not modify smat.
587 // Uses Gram-Schmidt process.
588 // See page 172 of Strang, Gilbert, Linear Algebra and its Applications
589 // Matt -- This routine should be moved to the vector matrix library.
590 // It IS legal for smat == rmat.
591 // We should also have the functions:
592 // mat_a = mat_b * scalar; // we now have mat_a = mat_a * scalar;
593 // mat_a = mat_b + mat_c * scalar; // or maybe not, maybe this is not primitive
594 void make_orthogonal(vms_matrix *rmat,vms_matrix *smat)
597 vms_vector tvec1,tvec2;
600 // Copy source matrix to work area.
603 // Normalize the three rows of the matrix tmat.
604 vm_vec_normalize(&tmat.xrow);
605 vm_vec_normalize(&tmat.yrow);
606 vm_vec_normalize(&tmat.zrow);
608 // Now, compute the first vector.
609 // This is very easy -- just copy the (normalized) source vector.
610 rmat->zrow = tmat.zrow;
612 // Now, compute the second vector.
613 // From page 172 of Strang, we use the equation:
614 // b' = b - [transpose(q1) * b] * q1
615 // where: b = the second row of tmat
616 // q1 = the first row of rmat
617 // b' = the second row of rmat
619 // Compute: transpose(q1) * b
620 dot = vm_vec_dotprod(&rmat->zrow,&tmat.yrow);
622 // Compute: b - dot * q1
623 rmat->yrow.x = tmat.yrow.x - fixmul(dot,rmat->zrow.x);
624 rmat->yrow.y = tmat.yrow.y - fixmul(dot,rmat->zrow.y);
625 rmat->yrow.z = tmat.yrow.z - fixmul(dot,rmat->zrow.z);
627 // Now, compute the third vector.
628 // From page 173 of Strang, we use the equation:
629 // c' = c - (q1*c)*q1 - (q2*c)*q2
630 // where: c = the third row of tmat
631 // q1 = the first row of rmat
632 // q2 = the second row of rmat
633 // c' = the third row of rmat
636 dot = vm_vec_dotprod(&rmat->zrow,&tmat.xrow);
638 tvec1.x = fixmul(dot,rmat->zrow.x);
639 tvec1.y = fixmul(dot,rmat->zrow.y);
640 tvec1.z = fixmul(dot,rmat->zrow.z);
643 dot = vm_vec_dotprod(&rmat->yrow,&tmat.xrow);
645 tvec2.x = fixmul(dot,rmat->yrow.x);
646 tvec2.y = fixmul(dot,rmat->yrow.y);
647 tvec2.z = fixmul(dot,rmat->yrow.z);
649 vm_vec_sub(&rmat->xrow,vm_vec_sub(&rmat->xrow,&tmat.xrow,&tvec1),&tvec2);
654 // ------------------------------------------------------------------------------------------
655 // Given a segment, extract the rotation matrix which defines it.
656 // Do this by extracting the forward, right, up vectors and then making them orthogonal.
657 // In the process of making the vectors orthogonal, favor them in the order forward, up, right.
658 // This means that the forward vector will remain unchanged.
659 void med_extract_matrix_from_segment(segment *sp,vms_matrix *rotmat)
661 vms_vector forwardvec,upvec;
663 extract_forward_vector_from_segment(sp,&forwardvec);
664 extract_up_vector_from_segment(sp,&upvec);
666 if (((forwardvec.x == 0) && (forwardvec.y == 0) && (forwardvec.z == 0)) || ((upvec.x == 0) && (upvec.y == 0) && (upvec.z == 0))) {
667 mprintf((0, "Trapped null vector in med_extract_matrix_from_segment, returning identity matrix.\n"));
668 *rotmat = vmd_identity_matrix;
673 vm_vector_2_matrix(rotmat,&forwardvec,&upvec,NULL);
678 extract_forward_vector_from_segment(sp,&rm.zrow);
679 extract_right_vector_from_segment(sp,&rm.xrow);
680 extract_up_vector_from_segment(sp,&rm.yrow);
682 vm_vec_normalize(&rm.xrow);
683 vm_vec_normalize(&rm.yrow);
684 vm_vec_normalize(&rm.zrow);
686 make_orthogonal(rotmat,&rm);
688 vm_vec_normalize(&rotmat->xrow);
689 vm_vec_normalize(&rotmat->yrow);
690 vm_vec_normalize(&rotmat->zrow);
692 // *rotmat = rm; // include this line (and remove the call to make_orthogonal) if you don't want the matrix orthogonalized
697 // ------------------------------------------------------------------------------------------
698 // Given a rotation matrix *rotmat which describes the orientation of a segment
699 // and a side destside, return the rotation matrix which describes the orientation for the side.
700 void set_matrix_based_on_side(vms_matrix *rotmat,int destside)
702 vms_angvec rotvec,*tmpvec;
707 tmpvec=vm_angvec_make(&rotvec,0,0,-16384);
708 vm_angles_2_matrix(&r1,&rotvec);
709 vm_matrix_x_matrix(&rtemp,rotmat,&r1);
714 tmpvec=vm_angvec_make(&rotvec,-16384,0,0);
715 vm_angles_2_matrix(&r1,&rotvec);
716 vm_matrix_x_matrix(&rtemp,rotmat,&r1);
721 tmpvec=vm_angvec_make(&rotvec,0,0,16384);
722 vm_angles_2_matrix(&r1,&rotvec);
723 vm_matrix_x_matrix(&rtemp,rotmat,&r1);
728 tmpvec=vm_angvec_make(&rotvec,+16384,-32768,0); // bank was -32768, but I think that was an erroneous compensation
729 vm_angles_2_matrix(&r1,&rotvec);
730 vm_matrix_x_matrix(&rtemp,rotmat,&r1);
735 tmpvec=vm_angvec_make(&rotvec,0,0,-32768);
736 vm_angles_2_matrix(&r1,&rotvec);
737 vm_matrix_x_matrix(&rtemp,rotmat,&r1);
747 // -------------------------------------------------------------------------------------
748 void change_vertex_occurrences(int dest, int src)
752 // Fix vertices in groups
753 for (g=0;g<num_groups;g++)
754 for (v=0; v<GroupList[g].num_vertices; v++)
755 if (GroupList[g].vertices[v] == src)
756 GroupList[g].vertices[v] = dest;
758 // now scan all segments, changing occurrences of src to dest
759 for (s=0; s<=Highest_segment_index; s++)
760 if (Segments[s].segnum != -1)
761 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++)
762 if (Segments[s].verts[v] == src)
763 Segments[s].verts[v] = dest;
766 // --------------------------------------------------------------------------------------------------
767 void compress_vertices(void)
771 if (Highest_vertex_index == Num_vertices - 1)
774 vert = Highest_vertex_index; //MAX_SEGMENT_VERTICES-1;
776 for (hole=0; hole < vert; hole++)
777 if (!Vertex_active[hole]) {
778 // found an unused vertex which is a hole if a used vertex follows (not necessarily immediately) it.
779 for ( ; (vert>hole) && (!Vertex_active[vert]); vert--)
784 // Ok, hole is the index of a hole, vert is the index of a vertex which follows it.
785 // Copy vert into hole, update pointers to it.
786 Vertices[hole] = Vertices[vert];
788 change_vertex_occurrences(hole, vert);
794 Highest_vertex_index = Num_vertices-1;
797 // --------------------------------------------------------------------------------------------------
798 void compress_segments(void)
802 if (Highest_segment_index == Num_segments - 1)
805 seg = Highest_segment_index;
807 for (hole=0; hole < seg; hole++)
808 if (Segments[hole].segnum == -1) {
809 // found an unused segment which is a hole if a used segment follows (not necessarily immediately) it.
810 for ( ; (seg>hole) && (Segments[seg].segnum == -1); seg--)
818 // Ok, hole is the index of a hole, seg is the index of a segment which follows it.
819 // Copy seg into hole, update pointers to it, update Cursegp, Markedsegp if necessary.
820 Segments[hole] = Segments[seg];
821 Segments[seg].segnum = -1;
823 if (Cursegp == &Segments[seg])
824 Cursegp = &Segments[hole];
826 if (Markedsegp == &Segments[seg])
827 Markedsegp = &Segments[hole];
829 // Fix segments in groups
830 for (g=0;g<num_groups;g++)
831 for (s=0; s<GroupList[g].num_segments; s++)
832 if (GroupList[g].segments[s] == seg)
833 GroupList[g].segments[s] = hole;
836 for (w=0;w<Num_walls;w++)
837 if (Walls[w].segnum == seg)
838 Walls[w].segnum = hole;
840 // Fix fuelcenters, robotcens, and triggers... added 2/1/95 -Yuan
841 for (f=0;f<Num_fuelcenters;f++)
842 if (Station[f].segnum == seg)
843 Station[f].segnum = hole;
845 for (f=0;f<Num_robot_centers;f++)
846 if (RobotCenters[f].segnum == seg)
847 RobotCenters[f].segnum = hole;
849 for (t=0;t<Num_triggers;t++)
850 for (l=0;l<Triggers[t].num_links;l++)
851 if (Triggers[t].seg[l] == seg)
852 Triggers[t].seg[l] = hole;
854 sp = &Segments[hole];
855 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
856 if (IS_CHILD(sp->children[s])) {
858 csegp = &Segments[sp->children[s]];
860 // Find out on what side the segment connection to the former seg is on in *csegp.
861 for (t=0; t<MAX_SIDES_PER_SEGMENT; t++) {
862 if (csegp->children[t] == seg) {
863 csegp->children[t] = hole; // It used to be connected to seg, so make it connected to hole
869 //Update object segment pointers
870 for (objnum = sp->objects; objnum != -1; objnum = Objects[objnum].next) {
871 Assert(Objects[objnum].segnum == seg);
872 Objects[objnum].segnum = hole;
877 } // end if (seg > hole)
880 Highest_segment_index = Num_segments-1;
881 med_create_new_segment_from_cursegp();
886 // -------------------------------------------------------------------------------
887 // Combine duplicate vertices.
888 // If two vertices have the same coordinates, within some small tolerance, then assign
889 // the same vertex number to the two vertices, freeing up one of the vertices.
890 void med_combine_duplicate_vertices(sbyte *vlp)
894 for (v=0; v<Highest_vertex_index; v++) // Note: ok to do to <, rather than <= because w for loop starts at v+1
896 vms_vector *vvp = &Vertices[v];
897 for (w=v+1; w<=Highest_vertex_index; w++)
898 if (vlp[w]) { // used to be Vertex_active[w]
899 if (vnear(vvp, &Vertices[w])) {
900 change_vertex_occurrences(v, w);
907 // ------------------------------------------------------------------------------
908 // Compress mine at Segments and Vertices by squeezing out all holes.
909 // If no holes (ie, an unused segment followed by a used segment), then no action.
910 // If Cursegp or Markedsegp is a segment which gets moved to fill in a hole, then
911 // they are properly updated.
912 void med_compress_mine(void)
914 if (Do_duplicate_vertex_check) {
915 med_combine_duplicate_vertices(Vertex_active);
916 Do_duplicate_vertex_check = 0;
923 //--repair-- create_local_segment_data();
925 // This is necessary becuase a segment search (due to click in 3d window) uses the previous frame's
926 // segment information, which could get changed by this.
927 Update_flags = UF_WORLD_CHANGED;
931 // ------------------------------------------------------------------------------------------
932 // Copy texture map ids for each face in sseg to dseg.
933 void copy_tmap_ids(segment *dseg, segment *sseg)
937 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
938 dseg->sides[s].tmap_num = sseg->sides[s].tmap_num;
939 dseg->sides[s].tmap_num2 = 0;
943 // ------------------------------------------------------------------------------------------
944 // Attach a segment with a rotated orientation.
946 // 0 = successful attach
947 // 1 = No room in Segments[].
948 // 2 = No room in Vertices[].
949 // 3 = newside != WFRONT -- for now, the new segment must be attached at its (own) front side
950 // 4 = already a face attached on destseg:destside
951 int med_attach_segment_rotated(segment *destseg, segment *newseg, int destside, int newside,vms_matrix *attmat)
957 vms_matrix rotmat,rotmat1,rotmat2,rotmat3,rotmat4;
958 vms_vector vr,vc,tvs[4],xlate_vec;
960 vms_vector forvec,upvec;
962 // Return if already a face attached on this side.
963 if (IS_CHILD(destseg->children[destside]))
966 segnum = get_free_segment_number();
968 forvec = attmat->fvec;
969 upvec = attmat->uvec;
971 // We are pretty confident we can add the segment.
972 nsp = &Segments[segnum];
973 nsp2 = &Segment2s[segnum];
975 nsp->segnum = segnum;
977 nsp2->matcen_num = -1;
980 nsp->group = destseg->group;
982 // Add segment to proper group list.
984 add_segment_to_group(SEGMENT_NUMBER(nsp), nsp->group);
986 // Copy the texture map ids.
987 copy_tmap_ids(nsp,newseg);
989 // clear all connections
990 for (side=0; side<MAX_SIDES_PER_SEGMENT; side++) {
991 nsp->children[side] = -1;
992 nsp->sides[side].wall_num = -1;
995 // Form the connection
996 destseg->children[destside] = segnum;
997 // destseg->sides[destside].render_flag = 0;
998 nsp->children[newside] = SEGMENT_NUMBER(destseg);
1000 // Copy vertex indices of the four vertices forming the joint
1001 dvp = Side_to_verts[destside];
1003 // Set the vertex indices for the four vertices forming the front of the new side
1005 nsp->verts[v] = destseg->verts[(int) dvp[v]];
1007 // The other 4 vertices must be created.
1008 // Their coordinates are determined by the 4 welded vertices and the vector from front
1009 // to back of the original *newseg.
1011 // Do lots of hideous matrix stuff, about 3/4 of which could probably be simplified out.
1012 med_extract_matrix_from_segment(destseg,&rotmat); // get orientation matrix for destseg (orthogonal rotation matrix)
1013 set_matrix_based_on_side(&rotmat,destside);
1014 vm_vector_2_matrix(&rotmat1,&forvec,&upvec,NULL);
1015 vm_matrix_x_matrix(&rotmat4,&rotmat,&rotmat1); // this is the desired orientation of the new segment
1016 med_extract_matrix_from_segment(newseg,&rotmat3); // this is the current orientation of the new segment
1017 vm_transpose_matrix(&rotmat3); // get the inverse of the current orientation matrix
1018 vm_matrix_x_matrix(&rotmat2,&rotmat4,&rotmat3); // now rotmat2 takes the current segment to the desired orientation
1020 // Warning -- look at this line!
1021 vm_transpose_matrix(&rotmat2); // added 12:33 pm, 10/01/93
1023 // Compute and rotate the center point of the attaching face.
1024 compute_center_point_on_side(&vc,newseg,newside);
1025 vm_vec_rotate(&vr,&vc,&rotmat2);
1027 // Now rotate the free vertices in the segment
1029 vm_vec_rotate(&tvs[v],&Vertices[newseg->verts[v+4]],&rotmat2);
1031 // Now translate the new segment so that the center point of the attaching faces are the same.
1032 compute_center_point_on_side(&vc,destseg,destside);
1033 vm_vec_sub(&xlate_vec,&vc,&vr);
1035 // Create and add the 4 new vertices.
1036 for (v=0; v<4; v++) {
1037 vm_vec_add2(&tvs[v],&xlate_vec);
1038 nsp->verts[v+4] = med_add_vertex(&tvs[v]);
1041 set_vertex_counts();
1043 // Now all the vertices are in place. Create the faces.
1044 validate_segment(nsp);
1046 // Say to not render at the joint.
1047 // destseg->sides[destside].render_flag = 0;
1048 // nsp->sides[newside].render_flag = 0;
1055 // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1057 // ------------------------------------------------------------------------------------------
1058 void scale_free_vertices(segment *sp,vms_vector *vp,fix scale_factor,int min_side,int max_side)
1063 verts = Side_to_verts[min_side];
1066 if (is_free_vertex(sp->verts[(int) verts[i]])) {
1067 Vertices[sp->verts[(int) verts[i]]].x = fixmul(vp->x,scale_factor)/2;
1068 Vertices[sp->verts[(int) verts[i]]].y = fixmul(vp->y,scale_factor)/2;
1069 Vertices[sp->verts[(int) verts[i]]].z = fixmul(vp->z,scale_factor)/2;
1072 verts = Side_to_verts[max_side];
1075 if (is_free_vertex(sp->verts[(int) verts[i]])) {
1076 Vertices[sp->verts[(int) verts[i]]].x = fixmul(vp->x,scale_factor)/2;
1077 Vertices[sp->verts[(int) verts[i]]].y = fixmul(vp->y,scale_factor)/2;
1078 Vertices[sp->verts[(int) verts[i]]].z = fixmul(vp->z,scale_factor)/2;
1083 // ------------------------------------------------------------------------------------------
1084 // Attach side newside of newseg to side destside of destseg.
1085 // Copies *newseg into global array Segments, increments Num_segments.
1086 // Forms a weld between the two segments by making the new segment fit to the old segment.
1087 // Updates number of faces per side if necessitated by new vertex coordinates.
1090 // 0 = successful attach
1091 // 1 = No room in Segments[].
1092 // 2 = No room in Vertices[].
1093 // 3 = newside != WFRONT -- for now, the new segment must be attached at its (own) front side
1094 // 4 = already a face attached on side newside
1095 int med_attach_segment(segment *destseg, segment *newseg, int destside, int newside)
1098 segment *ocursegp = Cursegp;
1100 vms_angvec tang = {0,0,0};
1103 vm_angles_2_matrix(&rotmat,&tang);
1104 rval = med_attach_segment_rotated(destseg,newseg,destside,newside,&rotmat);
1105 med_propagate_tmaps_to_segments(ocursegp,Cursegp,0);
1106 med_propagate_tmaps_to_back_side(Cursegp, Side_opposite[newside],0);
1107 copy_uvs_seg_to_seg(&New_segment,Cursegp);
1112 // -------------------------------------------------------------------------------
1113 // Delete a vertex, sort of.
1114 // Decrement the vertex count. If the count goes to 0, then the vertex is free (has been deleted).
1115 void delete_vertex(short v)
1117 Assert(v < MAX_VERTICES); // abort if vertex is not in array Vertices
1118 Assert(Vertex_active[v] >= 1); // abort if trying to delete a non-existent vertex
1123 // -------------------------------------------------------------------------------
1124 // Update Num_vertices.
1125 // This routine should be called by anyone who calls delete_vertex. It could be called in delete_vertex,
1126 // but then it would be called much more often than necessary, and it is a slow routine.
1127 void update_num_vertices(void)
1131 // Now count the number of vertices.
1133 for (v=0; v<=Highest_vertex_index; v++)
1134 if (Vertex_active[v])
1138 // -------------------------------------------------------------------------------
1139 // Set Vertex_active to number of occurrences of each vertex.
1140 // Set Num_vertices.
1141 void set_vertex_counts(void)
1147 for (v=0; v<=Highest_vertex_index; v++)
1148 Vertex_active[v] = 0;
1150 // Count number of occurrences of each vertex.
1151 for (s=0; s<=Highest_segment_index; s++)
1152 if (Segments[s].segnum != -1)
1153 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++) {
1154 if (!Vertex_active[Segments[s].verts[v]])
1156 Vertex_active[Segments[s].verts[v]]++;
1160 // -------------------------------------------------------------------------------
1161 // Delete all vertices in segment *sp from the vertex list if they are not contained in another segment.
1162 // This is kind of a dangerous routine. It modifies the global array Vertex_active, using the field as
1164 void delete_vertices_in_segment(segment *sp)
1170 set_vertex_counts();
1172 // Subtract one count for each appearance of vertex in deleted segment
1173 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++)
1174 delete_vertex(sp->verts[v]);
1176 update_num_vertices();
1179 extern void validate_segment_side(segment *sp, int sidenum);
1181 // -------------------------------------------------------------------------------
1182 // Delete segment *sp in Segments array.
1184 // 0 successfully deleted.
1185 // 1 unable to delete.
1186 int med_delete_segment(segment *sp)
1191 segnum = SEGMENT_NUMBER(sp);
1193 // Cannot delete segment if only segment.
1194 if (Num_segments == 1)
1197 // Don't try to delete if segment doesn't exist.
1198 if (sp->segnum == -1) {
1199 mprintf((0,"Hey -- you tried to delete a non-existent segment (segnum == -1)\n"));
1203 // Delete its refueling center if it has one
1206 delete_vertices_in_segment(sp);
1210 // If deleted segment has walls on any side, wipe out the wall.
1211 for (side=0; side < MAX_SIDES_PER_SEGMENT; side++)
1212 if (sp->sides[side].wall_num != -1)
1213 wall_remove_side(sp, side);
1215 // Find out what this segment was connected to and break those connections at the other end.
1216 for (side=0; side < MAX_SIDES_PER_SEGMENT; side++)
1217 if (IS_CHILD(sp->children[side])) {
1218 segment *csp; // the connecting segment
1221 csp = &Segments[sp->children[side]];
1222 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
1223 if (csp->children[s] == segnum) {
1224 csp->children[s] = -1; // this is the side of connection, break it
1225 validate_segment_side(csp,s); // we have converted a connection to a side so validate the segment
1226 med_propagate_tmaps_to_back_side(csp,s,0);
1229 med_create_new_segment_from_cursegp();
1230 copy_uvs_seg_to_seg(&New_segment,Cursegp);
1233 sp->segnum = -1; // Mark segment as inactive.
1235 // If deleted segment = marked segment, then say there is no marked segment
1236 if (sp == Markedsegp)
1239 // If deleted segment = a Group segment ptr, then wipe it out.
1240 for (s=0;s<num_groups;s++)
1241 if (sp == Groupsegp[s])
1244 // If deleted segment = group segment, wipe it off the group list.
1246 delete_segment_from_group(SEGMENT_NUMBER(sp), sp->group);
1248 // If we deleted something which was not connected to anything, must now select a new current segment.
1250 for (s=0; s<MAX_SEGMENTS; s++)
1251 if ((Segments[s].segnum != -1) && (s!=segnum) ) {
1252 Cursegp = &Segments[s];
1253 med_create_new_segment_from_cursegp();
1257 // If deleted segment contains objects, wipe out all objects
1258 if (sp->objects != -1) {
1259 // if (objnum == Objects[objnum].next) {
1260 // mprintf((0, "Warning -- object #%i points to itself. Setting next to -1.\n", objnum));
1261 // Objects[objnum].next = -1;
1263 for (objnum=sp->objects;objnum!=-1;objnum=Objects[objnum].next) {
1265 //if an object is in the seg, delete it
1266 //if the object is the player, move to new curseg
1268 if (objnum == OBJECT_NUMBER(ConsoleObject)) {
1269 compute_segment_center(&ConsoleObject->pos,Cursegp);
1270 obj_relink(objnum, SEGMENT_NUMBER(Cursegp));
1276 // Make sure everything deleted ok...
1277 Assert( sp->objects==-1 );
1279 // If we are leaving many holes in Segments or Vertices, then compress mine, because it is inefficient to be that way
1280 // if ((Highest_segment_index > Num_segments+4) || (Highest_vertex_index > Num_vertices+4*8))
1281 // med_compress_mine();
1286 // ------------------------------------------------------------------------------------------
1287 // Copy texture maps from sseg to dseg
1288 void copy_tmaps_to_segment(segment *dseg, segment *sseg)
1292 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
1293 dseg->sides[s].type = sseg->sides[s].type;
1294 dseg->sides[s].tmap_num = sseg->sides[s].tmap_num;
1295 dseg->sides[s].tmap_num2 = sseg->sides[s].tmap_num2;
1300 // ------------------------------------------------------------------------------------------
1301 // Rotate the segment *seg by the pitch, bank, heading defined by *rot, destructively
1302 // modifying its four free vertices in the global array Vertices.
1303 // It is illegal to rotate a segment which has connectivity != 1.
1304 // Pitch, bank, heading are about the point which is the average of the four points
1305 // forming the side of connection.
1307 // 0 = successful rotation
1308 // 1 = Connectivity makes rotation illegal (connected to 0 or 2+ segments)
1309 // 2 = Rotation causes degeneracy, such as self-intersecting segment.
1310 // 3 = Unable to rotate because not connected to exactly 1 segment.
1311 int med_rotate_segment(segment *seg, vms_matrix *rotmat)
1314 int newside=0,destside,s;
1316 int back_side,side_tmaps[MAX_SIDES_PER_SEGMENT];
1318 // Find side of attachment
1320 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
1321 if (IS_CHILD(seg->children[s])) {
1326 // Return if passed in segment is connected to other than 1 segment.
1330 destseg = &Segments[seg->children[newside]];
1333 while ((destseg->children[destside] != SEGMENT_NUMBER(seg)) && (destside < MAX_SIDES_PER_SEGMENT))
1336 // Before deleting the segment, copy its texture maps to New_segment
1337 copy_tmaps_to_segment(&New_segment,seg);
1339 if (med_delete_segment(seg))
1340 mprintf((0, "Error in rotation: Unable to delete segment %i\n", SEGMENT_NUMBER(seg)));
1342 if (Curside == WFRONT)
1345 med_attach_segment_rotated(destseg,&New_segment,destside,AttachSide,rotmat);
1347 // Save tmap_num on each side to restore after call to med_propagate_tmaps_to_segments and _back_side
1348 // which will change the tmap nums.
1349 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
1350 side_tmaps[s] = seg->sides[s].tmap_num;
1352 back_side = Side_opposite[find_connect_side(destseg, seg)];
1354 med_propagate_tmaps_to_segments(destseg, seg,0);
1355 med_propagate_tmaps_to_back_side(seg, back_side,0);
1357 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
1359 seg->sides[s].tmap_num = side_tmaps[s];
1364 // ----------------------------------------------------------------------------------------
1365 int med_rotate_segment_ang(segment *seg, vms_angvec *ang)
1369 return med_rotate_segment(seg,vm_angles_2_matrix(&rotmat,ang));
1372 // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1374 // ----------------------------------------------------------------------------
1375 // Compute the sum of the distances between the four pairs of points.
1376 // The connections are:
1377 // firstv1 : 0 (firstv1+1)%4 : 1 (firstv1+2)%4 : 2 (firstv1+3)%4 : 3
1378 fix seg_seg_vertex_distsum(segment *seg1, int side1, segment *seg2, int side2, int firstv1)
1384 for (secondv=0; secondv<4; secondv++) {
1387 firstv = (4-secondv + (3 - firstv1)) % 4;
1388 distsum += vm_vec_dist(&Vertices[seg1->verts[Side_to_verts[side1][firstv]]],&Vertices[seg2->verts[Side_to_verts[side2][secondv]]]);
1395 // ----------------------------------------------------------------------------
1396 // Determine how to connect two segments together with the least amount of twisting.
1397 // Returns vertex index in 0..3 on first segment. Assumed ordering of vertices
1398 // on second segment is 0,1,2,3.
1399 // So, if return value is 2, connect 2:0 3:1 0:2 1:3.
1401 // We select an ordering of vertices for connection. For the first pair of vertices to be connected,
1402 // compute the vector. For the three remaining pairs of vertices, compute the vectors from one vertex
1403 // to the other. Compute the dot products of these vectors with the original vector. Add them up.
1404 // The close we are to 3, the better fit we have. Reason: The largest value for the dot product is
1405 // 1.0, and this occurs for a parallel set of vectors.
1406 int get_index_of_best_fit(segment *seg1, int side1, segment *seg2, int side2)
1412 min_distance = F1_0*30000;
1414 for (firstv=0; firstv<4; firstv++) {
1416 t = seg_seg_vertex_distsum(seg1, side1, seg2, side2, firstv);
1417 if (t <= min_distance) {
1419 best_index = firstv;
1428 #define MAX_VALIDATIONS 50
1430 // ----------------------------------------------------------------------------
1431 // Remap uv coordinates in all sides in segment *sp which have a vertex in vp[4].
1432 // vp contains absolute vertex indices.
1433 void remap_side_uvs(segment *sp,int *vp)
1437 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
1439 for (i=0; i<4; i++) // scan each vertex in vp[4]
1440 if (Side_to_verts[s][v] == vp[i]) {
1441 assign_default_uvs_to_side(sp,s); // Side s needs to be remapped
1448 // ----------------------------------------------------------------------------
1449 // Assign default uv coordinates to Curside.
1450 void assign_default_uvs_to_curside(void)
1452 assign_default_uvs_to_side(Cursegp, Curside);
1455 // ----------------------------------------------------------------------------
1456 // Assign default uv coordinates to all sides in Curside.
1457 void assign_default_uvs_to_curseg(void)
1461 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
1462 assign_default_uvs_to_side(Cursegp,s); // Side s needs to be remapped
1465 // ----------------------------------------------------------------------------
1466 // Modify seg2 to share side2 with seg1:side1. This forms a connection between
1467 // two segments without creating a new segment. It modifies seg2 by sharing
1468 // vertices from seg1. seg1 is not modified. Four vertices from seg2 are
1472 // 1 -- no, this is legal! -- unable to form joint because one or more vertices of side2 is not free
1473 // 2 unable to form joint because side1 is already used
1474 int med_form_joint(segment *seg1, int side1, segment *seg2, int side2)
1477 int bfi,v,s,sv,s1,nv;
1478 int lost_vertices[4],remap_vertices[4];
1479 int validation_list[MAX_VALIDATIONS];
1481 // Make sure that neither side is connected.
1482 if (IS_CHILD(seg1->children[side1]) || IS_CHILD(seg2->children[side2]))
1485 // Make sure there is no wall there
1486 if ((seg1->sides[side1].wall_num != -1) || (seg2->sides[side2].wall_num != -1))
1489 // We can form the joint. Find the best orientation of vertices.
1490 bfi = get_index_of_best_fit(seg1, side1, seg2, side2);
1492 vp1 = Side_to_verts[side1];
1493 vp2 = Side_to_verts[side2];
1495 // Make a copy of the list of vertices in seg2 which will be deleted and set the
1496 // associated vertex number, so that all occurrences of the vertices can be replaced.
1498 lost_vertices[v] = seg2->verts[(int) vp2[v]];
1500 // Now, for each vertex in lost_vertices, determine which vertex it maps to.
1502 remap_vertices[3 - ((v + bfi) % 4)] = seg1->verts[(int) vp1[v]];
1504 // Now, in all segments, replace all occurrences of vertices in lost_vertices with remap_vertices
1506 // Put the one segment we know are being modified into the validation list.
1507 // Note: seg1 does not require a full validation, only a validation of the affected side. Its vertices do not move.
1509 validation_list[0] = SEGMENT_NUMBER(seg2);
1512 for (s=0; s<=Highest_segment_index; s++)
1513 if (Segments[s].segnum != -1)
1514 for (sv=0; sv<MAX_VERTICES_PER_SEGMENT; sv++)
1515 if (Segments[s].verts[sv] == lost_vertices[v]) {
1516 Segments[s].verts[sv] = remap_vertices[v];
1517 // Add segment to list of segments to be validated.
1518 for (s1=0; s1<nv; s1++)
1519 if (validation_list[s1] == s)
1522 validation_list[nv++] = s;
1523 Assert(nv < MAX_VALIDATIONS);
1526 // Form new connections.
1527 seg1->children[side1] = SEGMENT_NUMBER(seg2);
1528 seg2->children[side2] = SEGMENT_NUMBER(seg1);
1530 // validate all segments
1531 validate_segment_side(seg1,side1);
1532 for (s=0; s<nv; s++) {
1533 validate_segment(&Segments[validation_list[s]]);
1534 remap_side_uvs(&Segments[validation_list[s]],remap_vertices); // remap uv coordinates on sides which were reshaped (ie, have a vertex in lost_vertices)
1535 warn_if_concave_segment(&Segments[validation_list[s]]);
1538 set_vertex_counts();
1540 // Make sure connection is open, ie renderable.
1541 // seg1->sides[side1].render_flag = 0;
1542 // seg2->sides[side2].render_flag = 0;
1544 //--// debug -- check all segments, make sure if a children[s] == -1, then side[s].num_faces != 0
1547 //--for (seg=0; seg<MAX_SEGMENTS; seg++)
1548 //-- if (Segments[seg].segnum != -1)
1549 //-- for (side=0; side<MAX_SIDES_PER_SEGMENT; side++)
1550 //-- if (Segments[seg].children[side] == -1) {
1551 //-- if (Segments[seg].sides[side].num_faces == 0) {
1552 //-- mprintf((0,"Error: Segment %i, side %i is not connected, but has 0 faces.\n",seg,side));
1555 //-- } else if (Segments[seg].sides[side].num_faces != 0) {
1556 //-- mprintf((0,"Error: Segment %i, side %i is connected, but has %i faces.\n",seg,side,Segments[seg].sides[side].num_faces));
1564 // ----------------------------------------------------------------------------
1565 // Create a new segment and use it to form a bridge between two existing segments.
1566 // Specify two segment:side pairs. If either segment:side is not open (ie, segment->children[side] != -1)
1567 // then it is not legal to form the brider.
1569 // 0 bridge segment formed
1570 // 1 unable to form bridge because one (or both) of the sides is not open.
1571 // Note that no new vertices are created by this process.
1572 int med_form_bridge_segment(segment *seg1, int side1, segment *seg2, int side2)
1578 if (IS_CHILD(seg1->children[side1]) || IS_CHILD(seg2->children[side2]))
1581 bs = &Segments[get_free_segment_number()];
1582 // mprintf((0, "Forming bridge segment %i from %i to %i\n", SEGMENT_NUMBER(bs), SEGMENT_NUMBER(seg1), SEGMENT_NUMBER(seg2)));
1584 bs->segnum = SEGMENT_NUMBER(bs);
1587 // Copy vertices from seg2 into last 4 vertices of bridge segment.
1588 sv = Side_to_verts[side2];
1590 bs->verts[(3-v)+4] = seg2->verts[(int) sv[v]];
1592 // Copy vertices from seg1 into first 4 vertices of bridge segment.
1593 bfi = get_index_of_best_fit(seg1, side1, seg2, side2);
1595 sv = Side_to_verts[side1];
1597 bs->verts[(v + bfi) % 4] = seg1->verts[(int) sv[v]];
1599 // Form connections to children, first initialize all to unconnected.
1600 for (i=0; i<MAX_SIDES_PER_SEGMENT; i++) {
1601 bs->children[i] = -1;
1602 bs->sides[i].wall_num = -1;
1605 // Now form connections between segments.
1607 bs->children[AttachSide] = SEGMENT_NUMBER(seg1);
1608 bs->children[(int) Side_opposite[AttachSide]] = SEGMENT_NUMBER(seg2);
1610 seg1->children[side1] = SEGMENT_NUMBER(bs); // SEGMENT_NUMBER(seg2);
1611 seg2->children[side2] = SEGMENT_NUMBER(bs); // SEGMENT_NUMBER(seg1);
1613 // Validate bridge segment, and if degenerate, clean up mess.
1614 Degenerate_segment_found = 0;
1616 validate_segment(bs);
1618 if (Degenerate_segment_found) {
1619 seg1->children[side1] = -1;
1620 seg2->children[side2] = -1;
1621 bs->children[AttachSide] = -1;
1622 bs->children[(int) Side_opposite[AttachSide]] = -1;
1623 if (med_delete_segment(bs)) {
1624 mprintf((0, "Oops, tried to delete bridge segment (because it's degenerate), but couldn't.\n"));
1627 editor_status("Bridge segment would be degenerate, not created.\n");
1630 validate_segment(seg1); // used to only validate side, but segment does more error checking: ,side1);
1631 validate_segment(seg2); // ,side2);
1632 med_propagate_tmaps_to_segments(seg1,bs,0);
1634 editor_status("Bridge segment formed.");
1635 warn_if_concave_segment(bs);
1640 // -------------------------------------------------------------------------------
1641 // Create a segment given center, dimensions, rotation matrix.
1642 // Note that the created segment will always have planar sides and rectangular cross sections.
1643 // It will be created with walls on all sides, ie not connected to anything.
1644 void med_create_segment(segment *sp,fix cx, fix cy, fix cz, fix length, fix width, fix height, vms_matrix *mp)
1647 vms_vector v0,v1,cv;
1652 sp->segnum = 1; // What to put here? I don't know.
1653 sp2 = &Segment2s[sp->segnum];
1655 // Form connections to children, of which it has none.
1656 for (i=0; i<MAX_SIDES_PER_SEGMENT; i++) {
1657 sp->children[i] = -1;
1658 // sp->sides[i].render_flag = 0;
1659 sp->sides[i].wall_num = -1;
1663 sp2->matcen_num = -1;
1665 // Create relative-to-center vertices, which are the rotated points on the box defined by length, width, height
1666 sp->verts[0] = med_add_vertex(vm_vec_rotate(&v1,vm_vec_make(&v0,+width/2,+height/2,-length/2),mp));
1667 sp->verts[1] = med_add_vertex(vm_vec_rotate(&v1,vm_vec_make(&v0,+width/2,-height/2,-length/2),mp));
1668 sp->verts[2] = med_add_vertex(vm_vec_rotate(&v1,vm_vec_make(&v0,-width/2,-height/2,-length/2),mp));
1669 sp->verts[3] = med_add_vertex(vm_vec_rotate(&v1,vm_vec_make(&v0,-width/2,+height/2,-length/2),mp));
1670 sp->verts[4] = med_add_vertex(vm_vec_rotate(&v1,vm_vec_make(&v0,+width/2,+height/2,+length/2),mp));
1671 sp->verts[5] = med_add_vertex(vm_vec_rotate(&v1,vm_vec_make(&v0,+width/2,-height/2,+length/2),mp));
1672 sp->verts[6] = med_add_vertex(vm_vec_rotate(&v1,vm_vec_make(&v0,-width/2,-height/2,+length/2),mp));
1673 sp->verts[7] = med_add_vertex(vm_vec_rotate(&v1,vm_vec_make(&v0,-width/2,+height/2,+length/2),mp));
1675 // Now create the vector which is the center of the segment and add that to all vertices.
1676 while (!vm_vec_make(&cv,cx,cy,cz));
1678 // Now, add the center to all vertices, placing the segment in 3 space.
1679 for (i=0; i<MAX_VERTICES_PER_SEGMENT; i++)
1680 vm_vec_add(&Vertices[sp->verts[i]],&Vertices[sp->verts[i]],&cv);
1682 // Set scale vector.
1683 // sp->scale.x = width;
1684 // sp->scale.y = height;
1685 // sp->scale.z = length;
1687 // Add faces to all sides.
1688 for (f=0; f<MAX_SIDES_PER_SEGMENT; f++)
1689 create_walls_on_side(sp,f);
1691 sp->objects = -1; //no objects in this segment
1693 // Assume nothing special about this segment
1696 sp2->static_light = 0;
1697 sp2->matcen_num = -1;
1699 copy_tmaps_to_segment(sp, &New_segment);
1701 assign_default_uvs_to_segment(sp);
1704 // ----------------------------------------------------------------------------------------------
1705 // Create New_segment using a specified scale factor.
1706 void med_create_new_segment(vms_vector *scale)
1710 segment *sp = &New_segment;
1713 fix length,width,height;
1719 sp->segnum = 1; // What to put here? I don't know.
1720 sp2 = &Segment2s[sp->segnum];
1722 // Create relative-to-center vertices, which are the points on the box defined by length, width, height
1724 sp->verts[0] = med_set_vertex(NEW_SEGMENT_VERTICES+0,vm_vec_make(&v0,+width/2,+height/2,-length/2));
1725 sp->verts[1] = med_set_vertex(NEW_SEGMENT_VERTICES+1,vm_vec_make(&v0,+width/2,-height/2,-length/2));
1726 sp->verts[2] = med_set_vertex(NEW_SEGMENT_VERTICES+2,vm_vec_make(&v0,-width/2,-height/2,-length/2));
1727 sp->verts[3] = med_set_vertex(NEW_SEGMENT_VERTICES+3,vm_vec_make(&v0,-width/2,+height/2,-length/2));
1728 sp->verts[4] = med_set_vertex(NEW_SEGMENT_VERTICES+4,vm_vec_make(&v0,+width/2,+height/2,+length/2));
1729 sp->verts[5] = med_set_vertex(NEW_SEGMENT_VERTICES+5,vm_vec_make(&v0,+width/2,-height/2,+length/2));
1730 sp->verts[6] = med_set_vertex(NEW_SEGMENT_VERTICES+6,vm_vec_make(&v0,-width/2,-height/2,+length/2));
1731 sp->verts[7] = med_set_vertex(NEW_SEGMENT_VERTICES+7,vm_vec_make(&v0,-width/2,+height/2,+length/2));
1734 // sp->scale = *scale;
1736 // Form connections to children, of which it has none, init faces and tmaps.
1737 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
1738 sp->children[s] = -1;
1739 // sp->sides[s].render_flag = 0;
1740 sp->sides[s].wall_num = -1;
1741 create_walls_on_side(sp,s);
1742 sp->sides[s].tmap_num = s; // assign some stupid old tmap to this side.
1743 sp->sides[s].tmap_num2 = 0;
1746 Seg_orientation.p = 0; Seg_orientation.b = 0; Seg_orientation.h = 0;
1748 sp->objects = -1; //no objects in this segment
1750 assign_default_uvs_to_segment(sp);
1752 // Assume nothing special about this segment
1755 sp2->static_light = 0;
1756 sp2->matcen_num = -1;
1759 // -------------------------------------------------------------------------------
1760 void med_create_new_segment_from_cursegp(void)
1762 vms_vector scalevec;
1763 vms_vector uvec, rvec, fvec;
1765 med_extract_up_vector_from_segment_side(Cursegp, Curside, &uvec);
1766 med_extract_right_vector_from_segment_side(Cursegp, Curside, &rvec);
1767 extract_forward_vector_from_segment(Cursegp, &fvec);
1769 scalevec.x = vm_vec_mag(&rvec);
1770 scalevec.y = vm_vec_mag(&uvec);
1771 scalevec.z = vm_vec_mag(&fvec);
1773 med_create_new_segment(&scalevec);
1776 // -------------------------------------------------------------------------------
1777 // Initialize all vertices to inactive status.
1778 void init_all_vertices(void)
1783 for (v=0; v<MAX_SEGMENT_VERTICES; v++)
1784 Vertex_active[v] = 0;
1786 for (s=0; s<MAX_SEGMENTS; s++)
1787 Segments[s].segnum = -1;
1791 // --------------------------------------------------------------------------------------
1792 // Create a new mine, set global variables.
1793 int create_new_mine(void)
1797 vms_matrix m1 = IDENTITY_MATRIX;
1799 // initialize_mine_arrays();
1801 // gamestate_not_restored = 1;
1803 // Clear refueling center code
1805 // hostage_init_all();
1807 init_all_vertices();
1809 Current_level_num = 0; //0 means not a real level
1810 Current_level_name[0] = 0;
1812 Cur_object_index = -1;
1813 reset_objects(1); //just one object, the player
1819 Num_vertices = 0; // Number of vertices in global array.
1820 Highest_vertex_index = 0;
1821 Num_segments = 0; // Number of segments in global array, will get increased in med_create_segment
1822 Highest_segment_index = 0;
1823 Cursegp = Segments; // Say current segment is the only segment.
1824 Curside = WBACK; // The active side is the back side
1825 Markedsegp = 0; // Say there is no marked segment.
1826 Markedside = WBACK; // Shouldn't matter since Markedsegp == 0, but just in case...
1827 for (s=0;s<MAX_GROUPS+1;s++) {
1828 GroupList[s].num_segments = 0;
1829 GroupList[s].num_vertices = 0;
1830 Groupsegp[s] = NULL;
1834 Num_robot_centers = 0;
1839 // Create New_segment, which is the segment we will be adding at each instance.
1840 med_create_new_segment(vm_vec_make(&sizevec,DEFAULT_X_SIZE,DEFAULT_Y_SIZE,DEFAULT_Z_SIZE)); // New_segment = Segments[0];
1841 // med_create_segment(Segments,0,0,0,DEFAULT_X_SIZE,DEFAULT_Y_SIZE,DEFAULT_Z_SIZE,vm_mat_make(&m1,F1_0,0,0,0,F1_0,0,0,0,F1_0));
1842 med_create_segment(Segments,0,0,0,DEFAULT_X_SIZE,DEFAULT_Y_SIZE,DEFAULT_Z_SIZE,&m1);
1845 N_selected_segs = 0;
1848 //--repair-- create_local_segment_data();
1850 ControlCenterTriggers.num_links = 0;
1852 //editor_status("New mine created.");
1853 return 0; // say no error
1856 // --------------------------------------------------------------------------------------------------
1857 // Copy a segment from *ssp to *dsp. Do not simply copy the struct. Use *dsp's vertices, copying in
1858 // just the values, not the indices.
1859 void med_copy_segment(segment *dsp,segment *ssp)
1862 int verts_copy[MAX_VERTICES_PER_SEGMENT];
1864 // First make a copy of the vertex list.
1865 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++)
1866 verts_copy[v] = dsp->verts[v];
1868 // Now copy the whole struct.
1871 // Now restore the vertex indices.
1872 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++)
1873 dsp->verts[v] = verts_copy[v];
1875 // Now destructively modify the vertex values for all vertex indices.
1876 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++)
1877 Vertices[dsp->verts[v]] = Vertices[ssp->verts[v]];
1880 // -----------------------------------------------------------------------------
1881 // Create coordinate axes in orientation of specified segment, stores vertices at *vp.
1882 void create_coordinate_axes_from_segment(segment *sp,short *vertnums)
1887 med_extract_matrix_from_segment(sp,&rotmat);
1889 compute_segment_center(&Vertices[vertnums[0]],sp);
1892 vm_vec_scale(&t,i2f(32));
1893 vm_vec_add(&Vertices[vertnums[1]],&Vertices[vertnums[0]],&t);
1896 vm_vec_scale(&t,i2f(32));
1897 vm_vec_add(&Vertices[vertnums[2]],&Vertices[vertnums[0]],&t);
1900 vm_vec_scale(&t,i2f(32));
1901 vm_vec_add(&Vertices[vertnums[3]],&Vertices[vertnums[0]],&t);
1904 // -----------------------------------------------------------------------------
1905 // Determine if a segment is concave. Returns true if concave
1906 int check_seg_concavity(segment *s)
1911 for (sn=0;sn<MAX_SIDES_PER_SEGMENT;sn++)
1912 for (vn=0;vn<=4;vn++) {
1915 &Vertices[s->verts[Side_to_verts[sn][vn%4]]],
1916 &Vertices[s->verts[Side_to_verts[sn][(vn+1)%4]]],
1917 &Vertices[s->verts[Side_to_verts[sn][(vn+2)%4]]]);
1919 //vm_vec_normalize(&n1);
1921 if (vn>0) if (vm_vec_dotprod(&n0,&n1) < f0_5) return 1;
1930 // -----------------------------------------------------------------------------
1931 // Find all concave segments and add to list
1932 void find_concave_segs()
1939 for (s=Segments,i=Highest_segment_index;i>=0;s++,i--)
1940 if (s->segnum != -1)
1941 if (check_seg_concavity(s)) Warning_segs[N_warning_segs++]=SEG_PTR_2_NUM(s);
1947 // -----------------------------------------------------------------------------
1948 void warn_if_concave_segments(void)
1952 find_concave_segs();
1954 if (N_warning_segs) {
1955 editor_status("*** WARNING *** %d concave segments in mine! *** WARNING ***",N_warning_segs);
1956 sprintf( temp, "%d", N_warning_segs );
1960 // -----------------------------------------------------------------------------
1961 // Check segment s, if concave, warn
1962 void warn_if_concave_segment(segment *s)
1967 result = check_seg_concavity(s);
1970 Warning_segs[N_warning_segs++] = SEGMENT_NUMBER(s);
1972 if (N_warning_segs) {
1973 editor_status("*** WARNING *** New segment is concave! *** WARNING ***");
1974 sprintf( temp, "%d", N_warning_segs );
1977 // editor_status("");
1979 //editor_status("");
1983 // -------------------------------------------------------------------------------
1984 // Find segment adjacent to sp:side.
1985 // Adjacent means a segment which shares all four vertices.
1986 // Return true if segment found and fill in segment in adj_sp and side in adj_side.
1987 // Return false if unable to find, in which case adj_sp and adj_side are undefined.
1988 int med_find_adjacent_segment_side(segment *sp, int side, segment **adj_sp, int *adj_side)
1993 // Stuff abs_verts[4] array with absolute vertex indices
1995 abs_verts[v] = sp->verts[Side_to_verts[side][v]];
1997 // Scan all segments, looking for a segment which contains the four abs_verts
1998 for (seg=0; seg<=Highest_segment_index; seg++) {
1999 if (seg != SEGMENT_NUMBER(sp)) {
2000 for (v=0; v<4; v++) { // do for each vertex in abs_verts
2001 for (vv=0; vv<MAX_VERTICES_PER_SEGMENT; vv++) // do for each vertex in segment
2002 if (abs_verts[v] == Segments[seg].verts[vv])
2003 goto fass_found1; // Current vertex (indexed by v) is present in segment, try next
2004 goto fass_next_seg; // This segment doesn't contain the vertex indexed by v
2008 // All four vertices in sp:side are present in segment seg.
2009 // Determine side and return
2010 for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
2011 for (v=0; v<4; v++) {
2012 for (vv=0; vv<4; vv++) {
2013 if (Segments[seg].verts[Side_to_verts[s][v]] == abs_verts[vv])
2016 goto fass_next_side; // Couldn't find vertex v in current side, so try next side.
2019 // Found all four vertices in current side. We are done!
2020 *adj_sp = &Segments[seg];
2025 Assert(0); // Impossible -- we identified this segment as containing all 4 vertices of side "side", but we couldn't find them.
2035 #define JOINT_THRESHOLD 10000*F1_0 // (Huge threshold)
2037 // -------------------------------------------------------------------------------
2038 // Find segment closest to sp:side.
2039 // Return true if segment found and fill in segment in adj_sp and side in adj_side.
2040 // Return false if unable to find, in which case adj_sp and adj_side are undefined.
2041 int med_find_closest_threshold_segment_side(segment *sp, int side, segment **adj_sp, int *adj_side, fix threshold)
2044 vms_vector vsc, vtc; // original segment center, test segment center
2045 fix current_dist, closest_seg_dist;
2047 if (IS_CHILD(sp->children[side]))
2050 compute_center_point_on_side(&vsc, sp, side);
2052 closest_seg_dist = JOINT_THRESHOLD;
2054 // Scan all segments, looking for a segment which contains the four abs_verts
2055 for (seg=0; seg<=Highest_segment_index; seg++)
2056 if (seg != SEGMENT_NUMBER(sp))
2057 for (s=0;s<MAX_SIDES_PER_SEGMENT;s++) {
2058 if (!IS_CHILD(Segments[seg].children[s])) {
2059 compute_center_point_on_side(&vtc, &Segments[seg], s);
2060 current_dist = vm_vec_dist( &vsc, &vtc );
2061 if (current_dist < closest_seg_dist) {
2062 *adj_sp = &Segments[seg];
2064 closest_seg_dist = current_dist;
2069 if (closest_seg_dist < threshold)
2077 void med_check_all_vertices()
2085 for (s=0; s<Num_segments; s++) {
2087 if (sp->segnum != -1)
2088 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++)
2089 Assert(sp->verts[v] <= Highest_vertex_index);
2095 // -----------------------------------------------------------------------------------------------------
2096 void check_for_overlapping_segment(int segnum)
2100 vms_vector segcenter;
2102 compute_segment_center(&segcenter, &Segments[segnum]);
2104 for (i=0;i<=Highest_segment_index; i++) {
2106 masks = get_seg_masks(&segcenter, i, 0, __FILE__, __LINE__);
2107 if (masks.centermask == 0) {
2108 mprintf((0, "Segment %i center is contained in segment %i\n", segnum, i));
2112 for (v=0; v<8; v++) {
2113 vms_vector pdel, presult;
2115 vm_vec_sub(&pdel, &Vertices[Segments[segnum].verts[v]], &segcenter);
2116 vm_vec_scale_add(&presult, &segcenter, &pdel, (F1_0*15)/16);
2117 masks = get_seg_masks(&presult, i, 0, __FILE__, __LINE__);
2118 if (masks.centermask == 0) {
2119 mprintf((0, "Segment %i near vertex %i is contained in segment %i\n", segnum, v, i));
2128 // -----------------------------------------------------------------------------------------------------
2129 // Check for overlapping segments.
2130 void check_for_overlapping_segments(void)
2134 med_compress_mine();
2136 for (i=0; i<=Highest_segment_index; i++) {
2138 check_for_overlapping_segment(i);
2141 mprintf((0, "\nDone!\n"));