-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
796 lines (669 loc) · 20.2 KB
/
main.cpp
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
// main.cpp
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <random>
// Include ImGui
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
// Include custom headers
#include "Camera.h"
#include "Shader.h"
// Forward declarations
void processInput(GLFWwindow* window);
void renderScene(Shader& shaderProgram);
void parseInputData(const std::string& data);
void parseOBJData(const std::string& data);
void renderGUI();
void fitDataIntoView();
struct Vector3 {
float x, y, z;
};
struct Vertex {
glm::vec3 position;
glm::vec3 normal;
};
struct Primitive {
std::string name;
std::string type; // "drawtriangle", "drawline", "drawpoint", "overlaymesh"
std::vector<Vertex> vertices;
std::vector<unsigned int> indices; // For indexed drawing (overlaymesh)
glm::vec4 color;
};
void renderPrimitives(Shader& shaderProgram, const std::vector<Primitive>& primitives);
struct Frame {
std::vector<Primitive> primitives;
};
std::vector<Frame> frames;
std::vector<Primitive> overlayPrimitives;
int currentFrameIndex = 0;
Camera camera;
bool fitView = true;
// Create a random number generator and distribution
std::mt19937 rng(std::random_device{}());
std::uniform_real_distribution<float> colorDist(0.0f, 1.0f);
void parseOBJData(const std::string& data) {
// At the beginning of parseOBJData()
static GLuint overlayVAO = 0, overlayVBO = 0, overlayEBO = 0;
if (overlayVAO != 0) {
glDeleteVertexArrays(1, &overlayVAO);
glDeleteBuffers(1, &overlayVBO);
glDeleteBuffers(1, &overlayEBO);
overlayVAO = overlayVBO = overlayEBO = 0;
}
std::istringstream stream(data);
std::string line;
std::vector<Vertex> vertices; // Combined positions and normals
std::vector<unsigned int> indices;
std::vector<glm::vec3> objPositions; // Positions from 'v' lines
std::vector<std::vector<unsigned int>> faceIndices; // Indices for each face
// Clear previous overlay data
overlayPrimitives.clear();
while (std::getline(stream, line)) {
// Remove comments
size_t commentPos = line.find('#');
if (commentPos != std::string::npos) {
line = line.substr(0, commentPos);
}
std::istringstream linestream(line);
std::string prefix;
linestream >> prefix;
if (prefix == "v") {
// Vertex position
float x, y, z;
linestream >> x >> y >> z;
objPositions.push_back(glm::vec3(x, y, z));
}
else if (prefix == "f") {
// Face definition
std::vector<unsigned int> face;
std::string vertexStr;
while (linestream >> vertexStr) {
std::istringstream vertexStream(vertexStr);
std::string indexStr;
std::getline(vertexStream, indexStr, '/');
int index = std::stoi(indexStr);
face.push_back(index - 1); // OBJ indices start at 1
}
faceIndices.push_back(face);
}
// Ignore other prefixes (e.g., 'vn', 'vt')
}
// Calculate normals and build the vertex and index arrays
for (const auto& face : faceIndices) {
if (face.size() < 3) continue; // Skip degenerate faces
// Triangulate faces with more than 3 vertices
for (size_t i = 1; i < face.size() - 1; ++i) {
// Get vertex positions
glm::vec3 v0 = objPositions[face[0]];
glm::vec3 v1 = objPositions[face[i]];
glm::vec3 v2 = objPositions[face[i + 1]];
// Calculate face normal
glm::vec3 normal = glm::normalize(glm::cross(v1 - v0, v2 - v0));
// Create vertices with positions and normals
Vertex vertex0 = { v0, normal };
Vertex vertex1 = { v1, normal };
Vertex vertex2 = { v2, normal };
// Add vertices and indices
unsigned int indexOffset = vertices.size();
vertices.push_back(vertex0);
vertices.push_back(vertex1);
vertices.push_back(vertex2);
indices.push_back(indexOffset);
indices.push_back(indexOffset + 1);
indices.push_back(indexOffset + 2);
}
}
// Store the mesh data in the overlayPrimitives (as a single primitive)
Primitive meshPrim;
meshPrim.type = "overlaymesh";
meshPrim.name = "Overlay Mesh";
meshPrim.vertices = vertices;
meshPrim.indices = indices; // We need to add indices to the Primitive structure
// We can set a fixed color or leave it empty as we'll use lighting
overlayPrimitives.push_back(meshPrim);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
// Prevent division by zero
if (height == 0) height = 1;
glViewport(0, 0, width, height);
}
int main() {
// Initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW.\n";
return -1;
}
// OpenGL version 3.3 Core Profile
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// For MacOS
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// Create window
GLFWwindow* window = glfwCreateWindow(1280, 720, "Scene Debugger", NULL, NULL);
if (!window) {
std::cerr << "Failed to create GLFW window.\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
// Set viewport
glViewport(0, 0, 1280, 720);
// Set framebuffer size callback
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// Set window user pointer to the camera instance
glfwSetWindowUserPointer(window, &camera);
// Set scroll callback
glfwSetScrollCallback(window, Camera::scroll_callback);
// Initialize GLEW
glewExperimental = GL_TRUE;
GLenum err = glewInit();
// Ignore invalid enum error from glewInit
glGetError();
if (err != GLEW_OK) {
std::cerr << "Failed to initialize GLEW.\n";
return -1;
}
// Set up Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
// Set ImGui style
ImGui::StyleColorsDark();
// Initialize ImGui backend
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
// Enable depth testing
glEnable(GL_DEPTH_TEST);
// Load shaders
Shader shaderProgram("vertex_shader.glsl", "fragment_shader.glsl");
// Main loop
while (!glfwWindowShouldClose(window)) {
// Input handling
processInput(window);
// Start ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// GUI code
renderGUI();
// Rendering
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use shader program
shaderProgram.use();
// Set camera matrices
int width, height;
glfwGetFramebufferSize(window, &width, &height);
if (height == 0) height = 1; // Prevent division by zero
float aspectRatio = width / (float)height;
glm::mat4 projection = camera.getProjectionMatrix(aspectRatio, camera.nearPlane, camera.farPlane);
glm::mat4 view = camera.getViewMatrix();
glm::mat4 model = glm::mat4(1.0f); // Identity matrix
shaderProgram.setMat4("projection", projection);
shaderProgram.setMat4("view", view);
shaderProgram.setMat4("model", model);
// Set lighting uniforms
glm::vec3 lightPos = camera.target + glm::vec3(0.0f, 10.0f, 10.0f);
glm::vec3 viewPos = camera.getPosition();
shaderProgram.setVec3("lightPos", lightPos);
shaderProgram.setVec3("viewPos", viewPos);
// Render 3D scene
renderScene(shaderProgram);
// Render ImGui
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Swap buffers and poll events
glfwSwapBuffers(window);
glfwPollEvents();
}
// Cleanup ImGui and GLFW
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
// Process input
void processInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
camera.processInput(window);
}
// Render GUI
void renderGUI() {
ImGui::Begin("Controls");
if (ImGui::Button("Paste from Clipboard")) {
const char* clipboard = glfwGetClipboardString(NULL);
if (clipboard) {
frames.clear();
parseInputData(std::string(clipboard));
currentFrameIndex = 0;
fitView = true;
}
}
if (ImGui::Button("Paste OBJ from Clipboard as Overlay")) {
const char* clipboard = glfwGetClipboardString(NULL);
if (clipboard) {
parseOBJData(std::string(clipboard));
fitView = true;
}
}
if (ImGui::Button("Clear Overlay")) {
overlayPrimitives.clear();
fitView = true;
}
if (ImGui::Button("Clear Frames")) {
frames.clear();
currentFrameIndex = 0;
fitView = true;
}
if (!frames.empty()) {
if (ImGui::SliderInt("Frame", ¤tFrameIndex, 0, frames.size() - 1)) {
// optional: fitView = true;
}
ImGui::Text("Primitives:");
for (size_t i = 0; i < frames[currentFrameIndex].primitives.size(); ++i) {
const auto& prim = frames[currentFrameIndex].primitives[i];
ImGui::BulletText("%s %zu (%s)", prim.name.c_str(), i, prim.type.c_str());
}
}
else {
ImGui::Text("No frames loaded.");
}
ImGui::End();
}
// Render the scene
void renderScene(Shader& shaderProgram) {
if (frames.empty() && overlayPrimitives.empty()) return;
if (fitView) {
fitDataIntoView();
fitView = false;
}
// Render current frame primitives
if (!frames.empty()) {
const Frame& frame = frames[currentFrameIndex];
renderPrimitives(shaderProgram, frame.primitives);
}
// Render overlay primitives
if (!overlayPrimitives.empty()) {
renderPrimitives(shaderProgram, overlayPrimitives);
}
}
void renderPrimitives(Shader& shaderProgram, const std::vector<Primitive>& primitives) {
for (const auto& prim : primitives) {
if (prim.vertices.empty()) continue;
// Special handling for overlay mesh
if (prim.type == "overlaymesh") {
static GLuint overlayVAO = 0, overlayVBO = 0, overlayEBO = 0;
static size_t numIndices = 0;
if (overlayVAO == 0) {
glGenVertexArrays(1, &overlayVAO);
glGenBuffers(1, &overlayVBO);
glGenBuffers(1, &overlayEBO);
glBindVertexArray(overlayVAO);
glBindBuffer(GL_ARRAY_BUFFER, overlayVBO);
glBufferData(GL_ARRAY_BUFFER, prim.vertices.size() * sizeof(Vertex), prim.vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, overlayEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, prim.indices.size() * sizeof(unsigned int), prim.indices.data(), GL_STATIC_DRAW);
// Vertex positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glEnableVertexAttribArray(0);
// Vertex normals
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glEnableVertexAttribArray(1);
numIndices = prim.indices.size();
glBindVertexArray(0);
}
shaderProgram.setBool("useLighting", true);
shaderProgram.setVec4("primitiveColor", glm::vec4(0.7f, 0.7f, 0.7f, 1.0f));
glBindVertexArray(overlayVAO);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
else {
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
std::vector<float> vertices;
vertices.reserve(prim.vertices.size() * 3);
for (const auto& v : prim.vertices) {
vertices.push_back(v.position.x);
vertices.push_back(v.position.y);
vertices.push_back(v.position.z);
}
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
// Vertex positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
shaderProgram.setBool("useLighting", false);
// Set the primitive color uniform
shaderProgram.setVec4("primitiveColor", prim.color);
// Draw the primitive
if (prim.type == "drawtriangle" || prim.type == "overlaytriangle") {
glDrawArrays(GL_TRIANGLES, 0, 3);
}
else if (prim.type == "drawline" || prim.type == "overlayline") {
glDrawArrays(GL_LINES, 0, 2);
}
else if (prim.type == "drawpoint") {
glPointSize(5.0f); // Adjust the size as needed
glDrawArrays(GL_POINTS, 0, (GLsizei)prim.vertices.size());
}
// Cleanup
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
}
}
}
// Updated parseInputData to handle optional RGBA color bracket
void parseInputData(const std::string& data) {
std::string::const_iterator it = data.begin();
Frame currentFrame;
bool inFrame = false;
auto parseOptionalColor = [&](glm::vec4& color) {
// Skip whitespace
while (it != data.end() && std::isspace(*it)) ++it;
// If the next character is '[', parse RGBA
if (it != data.end() && *it == '[') {
++it; // skip '['
float rgba[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
std::string numStr;
int index = 0;
while (it != data.end() && *it != ']') {
if ((std::isdigit(*it) || *it == '.' || *it == '-')) {
numStr += *it;
}
else if (*it == ',') {
if (!numStr.empty()) {
float val = std::stof(numStr);
rgba[index] = val;
numStr.clear();
++index;
if (index > 3) break; // in case too many
}
}
++it;
}
// Capture the last value (A)
if (!numStr.empty() && index < 4) {
float val = std::stof(numStr);
rgba[index] = val;
numStr.clear();
}
color = glm::vec4(rgba[0], rgba[1], rgba[2], rgba[3]);
if (it != data.end()) ++it; // skip ']'
}
else {
// No bracket found, use random color
color = glm::vec4(colorDist(rng), colorDist(rng), colorDist(rng), 1.0f);
}
};
while (it != data.end()) {
// Skip whitespace
while (it != data.end() && std::isspace(*it)) ++it;
// Check for framestart()
if (std::distance(it, data.end()) >= 11 && std::equal(it, it + 11, "framestart(")) {
inFrame = true;
currentFrame = Frame();
it += 11;
}
// Check for frameend()
else if (std::distance(it, data.end()) >= 9 && std::equal(it, it + 9, "frameend(")) {
if (inFrame) {
frames.push_back(currentFrame);
currentFrame = Frame();
inFrame = false;
}
it += 9;
}
// Check for drawtriangle
else if (std::distance(it, data.end()) >= 12 && std::equal(it, it + 12, "drawtriangle")) {
it += 12; // Move iterator past "drawtriangle"
Primitive prim;
prim.type = "drawtriangle";
// Skip whitespace
while (it != data.end() && std::isspace(*it)) ++it;
// Parse name (optional quotes)
if (it != data.end() && *it == '"') {
++it; // skip quote
std::string name;
while (it != data.end() && *it != '"') {
name += *it;
++it;
}
if (it != data.end()) ++it; // skip closing quote
prim.name = name;
}
else {
prim.name = "Unnamed Triangle";
}
// Parse 3 vertices
std::vector<Vertex> vertices;
for (int i = 0; i < 3; ++i) {
while (it != data.end() && *it != '[') ++it;
if (it == data.end()) break;
++it; // skip '['
Vertex vert{};
std::string numStr;
int coordIndex = 0;
while (it != data.end() && *it != ']') {
if (std::isdigit(*it) || *it == '.' || *it == '-') {
numStr += *it;
}
else if (*it == ',') {
if (!numStr.empty()) {
float val = std::stof(numStr);
if (coordIndex == 0) vert.position.x = val;
else if (coordIndex == 1) vert.position.y = val;
numStr.clear();
++coordIndex;
}
}
++it;
}
// last coordinate
if (!numStr.empty()) {
float val = std::stof(numStr);
vert.position.z = val;
numStr.clear();
}
vertices.push_back(vert);
if (it != data.end()) ++it; // skip ']'
}
prim.vertices = vertices;
// Parse optional color
parseOptionalColor(prim.color);
currentFrame.primitives.push_back(prim);
}
// Check for drawline
else if (std::distance(it, data.end()) >= 8 && std::equal(it, it + 8, "drawline")) {
it += 8;
Primitive prim;
prim.type = "drawline";
// Skip whitespace
while (it != data.end() && std::isspace(*it)) ++it;
// Parse name
if (it != data.end() && *it == '"') {
++it;
std::string name;
while (it != data.end() && *it != '"') {
name += *it;
++it;
}
if (it != data.end()) ++it;
prim.name = name;
}
else {
prim.name = "Unnamed Line";
}
// Parse 2 vertices
std::vector<Vertex> vertices;
for (int i = 0; i < 2; ++i) {
while (it != data.end() && *it != '[') ++it;
if (it == data.end()) break;
++it; // skip '['
Vertex vert{};
std::string numStr;
int coordIndex = 0;
while (it != data.end() && *it != ']') {
if (std::isdigit(*it) || *it == '.' || *it == '-') {
numStr += *it;
}
else if (*it == ',') {
if (!numStr.empty()) {
float val = std::stof(numStr);
if (coordIndex == 0) vert.position.x = val;
else if (coordIndex == 1) vert.position.y = val;
numStr.clear();
++coordIndex;
}
}
++it;
}
// last coordinate
if (!numStr.empty()) {
float val = std::stof(numStr);
vert.position.z = val;
numStr.clear();
}
vertices.push_back(vert);
if (it != data.end()) ++it; // skip ']'
}
prim.vertices = vertices;
// Parse optional color
parseOptionalColor(prim.color);
currentFrame.primitives.push_back(prim);
}
// Check for drawpoint
else if (std::distance(it, data.end()) >= 9 && std::equal(it, it + 9, "drawpoint")) {
it += 9;
Primitive prim;
prim.type = "drawpoint";
// Skip whitespace
while (it != data.end() && std::isspace(*it)) ++it;
// Parse name
if (it != data.end() && *it == '"') {
++it;
std::string name;
while (it != data.end() && *it != '"') {
name += *it;
++it;
}
if (it != data.end()) ++it;
prim.name = name;
}
else {
prim.name = "Unnamed Point";
}
// Parse single vertex
while (it != data.end() && *it != '[') ++it;
if (it != data.end()) ++it; // skip '['
Vertex vtx{};
{
std::string numStr;
int coordIndex = 0;
while (it != data.end() && *it != ']') {
if (std::isdigit(*it) || *it == '.' || *it == '-') {
numStr += *it;
}
else if (*it == ',') {
if (!numStr.empty()) {
float val = std::stof(numStr);
if (coordIndex == 0) vtx.position.x = val;
else if (coordIndex == 1) vtx.position.y = val;
numStr.clear();
++coordIndex;
}
}
++it;
}
// last coordinate
if (!numStr.empty()) {
float val = std::stof(numStr);
vtx.position.z = val;
numStr.clear();
}
}
vtx.normal = glm::vec3(0.0f, 0.0f, 1.0f);
if (it != data.end()) ++it; // skip ']'
prim.vertices.push_back(vtx);
// Parse optional color
parseOptionalColor(prim.color);
currentFrame.primitives.push_back(prim);
}
// framestart / frameend or unknown text
else {
if (it != data.end()) ++it;
}
}
if (inFrame) {
// If there's an unclosed frame, push it at the end (optional)
frames.push_back(currentFrame);
}
}
// Fit data into view
void fitDataIntoView() {
if (frames.empty() && overlayPrimitives.empty()) return;
glm::vec3 minBounds(FLT_MAX);
glm::vec3 maxBounds(-FLT_MAX);
// Include current frame primitives
if (!frames.empty()) {
const Frame& frame = frames[currentFrameIndex];
for (const auto& prim : frame.primitives) {
for (const auto& vert : prim.vertices) {
glm::vec3 position = vert.position;
minBounds = glm::min(minBounds, position);
maxBounds = glm::max(maxBounds, position);
}
}
}
// Include overlay primitives
for (const auto& prim : overlayPrimitives) {
if (prim.type == "overlaymesh") {
for (const auto& vertex : prim.vertices) {
glm::vec3 position = vertex.position;
minBounds = glm::min(minBounds, position);
maxBounds = glm::max(maxBounds, position);
}
}
else {
for (const auto& vert : prim.vertices) {
glm::vec3 position(vert.position.x, vert.position.y, vert.position.z);
minBounds = glm::min(minBounds, position);
maxBounds = glm::max(maxBounds, position);
}
}
}
glm::vec3 center = (minBounds + maxBounds) / 2.0f;
float radius = glm::length(maxBounds - minBounds) / 2.0f;
radius = std::max(radius, 1.0f);
// Update camera target and distance
camera.target = center;
camera.distance = radius * 2.0f;
// Calculate distances from camera position to the near and far points of the bounding sphere
float nearPlane = 0.1f;
float farPlane = camera.distance + radius * 1.5f;
nearPlane = std::max(nearPlane, 0.1f);
camera.nearPlane = nearPlane;
camera.farPlane = farPlane;
}