[go: up one dir, main page]

Menu

[e79954]: / track / Strip_Track.cc  Maximize  Restore  History

Download this file

1657 lines (1462 with data), 46.9 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
// Strip_Track.cc - a track.
//
// Copyright (C) 2001--2004 Sam Varner
//
// This file is part of Vamos Automotive Simulator.
//
// Vamos is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vamos is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vamos. If not, see <http://www.gnu.org/licenses/>.
#include "Strip_Track.h"
#include "../geometry/Numeric.h"
#include "../geometry/Spline.h"
#include "../media/Texture_Image.h"
#include "../geometry/Parameter.h"
#include <GL/glu.h>
#include <cmath>
#include <cassert>
using namespace Vamos_Geometry;
using namespace Vamos_Media;
using namespace Vamos_Track;
Camera s_default_camera (0, Three_Vector (100.0, -20.0, 10.0), 0.0);
namespace Vamos_Track
{
class Sky_Box
{
Texture_Image* mp_sides;
Texture_Image* mp_top;
Texture_Image* mp_bottom;
GLuint m_list_id;
public:
Sky_Box (double side_length,
std::string sides_image,
std::string top_image,
std::string bottom_image,
bool smooth);
~Sky_Box ();
void draw (const Three_Vector& view) const;
};
}
Sky_Box::Sky_Box (double side_length,
std::string sides_image,
std::string top_image,
std::string bottom_image,
bool smooth)
// Clamp the textures to aviod showing seams.
: mp_sides (new Texture_Image (sides_image, smooth, true,
GL_CLAMP_TO_EDGE)),
mp_top (new Texture_Image (top_image, smooth, true,
GL_CLAMP_TO_EDGE)),
mp_bottom (new Texture_Image (bottom_image, smooth, true,
GL_CLAMP_TO_EDGE)),
m_list_id (glGenLists (1))
{
double height = side_length;
double length = side_length;
double width = side_length;
double x = -length / 2.0;
double y = -width / 2.0;
double z = -height / 2.0;
glNewList (m_list_id, GL_COMPILE);
glColor3f (1.0, 1.0, 1.0);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
mp_sides->activate ();
// front
glBegin (GL_QUAD_STRIP);
glTexCoord2d (0.0, 0.0);
glVertex3d (x + length, y + width, z + height);
glTexCoord2d (0.0, 1.0);
glVertex3d (x + length, y + width, z);
glTexCoord2d (0.25, 0.0);
glVertex3d (x + length, y, z + height);
glTexCoord2d (0.25, 1.0);
glVertex3d (x + length, y, z);
// right
glTexCoord2d (0.25, 0.0);
glVertex3d (x + length, y, z + height);
glTexCoord2d (0.25, 1.0);
glVertex3d (x + length, y, z);
glTexCoord2d (0.5, 0.0);
glVertex3d (x, y, z + height);
glTexCoord2d (0.5, 1.0);
glVertex3d (x, y, z);
// back
glTexCoord2d (0.5, 0.0);
glVertex3d (x, y, z + height);
glTexCoord2d (0.5, 1.0);
glVertex3d (x, y, z);
glTexCoord2d (0.75, 0.0);
glVertex3d (x, y + width, z + height);
glTexCoord2d (0.75, 1.0);
glVertex3d (x, y + width, z);
// left
glTexCoord2d (0.75, 0.0);
glVertex3d (x, y + width, z + height);
glTexCoord2d (0.75, 1.0);
glVertex3d (x, y + width, z);
glTexCoord2d (1.0, 0.0);
glVertex3d (x + length, y + width, z + height);
glTexCoord2d (1.0, 1.0);
glVertex3d (x + length, y + width, z);
glEnd();
// top
mp_top->activate ();
glBegin (GL_QUADS);
glTexCoord2d (0.0, 0.0);
glVertex3d (x, y + width, z + height);
glTexCoord2d (0.0, 1.0);
glVertex3d (x + length, y + width, z + height);
glTexCoord2d (1.0, 1.0);
glVertex3d (x + length, y, z + height);
glTexCoord2d (1.0, 0.0);
glVertex3d (x, y, z + height);
glEnd ();
// bottom
mp_bottom->activate ();
glBegin (GL_QUADS);
glTexCoord2d (0.0, 0.0);
glVertex3d (x + length, y + width, z);
glTexCoord2d (0.0, 1.0);
glVertex3d (x, y + width, z);
glTexCoord2d (1.0, 1.0);
glVertex3d (x, y, z);
glTexCoord2d (1.0, 0.0);
glVertex3d (x + length, y, z);
glEnd ();
glFlush ();
glEndList ();
}
Sky_Box::~Sky_Box ()
{
delete mp_bottom;
delete mp_top;
delete mp_sides;
glDeleteLists (m_list_id, 1);
}
void
Sky_Box::draw (const Three_Vector& view) const
{
glLoadIdentity ();
glTranslatef (view.x, view.y, view.z);
glCallList (m_list_id);
// Clear the depth buffer to keep the sky behind everything else.
glClear (GL_DEPTH_BUFFER_BIT);
}
//* Class Map_Background
namespace Vamos_Track
{
class Map_Background
{
Texture_Image* mp_image;
double m_x_offset;
double m_y_offset;
double m_x_size;
double m_y_size;
public:
Map_Background (std::string image_file_name,
double x_offset, double y_offset,
double x_size, double y_size);
~Map_Background ();
void draw () const;
};
}
Map_Background::Map_Background (std::string image_file_name,
double x_offset, double y_offset,
double x_size, double y_size)
: mp_image (new Texture_Image (image_file_name, true)),
m_x_offset (x_offset),
m_y_offset (y_offset),
m_x_size (x_size),
m_y_size (y_size)
{
}
Map_Background::~Map_Background ()
{
delete mp_image;
}
void
Map_Background::draw () const
{
glColor3f (1.0, 1.0, 1.0);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
mp_image->activate ();
glLoadIdentity ();
glTranslatef (m_x_offset, m_y_offset, 0.0);
glBegin (GL_QUADS);
glTexCoord2d (0.0, 1.0);
glVertex3d (m_x_offset, m_y_offset, 0.0);
glTexCoord2d (0.0, 0.0);
glVertex3d (m_x_offset, m_y_offset + m_y_size, 0.0);
glTexCoord2d (1.0, 0.0);
glVertex3d (m_x_offset + m_x_size, m_y_offset + m_y_size, 0.0);
glTexCoord2d (1.0, 1.0);
glVertex3d (m_x_offset + m_x_size, m_y_offset, 0.0);
glEnd ();
// Clear the depth buffer to keep the background behind the track.
glClear (GL_DEPTH_BUFFER_BIT);
}
//* Struct Camera
Camera::Camera (size_t segment_index_in,
const Three_Vector& position_in,
double range_in)
: segment_index (segment_index_in),
position (position_in),
fixed (false),
vertical_field_angle (10.0),
range (range_in)
{}
Camera::Camera ()
: segment_index (0),
fixed (false),
vertical_field_angle (10.0),
range (0.0)
{}
//* Class Racing_Line
Racing_Line::Racing_Line ()
: m_length (0.0),
mp_line (0),
m_list_id (0),
m_iterations (1500),
m_stiffness (0.5),
m_damping (0.01),
m_margin (1.6),
m_resolution (0.0)
{
}
Racing_Line::~Racing_Line ()
{
delete mp_line;
glDeleteLists (m_list_id, 1);
}
Two_Vector
Racing_Line::position (double along) const
{
assert (mp_line != 0);
return mp_line->interpolate (wrap (along, m_length));
}
Three_Vector
Racing_Line::curvature (double along, double offline_fraction) const
{
along = wrap (along, m_length);
const Three_Vector c1 = m_curvature.interpolate (along);
const Three_Vector c2 = (offline_fraction > 0.0)
? m_left_curvature.interpolate (along)
: m_right_curvature.interpolate (along);
// linearly interpolate from line to edge.
const double f = std::abs (offline_fraction);
return Three_Vector (Vamos_Geometry::interpolate (f, 0.0, c1.x, 1.0, c2.x),
Vamos_Geometry::interpolate (f, 0.0, c1.y, 1.0, c2.y),
Vamos_Geometry::interpolate (f, 0.0, c1.z, 1.0, c2.z));
}
Three_Vector
Racing_Line::tangent (double along) const
{
return m_tangent.interpolate (wrap (along, m_length));
}
Three_Vector Racing_Line::normal_curvature (const Three_Vector& p1,
const Three_Vector& p2,
const Three_Vector& p3) const
{
Three_Vector r21 (p1 - p2);
Three_Vector r23 (p3 - p2);
Three_Vector up (r23.cross (r21));
const double length_23 = r23.magnitude ();
// Assume the angle is small so that sin x = x.
return up / (r21.dot (r21) * length_23);
}
Three_Vector Racing_Line::planar_curvature (const Three_Vector& p1,
const Three_Vector& p2,
const Three_Vector& p3) const
{
return normal_curvature (p1, p2, p3).magnitude () * (p2 - (p1 + p3)/2.0).unit ();
}
void
Racing_Line::force (const Three_Vector& p1,
const Three_Vector& p2,
const Three_Vector& p3,
Three_Vector& f1,
Three_Vector& f2,
Three_Vector& f3)
{
Three_Vector r21 (p1 - p2);
Three_Vector r23 (p3 - p2);
Three_Vector curvature = normal_curvature (p1, p2, p3);
Three_Vector df1 = m_stiffness * curvature.cross (r21);
Three_Vector df3 = -m_stiffness * curvature.cross (r23);
f1 += df1;
f2 -= (df1 + df3);
f3 += df3;
}
double
Racing_Line::right_width (const Road& road, double along) const
{
return road.right_racing_line_width (along) - m_margin;
}
double
Racing_Line::left_width (const Road& road, double along) const
{
return road.left_racing_line_width (along) - m_margin;
}
void
Racing_Line::propagate (const Road& road,
std::vector <Three_Vector>& positions,
std::vector <Three_Vector>& velocites,
double interval,
bool close)
{
const size_t points = positions.size ();
std::vector <Three_Vector> forces (points);
force (positions [points-1], positions [0], positions [1],
forces [points-1], forces [0], forces [1]);
for (size_t i = 1; i < points - 1; i++)
{
force (positions [i-1], positions [i], positions [i+1],
forces [i-1], forces [i], forces [i+1]);
}
force (positions [points-2], positions [points-1], positions [0],
forces [points-2], forces [points-1], forces [0]);
size_t index = 0;
for (size_t i = 0; i < points; i++)
{
velocites [i] += (forces [i] - velocites [i] * m_damping);
positions [i] += velocites [i];
// Constrain the racing line to the track.
const double along = i*interval;
const double across = clip (road.track_coordinates (positions [i], index).y,
-right_width (road, along),
left_width (road, along));
positions [i] = road.position (along, across);
}
}
void
Racing_Line::build (const Road& road, bool close)
{
m_length = road.length ();
if (m_length <= 0.0)
throw Bad_Racing_Line_Length (m_length);
delete mp_line;
mp_line = new Parametric_Spline ();
// Divide the track into the smallest number of equal intervals not longer
// than 'max_interval'
const double max_interval = m_resolution > 0.0
? m_resolution
: 0.5*(left_width (road, 0.0) + right_width (road, 0.0));
double interval = max_interval;
const int divisions = std::ceil (m_length / interval);
if (divisions <= 0)
throw No_Racing_Line_Segments (divisions);
interval = m_length / divisions;
// Use the center of the track as the initial guess.
std::vector <Three_Vector> positions;
for (int node = 0; node < divisions; node++)
positions.push_back (road.position (node * interval, 0.0));
std::vector <Three_Vector> velocities (positions.size ());
for (size_t i = 0; i < m_iterations; i++)
propagate (road, positions, velocities, interval, close);
m_curvature.clear ();
m_left_curvature.clear ();
m_right_curvature.clear ();
m_tangent.clear ();
for (size_t i = 1; i < positions.size () - 1; ++i)
load_curvature (i*interval,
positions [i - 1],
positions [i],
positions [i + 1],
road);
if (close)
{
mp_line->set_periodic (m_length);
m_curvature.set_periodic (m_length);
m_left_curvature.set_periodic (m_length);
m_right_curvature.set_periodic (m_length);
m_tangent.set_periodic (m_length);
}
build_list (road);
}
void
Racing_Line::load_curvature (double along,
const Three_Vector& p1,
const Three_Vector& p2,
const Three_Vector& p3,
const Road& road)
{
const Gl_Road_Segment& segment = *road.segment_at (along);
mp_line->load (along, p2.x, p2.y);
m_tangent.load (along, (p3 - p1).unit ());
const double factor = segment.racing_line_curvature_factor ();
m_curvature.load (along, factor * planar_curvature (p1, p2, p3));
if (segment.radius () == 0.0)
{
m_left_curvature.load (along, Three_Vector::ZERO);
m_right_curvature.load (along, Three_Vector::ZERO);
}
else
{
double across = segment.left_racing_line_width (along);
Three_Vector p1 = road.position (along - 10, across);
Three_Vector p2 = road.position (along, across);
Three_Vector p3 = road.position (along + 10, across);
m_left_curvature.load (along, planar_curvature (p1, p2, p3));
across = segment.right_racing_line_width (along);
p1 = road.position (along - 10, across);
p2 = road.position (along, across);
p3 = road.position (along + 10, across);
m_right_curvature.load (along, planar_curvature (p1, p2, p3));
}
}
void
Racing_Line::build_list (const Road& road)
{
if (m_list_id != 0)
glDeleteLists (m_list_id, 1);
m_list_id = glGenLists (1);
glNewList (m_list_id, GL_COMPILE);
glDisable (GL_TEXTURE_2D);
glLineWidth (2.0);
glBegin (GL_LINE_STRIP);
Three_Vector last_world = position (0.0);
for (double along = 0.0; along < m_length; along += 0.1)
{
Three_Vector world = position (along);
Three_Vector forward = (world - last_world).unit ();
Three_Vector curve = curvature (along, 0.0);
double color = 100.0 * curve.magnitude ();
if (curve.cross (forward).z < 0.0)
color *= -1.0;
glColor4f (1.0 - color, 1.0 + color, 1.0, 0.5);
glVertex3d (world.x,
world.y,
road.segment_at (along)->world_elevation (world) + 0.05);
last_world = world;
}
glEnd ();
glPointSize (4.0);
glColor4f (0.8, 0.0, 0.0, 0.5);
glBegin (GL_POINTS);
for (size_t i = 0; i < mp_line->size (); i++)
{
Three_Vector world = (*mp_line) [i];
glVertex3d (world.x,
world.y,
road.segment_at (mp_line->parameter (i))->world_elevation (world) + 0.04);
}
glEnd ();
glEnable (GL_TEXTURE_2D);
glEndList ();
}
void
Racing_Line::draw () const
{
glCallList (m_list_id);
}
//* Class Road
Road::Road ()
: mp_elevation (new Spline ()),
m_start_direction (0.0),
m_racing_line (),
m_draw_racing_line (false),
m_is_closed (false)
{
clear ();
}
Road::~Road ()
{
clear ();
delete mp_elevation;
}
void
Road::clear ()
{
mp_elevation->clear ();
mp_elevation->load (0.0, 0.0);
m_length = 0.0;
m_bounds = Rectangle ();
for (Segment_List::iterator it = m_segments.begin ();
it != m_segments.end ();
it++)
{
delete (*it);
}
m_segments.clear ();
}
size_t
Road::add_segment (Gl_Road_Segment* segment)
{
if (!m_segments.empty ())
{
const Gl_Road_Segment* last = *(m_segments.end () - 1);
segment->set_start (last->end_coords (),
last->end_distance (),
last->end_angle (),
0.0,
last->texture_offsets ());
}
m_segments.push_back (segment);
return m_segments.size ();
}
double
Road::build_elevation (bool periodic)
{
double length = 0.0;
for (Segment_List::iterator it = m_segments.begin ();
it != m_segments.end ();
it++)
{
(*it)->build_elevation (mp_elevation, length);
length += (*it)->length ();
}
if (periodic)
mp_elevation->set_periodic (length);
return length;
}
void
Road::build_segments (Three_Vector start_coords,
double start_angle,
double start_bank)
{
std::vector <double>
texture_offsets ((*(m_segments.begin ()))->materials ().size ());
m_length = 0.0;
for (Segment_List::iterator it = m_segments.begin ();
it != m_segments.end ();
it++)
{
(*it)->set_start (start_coords, m_length, start_angle, start_bank,
texture_offsets);
(*it)->build ();
// Update the bounding dimensions.
m_bounds.enclose ((*it)->bounds ());
m_length += (*it)->length ();
start_coords = (*it)->end_coords ();
start_angle = (*it)->end_angle ();
start_bank = (*it)->banking ().end_angle ();
texture_offsets = (*it)->texture_offsets ();
}
}
Three_Vector
Road::position (double along, double from_center, const Gl_Road_Segment& segment) const
{
return segment.position (along - segment.start_distance (), from_center);
}
Three_Vector
Road::position (double along, double from_center) const
{
along = wrap (along, length ());
const Gl_Road_Segment* segment = segment_at (along);
return position (along, from_center, *segment);
}
// Return WORLD_POS transformed to the track's coordinate system.
// SEGMENT_INDEX will be modified if the position on another
// segment.
Three_Vector
Road::track_coordinates (const Three_Vector& world_pos,
size_t& segment_index,
bool forward_only) const
{
// Find the distance along the track, distance from center, and elevation
// for the world coordinates `world_pos.x' and `world_pos.y'.
Three_Vector track_pos;
assert (segment_index < m_segments.size ());
Gl_Road_Segment* segment = m_segments [segment_index];
size_t i = 0;
bool found = false;
while (i < m_segments.size () + 1)
{
double off = segment->coordinates (world_pos, track_pos);
if (std::abs (off) < 1.0e-6)
{
found = true;
break;
}
if (forward_only || (off > 0.0))
{
// We're off the end of the current segment. Find a new
// candidate segment.
segment_index++;
if (segment_index == m_segments.size ())
{
if (m_is_closed)
segment_index = 0;
else
{
segment_index--;
found = true;
break;
}
}
}
else
{
// Try the previous segment.
if (segment_index == 0)
{
if (m_is_closed)
segment_index = m_segments.size ();
else
{
found = true;
break;
}
}
segment_index--;
}
segment = m_segments [segment_index];
i++;
}
// Throw an exception if a segment could not be found.
if (!found)
throw Segment_Not_Found (world_pos, segment_index);
assert (segment_index < m_segments.size ());
track_pos.x += segment->start_distance ();
return track_pos;
}
double
Road::distance (double along1, double along2) const
{
const double limit = 0.5 * m_length;
return wrap (along1 - along2, -limit, limit);
}
double
Road::left_road_width (double along) const
{
return segment_at (along)->left_road_width (along);
}
double
Road::right_road_width (double along) const
{
return segment_at (along)->right_road_width (along);
}
double
Road::right_racing_line_width (double along) const
{
return segment_at (along)->right_racing_line_width (along);
}
double
Road::left_racing_line_width (double along) const
{
return segment_at (along)->left_racing_line_width (along);
}
const Gl_Road_Segment*
Road::segment_at (double along) const
{
double distance = 0.0;
for (Segment_List::const_iterator it = segments ().begin ();
it != segments ().end ();
it++)
{
if (distance + (*it)->length () >= along)
return *it;
distance += (*it)->length ();
}
return segments ()[0];
}
void
Road::build_racing_line ()
{
m_racing_line.build (*this, m_is_closed);
}
void
Road::narrow_pit_segments ()
{
Gl_Road_Segment* last_from_out = 0;
Gl_Road_Segment* last_from_in = 0;
for (Segment_List::iterator it = m_segments.begin ();
it != m_segments.end ();
it++)
{
const Pit_Lane_Transition& pit = (*it)->pit ();
if (!pit.active ())
continue;
if (pit.direction () == OUT)
{
for (Segment_List::reverse_iterator rit (it);
((rit != m_segments.rend ())
&& (*rit != last_from_in)
&& !(*rit)->pit ().active ());
rit++)
{
(*rit)->narrow (pit.side (), (*it)->pit_width ());
last_from_out = *rit;
}
}
else
{
for (Segment_List::iterator it2 (it + 1);
((it2 != m_segments.end ())
&& (*it2 != last_from_out)
&& !(*it2)->pit ().active ());
it2++)
{
(*it2)->narrow (pit.side (), (*it)->pit_width ());
last_from_in = *it2;
}
}
}
}
void
Road::build (bool close, int adjusted_segments, double length)
{
narrow_pit_segments ();
set_skews ();
Gl_Road_Segment& first = **m_segments.begin ();
Gl_Road_Segment& last = **(m_segments.end () - 1);
if (close)
{
join (first.start_coords (),
first.start_angle (),
first.start_coords (),
first.start_angle (),
adjusted_segments);
// Force the segment to end at 0, 0.
last.last_segment (true);
}
if (length != 0.0)
set_length (length);
// Make sure the walls join.
last.set_left_width (last.length (), first.left_width (0.0));
last.set_right_width (last.length (), first.right_width (0.0));
build_elevation (m_is_closed);
build_segments (Three_Vector (),
start_direction (),
close ? last.banking ().end_angle () : 0.0);
}
double perpendicular_distance (const Three_Vector& p1,
const Three_Vector& p2,
double angle)
{
return (p2 - p1).magnitude ()
* sin (atan2 (p1.y - p2.y, p1.x - p2.x) - angle);
}
// Force a road that starts at START_COORDS and START_ANGLE, to end at
// END_COORDS and END_ANGLE.
void
Road::join (const Three_Vector& start_coords,
double start_angle,
const Three_Vector& end_coords,
double end_angle,
int adjusted_segments)
{
m_is_closed = true;
if ((adjusted_segments < 0) || (adjusted_segments > 3))
{
std::ostringstream message;
message << "The number of segments to be adjusted (" << adjusted_segments
<< ") is not in the range [0, 3]";
throw Can_Not_Close (message.str ());
}
if (m_segments.size () < size_t (adjusted_segments))
{
std::ostringstream message;
message << "Track has fewer segments (" << m_segments.size()
<< ") than the number of segments to be adjusted ("
<< adjusted_segments << ")";
throw Can_Not_Close (message.str ());
}
if (adjusted_segments == 0)
{
// Call it closed without adjusting anything.
return;
}
Gl_Road_Segment* last_segment = *(m_segments.end () - 1);
Gl_Road_Segment* last_curve = (adjusted_segments > 1)
? *(m_segments.end () - 2)
: last_segment->is_straight () ? 0 : last_segment;
Gl_Road_Segment* other_straight = (adjusted_segments == 3)
? *(m_segments.end () - 3)
: 0;
if ((adjusted_segments > 1)
&& (last_curve->is_straight () || !last_segment->is_straight ()))
{
throw Can_Not_Close ("Track must end with a curve followed by "
"a straight when more than one segment "
"is to be adjusted.");
}
if ((adjusted_segments == 3) && !other_straight->is_straight ())
{
throw Can_Not_Close ("Track must end with a straight, a curve and a "
"straight when three segments are to be adjusted.");
}
// Make the last segment parallel to the first by changing the length of
// the last curve.
double last_arc = 0.0;
if (last_curve != 0)
{
last_arc = last_curve->arc ()
+ branch (end_angle - last_curve->end_angle (), -pi);
last_curve->set_arc (last_arc);
// If we're only adjusting the last curve, we're done.
if (last_segment == last_curve)
return;
}
if (adjusted_segments > 1)
{
// Make the last segment collinear with the first.
const double perp = perpendicular_distance (last_curve->end_coords (),
end_coords,
end_angle);
switch (adjusted_segments)
{
case 2:
{
// Change the radius of the curve.
// last_curve->set_radius (last_curve->radius () + perp / cos (last_arc));
last_curve->set_radius (last_curve->radius ()
+ perp / (1.0 - cos (last_arc)));
break;
}
case 3:
{
// Change the length of segment[-3].
other_straight->set_length (other_straight->length ()
+ perp / sin (last_arc));
break;
}
default:
// 2 and 3 should be the only possibilities in this branch.
assert (false);
}
// Propagate any adjustments to the end of the track.
connect (m_segments.end () - 2);
}
// Extend the last segment to meet the first. Assume they are collinear.
// This is guaranteed for 'adjusted_segments' == 2 or 3 but not 1.
last_segment->set_length ((last_segment->start_coords () - end_coords).magnitude ());
}
void
Road::set_skews ()
{
for (Segment_List::iterator it = m_segments.begin () + 1;
it != m_segments.end ();
it++)
{
double skew = (*it)->start_skew ();
if ((skew != 0.0) && ((*it)->arc () != 0.0))
{
if ((*(it - 1))->arc () == 0.0)
(*(it - 1))->set_end_skew (skew);
if ((*(it + 1))->arc () == 0.0)
(*(it + 1))->set_start_skew (-skew);
}
}
}
// Scale the track to a particular length.
void
Road::set_length (double length)
{
assert (m_segments.size () != 0);
// Find the current length.
double old_length = 0.0;
for (Segment_List::iterator it = m_segments.begin ();
it != m_segments.end ();
it++)
{
old_length += (*it)->length ();
}
assert (old_length != 0.0);
double factor = length / old_length;
// Adjust the segments.
for (Segment_List::iterator it = m_segments.begin ();
it != m_segments.end ();
it++)
{
(*it)->scale (factor);
}
}
void
Road::set_start_direction (double degrees)
{
m_start_direction = branch (deg_to_rad (degrees), 0.0);
if (m_segments.empty ())
return;
Gl_Road_Segment* first = *(m_segments.begin ());
first->set_start_angle (m_start_direction);
connect (m_segments.begin () + 1);
}
void
Road::connect (Segment_List::iterator it)
{
// There's nothing to do to the first segment.
if (it == m_segments.begin ())
it++;
const Gl_Road_Segment* last = *(it - 1);
for (; it != m_segments.end (); it++)
{
(*it)->set_start_angle (last->end_angle ());
(*it)->set_start_coords (last->end_coords ());
last = (*it);
}
}
void
Road::draw ()
{
std::for_each (m_segments.begin (), m_segments.end (),
std::mem_fun (&Gl_Road_Segment::draw));
if (m_draw_racing_line)
m_racing_line.draw ();
}
Three_Vector
Pit_Lane::pit_in_offset (Gl_Road_Segment& pit_in) const
{
double pit_width;
if (pit_in.pit ().side () == LEFT)
pit_width = (*segments ().begin ())->left_width (0.0);
else
pit_width = (*segments ().begin ())->right_width (0.0);
pit_width /= cos (start_direction ());
double along = pit_in.pit ().split_or_join ();
double offset;
if (pit_in.pit ().side () == LEFT)
offset = pit_in.left_width (along) - pit_width;
else
offset = -pit_in.right_width (along) + pit_width;
if (pit_in.radius () == 0.0)
return Three_Vector (along, offset, 0.0).
rotate (pit_in.angle (along) * Three_Vector::Z);
else
return pit_in.center_of_curve () - pit_in.start_coords ()
+ Three_Vector (pit_in.radius () - offset,
pit_in.angle (along) - pi/2);
}
Three_Vector
Pit_Lane::pit_out_offset (Gl_Road_Segment& pit_out) const
{
double pit_width;
const Gl_Road_Segment& last_segment = **(segments ().end () - 1);
if (pit_out.pit ().side () == LEFT)
pit_width = last_segment.left_width (last_segment.length ());
else
pit_width = last_segment.right_width (last_segment.length ());
pit_width /= cos (start_direction ());
double along = pit_out.pit ().split_or_join ();
double offset;
if (pit_out.pit ().side () == LEFT)
offset = pit_out.left_width (along) - pit_width;
else
offset = -pit_out.right_width (along) + pit_width;
if (pit_out.radius () == 0.0)
return Three_Vector (along, offset, 0.0).
rotate (pit_out.angle (along) * Three_Vector::Z);
else
return pit_out.center_of_curve () - pit_out.start_coords ()
+ Three_Vector (pit_out.radius () - offset,
pit_out.angle (along) - pi/2);
}
void
Pit_Lane::build (bool join_to_track,
int adjusted_segments,
Gl_Road_Segment& pit_in,
Gl_Road_Segment& pit_out,
const Spline& track_elevation)
{
if (m_segments.size () == 0) return;
// Skew the ends of the pit lane to meet the track.
set_skews ();
(*m_segments.begin ())->set_start_skew (tan (start_direction ()));
(*(m_segments.end () - 1))->set_end_skew (tan (end_direction ()));
// The ends of the pit lane attach to points on the track. Call
// build_segments() to transform the pit lane's coordinates to the track's
// orientation and pit-in position.
build_elevation (false);
build_segments (pit_in.start_coords () + pit_in_offset (pit_in),
pit_in.pit_angle () + start_direction (),
pit_in.banking ().end_angle ());
// Make the end of the pit lane meet the track's pit-out position.
if (join_to_track)
{
join (pit_in.start_coords () + pit_in_offset (pit_in),
pit_in.pit_angle () + start_direction (),
pit_out.start_coords () + pit_out_offset (pit_out),
pit_out.pit_angle () + end_direction (),
adjusted_segments);
}
// Load the pit lane with elevations from the track.
{
m_length = build_elevation (false);
mp_elevation->clear ();
double in_distance =
pit_in.start_distance () + pit_in.pit ().split_or_join ();
double out_distance =
pit_out.start_distance () + pit_out.pit ().split_or_join ();
double track_length = track_elevation [track_elevation.size () - 1].x;
double delta = wrap (out_distance - in_distance, track_length);
static const int elevations = 10;
for (int i = 0; i < elevations; i++)
{
double along_pit = i * m_length / elevations;
double along_track = wrap (in_distance + i * delta / elevations, track_length);
double z = track_elevation.interpolate (along_track);
mp_elevation->load (along_pit, z);
}
mp_elevation->load (m_length, track_elevation.interpolate (out_distance));
}
// Finalize the elevations and segments.
build_elevation (false);
build_segments (pit_in.start_coords () + pit_in_offset (pit_in),
pit_in.pit_angle () + start_direction (),
pit_in.banking ().end_angle ());
}
//* Class Strip_Track
Strip_Track::Strip_Track () :
mp_track (new Road),
mp_pit_lane (new Pit_Lane),
m_pit_in_index (-1),
m_pit_out_index (-1),
mp_sky_box (0),
mp_map_background (0)
{
m_timing_lines.clear ();
m_cameras.clear ();
}
Strip_Track::~Strip_Track ()
{
delete mp_pit_lane;
delete mp_track;
delete mp_sky_box;
delete mp_map_background;
}
void
Strip_Track::show_racing_line (bool show)
{
if (mp_track)
mp_track->show_racing_line (show);
}
// Read the track definition file.
void
Strip_Track::read (std::string data_dir, std::string track_file)
{
// Remember the file name for re-reading.
if ((data_dir != "") && (track_file != ""))
{
m_data_dir = data_dir;
m_track_file = track_file;
}
mp_track->clear ();
mp_pit_lane->clear ();
m_timing_lines.clear ();
Strip_Track_Reader reader (m_data_dir, m_track_file, this);
}
// Add a straight or curved segment of road to the track.
size_t
Strip_Track::add_segment (Gl_Road_Segment* segment)
{
return mp_track->add_segment (segment);
}
size_t
Strip_Track::add_pit_segment (Gl_Road_Segment* segment)
{
const bool start = (mp_pit_lane->segments ().size () == 0);
const double distance = start ? 0.0 : segment->length ();
const double width =
segment->left_width (distance) + segment->right_width (distance);
const double left_shoulder =
segment->left_width (distance) - segment->left_road_width (distance);
const double right_shoulder =
segment->right_width (distance) - segment->right_road_width (distance);
if (start)
{
mp_track->segments ()[m_pit_in_index]->
set_pit_width (width, left_shoulder, right_shoulder);
}
else
{
mp_track->segments ()[m_pit_out_index]->
set_pit_width (width, left_shoulder, right_shoulder);
}
return mp_pit_lane->add_segment (segment);
}
void
Strip_Track::set_pit_in (size_t index, double angle)
{
m_pit_in_index = index;
mp_pit_lane->set_start_direction (angle);
}
void
Strip_Track::set_pit_out (size_t index, double angle)
{
m_pit_out_index = index;
mp_pit_lane->set_end_direction (angle);
}
// Make the track.
void
Strip_Track::build (bool close,
int adjusted_road_segments,
double track_length,
bool join_pit_lane,
int adjusted_pit_segments)
{
mp_track->build (close, adjusted_road_segments, track_length);
if ((m_pit_in_index != -1) && (m_pit_out_index != -1))
{
Gl_Road_Segment& in = *mp_track->segments ()[m_pit_in_index];
Gl_Road_Segment& out = *mp_track->segments ()[m_pit_out_index];
mp_pit_lane->build (join_pit_lane,
adjusted_pit_segments,
in,
out,
mp_track->elevation ());
double along = in.pit ().split_or_join() + 1.0e-6;
double from_center = in.pit ().side () == RIGHT
? -in.right_width (along)
: in.left_width (along);
m_objects.push_back (Track_Object (position (along + in.start_distance (),
from_center),
in.pit ().side () == RIGHT
? in.right_material (0.0)
: in.left_material (0.0)));
along = out.pit ().split_or_join() - 1.0e-6;;
from_center = out.pit ().side () == RIGHT
? -out.right_width (along)
: out.left_width (along);
m_objects.push_back (Track_Object (position (along + out.start_distance(),
from_center),
in.pit ().side () == RIGHT
? out.right_material (0.0)
: out.left_material (0.0)));
}
}
void
Strip_Track::set_sky_box (std::string sides_image,
std::string top_image,
std::string bottom_image,
bool smooth)
{
delete mp_sky_box;
mp_sky_box = new Sky_Box (100.0,
sides_image,
top_image,
bottom_image,
smooth);
}
void
Strip_Track::set_map_background (std::string background_image,
double x_offset, double y_offset,
double x_size, double y_size)
{
delete mp_map_background;
mp_map_background = new Map_Background (background_image,
x_offset, y_offset,
x_size, y_size);
}
void
Strip_Track::draw_sky (const Three_Vector& view) const
{
mp_sky_box->draw (view);
}
void
Strip_Track::draw_map_background () const
{
if (mp_map_background != 0)
mp_map_background->draw ();
}
void
Strip_Track::draw () const
{
glLoadIdentity ();
mp_track->draw ();
mp_pit_lane->draw ();
}
// Return the new position for a vehicle at POS when a reset is
// performed.
Three_Vector
Strip_Track::reset_position (const Three_Vector& pos,
size_t& road_index,
size_t& segment_index)
{
Three_Vector track_pos = track_coordinates (pos, road_index, segment_index);
track_pos.y = 0.0;
track_pos.z = 0.0;
return track_pos;
}
// Return the new orientation for a vehicle at POS when a reset is
// performed.
Three_Matrix
Strip_Track::reset_orientation (const Three_Vector& pos,
size_t& road_index,
size_t& segment_index)
{
Three_Matrix orientation;
orientation.identity ();
// Align the car's up direction with the normal.
const Three_Vector& track_pos =
track_coordinates (pos, road_index, segment_index);
const Gl_Road_Segment* segment =
get_road (road_index).segments ()[segment_index];
const double along = track_pos.x - segment->start_distance ();
const double across = track_pos.y;
Three_Vector normal = segment->normal (along, across);
orientation.rotate (Three_Vector (-asin (normal.y),
asin (normal.x),
segment->angle (along)));
return orientation;
}
// Return the elevation of the track at the x and y components of POS.
double
Strip_Track::elevation (const Three_Vector& pos,
double x,
double y,
size_t& road_index,
size_t& segment_index)
{
Three_Vector bump = m_material.bump (x, y);
return track_coordinates (pos, road_index, segment_index).z + bump.z;
}
// Return WORLD_POS transformed to the track's coordinate system.
// SEGMENT_INDEX will be modified if the position on another
// segment.
Three_Vector
Strip_Track::track_coordinates (const Three_Vector& world_pos,
size_t& road_index,
size_t& segment_index)
{
// Find the distance along the track, distance from center, and elevation
// for the world coordinates `world_pos.x' and `world_pos.y'.
Three_Vector track_pos;
const Segment_List* segments = &get_road (road_index).segments ();
if (segment_index >= segments->size ())
{
std::cerr << segment_index << ' '
<< segments->size () << ' '
<< road_index << std::endl;
assert (false);
}
Gl_Road_Segment* segment = (*segments)[segment_index];
int direction = 0;
size_t i = 0;
bool found = false;
while (i < segments->size () + 1)
{
double off = segment->coordinates (world_pos, track_pos);
if (off == 0.0)
{
found = true;
break;
}
if ((direction == 1) || ((direction == 0) && (off > 0.0)))
{
direction = 1;
// We're off the end of the current segment. Find a new
// candidate segment.
if ((road_index == 0)
&& (segment_index == size_t (m_pit_in_index))
&& (segment->on_pit_merge (track_pos.x, track_pos.y)))
{
// We've moved onto the pit lane. Try the first segment
// on the pit lane.
road_index = 1;
segment_index = 0;
}
else if ((road_index == 1)
&& (segment_index == mp_pit_lane->segments ().size () - 1))
{
// We've moved off of the pit lane. Try the segment
// where the pit lane joins the track.
road_index = 0;
segment_index = m_pit_out_index;
}
else
{
// Try the next segment.
segment_index++;
if ((road_index == 0) && (segment_index == segments->size ()))
segment_index = 0;
}
}
else
{
direction = -1;
// We're off the beginning of the current segment. Find a new
// candidate segment.
if ((road_index == 0)
&& (segment_index == size_t (m_pit_out_index))
&& (segment->on_pit_merge (track_pos.x, track_pos.y)))
{
// We've moved onto the end of the pit lane. Try the
// last segment on the pit lane.
road_index = 1;
segment_index = mp_pit_lane->segments ().size () - 1;
}
else if ((road_index == 1) && (segment_index == 0))
{
// We've moved off of the beginning of the pit lane.
// Try the segment where the pit lane splits from the
// track.
road_index = 0;
segment_index = m_pit_in_index;
}
else
{
// Try the previous segment.
if ((road_index == 0) && (segment_index == 0))
segment_index = segments->size ();
segment_index--;
}
}
segments = &get_road (road_index).segments ();
segment = (*segments)[segment_index];
i++;
}
// Throw an exception if a segment could not be found.
if (!found)
throw Segment_Not_Found (world_pos, segment_index);
assert (segment_index < segments->size ());
m_material = segment->material_at (track_pos.x, track_pos.y);
track_pos.x += segment->start_distance ();
return track_pos;
}
const Road&
Strip_Track::get_road (size_t road_index) const
{
switch (road_index)
{
case 0: return *mp_track;
case 1: return *mp_pit_lane;
default: assert (false);
}
}
Contact_Info
Strip_Track::test_for_contact (const Three_Vector& pos,
double bump_parameter,
size_t& road_index,
size_t& segment_index)
{
const Three_Vector track_pos = track_coordinates (pos, road_index, segment_index);
const Gl_Road_Segment* segment =
get_road (road_index).segments ()[segment_index];
const double segment_distance = track_pos.x - segment->start_distance ();
bool contact = false;
Three_Vector normal;
// Test for contact with the road.
double diff =
elevation (pos, track_pos.x, track_pos.y, road_index, segment_index) - pos.z;
if (diff >= 0.0)
{
contact = true;
Three_Vector bump = m_material.bump (track_pos.x, track_pos.y);
normal = segment->normal (segment_distance, track_pos.y, bump);
}
// Test for contact with the walls.
// Left
if (!contact)
{
const Material& material = segment->left_material (pos.z);
Three_Vector bump = material.bump (track_pos.x, track_pos.y);
diff = track_pos.y - (segment->left_width (segment_distance) + bump.z);
if (diff >= 0.0)
{
contact = true;
m_material = material;
normal = segment->barrier_normal (segment_distance, track_pos.y, bump);
}
}
// Right
if (!contact)
{
const Material& material = segment->right_material (pos.z);
Three_Vector bump = material.bump (track_pos.x, track_pos.y);
diff = -track_pos.y - (segment->right_width (segment_distance) + bump.z);
if (diff >= 0.0)
{
contact = true;
m_material = material;
normal = segment->barrier_normal (segment_distance, track_pos.y, bump);
}
}
return Contact_Info (contact, diff, normal, m_material);
}
Three_Vector
Strip_Track::position (double along, double from_center) const
{
return mp_track->position (along, from_center);
}
// Return the timing sector at the given distance.
int
Strip_Track::sector (double distance)
{
for (size_t i = 0; i < m_timing_lines.size (); i++)
{
if (m_timing_lines [i] > distance)
return i;
}
return m_timing_lines.size ();
}
void
Strip_Track::set_start_direction (double degrees)
{
m_start_direction = deg_to_rad (degrees);
mp_track->set_start_direction (degrees);
}
void
Strip_Track::add_camera (const Camera& camera)
{
m_cameras.push_back (camera);
}
double
Strip_Track::camera_range (const Camera& camera) const
{
double range = mp_track->segments ()[camera.segment_index]->start_distance ()
+ camera.position.x
- camera.range;
return wrap (range, mp_track->length ());
}
Three_Vector
Strip_Track::camera_position (const Camera& camera) const
{
const Gl_Road_Segment& segment = *(mp_track->segments ()[camera.segment_index]);
return segment.position (camera.position.x, camera.position.y)
+ Three_Vector (0.0, 0.0, camera.position.z);
}
const Camera&
Strip_Track::get_camera (double distance) const
{
if (m_cameras.empty ())
return s_default_camera;
// See if we're near the end of the track and should be picked up by
// the first camera.
double first = m_cameras.begin ()->position.x - m_cameras.begin ()->range;
if (mp_track->is_closed ()
&& (first < 0.0)
&& (distance > wrap (first, mp_track->length ())))
return *m_cameras.begin ();
for (std::vector <Camera>::const_reverse_iterator rit = m_cameras.rbegin ();
rit != m_cameras.rend ();
rit++)
if (distance > camera_range (*rit))
return *rit;
return *m_cameras.begin ();
}
Three_Vector
Strip_Track::camera_target (const Camera& camera) const
{
double angle =
mp_track->segments ()[camera.segment_index]->angle (camera.position.x);
return camera_position (camera)
+ Three_Vector (-sin (deg_to_rad (camera.direction.x) + angle),
cos (deg_to_rad (camera.direction.x) + angle),
sin (deg_to_rad (camera.direction.y)));
}
const Rectangle&
Strip_Track::bounds () const
{
return Rectangle (mp_track->bounds ()).enclose (mp_pit_lane->bounds ());
}
Three_Vector Strip_Track::grid_position (int place, int total, bool pit) const
{
assert (place > 0); // 1-based
assert (place <= total);
static const double grid_interval = (pit ? 12.0 : 8.0);
// Put the 1st car 1 interval from the beginning of the 1st segment to avoid
// putting off the end.
double across = pit
? 1.5 * mp_track->left_road_width (0.0)
: 3.0 * std::pow (-1, place);
return Three_Vector (grid_interval * (total - place + 1), across, 0.0);
}