-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdbViewer.cpp
More file actions
1458 lines (1151 loc) · 39.9 KB
/
mdbViewer.cpp
File metadata and controls
1458 lines (1151 loc) · 39.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <string.h>
#include <codecvt>
#include <locale>
//OPENGL INCLUDE
#include <SFML/Window.hpp>
//Probably doesnt work.
#ifdef WINDOWS
#include <gl/GLEW.h>
#include <SFML/OpenGL.hpp>
//#include <glad/glad.h>
//#include <GLFW/glfw3.h>
#else
#include <SFML/OpenGL.hpp>
#include <GLES3/gl3.h>
#endif
#include <glm/gtc/matrix_transform.hpp>
#include <sstream>
#include "MDBParser.h"
#include "RMPA.h"
#include "MeshRenderer.h"
#include "SimpleRenderer.h"
#include <SFML/Graphics.hpp>
#include "RAB.h"
#include <filesystem>
#include "Util.h"
#include "ToolState.h"
#include "MapViewer.h"
#include "GUI.h"
//###################################################
//Tool State System
//###################################################
//Tool state is in "Model Renderer"
class CStateModelRenderer : public BaseToolState
{
public:
~CStateModelRenderer()
{
meshs.clear(); //Lets hope this call the destructor.
//TODO: Kill shaders?
};
void Init( sf::RenderWindow *iwindow )
{
window = iwindow;
//Todo: Reference font in some kind of common way.
font.loadFromFile( "Font.ttf" );
text = sf::Text( L"NO MESH LOADED", font, 20u );
text.setFillColor( sf::Color( 0, 255, 0 ) );
text.setPosition( sf::Vector2f( 5, 5 ) );
strReadoutText = "Rendering 1 Mesh(s) at position:";
meshObjReadoutText = sf::Text( strReadoutText, font, 15U );
meshObjReadoutText.setFillColor( sf::Color( 255, 0, 0 ) );
meshObjReadoutText.setPosition( sf::Vector2f( 5, 600-15-5 ) );
//Generate GUI elements:
//mainUI = CUITitledContainer( font, "Displaying Model", 800, 64, true );
//mainUI.SetActive( true );
//Init OPENGL related stuff;
//Enable Culling
glEnable( GL_CULL_FACE );
glFrontFace( GL_CCW );
// Create and compile our GLSL program from the shaders
programID = ShaderList::LoadShader( "SimpleTextured", "SimpleVertexShader.txt", "SimpleTexturedFragShader.txt" ); //Todo: Common rendering library?
shader_Untextured = ShaderList::LoadShader( "Untextured", "SimpleVertexShader.txt", "SimpleFragmentShader.txt" );
//mesh = std::make_unique<MeshObject>( reader.GetMeshPositionVertices(0, 0), reader.GetMeshIndices(0, 0), reader.uvs, programID, LoadDDS("texture1.dds") ); //Todo: Perhaps use new/pointers/ect.
//Try to form a bone map:
//lines.push_back( std::make_unique< CDebugLine >( reader.bonepos[1], reader.bonepos[0], glm::vec3( 0, 0, 255 ) ) );
//lines.push_back( std::make_unique< CDebugLine >( reader.bonepos[2], reader.bonepos[0], glm::vec3( 0, 0, 255 ) ) );
//lines.push_back( std::make_unique< CDebugLine >( reader.bonepos[3], reader.bonepos[2], glm::vec3( 0, 0, 255 ) ) );
// Enable depth test
glEnable( GL_DEPTH_TEST );
// Accept fragment if it closer to the camera than the former one
glDepthFunc( GL_LEQUAL );
//Final var init;
isDragging = false;
bUseWireframe = false;
fov = 45.0f;
cameraRotationX = 90;
cameraRotationY = 0;
cameraDistance = 2;
cameraPosition = glm::vec3( 0, 0, 0 );
float cameraRadiansX = cameraRotationX * 3.14159f / 180.f;
float cameraRadiansY = cameraRotationY * 3.14159f / 180.f;
cameraPosition.x = sin( cameraRadiansX ) * sin( cameraRadiansY ) * cameraDistance;
cameraPosition.y = cos( cameraRadiansX ) * cameraDistance;
cameraPosition.z = sin( cameraRadiansX ) * cos( cameraRadiansY ) * cameraDistance;
};
void ProccessEvent( sf::Event event )
{
if( event.type == sf::Event::Resized )
{
// adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
}
else if( event.type == sf::Event::KeyPressed ) //Temp:
{
if( event.key.code == sf::Keyboard::F )
{
bUseWireframe = !bUseWireframe;
//Toggle culling.
if( bUseWireframe )
glDisable( GL_CULL_FACE );
else
glEnable( GL_CULL_FACE );
//Not the best solution, but here we are.
for( int i = 0; i < meshs.size(); ++i )
{
meshs[i]->shaderID = bUseWireframe ? shader_Untextured : programID;
meshs[i]->TextureID = glGetUniformLocation(meshs[i]->shaderID, "myTextureSampler");
}
}
else if( event.key.code == sf::Keyboard::Down )
cameraPosition.x += 0.1;
}
if( event.type == sf::Event::MouseButtonPressed )
{
//Additional checks can go here.
if( event.mouseButton.button == sf::Mouse::Button::Left )
{
mouseOldPos = sf::Mouse::getPosition( *window );
isDragging = true;
}
}
else if( event.type == sf::Event::MouseButtonReleased )
{
//Additional checks can go here.
if( event.mouseButton.button == sf::Mouse::Button::Left && isDragging )
{
isDragging = false;
}
}
else if( event.type == sf::Event::MouseMoved && isDragging )
{
sf::Vector2i mousePos = sf::Mouse::getPosition( *window );
//for( int i = 0; i < meshs.size(); ++i )
// meshs[i]->angles.y -= (float)(mouseOldPos.x - mousePos.x) / 10.0f;
sf::Vector2i mouseDelta = mousePos - mouseOldPos;
cameraRotationY -= mouseDelta.x * 0.1f;
cameraRotationX -= mouseDelta.y * 0.1f;
if( cameraRotationY > 360 )
cameraRotationY -= 360;
if( cameraRotationX > 360 )
cameraRotationX -= 360;
float cameraRadiansX = cameraRotationX * 3.14159f / 180.f;
float cameraRadiansY = cameraRotationY * 3.14159f / 180.f;
cameraPosition.x = sin( cameraRadiansX ) * sin( cameraRadiansY ) * cameraDistance;
cameraPosition.y = cos( cameraRadiansX ) * cameraDistance;
cameraPosition.z = sin( cameraRadiansX ) * cos( cameraRadiansY ) * cameraDistance;
mouseOldPos = mousePos;
}
else if( event.type == sf::Event::MouseWheelMoved )
{
cameraDistance -= event.mouseWheel.delta * 0.1f;
if( cameraDistance < 0.1f )
{
cameraDistance = 0.1f;
}
float cameraRadiansX = cameraRotationX * 3.14159f / 180.f;
float cameraRadiansY = cameraRotationY * 3.14159f / 180.f;
cameraPosition.x = sin( cameraRadiansX ) * sin( cameraRadiansY ) * cameraDistance;
cameraPosition.y = cos( cameraRadiansX ) * cameraDistance;
cameraPosition.z = sin( cameraRadiansX ) * cos( cameraRadiansY ) * cameraDistance;
}
}
void Draw()
{
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
cam.Projection = glm::perspective( glm::radians(45.0f), (float)window->getSize().x / (float)window->getSize().y, 0.1f, 100.0f );
// Camera matrix
cam.View = glm::lookAt(
cameraPosition, // Camera position, in World Space
glm::vec3(0,0,0), // Camera Look At position
glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
);
//strReadoutText = "Rendering 1 'object' at position: " + std::to_string( mesh->position.x ) + ", " +
//std::to_string( mesh->position.y ) + ", " + std::to_string( mesh->position.z );
//meshObjReadoutText.setString( strReadoutText );
window->clear();
glClear( GL_DEPTH_BUFFER_BIT );
//Turn on wireframe mode
if( bUseWireframe )
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//Draw mesh(s)
for( int i = 0; i < meshs.size(); ++i )
meshs[i]->Draw( cam );
//std::cout << glGetError() + "\n";
//Turn off wireframe mode
if( bUseWireframe )
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//Render these above all else
glDepthFunc( GL_ALWAYS );
for( const auto & line : lines )
{
line->Draw( cam );
}
glDepthFunc( GL_LEQUAL );
glUseProgram(0);
window->pushGLStates();
window->draw( text );
window->draw( meshObjReadoutText );
//mainUI.Draw( window );
window->popGLStates();
window->display();
}
void LoadMDB( std::string ipath )
{
//Proccess:
std::filesystem::path fsPath = ipath;
std::string extension = fsPath.extension().string();
std::transform( extension.begin(), extension.end(), extension.begin(), ::tolower );
if( extension == ".mdb" ) //Load directly from MDB file
{
//Load mdb
//TODO: Load this more dynamically, through some kind of user input.
MDBReader reader( ipath.c_str() ); //Todo: Memory management.
//Text information about the viewer
std::wstring strObjName = reader.model.names[ reader.model.objects[0].nameindex ];
std::wstring strMeshInfo = L"Object 1 of " + std::to_wstring( reader.model.objectscount );
std::wstring strVertexInfo = L"Number of vertices (MESH 1): " + std::to_wstring( reader.model.objects[0].meshs[0].vertexnumber );
text.setString( L"Displaying: " + strObjName + L"\n" + strMeshInfo + L"\n" + strVertexInfo );
meshs.clear();
//meshs.push_back( std::make_unique<MeshObject>( reader.GetMeshPositionVertices(0, 0), reader.GetMeshIndices(0, 0), reader.uvs, programID, LoadDDS("texture1.dds") ) );
for( int i = 0; i < reader.model.objects[0].meshcount; ++i )
{
meshs.push_back( std::make_unique<MeshObject>( reader.GetMeshPositionVertices(0, i), reader.GetMeshIndices(0, i), reader.uvs, programID, LoadDDS("texture1.dds") ) );
}
}
else if( extension == ".rab" || extension == ".mrab" )
{
//Load RAB archive
RABReader mdlArc( ipath.c_str() );
std::wstring fileName;
//find first model in RAB archive. TODO: Allow user to select this, somehow?
for( int i = 0; i < mdlArc.numFiles; ++i )
{
if( mdlArc.files[i].folder == L"MODEL" )
{
fileName = mdlArc.files[i].name;
break;
}
}
MDBReader model( mdlArc.ReadFile( L"MODEL", fileName ) );
//Text information about the viewer
std::wstring strObjName = model.model.names[ model.model.objects[0].nameindex ];
std::wstring strMeshInfo = L"Objects: " + std::to_wstring( model.model.objectscount );
std::wstring strVertexInfo = L"Number of vertices (MESH 1): " + std::to_wstring( model.model.objects[0].meshs[0].vertexnumber );
text.setString( L"Displaying: " + strObjName + L"\n" + strMeshInfo + L"\n" + strVertexInfo );
//Fully construct the object:
for( int i = 0; i < model.model.objectscount; ++i )
{
for( int j = 0; j < model.model.objects[ i ].meshcount; ++j )
{
//Test folder names:
std::wstring folder = L"HD-TEXTURE";
if( !mdlArc.HasFolder( folder ) )
{
folder = L"TEXTURE";
}
std::vector< char > textureBytes;
textureBytes = mdlArc.ReadFile( folder, model.GetColourTextureFilename( i, j ) );
meshs.push_back( std::make_unique<MeshObject>( model.GetMeshPositionVertices( i, j ), model.GetMeshIndices( i, j ), model.GetMeshUVs( i, j ), programID, LoadDDS_FromBuffer( textureBytes ) ) );
}
}
//Camera angle for looking at humanoid bones.
//cameraPosition = glm::vec3( 0, 3, 2 );
//Attempt to generate bones.
std::vector< glm::mat4 > boneWorldTransforms;
for( int i = 0; i < model.model.bonescount; ++i )
{
if( model.model.bones[i].boneParent != -1 )
{
//Obvious, in retrospect. Each bone transforms itself relative to its parent to get its actual coords.
//However, the extra data is unknown.
int parentIndex = model.model.bones[ model.model.bones[i].boneParent ].boneIndex;
glm::vec4 pos1 = glm::vec4( 0.0f );
glm::vec4 pos2 = glm::vec4( 0.0f );
pos1.w = 1;
pos2.w = 1;
boneWorldTransforms.push_back( boneWorldTransforms[parentIndex] * model.model.bones[i].matrix );
pos1 = boneWorldTransforms[parentIndex] * pos1;
pos2 = boneWorldTransforms[i] * pos2;
lines.push_back( std::make_unique<CDebugLine>( pos1, pos2, glm::vec3( 0, 255, 0 ) ) );
//Visualise the "Reset to zero" transform
//pos2 = model.model.bones[i].matrix2 * pos2;
//lines.push_back( std::make_unique<CDebugLine>( pos1, pos2, glm::vec3( 255, 255, 0 ) ) );
}
else
{
boneWorldTransforms.push_back( model.model.bones[i].matrix );
}
}
}
}
protected:
//Renderables:
sf::Font font;
sf::Text text;
sf::Text meshObjReadoutText;
std::vector< std::unique_ptr<MeshObject>> meshs;
std::vector< std::unique_ptr<CDebugLine> > lines;
//Variables:
std::string strReadoutText;
GLuint programID;
GLuint shader_Untextured;
Camera cam;
bool bUseWireframe;
//TODO: This shouldnt be here, it should be in the camera class.
glm::vec3 cameraPosition;
float cameraRotationX;
float cameraRotationY;
float cameraDistance;
float fov;
//Control variables
sf::Vector2i mouseOldPos;
bool isDragging;
//GUI
//CUITitledContainer mainUI;
};
class CStateSceneRenderer : public BaseToolState
{
public:
~CStateSceneRenderer()
{
meshs.clear(); //Lets hope this call the destructor.
lines.clear();
//TODO: Kill shaders?
};
void Init( sf::RenderWindow *iwindow )
{
window = iwindow;
//Init OPENGL related stuff;
//Enable Culling
glEnable( GL_CULL_FACE );
glFrontFace( GL_CCW );
// Create and compile our GLSL program from the shaders
programID = ShaderList::LoadShader( "SimpleTextured", "SimpleVertexShader.txt", "SimpleTexturedFragShader.txt" ); //Todo: Common rendering library?
shader_Untextured = ShaderList::LoadShader( "Untextured", "SimpleVertexShader.txt", "SimpleFragmentShader.txt" );
// Enable depth test
glEnable( GL_DEPTH_TEST );
// Accept fragment if it closer to the camera than the former one
glDepthFunc( GL_LEQUAL );
//Final var init;
bUseWireframe = false;
fov = 45.0f;
cameraPosition = glm::vec3( 2, 0, 0 );
pitch = 0;
yaw = 180;
isMouseCamControl = false;
//sf::Mouse::setPosition( sf::Vector2i(window->getSize().x/2, window->getSize().y/2) , *window );
//mouseOldPos = sf::Mouse::getPosition( *window );
};
void ProccessEvent( sf::Event event )
{
if( event.type == sf::Event::Resized )
{
// adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
}
else if( event.type == sf::Event::KeyPressed ) //Temp:
{
if( event.key.code == sf::Keyboard::F )
{
bUseWireframe = !bUseWireframe;
//Toggle culling.
if( bUseWireframe )
glDisable( GL_CULL_FACE );
else
glEnable( GL_CULL_FACE );
//Not the best solution, but here we are.
for( int i = 0; i < meshs.size(); ++i )
{
meshs[i]->shaderID = bUseWireframe ? shader_Untextured : programID;
meshs[i]->TextureID = glGetUniformLocation(meshs[i]->shaderID, "myTextureSampler");
}
}
else if( event.key.code == sf::Keyboard::Space )
{
isMouseCamControl = !isMouseCamControl;
window->setMouseCursorVisible( !isMouseCamControl );
if( isMouseCamControl )
{
sf::Mouse::setPosition( sf::Vector2i( window->getSize().x/2, window->getSize().y/2 ) , *window );
mouseOldPos = sf::Mouse::getPosition( *window );
}
}
}
else if (event.type == sf::Event::MouseMoved )
{
if( isMouseCamControl )
{
if( event.mouseMove.x != mouseOldPos.x || event.mouseMove.y != mouseOldPos.y ) //Only care if the muse has actually moved.
{
pitch -= (event.mouseMove.y - mouseOldPos.y) / 10.0f;
yaw += (event.mouseMove.x - mouseOldPos.x) / 10.0f;
sf::Mouse::setPosition( sf::Vector2i(window->getSize().x/2, window->getSize().y/2) , *window );
}
}
}
}
void Draw()
{
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
cam.Projection = glm::perspective( glm::radians(45.0f), (float)window->getSize().x / (float)window->getSize().y, 0.1f, 1000.0f );
//pitch = 0;
//yaw = 180.0;
glm::vec3 direction;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
//cameraFront = glm::normalize(direction);
//We have the movement code here, as we want to run it each frame.
//TODO: Scale with a deltatime value.
float speed = cameraSpeed;
//Get needed vectors for camera movement.
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraRight = glm::normalize(glm::cross(up, direction));
if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
cameraPosition += direction * speed;
if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
cameraPosition -= direction * speed;
if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
cameraPosition += cameraRight * speed;
if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
cameraPosition -= cameraRight * speed;
// Camera matrix
cam.View = glm::lookAt(
cameraPosition, // Camera position, in World Space
cameraPosition + direction, // Camera Look At position
glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
);
//strReadoutText = "Rendering 1 'object' at position: " + std::to_string( mesh->position.x ) + ", " +
//std::to_string( mesh->position.y ) + ", " + std::to_string( mesh->position.z );
//meshObjReadoutText.setString( strReadoutText );
window->clear();
glClear( GL_DEPTH_BUFFER_BIT );
//Turn on wireframe mode
if( bUseWireframe )
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//Draw mesh(s)
for( int i = 0; i < meshs.size(); ++i )
meshs[i]->Draw( cam );
//std::cout << glGetError() + "\n";
//Turn off wireframe mode
if( bUseWireframe )
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
for( const auto & line : lines )
{
line->Draw( cam );
}
glUseProgram(0);
window->display();
}
void LoadScene( )
{
//Proccess:
std::string path = "EDFData/IG_BASE502";
//std::string path = "EDFData/IG_EDFROOM01";
//Load MAC.
MACReader map( ( path + ".MAC" ).c_str() );
//Load RAB archive
RABReader mdlArc( ( path + ".RAB" ).c_str() );
//Compare map object strings, replace with "true" object when I can.
/*
if( map.modelNames.size() == map.modelNames2.size() )
{
for( int i = 0; i < map.modelNames.size(); ++i )
{
for( int j = 0; j < mdlArc.files.size(); ++j )
{
if( mdlArc.files[j].folder == L"MODEL" && mdlArc.files[j].name == map.modelNames2[i] + L".mdb" )
{
//Should do this elsewere.
map.modelNames[i] = map.modelNames2[i] + L".mdb";
}
}
}
}
*/
for (int i = 0; i < map.modelNames.size(); ++i)
{
map.modelNames[i] = map.modelNames2[i] + L".mdb";
}
//Lets be more effecient about this.
//Determine "Unique" models
std::map < std::wstring, MDBReader > mdbs;
std::map < std::wstring, GLuint > mdbTextures;
for( int i = 0; i < map.modelNames.size(); ++i )
{
if( map.modelNames[i].size() == 0 )
continue;
if( mdbs.count( map.modelNames[i] ) == 0 )
{
std::wstring mdbName = map.modelNames[i];
std::vector< char > file = mdlArc.ReadFile(L"MODEL", mdbName);
if (file.size() == 0)
continue;
mdbs[mdbName] = MDBReader(file);
file.clear();
if( mdbs[ mdbName ].model.objectscount == 0 ) //Bizzare edge case where a null model exists.
continue;
for( int o = 0; o < mdbs[ mdbName ].model.objectscount; ++o )
{
for( int j = 0; j < mdbs[ mdbName ].model.objects[o].meshcount; ++j )
{
if( mdbTextures.count( mdbs[ mdbName ].GetColourTextureFilename( o, j ) ) == 0 )
{
std::vector< char > textureBytes;
std::wstring texFileName = mdbs[ mdbName ].GetColourTextureFilename( o, j );
textureBytes = mdlArc.ReadFile( L"HD-TEXTURE", texFileName );
mdbTextures[ texFileName ] = LoadDDS_FromBuffer(textureBytes);
textureBytes.clear();
}
}
}
}
//Load submodel, if available.
if( map.subModelNames[i].size() > 0 )
{
if( mdbs.count( map.subModelNames[i] ) == 0 )
{
std::wstring mdbName = map.subModelNames[i];
std::vector< char > file = mdlArc.ReadFile(L"MODEL", mdbName);
if (file.size() == 0)
continue;
mdbs[ mdbName ] = MDBReader( file );
file.clear();
if( mdbs[ mdbName ].model.objectscount == 0 ) //Bizzare edge case where a null model exists.
continue;
for( int o = 0; o < mdbs[ mdbName ].model.objectscount; ++o )
{
for( int j = 0; j < mdbs[ mdbName ].model.objects[o].meshcount; ++j )
{
if( mdbTextures.count( mdbs[ mdbName ].GetColourTextureFilename( o, j ) ) == 0 )
{
std::vector< char > textureBytes;
std::wstring texFileName = mdbs[ mdbName ].GetColourTextureFilename( o, j );
textureBytes = mdlArc.ReadFile( L"HD-TEXTURE", texFileName );
mdbTextures[ texFileName ] = LoadDDS_FromBuffer(textureBytes);
textureBytes.clear();
}
}
}
}
}
}
//Least effecient modelloader ever made :)
for( int iter = 0; iter < map.modelNames.size(); ++iter )
{
if( map.modelNames[iter].size() == 0 )
continue;
if( mdbs[ map.modelNames[iter] ].model.objectscount == 0 ) //Bizzare edge case where a null model exists.
continue;
for( int o = 0; o < mdbs[ map.modelNames[iter] ].model.objectscount; ++o )
{
for( int i = 0; i < mdbs[ map.modelNames[iter] ].model.objects[o].meshcount; ++i )
{
meshs.push_back( std::make_unique<MeshObject>(
mdbs[ map.modelNames[iter] ].GetMeshPositionVertices(o, i),
mdbs[ map.modelNames[iter] ].GetMeshIndices(o, i),
mdbs[ map.modelNames[iter] ].GetMeshUVs( o, i ),
programID,
mdbTextures[mdbs[ map.modelNames[iter] ].GetColourTextureFilename( o, i ) ] ) );
meshs.back()->position = map.positions[iter];
meshs.back()->angles = map.rotations[iter];
}
}
//Load submodel, if available.
if( map.subModelNames[iter].size() > 0 )
{
for( int o = 0; o < mdbs[ map.subModelNames[iter] ].model.objectscount; ++o )
{
for( int i = 0; i < mdbs[ map.subModelNames[iter] ].model.objects[o].meshcount; ++i )
{
meshs.push_back( std::make_unique<MeshObject>(
mdbs[ map.subModelNames[iter] ].GetMeshPositionVertices(o, i),
mdbs[ map.subModelNames[iter] ].GetMeshIndices(o, i),
mdbs[ map.subModelNames[iter] ].GetMeshUVs( o, i ),
programID,
mdbTextures[mdbs[ map.subModelNames[iter] ].GetColourTextureFilename( o, i ) ] ) );
meshs.back()->position = map.positions[iter] + map.submodelPositionOffsets[iter];
meshs.back()->angles = map.rotations[iter];
}
}
}
}
cameraSpeed = 0.5;
//Load RMPA
CRMPAReader rmpa( "EDFData/MISSION.RMPA" );
for( int i = 0; i < rmpa.spawnpoints.size(); ++i )
{
glm::vec3 pos( rmpa.spawnpoints[i].x, rmpa.spawnpoints[i].y, rmpa.spawnpoints[i].z );
glm::vec3 dir( rmpa.spawnpoints[i].fx, rmpa.spawnpoints[i].fy, rmpa.spawnpoints[i].fz );
lines.push_back( std::make_unique<CDebugLine>( pos, dir, glm::vec3( 0, 255, 0 ) ) );
//Create secondary lines to make the spoint point more obvious.
//Get needed vectors for camera movement.
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 lineRight = glm::normalize(glm::cross(up, pos - dir));
glm::vec3 lineUp = glm::normalize(glm::cross(lineRight, pos - dir));
lines.push_back( std::make_unique<CDebugLine>( pos - (lineRight), pos + (lineRight), glm::vec3( 0, 0, 255 ) ) );
lines.push_back( std::make_unique<CDebugLine>( pos - (lineUp), pos + (lineUp), glm::vec3( 255, 255, 0 ) ) );
}
}
protected:
//Renderables:
std::vector< std::unique_ptr<MeshObject>> meshs;
std::vector< std::unique_ptr<CDebugLine> > lines;
//Variables:
GLuint programID;
GLuint shader_Untextured;
Camera cam;
bool bUseWireframe;
//TODO: This shouldnt be here, it should be in the camera class.
glm::vec3 cameraPosition;
float yaw;
float pitch;
float fov;
//Control variables
sf::Vector2i mouseOldPos;
float cameraSpeed;
bool isMouseCamControl;
};
//Tool state is in "File Browser"
class CStateFileBrowser : public BaseToolState
{
public:
void Init( sf::RenderWindow *iwindow )
{
window = iwindow;
font.loadFromFile( "Font.ttf" );
path = "./";
PopulateTexts();
};
void ProccessEvent( sf::Event event )
{
if( event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Button::Left )
{
for( int i = 1; i < texts.size(); ++i )
{
if( texts[i].getGlobalBounds().contains( sf::Vector2f( sf::Mouse::getPosition( *window ) ) ) )
{
std::string newpath;
newpath = texts[i].getString();
if( std::filesystem::is_directory( newpath ) )
{
path = newpath;
PopulateTexts();
}
else
{
//Can't say I am happy with this. Feels inelegant, but it works.
CStateModelRenderer * renderer = (CStateModelRenderer*)pOwner->GetState( "viewer" );
renderer->LoadMDB( newpath );
pOwner->SetState( "viewer" );
}
}
}
}
};
void Draw()
{
window->clear();
window->pushGLStates();
for( int i = 0; i < texts.size(); ++i )
{
if( i > 0 && texts[i].getGlobalBounds().contains( sf::Vector2f( sf::Mouse::getPosition( *window ) ) ) )
texts[i].setFillColor( sf::Color( 255, 0, 0 ) );
else
texts[i].setFillColor( sf::Color( 0, 255, 0 ) );
window->draw(texts[i]);
}
window->popGLStates();
window->display();
};
protected:
void PopulateTexts()
{
texts.clear();
sf::Text headerText = sf::Text( "DIRECTORY:", font, 20u );
headerText.setFillColor( sf::Color( 0, 255, 0 ) );
headerText.setPosition( sf::Vector2f( 5, 5 ) );
texts.push_back( headerText );
for( const auto & entry : std::filesystem::directory_iterator( path ) )
{
std::string extension = entry.path().extension().string();
std::transform( extension.begin(), extension.end(), extension.begin(), ::tolower );
if( extension == ".mdb" || extension == ".rab" || extension == ".mrab" || entry.is_directory() )
{
sf::Text text = sf::Text( entry.path().c_str(), font, 20u );
text.setFillColor( sf::Color( 0, 255, 0 ) );
text.setPosition( sf::Vector2f( 5, texts.back().getGlobalBounds().top + texts.back().getGlobalBounds().height ) );
texts.push_back( text );
}
}
}
sf::Font font;
std::vector< sf::Text > texts;
std::string path;
};
//File browser UI element.
class CUIFileBrowser : public CBaseUIElement
{
public:
CUIFileBrowser(){};
CUIFileBrowser( sf::Font &txtFont, std::string startDirectory, std::string filters )
{
path = std::filesystem::canonical( std::filesystem::absolute( startDirectory ) ).generic_string();
font = txtFont;
//Generate a file browser UI.
float xSize = 480;
float ySize = 480;
//Background
background = sf::RectangleShape( sf::Vector2f( xSize, ySize ) );
background.setFillColor( sf::Color( 128, 128, 128 ) );
directoryBackground = sf::RectangleShape( sf::Vector2f( xSize, 32 ) );
directoryBackground.setOutlineThickness( 2 );
directoryBackground.setOutlineColor( sf::Color( 64, 64, 64 ) );
directoryBackground.setFillColor( sf::Color( 192, 192, 192 ) );
shpDirectorySelected = sf::RectangleShape( sf::Vector2f( 32, 32 ) );
shpDirectorySelected.setFillColor( sf::Color( 192, 192, 255 ) );
pSlider = new CUISliderVertical( &flScrollProgress, 0, 1, ySize - 64 );
pSlider->SetPosition( sf::Vector2f( xSize - 32, 32 ) );
pSlider->SetActive( true );
GenerateDirectory();
GenerateFileList();
//for( const auto & entry : std::filesystem::directory_iterator( path ) )
//{
//}
//directoryList->AddElement();
}
~CUIFileBrowser()
{
delete pSlider;
}
void SetPosition( sf::Vector2f pos )
{
vecPosition = pos;
};
//Handle SF events.
void HandleEvent( sf::Event e )
{
//Store new mouse position.
if( e.type == e.MouseMoved )
mousePos = sf::Vector2f( e.mouseMove.x, e.mouseMove.y );
if( e.type == sf::Event::KeyPressed && e.key.code == sf::Keyboard::Up )
{
//TODO: Use views. Will handle scrolling much better.
for( auto& btn : texts )
{
btn.setPosition( btn.getPosition() + sf::Vector2f( 0, 1 ) );
}
}
//Left click event, handle button inputs.
if( e.type == sf::Event::MouseButtonPressed && e.mouseButton.button == sf::Mouse::Button::Left )
{
//Iterate through every directory sub-path
for( int i = 0; i < directoryList.size(); ++i )
{
if( directoryList[i].getGlobalBounds().contains(sf::Vector2f(e.mouseButton.x, e.mouseButton.y) ) )
{
//Construct new path:
std::string pathNew = "";
for( int j = 0; j <= i; ++j )
{
pathNew += directoryList[ j ].getString();
}
//Set new path.
path = pathNew;
//Regenerate directory string.
GenerateDirectory();
//Repupulate file list
GenerateFileList();
break; //Exit loop.
}
}
}
//directoryList->HandleEvent( e );
pSlider->HandleEvent( e );
};
//Draw elements
void Draw( sf::RenderTarget *window )
{
//Draw background
window->draw( background );
window->draw( directoryBackground );
sf::RenderTexture renderTexture;
float xSize = 480;
float ySize = 480 - 64;
renderTexture.create( xSize, ySize );
//Draw all other elements as part of a Render Texture (?)
for( auto& btn : texts )
{
renderTexture.draw( btn );
}
renderTexture.display();
sf::Sprite sprite( renderTexture.getTexture() );
sprite.setPosition( vecPosition + sf::Vector2f( 0, 64 ) );
window->draw( sprite );
for( auto &dir : directoryList )
{
//Draw a little blue box over the folder in the directory box if applicable.
if( dir.getGlobalBounds().contains( sf::Vector2f( mousePos ) ) )
{