-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraytrace.cpp
More file actions
executable file
·807 lines (593 loc) · 17.5 KB
/
raytrace.cpp
File metadata and controls
executable file
·807 lines (593 loc) · 17.5 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
/******************************************************************/
/* Main raytracer file */
/* */
/* Justin Hust (jkh2367) */
/******************************************************************/
#ifdef _WIN32
#include <windows.h>
#endif
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "common.h"
#include "raytrace.h"
#include "VecMath.h"
#include "RTScene.h"
#include "RTCanvas.h"
#include "RTCamera.h"
#include "RTLight.h"
// random number seed
int iRandomSeed = 0;
// heap-allocated globals
RTCamera *cam;
RTCanvas *canvas;
RTScene *scene;
// **************************************************
enum ControlMode {
ControlMode_None = 0,
ControlMode_Prims = 1,
ControlMode_Lights = 2,
};
// **************************************************
// control mode and control index.
ControlMode cmode;
int cindex;
// **************************************************
void display() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
// render the scene from the camera onto the canvas.
canvas->ClearToGray();
scene->RenderToCanvas(cam, canvas);
canvas->SendToFrameBuffer();
glFlush();
}
// **************************************************
void UserInit(void) {
// init camera
cam = new RTCamera();
cam->vEye.Set(0.0f, 0.0f, 0.0f);
cam->pnear = 1.0f;
cam->fovx = (GLfloat) PI / 6.0f;
// init canvas
canvas = new RTCanvas(iScreenWidth, iScreenHeight);
canvas->SetCanvasProjection();
// init scene
scene = new RTScene();
scene->LoadPresetScene1();
// index of object to manipulate.
cmode = ControlMode_Prims;
cindex = 0;
}
// **************************************************
void UserCleanup() {
if(cam != NULL) delete cam;
cam = NULL;
if(canvas != NULL) delete canvas;
canvas = NULL;
if(scene != NULL) delete scene;
scene = NULL;
}
// **************************************************
void TryToggleVisibility() {
// *** TRYING TO TOGGLE A LIGHT ***
if(cmode == ControlMode_Lights) {
RTLight *l = scene->GetLightWithIndex(cindex);
if(l == NULL) {
printf("Cannot toggle visibility. No light at index %d.\n", cindex);
} else {
if(l->ToggleShow()) {
printf("Light %d turned on.\n", cindex);
} else {
printf("Light %d turned off.\n", cindex);
}
}
// **** TRYING TO MOVE A PRIMITIVE ***
} else {
RTPrimitive *p = scene->GetPrimitiveWithIndex(cindex);
if(p == NULL) {
printf("Cannot toggle visibility. No primitive at index %d.\n", cindex);
} else {
if( p->ToggleShow() )
printf("Primitive %d now showing.\n", cindex);
else
printf("Primitive %d now hidden.\n", cindex);
}
}
}
// **************************************************
void TryCycleMaterial() {
RTPrimitive *p = scene->GetPrimitiveWithIndex(cindex);
if(p == NULL) {
printf("Cannot cycle material. No primitive at index %d.\n", cindex);
} else {
p->CycleMaterial();
}
}
// **************************************************
void TryCycleTexture() {
RTPrimitive *p = scene->GetPrimitiveWithIndex(cindex);
if(p == NULL) {
printf("Cannot cycle texture. No primitive at index %d.\n", cindex);
} else {
p->CycleTexture();
}
}
// **************************************************
void TryMove(GLfloat x, GLfloat y, GLfloat z) {
// *** TRYING TO MOVE A LIGHT ***
if(cmode == ControlMode_Lights) {
RTLight *l = scene->GetLightWithIndex(cindex);
if(l == NULL) {
printf("Cannot move. No light at index %d.\n", cindex);
} else {
l->Move(x, y, z);
printf("Light %d moved to (%0.2f, %0.2f, %0.2f).\n",
cindex,
l->vPos.x,
l->vPos.y,
l->vPos.z);
}
// **** TRYING TO MOVE A PRIMITIVE ***
} else {
RTPrimitive *p = scene->GetPrimitiveWithIndex(cindex);
if(p == NULL) {
printf("Cannot move. No primitive at index %d.\n", cindex);
} else {
p->Move(x / 4.0f, y / 4.0f, z / 4.0f);
printf("Primitive %d moved to (%0.2f, %0.2f, %0.2f).\n",
cindex,
p->GetPos().x,
p->GetPos().y,
p->GetPos().z);
}
}
}
// ***********************************************************
void ToggleAmbient() {
if(scene->Toggle(SETT_AMBIENT))
printf("Ambient Lighting Enabled.\n");
else
printf("Ambient Lighting Disabled.\n");
glutPostRedisplay();
}
// ***********************************************************
void ToggleDiffuse() {
if(scene->Toggle(SETT_DIFFUSE))
printf("Diffuse Lighting Enabled.\n");
else
printf("Diffuse Lighting Disabled.\n");
glutPostRedisplay();
}
// ***********************************************************
void ToggleSpecular() {
if(scene->Toggle(SETT_SPECULAR))
printf("Specular Lighting Enabled.\n");
else
printf("Specular Lighting Disabled.\n");
glutPostRedisplay();
}
// ***********************************************************
void ToggleReflections() {
if(scene->Toggle(SETT_REFLECTION))
printf("Ray Reflection Enabled.\n");
else
printf("Ray Reflection Disabled.\n");
glutPostRedisplay();
}
// ***********************************************************
void ToggleTexturing() {
if(scene->Toggle(SETT_TEXTURING))
printf("Texturing Enabled.\n");
else
printf("Texturing Disabled.\n");
glutPostRedisplay();
}
// ***********************************************************
void ToggleTransparency() {
if(scene->Toggle(SETT_TRANSPARENCY))
printf("Transparency Enabled.\n");
else
printf("Transparency Disabled.\n");
glutPostRedisplay();
}
// ***********************************************************
void ToggleAntiAliasing() {
if(scene->Toggle(SETT_ANTIALIAS))
printf("Anti-aliasing Enabled.\n");
else
printf("Anti-aliasing Disabled.\n");
glutPostRedisplay();
}
// ***********************************************************
void ToggleAttenuation() {
if(scene->Toggle(SETT_ATTENUATION))
printf("Light Attenuation Enabled.\n");
else
printf("Light Attenuation Disabled.\n");
glutPostRedisplay();
}
// ***********************************************************
void AddRandomPrimitive(void) {
scene->AddRandomPrimitive();
printf("Added primitive. Scene now has %d primtives.\n", scene->GetNumPrimitives());
glutPostRedisplay();
}
// ***********************************************************
void AddRandomLight(void) {
scene->AddRandomLight();
printf("Added light. Scene now has %d lights.\n", scene->GetNumLights());
glutPostRedisplay();
}
// ***********************************************************
void LoadPresetScene1(void) {
scene->LoadPresetScene1();
printf("Preset Scene 1 Loaded.\n");
glutPostRedisplay();
}
// ***********************************************************
void LoadPresetScene2(void) {
scene->LoadPresetScene2();
printf("Preset Scene 2 Loaded.\n");
glutPostRedisplay();
}
// ***********************************************************
void LoadPresetScene3(void) {
scene->LoadPresetScene3();
printf("Preset Scene 3 Loaded.\n");
glutPostRedisplay();
}
// ***********************************************************
void ResetScene(void) {
// move to a new random scene.
iRandomSeed++;
srand(iRandomSeed);
scene->CreateRandomScene();
printf("Random scene created.\n");
glutPostRedisplay();
}
// ***********************************************************
void ExportScene(void) {
scene->ExportScript();
}
// ***********************************************************
// custom keyFunc with preset keys
void keyFunc(unsigned char ch, int x, int y) {
bool bPostRedisplay = false;
switch(ch) {
case 'q':
case 'Q':
bPostRedisplay = false;
exit(0);
break;
case '`':
cindex = 0;
if(cmode == ControlMode_Lights)
printf("Now controlling Light 0.\n");
else
printf("Now controlling Primitive 0.\n");
break;
case '1':
cindex = 1;
if(cmode == ControlMode_Lights)
printf("Now controlling Light 1.\n");
else
printf("Now controlling Primitive 1.\n");
break;
case '2':
cindex = 2;
if(cmode == ControlMode_Lights)
printf("Now controlling Light 2.\n");
else
printf("Now controlling Primitive 2.\n");
break;
case '3':
cindex = 3;
if(cmode == ControlMode_Lights)
printf("Now controlling Light 3.\n");
else
printf("Now controlling Primitive 3.\n");
break;
case '4':
cindex = 4;
if(cmode == ControlMode_Lights)
printf("Now controlling Light 4.\n");
else
printf("Now controlling Primitive 4.\n");
break;
case '5':
cindex = 5;
if(cmode == ControlMode_Lights)
printf("Now controlling Light 5.\n");
else
printf("Now controlling Primitive 5.\n");
break;
case '6':
cindex = 6;
if(cmode == ControlMode_Lights)
printf("Now controlling Light 6.\n");
else
printf("Now controlling Primitive 6.\n");
break;
case '7':
LoadPresetScene1();
break;
case '8':
LoadPresetScene2();
break;
case '9':
LoadPresetScene3();
break;
case 'A':
case 'a':
ToggleAmbient();
break;
case 'S':
case 's':
ToggleSpecular();
break;
case 'D':
case 'd':
ToggleDiffuse();
break;
case 'R':
case 'r':
ToggleReflections();
break;
case 'T':
case 't':
ToggleTransparency();
break;
case 'E':
case 'e':
ToggleTexturing();
break;
case 'J':
case 'j':
ToggleAntiAliasing();
break;
case 'U':
case 'u':
ToggleAttenuation();
break;
case 'L':
case 'l':
cmode = ControlMode_Lights;
printf("Control Mode: LIGHTS.\n");
break;
case 'P':
case 'p':
cmode = ControlMode_Prims;
printf("Control Mode: PRIMITIVES.\n");
break;
case 'V':
case 'v':
TryToggleVisibility();
bPostRedisplay = true;
break;
case 'M':
case 'm':
TryCycleMaterial();
bPostRedisplay = true;
break;
case 'X':
case 'x':
TryCycleTexture();
bPostRedisplay = true;
break;
// move controlled object left
case '[':
TryMove(-2.0f, 0.0f, 0.0f);
bPostRedisplay = true;
break;
// move controlled object right
case ']':
TryMove(2.0f, 0.0f, 0.0f);
bPostRedisplay = true;
break;
// move controlled object down
case '-':
TryMove(0.0f, -2.0f, 0.0f);
bPostRedisplay = true;
break;
// move controlled object up
case '=':
TryMove(0.0f, 2.0f, 0.0f);
bPostRedisplay = true;
break;
case ';':
TryMove(0.0f, 0.0f, -2.0f);
bPostRedisplay = true;
break;
case '\'':
TryMove(0.0f, 0.0f, 2.0f);
bPostRedisplay = true;
break;
case '.':
if( scene->DecreaseRayDepth())
printf("Ray depth decreased to %d.\n", scene->GetRayDepth());
else
printf("Ray depth already at 1.\n");
bPostRedisplay = true;
break;
case ',':
if( scene->IncreaseRayDepth())
printf("Ray depth increased to %d.\n", scene->GetRayDepth());
else
printf("Ray depth already at max.\n");
bPostRedisplay = true;
break;
case 'B':
case 'b':
ExportScene();
break;
case 'Z':
case 'z':
ResetScene();
break;
case 'O':
case 'o':
if(cmode == ControlMode_Lights) {
AddRandomLight();
} else {
AddRandomPrimitive();
}
break;
default:
/* Unrecognized keypress */
return;
}
if(bPostRedisplay)
glutPostRedisplay();
}
// **************************************************
// Menu items
enum MENU_TYPE
{
MENU_NONE,
MENU_HOWDOI,
MENU_TOGGLE,
MENU_TOGGLE_AMBIENT,
MENU_TOGGLE_DIFFUSE,
MENU_TOGGLE_SPECULAR,
MENU_TOGGLE_REFLECTIONS,
MENU_TOGGLE_TRANSPARENCY,
MENU_TOGGLE_TEXTURING,
MENU_TOGGLE_ANTIALIAS,
MENU_TOGGLE_ATTENUATION,
MENU_ADD_PRIMITIVE,
MENU_ADD_LIGHT,
MENU_PRESET_1,
MENU_PRESET_2,
MENU_PRESET_3,
MENU_PRESET_4,
MENU_RESET_SCENE,
MENU_EXPORT_SCENE
};
// **************************************************
void menuFunc(int m) {
switch(m) {
case MENU_TOGGLE_AMBIENT:
ToggleAmbient();
break;
case MENU_TOGGLE_DIFFUSE:
ToggleDiffuse();
break;
case MENU_TOGGLE_SPECULAR:
ToggleSpecular();
break;
case MENU_TOGGLE_REFLECTIONS:
ToggleReflections();
break;
case MENU_TOGGLE_TRANSPARENCY:
ToggleTransparency();
break;
case MENU_TOGGLE_TEXTURING:
ToggleTexturing();
break;
case MENU_TOGGLE_ANTIALIAS:
ToggleAntiAliasing();
break;
case MENU_TOGGLE_ATTENUATION:
ToggleAttenuation();
break;
case MENU_ADD_PRIMITIVE:
AddRandomPrimitive();
break;
case MENU_ADD_LIGHT:
AddRandomLight();
break;
case MENU_PRESET_1:
LoadPresetScene1();
break;
case MENU_PRESET_2:
LoadPresetScene2();
break;
case MENU_PRESET_3:
LoadPresetScene3();
break;
case MENU_RESET_SCENE:
ResetScene();
break;
case MENU_EXPORT_SCENE:
ExportScene();
break;
default:
break;
}
}
// **************************************************
void SetupMenu(void) {
// *** toggle menu ***
int menuToggle;
menuToggle = glutCreateMenu(menuFunc);
glutAddMenuEntry("Ambient Lighting ('A')", MENU_TOGGLE_AMBIENT);
glutAddMenuEntry("Diffuse Lighting ('D')", MENU_TOGGLE_DIFFUSE);
glutAddMenuEntry("Specular Lighting ('S')", MENU_TOGGLE_SPECULAR);
glutAddMenuEntry("Reflection Rays ('R')", MENU_TOGGLE_REFLECTIONS);
glutAddMenuEntry("Object Transparency ('T')", MENU_TOGGLE_TRANSPARENCY);
glutAddMenuEntry("Texturing ('E')", MENU_TOGGLE_TEXTURING);
glutAddMenuEntry("Anti-Aliasing ('J')", MENU_TOGGLE_ANTIALIAS);
glutAddMenuEntry("Light Attenuation ('U')", MENU_TOGGLE_ATTENUATION);
// **** add menu ****
int menuAdd;
menuAdd = glutCreateMenu(menuFunc);
glutAddMenuEntry("Add Random Primitive ('P' then 'O')", MENU_ADD_PRIMITIVE);
glutAddMenuEntry("Add Random Light ('L' then 'O')", MENU_ADD_LIGHT);
// **** preset scene menu *****
int menuPreset;
menuPreset = glutCreateMenu(menuFunc);
glutAddMenuEntry("Preset Scene 1 ('7')", MENU_PRESET_1);
glutAddMenuEntry("Preset Scene 2 ('8')", MENU_PRESET_2);
glutAddMenuEntry("Preset Scene 3 ('9')", MENU_PRESET_3);
glutAddMenuEntry("Reset Scene ('Z')", MENU_RESET_SCENE);
// **** preset scene menu *****
int menuEditScene;
menuEditScene = glutCreateMenu(menuFunc);
glutAddMenuEntry("Editing The Scene:", MENU_NONE);
glutAddMenuEntry("------------------", MENU_NONE);
glutAddMenuEntry("Switch between light/primitive mode with 'L' and 'P.'", MENU_NONE);
glutAddMenuEntry("In light mode, 'O' adds a random light.", MENU_NONE);
glutAddMenuEntry("In prim mode, 'O' adds a random primitive.", MENU_NONE);
glutAddMenuEntry("------------------", MENU_NONE);
glutAddMenuEntry("`,1,2,3,4,5,6 selects light or prim at that index.", MENU_NONE);
glutAddMenuEntry("------------------", MENU_NONE);
glutAddMenuEntry("Once selected, the following keys apply:", MENU_NONE);
glutAddMenuEntry("- and + for move down/up", MENU_NONE);
glutAddMenuEntry("[ and ] for move left/right", MENU_NONE);
glutAddMenuEntry("; and ' for move in/out", MENU_NONE);
glutAddMenuEntry("v to toggle visibility", MENU_NONE);
glutAddMenuEntry("x to cycle primitive texture", MENU_NONE);
glutAddMenuEntry("m to cycle primitive material", MENU_NONE);
// *** main menu ***
int menuMain;
menuMain = glutCreateMenu(menuFunc);
glutAddMenuEntry("How do I?", MENU_HOWDOI);
glutAddMenuEntry("---------", MENU_NONE);
glutAddSubMenu("Toggle Settings...", menuToggle);
glutAddSubMenu("Add..", menuAdd);
glutAddSubMenu("Preset Scene...", menuPreset);
glutAddSubMenu("Edit Scene...", menuEditScene);
glutAddMenuEntry("Export Scene Script ('B')", MENU_EXPORT_SCENE);
// Associate a mouse button with menu
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
// **************************************************
int main (int argc, char** argv) {
srand(iRandomSeed);
glutInit(&argc,argv);
glutInitWindowPosition(100,100);
glutInitWindowSize(iScreenWidth,iScreenHeight);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
int win = glutCreateWindow("Project 4 - Raytracer");
glutSetWindow(win);
glutKeyboardFunc(keyFunc);
glutDisplayFunc(display);
SetupMenu();
UserInit();
glutMainLoop();
UserCleanup();
return 0;
}