-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1867 lines (1551 loc) · 63 KB
/
Copy pathmain.cpp
File metadata and controls
1867 lines (1551 loc) · 63 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
//@HR
//this is Rouibah hanine project
//perspective future= ? not defined !
//last update : 2025-12-12 6:32PM
//vertex : pour gerer les transfotrmations géométrique
//fragment pour le calcule de la lumiere ( & couleurs)
//lets start !
#define STB_IMAGE_IMPLEMENTATION
#define TINYOBJLOADER_IMPLEMENTATION
#include "stb_image.h"
#include "tiny_obj_loader.h"
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <string>
#include <vector>
#include <iostream>
#include <windows.h> // required for PlaySound()
#include <mmsystem.h>
#include <fstream>
#include <sstream>
#include <cmath>
#include <ctime>
#include <map>
// --------------------- Globals (kept from original) ---------------------
// Mouse control for free cam
bool mouseCaptured = false; // Tracks if we're capturing mouse
int lastMouseX = -1, lastMouseY = -1;
float mouseSensitivity = 0.15f; // Adjust feel (lower = slower)
int winW = 1280, winH = 720;
// Safe camera limits for ORBIT mode
const float MAX_SAFE_ORBIT_RADIUS = 3.6f; // Never go beyond this
const float MIN_SAFE_ORBIT_RADIUS = 1.5f; // Don't get too close to center (optional)
bool showHelp = true;
float sunlight = 0.75f;
bool lampOn = true;
bool ceilingLightOn = true;
bool screenOn=true;
bool windowOpen = false;
bool wardrobeOpen = false;
bool doorOpen = false;
// camera
enum CamMode { CAM_AUTO, CAM_FREE };
CamMode camMode = CAM_AUTO;
bool animPaused = false;
float orbitAngle = 20.0f, orbitRadius = 3.6f, orbitHeight = 2.0f;
float cx = 0.0f, cy = 1.3f, cz = 0.0f;
float yaw = -90.0f, pitch = -10.0f;
float moveSpeed = 0.15f, turnSpeed = 2.5f;
// model state preserved
float winAngleLeft = 0.0f, winAngleRight = 0.0f;
float wardrobeDoorAngleLeft = 0.0f, wardrobeDoorAngleRight = 0.0f;
float doorAngle = 0.0f, targetDoorAngle = 0.0f;
float globalIntensity = 1.0f;
bool doorLightOn = true; // new toggle
float doorLightIntensity = 1.0f; // optional multiplier
bool isFullscreen = false;
GLuint screentextId=0;
std::string vertexSrc ;
std:: string fragSrc ;
// textures
std::map<std::string, GLuint> texMap;
GLuint texNoise = 0;
// tinyobj meshes (cpu-side)
struct Mesh
{
std::vector<float> vertices; // x,y,z
std::vector<float> normals; // x,y,z
std::vector<float> texcoords; // u,v
std::vector<unsigned int> indices;
// GPU
GLuint vao = 0, vbo = 0, ebo = 0;
};
std::map<std::string, Mesh> meshes;
// --------------------- Utility: read file ---------------------
std::string readFile(const std::string &path)
{
std::ifstream f(path);
if(!f) return std::string();
std::stringstream ss;
ss << f.rdbuf();
return ss.str();
}
// --------------------- Init loads textures + objects ---------------------
//std::string getExecutableDir(); // original helper if you need Tex(...) — omitted here for clarity
//----------------------------------------------
std::string getExecutableDir()
{
std::string path;
#ifdef _WIN32
char buffer[MAX_PATH];
if (GetModuleFileNameA(NULL, buffer, MAX_PATH) != 0)
{
path = std::string(buffer);
size_t pos = path.find_last_of("\\/");
if (pos != std::string::npos)
{
path = path.substr(0, pos);
}
}
#else
char buffer[PATH_MAX];
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
if (len != -1)
{
buffer[len] = '\0';
path = std::string(buffer);
size_t pos = path.find_last_of("/");
if (pos != std::string::npos)
{
path = path.substr(0, pos);
}
}
#endif
// Go up two levels: from /bin/Debug → /Project_Official
for (int i = 0; i < 2; ++i)
{
size_t pos = path.find_last_of("\\/");
if (pos != std::string::npos)
{
path = path.substr(0, pos);
}
}
return path;
}
std::string Tex(const std::string& filename)
{
return getExecutableDir() + "/Ressources/" + filename;
}
// --------------------- Shader helpers ---------------------
GLuint compileShader(GLenum type, const char *src)
{
GLuint s = glCreateShader(type);
glShaderSource(s, 1, &src, nullptr);
glCompileShader(s);
GLint ok;
glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
if(!ok)
{
char log[4096];
glGetShaderInfoLog(s, sizeof(log), nullptr, log);
std::cerr<<"Shader compile error: "<<log<<"\n";
}
return s;
}
GLuint linkProgram(GLuint vs, GLuint fs)
{
GLuint p = glCreateProgram();
glAttachShader(p, vs);
glAttachShader(p, fs);
glLinkProgram(p);
GLint ok;
glGetProgramiv(p, GL_LINK_STATUS, &ok);
if(!ok)
{
char log[4096];
glGetProgramInfoLog(p, sizeof(log), nullptr, log);
std::cerr<<"Program link error: "<<log<<"\n";
}
glDeleteShader(vs);
glDeleteShader(fs);
return p;
}
//-----------------------------------------------
void drawTextScreen(const char *txt, int x, int y)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, winW, 0, winH);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable(GL_TEXTURE_2D);
glRasterPos2i(x, y);
for(const char *c = txt; *c; ++c)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
glMatrixMode(GL_MODELVIEW);
glPopMatrix(); // restore previous MODELVIEW
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // restore previous PROJECTION (perspective)
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
}
//-------------------------------------
void toggleFullscreen()
{
if (isFullscreen)
{
glutLeaveFullScreen();
// Optionally restore original window size
glutReshapeWindow(1280, 720);
glutPositionWindow(50, 50); // optional: move window back
}
else
{
glutFullScreen();
}
isFullscreen = !isFullscreen;
}
// --------------------- Globals for modern path ---------------------
GLuint program = 0;
GLint uni_model= -1, uni_view=-1, uni_proj=-1, uni_viewPos=-1;
GLint uni_useTexture=-1, uni_tex0=-1, uni_objectColor=-1, uni_emission=-1, uni_alpha=-1;
GLint uni_uSun_dir=-1, uni_uSun_on=-1; // we'll use struct locations later via name queries
// cube and plane VAOs
GLuint cubeVAO = 0, cubeVBO = 0;
GLuint planeVAO = 0, planeVBO = 0;
// simple screen quad for posters, overlays
GLuint quadVAO = 0, quadVBO = 0;
// --------------------- Matrix Stack compatibility layer ---------------------
struct MatrixStack {
std::vector<glm::mat4> stack;
//glm::vec3 color = glm::vec3(1.0f); // not used by GL state, but we emulate glColor*
MatrixStack(){ stack.push_back(glm::mat4(1.0f)); }
void push(){ stack.push_back(stack.back()); }
void pop(){ if(stack.size()>1) stack.pop_back(); }
void loadIdentity(){ stack.back() = glm::mat4(1.0f); }
void translate(float x,float y,float z){ stack.back() = glm::translate(stack.back(), glm::vec3(x,y,z)); }
void scale(float x,float y,float z){ stack.back() = glm::scale(stack.back(), glm::vec3(x,y,z)); }
void rotate(float angleDeg, float x,float y,float z){ stack.back() = glm::rotate(stack.back(), glm::radians(angleDeg), glm::vec3(x,y,z)); }
glm::mat4 top() const { return stack.back(); }
glm::vec3 curColor = glm::vec3(1.0f);
} M; // single global stack named M
// Map common legacy calls to our stack — this lets many old draw functions work unchanged:
#define glPushmatrix() M.push()
#define glPopMatrix() M.pop()
#define glLoadIdentity() M.loadIdentity()
#define glTranslatef(x,y,z) M.translate((x),(y),(z))
#define glScalef(x,y,z) M.scale((x),(y),(z))
#define glRotatef(angle,x,y,z) M.rotate((angle),(x),(y),(z))
//#define glColor3f(r,g,b) (M.curColor = glm::vec3((r),(g),(b)))
// --------------------- Create unit cube and plane (GPU) ---------------------
void createCube() {
if(cubeVAO) return;
float verts[] = {
// positions // normals // uvs
// back face (-z)
-0.5f,-0.5f,-0.5f, 0,0,-1, 0.0f,0.0f,
0.5f,-0.5f,-0.5f, 0,0,-1, 1.0f,0.0f,
0.5f, 0.5f,-0.5f, 0,0,-1, 1.0f,1.0f,
0.5f, 0.5f,-0.5f, 0,0,-1, 1.0f,1.0f,
-0.5f, 0.5f,-0.5f, 0,0,-1, 0.0f,1.0f,
-0.5f,-0.5f,-0.5f, 0,0,-1, 0.0f,0.0f,
// front (+z)
-0.5f,-0.5f, 0.5f, 0,0,1, 0.0f,0.0f,
0.5f,-0.5f, 0.5f, 0,0,1, 1.0f,0.0f,
0.5f, 0.5f, 0.5f, 0,0,1, 1.0f,1.0f,
0.5f, 0.5f, 0.5f, 0,0,1, 1.0f,1.0f,
-0.5f, 0.5f, 0.5f, 0,0,1, 0.0f,1.0f,
-0.5f,-0.5f, 0.5f, 0,0,1, 0.0f,0.0f,
// left (-x)
-0.5f, 0.5f, 0.5f, -1,0,0, 1.0f,0.0f,
-0.5f, 0.5f,-0.5f, -1,0,0, 1.0f,1.0f,
-0.5f,-0.5f,-0.5f, -1,0,0, 0.0f,1.0f,
-0.5f,-0.5f,-0.5f, -1,0,0, 0.0f,1.0f,
-0.5f,-0.5f, 0.5f, -1,0,0, 0.0f,0.0f,
-0.5f, 0.5f, 0.5f, -1,0,0, 1.0f,0.0f,
// right (+x)
0.5f, 0.5f, 0.5f, 1,0,0, 1.0f,0.0f,
0.5f, 0.5f,-0.5f, 1,0,0, 1.0f,1.0f,
0.5f,-0.5f,-0.5f, 1,0,0, 0.0f,1.0f,
0.5f,-0.5f,-0.5f, 1,0,0, 0.0f,1.0f,
0.5f,-0.5f, 0.5f, 1,0,0, 0.0f,0.0f,
0.5f, 0.5f, 0.5f, 1,0,0, 1.0f,0.0f,
// bottom (-y)
-0.5f,-0.5f,-0.5f, 0,-1,0, 0.0f,1.0f,
0.5f,-0.5f,-0.5f, 0,-1,0, 1.0f,1.0f,
0.5f,-0.5f, 0.5f, 0,-1,0, 1.0f,0.0f,
0.5f,-0.5f, 0.5f, 0,-1,0, 1.0f,0.0f,
-0.5f,-0.5f, 0.5f, 0,-1,0, 0.0f,0.0f,
-0.5f,-0.5f,-0.5f, 0,-1,0, 0.0f,1.0f,
// top (+y)
-0.5f, 0.5f,-0.5f, 0,1,0, 0.0f,1.0f,
0.5f, 0.5f,-0.5f, 0,1,0, 1.0f,1.0f,
0.5f, 0.5f, 0.5f, 0,1,0, 1.0f,0.0f,
0.5f, 0.5f, 0.5f, 0,1,0, 1.0f,0.0f,
-0.5f, 0.5f, 0.5f, 0,1,0, 0.0f,0.0f,
-0.5f, 0.5f,-0.5f, 0,1,0, 0.0f,1.0f
};
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &cubeVBO);
glBindVertexArray(cubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)(3*sizeof(float))); glEnableVertexAttribArray(1);
glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)(6*sizeof(float))); glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
void createPlane() {
if(planeVAO) return;
float plane[] = {
// positions // normals // uvs
-0.5f,0.0f,-0.5f, 0,1,0, 0,0,
0.5f,0.0f,-0.5f, 0,1,0, 1,0,
0.5f,0.0f, 0.5f, 0,1,0, 1,1,
0.5f,0.0f, 0.5f, 0,1,0, 1,1,
-0.5f,0.0f, 0.5f, 0,1,0, 0,1,
-0.5f,0.0f,-0.5f, 0,1,0, 0,0
};
glGenVertexArrays(1,&planeVAO);
glGenBuffers(1,&planeVBO);
glBindVertexArray(planeVAO);
glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(plane), plane, GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)(3*sizeof(float))); glEnableVertexAttribArray(1);
glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)(6*sizeof(float))); glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
// quad for posters / overlays
void createQuad() {
if(quadVAO) return;
float q[] = {
// pos // normal // uv
-0.5f, 0.5f, 0.0f, 0,0,1, 0,1,
0.5f, 0.5f, 0.0f, 0,0,1, 1,1,
0.5f,-0.5f, 0.0f, 0,0,1, 1,0,
0.5f,-0.5f, 0.0f, 0,0,1, 1,0,
-0.5f,-0.5f, 0.0f, 0,0,1, 0,0,
-0.5f, 0.5f, 0.0f, 0,0,1, 0,1
};
glGenVertexArrays(1,&quadVAO);
glGenBuffers(1,&quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(q), q, GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)(3*sizeof(float))); glEnableVertexAttribArray(1);
glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)(6*sizeof(float))); glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
// --------------------- Load textures ---------------------
GLuint loadTexture(const std::string &path , bool inverse=true) {
int w,h,n;
stbi_set_flip_vertically_on_load(inverse);
unsigned char *data = stbi_load(path.c_str(), &w,&h,&n,0);
if(!data){ std::cerr<<"Failed to load texture: "<<path<<"\n"; return 0; }
GLuint id; glGenTextures(1,&id);
glBindTexture(GL_TEXTURE_2D,id);
GLenum format = (n==4)?GL_RGBA:GL_RGB;
glTexImage2D(GL_TEXTURE_2D,0,format,w,h,0,format,GL_UNSIGNED_BYTE,data);
//glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
stbi_image_free(data);
return id;
}
// --------------------- Create GPU mesh from Mesh ---------------------
void uploadMeshToGPU(Mesh &m) {
if(m.vao) return;
std::vector<float> interleaved;
size_t vcount = m.indices.size();
for(size_t i=0;i<vcount;i++){
unsigned int idx = m.indices[i];
interleaved.push_back(m.vertices[3*idx+0]);
interleaved.push_back(m.vertices[3*idx+1]);
interleaved.push_back(m.vertices[3*idx+2]);
interleaved.push_back(m.normals[3*idx+0]);
interleaved.push_back(m.normals[3*idx+1]);
interleaved.push_back(m.normals[3*idx+2]);
interleaved.push_back(m.texcoords[2*idx+0]);
interleaved.push_back(m.texcoords[2*idx+1]);
}
glGenVertexArrays(1,&m.vao);
glGenBuffers(1,&m.vbo);
glBindVertexArray(m.vao);
glBindBuffer(GL_ARRAY_BUFFER,m.vbo);
glBufferData(GL_ARRAY_BUFFER, interleaved.size()*sizeof(float), interleaved.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)(3*sizeof(float))); glEnableVertexAttribArray(1);
glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8*sizeof(float),(void*)(6*sizeof(float))); glEnableVertexAttribArray(2);
glBindVertexArray(0);
// store index count in indices vector size for draw
}
// --------------------- Draw helpers ---------------------
void setCommonUniforms(const glm::mat4 &view, const glm::mat4 &proj, const glm::vec3 &camPos) {
glUseProgram(program);
glUniformMatrix4fv(uni_view, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(uni_proj, 1, GL_FALSE, glm::value_ptr(proj));
glUniform3fv(uni_viewPos, 1, glm::value_ptr(camPos));
}
void drawCubeWithModel(const glm::mat4 &model, GLuint tex = 0, glm::vec3 color = glm::vec3(1.0f), glm::vec3 emi = glm::vec3(0.0f), float alpha=1.0f) {
glUseProgram(program);
glUniformMatrix4fv(uni_model,1,GL_FALSE,glm::value_ptr(model));
glUniform1i(uni_useTexture, tex?1:0);
glUniform3fv(uni_objectColor, 1, glm::value_ptr(color));
glUniform3fv(uni_emission,1, glm::value_ptr(emi));
glUniform1f(uni_alpha, alpha);
if(tex){
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(uni_tex0, 0);
}else {
// IMPORTANT: When no texture, bind 0 or a default white texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind any texture
}
glBindVertexArray(cubeVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
// === CLEANUP: Very important! ===
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glActiveTexture(GL_TEXTURE0); // Reset active unit (good practice)
glUseProgram(0);
}
void drawPlaneWithModel(const glm::mat4 &model, GLuint tex = 0, glm::vec3 color = glm::vec3(1.0f), glm::vec3 emi = glm::vec3(0.0f), float alpha=1.0f) {
glUseProgram(program);
glUniformMatrix4fv(uni_model,1,GL_FALSE,glm::value_ptr(model));
glUniform1i(uni_useTexture, tex?1:0);
glUniform3fv(uni_objectColor, 1, glm::value_ptr(color));
glUniform3fv(uni_emission,1, glm::value_ptr(emi)); // ← this was already there
glUniform1f(uni_alpha, alpha);
if(tex){
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(uni_tex0, 0);
}else {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
}
glBindVertexArray(planeVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
glUseProgram(0);
}
// Emulate old drawBox(sx,sy,sz, tex, draw) using the matrix stack top
void drawBox(float sx, float sy, float sz, GLuint tex = 0, bool draw = true) {
(void)draw;
glm::mat4 model = M.top() * glm::scale(glm::mat4(1.0f), glm::vec3(sx, sy, sz));
drawCubeWithModel(model, tex, M.curColor, glm::vec3(0.0f), 1.0f);
}
// plane: width w, depth d, repeat UV
void drawPlane(float w, float d, GLuint tex = 0, float repeat = 4.0f) {
// scale our unit plane (size 1x1 centered) into w x d and keep at current transform
glm::mat4 model = M.top() * glm::scale(glm::mat4(1.0f), glm::vec3(w, 1.0f, d));
drawPlaneWithModel(model, tex, M.curColor, glm::vec3(0.0f), 1.0f);
}
// draw a GPU-backed tinyobj mesh
void drawMeshGPU(const Mesh &m, GLuint tex = 0 , glm::vec3 color = glm::vec3(1.0f)) {
if(!m.vao) return;
glUseProgram(program);
glUniformMatrix4fv(uni_model,1,GL_FALSE,glm::value_ptr(M.top()));
// ← THIS WAS MISSING!
glUniform1i(uni_useTexture, (tex != 0) ? 1 : 0);
glUniform3fv(uni_objectColor, 1, glm::value_ptr(color));
glUniform3fv(uni_emission, 1, glm::value_ptr(glm::vec3(0.0f)));
glUniform1f(uni_alpha, 1.0f);
//glUniform1i(uni_useTexture, tex?1:0);
if(tex){
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(uni_tex0,0);
}
glBindVertexArray(m.vao);
// we uploaded as sequential vertices; draw with the number of vertices = indices.size()
GLsizei count = (GLsizei)m.indices.size();
glDrawArrays(GL_TRIANGLES, 0, count);
glBindVertexArray(0);
}
// --------------------- Lighting upload (replaces setupLights) ---------------------
void uploadLights() {
glUseProgram(program);
// SUN directional (window)
glm::vec3 sunDir = glm::vec3(0.5f, -0.3f, -1.0f);
// map to shader uniforms (struct)
GLint loc;
loc = glGetUniformLocation(program, "uSun.dir"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(sunDir));
loc = glGetUniformLocation(program, "uSun.ambient"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.01f*sunlight,0.03f*sunlight,0.05f*sunlight)));
loc = glGetUniformLocation(program, "uSun.diffuse"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.01f*sunlight,0.03f*sunlight,0.06f*sunlight)));
//loc = glGetUniformLocation(program, "uSun.spec"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.1f)));
loc = glGetUniformLocation(program, "uSun.spec"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.1f * sunlight)));
loc = glGetUniformLocation(program, "uSun.on"); if(loc>=0) glUniform1i(loc, 1);
// Lamp point as in original
loc = glGetUniformLocation(program, "uLamp.pos"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(1.9f,1.15f,-3.68f)));
//loc = glGetUniformLocation(program, "uLamp.ambient"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.04f*globalIntensity,0.025f*globalIntensity,0.015f*globalIntensity)));
glm::vec3 lampAmbient = lampOn ? glm::vec3(0.04f,0.025f,0.015f) * globalIntensity : glm::vec3(0.0f);
loc = glGetUniformLocation(program, "uLamp.ambient"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(lampAmbient));
loc = glGetUniformLocation(program, "uLamp.diffuse"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.98f*globalIntensity,0.8f*globalIntensity,0.43f*globalIntensity)));
loc = glGetUniformLocation(program, "uLamp.spec"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(1.0f)));
loc = glGetUniformLocation(program, "uLamp.constant"); if(loc>=0) glUniform1f(loc, 1.0f);
loc = glGetUniformLocation(program, "uLamp.linear"); if(loc>=0) glUniform1f(loc, 0.0f);
loc = glGetUniformLocation(program, "uLamp.quad"); if(loc>=0) glUniform1f(loc, 0.5f);
loc = glGetUniformLocation(program, "uLamp.on"); if(loc>=0) glUniform1i(loc, lampOn?1:0);
// Ceiling point light
loc = glGetUniformLocation(program, "uCeiling.pos"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.0f,3.0f,0.0f)));
//loc = glGetUniformLocation(program, "uCeiling.ambient"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.1f,0.18f,0.25f)));
//loc = glGetUniformLocation(program, "uCeiling.diffuse"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.80f,0.75f,0.72f)));
glm::vec3 ceilingAmbient = ceilingLightOn ? glm::vec3(0.1f,0.18f,0.25f) : glm::vec3(0.0f);
glm::vec3 ceilingDiffuse = ceilingLightOn ? glm::vec3(0.80f,0.75f,0.72f) : glm::vec3(0.0f);
loc = glGetUniformLocation(program, "uCeiling.ambient"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(ceilingAmbient));
loc = glGetUniformLocation(program, "uCeiling.diffuse"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(ceilingDiffuse));
loc = glGetUniformLocation(program, "uCeiling.spec"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.2f)));
loc = glGetUniformLocation(program, "uCeiling.constant"); if(loc>=0) glUniform1f(loc, 1.0f);
loc = glGetUniformLocation(program, "uCeiling.linear"); if(loc>=0) glUniform1f(loc, 0.015f);
loc = glGetUniformLocation(program, "uCeiling.quad"); if(loc>=0) glUniform1f(loc, 0.00005f);
loc = glGetUniformLocation(program, "uCeiling.on"); if(loc>=0) glUniform1i(loc, ceilingLightOn?1:0);
loc = glGetUniformLocation(program, "uDoorLight.pos"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.5f, 0.5f, -1.0f))); // just inside, above door
loc = glGetUniformLocation(program, "uDoorLight.ambient"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.01f*sunlight,0.04f*sunlight,0.09f*sunlight)));
loc = glGetUniformLocation(program, "uDoorLight.diffuse"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.01f*sunlight,0.04f*sunlight,0.1f*sunlight)));
loc = glGetUniformLocation(program, "uDoorLight.spec"); if(loc>=0) glUniform3fv(loc,1,glm::value_ptr(glm::vec3(0.1f)));
loc = glGetUniformLocation(program, "uDoorLight.on"); if(loc>=0) glUniform1i(loc, 1);
}
// --------------------- Load OBJ wrapper ---------------------
bool loadOBJtoMesh(const std::string &path, Mesh &out) {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn, err;
bool ok = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, path.c_str());
if(!warn.empty()) std::cout<<"WARN: "<<warn<<"\n";
if(!err.empty()) std::cerr<<"ERR: "<<err<<"\n";
if(!ok) return false;
out.vertices.clear(); out.normals.clear(); out.texcoords.clear(); out.indices.clear();
for(const auto &shape : shapes) {
for(const auto &idx : shape.mesh.indices){
out.vertices.push_back(attrib.vertices[3*idx.vertex_index+0]);
out.vertices.push_back(attrib.vertices[3*idx.vertex_index+1]);
out.vertices.push_back(attrib.vertices[3*idx.vertex_index+2]);
if(!attrib.normals.empty()){
out.normals.push_back(attrib.normals[3*idx.normal_index+0]);
out.normals.push_back(attrib.normals[3*idx.normal_index+1]);
out.normals.push_back(attrib.normals[3*idx.normal_index+2]);
} else {
out.normals.push_back(0); out.normals.push_back(1); out.normals.push_back(0);
}
if(!attrib.texcoords.empty()){
out.texcoords.push_back(attrib.texcoords[2*idx.texcoord_index+0]);
out.texcoords.push_back(attrib.texcoords[2*idx.texcoord_index+1]);
} else {
out.texcoords.push_back(0); out.texcoords.push_back(0);
}
out.indices.push_back((unsigned int)out.indices.size());
}
}
uploadMeshToGPU(out);
return true;
}
// --------------------- Convenience: drawPoster (replaces glBegin quads) ---------------------
void drawPosterAt(float x, float y, float z, float sx, float sy, float angle,bool turntowall ,bool forscreen,GLuint tex) {
M.push();
M.translate(x,y,z);
M.scale(1.0f,1.0f,1.1f);
if( forscreen){
M.scale(sx,sy,1.0);
}
if (turntowall){
M.rotate(90,1,0,0);
}
M.rotate(angle,0,0,1);
// Plane is in XZ plane in our unit definition; we want it facing +Z with normal +Z
// rotate so quad normal is +Z
// Our quad is in XY plane at z=0, but our plane/quad expects that orientation; so:
glm::mat4 model = M.top() * glm::scale(glm::mat4(1.0f), glm::vec3(sx,sy,1.0f));
drawPlaneWithModel(model, tex);
M.pop();
}
// --------------------- Converted drawRoom (example) ---------------------
void drawRoom() {
// floor
M.push();
M.loadIdentity();
M.translate(0.0f, 0.0f, 0.0f);
drawPlane(10.0f, 8.0f, texMap["floor"], 16.0f);
M.pop();
// back wall
M.push();
M.loadIdentity();
M.translate(0.0f, 2.0f, -4.0f);
M.scale(10.0f, 4.0f, 0.1f);
drawBox(1,1,1, texMap["Wall3"]);
M.pop();
// left wall
M.push();
M.loadIdentity();
M.translate(-5.0f, 2.0f, 0.0f);
M.scale(0.1f, 4.0f, 8.0f);
drawBox(1,1,1, texMap["Wall3"]);
M.pop();
// right wall
M.push(); M.loadIdentity();
M.translate(5.0f,2.0f,0.0f);
M.scale(0.1f,4.0f,8.0f);
drawBox(1,1,1, texMap["Wall3"]);
M.pop();
// ceiling (plain)
M.push(); M.loadIdentity();
M.translate(0, 4.0f, 0);
//M.scale(10.0f, 0.1f, 8.0f);
M.rotate(180,1,0,0);
//setMaterial(0.9f,0.9f,0.9f,2);
//drawBox(1,1,1, texMap["BlueWood"]);
drawPlane(10.0f, 8.0f, texMap["ceiling"], 6.0f);
M.pop();
// front wall sections, door & window simplified
// ======================================
// FRONT WALL (z = +4) WITH DOOR + CENTERED WINDOW
// ======================================
float wallW = 10.0f;
float wallH = 4.0f;
float wallZ = 4.0f;
// --- DOOR (left side) ---
float doorX = -2.0f;
float doorW = 1.11f;
float doorH = 2.2f;
float doorBottom = 0.0f;
// --- WINDOW (perfectly centered horizontally and vertically) ---
float windowW = 1.45f;
float windowH = 1.3f;
float windowX = 1.0f; // center X
float windowY = 1.5f; // center Y (middle of 4m wall)
float windowBottom = windowY - windowH/2.0f; // = 1.2
float windowTop = windowY + windowH/2.0f; // = 2.8
// 1. Left part (before door)
float leftWidth = (doorX - doorW/2.0f) + wallW/2.0f;
M.push();
glTranslatef(leftWidth/2.0f - wallW/2.0f, wallH/2.0f, wallZ);
glScalef(leftWidth, wallH, 0.1f);
drawBox(1,1,1, texMap["Wall3"]);
M.pop();
// 2. Between door and window
float midLeftW = (windowX - windowW/2.0f) - (doorX + doorW/2.0f);
if (midLeftW > 0.01f)
{
M.push();
glTranslatef((doorX + doorW/2.0f) + midLeftW/2.0f, wallH/2.0f, wallZ);
glScalef(midLeftW, wallH, 0.1f);
drawBox(1,1,1, texMap["Wall3"]);
M.pop();
}
// 3. Between window and right wall
float midRightW = wallW/2.0f - (windowX + windowW/2.0f);
if (midRightW > 0.01f)
{
M.push();
glTranslatef((windowX + windowW/2.0f) + midRightW/2.0f, wallH/2.0f, wallZ);
glScalef(midRightW, wallH, 0.1f);
drawBox(1,1,1, texMap["Wall3"]);
M.pop();
}
// 4. Above the door
M.push();
glTranslatef(doorX, doorBottom + doorH + (wallH - doorH)/2.0f, wallZ);
glScalef(doorW, wallH - doorH, 0.1f);
drawBox(1,1,1, texMap["Wall3"]);
M.pop();
// 5. Above the window
float topHeight = wallH - windowTop;
M.push();
glTranslatef(windowX, windowTop + topHeight/2.0f, wallZ);
glScalef(windowW, topHeight, 0.1f);
drawBox(1,1,1, texMap["Wall3"]);
M.pop();
// 6. Below the window (from floor to window bottom)
float bottomHeight = windowBottom - doorBottom;
if (bottomHeight > 0.01f)
{
M.push();
glTranslatef(windowX, bottomHeight/2.0f, wallZ);
glScalef(windowW, bottomHeight, 0.1f);
drawBox(1,1,1, texMap["Wall3"]);
M.pop();
}
//---------- door-----------
// door position
float doorXX = -2.0f; // center
float doorYY = 1.1f; // center Y
float doorZZ = 3.95f; // slightly in front of wall
float doorWW = 1.12f; // full width (trim width)
// === HINGE POSITION (right side) ===
float hingeXX = doorXX + (doorWW / 2.0f); // right side hinge
float hingeYY = doorYY;
float hingeZZ = doorZZ;
M.push();
// ------------------------------
// MOVE door to hinge, rotate, move back
// ------------------------------
glTranslatef(hingeXX, hingeYY, hingeZZ); // move to hinge
glRotatef(-doorAngle, 0, 1, 0); // rotate around hinge
glTranslatef(-hingeXX, -hingeYY, -hingeZZ); // move back to original
// =================================================================
// ORIGINAL DOOR CODE (unchanged)
// =================================================================
// Frame (trim)
M.push();
glTranslatef(doorXX, doorYY, doorZZ);
glRotatef(180, 0, 1, 0);
// Slightly larger than door leaf
M.push();
glScalef(1.12f, 2.32f, 0.08f);
drawBox(1,1,1, texMap["door_panel_b"]);
M.pop();
// Decorative vertical strip
M.push();
glTranslatef(0.0f, -0.03f, 0.026f);
glScalef(0.12f, 1.4f, 0.021f);
drawBox(1,1,1, texMap["door_panel_b"]);
M.pop();
M.pop(); // frame
M.pop(); // hinge transform
//---------------
// =========================================
// WINDOW WITH 2 LEAFS
// =========================================
float frameSize = 0.05f; // thickness of frames
float leafDepth = 0.08f; // depth of window leaf
float leafMargin = 0.01f;
// Top frame
M.push();
glTranslatef(windowX, windowTop + frameSize/2.0f, wallZ + 0.01f);
glScalef(windowW, frameSize, leafDepth);
drawBox(1,1,1, texMap["metal"]);
M.pop();
// Bottom frame
M.push();
glTranslatef(windowX, windowBottom - frameSize/2.0f, wallZ + 0.01f);
glScalef(windowW, frameSize, leafDepth);
drawBox(1,1,1, texMap["metal"]);
M.pop();
// Left vertical frame
M.push();
glTranslatef(windowX - windowW/2.0f - frameSize/2.0f, windowY, wallZ + 0.01f);
glScalef(frameSize, windowH, leafDepth);
drawBox(1,1,1, texMap["metal"]);
M.pop();
// Right vertical frame
M.push();
glTranslatef(windowX + windowW/2.0f + frameSize/2.0f, windowY, wallZ + 0.01f);
glScalef(frameSize, windowH, leafDepth);
drawBox(1,1,1, texMap["metal"]);
M.pop();
// =========================================
// LEFT LEAF
// =========================================
float halfW = windowW / 2.0f - leafMargin;
float halfH = windowH;
// hinge at left side
M.push();
glTranslatef(windowX - halfW, windowY, wallZ - 0.06f);
glRotatef(winAngleLeft, 0, 1, 0);
glTranslatef(halfW / 2.0f, 0, 0);
M.push();
glScalef(halfW, halfH, leafDepth);
drawBox(1,1,1, texMap["WindowTex"]);
M.pop();
// border inside the leaf (thin frame)
float innerT = 0.03f;
// Top border
M.push();
glTranslatef(0, halfH/2.0f - innerT/2.0f, 0);
glScalef(halfW, innerT, leafDepth + 0.01f);
drawBox(1,1,1, texMap["metal"]);
M.pop();
// Bottom border
M.push();
glTranslatef(0, -halfH/2.0f + innerT/2.0f, 0);
glScalef(halfW, innerT, leafDepth + 0.01f);
drawBox(1,1,1, texMap["metal"]);
M.pop();
// Left border
M.push();
glTranslatef(-halfW/2.0f + innerT/2.0f, 0, 0);
glScalef(innerT, halfH, leafDepth + 0.01f);
drawBox(1,1,1, texMap["metal"]);
M.pop();
// Right border
M.push();
glTranslatef(halfW/2.0f - innerT/2.0f, 0, 0);
glScalef(innerT, halfH, leafDepth + 0.01f);
drawBox(1,1,1, texMap["metal"]);
M.pop();
M.pop();
// =========================================
// RIGHT LEAF
// =========================================
// hinge at right side
M.push();
glTranslatef(windowX + halfW, windowY, wallZ - 0.06f);
glRotatef(winAngleRight, 0, 1, 0);
glTranslatef(-halfW / 2.0f, 0, 0);
M.push();
glScalef(halfW, halfH, leafDepth);
drawBox(1,1,1,texMap["WindowTex"]);
M.pop();
// Top border
M.push();
glTranslatef(0, halfH/2.0f - innerT/2.0f, 0);
glScalef(halfW, innerT, leafDepth + 0.01f);
drawBox(1,1,1, texMap["metal"]);
M.pop();
// Bottom border
M.push();
glTranslatef(0, -halfH/2.0f + innerT/2.0f, 0);
glScalef(halfW, innerT, leafDepth + 0.01f);
drawBox(1,1,1, texMap["metal"]);
M.pop();
// Left border
M.push();
glTranslatef(-halfW/2.0f + innerT/2.0f, 0, 0);
glScalef(innerT, halfH, leafDepth + 0.01f);
drawBox(1,1,1, texMap["metal"]);
M.pop();
// Right border
M.push();
glTranslatef(halfW/2.0f - innerT/2.0f, 0, 0);
glScalef(innerT, halfH, leafDepth + 0.01f);
drawBox(1,1,1, texMap["metal"]);
M.pop();
M.pop();
// desk + monitor (example of model mesh usage if available)
if(meshes.count("keyboard.obj")) {
// draw keyboard (the drawMeshGPU reads M.top())
M.push();
M.translate(-1.3f, 0.90f, -1.23f);
M.rotate(180,0,1,0);
M.scale(0.15f,0.25f,0.18f);
drawMeshGPU(meshes["keyboard.obj"], texMap["door_panel_a"]);
M.pop();
}
//wardrobe !
M.push();
M.translate(-4.2f, 0.0f, -3.0f);
M.rotate(-90,0,1,0);
M.scale(1.0f,1.1f,1.0f);
drawMeshGPU(meshes["wardrobe.obj"], texMap["wardrobe"]);
M.pop();
//uefa chamopns league cup!
M.push();
M.translate(-4.0f,2.70f, -2.85f);
M.rotate(-45,0,1,0);
M.scale(0.95f,0.95f,0.95f);
drawMeshGPU(meshes["UclCup.obj"], texMap["UclCup"]);
M.pop();
//draw shelves
float i=1.0; int j=1;
while(i<2.0f){
M.push();
M.translate(0.3f, i*1.05f, -3.85f);
//M.rotate(0,0,1,0);
M.push();
M.scale(0.35f,0.2f,0.25f);
drawMeshGPU(meshes["shelf.obj"], texMap["woood2"]);
M.pop();
if(j%2!=0 ){
M.push();
M.scale(0.1f,0.1f,0.1f);
M.translate(0.0f,(i*1.05)+1.385f,0.0f);
M.rotate(-90,1,0,0);
drawMeshGPU(meshes["book.obj"], texMap["book"]);
M.pop();
//coffee cup (this on in shelves)
M.push();
M.translate(0.47f, (i*0.109), 0.135f);
//M.rotate(-90,0,1,0);
M.scale(0.16f,0.16f,0.16f);
drawMeshGPU(meshes["PlasticCup.obj"], texMap["PlasticCup"]);
M.pop();
}
if (j%2==0 ){
M.push();
M.scale(0.05f,0.045f,0.05f);
M.translate(0.0f,1.97f,1.05f);
//M.rotate(-90,1,0,0);
drawMeshGPU(meshes["pots.obj"], texMap["pots"]);
M.pop();
}
M.pop();
i=i+0.8f;
j++;
}
// draw lamp
M.push();
M.translate(1.9f, 0.7f, -3.68f);