-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMarkup.h
More file actions
463 lines (413 loc) · 15.7 KB
/
Copy pathMarkup.h
File metadata and controls
463 lines (413 loc) · 15.7 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
/* 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/ */
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>
#include <limits>
// glm
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/string_cast.hpp>
#include "pugixml/pugixml.hpp"
#ifdef _DEBUG
#pragma comment(lib, "pugixml/Debug/pugixml.lib")
#else
#pragma comment(lib, "pugixml/Release/pugixml.lib")
#endif
#include "Utility.h"
#include "Camera.h"
#include "Shader.h"
#include "Model.h"
#include "Ocean.h"
struct sMark
{
wstring name;
vec3 pos;
wstring colour;
int boyshp;
int bcnshp;
int cardinal;
int lateral;
int landmark;
int mooring;
int idxModel = -1;
double time;
vec3 lightColor;
bool isInViewFrustum;
vec3 waterPosition;
};
// File must have Lon and Lat with dots nor decimal
class Markup
{
public:
Markup(wstring fullname)
{
// Load zones from XML file
LoadMarksFromXML(fullname.c_str());
// Initialiser le générateur aléatoire avec un seed unique (ici avec l'heure)
std::mt19937 rng(static_cast<unsigned int>(time(nullptr)));
std::uniform_real_distribution<double> dist(0.0, 8.0); // Cycle max = 8 secondes
// Pour chaque bouée, initialiser un décalage de temps aléatoire
for (auto& mark : mvMarks)
mark.time = dist(rng); // décalage aléatoire en secondes
// Load the different buoys
mModel[0] = make_unique<Model>("Resources/Buoys/Boy-North.gltf");
mModel[1] = make_unique<Model>("Resources/Buoys/Boy-East.gltf");
mModel[2] = make_unique<Model>("Resources/Buoys/Boy-South.gltf");
mModel[3] = make_unique<Model>("Resources/Buoys/Boy-West.gltf");
mModel[4] = make_unique<Model>("Resources/Buoys/Boy-Portside.gltf");
mModel[5] = make_unique<Model>("Resources/Buoys/Boy-Starboard.gltf");
mModel[6] = make_unique<Model>("Resources/Buoys/Boy-Danger.gltf");
mModel[7] = make_unique<Model>("Resources/Buoys/Bcn-North.gltf");
mModel[8] = make_unique<Model>("Resources/Buoys/Bcn-East.gltf");
mModel[9] = make_unique<Model>("Resources/Buoys/Bcn-South.gltf");
mModel[10] = make_unique<Model>("Resources/Buoys/Bcn-West.gltf");
mModel[11] = make_unique<Model>("Resources/Buoys/Bcn-Portside.gltf");
mModel[12] = make_unique<Model>("Resources/Buoys/Bcn-Starboard.gltf");
mModel[13] = make_unique<Model>("Resources/Buoys/Bcn-Danger.gltf");
mModel[14] = make_unique<Model>("Resources/Buoys/Boy-Mooring.gltf");
vvPatterns = {
// Buoy
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0}, // N
{1, 0, 1, 0, 1, 0, 0, 0}, // E
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0}, // S
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0}, // W
{1, 1, 0, 0},
{0, 1, 1, 0},
{1, 0, 1, 0, 0, 0, 0, 0},
// Beacon
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0}, // N
{1, 0, 1, 0, 1, 0, 0, 0}, // E
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0}, // S
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0}, // W
{1, 1, 0, 0},
{0, 1, 1, 0},
{1, 0, 1, 0, 0, 0, 0, 0},
// Mooring
{1, 0, 1, 0},
};
mShader = make_unique<Shader>("Resources/Misc/sun.vert", "Resources/Misc/sun.frag");
mShaderLight = make_unique<Shader>("Resources/Misc/light.vert", "Resources/Misc/light.frag");
InitBillboard();
};
~Markup() {};
void Render(Camera& camera, Ocean* ocean, Sky* sky)
{
if (!bVisible)
return;
// Check the visibility of all the marks
for (auto& mark : mvMarks)
{
if (camera.IsInViewFrustum(mark.pos))
{
mark.isInViewFrustum = true;
if (mark.boyshp || mark.mooring)
ocean->GetVerticeXYZ(mark.pos, mark.waterPosition);
}
else
mark.isInViewFrustum = false;
}
// Render the visible marks
mShader->use(); // Misc/sun.vert, Misc/sun.frag
mShader->setVec3("light.position", sky->SunPosition);
mShader->setVec3("light.ambient", sky->SunAmbient);
mShader->setVec3("light.diffuse", sky->SunDiffuse);
mShader->setVec3("light.specular", sky->SunSpecular);
mShader->setVec3("viewPos", camera.GetPosition());
mShader->setFloat("exposure", sky->Exposure * 2.0f);
mShader->setBool("bAbsorbance", sky->bAbsorbance);
mShader->setVec3("absorbanceColor", sky->AbsorbanceColor);
mShader->setFloat("absorbanceCoeff", sky->AbsorbanceCoeff);
mShader->setMat4("view", camera.GetView());
mShader->setMat4("projection", camera.GetProjection());
for (auto& mark : mvMarks)
{
if (mark.isInViewFrustum)
{
vec3 p;
if (mark.boyshp > 0 || mark.mooring > 0)
p = mark.waterPosition;
else
p = mark.pos;
mat4 model = glm::translate(mat4(1.0f), p);
mShader->setMat4("model", model);
mModel[mark.idxModel]->Render(*mShader);
}
}
}
void RenderLights(Camera& camera, bool bLights = false)
{
if (!bLights)
return;
glBindVertexArray(mQuadVAO);
mShaderLight->use(); // Misc/light.vert, Misc/light.frag
mShaderLight->setMat4("view", camera.GetView());
mShaderLight->setMat4("projection", camera.GetProjection());
for (auto& mark : mvMarks)
{
if (mark.isInViewFrustum)
{
int patternLength = static_cast<int>(vvPatterns[mark.idxModel].size());
int currentIndex = static_cast<int>(floor((glfwGetTime() + mark.time) * 2)) % patternLength;
bool lightOn = vvPatterns[mark.idxModel][currentIndex] == 1;
if (!lightOn)
continue;
vec3 p;
if (mark.boyshp > 0 || mark.mooring > 0)
p = mark.waterPosition;
else
p = mark.pos;
if (mark.boyshp)
p.y += 4.2f;
else if (mark.bcnshp)
p.y += 4.5f;
else if (mark.mooring)
p.y += 1.4f;
mat4 model = glm::translate(mat4(1.0f), p);
// Cylindrical billboard - the quad is always facing the camera
vec3 camRight = vec3(camera.GetView()[0][0], camera.GetView()[1][0], camera.GetView()[2][0]);
vec3 camUp = vec3(camera.GetView()[0][1], camera.GetView()[1][1], camera.GetView()[2][1]);
model[0] = vec4(camRight, 0.0f);
model[1] = vec4(camUp, 0.0f);
float scale = 5.0f;
model = glm::scale(model, vec3(scale, scale, scale));
mShaderLight->setMat4("model", model);
mShaderLight->setVec3("lightColor", mark.lightColor);
mShaderLight->setFloat("lightIntensity", 1.0f);
mShaderLight->setFloat("starIntensity", 0.1f);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
}
glBindVertexArray(0);
}
bool bVisible = true;
private:
void LoadMarksFromXML(const wstring filename)
{
mvMarks.clear();
pugi::xml_document doc;
// Load XML file
pugi::xml_parse_result result = doc.load_file(filename.c_str());
if (result)
{
// Get the root node
pugi::xml_node root = doc.child(L"Marks");
// Browse all "Mark" nodes
for (pugi::xml_node markNode : root.children(L"Mark"))
{
sMark mark;
// Read the data for each mark
mark.name = markNode.child(L"Name").text().as_string();
mark.colour = markNode.child(L"Colour").text().as_string();
mark.boyshp = markNode.child(L"Boyshp").attribute(L"value").as_int();
mark.bcnshp = markNode.child(L"Bcnshp").attribute(L"value").as_int();
mark.cardinal = markNode.child(L"Cardinal").attribute(L"value").as_int();
mark.lateral = markNode.child(L"Lateral").attribute(L"value").as_int();
mark.landmark = markNode.child(L"Landmark").attribute(L"value").as_int();
mark.mooring = markNode.child(L"Mooring").attribute(L"value").as_int();
float lat = markNode.child(L"Latitude").attribute(L"value").as_float();
float lon = markNode.child(L"Longitude").attribute(L"value").as_float();
mark.pos = lonlat_to_opengl(lon, lat);
if (mark.boyshp > 0)
{
if (mark.cardinal == 1) mark.idxModel = 0; // Buoy - North
else if (mark.cardinal == 2) mark.idxModel = 1; // Buoy - East
else if (mark.cardinal == 3) mark.idxModel = 2; // Buoy - South
else if (mark.cardinal == 4) mark.idxModel = 3; // Buoy - West
else if (mark.lateral == 1) mark.idxModel = 4; // Buoy - Portside
else if (mark.lateral == 2) mark.idxModel = 5; // Buoy - Starboard
else if (mark.colour == L"2,3,2") mark.idxModel = 6;// Buoy - Danger
}
if (mark.bcnshp > 0)
{
if (mark.cardinal == 1) mark.idxModel = 7; // Beacon - North
else if (mark.cardinal == 2) mark.idxModel = 8; // Beacon - East
else if (mark.cardinal == 3) mark.idxModel = 9; // Beacon - South
else if (mark.cardinal == 4) mark.idxModel = 10; // Beacon - West
else if (mark.lateral == 1) mark.idxModel = 11; // Beacon - Portside
else if (mark.lateral == 2) mark.idxModel = 12; // Beacon - Starboard
else if (mark.colour == L"2,3,2") mark.idxModel = 13;// Buoy - Danger
}
if (mark.mooring > 0)
mark.idxModel = 14;// Buoy - Mooring
switch (mark.idxModel)
{
case 4: // Buoy-Portside
case 11: // Bcn-Portside
mark.lightColor = vec3(1.0f, 0.0f, 0.0f); break; // Red
case 5: // Buoy-Starboard
case 12: // Bcn-Starboard
mark.lightColor = vec3(0.0f, 1.0f, 0.0f); break; // Green
case 6: // Buoy-Danger
case 13: // Bcn-Danger
case 14: // Boy-Mooring
mark.lightColor = vec3(1.0f, 1.0f, 0.0f); break; // Yellow
default: // Cardinals
mark.lightColor = vec3(1.0f, 1.0f, 1.0f); break; // White
}
if(mark.idxModel != -1)
mvMarks.push_back(mark);
}
}
else
{
// Handle file loading error
std::wcerr << L"Error loading XML file: " << result.description() << std::endl;
}
}
void 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);
}
unique_ptr<Shader> mShader;
unique_ptr<Model> mModel[15];
vector<sMark> mvMarks;
vector<vector<int>> vvPatterns;
GLuint mQuadVAO = 0;
unique_ptr<Shader> mShaderLight;
};
/*
File lut.json in BALISAGE.z
{
"BCNSHP": {
"0": "Forme de balise",
"1": "Perche, jalon, pieu",
"2": "Branche",
"3": "Tourelle",
"4": "Balise à treillis",
"5": "Balise pilier",
"6": "Cairn",
"7": "Balise à flotteurs"
},
"BOYSHP": {
"0": "Forme de bouée",
"1": "Cônique",
"2": "Cylindrique",
"3": "Sphérique",
"4": "Pylône, charpente",
"5": "Espar, fuseau",
"6": "Bouée tonne",
"7": "Super-bouée/bouée géante",
"8": "Pour région glaciaire"
},
"BUISHP": {
"0": "Forme de bâtiment",
"1": "{no specific shape},",
"2": "{tower},",
"3": "{spire},",
"4": "{cupola (dome) },",
"5": "Immeuble haut",
"6": "Pyramide",
"7": "Cylindrique",
"8": "Sphérique",
"9": "Cubique",
"701": "UNKNOWN",
"703": "Not Applicable",
"704": "Autre"
},
"CATCAM": {
"0": "Catégorie de marque cardinale",
"1": "Marque cardinale nord",
"2": "Marque cardinale est",
"3": "Marque cardinale sud",
"4": "Marque cardinale ouest",
"701": "UNKNOWN",
"703": "Not Applicable"
},
"CATLAM": {
"0": "Catégorie de marque latérale",
"1": "Latérale bâbord",
"2": "Latérale tribord",
"3": "Chenal préféré à tribord",
"4": "Chenal préféré à bâbord",
"701": "UNKNOWN",
"703": "Not Applicable"
},
"CATLMK": {
"0": "Catégorie d'amers",
"1": "Cairn",
"2": "Cimetière",
"3": "Cheminée",
"4": "Antenne à réflecteur",
"5": "Mât de pavillon",
"6": "Torchère",
"7": "Mât, pilier",
"8": "Manche à air",
"9": "Monument",
"10": "Colonne",
"11": "Plaque commémorative",
"12": "Obélisque",
"13": "Statue",
"14": "Croix",
"15": "Dôme",
"16": "Antenne radar",
"17": "Tour",
"18": "Moulin à vent",
"19": "Eolienne",
"20": "Flèche/minaret",
"21": "Rocher/bloc rocheux à terre",
"22": "rock pinnacle",
"701": "UNKNOWN",
"702": "Multiple",
"703": "Not Applicable",
"704": "Autre"
},
"CATMOR": {
"0": "Catégorie de dispositif d'amarrage",
"1": "Duc d'Albe/dauphin",
"2": "Duc d'Albe pour la régulation des compas",
"3": "Bollard",
"4": "Mur d'amarrage",
"5": "Poteau/pilier",
"6": "Chaine/câble",
"7": "Coffres ou bouées d'amarrage",
"501": "fast patrol boat waiting position",
"701": "UNKNOWN",
"703": "Not Applicable",
"704": "Autre"
},
"COLOUR": {
"0": "Couleur",
"1": "Blanc",
"2": "Noir",
"3": "Rouge",
"4": "Vert",
"5": "Bleu",
"6": "Jaune",
"7": "Gris",
"8": "Marron",
"9": "Ambre",
"10": "Violet",
"11": "Orange",
"12": "Magenta ",
"13": "Rose",
"701": "UNKNOWN",
"702": "Multiple",
"703": "Not Applicable",
"704": "Autre"
},
}*/