2 * $Logfile: /Freespace2/code/fred2/FredRender.cpp $
7 * Handles rendering the scene in the window for Fred. Also handles several other
11 * Revision 1.1 2002/05/03 03:28:08 root
15 * 6 4/07/99 6:21p Dave
16 * Fred and Freespace support for multiple background bitmaps and suns.
17 * Fixed link errors on all subprojects. Moved encrypt_init() to
18 * cfile_init() and lcl_init(), since its safe to call twice.
20 * 5 3/26/99 4:49p Dave
21 * Made cruisers able to dock with stuff. Made docking points and paths
24 * 4 2/07/99 8:51p Andsager
25 * Add inner bound to asteroid field. Inner bound tries to stay astroid
26 * free. Wrap when within and don't throw at ships inside.
28 * 3 1/27/99 4:09p Andsager
29 * Added highlight to ship subsystems
31 * 2 10/07/98 6:28p Dave
32 * Initial checkin. Renamed all relevant stuff to be Fred2 instead of
33 * Fred. Globalized mission and campaign file extensions. Removed Silent
34 * Threat specific code.
36 * 1 10/07/98 3:02p Dave
38 * 1 10/07/98 3:00p Dave
40 * 162 5/21/98 12:58a Hoffoss
41 * Fixed warnings optimized build turned up.
43 * 161 4/11/98 6:53p John
44 * Added first rev of subspace effect.
46 * 160 4/07/98 4:17p John
47 * Made Fred be able to move suns. Made suns actually affect the lighting
50 * 159 3/21/98 7:36p Lawrance
51 * Move jump nodes to own lib.
53 * 158 3/19/98 11:41a Hoffoss
54 * Fixed problems with rendering and reading of flying controls in Fred.
56 * 157 3/14/98 5:11p Hoffoss
57 * Changed the starting camera position and orientation for Fred.
59 * 156 3/10/98 4:24p Hoffoss
60 * Made object detection under cursor use the object's radius if it has
63 * 155 3/10/98 4:18p John
64 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
65 * & Glide have popups and print screen. Took out all >8bpp software
66 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
67 * support Fred. Made zbuffering key off of functions rather than one
70 * 154 3/10/98 1:41p Sandeep
72 * 153 3/09/98 10:56a Hoffoss
73 * Added jump node objects to Fred.
75 * 152 3/03/98 11:00a Andsager
76 * Fixed fred bug with control object initialization.
78 * 151 2/18/98 6:45p Hoffoss
79 * Added support for lines between icons in briefings for Fred.
81 * 150 2/12/98 3:48p Hoffoss
82 * Reduced scaler for extra precision.
84 * 149 2/10/98 6:43p Lawrance
85 * Moved asteroid code to a separate lib.
87 * 148 2/06/98 3:08p Mike
88 * More asteroid stuff, including resolving conflicts between the two
89 * asteroid_field structs!
91 * 147 1/27/98 11:02a John
92 * Added first rev of sparks. Made all code that calls model_render call
93 * clear_instance first. Made debris pieces not render by default when
94 * clear_instance is called.
96 * 146 1/16/98 2:22p Hoffoss
97 * Changed rendering colors of objects.
99 * 145 12/29/97 5:09p Allender
100 * fixed problems with speed not being reported properly in multiplayer
101 * games. Made read_flying_controls take frametime as a parameter. More
102 * ship/weapon select stuff
104 * 144 10/30/97 3:30p Hoffoss
105 * Made anti-aliased gridlines an option in Fred.
107 * 143 10/29/97 5:13p John
108 * Trying to fix my lighting bugs in Fred.
110 * 142 10/03/97 9:48a John
111 * took out alphacolors some grid lines don't draw antialiased.
113 * 141 10/03/97 8:55a John
114 * moved Fred's grid_render code out of MissionGrid and into Fred. Added
115 * code to turn background under overlays grey.
117 * 140 9/18/97 9:59a John
118 * fixed bug I caused in model_collide
120 * 139 9/16/97 9:41p Hoffoss
121 * Changed Fred code around to stop using Parse_player structure for
122 * player information, and use actual ships instead.
124 * 138 9/09/97 2:12p Hoffoss
125 * Added code to allow briefing editor view to be a 1:1 pixel size fixed
126 * as the FreeSpace view will have.
128 * 137 9/06/97 2:13p Mike
129 * Replace support for TEAM_NEUTRAL
131 * 136 8/29/97 5:41p Johnson
132 * Fixed bug with controlling marked objects without having done any
133 * rotations yet of any other type.
135 * 135 8/25/97 5:58p Hoffoss
136 * Created menu items for keypress functions in Fred, and fixed bug this
137 * uncovered with wing_delete function.
139 * 134 8/19/97 5:46p Hoffoss
140 * Changed font used in Fred, and added display to show current eye
143 * 133 8/19/97 11:23a Hoffoss
144 * Fixes to briefing editor icon select code.
146 * 132 8/17/97 10:22p Hoffoss
147 * Fixed several bugs in Fred with Undo feature. In the process, recoded
148 * a lot of CFile.cpp.
150 * 131 8/16/97 2:02a Hoffoss
151 * Made docked objects move together in Fred.
153 * 130 8/14/97 7:01p Hoffoss
154 * Fixed bug with outline rendering while in background bitmap editing
157 * 129 8/14/97 2:32p Hoffoss
158 * fixed bug where controlling an object doesn't cause screen updates, and
159 * added a number of cool features to viewpoint/control object code.
161 * 128 8/13/97 1:38p Hoffoss
162 * Added ability to update multiple times, which in needed in one case to
163 * brute force redraw so deleted ships actually do get removed from the
166 * 127 8/12/97 1:55a Hoffoss
167 * Made extensive changes to object reference checking and handling for
168 * object deletion call.
170 * 126 8/07/97 6:01p Hoffoss
171 * Added a rotate about selected object button to toolbar and
172 * functionality, as requested by Comet.
174 * 125 8/06/97 7:55p Hoffoss
175 * Fixed bug with objects not seeming to be where they are drawn (due to
176 * new briefing clip render stuff). This caused rendering problems,
177 * though. So I fixed those next.
179 * 124 8/06/97 6:10p Hoffoss
180 * Changed Fred to display a forced aspect ratio while briefing editor is
181 * open. This aspect ratio is the same as the briefing view in FreeSpace,
182 * so icons appear on and off screen the same as would be in FreeSpace.
184 * 123 7/24/97 10:24a Mike
185 * Restore support for Unknown team
187 * 122 7/17/97 1:34p Hoffoss
188 * Fixed Fred to work with physics changes.
190 * 121 6/26/97 6:03p Hoffoss
191 * briefing icon selection now has higher priority of selection than other
194 * 120 6/26/97 5:18p Hoffoss
195 * Major rework of briefing editor functionality.
197 * 119 6/26/97 4:20p Hoffoss
198 * Changed the behavior of Ctrl-L
206 #include "fredview.h"
214 #include "management.h"
223 #include "floating.h"
231 #include "missionparse.h"
232 #include "linklist.h"
234 #include "3dinternal.h"
237 #include "fredrender.h"
239 #include "starfield.h"
241 #include "lighting.h"
242 #include "asteroid.h"
243 #include "jumpnode.h"
245 extern float flFrametime;
246 extern subsys_to_render Render_subsys;
249 #define new DEBUG_NEW
251 static char THIS_FILE[] = __FILE__;
254 #define MAX_FRAMETIME (F1_0/4) // Frametime gets saturated at this.
255 #define MIN_FRAMETIME (F1_0/120)
256 #define MAX_STARS 500
257 #define LOLLIPOP_SIZE 2.5f
259 int Aa_gridlines = 0;
260 int Fred_outline = 0;
263 int Editing_mode = 1;
264 int Control_mode = 0;
265 int last_x=0, last_y=0;
267 int Show_outlines = 0;
269 int Show_grid_positions = 1;
270 int Show_coordinates = 0;
271 int Show_distances = 0;
272 int Show_horizon = 0;
273 int Show_asteroid_field = 1;
275 int Single_axis_constraint = 0;
276 int Universal_heading = 0;
277 int Flying_controls_mode = 1;
278 int Group_rotate = 1;
279 int info_popup_active = 0;
280 int rendering_order[MAX_SHIPS];
281 int render_count = 0;
282 int Last_cursor_over = -1;
283 int True_rw, True_rh;
284 int Fixed_briefing_size = 1;
287 vector my_pos = {0.0f, 0.0f, -5.0f};
288 vector view_pos, eye_pos, Viewer_pos, Last_eye_pos = { 0.0f };
289 vector Last_control_pos = { 0.0f };
291 vector Constraint = { 1.0f, 0.0f, 1.0f };
292 vector Anticonstraint = { 0.0f, 1.0f, 0.0f };
293 vector Tp1, Tp2; // test points
295 matrix my_orient = IDENTITY_MATRIX;
296 matrix trackball_orient = IDENTITY_MATRIX;
297 matrix view_orient = IDENTITY_MATRIX, eye_orient, Last_eye_orient = IDENTITY_MATRIX;
298 matrix Last_control_orient = IDENTITY_MATRIX;
299 physics_info view_physics;
300 control_info view_controls;
303 static vector Global_light_world = { 0.208758f, -0.688253f, -0.694782f };
305 void display_distances();
306 void render_model_x(vector *pos, grid *gridp, int col_scheme = 0);
307 void draw_orient_sphere(object *obj, int r, int g, int b);
308 void draw_orient_sphere2(int col, object *obj, int r, int g, int b);
309 void render_compass(void);
310 void draw_compass_arrow(vector *v0);
311 void process_controls(vector *pos, matrix *orient, float frametime, int key, int mode = 0);
312 void render_one_model(object *objp);
313 void inc_mission_time();
314 void draw_asteroid_field();
315 void hilight_bitmap();
317 // Called every time a new mission is created (and erasing old mission from memory).
318 // New mission should be blank at this point.
319 void fred_render_init()
323 physics_init(&view_physics);
324 view_physics.max_vel.z = 5.0f; //forward/backward
325 view_physics.max_rotvel.x = 1.5f; //pitch
326 memset(&view_controls, 0, sizeof(control_info));
328 vm_vec_make(&view_pos, 0.0f, 150.0f, -200.0f);
329 vm_vec_make(&f, 0.0f, -0.5f, 0.866025404f); // 30 degree angle
330 vm_vec_make(&u, 0.0f, 0.866025404f, 0.5f);
331 vm_vec_make(&r, 1.0f, 0.0f, 0.0f);
332 vm_vector_2_matrix(&view_orient, &f, &u, &r);
334 The_grid = create_default_grid();
335 maybe_create_new_grid(The_grid, &view_pos, &view_orient, 1);
336 // vm_set_identity(&view_orient);
339 void level_object(matrix *orient)
343 u = orient->uvec = The_grid->gmatrix.uvec;
344 if (u.x) // y-z plane
346 orient->fvec.x = orient->rvec.x = 0.0f;
348 } else if (u.y) { // x-z plane
349 orient->fvec.y = orient->rvec.y = 0.0f;
351 } else if (u.z) { // x-y plane
352 orient->fvec.z = orient->rvec.z = 0.0f;
355 vm_fix_matrix(orient);
358 void level_controlled()
360 int cmode, count = 0;
363 cmode = Control_mode;
364 if ((viewpoint == 1) && !cmode)
368 case 0: // Control the viewer's location and orientation
369 level_object(&view_orient);
372 case 2: // Control viewpoint object
373 level_object(&Objects[view_obj].orient);
374 object_moved(&Objects[view_obj]);
376 FREDDoc_ptr->autosave("level object");
379 case 1: // Control the current object's location and orientation
380 objp = GET_FIRST(&obj_used_list);
381 while (objp != END_OF_LIST(&obj_used_list)) {
382 if (objp->flags & OF_MARKED)
383 level_object(&objp->orient);
385 objp = GET_NEXT(objp);
388 objp = GET_FIRST(&obj_used_list);
389 while (objp != END_OF_LIST(&obj_used_list)) {
390 if (objp->flags & OF_MARKED) {
395 objp = GET_NEXT(objp);
400 FREDDoc_ptr->autosave("level objects");
402 FREDDoc_ptr->autosave("level object");
413 void align_vector_to_axis(vector *v)
429 if ((x > y) && (x > z)) { // x axis
430 if (v->x < 0) // negative x
431 vm_vec_make(v, -1.0f, 0.0f, 0.0f);
433 vm_vec_make(v, 1.0f, 0.0f, 0.0f);
435 } else if (y > z) { // y axis
436 if (v->y < 0) // negative y
437 vm_vec_make(v, 0.0f, -1.0f, 0.0f);
439 vm_vec_make(v, 0.0f, 1.0f, 0.0f);
442 if (v->z < 0) // negative z
443 vm_vec_make(v, 0.0f, 0.0f, -1.0f);
445 vm_vec_make(v, 0.0f, 0.0f, 1.0f);
449 void verticalize_object(matrix *orient)
451 align_vector_to_axis(&orient->fvec);
452 align_vector_to_axis(&orient->uvec);
453 align_vector_to_axis(&orient->rvec);
454 vm_fix_matrix(orient); // just in case something odd occurs.
457 void verticalize_controlled()
459 int cmode, count = 0;
462 cmode = Control_mode;
463 if ((viewpoint == 1) && !cmode)
467 case 0: // Control the viewer's location and orientation
468 verticalize_object(&view_orient);
471 case 2: // Control viewpoint object
472 verticalize_object(&Objects[view_obj].orient);
473 object_moved(&Objects[view_obj]);
474 FREDDoc_ptr->autosave("align object");
478 case 1: // Control the current object's location and orientation
479 objp = GET_FIRST(&obj_used_list);
480 while (objp != END_OF_LIST(&obj_used_list)) {
481 if (objp->flags & OF_MARKED)
482 verticalize_object(&objp->orient);
484 objp = GET_NEXT(objp);
487 objp = GET_FIRST(&obj_used_list);
488 while (objp != END_OF_LIST(&obj_used_list)) {
489 if (objp->flags & OF_MARKED) {
494 objp = GET_NEXT(objp);
499 FREDDoc_ptr->autosave("align objects");
501 FREDDoc_ptr->autosave("align object");
512 void move_mouse( int btn, int mdx, int mdy )
522 matrix tempm, mousem;
525 vm_trackball( dx, dy, &mousem );
526 vm_matrix_x_matrix(&tempm, &trackball_orient, &mousem);
527 trackball_orient = tempm;
528 view_orient = trackball_orient;
533 my_pos.z += (float)dy;
537 ///////////////////////////////////////////////////
538 void process_system_keys(int key)
540 // mprintf(("Key = %d\n", key));
544 CFREDView::GetView()->cycle_constraint();
547 case KEY_R: // for some stupid reason, an accelerator for 'R' doesn't work.
552 Selection_lock = !Selection_lock;
563 void render_waypoints(void)
569 for (i=0; i<Num_waypoint_lists; i++)
571 ptr = &Waypoint_lists[i];
572 for (j=0; j<ptr->count; j++)
574 g3_rotate_vertex(&v, &ptr->waypoints[j]);
575 if (!(v.codes & CC_BEHIND))
576 if (!(g3_project_vertex(&v) & PF_OVERFLOW))
578 if (cur_waypoint_list == i && cur_waypoint == j)
579 gr_set_color(255, 255, 255);
580 else if (Waypoint_lists[i].flags[j] & WL_MARKED)
581 gr_set_color(160, 255, 0);
583 gr_set_color(160, 96, 0);
585 g3_draw_sphere(&v, LOLLIPOP_SIZE);
587 gr_set_color(0, 0, 0);
589 gr_set_color(160, 96, 0);
591 g3_draw_sphere(&v, LOLLIPOP_SIZE * 0.66667f);
592 gr_set_color(160, 96, 0);
593 g3_draw_sphere(&v, LOLLIPOP_SIZE * 0.33333f);
597 for (j=0; j<ptr->count; j++)
598 render_model_x(&ptr->waypoints[j], The_grid, 1);
600 gr_set_color(160, 96, 0);
601 for (j=1; j<ptr->count; j++)
602 rpd_line(&ptr->waypoints[j-1], &ptr->waypoints[j]);
606 // --------------------------------------------------------------------------------
607 // get_subsystem_world_pos2() returns the world position for a given subobject on a ship
610 vector* get_subsystem_world_pos2(object* parent_obj, ship_subsys* subsys, vector* world_pos)
612 if (subsys == NULL) {
613 *world_pos = parent_obj->pos;
617 vm_vec_unrotate(world_pos, &subsys->system_info->pnt, &parent_obj->orient);
618 vm_vec_add2(world_pos, &parent_obj->pos);
623 // returns 1 for valid bounding rect, 0 otherwise
624 int get_subsys_bounding_rect(object *ship_obj, ship_subsys *subsys, int *x1, int *x2, int *y1, int *y2)
626 if (subsys != NULL) {
627 vertex subobj_vertex;
630 get_subsystem_world_pos2(ship_obj, subsys, &subobj_pos);
632 g3_rotate_vertex(&subobj_vertex, &subobj_pos);
634 g3_project_vertex(&subobj_vertex);
635 if (subobj_vertex.flags & PF_OVERFLOW) // if overflow, no point in drawing brackets
640 bound_rc = subobj_find_2d_bound(subsys->system_info->radius, &ship_obj->orient, &subobj_pos, x1, y1, x2, y2);
650 void cancel_display_active_ship_subsystem()
652 Render_subsys.do_render = false;
653 Render_subsys.ship_obj = NULL;
654 Render_subsys.cur_subsys = NULL;
657 void display_active_ship_subsystem()
659 if (cur_object_index != -1) {
660 if (Objects[cur_object_index].type == OBJ_SHIP) {
662 object *objp = &Objects[cur_object_index];
666 // switching to a new ship, so reset
667 if (objp != Render_subsys.ship_obj) {
668 cancel_display_active_ship_subsystem();
672 if (Render_subsys.do_render) {
675 strcpy(buf, Render_subsys.cur_subsys->system_info->subobj_name);
678 if ( get_subsys_bounding_rect(objp, Render_subsys.cur_subsys, &x1, &x2, &y1, &y2) ) {
681 gr_set_color(255, 32, 32);
684 gr_line(x1, y1, x1, y2); gr_line(x1-1, y1, x1-1, y2);
685 gr_line(x1, y2, x2, y2); gr_line(x1, y2+1, x2, y2+1);
686 gr_line(x2, y2, x2, y1); gr_line(x2+1, y2, x2+1, y1);
687 gr_line(x2, y1, x1, y1); gr_line(x2, y1-1, x1, y1-1);
690 gr_string_win( (x1+x2)/2, y2 + 10, buf);
695 cancel_display_active_ship_subsystem();
699 void render_models(void)
702 obj_render_all(render_one_model);
704 Briefing_dialog->batch_render();
707 void render_one_model(object *objp)
712 Assert(objp->type != OBJ_NONE);
714 if ( objp->type == OBJ_JUMP_NODE ) {
718 if ((objp->type == OBJ_WAYPOINT) && !Show_waypoints)
721 if ((objp->type == OBJ_START) && !Show_starts)
724 if (objp->type == OBJ_SHIP) {
728 if ((Ships[objp->instance].team == TEAM_FRIENDLY) && !Show_friendly)
731 if ((Ships[objp->instance].team == TEAM_HOSTILE) && !Show_hostile)
734 if ((Ships[objp->instance].team == TEAM_NEUTRAL) && !Show_neutral)
738 if (objp->flags & OF_HIDDEN)
741 rendering_order[render_count] = OBJ_INDEX(objp);
743 if ((OBJ_INDEX(objp) == cur_object_index) && !Bg_bitmap_dialog)
744 Fred_outline = 0xffffff;
746 else if ((objp->flags & OF_MARKED) && !Bg_bitmap_dialog) // is it a marked object?
747 Fred_outline = 0x9fff00;
749 else if ((objp->type == OBJ_SHIP) && Show_outlines) {
750 switch (Ships[objp->instance].team) {
751 case TEAM_FRIENDLY: Fred_outline = 0x0000c0; break;
752 case TEAM_NEUTRAL: Fred_outline = 0x007f7f; break;
753 case TEAM_UNKNOWN: Fred_outline = 0x684010; break;
754 case TEAM_HOSTILE: Fred_outline = 0x7f0000; break;
756 Fred_outline = 0x7f007f; break;
759 } else if ((objp->type == OBJ_START) && Show_outlines) {
760 Fred_outline = 0x007f00;
766 if ((Show_ship_models || Show_outlines) && ((objp->type == OBJ_SHIP) || (objp->type == OBJ_START))){
767 if (Show_ship_models){
773 if(Show_dock_points){
783 model_clear_instance( Ships[z].modelnum );
785 // if (!viewpoint || OBJ_INDEX(objp) != cur_object_index)
788 model_set_outline_color(Fred_outline >> 16, (Fred_outline >> 8) & 0xff, Fred_outline & 0xff);
789 model_render(Ships[z].modelnum, &objp->orient, &objp->pos, j | MR_SHOW_OUTLINE);
791 model_render(Ships[z].modelnum, &objp->orient, &objp->pos, j);
796 int r = 0, g = 0, b = 0;
798 if (objp->type == OBJ_SHIP)
803 switch (Ships[objp->instance].team) {
804 case TEAM_FRIENDLY: r = 0; g = 0; b = 192; break;
805 case TEAM_NEUTRAL: r = 0; g = 127; b = 127; break;
806 case TEAM_UNKNOWN: r = 104; g = 64; b = 16; break;
807 case TEAM_HOSTILE: r = 127; g = 0; b = 0; break;
808 default: r = 127; g = 0; b = 0; break;
811 } else if (objp->type == OBJ_START) {
812 r = 0; g = 127; b = 0;
814 } else if (objp->type == OBJ_WAYPOINT) {
815 r = 96; g = 0; b = 112;
817 } else if (objp->type == OBJ_POINT) {
818 if (objp->instance != BRIEFING_LOOKAT_POINT_ID) {
819 Assert(Briefing_dialog);
820 Briefing_dialog->draw_icon(objp);
824 r = 196; g = 32; b = 196;
830 draw_orient_sphere2(Fred_outline, objp, r, g, b);
832 draw_orient_sphere(objp, r, g, b);
836 if (objp->type == OBJ_WAYPOINT)
837 for (j=0; j<render_count; j++)
839 o2 = &Objects[rendering_order[j]];
840 if (o2->type == OBJ_WAYPOINT)
841 if ((o2->instance == objp->instance - 1) || (o2->instance == objp->instance + 1))
842 rpd_line(&o2->pos, &objp->pos);
845 render_model_x(&objp->pos, The_grid);
849 void display_distances()
856 gr_set_color(255, 0, 0);
857 objp = GET_FIRST(&obj_used_list);
858 while (objp != END_OF_LIST(&obj_used_list))
860 if (objp->flags & OF_MARKED)
863 while (o2 != END_OF_LIST(&obj_used_list))
865 if (o2->flags & OF_MARKED)
867 rpd_line(&objp->pos, &o2->pos);
868 vm_vec_avg(&pos, &objp->pos, &o2->pos);
869 g3_rotate_vertex(&v, &pos);
870 if (!(v.codes & CC_BEHIND))
871 if (!(g3_project_vertex(&v) & PF_OVERFLOW)) {
872 sprintf(buf, "%.1f", vm_vec_dist(&objp->pos, &o2->pos));
873 gr_string_win((int) v.sx, (int) v.sy, buf);
881 objp = GET_NEXT(objp);
885 void display_ship_info()
887 char buf[512], pos[80];
892 objp = GET_FIRST(&obj_used_list);
893 while (objp != END_OF_LIST(&obj_used_list)) {
894 Assert(objp->type != OBJ_NONE);
897 if (OBJ_INDEX(objp) == cur_object_index)
898 Fred_outline = 0xffffff;
899 else if (objp->flags & OF_MARKED) // is it a marked object?
900 Fred_outline = 0x9fff00;
904 if ((objp->type == OBJ_WAYPOINT) && !Show_waypoints)
907 if ((objp->type == OBJ_START) && !Show_starts)
910 if (objp->type == OBJ_SHIP) {
914 if ((Ships[objp->instance].team == TEAM_FRIENDLY) && !Show_friendly)
917 if ((Ships[objp->instance].team == TEAM_HOSTILE) && !Show_hostile)
920 if ((Ships[objp->instance].team == TEAM_NEUTRAL) && !Show_neutral)
924 if (objp->flags & OF_HIDDEN)
927 g3_rotate_vertex(&v, &objp->pos);
928 if (!(v.codes & CC_BEHIND) && render)
929 if (!(g3_project_vertex(&v) & PF_OVERFLOW)) {
933 if ((objp->type == OBJ_SHIP) || (objp->type == OBJ_START)) {
937 shipp = &Ships[objp->instance];
938 ship_type = shipp->ship_info_index;
939 ASSERT(ship_type >= 0);
940 sprintf(buf, "%s\n%s", shipp->ship_name, Ship_info[ship_type].short_name);
942 } else if (objp->type == OBJ_WAYPOINT) {
943 sprintf(buf, "%s\nWaypoint %d",
944 Waypoint_lists[objp->instance / 65536].name,
945 (objp->instance & 0xffff) + 1);
947 } else if (objp->type == OBJ_POINT) {
948 if (objp->instance == BRIEFING_LOOKAT_POINT_ID)
949 strcpy(buf, "Camera lookat point");
951 strcpy(buf, "Briefing icon");
953 } else if (objp->type == OBJ_JUMP_NODE) {
954 strcpy(buf, "Jump Node");
959 if (Show_coordinates)
961 sprintf(pos, "( %.0f , %.0f , %.0f )", objp->pos.x, objp->pos.y, objp->pos.z);
971 gr_set_color(Fred_outline >> 16, (Fred_outline >> 8) & 0xff, Fred_outline & 0xff);
973 gr_set_color(160, 160, 160);
975 gr_string_win((int) v.sx, (int) v.sy, buf);
979 objp = GET_NEXT(objp);
983 void draw_orient_sphere(object *obj, int r, int g, int b)
990 size = fl_sqrt(vm_vec_dist(&eye_pos, &obj->pos) / 20.0f);
991 if (size < LOLLIPOP_SIZE)
992 size = LOLLIPOP_SIZE;
994 if ((obj->type != OBJ_WAYPOINT) && (obj->type != OBJ_POINT))
996 flag = (vm_vec_dotprod(&eye_orient.fvec, &obj->orient.fvec) < 0.0f);
998 vm_vec_scale_add2(&v1, &obj->orient.fvec, size);
999 vm_vec_scale_add2(&v2, &obj->orient.fvec, size * 1.5f);
1002 gr_set_color(192, 192, 192);
1007 gr_set_color(r, g, b);
1008 g3_rotate_vertex(&v, &obj->pos);
1009 if (!(v.codes & CC_BEHIND))
1010 if (!(g3_project_vertex(&v) & PF_OVERFLOW))
1011 g3_draw_sphere(&v, size);
1014 gr_set_color(192, 192, 192);
1019 void draw_orient_sphere2(int col, object *obj, int r, int g, int b)
1026 size = fl_sqrt(vm_vec_dist(&eye_pos, &obj->pos) / 20.0f);
1027 if (size < LOLLIPOP_SIZE)
1028 size = LOLLIPOP_SIZE;
1030 if ((obj->type != OBJ_WAYPOINT) && (obj->type != OBJ_POINT))
1032 flag = (vm_vec_dotprod(&eye_orient.fvec, &obj->orient.fvec) < 0.0f);
1035 vm_vec_scale_add2(&v1, &obj->orient.fvec, size);
1036 vm_vec_scale_add2(&v2, &obj->orient.fvec, size * 1.5f);
1039 gr_set_color(192, 192, 192);
1044 g3_rotate_vertex(&v, &obj->pos);
1045 if (!(v.codes & CC_BEHIND))
1046 if (!(g3_project_vertex(&v) & PF_OVERFLOW))
1048 gr_set_color((col >> 16) & 0xff, (col >> 8) & 0xff, col & 0xff);
1049 g3_draw_sphere(&v, size);
1050 gr_set_color(r, g, b);
1051 g3_draw_sphere(&v, size * 0.75f);
1055 gr_set_color(192, 192, 192);
1060 void render_model_x(vector *pos, grid *gridp, int col_scheme)
1062 vector gpos; // Location of point on grid.
1068 if (!Show_grid_positions)
1071 tplane.A = gridp->gmatrix.uvec.x;
1072 tplane.B = gridp->gmatrix.uvec.y;
1073 tplane.C = gridp->gmatrix.uvec.z;
1074 tplane.D = gridp->planeD;
1076 compute_point_on_plane(&gpos, &tplane, pos);
1077 dxz = vm_vec_dist(pos, &gpos)/8.0f;
1078 gv = &gridp->gmatrix.uvec;
1079 if (gv->x * pos->x + gv->y * pos->y + gv->z * pos->z < -gridp->planeD)
1080 gr_set_color(0, 127, 0);
1082 gr_set_color(192, 192, 192);
1085 rpd_line(&gpos, pos); // Line from grid to object center.
1089 vm_vec_scale_add2(&gpos, &gridp->gmatrix.rvec, -dxz/2);
1090 vm_vec_scale_add2(&gpos, &gridp->gmatrix.fvec, -dxz/2);
1092 vm_vec_scale_add2(&tpos, &gridp->gmatrix.rvec, dxz/2);
1093 vm_vec_scale_add2(&tpos, &gridp->gmatrix.fvec, dxz/2);
1095 rpd_line(&gpos, &tpos);
1097 vm_vec_scale_add2(&gpos, &gridp->gmatrix.rvec, dxz);
1098 vm_vec_scale_add2(&tpos, &gridp->gmatrix.rvec, -dxz);
1100 rpd_line(&gpos, &tpos);
1103 void render_active_rect(void)
1106 gr_set_color(255, 255, 255);
1107 gr_line(marking_box.x1, marking_box.y1, marking_box.x1, marking_box.y2);
1108 gr_line(marking_box.x1, marking_box.y2, marking_box.x2, marking_box.y2);
1109 gr_line(marking_box.x2, marking_box.y2, marking_box.x2, marking_box.y1);
1110 gr_line(marking_box.x2, marking_box.y1, marking_box.x1, marking_box.y1);
1115 void process_movement_keys(int key, vector *mvec, angles *angs)
1126 raw_key = key & 0xff;
1129 case KEY_PAD1: mvec->x += -1.0f; break;
1130 case KEY_PAD3: mvec->x += +1.0f; break;
1131 case KEY_PADPLUS: mvec->y += -1.0f; break;
1132 case KEY_PADMINUS: mvec->y += +1.0f; break;
1133 case KEY_A: mvec->z += +1.0f; break;
1134 case KEY_Z: mvec->z += -1.0f; break;
1135 case KEY_PAD4: angs->h += -0.1f; break;
1136 case KEY_PAD6: angs->h += +0.1f; break;
1137 case KEY_PAD8: angs->p += -0.1f; break;
1138 case KEY_PAD2: angs->p += +0.1f; break;
1139 case KEY_PAD7: angs->b += -0.1f; break;
1140 case KEY_PAD9: angs->b += +0.1f; break;
1144 if (key & KEY_SHIFTED) {
1145 vm_vec_scale(mvec, 5.0f);
1152 void process_controls(vector *pos, matrix *orient, float frametime, int key, int mode)
1154 if (Flying_controls_mode) {
1155 grid_read_camera_controls(&view_controls, frametime);
1157 if (key_get_shift_status())
1158 memset(&view_controls, 0, sizeof(control_info));
1160 if ((fabs(view_controls.pitch) > (frametime / 100)) &&
1161 (fabs(view_controls.vertical) > (frametime / 100)) &&
1162 (fabs(view_controls.heading) > (frametime / 100)) &&
1163 (fabs(view_controls.sideways) > (frametime / 100)) &&
1164 (fabs(view_controls.bank) > (frametime / 100)) &&
1165 (fabs(view_controls.forward) > (frametime / 100)))
1168 flFrametime = frametime;
1169 physics_read_flying_controls(orient, &view_physics, &view_controls, flFrametime);
1171 physics_sim_editor(pos, orient, &view_physics, frametime);
1173 physics_sim(pos, orient, &view_physics, frametime);
1176 vector movement_vec, rel_movement_vec;
1178 matrix newmat, rotmat;
1180 process_movement_keys(key, &movement_vec, &rotangs);
1181 vm_vec_rotate(&rel_movement_vec, &movement_vec, &The_grid->gmatrix);
1182 vm_vec_add2(pos, &rel_movement_vec);
1184 vm_angles_2_matrix(&rotmat, &rotangs);
1185 if (rotangs.h && Universal_heading)
1186 vm_transpose_matrix(orient);
1187 vm_matrix_x_matrix(&newmat, orient, &rotmat);
1189 if (rotangs.h && Universal_heading)
1190 vm_transpose_matrix(orient);
1194 int Fred_grid_colors_inited = 0;
1195 color Fred_grid_bright, Fred_grid_dark, Fred_grid_bright_aa, Fred_grid_dark_aa;
1197 // Renders a grid defined in a grid struct
1198 void fred_render_grid(grid *gridp)
1200 int i, ncols, nrows;
1202 if ( !Fred_grid_colors_inited ) {
1203 Fred_grid_colors_inited = 1;
1205 gr_init_alphacolor( &Fred_grid_dark_aa, 64, 64, 64, 255 );
1206 gr_init_alphacolor( &Fred_grid_bright_aa, 128, 128, 128, 255 );
1207 gr_init_color( &Fred_grid_dark, 64, 64, 64 );
1208 gr_init_color( &Fred_grid_bright, 128, 128, 128 );
1211 ncols = gridp->ncols;
1212 nrows = gridp->nrows;
1213 if (double_fine_gridlines) {
1219 gr_set_color_fast(&Fred_grid_dark_aa);
1221 gr_set_color_fast(&Fred_grid_dark);
1223 // Draw the column lines.
1224 for (i=0; i<=ncols; i++)
1225 rpd_line(&gridp->gpoints1[i], &gridp->gpoints2[i]);
1227 // Draw the row lines.
1228 for (i=0; i<=nrows; i++)
1229 rpd_line(&gridp->gpoints3[i], &gridp->gpoints4[i]);
1231 ncols = gridp->ncols / 2;
1232 nrows = gridp->nrows / 2;
1234 // now draw the larger, brighter gridlines that is x10 the scale of smaller one.
1236 gr_set_color_fast(&Fred_grid_bright_aa);
1238 gr_set_color_fast(&Fred_grid_bright);
1240 for (i=0; i<=ncols; i++)
1241 rpd_line(&gridp->gpoints5[i], &gridp->gpoints6[i]);
1243 for (i=0; i<=nrows; i++)
1244 rpd_line(&gridp->gpoints7[i], &gridp->gpoints8[i]);
1250 int x, y, w, h, inst;
1260 if (Briefing_dialog) {
1263 Fred_main_wnd->GetClientRect(rect);
1264 True_rw = rect.Width();
1265 True_rh = rect.Height();
1266 if (Fixed_briefing_size) {
1267 True_rw = BRIEF_GRID_W;
1268 True_rh = BRIEF_GRID_H;
1271 if ((float) True_rh / (float) True_rw > (float) BRIEF_GRID_H / (float) BRIEF_GRID_W) {
1272 True_rh = (int) ((float) BRIEF_GRID_H * (float) True_rw / (float) BRIEF_GRID_W);
1274 } else { // Fred is wider than briefing window
1275 True_rw = (int) ((float) BRIEF_GRID_W * (float) True_rh / (float) BRIEF_GRID_H);
1280 gr_set_color(255, 255, 255);
1281 gr_line(0, True_rh, True_rw, True_rh);
1282 gr_line(True_rw, 0, True_rw, True_rh);
1284 gr_set_clip(0, 0, True_rw, True_rh);
1287 g3_start_frame(1); // 1 means use zbuffering
1288 gr_set_font(Fred_font);
1291 g3_set_view_matrix(&eye_pos, &eye_orient, 0.5f);
1292 Viewer_pos = eye_pos; // for starfield code
1294 if ( Bg_bitmap_dialog ) {
1295 stars_draw( Show_stars, 1, Show_stars, 0 );
1297 stars_draw( Show_stars, Show_stars, Show_stars, 0 );
1301 gr_set_color(128, 128, 64);
1302 g3_draw_horizon_line();
1305 if (Show_asteroid_field) {
1306 gr_set_color(192, 96, 16);
1307 draw_asteroid_field();
1311 fred_render_grid(The_grid);
1312 if (Bg_bitmap_dialog)
1315 gr_set_color(0, 0, 64);
1318 display_distances();
1320 display_ship_info();
1321 display_active_ship_subsystem();
1322 render_active_rect();
1324 if (query_valid_object(Cursor_over)) { // display a tool-tip like infobox
1325 pos = Objects[Cursor_over].pos;
1326 inst = Objects[Cursor_over].instance;
1327 if ((Objects[Cursor_over].type == OBJ_SHIP) || (Objects[Cursor_over].type == OBJ_START)) {
1328 vm_extract_angles_matrix(&a, &Objects[Cursor_over].orient);
1329 sprintf(buf, "%s\n%s\n( %.1f , %.1f , %.1f )\nHeading: %.2f\nPitch: %.2f\nBank: %.2f",
1330 Ships[inst].ship_name, Ship_info[Ships[inst].ship_info_index].short_name,
1331 pos.x, pos.y, pos.z, a.h, a.p, a.b);
1333 } else if (Objects[Cursor_over].type == OBJ_WAYPOINT) {
1334 sprintf(buf, "%s\nWaypoint %d\n( %.1f , %.1f , %.1f )",
1335 Waypoint_lists[inst / 65536].name, (inst & 0xffff) + 1, pos.x, pos.y, pos.z);
1337 } else if (Objects[Cursor_over].type == OBJ_POINT) {
1338 sprintf(buf, "Briefing icon\n( %.1f , %.1f , %.1f )", pos.x, pos.y, pos.z);
1341 sprintf(buf, "( %.1f , %.1f , %.1f )", pos.x, pos.y, pos.z);
1343 g3_rotate_vertex(&v, &pos);
1344 if (!(v.codes & CC_BEHIND))
1345 if (!(g3_project_vertex(&v) & PF_OVERFLOW)) {
1347 y = (int) v.sy + 20;
1348 gr_get_string_size_win(&w, &h, buf);
1349 gr_set_color(192, 192, 192);
1350 gr_rect(x-1, y-1, w+2, h+2);
1351 gr_set_color(255, 255, 255);
1352 gr_line(x-2, y-2, x+w+1, y-2);
1353 gr_line(x-2, y-2, x-2, y+h+1);
1354 gr_line(x+w+1, y-2, x+w+1, y+h+1);
1355 gr_line(x-2, y+h+1, x+w+1, y+h+1);
1357 gr_set_color(0, 0, 0);
1358 gr_string_win(x, y, buf);
1362 gr_set_color(0, 160, 0);
1363 jumpnode_render_all();
1365 sprintf(buf, "( %.1f , %.1f , %.1f )", eye_pos.x, eye_pos.y, eye_pos.z);
1366 gr_get_string_size_win(&w, &h, buf);
1367 gr_set_color(192, 192, 192);
1368 gr_string_win(gr_screen.max_w - w - 2, 2, buf);
1373 if (Briefing_dialog)
1374 gr_set_clip(0, 0, True_rw, True_rh);
1377 g3_set_view_matrix(&eye_pos, &eye_orient, 0.5f);
1380 void game_do_frame()
1383 vector viewer_position, control_pos;
1385 matrix control_orient;
1389 viewer_position = my_orient.fvec;
1390 vm_vec_scale(&viewer_position,my_pos.z);
1392 if ((viewpoint == 1) && !query_valid_object(view_obj))
1396 process_system_keys(key);
1397 cmode = Control_mode;
1398 if ((viewpoint == 1) && !cmode)
1401 control_pos = Last_control_pos;
1402 control_orient = Last_control_orient;
1404 // if ((key & KEY_MASK) == key) // unmodified
1406 case 0: // Control the viewer's location and orientation
1407 process_controls(&view_pos, &view_orient, f2fl(Frametime), key, 1);
1408 control_pos = view_pos;
1409 control_orient = view_orient;
1412 case 2: // Control viewpoint object
1413 process_controls(&Objects[view_obj].pos, &Objects[view_obj].orient, f2fl(Frametime), key);
1414 object_moved(&Objects[view_obj]);
1415 control_pos = Objects[view_obj].pos;
1416 control_orient = Objects[view_obj].orient;
1419 case 1: // Control the current object's location and orientation
1420 if (query_valid_object()) {
1421 vector delta_pos, leader_old_pos;
1422 matrix leader_orient, leader_transpose, tmp;
1425 leader = &Objects[cur_object_index];
1426 leader_old_pos = leader->pos; // save original position
1427 leader_orient = leader->orient; // save original orientation
1428 vm_copy_transpose_matrix(&leader_transpose, &leader_orient);
1430 process_controls(&leader->pos, &leader->orient, f2fl(Frametime), key);
1431 vm_vec_sub(&delta_pos, &leader->pos, &leader_old_pos); // get position change
1432 control_pos = leader->pos;
1433 control_orient = leader->orient;
1435 objp = GET_FIRST(&obj_used_list);
1436 while (objp != END_OF_LIST(&obj_used_list)) {
1437 Assert(objp->type != OBJ_NONE);
1438 if ((objp->flags & OF_MARKED) && (cur_object_index != OBJ_INDEX(objp))) {
1441 vector tmpv1, tmpv2;
1443 // change rotation matrix to rotate in opposite direction. This rotation
1444 // matrix is what the leader ship has rotated by.
1445 vm_copy_transpose_matrix(&rot_trans, &view_physics.last_rotmat);
1447 // get point relative to our point of rotation (make POR the origin). Since
1448 // only the leader has been moved yet, and not the objects, we have to use
1449 // the old leader's position.
1450 vm_vec_sub(&tmpv1, &objp->pos, &leader_old_pos);
1452 // convert point from real-world coordinates to leader's relative coordinate
1453 // system (z=forward vec, y=up vec, x=right vec
1454 vm_vec_rotate(&tmpv2, &tmpv1, &leader_orient);
1456 // now rotate the point by the transpose from above.
1457 vm_vec_rotate(&tmpv1, &tmpv2, &rot_trans);
1459 // convert point back into real-world coordinates
1460 vm_vec_rotate(&tmpv2, &tmpv1, &leader_transpose);
1462 // and move origin back to real-world origin. Object is now at it's correct
1463 // position. Note we used the leader's new position, instead of old position.
1464 vm_vec_add(&objp->pos, &leader->pos, &tmpv2);
1466 // Now fix the object's orientation to what it should be.
1467 vm_matrix_x_matrix(&tmp, &objp->orient, &view_physics.last_rotmat);
1468 vm_orthogonalize_matrix(&tmp); // safety check
1472 vm_vec_add2(&objp->pos, &delta_pos);
1473 vm_matrix_x_matrix(&tmp, &objp->orient, &view_physics.last_rotmat);
1478 objp = GET_NEXT(objp);
1481 objp = GET_FIRST(&obj_used_list);
1482 while (objp != END_OF_LIST(&obj_used_list)) {
1483 if (objp->flags & OF_MARKED)
1486 objp = GET_NEXT(objp);
1498 if (Lookat_mode && query_valid_object()) {
1501 dist = vm_vec_dist(&view_pos, &Objects[cur_object_index].pos);
1502 vm_vec_scale_add(&view_pos, &Objects[cur_object_index].pos, &view_orient.fvec, -dist);
1509 eye_orient = view_orient;
1513 eye_pos = Objects[view_obj].pos;
1514 eye_orient = Objects[view_obj].orient;
1521 maybe_create_new_grid(The_grid, &eye_pos, &eye_orient);
1523 if (Cursor_over != Last_cursor_over) {
1524 Last_cursor_over = Cursor_over;
1528 // redraw screen if controlled object moved or rotated
1529 if (vm_vec_cmp(&control_pos, &Last_control_pos) || vm_matrix_cmp(&control_orient, &Last_control_orient)) {
1531 Last_control_pos = control_pos;
1532 Last_control_orient = control_orient;
1535 // redraw screen if current viewpoint moved or rotated
1536 if (vm_vec_cmp(&eye_pos, &Last_eye_pos) || vm_matrix_cmp(&eye_orient, &Last_eye_orient)) {
1538 Last_eye_pos = eye_pos;
1539 Last_eye_orient = eye_orient;
1543 void hilight_bitmap()
1549 if (Starfield_bitmaps[Cur_bitmap].bitmap_index == -1) // can't draw if no bitmap
1554 g3_rotate_faraway_vertex(&p[i], &Starfield_bitmaps[Cur_bitmap].points[i]);
1555 if (p[i].codes & CC_BEHIND)
1558 g3_project_vertex(&p[i]);
1559 if (p[i].flags & PF_OVERFLOW)
1563 gr_set_color(255, 255, 255);
1564 g3_draw_line(&p[0], &p[1]);
1565 g3_draw_line(&p[1], &p[2]);
1566 g3_draw_line(&p[2], &p[3]);
1567 g3_draw_line(&p[3], &p[0]);
1571 void draw_asteroid_field()
1577 for (i=0; i<1 /*MAX_ASTEROID_FIELDS*/; i++)
1578 if (Asteroid_field.num_initial_asteroids) {
1579 p[0].x = p[2].x = p[4].x = p[6].x = Asteroid_field.min_bound.x;
1580 p[1].x = p[3].x = p[5].x = p[7].x = Asteroid_field.max_bound.x;
1581 p[0].y = p[1].y = p[4].y = p[5].y = Asteroid_field.min_bound.y;
1582 p[2].y = p[3].y = p[6].y = p[7].y = Asteroid_field.max_bound.y;
1583 p[0].z = p[1].z = p[2].z = p[3].z = Asteroid_field.min_bound.z;
1584 p[4].z = p[5].z = p[6].z = p[7].z = Asteroid_field.max_bound.z;
1587 g3_rotate_vertex(&v[j], &p[j]);
1589 g3_draw_line(&v[0], &v[1]);
1590 g3_draw_line(&v[2], &v[3]);
1591 g3_draw_line(&v[4], &v[5]);
1592 g3_draw_line(&v[6], &v[7]);
1593 g3_draw_line(&v[0], &v[2]);
1594 g3_draw_line(&v[1], &v[3]);
1595 g3_draw_line(&v[4], &v[6]);
1596 g3_draw_line(&v[5], &v[7]);
1597 g3_draw_line(&v[0], &v[4]);
1598 g3_draw_line(&v[1], &v[5]);
1599 g3_draw_line(&v[2], &v[6]);
1600 g3_draw_line(&v[3], &v[7]);
1603 // maybe draw inner box
1604 if (Asteroid_field.has_inner_bound) {
1606 gr_set_color(16, 192, 92);
1608 ip[0].x = ip[2].x = ip[4].x = ip[6].x = Asteroid_field.inner_min_bound.x;
1609 ip[1].x = ip[3].x = ip[5].x = ip[7].x = Asteroid_field.inner_max_bound.x;
1610 ip[0].y = ip[1].y = ip[4].y = ip[5].y = Asteroid_field.inner_min_bound.y;
1611 ip[2].y = ip[3].y = ip[6].y = ip[7].y = Asteroid_field.inner_max_bound.y;
1612 ip[0].z = ip[1].z = ip[2].z = ip[3].z = Asteroid_field.inner_min_bound.z;
1613 ip[4].z = ip[5].z = ip[6].z = ip[7].z = Asteroid_field.inner_max_bound.z;
1616 g3_rotate_vertex(&iv[j], &ip[j]);
1618 g3_draw_line(&iv[0], &iv[1]);
1619 g3_draw_line(&iv[2], &iv[3]);
1620 g3_draw_line(&iv[4], &iv[5]);
1621 g3_draw_line(&iv[6], &iv[7]);
1622 g3_draw_line(&iv[0], &iv[2]);
1623 g3_draw_line(&iv[1], &iv[3]);
1624 g3_draw_line(&iv[4], &iv[6]);
1625 g3_draw_line(&iv[5], &iv[7]);
1626 g3_draw_line(&iv[0], &iv[4]);
1627 g3_draw_line(&iv[1], &iv[5]);
1628 g3_draw_line(&iv[2], &iv[6]);
1629 g3_draw_line(&iv[3], &iv[7]);
1635 // See if object "objnum" obstructs the vector from point p0 to point p1.
1636 // If so, return true and stuff hit point in *hitpos.
1637 // If not, return false.
1638 int object_check_collision(object *objp, vector *p0, vector *p1, vector *hitpos)
1642 if ((objp->type == OBJ_NONE) || (objp->type == OBJ_POINT))
1645 if ((objp->type == OBJ_WAYPOINT) && !Show_waypoints)
1648 if ((objp->type == OBJ_START) && !Show_starts)
1651 if (objp->type == OBJ_SHIP) {
1655 if ((Ships[objp->instance].team == TEAM_FRIENDLY) && !Show_friendly)
1658 if ((Ships[objp->instance].team == TEAM_HOSTILE) && !Show_hostile)
1661 if ((Ships[objp->instance].team == TEAM_NEUTRAL) && !Show_neutral)
1665 if (objp->flags & OF_HIDDEN)
1668 if ((Show_ship_models || Show_outlines) && (objp->type == OBJ_SHIP)) {
1669 mc.model_num = Ships[objp->instance].modelnum; // Fill in the model to check
1671 } else if ((Show_ship_models || Show_outlines) && (objp->type == OBJ_START)) {
1672 mc.model_num = Ships[objp->instance].modelnum; // Fill in the model to check
1675 return fvi_ray_sphere(hitpos, p0, p1, &objp->pos, (objp->radius > 0.1f) ? objp->radius : LOLLIPOP_SIZE);
1677 mc.orient = &objp->orient; // The object's orient
1678 mc.pos = &objp->pos; // The object's position
1679 mc.p0 = p0; // Point 1 of ray to check
1680 mc.p1 = p1; // Point 2 of ray to check
1681 mc.flags = MC_CHECK_MODEL | MC_CHECK_RAY; // flags
1683 *hitpos = mc.hit_point_world;
1684 if ( mc.num_hits < 1 ) {
1686 mc.orient = &objp->orient; // The object's orient
1687 mc.pos = &objp->pos; // The object's position
1688 mc.p0 = p0; // Point 1 of ray to check
1689 mc.p1 = p1; // Point 2 of ray to check
1690 mc.flags = MC_CHECK_SHIELD; // flags
1692 *hitpos = mc.hit_point_world;
1698 // Finds the closest object or waypoint under the mouse cursor and returns
1699 // it's index, or -1 if nothing there.
1700 int select_object(int cx, int cy)
1703 double dist, best_dist = 9e99;
1704 vector p0, p1, v, hitpos;
1708 if (Briefing_dialog) {
1709 best = Briefing_dialog->check_mouse_hit(cx, cy);
1716 g3_set_view_matrix(&eye_pos, &eye_orient, 0.5f);*/
1718 // Get 3d vector specified by mouse cursor location.
1719 g3_point_to_vec(&v, cx, cy);
1722 if (!v.x && !v.y && !v.z) // zero vector
1726 vm_vec_scale_add(&p1, &p0, &v, 100.0f);
1728 ptr = GET_FIRST(&obj_used_list);
1729 while (ptr != END_OF_LIST(&obj_used_list))
1731 if (object_check_collision(ptr, &p0, &p1, &hitpos)) {
1732 hitpos.x = ptr->pos.x - view_pos.x;
1733 hitpos.y = ptr->pos.y - view_pos.y;
1734 hitpos.z = ptr->pos.z - view_pos.z;
1735 dist = hitpos.x * hitpos.x + hitpos.y * hitpos.y + hitpos.z * hitpos.z;
1736 if (dist < best_dist) {
1737 best = OBJ_INDEX(ptr);
1742 ptr = GET_NEXT(ptr);
1748 ptr = GET_FIRST(&obj_used_list);
1749 while (ptr != END_OF_LIST(&obj_used_list))
1751 g3_rotate_vertex(&vt, &ptr->pos);
1752 if (!(vt.codes & CC_BEHIND))
1753 if (!(g3_project_vertex(&vt) & PF_OVERFLOW)) {
1754 hitpos.x = vt.sx - cx;
1755 hitpos.y = vt.sy - cy;
1756 dist = hitpos.x * hitpos.x + hitpos.y * hitpos.y;
1757 if ((dist < 8) && (dist < best_dist)) {
1758 best = OBJ_INDEX(ptr);
1763 ptr = GET_NEXT(ptr);
1769 void render_compass(void)
1771 vector v, eye = { 0.0f };
1776 gr_set_clip(gr_screen.max_w - 100, 0, 100, 100);
1777 g3_start_frame(0); // required !!!
1778 vm_vec_scale_add2(&eye, &eye_orient.fvec, -1.5f);
1779 g3_set_view_matrix(&eye, &eye_orient, 1.0f);
1783 if (vm_vec_dotprod(&eye, &v) < 0.0f)
1784 gr_set_color(159, 20, 20);
1786 gr_set_color(255, 32, 32);
1787 draw_compass_arrow(&v);
1791 if (vm_vec_dotprod(&eye, &v) < 0.0f)
1792 gr_set_color(20, 159, 20);
1794 gr_set_color(32, 255, 32);
1795 draw_compass_arrow(&v);
1799 if (vm_vec_dotprod(&eye, &v) < 0.0f)
1800 gr_set_color(20, 20, 159);
1802 gr_set_color(32, 32, 255);
1803 draw_compass_arrow(&v);
1808 void draw_compass_arrow(vector *v0)
1810 vector v1 = { 0.0f };
1813 g3_rotate_vertex(&tv0, v0);
1814 g3_rotate_vertex(&tv1, &v1);
1815 g3_project_vertex(&tv0);
1816 g3_project_vertex(&tv1);
1817 // tv0.sx = (tv0.sx - tv1.sx) * 1 + tv1.sx;
1818 // tv0.sy = (tv0.sy - tv1.sy) * 1 + tv1.sy;
1819 g3_draw_line(&tv0, &tv1);
1823 void inc_mission_time()
1827 thistime = timer_get_fixed_seconds();
1829 Frametime = F1_0 / 30;
1831 Frametime = thistime - lasttime;
1833 if (Frametime > MAX_FRAMETIME)
1834 Frametime = MAX_FRAMETIME;
1836 if (Frametime < MIN_FRAMETIME)
1837 Frametime = MIN_FRAMETIME;
1839 Missiontime += Frametime;
1840 lasttime = thistime;