-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.cpp
More file actions
708 lines (578 loc) · 26.4 KB
/
scene.cpp
File metadata and controls
708 lines (578 loc) · 26.4 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
////////////////////////////////////////////////////////////////////////
// The scene class contains all the parameters needed to define and
// draw a simple scene, including:
// * Geometry
// * Light parameters
// * Material properties
// * viewport size parameters
// * Viewing transformation values
// * others ...
//
// Some of these parameters are set when the scene is built, and
// others are set by the framework in response to user mouse/keyboard
// interactions. All of them can be used to draw the scene.
#include "math.h"
#include <iostream>
#include <stdlib.h>
#include <glbinding/gl/gl.h>
#include <glbinding/Binding.h>
using namespace gl;
#include <glu.h> // For gluErrorString
#define GLM_FORCE_CTOR_INIT
#define GLM_FORCE_RADIANS
#define GLM_SWIZZLE
#include <glm/glm.hpp>
#include <glm/ext.hpp> // For printing GLM objects with to_string
#include "framework.h"
#include "shapes.h"
#include "object.h"
#include "texture.h"
#include "transform.h"
#include "HDR.h"
#include "Texture.h"
const bool fullPolyCount = true; // Use false when emulating the graphics pipeline in software
const float PI = 3.14159f;
const float rad = PI/180.0f; // Convert degrees to radians
glm::mat4 Identity(1.0);
glm::mat4 ShadowMatrix;
glm::mat4 ShadowView;
glm::mat4 ShadowProj;
FBO shadowFbo;
FBO reflectionTopFbo;
FBO reflectionBottomFbo;
HDR* skyIrrMap;
const float grndSize = 100.0; // Island radius; Minimum about 20; Maximum 1000 or so
const float grndOctaves = 4.0; // Number of levels of detail to compute
const float grndFreq = 0.03; // Number of hills per (approx) 50m
const float grndPersistence = 0.03; // Terrain roughness: Slight:0.01 rough:0.05
const float grndLow = -3.0; // Lowest extent below sea level
const float grndHigh = 5.0; // Highest extent above sea level
const bool showSpheres = true;
////////////////////////////////////////////////////////////////////////
// This macro makes it easy to sprinkle checks for OpenGL errors
// throughout your code. Most OpenGL calls can record errors, and a
// careful programmer will check the error status *often*, perhaps as
// often as after every OpenGL call. At the very least, once per
// refresh will tell you if something is going wrong.
#define CHECKERROR {GLenum err = glGetError(); if (err != GL_NO_ERROR) { fprintf(stderr, "OpenGL error (at line scene.cpp:%d): %s\n", __LINE__, gluErrorString(err)); exit(-1);} }
// Create an RGB color from human friendly parameters: hue, saturation, value
glm::vec3 HSV2RGB(const float h, const float s, const float v)
{
if (s == 0.0)
return glm::vec3(v,v,v);
int i = (int)(h*6.0) % 6;
float f = (h*6.0f) - i;
float p = v*(1.0f - s);
float q = v*(1.0f - s*f);
float t = v*(1.0f - s*(1.0f-f));
if (i == 0) return glm::vec3(v,t,p);
else if (i == 1) return glm::vec3(q,v,p);
else if (i == 2) return glm::vec3(p,v,t);
else if (i == 3) return glm::vec3(p,q,v);
else if (i == 4) return glm::vec3(t,p,v);
else /*i == 5*/ return glm::vec3(v,p,q);
}
////////////////////////////////////////////////////////////////////////
// Constructs a hemisphere of spheres of varying hues
Object* SphereOfSpheres(Shape* SpherePolygons)
{
Object* ob = new Object(NULL, nullId);
for (float angle=0.0; angle<360.0; angle+= 18.0)
for (float row=0.075; row<PI/2.0; row += PI/2.0/6.0) {
glm::vec3 hue = HSV2RGB(angle/360.0, 1.0f-2.0f*row/PI, 1.0f);
Object* sp = new Object(SpherePolygons, spheresId,
hue, glm::vec3(1.0, 1.0, 1.0), 120.0, false, NULL, NULL);
float s = sin(row);
float c = cos(row);
ob->add(sp, Rotate(2,angle)*Translate(c,0,s)*Scale(0.075*c,0.075*c,0.075*c));
}
return ob;
}
////////////////////////////////////////////////////////////////////////
// Constructs a -1...+1 quad (canvas) framed by four (elongated) boxes
Object* FramedPicture(const glm::mat4& modelTr, const int objectId,
Shape* BoxPolygons, Shape* QuadPolygons, Texture* Texture=NULL)
{
// This draws the frame as four (elongated) boxes of size +-1.0
float w = 0.05; // Width of frame boards.
Object* frame = new Object(NULL, nullId);
Object* ob;
glm::vec3 woodColor(87.0/255.0,51.0/255.0,35.0/255.0);
ob = new Object(BoxPolygons, frameId,
woodColor, glm::vec3(0.2, 0.2, 0.2), 10.0, false, NULL, NULL);
frame->add(ob, Translate(0.0, 0.0, 1.0+w)*Scale(1.0, w, w));
frame->add(ob, Translate(0.0, 0.0, -1.0-w)*Scale(1.0, w, w));
frame->add(ob, Translate(1.0+w, 0.0, 0.0)*Scale(w, w, 1.0+2*w));
frame->add(ob, Translate(-1.0-w, 0.0, 0.0)*Scale(w, w, 1.0+2*w));
if (Texture)
{
ob = new Object(QuadPolygons, objectId,
woodColor, glm::vec3(0.0, 0.0, 0.0), 10.0, false, Texture, NULL);
}
else
{
ob = new Object(QuadPolygons, objectId,
woodColor, glm::vec3(0.0, 0.0, 0.0), 10.0, false, NULL, NULL);
}
frame->add(ob, Rotate(0,90));
return frame;
}
////////////////////////////////////////////////////////////////////////
// InitializeScene is called once during setup to create all the
// textures, shape VAOs, and shader programs as well as setting a
// number of other parameters.
void Scene::InitializeScene()
{
glEnable(GL_DEPTH_TEST);
CHECKERROR;
// @@ Initialize interactive viewing variables here. (spin, tilt, ry, front back, ...)
spin = 0.0;
tilt = 30.0;
tx = 0.0;
ty = 0.0;
zoom = 25.0;
ry = 0.4;
front = 0.5;
back = 5000.0;
speed = 10.0;
eye = glm::vec3(0.0, -20.0, 0.0);
transformation_mode = false;
last_refresh_time = 0.0;
w_down = false;
a_down = false;
s_down = false;
d_down = false;
// Set initial light parameters
lightSpin = 150.0;
lightTilt = -45.0;
lightDist = 100.0;
// @@ Perhaps initialize additional scene lighting values here. (lightVal, lightAmb)
CHECKERROR;
objectRoot = new Object(NULL, nullId);
shadowFbo.CreateFBO(1000, 1000);
reflectionTopFbo.CreateFBO(1024, 1024);
reflectionBottomFbo.CreateFBO(1024, 1024);
// Enable OpenGL depth-testing
glEnable(GL_DEPTH_TEST);
// Create the lighting shader program from source code files.
// @@ Initialize additional shaders if necessary
lightingProgram = new ShaderProgram();
lightingProgram->AddShader("final.vert", GL_VERTEX_SHADER);
lightingProgram->AddShader("final.frag", GL_FRAGMENT_SHADER);
lightingProgram->AddShader("lighting.vert", GL_VERTEX_SHADER);
lightingProgram->AddShader("lighting.frag", GL_FRAGMENT_SHADER);
shadowProgram = new ShaderProgram();
shadowProgram->AddShader("shadow.frag", GL_FRAGMENT_SHADER);
shadowProgram->AddShader("shadow.vert", GL_VERTEX_SHADER);
reflectionProgram = new ShaderProgram();
reflectionProgram->AddShader("reflection.frag", GL_FRAGMENT_SHADER);
reflectionProgram->AddShader("reflection.vert", GL_VERTEX_SHADER);
reflectionProgram->AddShader("lighting.vert", GL_VERTEX_SHADER);
reflectionProgram->AddShader("lighting.frag", GL_FRAGMENT_SHADER);
glBindAttribLocation(lightingProgram->programId, 0, "vertex");
glBindAttribLocation(lightingProgram->programId, 1, "vertexNormal");
glBindAttribLocation(lightingProgram->programId, 2, "vertexTexture");
glBindAttribLocation(lightingProgram->programId, 3, "vertexTangent");
lightingProgram->LinkProgram();
glBindAttribLocation(shadowProgram->programId, 0, "vertex");
glBindAttribLocation(shadowProgram->programId, 1, "vertexNormal");
glBindAttribLocation(shadowProgram->programId, 2, "vertexTexture");
glBindAttribLocation(shadowProgram->programId, 3, "vertexTangent");
shadowProgram->LinkProgram();
glBindAttribLocation(reflectionProgram->programId, 0, "vertex");
glBindAttribLocation(reflectionProgram->programId, 1, "vertexNormal");
glBindAttribLocation(reflectionProgram->programId, 2, "vertexTexture");
glBindAttribLocation(reflectionProgram->programId, 3, "vertexTangent");
reflectionProgram->LinkProgram();
// Create all the Polygon shapes
proceduralground = new ProceduralGround(grndSize, 400,
grndOctaves, grndFreq, grndPersistence,
grndLow, grndHigh);
Shape* TeapotPolygons = new Teapot(fullPolyCount?12:2);
Shape* BoxPolygons = new Box();
Shape* SpherePolygons = new Sphere(32);
Shape* RoomPolygons = new Ply("room.ply");
Shape* FloorPolygons = new Plane(10.0, 10);
Shape* QuadPolygons = new Quad();
Shape* SeaPolygons = new Plane(2000.0, 50);
Shape* GroundPolygons = proceduralground;
// Various colors used in the subsequent models
glm::vec3 woodColor(87.0/255.0, 51.0/255.0, 35.0/255.0);
glm::vec3 brickColor(134.0/255.0, 60.0/255.0, 56.0/255.0);
glm::vec3 floorColor(6*16/255.0, 5.5*16/255.0, 3*16/255.0);
glm::vec3 brassColor(0.5, 0.5, 0.1);
glm::vec3 grassColor(62.0/255.0, 102.0/255.0, 38.0/255.0);
glm::vec3 waterColor(0.3, 0.3, 1.0);
// Ks values in a range appropriate range for BRDF calculations. (Phong needs 10* this.)
glm::vec3 noSpec(0.0, 0.0, 0.0);
glm::vec3 brightSpec(0.03, 0.03, 0.03);
// Creates all the models from which the scene is composed. Each
// is created with a polygon shape (possibly NULL), a
// transformation, and the surface lighting parameters Kd, Ks, and
// alpha.
// @@ This is where you could read in all the textures and
// associate them with the various objects being created in the
// next dozen lines of code.
Texture* roomTexture = new Texture("textures/Standard_red_pxr128.png");
Texture* floorTexture = new Texture("textures/6670-diffuse.jpg");
Texture* teapotTexture = new Texture("textures/cracks.png");
Texture* podiumTexture = new Texture("textures/Brazilian_rosewood_pxr128.png");
Texture* groundTexture = new Texture("textures/grass.jpg");
Texture* rightFrameTexture = new Texture("textures/my-house-01.png");
Texture* skyTexture = new Texture("skys/Ocean.png");
HDR* skyHDR = new HDR("skys/Road_to_MonumentValley_Ref.hdr");
skyIrrMap = new HDR("skys/Road_to_MonumentValley_Ref.irr.hdr"); // is this how you really read it in?
Texture* roomNormal = new Texture("textures/Standard_red_pxr128_normal.png");
Texture* seaNormal = new Texture("textures/ripples_normalmap.png");
Texture* podiumNormal = new Texture("textures/Brazilian_rosewood_pxr128_normal.png");
Texture* floorNormal = new Texture("textures/6670-normal.jpg");
// @@ To change an object's surface parameters (Kd, Ks, or alpha),
// modify the following lines.
central = new Object(NULL, nullId);
anim = new Object(NULL, nullId);
room = new Object(RoomPolygons, roomId, brickColor, noSpec, 2, false, roomTexture, roomNormal);
floor = new Object(FloorPolygons, floorId, floorColor, noSpec, 2, false, floorTexture, floorNormal);
teapot = new Object(TeapotPolygons, teapotId, brassColor, brightSpec, 100, true, teapotTexture);
podium = new Object(BoxPolygons, boxId, glm::vec3(woodColor), brightSpec, 5, false, podiumTexture, podiumNormal);
sky = new Object(SpherePolygons, skyId, noSpec, noSpec, 0, false, skyHDR);
ground = new Object(GroundPolygons, groundId, grassColor, noSpec, 3, false, groundTexture);
sea = new Object(SeaPolygons, seaId, waterColor, brightSpec, 100, false, skyTexture, seaNormal);
leftFrame = FramedPicture(Identity, lPicId, BoxPolygons, QuadPolygons);
rightFrame = FramedPicture(Identity, rPicId, BoxPolygons, QuadPolygons, rightFrameTexture);
spheres = SphereOfSpheres(SpherePolygons);
#ifdef REFL
spheres->drawMe = true;
#else
spheres->drawMe = false;
#endif
// @@ To change the scene hierarchy, examine the hierarchy created
// by the following object->add() calls and adjust as you wish.
// The objects being manipulated and their polygon shapes are
// created above here.
// Scene is composed of sky, ground, sea, room and some central models
if (fullPolyCount) {
objectRoot->add(sky, Scale(2000.0, 2000.0, 2000.0)); //check scale, but this probably fine
objectRoot->add(sea);
objectRoot->add(ground); }
objectRoot->add(central);
#ifndef REFL
objectRoot->add(room, Translate(0.0, 0.0, 0.02));
#endif
objectRoot->add(floor, Translate(0.0, 0.0, 0.02));
// Central model has a rudimentary animation (constant rotation on Z)
animated.push_back(anim);
// Central contains a teapot on a podium and an external sphere of spheres
central->add(podium, Translate(0.0, 0,0));
central->add(anim, Translate(0.0, 0,0));
anim->add(teapot, Translate(0,0,1)*Scale(0.31,0.31,0.31));
if (fullPolyCount)
anim->add(spheres, Translate(0.0, 0.0, 0.0)*Scale(16, 16, 16));
// Room contains two framed pictures
if (fullPolyCount) {
room->add(leftFrame, Translate(-1.5, 9.85, 1.)*Scale(0.8, 0.8, 0.8));
room->add(rightFrame, Translate( 1.5, 9.85, 1.)*Scale(0.8, 0.8, 0.8)); }
CHECKERROR;
// Options menu stuff
show_demo_window = false;
}
void Scene::DrawMenu()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if (ImGui::BeginMainMenuBar()) {
// This menu demonstrates how to provide the user a list of toggleable settings.
if (ImGui::BeginMenu("Objects")) {
if (ImGui::MenuItem("Draw spheres", "", spheres->drawMe)) {spheres->drawMe ^= true; }
if (ImGui::MenuItem("Draw walls", "", room->drawMe)) {room->drawMe ^= true; }
if (ImGui::MenuItem("Draw ground/sea", "", ground->drawMe)){ground->drawMe ^= true;
sea->drawMe = ground->drawMe;}
ImGui::EndMenu(); }
// This menu demonstrates how to provide the user a choice
// among a set of choices. The current choice is stored in a
// variable named "mode" in the application, and sent to the
// shader to be used as you wish.
if (ImGui::BeginMenu("Menu ")) {
if (ImGui::MenuItem("<sample menu of choices>", "", false, false)) {}
if (ImGui::MenuItem("Do nothing 0", "", mode==0)) { mode=0; }
if (ImGui::MenuItem("Do nothing 1", "", mode==1)) { mode=1; }
if (ImGui::MenuItem("Do nothing 2", "", mode==2)) { mode=2; }
ImGui::EndMenu(); }
ImGui::EndMainMenuBar(); }
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void Scene::BuildTransforms()
{
// @@ When you are ready to try interactive viewing, replace the
// following hard coded values for WorldProj and WorldView with
// transformation matrices calculated from variables such as spin,
// tilt, tr, ry, front, and back.
//WorldProj[0][0]= 2.368;
//WorldProj[1][0]= -0.800;
//WorldProj[2][0]= 0.000;
//WorldProj[3][0]= 0.000;
//WorldProj[0][1]= 0.384;
//WorldProj[1][1]= 1.136;
//WorldProj[2][1]= 2.194;
//WorldProj[3][1]= 0.000;
//WorldProj[0][2]= 0.281;
//WorldProj[1][2]= 0.831;
//WorldProj[2][2]= -0.480;
//WorldProj[3][2]= 42.451;
//WorldProj[0][3]= 0.281;
//WorldProj[1][3]= 0.831;
//WorldProj[2][3]= -0.480;
//WorldProj[3][3]= 43.442;
//
//WorldView[3][0]= 0.0;
//WorldView[3][1]= 0.0;
//WorldView[3][2]= 0.0;
if (transformation_mode)
{
WorldProj = Perspective(rx, ry, front, back);
WorldView = Rotate(6, tilt - 90) * Rotate(2, spin) *
Translate(-eye.x, -eye.y, -eye.z);
}
else
{
WorldProj = Perspective(rx, ry, front, back);
WorldView = Translate(tx, ty, -zoom) * Rotate(6, tilt - 90) * Rotate(2, spin);
}
// @@ Print the two matrices (in column-major order) for
// comparison with the project document.
//std::cout << "WorldView: " << glm::to_string(WorldView) << std::endl;
//std::cout << "WorldProj: " << glm::to_string(WorldProj) << std::endl;
}
////////////////////////////////////////////////////////////////////////
// Procedure DrawScene is called whenever the scene needs to be
// drawn. (Which is often: 30 to 60 times per second are the common
// goals.)
void Scene::DrawScene()
{
// Set the viewport
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
// recalculate eye position
// Calculate time step
double current_time = glfwGetTime();
time_since_last_refresh = current_time - last_refresh_time;
step = speed * time_since_last_refresh;
last_refresh_time = current_time;
// Update eye position based on keys pressed
if (w_down)
{
eye += step * glm::vec3(sin(spin * rad), cos(spin * rad), 0.0);
}
else if (s_down)
{
eye -= step * glm::vec3(sin(spin * rad), cos(spin * rad), 0.0);
}
else if (a_down)
{
eye -= step * glm::vec3(cos(spin * rad), -sin(spin * rad), 0.0);
}
else if (d_down)
{
eye += step * glm::vec3(cos(spin * rad), -sin(spin * rad), 0.0);
}
eye.z = proceduralground->HeightAt(eye.x, eye.y) + 2.0;
CHECKERROR;
// Calculate the light's position from lightSpin, lightTilt, lightDist
lightPos = glm::vec3(lightDist*cos(lightSpin*rad)*sin(lightTilt*rad),
lightDist*sin(lightSpin*rad)*sin(lightTilt*rad),
lightDist*cos(lightTilt*rad));
// Update position of any continuously animating objects
double atime = 360.0*glfwGetTime()/36;
for (std::vector<Object*>::iterator m=animated.begin(); m<animated.end(); m++)
(*m)->animTr = Rotate(2, atime);
rx = ry * width / height;
BuildTransforms();
// The lighting algorithm needs the inverse of the WorldView matrix
WorldInverse = glm::inverse(WorldView);
CreateShader();
}
void Scene::CreateShader()
{
////////////////////////////////////////////////////////////////////////////////
// Anatomy of a pass:
// Choose a shader (create the shader in InitializeScene above)
// Choose and FBO/Render-Target (if needed; create the FBO in InitializeScene above)
// Set the viewport (to the pixel size of the screen or FBO)
// Clear the screen.
// Set the uniform variables required by the shader
// Draw the geometry
// Unset the FBO (if one was used)
// Unset the shader
////////////////////////////////////////////////////////////////////////////////
CHECKERROR;
int loc, programId;
////////////////////////////////////////////////////////////////////////////////
// Shadow pass
////////////////////////////////////////////////////////////////////////////////
shadowProgram->UseShader();
programId = shadowProgram->programId;
shadowFbo.BindFBO();
ShadowView = LookAt(lightPos, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
ShadowProj = Perspective(40 / lightDist, 40 / lightDist, front, back);
// Set the viewport, and clear the screen
glViewport(0, 0, 1000, 1000); //set variable
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set Light
glm::vec3 Light(3, 3, 3);
glm::vec3 Ambient(0.4, 0.4, 0.4);
loc = glGetUniformLocation(programId, "ProjectionMatrix");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(ShadowProj));
loc = glGetUniformLocation(programId, "ViewMatrix");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(ShadowView));
// Draw all objects (This recursively traverses the object hierarchy.)
CHECKERROR;
objectRoot->Draw(shadowProgram, Identity);
CHECKERROR;
// Turn off the shader
shadowFbo.UnbindFBO();
shadowProgram->UnuseShader();
////////////////////////////////////////////////////////////////////////////////
// End of Shadow pass
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Reflection pass
////////////////////////////////////////////////////////////////////////////////
reflectionProgram->UseShader();
programId = reflectionProgram->programId;
shadowFbo.BindTexture(2, programId, "shadowMap");
sky->texture->BindTexture(5, programId, "skyboxMap");
skyIrrMap->BindTexture(6, programId, "irradianceMap");
reflectionTopFbo.BindFBO();
// Set the viewport, and clear the screen
glViewport(0, 0, reflectionTopFbo.width, reflectionTopFbo.height);
glClearColor(1.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 B = Translate(0.5f, 0.5f, 0.5f) * Scale(0.5f, 0.5f, 0.5f);
ShadowMatrix = B * ShadowProj * ShadowView;
glm::vec3 eye = glm::vec3(0.0f, 0.0f, 1.5f);
float reflectDir = 1.0f;
loc = glGetUniformLocation(programId, "Eye");
glUniform3fv(loc, 1, &(eye[0]));
loc = glGetUniformLocation(programId, "ReflectDir");
glUniform1fv(loc, 1, &reflectDir);
loc = glGetUniformLocation(programId, "ShadowMatrix");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(ShadowMatrix));
loc = glGetUniformLocation(programId, "WorldProj");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(WorldProj));
loc = glGetUniformLocation(programId, "WorldInverse");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(WorldInverse));
loc = glGetUniformLocation(programId, "lightPos");
glUniform3fv(loc, 1, &(lightPos[0]));
loc = glGetUniformLocation(programId, "Light");
glUniform3fv(loc, 1, &(Light[0]));
loc = glGetUniformLocation(programId, "Ambient");
glUniform3fv(loc, 1, &(Ambient[0]));
loc = glGetUniformLocation(programId, "mode");
glUniform1i(loc, mode);
// Draw all objects (This recursively traverses the object hierarchy.)
CHECKERROR;
teapot->drawMe = false; // Do not draw the teapot in reflection
objectRoot->Draw(reflectionProgram, Identity);
CHECKERROR;
sky->texture->UnbindTexture(5);
skyIrrMap->UnbindTexture(6);
// Turn off the shader
reflectionProgram->UnuseShader();
reflectionTopFbo.UnbindFBO();
//////////////////////////
// Bottom Reflection pass
//////////////////////////
reflectionProgram->UseShader();
reflectionBottomFbo.BindFBO();
// Set the viewport, and clear the screen
glViewport(0, 0, reflectionBottomFbo.width, reflectionBottomFbo.height);
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
reflectDir = -1.0f;
loc = glGetUniformLocation(programId, "Eye");
glUniform3fv(loc, 1, &(eye[0]));
loc = glGetUniformLocation(programId, "ReflectDir");
glUniform1fv(loc, 1, &reflectDir);
loc = glGetUniformLocation(programId, "ShadowMatrix");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(ShadowMatrix));
loc = glGetUniformLocation(programId, "WorldProj");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(WorldProj));
loc = glGetUniformLocation(programId, "WorldView");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(WorldView));
loc = glGetUniformLocation(programId, "WorldInverse");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(WorldInverse));
loc = glGetUniformLocation(programId, "lightPos");
glUniform3fv(loc, 1, &(lightPos[0]));
loc = glGetUniformLocation(programId, "Light");
glUniform3fv(loc, 1, &(Light[0]));
loc = glGetUniformLocation(programId, "Ambient");
glUniform3fv(loc, 1, &(Ambient[0]));
loc = glGetUniformLocation(programId, "mode");
glUniform1i(loc, mode);
// Draw all objects (This recursively traverses the object hierarchy.)
CHECKERROR;
teapot->drawMe = false; // Do not draw the teapot in reflection
objectRoot->Draw(reflectionProgram, Identity);
CHECKERROR;
// Turn off the shader
shadowFbo.UnbindTexture(2);
reflectionProgram->UnuseShader();
reflectionBottomFbo.UnbindFBO();
////////////////////////////////////////////////////////////////////////////////
// Lighting pass
////////////////////////////////////////////////////////////////////////////////
// Choose the lighting shader
lightingProgram->UseShader();
programId = lightingProgram->programId;
shadowFbo.BindTexture(2, programId, "shadowMap");
reflectionTopFbo.BindTexture(3, programId, "reflectionTop");
reflectionBottomFbo.BindTexture(4, programId, "reflectionBottom");
sky->texture->BindTexture(5, programId, "skyboxMap");
skyIrrMap->BindTexture(6, programId, "irradianceMap");
// Set the viewport, and clear the screen
glViewport(0, 0, width, height);
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// @@ The scene specific parameters (uniform variables) used by
// the shader are set here. Object specific parameters are set in
// the Draw procedure in object.cpp
B = Translate(0.5f, 0.5f, 0.5f) * Scale(0.5f, 0.5f, 0.5f);
ShadowMatrix = B * ShadowProj * ShadowView;
// Set Eye
eye = glm::vec3(WorldInverse * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
loc = glGetUniformLocation(programId, "Eye");
glUniform3fv(loc, 1, &(eye[0]));
loc = glGetUniformLocation(programId, "ShadowMatrix");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(ShadowMatrix));
loc = glGetUniformLocation(programId, "WorldProj");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(WorldProj));
loc = glGetUniformLocation(programId, "WorldView");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(WorldView));
loc = glGetUniformLocation(programId, "WorldInverse");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(WorldInverse));
loc = glGetUniformLocation(programId, "lightPos");
glUniform3fv(loc, 1, &(lightPos[0]));
loc = glGetUniformLocation(programId, "Light");
glUniform3fv(loc, 1, &(Light[0]));
loc = glGetUniformLocation(programId, "Ambient");
glUniform3fv(loc, 1, &(Ambient[0]));
loc = glGetUniformLocation(programId, "mode");
glUniform1i(loc, mode);
// Draw all objects (This recursively traverses the object hierarchy.)
CHECKERROR;
teapot->drawMe = true; // Restore drawing of the teapot
objectRoot->Draw(lightingProgram, Identity);
CHECKERROR;
shadowFbo.UnbindTexture(2);
reflectionTopFbo.UnbindTexture(3);
reflectionBottomFbo.UnbindTexture(4);
sky->texture->UnbindTexture(5);
skyIrrMap->UnbindTexture(6);
// Turn off the shader
lightingProgram->UnuseShader();
////////////////////////////////////////////////////////////////////////////////
// End of Lighting pass
////////////////////////////////////////////////////////////////////////////////
}