-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathShip.cpp
More file actions
4282 lines (3619 loc) · 158 KB
/
Ship.cpp
File metadata and controls
4282 lines (3619 loc) · 158 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
/* SimShip by Edouard Halbert
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
http://creativecommons.org/licenses/by-nc-nd/4.0/ */
#include "Ship.h"
#include "MeshPlaneIntersect.hpp" // Used to find a contour
#include <math.h>
#include "clipper/clipper.h" // Used to offset a contour
#ifdef _DEBUG
#pragma comment(lib, "clipper/Debug/clipper.lib")
#else
#pragma comment(lib, "clipper/Release/clipper.lib")
#endif
using namespace Clipper2Lib;
//#define DEBUG_SMOKE // Update smoke.comp with 2 lines: layout(std430, binding = 1) buffer CounterBuffer { int liveCounter; }; atomicAdd(liveCounter, 1);
extern float g_TWS_Kn;
extern float g_TWS_Deg;
extern vec2 g_Wind;
extern SoundManager * g_SoundMgr;
extern bool g_bPause;
extern Camera g_Camera;
extern bool g_bShipShadow;
extern bool g_bShowShipForcesWindows;
GLuint TexContourShip = 0; // Texture of the contour of the ship
int TexContourShipW;
int TexContourShipH;
GLuint TexWakeBuffer = 0;
int TexWakeBufferSize = 512;
GLuint TexWakeVao = 0;
int TexWakeVaoSize = 1024;
bool bTexWakeByVAO = true;
GLuint TexShadowMap = 0;
mat4 LightViewProjection;
Ship::~Ship()
{
BBoxShape.reset();
mSpray.reset();
glDeleteVertexArrays(1, &mVaoHull);
glDeleteBuffers(1, &mVboHull);
glDeleteVertexArrays(1, &mVaoLines);
mModelFull.reset();
mPropeller1.reset();
mRudder.reset();
mLight.reset();
mRadar1.reset();
mRadar2.reset();
mShaderHullColored.reset();
mShaderWireframe.reset();
mShaderPressure.reset();
mShaderCamera.reset();
mShaderShipShadow.reset();
mShaderUnicolor.reset();
mShaderLight.reset();
mShaderSmokeCompute.reset();
mTexEnvironment.reset();
mForceVector.reset();
mForceApplication.reset();
mAxis.reset();
mSoundThrust1.reset();
mSoundThrust2.reset();
mSoundBowThruster.reset();
mSoundSternThruster.reset();
glDeleteVertexArrays(1, &mVaoWake);
glDeleteBuffers(1, &mVboWake);
mShaderWakeVao.reset();
mTexWake.reset();
mvVertices.clear();
mvVertices.resize(0);
mvTris.clear();
mvTris.resize(0);
mvVertexColored.clear();
mvVertexColored.resize(0);
mvVertSubmerged.clear();
mvVertSubmerged.resize(0);
mvVertWaterHeight.clear();
mvVertWaterHeight.resize(0);
mvWaterPos.clear();
mvWaterPos.resize(0);
glDeleteVertexArrays(1, &mVaoContour1);
glDeleteBuffers(1, &mVboContour1);
}
void Ship::SetOcean(Ocean* ocean)
{
mOcean = ocean;
pDisplacement = ocean->GetPixelsDisplacement();
};
void Ship::Init(sShip& ship, Camera& camera)
{
//CreateKelvinImages();
chrono.start();
this->ship = ship;
// Read the mvVertices and the faces
igl::readOBJ(ship.PathnameHull.c_str(), mV, mF);
stringstream ssHull;
ssHull << "Hull: " << mV.rows() << " vertices & " << mF.rows() << " faces" << endl;
// Vertices
mvVertices.resize(mV.rows());
mvVertSubmerged.resize(mV.rows());
mvVertWaterHeight.resize(mV.rows());
mWorld = mat4(1.0f);
TransformVertices();
// Get data
InitDimensions();
UpdateWorldMatrix(); // Necessary for several calculations to come
mvTris.resize(mF.rows());
InitTriangles(); // Create the list of the triangles
InitCentroid(); // Compute the centre of the volume
InitSurfaces(); // Certain surfaces
InitInertia(); // Compute volume & all moments of inertia (Ixx, Iyy, Izz, Ixy, Ixz, Iyz)
// Info to display with interface
ssHull << "Length/Width : " << std::fixed << std::setprecision(2) << mLength << " m x " << mWidth << " m " << endl;
ssHull << "Draft : " << std::fixed << std::setprecision(2) << mDraft << " m" << endl;
ssHull << "Mass : " << std::setprecision(0) << int(ship.Mass_t) << " t" << endl;
InfoHull = ssHull.str();
InitWaterVertices(); // Create the list of water vertices in the reference patch
InitVaoHull(); // Create the VAO of the colored hull
InitContours();
InitShaders();
InitTextures();
InitVaoWake();
InitModels();
InitSounds(camera);
InitSmoke();
mSpray = make_unique<Spray>();
ResetVelocities();
bMotion = false;
bSound = true;
#ifdef TRACE
InitTrace();
#endif
InitBillboard();
mArchimede.Name = "Archimede";
mGravity.Name = "Gravity";
mHeaveDrag.Name = "Heave Drag";
mThrust1.Name = "Thrust1";
mThrust2.Name = "Thrust2";
mPropDrag1.Name = "Prop Drag 1";
mPropDrag2.Name = "Prop Drag 2";
mPropTorque1.Name = "Prop Torque 1";
mPropTorque2.Name = "Prop Torque 2";
mViscousDrag.Name = "Viscous Drag";
mWavesDrag.Name = "Waves Drag";
mResidualDrag.Name = "Residual Drag";
mBowThrust.Name = "Bow Thrust";
mSternThrust.Name = "Stern Thrust";
mRudderLift.Name = "Rudder Lift";
mRudderDrag.Name = "Rudder Drag";
mWindTorque.Name = "Wind Rotation";
mWindFrontDrag.Name = "Wind Front";
mWindRearDrag.Name = "Wind Rear";
mAirDrag.Name = "Air Drag";
mCentrifugalTorque.Name = "Centrifugal";
}
void Ship::InitDimensions()
{
mModelFull = make_unique<Model>(ship.PathnameFull);
mBbox = mModelFull->GetBoundingBox();
mMass = ship.Mass_t * 1000.0f; // t -> kg for all physical calculations
mPowerW = ship.PowerkW * 1000.0f; // kW -> W for all physical calculations
if (ship.nPropeller == 2)
mPowerW *= 0.5f;
mLength = fabs(mBbox.max.x - mBbox.min.x); // Overall length
mWidth = fabs(mBbox.max.z - mBbox.min.z); // Overall width
mHeight = fabs(mBbox.max.y - mBbox.min.y); // Overall height
if (mLength < mWidth) std::swap(mLength, mWidth);
mDraft = -mBbox.min.y; // Below the water level
mAirDraft = mBbox.max.y; // Above the water level
mBow = vec3(mBbox.max.x, 0.0f, 0.0f); // Distance to the centre
mStern = vec3(mBbox.min.x, 0.0f, 0.0f); // Distance to the centre
mWakePivot = vec3(mBbox.min.x + 0.5f * mWidth, 0.0f, 0.0f);
mRudderArea = (mLength * mDraft * 0.01f) * (1.0f + 0.25f * (mWidth / mDraft) * (mWidth / mDraft)); // DNV2 formula for the area of the rudder in m²
}
void Ship::InitTriangles()
{
sTriangle tri;
for (int i = 0; i < mF.rows(); ++i)
{
tri.I[0] = mF(i, 0);
tri.I[1] = mF(i, 1);
tri.I[2] = mF(i, 2);
vec3 u = mvVertices[tri.I[1]] - mvVertices[tri.I[0]];
vec3 v = mvVertices[tri.I[2]] - mvVertices[tri.I[0]];
vec3 a = glm::cross(v, u);
tri.Area = 0.5 * sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
tri.Normal = glm::normalize(a);
mvTris[i] = tri;
}
}
void Ship::InitCentroid()
{
mCentroid = vec3(0.0f);
for (const auto& tri : mvTris)
mCentroid += mvVertices[tri.I[0]] + mvVertices[tri.I[1]] + mvVertices[tri.I[2]];
if (mvTris.size())
mCentroid /= (mvTris.size() * 3.0f);
#ifdef PROPERTIES
cout << "Centroid : ( " << mCentroid.x << ", " << mCentroid.y << ", " << mCentroid.z << " )" << endl;
#endif
}
bool IsPolygonClockwise(const Clipper2Lib::Path64& polygon)
{
int64_t sum = 0;
int n = (int)polygon.size();
for (int i = 0; i < n; ++i)
{
int j = (i + 1) % n;
sum += (polygon[j].x - polygon[i].x) * (polygon[j].y + polygon[i].y);
}
return sum > 0; // true si dans le sens horaire (clockwise)
}
void Ship::InitSurfaces()
{
// Area
mAreaXZ = mLength * mWidth;
// Cube root of the area in the XZ plane
mAreaXZ_RacCub = pow(mAreaXZ, 1.0f / 3.0f);
// Wet area
mAreaWettedMax = 0.0f;
for (auto& tri : mvTris)
mAreaWettedMax += tri.Area;
#ifdef PROPERTIES
cout << "Surface XZ : " << mAreaXZ << " m2" << endl;
cout << "Surface : " << mAreaWettedMax << " m2" << endl;
#endif
if (ship.mAreaFront != 0.0f && ship.mAreaLat != 0.0f)
{
mAreaFront = ship.mAreaFront;
mAreaFrontCenter = ship.mAreaFrontCenter;
mAreaLat = ship.mAreaLat;
mAreaLatCenter = ship.mAreaLatCenter;
return;
}
vector<Mesh>& vMeshes = mModelFull->GetMesh();
// Get the vertices
Clipper2Lib::Paths64 projectedFront;
Clipper2Lib::Paths64 projectedLat;
double scale = 1e4;
double scale2 = scale * scale;
for (const auto& mesh : vMeshes)
{
for (size_t i = 0; i < mesh.vIndices.size(); i += 3)
{
const sVertex& v0 = mesh.vVertices[mesh.vIndices[i]];
const sVertex& v1 = mesh.vVertices[mesh.vIndices[i + 1]];
const sVertex& v2 = mesh.vVertices[mesh.vIndices[i + 2]];
Clipper2Lib::Path64 triFront;
triFront.push_back({ (int64_t)(v0.Position.y * scale), (int64_t)(v0.Position.z * scale) });
triFront.push_back({ (int64_t)(v1.Position.y * scale), (int64_t)(v1.Position.z * scale) });
triFront.push_back({ (int64_t)(v2.Position.y * scale), (int64_t)(v2.Position.z * scale) });
projectedFront.push_back(triFront);
Clipper2Lib::Path64 triLat;
triLat.push_back({ (int64_t)(v0.Position.x * scale), (int64_t)(v0.Position.y * scale) });
triLat.push_back({ (int64_t)(v1.Position.x * scale), (int64_t)(v1.Position.y * scale) });
triLat.push_back({ (int64_t)(v2.Position.x * scale), (int64_t)(v2.Position.y * scale) });
projectedLat.push_back(triLat);
}
}
// Reverse the clockwise polygons
for (auto& poly : projectedFront)
if (IsPolygonClockwise(poly))
std::reverse(poly.begin(), poly.end());
for (auto& poly : projectedLat)
if (IsPolygonClockwise(poly))
std::reverse(poly.begin(), poly.end());
// Area front
Clipper2Lib::Path64 clipPolygonFront;
clipPolygonFront.push_back({ (int64_t)0, numeric_limits<int64_t>::min() / 2 });
clipPolygonFront.push_back({ numeric_limits<int64_t>::max() / 2, numeric_limits<int64_t>::min() / 2 });
clipPolygonFront.push_back({ numeric_limits<int64_t>::max() / 2, numeric_limits<int64_t>::max() / 2 });
clipPolygonFront.push_back({ (int64_t)0, numeric_limits<int64_t>::max() / 2 });
Clipper2Lib::Clipper64 clipperFront;
clipperFront.AddSubject(projectedFront);
clipperFront.AddClip(Clipper2Lib::Paths64{ clipPolygonFront });
Clipper2Lib::Paths64 solutionFront;
clipperFront.Execute(Clipper2Lib::ClipType::Intersection, Clipper2Lib::FillRule::NonZero, solutionFront);
// Area lateral
Clipper2Lib::Path64 clipPolygonLat;
clipPolygonLat.push_back({ (int64_t)0, numeric_limits<int64_t>::min() / 2 });
clipPolygonLat.push_back({ numeric_limits<int64_t>::max() / 2, numeric_limits<int64_t>::min() / 2 });
clipPolygonLat.push_back({ numeric_limits<int64_t>::max() / 2, numeric_limits<int64_t>::max() / 2 });
clipPolygonLat.push_back({ (int64_t)0, numeric_limits<int64_t>::max() / 2 });
Clipper2Lib::Clipper64 clipperLat;
clipperLat.AddSubject(projectedLat);
clipperLat.AddClip(Clipper2Lib::Paths64{ clipPolygonLat });
Clipper2Lib::Paths64 solutionLat;
clipperLat.Execute(Clipper2Lib::ClipType::Intersection, Clipper2Lib::FillRule::NonZero, solutionLat);
// Geometric front center
for (const auto& poly : solutionFront)
{
double area_poly = Clipper2Lib::Area(poly) / scale2;
if (area_poly < 1e-12) continue;
double cx_poly = 0.0;
double cy_poly = 0.0;
int n = (int)poly.size();
for (int i = 0; i < n; ++i)
{
int j = (i + 1) % n;
double xi = (double)poly[i].x / scale;
double yi = (double)poly[i].y / scale;
double xj = (double)poly[j].x / scale;
double yj = (double)poly[j].y / scale;
double factor = (xi * yj - xj * yi);
cx_poly += (xi + xj) * factor;
cy_poly += (yi + yj) * factor;
}
cx_poly /= (6.0 * area_poly);
cy_poly /= (6.0 * area_poly);
mAreaFrontCenter.y += cx_poly * area_poly;
mAreaFrontCenter.z += cy_poly * area_poly;
mAreaFront += area_poly;
}
if (mAreaFront > 0)
{
mAreaFrontCenter.y /= mAreaFront;
mAreaFrontCenter.z /= mAreaFront;
}
// Geometric lateral center
for (const auto& poly : solutionLat)
{
double area_poly = Clipper2Lib::Area(poly) / (scale * scale);
if (area_poly < 1e-12) continue;
double cx_poly = 0.0;
double cy_poly = 0.0;
int n = (int)poly.size();
for (int i = 0; i < n; ++i)
{
int j = (i + 1) % n;
double xi = (double)poly[i].x / scale;
double yi = (double)poly[i].y / scale;
double xj = (double)poly[j].x / scale;
double yj = (double)poly[j].y / scale;
double factor = (xi * yj - xj * yi);
cx_poly += (xi + xj) * factor;
cy_poly += (yi + yj) * factor;
}
cx_poly /= (6.0 * area_poly);
cy_poly /= (6.0 * area_poly);
mAreaLatCenter.x += cx_poly * area_poly;
mAreaLatCenter.y += cy_poly * area_poly;
mAreaLat += area_poly;
}
if (mAreaLat > 0)
{
mAreaLatCenter.x /= mAreaLat;
mAreaLatCenter.y /= mAreaLat;
}
//cout << "total_area_front : " << AreaFront << " ";
//PrintGlmVec3(AreaFrontCenter);
//cout << "total_area_lat : " << AreaLat << " ";
//PrintGlmVec3(AreaLatCenter);
//cout << endl;
}
void Ship::InitInertia()
{
mVolume = 0.0f; // m3
mIxx = 0.0f; // kg.m2
mIyy = 0.0f;
mIzz = 0.0f;
mIxy = 0.0f;
mIxz = 0.0f;
mIyz = 0.0f;
// Calculation of total volume and moments of inertia
for (const auto& tri : mvTris)
{
vec3 a = mvVertices[tri.I[0]];
vec3 b = mvVertices[tri.I[1]];
vec3 c = mvVertices[tri.I[2]];
// Calcul du volume signé de ce tétraèdre (méthode 1)
double volume = ( a.x * b.y * c.z + a.y * b.z * c.x + b.x * c.y * a.z - c.x * b.y * a.z - b.x * a.y * c.z - c.y * b.z * a.x ) / 6.0;
mVolume += volume;
// Calcul des moments d'inertie, comme précédemment
mIxx += (a.y * a.y + b.y * b.y + c.y * c.y + a.y * b.y + b.y * c.y + c.y * a.y + a.z * a.z + b.z * b.z + c.z * c.z + a.z * b.z + b.z * c.z + c.z * a.z) * volume / 10.0;
mIyy += (a.x * a.x + b.x * b.x + c.x * c.x + a.x * b.x + b.x * c.x + c.x * a.x + a.z * a.z + b.z * b.z + c.z * c.z + a.z * b.z + b.z * c.z + c.z * a.z) * volume / 10.0;
mIzz += (a.x * a.x + b.x * b.x + c.x * c.x + a.x * b.x + b.x * c.x + c.x * a.x + a.y * a.y + b.y * b.y + c.y * c.y + a.y * b.y + b.y * c.y + c.y * a.y) * volume / 10.0;
mIxy += (2 * a.x * a.y + 2 * b.x * b.y + 2 * c.x * c.y + a.x * b.y + a.x * c.y + b.x * a.y + b.x * c.y + c.x * a.y + c.x * b.y) * volume / 20.0;
mIxz += (2 * a.x * a.z + 2 * b.x * b.z + 2 * c.x * c.z + a.x * b.z + a.x * c.z + b.x * a.z + b.x * c.z + c.x * a.z + c.x * b.z) * volume / 20.0;
mIyz += (2 * a.y * a.z + 2 * b.y * b.z + 2 * c.y * c.z + a.y * b.z + a.y * c.z + b.y * a.z + b.y * c.z + c.y * a.z + c.y * b.z) * volume / 20.0;
}
if (mVolume != 0.0f)
{
// Calculation of density (mass in kg, volume in m3)
double densite = mMass / mVolume;
// Adjustment of moments of inertia relative to the barycenter
mIxx = densite * mIxx - mMass * (ship.PosGravity.y * ship.PosGravity.y + ship.PosGravity.z * ship.PosGravity.z);
mIyy = densite * mIyy - mMass * (ship.PosGravity.x * ship.PosGravity.x + ship.PosGravity.z * ship.PosGravity.z);
mIzz = densite * mIzz - mMass * (ship.PosGravity.x * ship.PosGravity.x + ship.PosGravity.y * ship.PosGravity.y);
mIxy = densite * mIxy + mMass * ship.PosGravity.x * ship.PosGravity.y;
mIxz = densite * mIxz + mMass * ship.PosGravity.x * ship.PosGravity.z;
mIyz = densite * mIyz + mMass * ship.PosGravity.y * ship.PosGravity.z;
}
#ifdef PROPERTIES
// Displaying results
cout << "Volume : " << mVolume << " m3" << endl;
cout << "============================" << endl;
cout << "Moments d'inertie volumiques" << endl;
cout << "Volume total : " << mVolume << " m3" << endl;
cout << "Ixx = " << mIxx << " kg/m2" << endl;
cout << "Iyy = " << mIyy << " kg/m2" << endl;
cout << "Izz = " << mIzz << " kg/m2" << endl;
cout << "Ixy = " << mIxy << " kg/m2" << endl;
cout << "Ixz = " << mIxz << " kg/m2" << endl;
cout << "Iyz = " << mIyz << " kg/m2" << endl;
cout << endl;
#endif
}
void Ship::InitWaterVertices()
{
// Positions
for (int z = 0; z <= mOcean->MESH_SIZE; ++z)
{
vector<vec3> vPos;
for (int x = 0; x <= mOcean->MESH_SIZE; ++x)
{
int index = z * mOcean->MESH_SIZE_1 + x;
vec3 v;
v.x = (x - mOcean->MESH_SIZE / 2.0f) * mOcean->PATCH_SIZE / mOcean->MESH_SIZE;
v.y = 0.0f;
v.z = (z - mOcean->MESH_SIZE / 2.0f) * mOcean->PATCH_SIZE / mOcean->MESH_SIZE;
vPos.push_back(v);
}
mvWaterPos.push_back(vPos);
}
}
void Ship::InitVaoHull()
{
// Converting mvVertices, Normals and Colors
mvVertexColored = vector<float>(mvTris.size() * 3 * 6);
int index = 0;
for (const auto& tri : mvTris)
{
for (int j = 0; j < 3; ++j)
{
// Position
mvVertexColored[index++] = mvVertices[tri.I[j]].x; // x
mvVertexColored[index++] = mvVertices[tri.I[j]].y; // y
mvVertexColored[index++] = mvVertices[tri.I[j]].z; // z
// Color
mvVertexColored[index++] = tri.Color.r; // r
mvVertexColored[index++] = tri.Color.g; // g
mvVertexColored[index++] = tri.Color.b; // b
}
}
// Generation of mvIndices
vector<unsigned int> indices(mF.rows() * 3);
for (unsigned int i = 0; i < indices.size(); ++i)
indices[i] = i;
mIndicesFull = indices.size();
glGenVertexArrays(1, &mVaoHull);
glGenBuffers(1, &mVboHull);
glGenBuffers(1, &mEboHull);
glBindVertexArray(mVaoHull);
glBindBuffer(GL_ARRAY_BUFFER, mVboHull);
glBufferData(GL_ARRAY_BUFFER, mvVertexColored.size() * sizeof(float), mvVertexColored.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEboHull);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
// Configuring vertex attributes
// Position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Ship::InitContours()
{
// Contour
vector<vec3> contour = ComputeContour(); // Intersect the mesh with the ocean (Y = 0)
if (contour.size() == 0)
return;
contour = ArrangeContour(contour); // Sort the points
CreateContourVAO1(contour); // Create the first contour which has the size of the ship
vector<vec3> contourSpray = OffsetContour(contour, 0.05f);
InitSpray(contourSpray);
contour = OffsetContour(contour, 1.0f); // Expand the contour with a constant offset
CreateContourVAO2(contour); // Create the second contour which is expanded
CreateTexWake(contour); // Create the texture formed with foam inside the exapnded contour
}
void Ship::InitShaders()
{
// Shaders for the ship
mShaderHullColored = make_unique<Shader>("Resources/Ship/hull_colored.vert", "Resources/Ship/hull_colored.frag"); // For the hull (colored triangles for Archimede)
mShaderWireframe = make_unique<Shader>("Resources/Misc/unicolor.vert", "Resources/Misc/unicolor.frag", "Resources/Misc/unicolor.geom");
mShaderPressure = make_unique<Shader>("Resources/Misc/unicolor.vert", "Resources/Misc/unicolor.frag"); // For the forces of pressure (lines)
mShaderCamera = make_unique<Shader>("Resources/Misc/camera.vert", "Resources/Misc/camera.frag"); // For the ship (the camera is the sun)
mShaderShipShadow = make_unique<Shader>("Resources/Ship/ship_shadow.vert", "Resources/Ship/ship_shadow.frag"); // Enhanced shader for the ship model
mShaderShip = make_unique<Shader>("Resources/Ship/ship.vert", "Resources/Ship/ship.frag");
mShaderUnicolor = make_unique<Shader>("Resources/Misc/unicolor.vert", "Resources/Misc/unicolor.frag"); // For bounding box & contours
mShaderLight = make_unique<Shader>("Resources/Misc/light.vert", "Resources/Misc/light.frag"); // For the navigation lights of the ship
mShaderShadow = make_unique<Shader>("Resources/Ship/shadow.vert", "Resources/Ship/shadow.frag"); // For the shadow depth map
mShaderStencil = make_unique<Shader>("Resources/Misc/stencil.vert", "Resources/Misc/stencil.frag"); // For the stencil (windows of the ship)
mShaderShipShadow->use();
mShaderShipShadow->setInt("texture_diffuse1", 1);
mShaderShipShadow->setInt("shadowMap", 2);
// Shaders for the wake
mShaderBuffer = make_unique<Shader>("Resources/Ship/wake_buffer.vert", "Resources/Ship/wake_buffer.frag"); // Wake as an accumulation buffer
mShaderWakeVaoToTex = make_unique<Shader>("Resources/Ship/wake_vao.vert", "Resources/Ship/wake_vao.frag"); // Wake as a projection of VAO on texture
mShaderGaussH = make_unique<Shader>("Resources/Ship/wake_gauss.vert", "Resources/Ship/wake_gauss_h.frag"); // Gaussian blur - horizontal pass (1)
mShaderGaussV = make_unique<Shader>("Resources/Ship/wake_gauss.vert", "Resources/Ship/wake_gauss_v.frag"); // Gaussian blur - vertical pass (2)
mShaderWakeVao = make_unique<Shader>("Resources/Misc/unicolor.vert", "Resources/Misc/unicolor.frag"); // For the drawing of the vao (as a debug)
}
void Ship::InitTextures()
{
mTexWake = make_unique<Texture>();
mTexWake->CreateFromFile("Resources/Ocean/seamless-seawater-with-foam-1.jpg");
glBindTexture(GL_TEXTURE_2D, mTexWake->id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8);
// W A K E ===========================================================
// Create accumulation buffer for the wake
glGenTextures(2, mTexBuffer);
glGenFramebuffers(2, FBO_BUFFER);
for (int i = 0; i < 2; ++i)
{
glBindTexture(GL_TEXTURE_2D, mTexBuffer[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, TexWakeBufferSize, TexWakeBufferSize, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindFramebuffer(GL_FRAMEBUFFER, FBO_BUFFER[i]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexBuffer[i], 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout << "Error creating FBO for the wake drawing" << endl;
}
mScreenQuadWakeBuffer = make_unique<ScreenQuad>();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Creation of the multisample texture (no mipmaps or filters)
glGenTextures(1, &msTexWakeVAO);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msTexWakeVAO);
int samples = 4;
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_R8, TexWakeVaoSize, TexWakeVaoSize, GL_TRUE);
// Multisample FBO creation
glGenFramebuffers(1, &msFBO_WAKE);
glBindFramebuffer(GL_FRAMEBUFFER, msFBO_WAKE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, msTexWakeVAO, 0);
// Creating a classic FBO with simple texture for resolution (blit)
glGenFramebuffers(1, &FBO_BLIT_WAKE);
glBindFramebuffer(GL_FRAMEBUFFER, FBO_BLIT_WAKE);
glGenTextures(1, &TexBlit);
glBindTexture(GL_TEXTURE_2D, TexBlit);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, TexWakeVaoSize, TexWakeVaoSize, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, TexBlit, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout << "Error creating FBO for the wake post processing" << endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// FBO and texture for the 2 passes of the gaussian blur (horizontal and vertical)
glGenTextures(1, &mTexGauss1);
glBindTexture(GL_TEXTURE_2D, mTexGauss1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, TexWakeVaoSize, TexWakeVaoSize, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenFramebuffers(1, &FBO_GAUSS1);
glBindFramebuffer(GL_FRAMEBUFFER, FBO_GAUSS1);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexGauss1, 0);
glGenTextures(1, &TexWakeVao);
glBindTexture(GL_TEXTURE_2D, TexWakeVao);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, TexWakeVaoSize, TexWakeVaoSize, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenFramebuffers(1, &FBO_GAUSS2);
glBindFramebuffer(GL_FRAMEBUFFER, FBO_GAUSS2);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, TexWakeVao, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout << "Error creating FBO for the Gauss passes of the wake" << endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// S H A D O W ===========================================================
glGenFramebuffers(1, &FBO_SHADOW);
glGenTextures(1, &TexShadowMap);
glBindTexture(GL_TEXTURE_2D, TexShadowMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_SIZE, SHADOW_SIZE, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
glBindFramebuffer(GL_FRAMEBUFFER, FBO_SHADOW);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, TexShadowMap, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout << "Error creating FBO for the Shadow pass" << endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Create environment map
mTexEnvironment = make_unique<Texture>();
mTexEnvironment->CreateFromDDSFile("Resources/Ocean/ocean_env.dds");
}
void Ship::InitVaoWake()
{
glGenVertexArrays(1, &mVaoWake);
glGenBuffers(1, &mVboWake);
glBindVertexArray(mVaoWake);
glBindBuffer(GL_ARRAY_BUFFER, mVboWake);
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
// position (3 floats)
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(sFoamVertex), (void*)offsetof(sFoamVertex, pos));
// alpha (1 float)
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, sizeof(sFoamVertex), (void*)offsetof(sFoamVertex, alpha));
glBindVertexArray(0);
}
void Ship::InitModels()
{
// The ship
stringstream ssFull;
ssFull << mModelFull->NbVertices << " vertices & " << mModelFull->NbFaces << " faces" << endl;
InfoFull = ssFull.str();
if (ship.PathnamePropeller1.length())
mPropeller1 = make_unique<Model>(ship.PathnamePropeller1);
if (ship.PathnamePropeller2.length())
mPropeller2 = make_unique<Model>(ship.PathnamePropeller2);
if (ship.PathnameRudder.length())
mRudder = make_unique<Model>(ship.PathnameRudder);
if (ship.PathnameRadar1.length())
mRadar1 = make_unique<Model>(ship.PathnameRadar1);
if (ship.nRadar > 1 && ship.PathnameRadar2.length())
mRadar2 = make_unique<Model>(ship.PathnameRadar2);
if (ship.bFlag)
{
float spacing = ship.DimXFlag / 15.0f;
mFlag = make_unique<Flag>(15, 10, spacing, ship.PathnameFlag.c_str());
}
// Others
BBoxShape = make_unique<BBox>(mBbox.min, mBbox.max);
BBoxShape->bVisible = false;
mForceVector = make_unique<Cube>();
mForceApplication = make_unique<Sphere>(0.1f, 16);
mAxis = make_unique<Cube>();
mLight = make_unique<Sphere>(0.1f, 16);
}
void Ship::InitSounds(Camera& camera)
{
// Sounds
g_SoundMgr->setListenerPosition(camera.GetPosition());
g_SoundMgr->setListenerOrientation(camera.GetAt(), camera.GetUp());
// Power 1
mSoundThrust1 = make_unique<Sound>(ship.ThrustSound);
mSoundThrust1->setVolume(0.25f);
mSoundThrust1->setPosition(ship.Position + ship.PosPropeller1);
mSoundThrust1->setLooping(true);
mSoundThrust1->adjustDistances();
// Power 2
mSoundThrust2 = make_unique<Sound>(ship.ThrustSound);
mSoundThrust2->setVolume(0.25f);
mSoundThrust2->setPosition(ship.Position + ship.PosPropeller2);
mSoundThrust2->setLooping(true);
mSoundThrust2->adjustDistances();
if (bSound)
{
mSoundThrust1->play();
mSoundThrust2->play();
}
// Bow thruster
if (ship.HasBowThruster)
{
mSoundBowThruster = make_unique<Sound>(ship.BowThrusterSound);
mSoundBowThruster->setVolume(0.25f);
mSoundBowThruster->setPosition(ship.Position + ship.PosBowThruster);
mSoundBowThruster->setLooping(true);
mSoundBowThruster->adjustDistances();
}
// Stern thruster
if (ship.HasSternThruster)
{
mSoundSternThruster = make_unique<Sound>(ship.SternThrusterSound);
mSoundSternThruster->setVolume(0.25f);
mSoundSternThruster->setPosition(ship.Position + ship.PosSternThruster);
mSoundSternThruster->setLooping(true);
mSoundSternThruster->adjustDistances();
}
}
void Ship::InitSmoke()
{
// Initialize array of "dead" particles
vector<ParticleGPU> particles(mSmokeMaxParticles);
for (int i = 0; i < mSmokeMaxParticles; ++i)
particles[i].life = 0.0f; // dead at the start
// Generate and allocate SSBO
glGenBuffers(1, &mSSBO_SMOKE);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, mSSBO_SMOKE);
glBufferData(GL_SHADER_STORAGE_BUFFER, mSmokeMaxParticles * sizeof(ParticleGPU), particles.data(), GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, mSSBO_SMOKE);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
// VAO setup for drawing points
glGenVertexArrays(1, &mVaoSmoke);
glBindVertexArray(mVaoSmoke);
// No attributes needed, data will be read into vertex shader via SSBO
glBindVertexArray(0);
#ifdef DEBUG_SMOKE
glGenBuffers(1, &SSBO_LIVE_COUNTER);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO_LIVE_COUNTER);
int zero = 0;
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int), &zero, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
#endif
mShaderSmokeCompute = make_unique<Shader>("", "", "", "Resources/Ship/smoke.comp");
mShaderSmokeRender = make_unique<Shader>("Resources/Ship/smoke.vert", "Resources/Ship/smoke.frag");
}
void FilterClosePoints(std::vector<sSprayPt>& pts)
{
if (pts.size() < 2)
return;
float totalDist = glm::length(pts.front().p - pts.back().p);
float threshold = totalDist / pts.size();
// nouvelle liste filtrée
std::vector<sSprayPt> filtered;
filtered.reserve(pts.size());
filtered.push_back(pts[0]); // toujours garder le premier point
vec3 lastPos = pts[0].p;
for (size_t i = 1; i < pts.size(); ++i)
{
float dist = glm::length(pts[i].p - lastPos);
if (dist >= threshold)
{
filtered.push_back(pts[i]);
lastPos = pts[i].p;
}
// sinon on ignore le point trop proche
}
pts = std::move(filtered);
}
void Ship::InitSpray(vector<vec3>& contour)
{
if (contour.size() == 0)
return;
float maxForward = contour[0].x;
int frontIndex = 0;
for (int i = 1; i < (int)contour.size(); i++)
{
if (contour[i].x > maxForward)
{
maxForward = contour[i].x;
frontIndex = i;
}
}
vec3 frontPoint = contour[frontIndex];
mLeft.clear();
mRight.clear();
float dist = mLength * ship.SprayLength;
for (size_t i = 0; i < contour.size(); ++i)
{
vec3 p = contour[i];
float d = glm::length(p - frontPoint);
if (d <= dist)
{
// Find the point before and the point after on the contour
int idxPrev = (i == 0) ? (int)contour.size() - 1 : (int)i - 1;
int idxNext = (i == contour.size() - 1) ? 0 : (int)i + 1;
vec3 prev = contour[idxPrev];
vec3 next = contour[idxNext];
// Tangent vector (direction of the contour at point p)
vec3 tangent = glm::normalize(next - prev);
// For the x/z plane: take the outside
vec3 toPrev = prev - p;
vec3 toNext = next - p;
// Lateral vector in the plane (here (toNext - toPrev))
vec3 lateral = toNext - toPrev;
// Normal: perpendicular to tangent, oriented outward
vec3 n = glm::normalize(glm::cross(tangent, vec3(0, 1, 0)));
// We want n.z to have the same sign as p.z
if ((p.z < 0.0f && n.z < 0.0f) || (p.z > 0.0f && n.z > 0.0f))
{
// n already on the right side
}
else
n = -n;
sSprayPt pt;
pt.p = p;
pt.n = n;
if (p.z < 0.0f)
mLeft.push_back(pt);
else if (p.z > 0.0f)
mRight.push_back(pt);
}
}
// Comparator to sort in ascending order of distance on the X axis from frontPoint
auto compareNearToFarX = [frontPoint](const sSprayPt& a, const sSprayPt& b) {
float distA = frontPoint.x - a.p.x; // the "distance" in x from frontPoint
float distB = frontPoint.x - b.p.x;
return distA < distB;
};
std::sort(mLeft.begin(), mLeft.end(), compareNearToFarX);
std::sort(mRight.begin(), mRight.end(), compareNearToFarX);
FilterClosePoints(mLeft);
FilterClosePoints(mRight);
if (mLeft.size() > 1 && mRight.size() > 1)
{
mRandomOffsetRange = 0.0f;
for (size_t i = 0; i < mLeft.size() - 1; ++i)
mRandomOffsetRange += glm::length(mLeft[i + 1].p - mLeft[i].p);
for (size_t i = 0; i < mRight.size() - 1; ++i)
mRandomOffsetRange += glm::length(mRight[i + 1].p - mRight[i].p);
mRandomOffsetRange /= (mLeft.size() - 1 + mRight.size() - 1);
mRandomOffsetRange *= 0.5f;
}
}
void Ship::InitBillboard()
{
float quadVertices[] = {
// positions // UVs
-0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 1.0f, 0.0f,
};
GLuint quadVBO = 0;
glGenVertexArrays(1, &mQuadVAO);
glGenBuffers(1, &quadVBO);
glBindVertexArray(mQuadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW);
// position attribute
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
// UV attribute
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glBindVertexArray(0);
}
GLuint Ship::GetTraceID()
{
#ifdef TRACE
if (mTexTrace[mTraceIdx])
return mTexTrace[mTraceIdx];
#endif
return 0;
}
void Ship::InitTrace()
{
mTexTrace = new unsigned int[2];
glGenTextures(2, mTexTrace);
for (unsigned int i = 0; i < 2; i++)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexTrace[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, TEX_SIZE, TEX_SIZE, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
}
mShaderTrace = make_unique<Shader>("", "", "", "Resources/Ship/trace.comp");
}
void Ship::UpdateTrace()
{