1 /* render routines that replaces the game rendering by OpenGL rendering
3 * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * 1. Each game's rendering is capured:
23 * a: call to render_capture_start()
24 * b: several calls to render_capture_event() to capture all data
25 * c: call to render_capture_stop()
26 * 2. The captured motion is then interpolated to get smooth motion:
27 * - new_motion for motion of current/recent capture
28 * - old_motion for motion of previous capture
29 * - interpolation for interpolated result
30 * 3. The complete scene is rendered:
31 * - render_all_items() calls the render_item() for each item
32 * - The recent capture (NEW) is rendered
33 * - Interpolation result is taken into account
41 #include "../libsdl/print.h"
42 #include "../libcpu/m68k.h"
43 #include "../libcpu/m68kcpu.h"
44 #include "../libcpu/execute.h"
45 #include "../libopengl/opengl.h"
46 #include "mercenary.h"
48 #define GL3_PROTOTYPES 1
52 //#define DEBUG_VERTEX
55 #define MAX_POLYGON 16 /* number of polygon complexity (vertices) */
56 #define MAX_VERTEX 0x100 /* this is the value range, these are 64 vertices */
57 #define MAX_INTERIOR_VERTEX 0x400 /* do we need that much? */
58 #define MAX_INTERSTARS 80 /* always 80 stars */
59 #define MAX_MOVING_OBJECTS 16 /* maximum number of moving objects (used for interpolation) */
60 #define MAX_EXPLOSION 256 /* how many explosion particles can be stored in one object */
61 #define PLANET_VERTICES 128
62 #define PLANET_STRETCH 1.19 /* stretch horizontally (this compensates NTSC/PAL line ratio of 625/525)*/
63 #define EXPLOSION_VERTICES 16
64 #define EXPLOSION_STRETCH 1.19 /* as above */
65 #define SIGHT_DIST 78.74 /* distanc of sights in inch */
66 #define FIX_OBJECT_SCALE 16
69 * render item definition and structures
73 enum render_item_type {
74 RENDER_ITEM_OBJECT_INFO,
75 RENDER_ITEM_VERTICES_0,
76 RENDER_ITEM_VERTICES_1,
77 RENDER_ITEM_VERTICES_2,
78 RENDER_ITEM_VERTICES_INTERIOR,
81 RENDER_ITEM_OBJECT_POLYGON,
82 RENDER_ITEM_OBJECT_LINE,
83 RENDER_ITEM_BEACON_POINT,
84 RENDER_ITEM_BUILDING_EXTERIOR_POLYGON,
85 RENDER_ITEM_BUILDING_EXTERIOR_LINE,
86 RENDER_ITEM_BUILDING_INTERIOR_1TO4,
87 RENDER_ITEM_BUILDING_INTERIOR_5TO6,
88 RENDER_ITEM_BUILDING_INTERIOR_WALL,
89 RENDER_ITEM_COMET_POLYGON,
90 RENDER_ITEM_ROAD_LINE,
91 RENDER_ITEM_ROAD_POLYGON,
92 RENDER_ITEM_TAG_LINE_OBJECT,
93 RENDER_ITEM_TAG_LINE_OTHER,
94 RENDER_ITEM_TAG_POLYGON_OBJECT,
95 RENDER_ITEM_TAG_POLYGON_OTHER,
98 RENDER_ITEM_INTERSTELLAR_STARS,
99 RENDER_ITEM_INTERSTELLAR_SUN,
100 RENDER_ITEM_ISLAND_POLYGON,
102 RENDER_ITEM_EXPLOSION,
105 struct render_item_info {
108 int32_t east, height, north;
111 struct render_item_vertices {
112 double x[MAX_VERTEX >> 2], y[MAX_VERTEX >> 2], z[MAX_VERTEX >> 2];
115 struct render_item_vertices_interior {
116 uint8_t set[MAX_INTERIOR_VERTEX >> 2];
117 double x[MAX_INTERIOR_VERTEX >> 2], y[4], z[MAX_INTERIOR_VERTEX >> 2];
120 struct render_item_sky {
121 double red, green, blue;
124 struct render_item_ground {
125 double red, green, blue;
128 struct render_item_polygon {
129 double red, green, blue;
131 int vertex[MAX_POLYGON];
134 struct render_item_interior14 {
135 double red, green, blue;
140 struct render_item_interior56 {
141 double red, green, blue;
148 struct render_item_line {
149 double red, green, blue;
153 struct render_item_point {
154 double red, green, blue;
158 struct render_item_planet {
159 double front_red, front_green, front_blue;
160 double back_red, back_green, back_blue;
165 struct render_item_stars {
172 struct render_item_interstars {
173 uint8_t color[MAX_INTERSTARS];
174 int16_t x[MAX_INTERSTARS], y[MAX_INTERSTARS];
178 struct render_item_explosion {
179 double red, green, blue;
180 int32_t x[MAX_EXPLOSION], y[MAX_EXPLOSION], z[MAX_EXPLOSION];
181 double size[MAX_EXPLOSION];
185 typedef struct render_item {
186 struct render_item *next;
187 enum render_item_type type;
189 struct render_item_info info;
190 struct render_item_vertices vertices;
191 struct render_item_vertices_interior vertices_interior;
192 struct render_item_sky sky;
193 struct render_item_ground ground;
194 struct render_item_polygon polygon;
195 struct render_item_line line;
196 struct render_item_point point;
197 struct render_item_interior14 interior14;
198 struct render_item_interior56 interior56;
199 struct render_item_planet planet;
200 struct render_item_stars stars;
201 struct render_item_interstars interstars;
202 struct render_item_explosion explosion;
206 /* information about motion in each game rendering */
207 typedef struct motion {
208 int32_t position_east, position_height, position_north;
209 double orientation_roll, orientation_pitch, orientation_yaw;
210 uint16_t orientation_raw_yaw;
211 int16_t orientation_raw_pitch;
213 double planet_inclination, planet_azimuth;
216 /* information about interpolation between two game renedrings */
217 typedef struct interpolation {
218 double offset_east, offset_height, offset_north;
219 double orientation_roll, orientation_pitch, orientation_yaw;
220 double orientation_raw_yaw, orientation_raw_pitch;
221 double planet_inclination, planet_azimuth;
222 int object_id[MAX_MOVING_OBJECTS];
223 double object_offset_east[MAX_MOVING_OBJECTS], object_offset_height[MAX_MOVING_OBJECTS], object_offset_north[MAX_MOVING_OBJECTS];
225 render_item_t *interior;
226 render_item_t *planets;
229 #define GET_ORIENTATION \
230 double roll = interpolation.orientation_roll; \
231 double pitch = interpolation.orientation_pitch; \
232 double yaw = interpolation.orientation_yaw
234 #define GET_ORIENTATION_FIX \
235 roll = motion_new.orientation_roll; \
236 pitch = motion_new.orientation_pitch; \
237 yaw = motion_new.orientation_yaw
239 /* rendering options */
240 static int extend_roads; /* extend roads in its original width, rather than just using a single point */
241 static int improve_stars; /* stars are rendered spherical */
242 static int fix_sky_rotation; /* sky will rotate correctly by rotating planets/comet by 180 degrees */
243 static double planet_aspect, explosion_aspect; /* aspect ratio */
245 static double debug_opacity;
246 static double frustum_slope_64, frustum_slope_fov;
248 /* states while collecting render items */
249 static motion_t motion_old, motion_new;
250 static int32_t old_height_offset = 0, new_height_offset = 0;
251 static interpolation_t interpolation;
252 static int ground_index, last_ground_index = -1;
253 static int interior_level12 = 0;
254 static int interior_level34 = 0;
255 static int tag_is_object;
256 /* current render item list */
257 static render_item_t *render_list_new = NULL, **render_list_end = &render_list_new;
258 /* previous render item list */
259 static render_item_t *render_list_old = NULL;
260 /* current item to be processed */
261 static render_item_t *render_item;
262 /* current object info */
263 static render_item_t *render_item_object_info;
264 /* current vertices */
265 static render_item_t *render_item_vertices_0, *render_item_vertices_1, *render_item_vertices_2;
266 static render_item_t *render_item_vertices_planets, *render_item_vertices_interior;
272 static void render_item_add(enum render_item_type type)
274 render_item = calloc(1, sizeof(render_item_t));
276 print_error("No memory, must abort!\n");
279 render_item->type = type;
280 *render_list_end = render_item;
281 render_list_end = &render_item->next;
284 static void flush_old_items(void)
286 /* flush old render list */
287 while (render_list_old) {
288 render_item = render_list_old;
289 render_list_old = render_list_old->next;
294 /* rendering starts, initialize variables */
295 void render_capture_start(double _fov, int _extend_roads, int _smooth_planets, int _improve_stars, int _fix_sky_rotation, int _round_planets, int debug)
297 #if defined(DEBUG_COLOR) || defined(DEBUG_VERTEX)
298 printf("start rendering a new frame...\n");
303 /* move new render list to old render list */
304 render_list_old = render_list_new;
305 /* setup new render list */
306 render_list_new = NULL;
307 render_list_end = &render_list_new;
309 /* move new motion to old motion */
310 memcpy(&motion_old, &motion_new, sizeof(motion_old));
312 /* set rendering options */
314 extend_roads = _extend_roads;
315 fix_sky_rotation = _fix_sky_rotation;
316 improve_stars = _improve_stars;
317 planet_aspect = (_round_planets) ? 1.0 : PLANET_STRETCH;
318 explosion_aspect = (_round_planets) ? 1.0 : EXPLOSION_STRETCH;
319 /* set some transpareny, if debugging is enabled */
320 debug_opacity = (debug) ? 0.5 : 1.0;
322 /* calculate slope of 64 degree frustum and current FOV's frustum */
323 frustum_slope_64 = tan(64.0 / 2.0 / 180.0 * M_PI);
324 frustum_slope_fov = tan(fov / 2.0 / 180.0 * M_PI);
327 mercenary_get_location(&motion_new.position_east, &motion_new.position_height, &motion_new.position_north);
328 /* in case of player on the ground, there is no roll, so it will be reset to 0 when screen is cleared */
329 mercenary_get_orientation(&motion_new.orientation_roll, &motion_new.orientation_pitch, &motion_new.orientation_yaw);
330 mercenary_get_orientation_raw(&motion_new.orientation_raw_pitch, &motion_new.orientation_raw_yaw);
331 motion_new.planet_rotation = motion_old.planet_rotation;
332 mercenary_get_orientation_planet(&motion_new.planet_inclination, &motion_new.planet_azimuth, _smooth_planets);
334 render_item_object_info = NULL;
335 render_item_vertices_0 = render_item_vertices_1 = render_item_vertices_2 = NULL;
336 render_item_vertices_planets = NULL;
337 render_item_vertices_interior = NULL;
339 /* detect elevator movement */
340 old_height_offset = new_height_offset;
342 new_height_offset = motion_new.position_height - motion_old.position_height;
344 /* detect switching between space (-1) and over ground (>=0) */
345 last_ground_index = ground_index;
351 void render_capture_stop(void)
353 /* no new list (due to STOP_AT_WAIT_INPUT), use old list, if any */
354 if (!render_list_new) {
355 render_list_new = render_list_old;
356 render_list_end = &render_list_new;
357 render_list_old = NULL;
362 static void gamecolor2gl_index(double *red, double *green, double *blue, uint16_t index)
367 /* use given index from current palette, so we take the palette from M3:DS_0+0x1FA8 */
369 palette = mercenary_palette_render();
370 color = m68k_read_memory_16(palette + index);
372 printf("using color index (%d) from current palette, color is now 0x%04x\n", index, color);
374 if (color >= 0x8000) {
376 fprintf(stderr, "Use of color index from current palette, but index is not defined as being set!\n");
379 *red = (double)((color >> 8) & 0xf) / 15.0;
380 *green = (double)((color >> 4) & 0xf) / 15.0;
381 *blue = (double)(color & 0xf) / 15.0;
384 static void gamecolor2gl(double *red, double *green, double *blue, uint16_t color)
391 printf("color is given as 0x%04x\n", color);
394 /* color conversion: see for example M3: 0x4f830 */
395 if (color < 0x8000) {
396 /* use given color but shift it left by 1 */
399 printf("using given color, color is now 0x%04x\n", color);
401 } else if ((color & 0xff) < 0x80) {
402 gamecolor2gl_index(red, green, blue, color & 0xf);
405 /* use given index from pre-defined palette */
406 index = color & 0x7e;
407 palette = mercenary_palette_predefined();
408 color = m68k_read_memory_16(palette + index);
410 printf("offset (%d) from pre-defined palette (at 0x%x) given, color is now 0x%04x\n", index, palette, color);
412 /* now use that color info parse again (hopefully it does not contain a "pre-defined palette" again and again! */
413 if (nesting++ == 8) {
414 print_info("Color lookup from pre-defined palette is nesting too much, please fix!\n");
419 *red = (double)((color >> 8) & 0xf) / 15.0;
420 *green = (double)((color >> 4) & 0xf) / 15.0;
421 *blue = (double)(color & 0xf) / 15.0;
424 static int32_t wrap_int28(int32_t value)
431 static void store_coord(const char __attribute__((unused)) *what, uint32_t vertex, int32_t x, int32_t y, int32_t z, double scale)
434 print_info("Vertex %d is not a multiple of four!\n", vertex);
437 /* create new vertices item, if there was no vertex before, or if the vertex has different offet */
438 if (vertex < 0x100) {
439 if (!render_item || render_item->type != RENDER_ITEM_VERTICES_0) {
440 render_item_add(RENDER_ITEM_VERTICES_0);
441 /* copy vertices that have been captured already */
442 if (render_item_vertices_0)
443 memcpy(&render_item->u.vertices, &render_item_vertices_0->u.vertices, sizeof(render_item->u.vertices));
444 render_item_vertices_0 = render_item;
447 if (vertex < 0x200) {
448 if (!render_item || render_item->type != RENDER_ITEM_VERTICES_1) {
449 render_item_add(RENDER_ITEM_VERTICES_1);
450 /* copy vertices that have been captured already */
451 if (render_item_vertices_1)
452 memcpy(&render_item->u.vertices, &render_item_vertices_1->u.vertices, sizeof(render_item->u.vertices));
453 render_item_vertices_1 = render_item;
457 if (vertex < 0x300) {
458 if (!render_item || render_item->type != RENDER_ITEM_VERTICES_2) {
459 render_item_add(RENDER_ITEM_VERTICES_2);
460 /* copy vertices that have been captured already */
461 if (render_item_vertices_2)
462 memcpy(&render_item->u.vertices, &render_item_vertices_2->u.vertices, sizeof(render_item->u.vertices));
463 render_item_vertices_2 = render_item;
467 print_info("Vertex %d exceeds maximum vertex number, please fix!\n", vertex);
472 printf("storing %s coordinates: vertex=%d, x=%d, y=%d, z=%d\n", what, vertex, x, y, z);
474 /* use absolute position */
475 render_item->u.vertices.x[vertex] = (double)x * scale + motion_new.position_east;
476 render_item->u.vertices.y[vertex] = (double)y * scale + motion_new.position_height;
477 render_item->u.vertices.z[vertex] = (double)z * scale + motion_new.position_north;
480 static void store_planets_coord(const char __attribute__((unused)) *what, uint32_t vertex, int32_t x, int32_t y, int32_t z)
483 print_info("Vertex %d is not a multiple of four!\n", vertex);
486 /* create new vertices item, if there was no vertex before, or if the vertex has different offet */
487 if (vertex >= MAX_VERTEX) {
488 print_info("Vertex %d exceeds maximum vertex number %d!\n", vertex, MAX_INTERIOR_VERTEX);
491 if (!render_item || render_item->type != RENDER_ITEM_VERTICES_0) {
492 render_item_add(RENDER_ITEM_VERTICES_0);
493 /* copy vertices that have been captured already */
494 if (render_item_vertices_0)
495 memcpy(&render_item->u.vertices, &render_item_vertices_0->u.vertices, sizeof(render_item->u.vertices));
496 render_item_vertices_0 = render_item;
500 printf("storing %s coordinates: vertex=%d, x=%d, y=%d, z=%d\n", what, vertex, x, y, z);
502 render_item->u.vertices.x[vertex] = x;
503 render_item->u.vertices.y[vertex] = y;
504 render_item->u.vertices.z[vertex] = z;
507 static void store_interior_coord(const char __attribute__((unused)) *what, uint32_t vertex, int32_t x, int32_t y1, int32_t y2, int32_t y3, int32_t y4, int32_t z)
510 print_info("Vertex is not a multiple of four!\n");
513 if (vertex >= MAX_INTERIOR_VERTEX) {
514 print_info("Vertex %d exceeds maximum vertex number %d!\n", vertex, MAX_INTERIOR_VERTEX);
517 if (!render_item || render_item->type != RENDER_ITEM_VERTICES_INTERIOR)
518 render_item_add(RENDER_ITEM_VERTICES_INTERIOR);
521 printf("storing %s coordinates: vertex=%d, x=%d, y=%d;%d;%d;%d, z=%d\n", what, vertex, x, y1, y2, y3, y4, z);
523 /* use absolute position */
524 x += motion_new.position_east;
525 y1 += motion_new.position_height;
526 y2 += motion_new.position_height;
527 y3 += motion_new.position_height;
528 y4 += motion_new.position_height;
529 z += motion_new.position_north;
530 render_item->u.vertices_interior.x[vertex] = (double)x;
531 render_item->u.vertices_interior.y[0] = (double)y1;
532 render_item->u.vertices_interior.y[1] = (double)y2;
533 render_item->u.vertices_interior.y[2] = (double)y3;
534 render_item->u.vertices_interior.y[3] = (double)y4;
535 render_item->u.vertices_interior.z[vertex] = (double)z;
536 render_item->u.vertices_interior.set[vertex] = 1;
539 static void rotate_coordinate(double roll, double pitch, double yaw, double *x, double *y, double *z)
541 double out_x, out_y, out_z;
543 /* rotate yaw (German: Gier, turn view to the right) */
545 out_z = (*z) * cos(yaw) - (*x) * sin(yaw);
546 out_x = (*z) * sin(yaw) + (*x) * cos(yaw);
550 /* rotate pitch (German: Nick, turn head down) */
552 out_y = (*y) * cos(pitch) - (*z) * sin(pitch);
553 out_z = (*y) * sin(pitch) + (*z) * cos(pitch);
557 /* rotate roll (tilt head to the right) */
559 out_x = (*x) * cos(roll) - (*y) * sin(roll);
560 out_y = (*x) * sin(roll) + (*y) * cos(roll);
566 /* clear screen color (sky / universe) */
567 static void clear_screen(int index)
570 printf("clear screen:\n");
573 /* allocate render item */
574 render_item_add(RENDER_ITEM_SKY);
577 gamecolor2gl_index(&render_item->u.ground.red, &render_item->u.ground.green, &render_item->u.ground.blue, 8);
579 /* store for later use after planets have been rendered */
580 ground_index = index;
584 static void draw_ground(void)
586 /* no ground in space :) */
587 if (ground_index < 0)
591 printf("add ground plane:\n");
594 /* allocate render item */
595 render_item_add(RENDER_ITEM_GROUND);
598 gamecolor2gl_index(&render_item->u.ground.red, &render_item->u.ground.green, &render_item->u.ground.blue, ground_index);
602 static void info_object(int moving)
605 printf("add object's info:\n");
608 /* allocate render item */
609 render_item_add(RENDER_ITEM_OBJECT_INFO);
612 render_item->u.info.moving = moving;
614 mercenary_get_object_info(&render_item->u.info.id, &render_item->u.info.east, &render_item->u.info.height, &render_item->u.info.north);
617 /* coordinates ready for an object */
618 static void coord_object(void)
622 x = (int16_t)REG_D[3];
623 x += (int32_t)REG_A[1];
624 y = (int16_t)REG_D[4];
625 y += (int32_t)REG_A[2];
626 z = (int16_t)REG_D[5];
627 z += (int32_t)REG_A[3];
628 store_coord("object", REG_A[0], x, y, z, 1.0);
631 /* polygon of object */
632 static void poly_object(int mercenary)
634 uint32_t vertex_address = REG_A[0];
639 printf("add object's polygon:\n");
642 /* allocate render item */
643 render_item_add(RENDER_ITEM_OBJECT_POLYGON);
647 gamecolor2gl(&render_item->u.polygon.red, &render_item->u.polygon.green, &render_item->u.polygon.blue, REG_D[0]);
650 color = m68k_read_memory_8(vertex_address++) << 8;
651 color |= m68k_read_memory_8(vertex_address++);
652 gamecolor2gl(&render_item->u.polygon.red, &render_item->u.polygon.green, &render_item->u.polygon.blue, color);
655 /* the vertex list is zero-terminated */
656 for (i = 0; i < MAX_POLYGON; i++) {
657 vertex = m68k_read_memory_8(vertex_address++);
658 if (vertex == 0 && i)
660 render_item->u.polygon.vertex[i] = vertex;
662 render_item->u.polygon.vertices = i;
666 static void line_object(void)
668 uint32_t vertex_address = REG_A[0];
672 printf("add object's line:\n");
675 /* allocate render item */
676 render_item_add(RENDER_ITEM_OBJECT_LINE);
679 gamecolor2gl(&render_item->u.line.red, &render_item->u.line.green, &render_item->u.line.blue, REG_D[0]);
682 vertex = m68k_read_memory_8(vertex_address++);
683 render_item->u.line.vertex[0] = vertex;
684 vertex = m68k_read_memory_8(vertex_address++);
685 render_item->u.line.vertex[1] = vertex;
688 /* coordinates ready for a beacon */
689 static void coord_beacon(void)
693 /* only 28 bits seem to be a correct signed int value */
694 x = (int32_t)(REG_D[3] << 4) / 16;
695 y = (int32_t)(REG_D[4] << 4) / 16;
696 z = (int32_t)(REG_D[5] << 4) / 16;
697 store_coord("beacon", 0, x, y, z, 1.0);
700 /* point of beacon */
701 static void point_beacon(void)
704 printf("add beacon's point:\n");
707 /* allocate render item */
708 render_item_add(RENDER_ITEM_BEACON_POINT);
711 gamecolor2gl(&render_item->u.point.red, &render_item->u.point.green, &render_item->u.point.blue, REG_D[2]);
714 render_item->u.point.vertex = 0;
717 /* coordinates ready for a building (exterior) */
718 static void coord_building_exterior(void)
722 x = (int32_t)REG_D[3];
723 y = (int32_t)REG_D[4];
724 z = (int32_t)REG_D[5];
725 store_coord("building exterior", REG_A[0], x, y, z, 1.0);
728 /* polygon of building (exterior) */
729 static void poly_building_exterior(void)
732 uint32_t vertex_address = REG_A[0];
737 printf("add building's polygon:\n");
740 /* allocate render item */
741 render_item_add(RENDER_ITEM_BUILDING_EXTERIOR_POLYGON);
744 color = m68k_read_memory_8(vertex_address++) << 8;
745 color |= m68k_read_memory_8(vertex_address++);
746 gamecolor2gl(&render_item->u.polygon.red, &render_item->u.polygon.green, &render_item->u.polygon.blue, color);
748 /* the vertex list is zero-terminated */
749 for (i = 0; i < MAX_POLYGON; i++) {
750 vertex = m68k_read_memory_8(vertex_address++);
751 if (vertex == 0 && i)
753 render_item->u.polygon.vertex[i] = vertex | 0x100;
755 render_item->u.polygon.vertices = i;
758 /* line of building (exterior) */
759 static void line_building_exterior(void)
761 uint32_t vertex_address = REG_A[0];
765 printf("add building's line:\n");
768 /* allocate render item */
769 render_item_add(RENDER_ITEM_BUILDING_EXTERIOR_LINE);
772 gamecolor2gl(&render_item->u.line.red, &render_item->u.line.green, &render_item->u.line.blue, REG_D[0]);
775 vertex = m68k_read_memory_8(vertex_address++);
776 render_item->u.line.vertex[0] = vertex | 0x100;
777 vertex = m68k_read_memory_8(vertex_address++);
778 render_item->u.line.vertex[1] = vertex | 0x100;
781 /* coordinates ready for a building (interior) */
782 static void coord_building_interior(void)
785 int32_t height1, height2, height3, height4;
787 mercenary_coord_building_interior(&east, &height1, &height2, &height3, &height4, &north);
788 store_interior_coord("interior", REG_A[0], east, height1, height2, height3, height4, north);
791 /* polygon of building (interior) */
792 static void poly_building_interior1to4(int level)
799 printf("add roof/floor's polygon at level %d:\n", level);
802 /* allocate render item */
803 render_item_add(RENDER_ITEM_BUILDING_INTERIOR_1TO4);
806 color = m68k_read_memory_8(REG_A[0]) << 8;
807 color |= m68k_read_memory_8(REG_A[0] + 1);
808 gamecolor2gl(&render_item->u.interior14.red, &render_item->u.interior14.green, &render_item->u.interior14.blue, color);
810 /* four vertices, one level */
811 for (i = 0; i < 4; i++) {
812 vertex = REG_A[(2 + i)];
813 render_item->u.interior14.vertex[i] = vertex;
815 render_item->u.interior14.level = level;
818 /* polygon of building (interior) */
819 static void poly_building_interior5to6(int level12, int level34)
824 printf("add polygon above/below window/door at level %d/%d:\n", level12, level34);
827 /* allocate render item */
828 render_item_add(RENDER_ITEM_BUILDING_INTERIOR_5TO6);
831 color = m68k_read_memory_8(REG_A[0]) << 8;
832 color |= m68k_read_memory_8(REG_A[0] + 1);
833 gamecolor2gl(&render_item->u.interior56.red, &render_item->u.interior56.green, &render_item->u.interior56.blue, color);
835 /* two vertices, two levels */
836 render_item->u.interior56.vertex14 = REG_A[2];
837 render_item->u.interior56.vertex23 = REG_A[3];
838 render_item->u.interior56.level12 = level12;
839 render_item->u.interior56.level34 = level34;
842 /* wall part of a building */
843 static void wall_building(void)
846 printf("add wall:\n");
849 /* allocate render item */
850 render_item_add(RENDER_ITEM_BUILDING_INTERIOR_WALL);
853 gamecolor2gl(&render_item->u.interior56.red, &render_item->u.interior56.green, &render_item->u.interior56.blue, REG_D[3]);
855 /* two vertices, two levels */
856 render_item->u.interior56.vertex14 = REG_A[1];
857 render_item->u.interior56.vertex23 = REG_A[2];
858 /* get top level according to bit 12 in D3 */
859 render_item->u.interior56.level12 = (REG_D[3] & (1 << 12)) ? 3 : 2;
860 render_item->u.interior56.level34 = 1;
863 /* coordinates ready for comet tail */
864 static void coord_comet(void)
868 x = (int32_t)REG_D[3];
869 y = (int32_t)REG_D[4];
870 z = (int32_t)REG_D[5];
871 store_planets_coord("comet tail", REG_A[0], x, y, z);
874 /* polygon of comet tail */
875 static void poly_comet(void)
878 uint32_t vertex_address = REG_A[0];
883 printf("add comet's polygon:\n");
886 /* allocate render item */
887 render_item_add(RENDER_ITEM_COMET_POLYGON);
890 color = m68k_read_memory_8(vertex_address++) << 8;
891 color |= m68k_read_memory_8(vertex_address++);
892 gamecolor2gl(&render_item->u.polygon.red, &render_item->u.polygon.green, &render_item->u.polygon.blue, color);
894 /* the vertex list is zero-terminated */
895 for (i = 0; i < MAX_POLYGON; i++) {
896 vertex = m68k_read_memory_8(vertex_address++);
897 if (vertex == 0 && i)
899 render_item->u.polygon.vertex[i] = vertex;
901 render_item->u.polygon.vertices = i;
904 /* coordinates ready for lines of a road / ground surface */
905 static void coord_line_road(void)
910 y = -motion_new.position_height;
912 store_coord("road", REG_A[0], x, y, z, 1.0);
916 static void line_road(void)
921 printf("add road's line:\n");
924 /* allocate render item */
925 render_item_add(RENDER_ITEM_ROAD_LINE);
928 gamecolor2gl_index(&render_item->u.line.red, &render_item->u.line.green, &render_item->u.line.blue, mercenary_street_color_index());
932 render_item->u.line.vertex[0] = vertex;
934 render_item->u.line.vertex[1] = vertex;
937 /* coordinates ready for polygons of a road / ground surface */
938 static void coord_poly_road(void)
942 x = m68k_read_memory_32(320 + REG_A[0]);
944 /* the A2 is already converted to game's coordinate, so we use the memory location DS_0+0x1DBA (m3) instead */
945 y = -motion_new.position_height;
946 z = m68k_read_memory_32(576 + REG_A[0]);
948 store_coord("road/place", REG_A[0], x, y, z, 1.0);
951 /* polygon of road */
952 static void poly_road()
955 uint32_t vertex_address = REG_A[0];
960 printf("add road/place's polygon:\n");
963 /* allocate render item */
964 render_item_add(RENDER_ITEM_ROAD_POLYGON);
967 color = m68k_read_memory_8(vertex_address++) << 8;
968 color |= m68k_read_memory_8(vertex_address++);
969 gamecolor2gl(&render_item->u.polygon.red, &render_item->u.polygon.green, &render_item->u.polygon.blue, color);
971 /* the vertex list is zero-terminated */
972 for (i = 0; i < MAX_POLYGON; i++) {
973 vertex = m68k_read_memory_8(vertex_address++);
974 if (vertex == 0 && i)
976 render_item->u.polygon.vertex[i] = vertex;
978 render_item->u.polygon.vertices = i;
981 /* coordinates ready for tags */
982 static void coord_tags(void)
986 x = (int16_t)REG_D[3];
987 x += (int32_t)REG_A[1];
988 y = (int16_t)REG_D[4];
989 y += (int32_t)REG_A[2];
990 z = (int16_t)REG_D[5];
991 z += (int32_t)REG_A[3];
992 store_coord("tags", REG_A[0], x, y, z, 1.0);
995 /* coordinates ready for large tags */
996 static void coord_tags2(void)
1000 x = (int16_t)REG_D[3];
1001 x += 2 * (int32_t)REG_A[1];
1002 y = (int16_t)REG_D[4];
1003 y += 2 * (int32_t)REG_A[2];
1004 z = (int16_t)REG_D[5];
1005 z += 2 * (int32_t)REG_A[3];
1006 /* note that large tags have double distance, so the resolution is vitrually doubled.
1007 * since we use interpolation and VR, we need to scale the vertex back to normal distance.
1009 store_coord("large tags", REG_A[0], x, y, z, 0.5);
1013 static void line_tags(int last_color)
1015 uint32_t vertex_address = REG_A[0];
1019 printf("add tag's line:\n");
1022 /* allocate render item */
1023 render_item_add((tag_is_object) ? RENDER_ITEM_TAG_LINE_OBJECT : RENDER_ITEM_TAG_LINE_OTHER);
1027 gamecolor2gl(&render_item->u.line.red, &render_item->u.line.green, &render_item->u.line.blue, REG_D[0]);
1029 gamecolor2gl_index(&render_item->u.line.red, &render_item->u.line.green, &render_item->u.line.blue, mercenary_line_tags_index());
1032 vertex = m68k_read_memory_8(vertex_address++);
1033 render_item->u.line.vertex[0] = vertex | 0x200;
1034 vertex = m68k_read_memory_8(vertex_address++);
1035 render_item->u.line.vertex[1] = vertex | 0x200;
1038 /* polygon of tags */
1039 static void poly_tags(int last_color)
1041 uint32_t vertex_address = REG_A[0];
1046 printf("add tag's polygon:\n");
1049 /* allocate render item */
1050 render_item_add((tag_is_object) ? RENDER_ITEM_TAG_POLYGON_OBJECT : RENDER_ITEM_TAG_POLYGON_OTHER);
1054 gamecolor2gl(&render_item->u.polygon.red, &render_item->u.polygon.green, &render_item->u.polygon.blue, REG_D[0]);
1056 gamecolor2gl(&render_item->u.polygon.red, &render_item->u.polygon.green, &render_item->u.polygon.blue, mercenary_poly_tags_color());
1057 /* the vertex list is zero-terminated */
1058 for (i = 0; i < MAX_POLYGON; i++) {
1059 vertex = m68k_read_memory_8(vertex_address++);
1060 if (vertex == 0 && i)
1062 render_item->u.polygon.vertex[i] = vertex | 0x200;
1064 render_item->u.polygon.vertices = i;
1067 /* coordinates ready for planet */
1068 static void coord_planet(void)
1072 x = (int32_t)REG_D[3];
1073 y = (int32_t)REG_D[4];
1074 z = (int32_t)REG_D[5];
1075 store_planets_coord("planet", REG_A[0], x, y, z);
1079 static void draw_planet(int comet)
1082 uint32_t scale_index;
1083 double scale1, scale2;
1087 /* fixing (not noticable) bug in game: don't render comet twice */
1088 if (!comet && vertex == 116)
1092 printf("add planet/comet/sun: vertex=%d color=%08x\n", vertex, color);
1095 /* allocate render item */
1096 render_item_add(RENDER_ITEM_PLANET);
1100 /* make comet black on front side and bright on back */
1101 gamecolor2gl(&render_item->u.planet.front_red, &render_item->u.planet.front_green, &render_item->u.planet.front_blue, 0x000);
1102 gamecolor2gl(&render_item->u.planet.back_red, &render_item->u.planet.back_green, &render_item->u.planet.back_blue, 0x777);
1105 gamecolor2gl(&render_item->u.planet.front_red, &render_item->u.planet.front_green, &render_item->u.planet.front_blue, REG_D[0]);
1106 /* use background color for dark side */
1107 gamecolor2gl(&render_item->u.planet.back_red, &render_item->u.planet.back_green, &render_item->u.planet.back_blue, mercenary_background_index() | 0x8000);
1111 render_item->u.planet.vertex = vertex;
1113 /* I REVERSED THE F*CKING PLANET SIZE, took me half a day!
1114 * the long word 21584(A0) contains two scales
1115 * the lower word contains the scale between 4096 .. 8191, this is 1.0 .. 2.0
1116 * the upper word defines how much this scale is shifted to the left.
1118 scale_index = mercenary_planet_scale_index();
1119 scale1 = (double)(m68k_read_memory_16(scale_index + 2 + vertex) & 0x1fff) / 4096.0;
1120 scale2 = (double)(1 << (uint16_t)m68k_read_memory_16(scale_index + vertex));
1121 render_item->u.planet.size = scale1 * scale2 / 256.0;
1125 static void draw_stars(int16_t v_offset, int tilt, int above_zenith)
1128 printf("add stars\n");
1131 /* allocate render item */
1132 render_item_add(RENDER_ITEM_STARS);
1134 /* vertical offset */
1135 render_item->u.stars.v_offset = v_offset;
1137 /* if we fly over the planet, we have tilt, so we get the calculated tilt value from game */
1138 render_item->u.stars.tilt = tilt;
1140 render_item->u.stars.tilt_value = (int32_t)REG_A[4];
1142 /* stars above zenith */
1143 render_item->u.stars.above_zenith = above_zenith;
1146 /* stars of interstellar flight */
1147 static void draw_stars_interstellar(void)
1153 printf("add interstellar stars\n");
1156 /* allocate render item */
1157 render_item_add(RENDER_ITEM_INTERSTELLAR_STARS);
1160 count = REG_D[5] + 1;
1161 if (count > MAX_INTERSTARS) {
1162 print_info("Expecting maximum of %d stars here, plese fix!\n", MAX_INTERSTARS);
1165 for (i = 0; i < count; i++) {
1168 render_item->u.interstars.color[i] = (m68k_read_memory_16(table) >> 5) & 0xf;
1170 render_item->u.interstars.x[i] = m68k_read_memory_16(table);
1172 render_item->u.interstars.y[i] = m68k_read_memory_16(table);
1175 render_item->u.interstars.count = count;
1178 /* sun of interstellar flight (center dot) */
1179 static void draw_sun_interstellar(void)
1182 printf("add interstellar sun\n");
1185 /* allocate render item */
1186 render_item_add(RENDER_ITEM_INTERSTELLAR_SUN);
1189 /* coordinates ready for polygons of islands */
1190 static void coord_islands(void)
1194 x = ((int16_t)m68k_read_memory_16(REG_A[4] - 2) * 65536);
1195 x += (int32_t)REG_A[1];
1196 /* the A2 is already converted to game's coordinate, so we use the memory location DS_0+0x1DBA instead */
1197 y = -motion_new.position_height;
1198 z = ((int16_t)m68k_read_memory_16(REG_A[4]) * 65536);
1199 z += (int32_t)REG_A[3];
1200 store_coord("island", REG_A[0], x, y, z, 1.0);
1203 /* polygon of island */
1204 static void poly_island()
1207 uint32_t vertex_address = REG_A[0];
1212 printf("add island:\n");
1215 /* allocate render item */
1216 render_item_add(RENDER_ITEM_ISLAND_POLYGON);
1219 color = m68k_read_memory_8(vertex_address++) << 8;
1220 color |= m68k_read_memory_8(vertex_address++);
1221 gamecolor2gl(&render_item->u.polygon.red, &render_item->u.polygon.green, &render_item->u.polygon.blue, color);
1223 /* the vertex list is zero-terminated */
1225 while (i < MAX_POLYGON) {
1226 vertex = m68k_read_memory_8(vertex_address++);
1227 if (vertex == 0 && i)
1229 /* skip mysterious points when rendering island */
1232 render_item->u.polygon.vertex[i] = vertex;
1235 render_item->u.polygon.vertices = i;
1239 static void draw_sights(void)
1242 printf("add sights:\n");
1245 /* allocate render item */
1246 render_item_add(RENDER_ITEM_SIGHTS);
1249 static int32_t coord_explosion_x, coord_explosion_y, coord_explosion_z;
1251 static void coord_explosion(void)
1253 coord_explosion_x = REG_D[3];
1254 coord_explosion_y = REG_D[4];
1255 coord_explosion_z = REG_D[5];
1258 static void draw_explosion(void)
1261 double scale1, scale2;
1264 printf("add explosion:\n");
1267 /* allocate render item */
1268 if (!render_item || render_item->type != RENDER_ITEM_EXPLOSION) {
1269 render_item_add(RENDER_ITEM_EXPLOSION);
1270 /* get color from render palette */
1271 color = (m68k_read_memory_16(mercenary_palette_view() + 13*2) & 0xfff) >> 1;
1272 gamecolor2gl(&render_item->u.explosion.red, &render_item->u.explosion.green, &render_item->u.explosion.blue, color);
1273 render_item->u.explosion.count = 0;
1275 if (render_item->u.explosion.count == MAX_EXPLOSION)
1278 /* the D1 register contains two scales
1279 * the lower word contains the scale between 4096 .. 8191, this is 1.0 .. 2.0
1280 * the upper word defines how much this scale is shifted to the left.
1282 if ((int16_t)(REG_D[1] >> 16) < 0) {
1283 scale1 = (double)(REG_D[1] & 0x1fff) / 4096.0;
1284 scale2 = (double)(256 >> -(int16_t)(REG_D[1] >> 16)) / 256.0;
1286 if ((int16_t)(REG_D[1] >> 16) <= 10) {
1287 scale1 = (double)(REG_D[1] & 0x1fff) / 4096.0;
1288 scale2 = (double)(1 << (int16_t)(REG_D[1] >> 16));
1293 render_item->u.explosion.size[render_item->u.explosion.count] = scale1 * scale2;
1294 render_item->u.explosion.x[render_item->u.explosion.count] = coord_explosion_x;
1295 render_item->u.explosion.y[render_item->u.explosion.count] = coord_explosion_y;
1296 render_item->u.explosion.z[render_item->u.explosion.count] = coord_explosion_z;
1297 render_item->u.explosion.count++;
1300 /* stop event from CPU received */
1301 void render_capture_event(int event)
1304 case STOP_AT_CLEAR_SCREEN1:
1305 clear_screen(16); /* color 16 is raster split */
1306 /* in case of screen clearing on the ground, there is no roll */
1307 motion_new.orientation_roll = 0;
1309 case STOP_AT_CLEAR_SCREEN2:
1312 case STOP_AT_CLEAR_SCREEN3:
1313 clear_screen(-1); /* no ground (in universe) */
1315 case STOP_AT_DRAW_GROUND:
1318 case STOP_AT_INFO_OBJECT_MOVING:
1321 case STOP_AT_INFO_OBJECT_FIX:
1324 case STOP_AT_TAG_IS_OBJECT_1:
1327 case STOP_AT_TAG_IS_OBJECT_0:
1330 case STOP_AT_COORD_OBJECT:
1333 case STOP_AT_POLY_OBJECT_M3:
1336 case STOP_AT_POLY_OBJECT_M2:
1339 case STOP_AT_LINE_OBJECT:
1342 case STOP_AT_COORD_BEACON:
1345 case STOP_AT_POINT_BEACON:
1347 /* note: we may not call the point-renderer, because projected coordinates are invalid */
1348 mercenary_patch_render();
1350 case STOP_AT_COORD_BUILDING_EXTERIOR:
1351 coord_building_exterior();
1353 case STOP_AT_POLY_BUILDING_EXTERIOR:
1354 poly_building_exterior();
1356 case STOP_AT_LINE_BUILDING_EXTERIOR:
1357 line_building_exterior();
1359 case STOP_AT_COORD_BUILDING_INTERIOR:
1360 coord_building_interior();
1362 case STOP_AT_POLY_BUILDING_INTERIOR1:
1364 interior_level12 = 1;
1365 interior_level34 = 1;
1367 case STOP_AT_POLY_BUILDING_INTERIOR2:
1369 interior_level12 = 2;
1370 interior_level34 = 2;
1372 case STOP_AT_POLY_BUILDING_INTERIOR3:
1373 /* door/window top */
1374 interior_level12 = 3;
1375 interior_level34 = 3;
1377 case STOP_AT_POLY_BUILDING_INTERIOR4:
1379 interior_level12 = 4;
1380 interior_level34 = 4;
1382 case STOP_AT_POLY_BUILDING_INTERIOR5:
1383 /* door/window top */
1384 interior_level12 = 2;
1385 interior_level34 = 3;
1387 case STOP_AT_POLY_BUILDING_INTERIOR6:
1389 interior_level12 = 1;
1390 interior_level34 = 4;
1392 case STOP_AT_POLY_BUILDING_INTERIOR1to4:
1393 /* before we come here, we must already passed the break points above, so we know the level to be rendered */
1394 if (interior_level12 == 0) {
1395 print_info("Interior level is not set, please fix!\n");
1398 poly_building_interior1to4(interior_level12);
1399 interior_level12 = 0;
1401 case STOP_AT_POLY_BUILDING_INTERIOR5to6:
1402 /* before we come here, we must already passed the break points above, so we know the level to be rendered */
1403 if (interior_level12 == 0) {
1404 print_info("Interior level is not set, please fix!\n");
1407 poly_building_interior5to6(interior_level12, interior_level34);
1408 interior_level12 = 0;
1410 case STOP_AT_WALL_BUILDING:
1413 case STOP_AT_COORD_COMET:
1416 case STOP_AT_MATRIX_COMET:
1417 case STOP_AT_MATRIX_PLANET:
1418 /* track the rotation matrix
1419 * if we have 0x42c44 matrix, we must add extra rotation to planet.
1420 * the rotation will change the view from the planet's surface */
1421 if (REG_A[4] == 0x42c44) /* same with M2 and M3 */
1422 motion_new.planet_rotation = 1;
1424 motion_new.planet_rotation = 0;
1426 case STOP_AT_POLY_COMET:
1429 case STOP_AT_COORD_LINE_ROADS:
1432 case STOP_AT_LINE_ROADS:
1435 case STOP_AT_COORD_POLY_ROADS:
1438 case STOP_AT_LINE_ROADS_CENTER:
1439 /* we don't need to render center lines of roads, because there are polygons already
1440 * it does not make sense, since OpenGL has much higher resolution.
1443 case STOP_AT_POLY_ROADS:
1446 case STOP_AT_COORD_TAGS:
1449 case STOP_AT_COORD_TAGS2:
1452 case STOP_AT_LINE_TAGS1:
1455 case STOP_AT_LINE_TAGS2:
1458 case STOP_AT_POLY_TAGS1:
1461 case STOP_AT_POLY_TAGS2:
1464 case STOP_AT_COORD_PLANET:
1467 case STOP_AT_DRAW_PLANET:
1470 case STOP_AT_DRAW_COMET:
1473 case STOP_AT_DRAW_STARS_SPACE:
1474 /* render stars with vertical offset 0x1c0, no tilt, not above zenith */
1475 draw_stars(0x1c0, 0, 0);
1477 case STOP_AT_DRAW_STARS_GROUND:
1478 /* render stars with vertical offset 0x128, no tilt, not above zenith */
1479 draw_stars(0x128, 0, 0); /* yet it's hex 128! */
1481 case STOP_AT_DRAW_STARS_FLYING:
1482 /* render stars with vertical offset 0x128, with tilt, not above zenith */
1483 draw_stars(0x128, 1, 0); /* yet it's hex 128! */
1485 case STOP_AT_DRAW_STARS_FLYING2:
1486 /* render stars with vertical offset 0x128, with tilt, and above zenith */
1487 draw_stars(0x128, 1, 1); /* yet it's hex 128! */
1489 case STOP_AT_DRAW_STARS_INTERSTELLAR:
1490 draw_stars_interstellar();
1492 case STOP_AT_DRAW_SUN_INTERSTELLAR:
1493 draw_sun_interstellar();
1495 case STOP_AT_COORD_ISLANDS:
1498 case STOP_AT_POLY_ISLANDS:
1501 case STOP_AT_LINE_ISLANDS:
1502 /* this is not used, as i had noticed so far */
1503 puts("line island");
1505 case STOP_AT_DRAW_SIGHTS:
1508 case STOP_AT_POLY_UKN2:
1511 case STOP_AT_COORD_EXPLOSION:
1514 case STOP_AT_DRAW_EXPLOSION:
1517 case STOP_AT_PATCH_RENDER:
1518 mercenary_patch_render();
1527 static int use_coord(const char __attribute__((unused)) *what, uint32_t vertex, double *x, double *y, double *z, int fix)
1529 render_item_t *ri = NULL;
1532 print_info("Vertex %d is not a multiple of four!\n", vertex);
1535 if (vertex < 0x100) {
1536 if (!render_item_vertices_0) {
1537 print_info("Vertices item for vertex %d not yet set!\n", vertex);
1540 ri = render_item_vertices_0;
1542 if (vertex < 0x200) {
1543 if (!render_item_vertices_1) {
1544 print_info("Vertices item for vertex %d not yet set!\n", vertex);
1547 ri = render_item_vertices_1;
1550 if (vertex < 0x300) {
1551 if (!render_item_vertices_2) {
1552 print_info("Vertices item for vertex %d not yet set!\n", vertex);
1555 ri = render_item_vertices_2;
1558 print_info("Vertex %d exceeds maximum vertex number %d!\n", vertex, MAX_VERTEX);
1562 /* translate to original position */
1563 *x = ri->u.vertices.x[vertex] - motion_new.position_east;
1564 *y = ri->u.vertices.y[vertex] - motion_new.position_height;
1565 *z = ri->u.vertices.z[vertex] - motion_new.position_north;
1567 /* translate to floating (interpolated) position offset */
1568 *x -= interpolation.offset_east;
1569 *y -= interpolation.offset_height;
1570 *z -= interpolation.offset_north;
1573 printf("using %s coordinates: vertex=%d, x=%.0f, y=%.0f, z=%.0f\n", what, vertex, *x, *y, *z);
1579 static int use_planet_coord(const char __attribute__((unused)) *what, uint32_t vertex, double *x, double *y, double *z)
1581 render_item_t *ri = NULL;
1584 print_info("Vertex %d is not a multiple of four!\n", vertex);
1587 if (vertex >= MAX_VERTEX) {
1588 print_info("Vertex %d exceeds maximum vertex number %d!\n", vertex, MAX_VERTEX);
1591 if (interpolation.planets)
1592 ri = interpolation.planets;
1594 ri = render_item_vertices_0;
1596 print_info("Vertices item for planets verticies not yet set!\n");
1600 *x = ri->u.vertices.x[vertex];
1601 *y = ri->u.vertices.y[vertex];
1602 *z = ri->u.vertices.z[vertex];
1604 printf("using %s coordinates: vertex=%d, x=%.0f, y=%.0f, z=%.0f\n", what, vertex, *x, *y, *z);
1610 static int use_interior_coord(const char __attribute__((unused)) *what, uint32_t vertex, int level, double *x, double *y, double *z)
1613 print_info("Vertex is not a multiple of four!\n");
1616 if (vertex >= MAX_INTERIOR_VERTEX) {
1617 print_info("Vertex %d exceeds maximum vertex number %d!\n", vertex, MAX_VERTEX);
1620 if (level < 1 || level > 4) {
1621 print_info("Level %d is out of range (1..4)!\n", level);
1624 if (!render_item_vertices_interior) {
1625 print_info("Vertices item for interior verticies not yet set!\n");
1629 *x = render_item_vertices_interior->u.vertices_interior.x[vertex];
1630 *y = render_item_vertices_interior->u.vertices_interior.y[level - 1];
1631 *z = render_item_vertices_interior->u.vertices_interior.z[vertex];
1632 /* translate to position back to original */
1633 *x -= motion_new.position_east;
1634 *y -= motion_new.position_height;
1635 *z -= motion_new.position_north;
1636 /* translate to floating (interpolated) position offset */
1637 *x -= interpolation.offset_east;
1638 *y -= interpolation.offset_height;
1639 *z -= interpolation.offset_north;
1642 printf("using %s coordinates: vertex=%d, x=%.0f, y=%.0f, z=%.0f\n", what, vertex, *x, *y, *z);
1648 /* renders one item from render list */
1649 void render_one_item(render_item_t *render_item, int vr)
1651 switch (render_item->type) {
1652 case RENDER_ITEM_OBJECT_INFO:
1655 printf("RENDER_ITEM_OBJECT_INFO object-id=%d\n", render_item->u.info.id);
1657 render_item_object_info = render_item;
1660 case RENDER_ITEM_VERTICES_0:
1663 printf("RENDER_ITEM_VERTICES_0\n");
1665 render_item_vertices_0 = render_item;
1668 case RENDER_ITEM_VERTICES_1:
1671 printf("RENDER_ITEM_VERTICES_1\n");
1673 render_item_vertices_1 = render_item;
1676 case RENDER_ITEM_VERTICES_2:
1679 printf("RENDER_ITEM_VERTICES_2\n");
1681 render_item_vertices_2 = render_item;
1684 case RENDER_ITEM_VERTICES_INTERIOR:
1687 printf("RENDER_ITEM_VERTICES_INTERIOR\n");
1689 if (interpolation.interior)
1690 render_item_vertices_interior = interpolation.interior;
1692 render_item_vertices_interior = render_item;
1695 case RENDER_ITEM_SKY:
1697 double x[4], y[4], z[4];
1700 printf("RENDER_ITEM_SKY\n");
1703 opengl_render_color(render_item->u.sky.red, render_item->u.sky.green, render_item->u.sky.blue, 1.0);
1704 /* create box to fill view */
1705 x[0] = x[1] = y[1] = y[2] = -1000;
1706 x[2] = x[3] = y[0] = y[3] = 1000;
1707 z[0] = z[1] = z[2] = z[3] = 1000;
1708 opengl_render_polygon_and_line(x, y, z, 4);
1709 z[0] = z[1] = z[2] = z[3] = -1000;
1710 opengl_render_polygon_and_line(x, y, z, 4);
1711 x[0] = x[1] = z[1] = z[2] = -1000;
1712 x[2] = x[3] = z[0] = z[3] = 1000;
1713 y[0] = y[1] = y[2] = y[3] = 1000;
1714 opengl_render_polygon_and_line(x, y, z, 4);
1715 y[0] = y[1] = y[2] = y[3] = -1000;
1716 opengl_render_polygon_and_line(x, y, z, 4);
1717 y[0] = y[1] = z[1] = z[2] = -1000;
1718 y[2] = y[3] = z[0] = z[3] = 1000;
1719 x[0] = x[1] = x[2] = x[3] = 1000;
1720 opengl_render_polygon_and_line(x, y, z, 4);
1721 x[0] = x[1] = x[2] = x[3] = -1000;
1722 opengl_render_polygon_and_line(x, y, z, 4);
1725 case RENDER_ITEM_GROUND:
1728 double x[4], y[4], z[4];
1732 printf("RENDER_ITEM_GROUND\n");
1735 opengl_render_color(render_item->u.ground.red, render_item->u.ground.green, render_item->u.ground.blue, 1.0);
1736 yaw = 0.0; /* no need to rotate x-z plane, we don't want look at a corner of the 'ground-square' */
1737 /* create huge square */
1738 x[0] = x[1] = z[1] = z[2] = -1000000000;
1739 x[2] = x[3] = z[0] = z[3] = 1000000000;
1740 y[0] = y[1] = y[2] = y[3] = -1000;
1742 for (i = 0; i < 4; i++)
1743 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
1745 opengl_render_polygon(x, y, z, 4, 0); /* no culling, because gound is always visible! */
1748 case RENDER_ITEM_OBJECT_POLYGON:
1749 case RENDER_ITEM_TAG_POLYGON_OBJECT:
1750 case RENDER_ITEM_TAG_POLYGON_OTHER:
1751 case RENDER_ITEM_ISLAND_POLYGON:
1755 double x[MAX_POLYGON], y[MAX_POLYGON], z[MAX_POLYGON];
1760 if (render_item->type == RENDER_ITEM_OBJECT_POLYGON)
1761 printf("RENDER_ITEM_OBJECT_POLYGON\n");
1762 if (render_item->type == RENDER_ITEM_TAG_POLYGON_OBJECT)
1763 printf("RENDER_ITEM_TAG_POLYGON_OBJECT\n");
1764 if (render_item->type == RENDER_ITEM_TAG_POLYGON_OTHER)
1765 printf("RENDER_ITEM_TAG_POLYGON_OTHER\n");
1766 if (render_item->type == RENDER_ITEM_ISLAND_POLYGON)
1767 printf("RENDER_ITEM_ISLAND_POLYGON\n");
1770 /* special case where we don't want to interpolate motion (taxi/busses/intercity) */
1771 if ((render_item->type == RENDER_ITEM_OBJECT_POLYGON || render_item->type == RENDER_ITEM_TAG_POLYGON_OBJECT) && render_item_object_info && !render_item_object_info->u.info.moving) {
1772 // GET_ORIENTATION_FIX;
1777 opengl_render_color(render_item->u.polygon.red, render_item->u.polygon.green, render_item->u.polygon.blue, debug_opacity);
1778 /* get and rotate vertex */
1779 for (i = 0; i < render_item->u.polygon.vertices; i++) {
1781 rc = use_coord("object", render_item->u.polygon.vertex[i], &x[i], &y[i], &z[i], fix);
1784 /* fixed objects are pre-scaled by 16, so we correct this */
1786 x[i] /= FIX_OBJECT_SCALE;
1787 y[i] /= FIX_OBJECT_SCALE;
1788 z[i] /= FIX_OBJECT_SCALE;
1790 /* interpolate motion, if object is moving */
1791 if ((render_item->type == RENDER_ITEM_OBJECT_POLYGON || render_item->type == RENDER_ITEM_TAG_POLYGON_OBJECT) && render_item_object_info && render_item_object_info->u.info.moving) {
1792 for (o = 0; o < interpolation.object_count; o++) {
1793 if (interpolation.object_id[o] == render_item_object_info->u.info.id)
1796 if (o < interpolation.object_count) {
1797 x[i] += interpolation.object_offset_east[o];
1798 y[i] += interpolation.object_offset_height[o];
1799 z[i] += interpolation.object_offset_north[o];
1803 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
1806 opengl_render_polygon(x, y, z, render_item->u.polygon.vertices, 1); /* back face culling */
1809 case RENDER_ITEM_OBJECT_LINE:
1810 case RENDER_ITEM_TAG_LINE_OBJECT:
1811 case RENDER_ITEM_TAG_LINE_OTHER:
1815 double x[2], y[2], z[2];
1820 if (render_item->type == RENDER_ITEM_OBJECT_LINE)
1821 printf("RENDER_ITEM_OBJECT_LINE\n");
1822 if (render_item->type == RENDER_ITEM_TAG_LINE_OBJECT)
1823 printf("RENDER_ITEM_TAG_LINE_OBJECT\n");
1824 if (render_item->type == RENDER_ITEM_TAG_LINE_OTHER)
1825 printf("RENDER_ITEM_TAG_LINE_OTHER\n");
1828 /* special case where we don't want to interpolate motion (taxi/busses/intercity) */
1829 if ((render_item->type == RENDER_ITEM_OBJECT_LINE || render_item->type == RENDER_ITEM_TAG_LINE_OBJECT) && render_item_object_info && !render_item_object_info->u.info.moving) {
1830 // GET_ORIENTATION_FIX;
1835 opengl_render_color(render_item->u.line.red, render_item->u.line.green, render_item->u.line.blue, debug_opacity);
1836 /* get and rotate vertex */
1837 for (i = 0; i < 2; i++) {
1839 rc = use_coord("object", render_item->u.line.vertex[i], &x[i], &y[i], &z[i], fix);
1842 /* fixed objects are pre-scaled by 16, so we correct this */
1844 x[i] /= FIX_OBJECT_SCALE;
1845 y[i] /= FIX_OBJECT_SCALE;
1846 z[i] /= FIX_OBJECT_SCALE;
1848 /* interpolate motion, if object is moving */
1849 if ((render_item->type == RENDER_ITEM_OBJECT_LINE || render_item->type == RENDER_ITEM_TAG_LINE_OBJECT) && render_item_object_info && render_item_object_info->u.info.moving) {
1850 for (o = 0; o < interpolation.object_count; o++) {
1851 if (interpolation.object_id[o] == render_item_object_info->u.info.id)
1854 if (o < interpolation.object_count) {
1855 x[i] += interpolation.object_offset_east[o];
1856 y[i] += interpolation.object_offset_height[o];
1857 z[i] += interpolation.object_offset_north[o];
1861 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
1864 opengl_render_line(x[0], y[0], z[0], x[1], y[1], z[1], 0.0);
1867 case RENDER_ITEM_BEACON_POINT:
1874 printf("RENDER_ITEM_BEACON_POINT\n");
1877 opengl_render_color(render_item->u.point.red, render_item->u.point.green, render_item->u.point.blue, debug_opacity);
1879 rc = use_coord("beacon", render_item->u.point.vertex, &x, &y, &z, 0);
1883 rotate_coordinate(roll, pitch, yaw, &x, &y, &z);
1885 opengl_render_point(x, y, z, 0.0);
1888 case RENDER_ITEM_BUILDING_EXTERIOR_POLYGON:
1891 double x[MAX_POLYGON], y[MAX_POLYGON], z[MAX_POLYGON];
1896 printf("RENDER_ITEM_BUILDING_EXTERIOR_POLYGON\n");
1899 opengl_render_color(render_item->u.polygon.red, render_item->u.polygon.green, render_item->u.polygon.blue, debug_opacity);
1900 /* get and rotate vertex */
1901 for (i = 0; i < render_item->u.polygon.vertices; i++) {
1903 rc = use_coord("building exterior", render_item->u.polygon.vertex[i], &x[i], &y[i], &z[i], 0);
1907 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
1910 opengl_render_polygon(x, y, z, render_item->u.polygon.vertices, 1); /* back face culling */
1913 case RENDER_ITEM_BUILDING_EXTERIOR_LINE:
1916 double x[2], y[2], z[2];
1921 printf("RENDER_ITEM_BUILDING_EXTERIOR_LINE\n");
1924 opengl_render_color(render_item->u.line.red, render_item->u.line.green, render_item->u.line.blue, debug_opacity);
1925 /* get and rotate vertex */
1926 for (i = 0; i < 2; i++) {
1928 rc = use_coord("building exterior", render_item->u.line.vertex[i], &x[i], &y[i], &z[i], 0);
1932 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
1935 opengl_render_line(x[0], y[0], z[0], x[1], y[1], z[1], 0.0);
1938 case RENDER_ITEM_BUILDING_INTERIOR_1TO4:
1941 double x[4], y[4], z[4];
1946 printf("RENDER_ITEM_BUILDING_INTERIOR_1TO4\n");
1949 opengl_render_color(render_item->u.interior14.red, render_item->u.interior14.green, render_item->u.interior14.blue, debug_opacity);
1950 /* get and rotate vertex */
1951 for (i = 0; i < 4; i++) {
1953 rc = use_interior_coord("building exterior", render_item->u.interior14.vertex[i], render_item->u.interior14.level, &x[i], &y[i], &z[i]);
1957 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
1960 opengl_render_polygon(x, y, z, 4, 1); /* back face culling */
1963 case RENDER_ITEM_BUILDING_INTERIOR_5TO6:
1966 double x[4], y[4], z[4];
1972 printf("RENDER_ITEM_BUILDING_INTERIOR_5TO6\n");
1975 opengl_render_color(render_item->u.interior56.red, render_item->u.interior56.green, render_item->u.interior56.blue, debug_opacity);
1976 /* get and rotate vertex */
1977 for (i = 0; i < 4; i++) {
1979 vertex = (i == 0 || i == 3) ? render_item->u.interior56.vertex14 : render_item->u.interior56.vertex23;
1980 level = (i == 0 || i == 1) ? render_item->u.interior56.level12 : render_item->u.interior56.level34;
1981 rc = use_interior_coord("building interior", vertex, level, &x[i], &y[i], &z[i]);
1985 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
1988 opengl_render_polygon(x, y, z, 4, 1); /* back face culling */
1991 case RENDER_ITEM_BUILDING_INTERIOR_WALL:
1994 double x[4], y[4], z[4];
2000 printf("RENDER_ITEM_BUILDING_INTERIOR_WALL\n");
2003 opengl_render_color(render_item->u.interior56.red, render_item->u.interior56.green, render_item->u.interior56.blue, debug_opacity);
2004 /* chedck if wall is a rectangle or a line */
2005 if (render_item->u.interior56.vertex14 != render_item->u.interior56.vertex23) {
2006 /* get and rotate vertex */
2007 for (i = 0; i < 4; i++) {
2009 vertex = (i == 0 || i == 3) ? render_item->u.interior56.vertex14 : render_item->u.interior56.vertex23;
2010 level = (i == 0 || i == 1) ? render_item->u.interior56.level12 : render_item->u.interior56.level34;
2011 rc = use_interior_coord("building interior", vertex, level, &x[i], &y[i], &z[i]);
2015 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
2018 opengl_render_polygon_and_line(x, y, z, 4); /* no culling, because walls are always visible! */
2020 /* get and rotate vertex */
2021 for (i = 0; i < 2; i++) {
2023 vertex = render_item->u.interior56.vertex14;
2024 level = (i == 0) ? render_item->u.interior56.level12 : render_item->u.interior56.level34;
2025 rc = use_interior_coord("building interior", vertex, level, &x[i], &y[i], &z[i]);
2029 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
2032 opengl_render_line(x[0], y[0], z[0], x[1], y[1], z[1], 0.0);
2036 case RENDER_ITEM_COMET_POLYGON:
2039 double inclination = interpolation.planet_inclination, azimuth = interpolation.planet_azimuth;
2040 double x[MAX_POLYGON], y[MAX_POLYGON], z[MAX_POLYGON];
2043 double rotate_sky = 0.0;
2046 printf("RENDER_ITEM_COMET_POLYGON\n");
2049 opengl_render_color(render_item->u.polygon.red, render_item->u.polygon.green, render_item->u.polygon.blue, debug_opacity);
2050 /* get and rotate vertex */
2051 for (i = 0; i < render_item->u.polygon.vertices; i++) {
2053 rc = use_planet_coord("comet tail", render_item->u.polygon.vertex[i], &x[i], &y[i], &z[i]);
2057 if (motion_new.planet_rotation) {
2058 if (fix_sky_rotation)
2060 rotate_coordinate(0.0, inclination, azimuth, &x[i], &y[i], &z[i]);
2062 rotate_coordinate(roll, pitch, yaw + rotate_sky, &x[i], &y[i], &z[i]);
2065 opengl_render_polygon_and_line(x, y, z, render_item->u.polygon.vertices); /* no culling, because we render only two (out of four) planes! */
2068 case RENDER_ITEM_ROAD_LINE:
2071 double x[2], y[2], z[2];
2076 printf("RENDER_ITEM_ROAD_LINE\n");
2079 opengl_render_color(render_item->u.line.red, render_item->u.line.green, render_item->u.line.blue, debug_opacity);
2080 /* get and rotate vertex */
2081 for (i = 0; i < 2; i++) {
2083 rc = use_coord("road", render_item->u.line.vertex[i], &x[i], &y[i], &z[i], 0);
2087 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
2090 opengl_render_line(x[0], y[0], z[0], x[1], y[1], z[1], 0.0);
2093 case RENDER_ITEM_ROAD_POLYGON:
2096 double x[MAX_POLYGON], y[MAX_POLYGON], z[MAX_POLYGON];
2098 uint32_t vertex, vertex_prev, vertex_next;
2099 double x_current, y_current, z_current;
2100 double x_prev, y_prev, z_prev;
2101 double x_next, y_next, z_next;
2106 printf("RENDER_ITEM_ROAD_POLYGON\n");
2109 opengl_render_color(render_item->u.polygon.red, render_item->u.polygon.green, render_item->u.polygon.blue, debug_opacity);
2110 /* get and rotate vertex */
2112 vertices_num = render_item->u.polygon.vertices;
2113 for (v = 0; v < vertices_num; v++) {
2115 vertex = render_item->u.polygon.vertex[v];
2116 rc = use_coord("road/place", vertex, &x_current, &y_current, &z_current, 0);
2119 /* check for road extension, so we extend the road to the given end point */
2120 if (extend_roads && vertex >= 0xf0) {
2121 /* previous vertex */
2122 vertex_prev = render_item->u.polygon.vertex[(v + vertices_num - 1) % vertices_num];
2123 rc = use_coord("road/place", vertex_prev, &x_prev, &y_prev, &z_prev, 0);
2127 vertex_next = render_item->u.polygon.vertex[(v + 1) % vertices_num];
2128 rc = use_coord("road/place", vertex_next, &x_next, &y_next, &z_next, 0);
2131 /* extend vertices to end point position
2132 * change x or z coordinate, whatever is greater
2134 if (fabs(x_prev - x_current) > fabs(z_prev - z_current))
2135 x_prev = x_next = x_current;
2137 z_prev = z_next = z_current;
2138 /* store vertices */
2143 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
2144 if (i++ == MAX_POLYGON)
2150 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
2151 if (i++ == MAX_POLYGON)
2155 /* no extension, just keep the current point as is */
2160 rotate_coordinate(roll, pitch, yaw, &x[i], &y[i], &z[i]);
2161 if (i++ == MAX_POLYGON)
2166 opengl_render_polygon(x, y, z, i, 0); /* no culling, because polygons lay on the gound and are always visible! */
2169 case RENDER_ITEM_PLANET:
2172 double inclination = interpolation.planet_inclination, azimuth = interpolation.planet_azimuth;
2173 double sun_x, sun_y, sun_z, angle_sun_h;
2174 double loc_x, loc_y, loc_z, angle_loc_h, angle_loc_v;
2175 double circle_x[PLANET_VERTICES], circle_y[PLANET_VERTICES], circle_z[PLANET_VERTICES];
2176 double crescent_x[PLANET_VERTICES], crescent_y[PLANET_VERTICES], crescent_z[PLANET_VERTICES];
2177 double x[PLANET_VERTICES], y[PLANET_VERTICES], z[PLANET_VERTICES];
2178 double dist, size, angle, fabs_angle, crescent;
2182 double rotate_sky = 0.0;
2185 printf("RENDER_ITEM_PLANET\n");
2188 rc = use_planet_coord("planet(sun)", 0, &sun_x, &sun_y, &sun_z);
2191 rc = use_planet_coord("planet", render_item->u.planet.vertex, &loc_x, &loc_y, &loc_z);
2195 size = render_item->u.planet.size;
2197 if (motion_new.planet_rotation) {
2198 if (fix_sky_rotation)
2200 rotate_coordinate(0.0, inclination, azimuth, &sun_x, &sun_y, &sun_z);
2201 rotate_coordinate(0.0, inclination, azimuth, &loc_x, &loc_y, &loc_z);
2203 rotate_coordinate(roll, pitch, yaw + rotate_sky, &sun_x, &sun_y, &sun_z);
2204 rotate_coordinate(roll, pitch, yaw + rotate_sky, &loc_x, &loc_y, &loc_z);
2206 /* distance to planet */
2207 dist = sqrt(loc_x * loc_x + loc_y * loc_y + loc_z * loc_z);
2209 /* calculate direction of the sun */
2210 angle_sun_h = atan2(sun_x, sun_z);
2211 angle_loc_h = atan2(loc_x, loc_z);
2212 angle_loc_v = atan2(loc_y, sqrt(loc_x * loc_x + loc_z * loc_z));
2213 /* angle between planets */
2214 angle = angle_sun_h - angle_loc_h;
2216 angle -= 2.0 * M_PI;
2218 angle += 2.0 * M_PI;
2219 /* absolute angle to be used as crescent */
2220 fabs_angle = fabs(angle);
2221 if (fabs_angle > M_PI / 2.0)
2222 fabs_angle = M_PI - fabs_angle;
2224 /* on which side are we (sun is always bright, vertex == 0) */
2225 if ((angle < M_PI / 2.0 && angle > -M_PI / 2.0) || render_item->u.planet.vertex == 0) {
2226 /* get front side color */
2227 opengl_render_color(render_item->u.planet.front_red, render_item->u.planet.front_green, render_item->u.planet.front_blue, debug_opacity);
2229 /* get back side color */
2230 opengl_render_color(render_item->u.planet.back_red, render_item->u.planet.back_green, render_item->u.planet.back_blue, debug_opacity);
2233 /* create and render cicle */
2234 crescent = sin((1.0 - fabs_angle / (M_PI / 2)) * (M_PI / 2.0));
2236 for (i = 0; i < PLANET_VERTICES; i++) {
2237 _sin = size * sin(2.0 * M_PI * (double)i / PLANET_VERTICES);
2238 _cos = size * cos(2.0 * M_PI * (double)i / PLANET_VERTICES);
2239 circle_x[i] = _sin * planet_aspect;
2242 crescent_x[i] = circle_x[i] * crescent;
2243 crescent_y[i] = circle_y[i];
2244 crescent_z[i] = circle_z[i];
2245 /* rotate circle and cresent towards observer (billboarding) */
2246 rotate_coordinate(0.0, -angle_loc_v, 0.0, &circle_x[i], &circle_y[i], &circle_z[i]);
2247 rotate_coordinate(0.0, 0.0, angle_loc_h, &circle_x[i], &circle_y[i], &circle_z[i]);
2248 rotate_coordinate(0.0, -angle_loc_v, 0.0, &crescent_x[i], &crescent_y[i], &crescent_z[i]);
2249 rotate_coordinate(0.0, 0.0, angle_loc_h, &crescent_x[i], &crescent_y[i], &crescent_z[i]);
2252 for (i = 0; i < PLANET_VERTICES; i++) {
2253 _sin = size * sin(2.0 * M_PI * (double)i / PLANET_VERTICES);
2254 _cos = size * cos(2.0 * M_PI * (double)i / PLANET_VERTICES);
2255 circle_x[i] = _sin * planet_aspect;
2258 crescent_x[i] = circle_x[i] * crescent;
2259 crescent_y[i] = circle_y[i];
2260 crescent_z[i] = circle_z[i];
2263 for (i = 0; i < PLANET_VERTICES; i++) {
2264 x[i] = (loc_x + circle_x[i]) / dist * 1000000.0;
2265 y[i] = (loc_y + circle_y[i]) / dist * 1000000.0;
2266 z[i] = (loc_z + circle_z[i]) / dist * 1000000.0;
2268 opengl_render_polygon_and_line(x, y, z, PLANET_VERTICES); /* no culling, its a planet! */
2270 if (render_item->u.planet.vertex == 0) {
2271 /* sun has no crescent */
2275 /* on which side are we */
2276 if (angle < M_PI / 2.0 && angle > -M_PI / 2.0) {
2277 /* get back side color */
2278 opengl_render_color(render_item->u.planet.back_red, render_item->u.planet.back_green, render_item->u.planet.back_blue, debug_opacity);
2280 /* get front side color */
2281 opengl_render_color(render_item->u.planet.front_red, render_item->u.planet.front_green, render_item->u.planet.front_blue, debug_opacity);
2284 /* create and render crescent */
2285 if (angle > M_PI / 2.0 || (angle < 0.0 && angle > -M_PI / 2.0)) {
2287 for (i = 0; i < PLANET_VERTICES / 2 + 1; i++) {
2288 x[i] = (loc_x + circle_x[i]) / dist * 1000000.0;
2289 y[i] = (loc_y + circle_y[i]) / dist * 1000000.0;
2290 z[i] = (loc_z + circle_z[i]) / dist * 1000000.0;
2292 _sin = sin((1.0 - angle / (M_PI / 2)) * (M_PI / 2.0));
2293 for (; i < PLANET_VERTICES; i++) {
2294 x[i] = (loc_x + crescent_x[i]) / dist * 1000000.0;
2295 y[i] = (loc_y + crescent_y[i]) / dist * 1000000.0;
2296 z[i] = (loc_z + crescent_z[i]) / dist * 1000000.0;
2300 for (i = 0; i < PLANET_VERTICES / 2 + 1; i++) {
2301 x[i] = (loc_x + crescent_x[i]) / dist * 1000000.0;
2302 y[i] = (loc_y + crescent_y[i]) / dist * 1000000.0;
2303 z[i] = (loc_z + crescent_z[i]) / dist * 1000000.0;
2305 for (; i < PLANET_VERTICES; i++) {
2306 x[i] = (loc_x + circle_x[i]) / dist * 1000000.0;
2307 y[i] = (loc_y + circle_y[i]) / dist * 1000000.0;
2308 z[i] = (loc_z + circle_z[i]) / dist * 1000000.0;
2311 opengl_render_polygon_and_line(x, y, z, PLANET_VERTICES); /* no culling, its a planet! */
2312 opengl_render_point(loc_x, loc_y, loc_z, 0.0); /* planet is visible at any distance - at least as a point */
2315 case RENDER_ITEM_STARS:
2317 double tilt_offset = 0;
2318 double x_offset = 0;
2320 double view_width, yaw = interpolation.orientation_raw_yaw;
2321 double pitch = interpolation.orientation_raw_pitch;
2322 uint32_t table, table_start;
2325 double red, green, blue;
2326 double rotate_sky = 0.0;
2329 printf("RENDER_ITEM_STARS\n");
2331 /* use default fov of 64 to calculate z distance */
2332 z = 160.0 / frustum_slope_64;
2334 view_width = (int)(320.0 / frustum_slope_64 * frustum_slope_fov);
2337 for (i = 0; i < 16; i++)
2338 color[i] = m68k_read_memory_16(mercenary_palette_stars() + (i << 2));
2340 if (!vr && !improve_stars) {
2341 /* render legacy stars (as with the original game) */
2343 /* table offset is 91, so we substract it and add it back with different FOV, so table begins at later
2344 * full turn is 1024, we have default of 64 degrees: 1024 / 360 * 64 = 182
2345 * then we half it, so we get to the center via 91
2348 if (render_item->u.stars.above_zenith)
2350 yaw = fmod(yaw + 91.0 - (91.0 * (frustum_slope_fov / frustum_slope_64)) + 65536.0, 0x400);
2352 table = mercenary_star_table();
2353 table_start = table + m68k_read_memory_16(table);
2354 table += m68k_read_memory_16(table + ((uint32_t)yaw & 0x7fe));
2355 yaw = yaw / (double)0x800 * 1800.0;
2357 if (render_item->u.stars.above_zenith)
2358 pitch = 0x200 - pitch;
2359 pitch = fmod(pitch + 65536.0, 0x400);
2360 pitch -= render_item->u.stars.v_offset;
2362 pitch = pitch * (double)0x6ccc / 65536.0;
2365 x = m68k_read_memory_16(table);
2368 table = table_start;
2370 x = (view_width - 1) - m68k_read_memory_16(table) - x_offset + yaw;
2374 /* special case where we tilt the view when flying on the planet */
2375 if (render_item->u.stars.tilt) {
2376 /* use offset as given by game: 160 is half of the screen width
2377 * we extend the width to the actual FOV, so it fits
2379 tilt_offset = ((x - (160.0 / frustum_slope_64 * frustum_slope_fov)) * render_item->u.stars.tilt_value) / 65536.0;
2381 y = (double)((m68k_read_memory_16(table)) & 0x1ff) - pitch + tilt_offset;
2383 if (render_item->u.stars.above_zenith) {
2384 x = (double)(view_width - 1) - x;
2388 gamecolor2gl(&red, &green, &blue, color[(m68k_read_memory_8(table - 2) & 0x3c) >> 2]);
2389 opengl_render_color(red, green, blue, debug_opacity);
2391 opengl_render_point(160.0 / frustum_slope_64 * frustum_slope_fov - x, 68.0 - y, z, 0.0);
2394 /* render ovr stars, render star table as a sphere */
2398 double inclination = interpolation.planet_inclination, azimuth = interpolation.planet_azimuth;
2400 if (render_item->u.stars.above_zenith)
2403 table = mercenary_star_table();
2404 table += m68k_read_memory_16(table);
2407 x = m68k_read_memory_16(table);
2411 y = (double)((m68k_read_memory_16(table)) & 0x1ff) - 108.796875;
2414 gamecolor2gl(&red, &green, &blue, color[(m68k_read_memory_8(table - 2) & 0x3c) >> 2]);
2415 opengl_render_color(red, green, blue, debug_opacity);
2417 h = (900.0 - x + 160) / 900.0 * M_PI;
2418 v1 = (68.0 - y) / 900.0 * M_PI;
2419 /* wrap star field (is actually 86.2 degrees high) */
2421 v2 = v1 + (86.2 / 180.0 * M_PI);
2423 v2 = v1 - (86.2 / 180.0 * M_PI);
2424 if (v1 < 0.934 && v1 > -0.934) {
2425 /* be sure that v1 will not exceed PI/2 */
2426 v1 = v1 / cos(v1); /* FIXME: there should be a better way to distribute stars equally */
2427 x = -sin(h) * cos(v1);
2429 z = cos(h) * cos(v1);
2430 if (motion_new.planet_rotation) {
2431 if (fix_sky_rotation)
2433 rotate_coordinate(0.0, inclination, azimuth, &x, &y, &z);
2435 rotate_coordinate(roll, pitch, yaw + rotate_sky, &x, &y, &z);
2436 opengl_render_point(1000000.0 * x, 1000000.0 * y, 1000000.0 * z, 0.0);
2438 if (v2 < 0.934 && v2 > -0.934) {
2439 /* be sure that v2 will not exceed PI/2 */
2440 v2 = v2 / cos(v2) /* FIXME: there should be a better way to distribute stars equally */;
2441 x = -sin(h) * cos(v2);
2443 z = cos(h) * cos(v2);
2444 if (motion_new.planet_rotation) {
2445 if (fix_sky_rotation)
2447 rotate_coordinate(0.0, inclination, azimuth, &x, &y, &z);
2449 rotate_coordinate(roll, pitch, yaw + rotate_sky, &x, &y, &z);
2450 opengl_render_point(1000000.0 * x, 1000000.0 * y, 1000000.0 * z, 0.0);
2456 case RENDER_ITEM_INTERSTELLAR_STARS:
2461 double red, green, blue;
2464 printf("RENDER_ITEM_INTERSTELLAR_STARS\n");
2466 /* use default fov of 64 to calculate z distance */
2467 z = 160.0 / frustum_slope_64;
2470 for (i = 0; i < 16; i++)
2471 color[i] = m68k_read_memory_16(mercenary_palette_stars() + (i << 2));
2473 for (i = 0; i < render_item->u.interstars.count; i++) {
2474 gamecolor2gl(&red, &green, &blue, color[render_item->u.interstars.color[i]]);
2475 opengl_render_color(red, green, blue, debug_opacity);
2477 opengl_render_point((160.0 - (double)render_item->u.interstars.x[i]) * 100, (68.0 - (double)render_item->u.interstars.y[i]) * 100, z * 100, 0.0);
2481 case RENDER_ITEM_INTERSTELLAR_SUN:
2483 double red, green, blue;
2486 printf("RENDER_ITEM_INTERSTELLAR_SUN\n");
2489 gamecolor2gl(&red, &green, &blue, 0x777);
2490 opengl_render_color(red, green, blue, debug_opacity);
2492 opengl_render_point(0.0, 0.0, 1000000.0, 0.0);
2495 case RENDER_ITEM_SIGHTS:
2497 double x[4], y[4], z[4];
2498 double red, green, blue;
2501 printf("RENDER_ITEM_SIGHTS\n");
2503 /* use default fov of 64 to calculate z distance */
2504 z[0] = z[1] = z[2] = z[3] = SIGHT_DIST;
2507 gamecolor2gl(&red, &green, &blue, 0x777);
2508 opengl_render_color(red, green, blue, debug_opacity);
2510 y[0] = y[3] = -1.0 / 160.0 * SIGHT_DIST * frustum_slope_64;
2511 y[1] = y[2] = 1.0 / 160.0 * SIGHT_DIST * frustum_slope_64;
2512 x[0] = x[1] = -16.0 / 160.0 * SIGHT_DIST * frustum_slope_64;
2513 x[2] = x[3] = -8.0 / 160.0 * SIGHT_DIST * frustum_slope_64;
2514 opengl_render_polygon(x, y, z, 4, 0); /* no culling, because sights are always visible! */
2515 x[0] = x[1] = 8.0 / 160.0 * SIGHT_DIST * frustum_slope_64;
2516 x[2] = x[3] = 16.0 / 160.0 * SIGHT_DIST * frustum_slope_64;
2517 opengl_render_polygon(x, y, z, 4, 0); /* no culling, because sights are always visible! */
2520 case RENDER_ITEM_EXPLOSION:
2523 double loc_x, loc_y, loc_z, size;
2524 double x[EXPLOSION_VERTICES], y[EXPLOSION_VERTICES], z[EXPLOSION_VERTICES];
2528 printf("RENDER_ITEM_EXPLOSION\n");
2530 opengl_render_color(render_item->u.explosion.red, render_item->u.explosion.green, render_item->u.explosion.blue, debug_opacity);
2532 for (e = 0; e < render_item->u.explosion.count; e++) {
2533 loc_x = render_item->u.explosion.x[e];
2534 loc_y = render_item->u.explosion.y[e];
2535 loc_z = render_item->u.explosion.z[e];
2537 rotate_coordinate(roll, pitch, yaw, &loc_x, &loc_y, &loc_z);
2538 /* calculate size from projected size and z (256 is the distance of the game's projection plane) */
2539 size = render_item->u.explosion.size[e] * (fabs(loc_z) / 256.0);
2540 /* create and render cicle */
2541 for (i = 0; i < EXPLOSION_VERTICES; i++) {
2542 x[i] = loc_x + size * sin(2 * M_PI * (double)i / EXPLOSION_VERTICES) * explosion_aspect;
2543 y[i] = loc_y + size * cos(2 * M_PI * (double)i / EXPLOSION_VERTICES);
2546 opengl_render_polygon_and_line(x, y, z, EXPLOSION_VERTICES); /* no culling, its a debris! */
2547 opengl_render_point(loc_x, loc_y, loc_z, 0.0); /* debris is visible at any distance - at least as a point */
2552 print_info("Unknown render item type, please fix!\n");
2560 static double interpolate_orientation(double old, double new, double inter)
2562 double turn = new - old;
2569 /* don't interpolate, if our rotation was too fast.
2570 * e.g: taxi drive around corder, load/quit game, ...
2572 if (turn > M_PI / 8.0 || turn < -M_PI / 8.0)
2575 new = old + turn * inter;
2585 static double interpolate_raw_orientation(uint16_t old, uint16_t new, double inter)
2587 int16_t turn = (new - old) & 0x3ff;
2592 /* don't interpolate, if our rotation was too fast.
2593 * e.g: taxi drive around corder, load/quit game, ...
2595 if (turn > 0x200 / 8 || turn < -0x200 / 8)
2598 /* don't do modulo 0x400, since the user of this data does it */
2599 return (double)old + (double)turn * inter;
2602 static double interpolate_offset(int32_t old, int32_t new, double inter, int32_t limit)
2606 /* be sure to look only at the lower 28 bits, because on planet these bits are used only */
2607 if (ground_index >= 0)
2608 offset = wrap_int28(old - new);
2610 offset = (int32_t)(old - new);
2612 if (limit > 0 && (offset > limit || offset < -limit))
2615 return offset * (1.0 - inter);
2618 static render_item_t *interpolate_door(double inter)
2620 static render_item_t interpolated;
2621 render_item_t *old_vertices = render_list_old, *new_vertices = render_list_new;
2624 /* find old and new vertices */
2625 while (old_vertices && old_vertices->type != RENDER_ITEM_VERTICES_INTERIOR)
2626 old_vertices = old_vertices->next;
2627 while (new_vertices && new_vertices->type != RENDER_ITEM_VERTICES_INTERIOR)
2628 new_vertices = new_vertices->next;
2630 /* building does not exist in old or new render */
2631 if (!old_vertices || !new_vertices)
2634 memcpy(&interpolated, new_vertices, sizeof(interpolated));
2635 ii = MAX_INTERIOR_VERTEX >> 2;
2636 for (i = 0; i < ii; i++) {
2637 /* vertex must exist in both frames */
2638 if (!old_vertices->u.vertices_interior.set[i] || !new_vertices->u.vertices_interior.set[i])
2640 /* all verices must not have be too far away */
2641 if (fabs(old_vertices->u.vertices_interior.x[i] - new_vertices->u.vertices_interior.x[i]) > 100.0)
2643 if (fabs(old_vertices->u.vertices_interior.z[i] - new_vertices->u.vertices_interior.z[i]) > 100.0)
2646 interpolated.u.vertices_interior.x[i] =
2647 (double)old_vertices->u.vertices_interior.x[i] * (1.0 - inter) +
2648 (double)new_vertices->u.vertices_interior.x[i] * inter;
2649 interpolated.u.vertices_interior.z[i] =
2650 (double)old_vertices->u.vertices_interior.z[i] * (1.0 - inter) +
2651 (double)new_vertices->u.vertices_interior.z[i] * inter;
2653 for (i = 0; i < 4; i++) {
2654 if (old_vertices->u.vertices_interior.y[i] != new_vertices->u.vertices_interior.y[i])
2658 return &interpolated;
2661 /* make a list of objects that moved and store their displacement */
2662 static void interpolate_objects(double inter)
2664 render_item_t *old_info, *new_info;
2667 /* hunt for objects that exist in both (old and new) lists and moved */
2669 for (new_info = render_list_new; new_info; new_info = new_info -> next) {
2671 if (new_info->type != RENDER_ITEM_OBJECT_INFO)
2674 if (!new_info->u.info.moving)
2676 /* interiors don't move */
2677 if (new_info->u.info.id < 0)
2679 /* check matching object with same ID */
2680 for (old_info = render_list_old; old_info; old_info = old_info -> next) {
2682 if (old_info->type != RENDER_ITEM_OBJECT_INFO)
2685 if (!old_info->u.info.moving)
2688 if (old_info->u.info.id == new_info->u.info.id)
2691 /* no matching object found */
2695 if (old_info->u.info.east == new_info->u.info.east
2696 && old_info->u.info.height == new_info->u.info.height
2697 && old_info->u.info.north == new_info->u.info.north)
2699 /* interpolate and store */
2700 interpolation.object_id[count] = new_info->u.info.id;
2701 interpolation.object_offset_east[count] = interpolate_offset(old_info->u.info.east, new_info->u.info.east, inter, 4096);
2702 interpolation.object_offset_height[count] = interpolate_offset(old_info->u.info.height, new_info->u.info.height, inter, 4096);
2703 interpolation.object_offset_north[count] = interpolate_offset(old_info->u.info.north, new_info->u.info.north, inter, 4096);
2704 if (count++ == MAX_MOVING_OBJECTS)
2707 interpolation.object_count = count;
2710 /* make a vertex list of interpolated planets */
2711 static render_item_t *interpolate_planets(double inter)
2713 static render_item_t interpolated;
2714 render_item_t *old_info, *new_info;
2715 render_item_t *old_vertices = NULL, *new_vertices = NULL;
2718 /* get vertices for planets/comet */
2719 for (old_info = render_list_old; old_info; old_info = old_info -> next) {
2720 if (old_info->type == RENDER_ITEM_VERTICES_0)
2721 old_vertices = old_info;
2722 /* check until comet's polygons are renderd. at this time the coordinates are all ready, so we can interpolate */
2723 if (old_info->type == RENDER_ITEM_COMET_POLYGON)
2726 for (new_info = render_list_new; new_info; new_info = new_info -> next) {
2727 if (new_info->type == RENDER_ITEM_VERTICES_0)
2728 new_vertices = new_info;
2729 /* check until comet's polygons are renderd. at this time the coordinates are all ready, so we can interpolate */
2730 if (new_info->type == RENDER_ITEM_COMET_POLYGON)
2734 /* check for existing planet's vertices and if planets are rendered in both lists (old and new) */
2735 if (old_info == NULL || new_info == NULL || old_vertices == NULL || new_vertices == NULL)
2738 /* interpolate vertices */
2739 for (i = 0; i < (MAX_VERTEX >> 2); i++) {
2740 interpolated.u.vertices.x[i] = old_vertices->u.vertices.x[i] * (1.0 - inter) + new_vertices->u.vertices.x[i] * inter;
2741 interpolated.u.vertices.y[i] = old_vertices->u.vertices.y[i] * (1.0 - inter) + new_vertices->u.vertices.y[i] * inter;
2742 interpolated.u.vertices.z[i] = old_vertices->u.vertices.z[i] * (1.0 - inter) + new_vertices->u.vertices.z[i] * inter;
2745 return &interpolated;
2748 /* always renders NEW! items
2749 * use inter == 1.0 to render motion to vertices of NEW items
2750 * use inter 0.0 .. 1.0 to interpolate motion between OLD and NEW items
2751 * return 0, if the scene was rendered, returns < 0, if there is no scene
2753 int render_all_items(double inter, int vr)
2755 render_item_object_info = NULL;
2756 render_item_vertices_0 = render_item_vertices_1 = render_item_vertices_2 = NULL;
2757 render_item_vertices_interior = NULL;
2758 render_item_vertices_planets = NULL;
2760 /* no interpolation when leaving or entering planet to/from space */
2761 if ((last_ground_index < 0 && ground_index >= 0)
2762 || (last_ground_index >= 0 && ground_index < 0)) {
2766 /* reset interpolation */
2767 memset(&interpolation, 0, sizeof(interpolation));
2768 interpolation.orientation_roll = motion_new.orientation_roll;
2769 interpolation.orientation_pitch = motion_new.orientation_pitch;
2770 interpolation.orientation_yaw = motion_new.orientation_yaw;
2771 interpolation.orientation_raw_pitch = motion_new.orientation_raw_pitch;
2772 interpolation.orientation_raw_yaw = motion_new.orientation_raw_yaw;
2773 interpolation.planet_inclination = motion_new.planet_inclination;
2774 interpolation.planet_azimuth = motion_new.planet_azimuth;
2775 //printf("we are at %08x %08x %08x\n", motion_new.position_east, motion_new.position_height, motion_new.position_north);
2777 /* do interpolation */
2778 if (inter != 1.0 && render_list_old) {
2779 /* interpolate orientation */
2780 interpolation.orientation_roll = interpolate_orientation(motion_old.orientation_roll, motion_new.orientation_roll, inter);
2781 interpolation.orientation_pitch = interpolate_orientation(motion_old.orientation_pitch, motion_new.orientation_pitch, inter);
2782 interpolation.orientation_yaw = interpolate_orientation(motion_old.orientation_yaw, motion_new.orientation_yaw, inter);
2783 interpolation.orientation_raw_pitch = interpolate_raw_orientation(motion_old.orientation_raw_pitch, motion_new.orientation_raw_pitch, inter);
2784 interpolation.orientation_raw_yaw = interpolate_raw_orientation(motion_old.orientation_raw_yaw, motion_new.orientation_raw_yaw, inter);
2785 interpolation.planet_inclination = interpolate_orientation(motion_old.planet_inclination, motion_new.planet_inclination, inter);
2786 interpolation.planet_azimuth = interpolate_orientation(motion_old.planet_azimuth, motion_new.planet_azimuth, inter);
2788 /* interpolate position */
2789 interpolation.offset_east = interpolate_offset(motion_old.position_east, motion_new.position_east, inter, 0);
2790 interpolation.offset_height = interpolate_offset(motion_old.position_height, motion_new.position_height, inter, 0);
2791 interpolation.offset_north = interpolate_offset(motion_old.position_north, motion_new.position_north, inter, 0);
2792 /* prevent glitch when using elevators: a sudden vertical move is ignored
2793 * this is not the best solution, because fast vertical flying (from 0) will also be detected */
2794 if (old_height_offset == 0
2795 && (new_height_offset >= 150 || new_height_offset <= -150)) {
2796 interpolation.offset_east = 0.0;
2797 interpolation.offset_height = 0.0;
2798 interpolation.offset_north = 0.0;
2801 /* interpolate doors of building (if any) */
2802 interpolation.interior = interpolate_door(inter);
2804 /* interpolate objects */
2805 interpolate_objects(inter);
2807 /* interpolate planets */
2808 interpolation.planets = interpolate_planets(inter);
2811 /* return failure, if nothing can be rendered */
2812 if (!render_list_new)
2815 for (render_item = render_list_new; render_item; render_item = render_item->next) {
2816 render_one_item(render_item, vr);
2822 int render_all_white(int vr)
2824 render_item_t sky_item;
2826 memset(&sky_item, 0, sizeof(sky_item));
2827 sky_item.type = RENDER_ITEM_SKY;
2830 sky_item.u.ground.red = 1.0;
2831 sky_item.u.ground.green = 1.0;
2832 sky_item.u.ground.blue = 1.0;
2834 render_one_item(&sky_item, vr);
2839 void render_capture_reset(void)
2841 /* flush old list, if exists */
2843 /* flush new list, if exists */
2844 render_list_old = render_list_new;
2846 /* reset list pointers */
2847 render_list_old = NULL;
2848 render_list_new = NULL;
2849 render_list_end = &render_list_new;
2853 int render_capture_is_interstellar(void)
2855 if (!render_list_new)
2857 for (render_item = render_list_new; render_item; render_item = render_item->next) {
2858 if (render_item->type == RENDER_ITEM_INTERSTELLAR_STARS)