-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelViewerWidget.cpp
More file actions
1038 lines (835 loc) · 29.1 KB
/
ModelViewerWidget.cpp
File metadata and controls
1038 lines (835 loc) · 29.1 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
#define NOMINMAX
#include "ModelViewerWidget.h"
#include "GLCamera.h"
#include <assimp/postprocess.h>
#include <cfloat>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QMatrix4x4>
#include <QMouseEvent>
#include <QPushButton>
#include <QTimer>
#include <QToolButton>
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <algorithm>
ModelViewerWidget::ModelViewerWidget(QWidget* parent)
: QOpenGLWidget(parent) {
QSurfaceFormat fmt;
fmt.setDepthBufferSize(24);
fmt.setVersion(3, 3);
fmt.setProfile(QSurfaceFormat::CompatibilityProfile);
QSurfaceFormat::setDefaultFormat(fmt);
m_modelMatrix.setToIdentity();
m_viewMatrix.setToIdentity();
m_projectionMatrix.setToIdentity();
m_viewRadius = 1000;
m_cameraDistance = 500;
m_sceneUpdated = false;
m_inertiaTimer = new QTimer(this);
m_inertiaTimer->setInterval(16); // ~60 FPS
connect(m_inertiaTimer, &QTimer::timeout, this, &ModelViewerWidget::onInertiaTimeout);
_viewToolbar = new QWidget(this);
_viewToolbar->setAttribute(Qt::WA_TransparentForMouseEvents, false);
_viewToolbar->setStyleSheet("background: rgba(255, 255, 255, 100); border: 1px solid gray; border-radius: 4px;");
_viewToolbar->setFixedHeight(64);
QHBoxLayout* layout = new QHBoxLayout(_viewToolbar);
layout->setContentsMargins(4, 4, 4, 4);
layout->setSpacing(6);
layout->addWidget(createViewButton(":/icons/res/top.png", "Top View", [this]() { setViewTop(); }, _viewToolbar));
layout->addWidget(createViewButton(":/icons/res/front.png", "Front View", [this]() { setViewFront(); }, _viewToolbar));
layout->addWidget(createViewButton(":/icons/res/left.png", "Left View", [this]() { setViewLeft(); }, _viewToolbar));
layout->addWidget(createViewButton(":/icons/res/bottom.png", "Bottom View", [this]() { setViewBottom(); }, _viewToolbar));
layout->addWidget(createViewButton(":/icons/res/back.png", "Rear View", [this]() { setViewRear(); }, _viewToolbar));
layout->addWidget(createViewButton(":/icons/res/right.png", "Right View", [this]() { setViewRight(); }, _viewToolbar));
layout->addWidget(createViewButton(":/icons/res/isometric.png", "Isometric View", [this]() { setViewAxonometric(); }, _viewToolbar));
layout->addWidget(createViewButton(":/icons/res/fit-all.png", "Fit All", [this]() { fitToView(); }, _viewToolbar));
QToolButton* projToggleButton = new QToolButton(_viewToolbar);
projToggleButton->setCheckable(true);
projToggleButton->setIcon(QIcon(":/icons/res/Perspective.png")); // default icon
projToggleButton->setIconSize(QSize(64, 64));
projToggleButton->setToolTip("Toggle Projection");
connect(projToggleButton, &QToolButton::toggled, this, [this, projToggleButton](bool checked) {
if (checked) {
m_camera->setProjectionType(GLCamera::ProjectionType::ORTHOGRAPHIC);
projToggleButton->setIcon(QIcon(":/icons/res/Ortho.png"));
projToggleButton->setIconSize(QSize(64, 64));
projToggleButton->setToolTip("Switch to Perspective");
}
else {
m_camera->setProjectionType(GLCamera::ProjectionType::PERSPECTIVE);
projToggleButton->setIcon(QIcon(":/icons/res/Perspective.png"));
projToggleButton->setIconSize(QSize(64, 64));
projToggleButton->setToolTip("Switch to Orthographic");
}
update();
});
layout->addWidget(projToggleButton);
setFocusPolicy(Qt::StrongFocus);
}
QToolButton* ModelViewerWidget::createViewButton(const QString& iconPath, const QString& tooltip, const std::function<void()>& callback, QWidget* parent) {
QToolButton* button = new QToolButton(parent);
button->setIcon(QIcon(iconPath));
button->setIconSize(QSize(64, 64));
button->setToolTip(tooltip);
button->setAutoRaise(true); // Flat appearance
QObject::connect(button, &QToolButton::clicked, callback);
return button;
}
void ModelViewerWidget::initializeGL() {
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE); // Normalize normals for non-uniform scaling
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
// Set light position and color
GLfloat lightPos[] = { 0.0f, 0.0f, m_viewRadius, 0.0f };
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
GLfloat lightColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor);
GLfloat materialAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, materialAmbient);
// Set specular material color
GLfloat materialSpecular[] = { 0.5f, 0.5f, 0.5f, 1.0f };
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materialSpecular);
// Set shininess (range: 0 to 128; higher = smaller, sharper highlight)
GLfloat shininess = 64.0f;
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE); // Normalize normals for non-uniform scaling
m_camera = new GLCamera(height(), width(), m_viewRadius, 45);
m_camera->setProjectionType(GLCamera::ProjectionType::PERSPECTIVE);
m_camera->setView(GLCamera::ViewProjection::SE_ISOMETRIC_VIEW);
}
void ModelViewerWidget::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (h == 0) h = 1; // Prevent division by zero
m_camera->setScreenSize(w, h);
m_camera->setViewRange(m_viewRadius * 2.1f);
m_viewMatrix = m_camera->getViewMatrix();
m_projectionMatrix = m_camera->getProjectionMatrix();
glMatrixMode(GL_MODELVIEW);
}
void ModelViewerWidget::paintGL() {
glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawGradientBackground();
m_viewMatrix.setToIdentity();
m_viewMatrix = m_camera->getViewMatrix();
m_projectionMatrix = m_camera->getProjectionMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadMatrixf(m_projectionMatrix.constData());
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
QMatrix4x4 modelViewMatrix = m_viewMatrix * m_modelMatrix;
glLoadMatrixf(modelViewMatrix.constData());
if (m_scene) {
if (m_scene->mRootNode)
drawNode(m_scene->mRootNode);
}
drawTrihedronOverlay();
}
void ModelViewerWidget::updateCamera() {
// Update camera position based on the current rotation and zoom
if (!m_scene) {
return;
}
// After computing bounding box:
aiVector3D minimum(FLT_MAX, FLT_MAX, FLT_MAX);
aiVector3D maximum(-FLT_MAX, -FLT_MAX, -FLT_MAX);
aiMatrix4x4 identity;
computeBoundingBox(m_scene, m_scene->mRootNode, minimum, maximum, identity);
float maxExtent = std::max({ maximum.x - minimum.x, maximum.y - minimum.y, maximum.z - minimum.z });
m_viewCenter = (maximum + minimum) * 0.5f;
m_viewRadius = maxExtent * 0.5f;
// Ideal distance from camera to model center based on FOV
float fovYRadians = 45.0f * M_PI / 180.0f;
m_cameraDistance = m_viewRadius / std::tan(fovYRadians * 0.5f);
// Final camera position offset along Z axis (behind object)
QVector3D center(m_viewCenter.x, m_viewCenter.y, m_viewCenter.z);
QVector3D camPos = center + QVector3D(0, 0, m_cameraDistance);
m_camera->setViewRange(m_viewRadius * 2.1f);
QVector3D viewPos(m_viewCenter.x, m_viewCenter.y, m_viewCenter.z);
m_camera->setPosition(viewPos);
m_viewMatrix = m_camera->getViewMatrix();
m_projectionMatrix = m_camera->getProjectionMatrix();
m_sceneUpdated = true;
}
void ModelViewerWidget::computeBoundingBox(const aiScene* scene, const aiNode* node,
aiVector3D& minimum, aiVector3D& maximum, const aiMatrix4x4& transform)
{
aiMatrix4x4 currentTransform = transform * node->mTransformation;
for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
const aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
for (unsigned int j = 0; j < mesh->mNumVertices; ++j) {
aiVector3D v = mesh->mVertices[j];
v *= currentTransform; // apply transformation
minimum.x = std::min(minimum.x, v.x);
minimum.y = std::min(minimum.y, v.y);
minimum.z = std::min(minimum.z, v.z);
maximum.x = std::max(maximum.x, v.x);
maximum.y = std::max(maximum.y, v.y);
maximum.z = std::max(maximum.z, v.z);
}
}
for (unsigned int i = 0; i < node->mNumChildren; ++i) {
computeBoundingBox(scene, node->mChildren[i], minimum, maximum, currentTransform);
}
}
void ModelViewerWidget::drawGradientBackground() {
glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_CURRENT_BIT); // Save state
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING); // Disable lighting if enabled by model draw
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glBegin(GL_QUADS);
glColor3f(0.3f, 0.3f, 0.3f); glVertex2f(-1.0f, 1.0f); // Top left
glColor3f(0.55f, 0.55f, 0.55f); glVertex2f(1.0f, 1.0f); // Top right
glColor3f(0.95f, 0.95f, 0.95f); glVertex2f(1.0f, -1.0f); // Bottom right
glColor3f(0.7f, 0.7f, 0.7f); glVertex2f(-1.0f, -1.0f); // Bottom left
glEnd();
glPopMatrix(); // MODELVIEW
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopAttrib(); // Restore GL state
}
void ModelViewerWidget::drawTrihedron(float axisLength, float axisRadius, float coneHeight, float coneRadius, float sphereRadius) {
GLUquadric* quad = gluNewQuadric();
glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_TRANSFORM_BIT);
glPushMatrix();
glDisable(GL_LIGHTING);
// Draw center sphere
glColor3f(0.8f, 0.8f, 0.0f);
gluSphere(quad, sphereRadius, 16, 16);
// ===== X Axis (Red) =====
glColor3f(1.0f, 0.0f, 0.0f); // Red
glPushMatrix();
glRotatef(90, 0, 1, 0); // Point along +X
gluCylinder(quad, axisRadius, axisRadius, axisLength, 12, 1);
glTranslatef(0, 0, axisLength);
gluCylinder(quad, coneRadius, 0.0, coneHeight, 12, 1); // Cone tip
glPopMatrix();
// ===== Y Axis (Green) =====
glColor3f(0.0f, 1.0f, 0.0f); // Green
glPushMatrix();
glRotatef(-90, 1, 0, 0); // Point along +Y
gluCylinder(quad, axisRadius, axisRadius, axisLength, 12, 1);
glTranslatef(0, 0, axisLength);
gluCylinder(quad, coneRadius, 0.0, coneHeight, 12, 1); // Cone tip
glPopMatrix();
// ===== Z Axis (Blue) =====
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glPushMatrix();
// Already aligned with +Z
gluCylinder(quad, axisRadius, axisRadius, axisLength, 12, 1);
glTranslatef(0, 0, axisLength);
gluCylinder(quad, coneRadius, 0.0, coneHeight, 12, 1); // Cone tip
glPopMatrix();
gluDeleteQuadric(quad);
glPopMatrix();
glPopAttrib();
}
void ModelViewerWidget::drawTrihedronOverlay() {
int size = 100; // Size of mini viewport
int margin = 10;
glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(margin, margin, size, size);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
if (m_camera->getProjectionType() == GLCamera::ProjectionType::PERSPECTIVE) {
// Narrow FOV for better appearance in small viewport
gluPerspective(30.0, 1.0, 0.1, 10.0);
glScalef(1.0f, 1.0f, 1.0f); // Scale to normal size
}
else {
// Orthographic projection for overlay
float orthoSize = 1.5f;
glOrtho(-orthoSize, orthoSize, -orthoSize, orthoSize, 0.1, 10.0);
glScalef(1.25f, 1.25f, 1.25f); // Scale to fit viewport
}
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, // Eye position
0.0, 0.0, 0.0, // Look at origin
0.0, 1.0, 0.0); // Up vector
// Extract the camera's rotation matrix
QMatrix4x4 view = m_camera->getViewMatrix();
// Remove translation component
view.setColumn(3, QVector4D(0, 0, 0, 1));
// Apply only rotation part of the main camera
glMultMatrixf(view.constData());
drawTrihedron();
// Restore OpenGL state
glPopMatrix(); // ModelView
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopAttrib();
}
void ModelViewerWidget::loadModel(const QString& filePath) {
if (m_scene)
{
m_importer.FreeScene();
m_scene = nullptr;
}
QFileInfo fileInfo(filePath);
m_lastModelPath = fileInfo.absolutePath();
resetView();
for (auto texId : m_materialTextureCache) {
if (texId.second) glDeleteTextures(1, &texId.second);
}
m_materialTextureCache.clear();
m_scene = m_importer.ReadFile(filePath.toStdString(),
aiProcess_Triangulate | aiProcess_ValidateDataStructure |
aiProcess_CalcTangentSpace | aiProcess_GenSmoothNormals |
aiProcess_FixInfacingNormals | aiProcess_JoinIdenticalVertices |
aiProcess_OptimizeMeshes | aiProcess_GenUVCoords | aiProcess_SortByPType);
if (!m_scene || m_scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !m_scene->mRootNode) // if is Not Zero
{
qDebug() << "ERROR::ASSIMP:: " << m_importer.GetErrorString();
m_scene = nullptr;
return;
}
updateCamera();
update();
}
GLuint ModelViewerWidget::loadTextureIfNeeded(const aiMaterial* material, unsigned int materialIndex)
{
if (m_materialTextureCache.find(materialIndex) != m_materialTextureCache.end())
return m_materialTextureCache[materialIndex];
if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0) {
aiString texturePath;
if (material->GetTexture(aiTextureType_DIFFUSE, 0, &texturePath) == AI_SUCCESS) {
QString fullPath = QDir(m_lastModelPath).filePath(QString::fromUtf8(texturePath.C_Str()));
QImage image(fullPath);
if (!image.isNull()) {
image = image.convertToFormat(QImage::Format_RGBA8888).mirrored();
GLuint texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, image.bits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
m_materialTextureCache[materialIndex] = texId;
return texId;
}
}
}
m_materialTextureCache[materialIndex] = 0; // No texture
return 0;
}
void ModelViewerWidget::highlightNode(aiNode* node) {
m_highlightedNode = node;
update();
}
void ModelViewerWidget::drawNode(aiNode* node)
{
for (unsigned i = 0; i < node->mNumMeshes; ++i) {
const aiMesh* mesh = m_scene->mMeshes[node->mMeshes[i]];
if (!mesh) continue;
const aiMaterial* material = m_scene->mMaterials[mesh->mMaterialIndex];
GLuint texId = loadTextureIfNeeded(material, mesh->mMaterialIndex);
if (texId) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texId);
}
else {
glDisable(GL_TEXTURE_2D);
}
aiColor4D diffuse;
if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, &diffuse)) {
glColor4f(diffuse.r, diffuse.g, diffuse.b, diffuse.a);
}
else {
glColor4f(0.8f, 0.8f, 0.8f, 1.0f);
}
if (node == m_highlightedNode && !texId) {
// Set full specular material color for highlighted node
GLfloat materialSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materialSpecular);
}
else {
// Set default specular color
GLfloat materialSpecular[] = { 0.5f, 0.5f, 0.5f, 1.0f };
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materialSpecular);
}
glBegin(GL_TRIANGLES);
for (unsigned int j = 0; j < mesh->mNumFaces; ++j) {
const aiFace& face = mesh->mFaces[j];
for (unsigned int k = 0; k < face.mNumIndices; ++k) {
unsigned int index = face.mIndices[k];
if (mesh->HasNormals()) {
const aiVector3D& n = mesh->mNormals[index];
glNormal3f(n.x, n.y, n.z);
}
if (mesh->HasTextureCoords(0)) {
const aiVector3D& uv = mesh->mTextureCoords[0][index];
glTexCoord2f(uv.x, uv.y);
}
if (node == m_highlightedNode && !texId)
glColor3d(204, 255, 0); // Flourescent Yellow highlight
const aiVector3D& v = mesh->mVertices[index];
glVertex3f(v.x, v.y, v.z);
}
}
glEnd();
glDisable(GL_TEXTURE_2D);
// Optional second pass: wireframe highlight
if (node == m_highlightedNode && texId) {
glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT | GL_LINE_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glEnable(GL_POLYGON_OFFSET_LINE);
glPolygonOffset(-1.0f, -1.0f); // Pull forward to avoid z-fighting
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glLineWidth(0.50f);
glColor3d(204, 255, 0); // Flourescent Yellow highlight
glBegin(GL_TRIANGLES);
for (unsigned int j = 0; j < mesh->mNumFaces; ++j) {
const aiFace& face = mesh->mFaces[j];
for (unsigned int k = 0; k < face.mNumIndices; ++k) {
unsigned int index = face.mIndices[k];
const aiVector3D& v = mesh->mVertices[index];
glVertex3f(v.x, v.y, v.z);
}
}
glEnd();
glPopAttrib();
}
if (texId)
glBindTexture(GL_TEXTURE_2D, 0);
}
for (unsigned i = 0; i < node->mNumChildren; ++i)
drawNode(node->mChildren[i]);
}
void ModelViewerWidget::mousePressEvent(QMouseEvent* event)
{
m_isDragging = true;
m_totalMouseDelta = QPoint(0, 0); // Reset drag distance
if (m_inertiaTimer->isActive())
m_inertiaTimer->stop();
m_lastMousePos = event->pos();
if ((event->modifiers() & Qt::ControlModifier))
{
if (event->button() == Qt::LeftButton)
m_mode = InteractionMode::Rotate;
else if (event->button() == Qt::RightButton)
m_mode = InteractionMode::Pan;
else if (event->button() == Qt::MiddleButton)
m_mode = InteractionMode::Zoom;
}
else
m_mode = InteractionMode::Select;
if (event->button() == Qt::LeftButton && m_mode == InteractionMode::Select && m_scene) {
pickAtScreenPosition(event->pos());
}
}
void ModelViewerWidget::mouseMoveEvent(QMouseEvent* event)
{
QPoint delta = event->pos() - m_lastMousePos;
m_totalMouseDelta += delta;
QPoint downPoint = event->pos();
if (m_mode == InteractionMode::Rotate) {
setCursor(QCursor(QPixmap(":/icons/res/rotatecursor.png")));
QPoint rotate = m_lastMousePos - downPoint;
m_camera->rotateX(rotate.y() / 2.0);
m_camera->rotateY(rotate.x() / 2.0);
// Store rotation velocity
m_rotationVelocity = QVector2D(delta.x(), delta.y()) / 2.0f;
m_inertiaActive = false; // Stop inertia while dragging
}
else if (m_mode == InteractionMode::Pan) {
setCursor(QCursor(QPixmap(":/icons/res/pancursor.png")));
// Pan speed scaled by distance
float panSpeed = m_cameraDistance * 0.001f;
// Apply panning to the view center
QVector3D OP = get3dTranslationVectorFromMousePoints(downPoint, m_lastMousePos);
m_camera->move(OP.x(), OP.y(), OP.z());
m_panVelocity = OP;
m_inertiaActive = false;
}
else if (m_mode == InteractionMode::Zoom) {
setCursor(QCursor(QPixmap(":/icons/res/zoomcursor.png")));
float zoomDelta = delta.y() * 0.01f;
if (downPoint.x() > m_lastMousePos.x() || downPoint.y() < m_lastMousePos.y())
m_viewRadius /= 1.05f;
else
m_viewRadius *= 1.05f;
// Translate to focus on mouse center
QPoint cen = QRect(0, 0, width(), height()).center();
float sign = (downPoint.x() > m_lastMousePos.x() || downPoint.y() < m_lastMousePos.y()) ? 1.0f : -1.0f;
QVector3D OP = get3dTranslationVectorFromMousePoints(cen, event->position().toPoint());
OP *= sign * 0.05f;
m_camera->move(OP.x(), OP.y(), OP.z());
// Store velocity
m_zoomVelocity = sign * 0.10f;
m_zoomPanVelocity = OP;
m_inertiaActive = false;
resizeGL(width(), height());
}
m_lastMousePos = downPoint;
update();
}
void ModelViewerWidget::mouseReleaseEvent(QMouseEvent* event)
{
setCursor(QCursor(Qt::ArrowCursor));
m_isDragging = false;
int movementThreshold = 1; // Keep low for now
if (m_totalMouseDelta.manhattanLength() > movementThreshold)
{
if (!m_inertiaTimer->isActive())
{
m_inertiaTimer->start(16); // 60 fps
m_inertiaActive = true;
}
}
else {
// No real movement ? don't start inertia
m_panVelocity = QVector3D(0, 0, 0);
m_zoomPanVelocity = QVector3D(0, 0, 0);
m_zoomVelocity = 0.0f;
m_rotationVelocity = QVector2D(0, 0);
m_inertiaTimer->stop();
}
Q_UNUSED(event);
if (!(event->modifiers() & Qt::ControlModifier))
m_mode = InteractionMode::Select;
}
void ModelViewerWidget::wheelEvent(QWheelEvent* event)
{
QPoint numDegrees = event->angleDelta() / 8;
if (!numDegrees.isNull()) {
if (!m_inertiaTimer->isActive())
m_inertiaTimer->start(16);
QPoint numSteps = numDegrees / 30;
float zoomStep = numSteps.y();
float zoomFactor = abs(zoomStep) + 0.05;
if (zoomStep < 0)
m_viewRadius *= zoomFactor;
else
m_viewRadius /= zoomFactor;
// Translate to focus on mouse center
QPoint cen = QRect(0, 0, width(), height()).center();
float sign = (event->position().x() > cen.x() || event->position().y() < cen.y() ||
(event->position().x() < cen.x() && event->position().y() > cen.y())) && (zoomStep > 0) ? 1.0f : -1.0f;
QVector3D OP = get3dTranslationVectorFromMousePoints(cen, event->position().toPoint());
OP *= sign * 0.05f;
m_camera->move(OP.x(), OP.y(), OP.z());
// Add to velocities instead of overriding
m_zoomVelocity += sign * 0.1f; // Tune factor
m_zoomPanVelocity += OP * sign * 0.05f;
}
resizeGL(width(), height());
update();
}
void ModelViewerWidget::keyPressEvent(QKeyEvent* event) {
if (event->modifiers() & Qt::ControlModifier) {
if (event->key() == Qt::Key_T) {
setViewProjection(ViewProjection::Top);
}
else if (event->key() == Qt::Key_F) {
setViewProjection(ViewProjection::Front);
}
else if (event->key() == Qt::Key_L) {
setViewProjection(ViewProjection::Left);
}
if (event->key() == Qt::Key_B) {
setViewProjection(ViewProjection::Bottom);
}
else if (event->key() == Qt::Key_R) {
setViewProjection(ViewProjection::Rear);
}
else if (event->key() == Qt::Key_J) {
setViewProjection(ViewProjection::Right);
}
else if (event->key() == Qt::Key_A) {
setViewProjection(ViewProjection::Axonometric);
}
}
else {
if (event->key() == Qt::Key_Home) {
updateCamera();
update();
}
}
}
void ModelViewerWidget::resizeEvent(QResizeEvent* event) {
QOpenGLWidget::resizeEvent(event);
QWidget::resizeEvent(event);
if (_viewToolbar) {
_viewToolbar->adjustSize();
QSize toolbarSize = _viewToolbar->size();
int margin = 10;
int x = (width() - toolbarSize.width()) / 2;
int y = height() - toolbarSize.height() - margin;
// Clamp to ensure it's inside bounds
x = std::max(0, x);
y = std::max(0, y);
_viewToolbar->move(x, y);
}
}
void ModelViewerWidget::resetView() {
m_cameraDistance = 0.0f;
m_viewCenter = aiVector3D(0, 0, 0);
m_viewRadius = 1.0f;
}
void ModelViewerWidget::setViewProjection(ViewProjection view)
{
m_viewProjection = view;
switch (view) {
case ViewProjection::Top:
setViewTop();
break;
case ViewProjection::Front:
setViewFront();
break;
case ViewProjection::Left:
setViewLeft();
break;
case ViewProjection::Bottom:
setViewBottom();
break;
case ViewProjection::Rear:
setViewRear();
break;
case ViewProjection::Right:
setViewRight();
break;
case ViewProjection::Axonometric:
setViewAxonometric();
break;
case ViewProjection::Custom:
default:
// Do nothing or reset to user-controlled
break;
}
m_viewMatrix = m_camera->getViewMatrix();
m_projectionMatrix = m_camera->getProjectionMatrix();
update();
}
void ModelViewerWidget::setViewTop() {
m_camera->setView(GLCamera::ViewProjection::TOP_VIEW);
update();
}
void ModelViewerWidget::setViewFront() {
m_camera->setView(GLCamera::ViewProjection::FRONT_VIEW);
update();
}
void ModelViewerWidget::setViewLeft() {
m_camera->setView(GLCamera::ViewProjection::LEFT_VIEW);
update();
}
void ModelViewerWidget::setViewBottom() {
m_camera->setView(GLCamera::ViewProjection::BOTTOM_VIEW);
update();
}
void ModelViewerWidget::setViewRear() {
m_camera->setView(GLCamera::ViewProjection::REAR_VIEW);
update();
}
void ModelViewerWidget::setViewRight() {
m_camera->setView(GLCamera::ViewProjection::RIGHT_VIEW);
update();
}
void ModelViewerWidget::setViewAxonometric() {
m_camera->setView(GLCamera::ViewProjection::SE_ISOMETRIC_VIEW);
update();
}
void ModelViewerWidget::fitToView() {
// Optional: adjust _cameraDistance or zoom to fit model bounds
updateCamera();
update();
}
void ModelViewerWidget::onInertiaTimeout()
{
if(m_mode == InteractionMode::Select)
return; // Don't apply inertia while selecting
if (!m_inertiaActive)
return;
bool stillActive = false;
// Apply rotation inertia
if (!m_rotationVelocity.isNull()) {
m_camera->rotateX(-m_rotationVelocity.y());
m_camera->rotateY(-m_rotationVelocity.x());
m_rotationVelocity *= m_inertiaFactor;
if (m_rotationVelocity.length() > 0.01f)
stillActive = true;
else
m_rotationVelocity = QVector2D();
}
// Apply pan inertia
if (!m_panVelocity.isNull()) {
m_camera->move(m_panVelocity.x(), m_panVelocity.y(), m_panVelocity.z());
m_panVelocity *= m_inertiaFactor;
if (m_panVelocity.length() > 0.0001f)
stillActive = true;
else
m_panVelocity = QVector3D();
}
// Apply zoom inertia
if (std::abs(m_zoomVelocity) > 0.001f) {
float zoomFactor = 1.005f;
if (m_zoomVelocity > 0)
m_viewRadius /= zoomFactor;
else
m_viewRadius *= zoomFactor;
// Zoom-centric pan
QPoint cen = rect().center();
QVector3D OP = get3dTranslationVectorFromMousePoints(cen, cen);
OP *= -m_zoomPanVelocity * 0.05f;
m_camera->move(OP.x(), OP.y(), OP.z());
m_zoomVelocity *= m_inertiaFactor * 0.75f;
if (std::abs(m_zoomVelocity) > 0.001f)
stillActive = true;
else
m_zoomVelocity = 0.0f;
resizeGL(width(), height());
}
if (!stillActive) {
m_inertiaActive = false;
m_inertiaTimer->stop();
}
update();
}
void ModelViewerWidget::pickAtScreenPosition(const QPoint& pos) {
makeCurrent(); // Needed if using Qt with OpenGL
GLint viewport[4];
GLdouble modelview[16], projection[16];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
float x = pos.x();
float y = viewport[3] - pos.y(); // Flip Y for OpenGL
// Near and far points
double nearX, nearY, nearZ;
double farX, farY, farZ;
gluUnProject(x, y, 0.0, modelview, projection, viewport, &nearX, &nearY, &nearZ);
gluUnProject(x, y, 1.0, modelview, projection, viewport, &farX, &farY, &farZ);
aiVector3D rayOrigin((float)nearX, (float)nearY, (float)nearZ);
aiVector3D rayDirection((float)(farX - nearX), (float)(farY - nearY), (float)(farZ - nearZ));
rayDirection.Normalize();
pickRay(rayOrigin, rayDirection);
}
void ModelViewerWidget::pickRay(const aiVector3D& origin, const aiVector3D& dir) {
float minDistance = std::numeric_limits<float>::max();
int hitMeshIndex = -1;
aiMatrix4x4 hitTransform;
// Track intersected mesh index
std::function<void(aiNode*, const aiMatrix4x4&)> traverse;
traverse = [&](aiNode* node, const aiMatrix4x4& parentTransform) {
aiMatrix4x4 transform = parentTransform * node->mTransformation;
for (unsigned i = 0; i < node->mNumMeshes; ++i) {
const int meshIndex = node->mMeshes[i];
const aiMesh* mesh = m_scene->mMeshes[meshIndex];
for (unsigned f = 0; f < mesh->mNumFaces; ++f) {
const aiFace& face = mesh->mFaces[f];
if (face.mNumIndices != 3) continue;
aiVector3D v0 = mesh->mVertices[face.mIndices[0]];
aiVector3D v1 = mesh->mVertices[face.mIndices[1]];
aiVector3D v2 = mesh->mVertices[face.mIndices[2]];
v0 *= transform;
v1 *= transform;
v2 *= transform;
float t;
if (rayIntersectsTriangle(origin, dir, v0, v1, v2, t)) {
if (t < minDistance) {
minDistance = t;
hitMeshIndex = meshIndex;
hitTransform = transform;
}
}
}
}
for (unsigned i = 0; i < node->mNumChildren; ++i)
traverse(node->mChildren[i], transform);
};
traverse(m_scene->mRootNode, aiMatrix4x4());
if (hitMeshIndex != -1) {
aiNode* hitNode = findNodeForMesh(m_scene->mRootNode, hitMeshIndex);
if (m_lastPickedNode == hitNode) {
emit nodePicked(nullptr); // Signal to deselect
m_lastPickedNode = nullptr;
}
else {
emit nodePicked(hitNode);
m_lastPickedNode = hitNode;
}
highlightNode(m_lastPickedNode);
}
else {
highlightNode(nullptr);
}
}
bool ModelViewerWidget::rayIntersectsTriangle(
const aiVector3D& orig, const aiVector3D& dir,
const aiVector3D& v0, const aiVector3D& v1, const aiVector3D& v2,
float& outT
)
{
const float EPSILON = 1e-5f;
aiVector3D edge1 = v1 - v0;
aiVector3D edge2 = v2 - v0;
aiVector3D h = dir ^ edge2;
float a = edge1 * h;
if (fabs(a) < EPSILON) return false;
float f = 1.0f / a;
aiVector3D s = orig - v0;
float u = f * (s * h);
if (u < 0.0 || u > 1.0) return false;
aiVector3D q = s ^ edge1;
float v = f * (dir * q);