-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasst8.cpp
More file actions
executable file
·878 lines (714 loc) · 28.2 KB
/
asst8.cpp
File metadata and controls
executable file
·878 lines (714 loc) · 28.2 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
////////////////////////////////////////////////////////////////////////
//
// Harvard University
// CS175 : Computer Graphics
// Professor Steven Gortler
//
////////////////////////////////////////////////////////////////////////
#include <cstddef>
#include <vector>
#include <list>
#include <string>
#include <memory>
#include <map>
#include <fstream>
#include <stdexcept>
#if __GNUG__
# include <tr1/memory>
#endif
#include <GL/glew.h>
#ifdef __MAC__
# include <GLUT/glut.h>
#else
# include <GL/glut.h>
#endif
#include "ppm.h"
#include "cvec.h"
#include "matrix4.h"
#include "rigtform.h"
#include "glsupport.h"
#include "geometrymaker.h"
#include "geometry.h"
#include "arcball.h"
#include "scenegraph.h"
#include "sgutils.h"
#include "asstcommon.h"
#include "drawer.h"
#include "picker.h"
#include "mesh.h"
// include new headers
#include "spring.h"
#include "ball.h"
#include "cloth.h"
using namespace std;
using namespace tr1;
// G L O B A L S ///////////////////////////////////////////////////
const bool g_Gl2Compatible = true;
static const float g_frustMinFov = 60.0; // A minimal of 60 degree field of view
static float g_frustFovY = g_frustMinFov; // FOV in y direction (updated by updateFrustFovY)
static const float g_frustNear = -0.1; // near plane
static const float g_frustFar = -50.0; // far plane
static const float g_groundY = -2.5; // y coordinate of the ground
static const float g_groundSize = 10.0; // half the ground length
enum SkyMode {WORLD_SKY=0, SKY_SKY=1};
static int g_windowWidth = 512;
static int g_windowHeight = 512;
static bool g_mouseClickDown = false; // is the mouse button pressed
static bool g_mouseLClickButton, g_mouseRClickButton, g_mouseMClickButton;
static int g_mouseClickX, g_mouseClickY; // coordinates for mouse click event
static SkyMode g_activeCameraFrame = WORLD_SKY;
static bool g_displayArcball = false;
static double g_arcballScreenRadius = 100; // number of pixels
static double g_arcballScale = 1;
static bool g_pickingMode = false;
// --------- Materials
static shared_ptr<Material> g_bumpFloorMat,
g_arcballMat,
g_pickingMat,
g_lightMat,
g_bunnyMat; // for the bunny
shared_ptr<Material> g_overridingMaterial;
// --------- Geometry
typedef SgGeometryShapeNode MyShapeNode;
// Vertex buffer and index buffer associated with the ground and light
// geometry
static shared_ptr<Geometry> g_ground, g_sphere;
static shared_ptr<SimpleGeometryPN> g_bunnyGeometry;
static Mesh g_bunnyMesh;
// --------- Scene
static shared_ptr<SgRootNode> g_world;
static shared_ptr<SgRbtNode> g_skyNode, g_groundNode, g_light1, g_light2, g_bunnyNode;
static shared_ptr<SgRbtNode> g_currentCameraNode;
static shared_ptr<SgRbtNode> g_currentPickedRbtNode;
/// FINAL PROJECT GLOBALS ///
Cloth g_cloth; // global cloth variable
static bool g_clothWire = false; // cloth is wirefram or filled
static Cvec3 clothTranslation = Cvec3(-2.5,-.5,-2.5); // center cloth on screen
static shared_ptr<SimpleGeometryPN> g_clothGeometry; // vertices of cloth
static shared_ptr<SgRbtNode> g_clothNode; // cloth node
// new cloth and sphere materials
static shared_ptr<Material> g_clothMat;
static shared_ptr<Material> g_sphereMat;
// Struct that contains information for rendering nodes on the screen
// and detecting collisions with the cloth.
struct sphereComp {
Cvec3 pos;
float radius;
shared_ptr<Geometry> geometry;
shared_ptr<SgRbtNode> node;
// functions for easily setting values
sphereComp& setPos(Cvec3 p) { pos = p; return *this;}
sphereComp& setRad(float r) { radius = r; return *this;}
};
static bool g_bunny = false; // render bunny or sphere
static bool g_isWind = false;
static std::vector<struct sphereComp> g_spheres; // handles collisions with cloth
static std::vector<struct sphereComp> g_normalSphere; // vector containing a single sphere
static std::vector<struct sphereComp> g_bunnySpheres; // vector containing spheres that make up bunny
static const Cvec3 g_gravity(0, -0.025, 0); // gravity vector
static const Cvec3 g_wind(-.02, 0, 0); // wind vector
static int g_animateFramesPerSecond = 60; // cloth frames to render per second
///////////////// END OF G L O B A L S //////////////////////////////////////////////////
static VertexPN getVertexPN(Mesh& m, const int face, const int vertex);
static VertexPNX getFurVertexPNX(Mesh& m, const int face, const int vertex, int shellNum);
/// FINAL PROJECT SIGNATURES ///
static void initCloth(); // initialize global cloth object
static void animateCloth(int dontCare); // cloth animation loop
static void clothCollisions(); // detect collisions with spheres and updates vertices
static void initSpheres(); // initialize objects composed of spheres
static void initNormalSphere (); // create single centered sphere
static void initBunnySpheres (); // create sphere bunny
static void initSphereGeometry(std::vector<struct sphereComp> &v); // initialize geometry of sphere object
static void initSphereNodes(); // create nodes for each of the spheres in the objects
// initializes nodes for all spheres in an object
static void initSphereNode(std::vector<struct sphereComp> &v, bool add);
// remove all of an objects sphere nodes from world
static void removeSphereNodes(std::vector<struct sphereComp> &v);
// add all of an objects sphere nodes from world
static void addSphereNodes(std::vector<struct sphereComp> &v);
/// END FINAL PROJECT SIGNATURES ///
/// FINAL PROJECT FUNCTIONS ///
// initializes a global cloth object and gets its geometry
static void initCloth() {
g_cloth.loadDimensions(5,5,45,45);
g_clothGeometry.reset(new SimpleGeometryPN(&(g_cloth.getVertices()[0]), g_cloth.getVertices().size()));
}
// cloth animation loop
static void animateCloth(int dontCare) {
g_cloth.addForce(g_gravity); // add forces
if (g_isWind)
g_cloth.addWind(g_wind);
g_cloth.timeStep(); // calculate cloth configuration in next time interval
clothCollisions(); // detect collisions with spheres
std::vector<VertexPN> clothVertices = g_cloth.getVertices();
g_clothGeometry->upload(&(clothVertices[0]), clothVertices.size()); // update cloth vertices
glutTimerFunc(1000/g_animateFramesPerSecond, animateCloth, 0);
glutPostRedisplay();
}
// loop over each sphere in object and feed it to the collision object to
// react to an an intersection.
static void clothCollisions() {
std::vector<struct sphereComp>::iterator iter;
for (iter = g_spheres.begin(); iter != g_spheres.end(); iter++) {
g_cloth.collision((*((*iter).node)).getRbt().getTranslation()-
(*g_clothNode).getRbt().getTranslation(), (*iter).radius,g_groundY+0.6);
}
}
// initializes the geometries for different objects composed of spheres
static void initSpheres() {
initNormalSphere();
initBunnySpheres();
}
// create a single sphere centered in middle
static void initNormalSphere () {
g_normalSphere.clear();
struct sphereComp sphere;
sphere.setRad(1).setPos(Cvec3(0,0,0));
g_normalSphere.push_back(sphere);
initSphereGeometry(g_normalSphere);
}
// create bunny obect from spheres
static void initBunnySpheres () {
g_bunnySpheres.clear();
struct sphereComp sphere;
sphere.setRad(.85).setPos(Cvec3(.343,-.225,0));
g_bunnySpheres.push_back(sphere);
sphere.setRad(.6).setPos(Cvec3(.97,-.48,.05));
g_bunnySpheres.push_back(sphere);
sphere.setRad(.8).setPos(Cvec3(-.43,0,0));
g_bunnySpheres.push_back(sphere);
sphere.setRad(.49).setPos(Cvec3(-.66,.51,.19));
g_bunnySpheres.push_back(sphere);
initSphereGeometry(g_bunnySpheres);
}
// use geometrymaker, makeSphere to create a sphere geometry for each sphere in
// object.
static void initSphereGeometry(std::vector<struct sphereComp> &v){
std::vector<struct sphereComp>::iterator iter;
for (iter = v.begin(); iter != v.end(); iter++) {
int ibLen, vbLen;
getSphereVbIbLen(20, 10, vbLen, ibLen);
vector<VertexPNTBX> vtx(vbLen);
vector<unsigned short> idx(ibLen);
makeSphere((*iter).radius, 20, 10, vtx.begin(), idx.begin());
((*iter).geometry).reset(new SimpleIndexedGeometryPNTBX(&vtx[0], &idx[0], vtx.size(), idx.size()));
}
}
// create nodes for each spheres in each of the objects
static void initSphereNodes() {
initSphereNode(g_normalSphere,true);
initSphereNode(g_bunnySpheres,false);
g_spheres = g_normalSphere;
}
// for each of the spheres in the object, create a new node using its geometry
// and the sphere material
static void initSphereNode(std::vector<struct sphereComp> &v, bool add) {
std::vector<struct sphereComp>::iterator iter;
for (iter = v.begin(); iter != v.end(); iter++) {
((*iter).node).reset(new SgRbtNode((*iter).pos));
((*iter).node)->addChild(shared_ptr<MyShapeNode>(new MyShapeNode((*iter).geometry, g_sphereMat)));
// we start by adding the normalSphere to the world
if (add)
g_world->addChild((*iter).node);
}
}
// add all of object's sphere nodes to the world
static void addSphereNodes(std::vector<struct sphereComp> &v) {
std::vector<struct sphereComp>::iterator iter;
for (iter = v.begin(); iter != v.end(); iter++)
g_world->addChild((*iter).node);
}
// remove all of object's sphere nodes from the world
static void removeSphereNodes(std::vector<struct sphereComp> &v) {
std::vector<struct sphereComp>::iterator iter;
for (iter = v.begin(); iter != v.end(); iter++)
g_world->removeChild((*iter).node);
}
/// END FINAL PROJECT FUNCTIONS ///
static void initSphere() {
int ibLen, vbLen;
getSphereVbIbLen(20, 10, vbLen, ibLen);
// Temporary storage for sphere Geometry
vector<VertexPNTBX> vtx(vbLen);
vector<unsigned short> idx(ibLen);
makeSphere(1, 20, 10, vtx.begin(), idx.begin());
g_sphere.reset(new SimpleIndexedGeometryPNTBX(&vtx[0], &idx[0], vtx.size(), idx.size()));
}
static void initGround() {
int ibLen, vbLen;
getPlaneVbIbLen(vbLen, ibLen);
vector<VertexPNTBX> vtx(vbLen);
vector<unsigned short> idx(ibLen);
makePlane(g_groundSize*2, vtx.begin(), idx.begin());
g_ground.reset(new SimpleIndexedGeometryPNTBX(&vtx[0], &idx[0], vbLen, ibLen));
}
static void initNormals(Mesh& m) {
for (int i = 0; i < m.getNumVertices(); ++i) {
m.getVertex(i).setNormal(Cvec3(0));
}
for (int i = 0; i < m.getNumFaces(); ++i) {
const Mesh::Face f = m.getFace(i);
const Cvec3 n = f.getNormal();
for (int j = 0; j < f.getNumVertices(); ++j) {
f.getVertex(j).setNormal(f.getVertex(j).getNormal() + n);
}
}
for (int i = 0; i < m.getNumVertices(); ++i) {
Cvec3 n = m.getVertex(i).getNormal();
if (norm2(n) > CS175_EPS2)
m.getVertex(i).setNormal(normalize(n));
}
}
static VertexPN getVertexPN(Mesh& m, const int face, const int vertex) {
const Mesh::Face f = m.getFace(face);
const Cvec3 n = f.getNormal();
const Cvec3& v = f.getVertex(vertex).getPosition();
return VertexPN(v[0], v[1], v[2], n[0], n[1], n[2]);
}
// takes a projection matrix and send to the the shaders
static void sendProjectionMatrix(Uniforms& uniforms, const Matrix4& projMatrix) {
uniforms.put("uProjMatrix", projMatrix);
}
// update g_frustFovY from g_frustMinFov, g_windowWidth, and g_windowHeight
static void updateFrustFovY() {
if (g_windowWidth >= g_windowHeight)
g_frustFovY = g_frustMinFov;
else {
const double RAD_PER_DEG = 0.5 * CS175_PI/180;
g_frustFovY = atan2(sin(g_frustMinFov * RAD_PER_DEG) * g_windowHeight / g_windowWidth, cos(g_frustMinFov * RAD_PER_DEG)) / RAD_PER_DEG;
}
}
static Matrix4 makeProjectionMatrix() {
return Matrix4::makeProjection(
g_frustFovY, g_windowWidth / static_cast <double> (g_windowHeight),
g_frustNear, g_frustFar);
}
enum ManipMode {
ARCBALL_ON_PICKED,
ARCBALL_ON_SKY,
EGO_MOTION
};
static ManipMode getManipMode() {
// if nothing is picked or the picked transform is the transfrom we are viewing from
if (g_currentPickedRbtNode == NULL || g_currentPickedRbtNode == g_currentCameraNode) {
if (g_currentCameraNode == g_skyNode && g_activeCameraFrame == WORLD_SKY)
return ARCBALL_ON_SKY;
else
return EGO_MOTION;
}
else
return ARCBALL_ON_PICKED;
}
static bool shouldUseArcball() {
return getManipMode() != EGO_MOTION;
}
// The translation part of the aux frame either comes from the current
// active object, or is the identity matrix when
static RigTForm getArcballRbt() {
switch (getManipMode()) {
case ARCBALL_ON_PICKED:
return getPathAccumRbt(g_world, g_currentPickedRbtNode);
case ARCBALL_ON_SKY:
return RigTForm();
case EGO_MOTION:
return getPathAccumRbt(g_world, g_currentCameraNode);
default:
throw runtime_error("Invalid ManipMode");
}
}
static void updateArcballScale() {
RigTForm arcballEye = inv(getPathAccumRbt(g_world, g_currentCameraNode)) * getArcballRbt();
double depth = arcballEye.getTranslation()[2];
if (depth > -CS175_EPS)
g_arcballScale = 0.02;
else
g_arcballScale = getScreenToEyeScale(depth, g_frustFovY, g_windowHeight);
}
static void drawArcBall(Uniforms& uniforms) {
RigTForm arcballEye = inv(getPathAccumRbt(g_world, g_currentCameraNode)) * getArcballRbt();
Matrix4 MVM = rigTFormToMatrix(arcballEye) * Matrix4::makeScale(Cvec3(1, 1, 1) * g_arcballScale * g_arcballScreenRadius);
sendModelViewNormalMatrix(uniforms, MVM, normalMatrix(MVM));
g_arcballMat->draw(*g_sphere, uniforms);
}
static void drawStuff(bool picking) {
// every time we redraw, we update collisions so that cloth will always be
// in front of object
clothCollisions();
g_clothGeometry->upload(&(g_cloth.getVertices()[0]), g_cloth.getVertices().size());
// if we are not translating, update arcball scale
if (!(g_mouseMClickButton || (g_mouseLClickButton && g_mouseRClickButton)))
updateArcballScale();
Uniforms uniforms;
// build & send proj. matrix to vshader
const Matrix4 projmat = makeProjectionMatrix();
sendProjectionMatrix(uniforms, projmat);
const RigTForm eyeRbt = getPathAccumRbt(g_world, g_currentCameraNode);
const RigTForm invEyeRbt = inv(eyeRbt);
Cvec3 l1 = getPathAccumRbt(g_world, g_light1).getTranslation();
Cvec3 l2 = getPathAccumRbt(g_world, g_light2).getTranslation();
uniforms.put("uLight", Cvec3(invEyeRbt * Cvec4(l1, 1)));
uniforms.put("uLight2", Cvec3(invEyeRbt * Cvec4(l2, 1)));
if (!picking) {
Drawer drawer(invEyeRbt, uniforms);
g_world->accept(drawer);
if (g_displayArcball && shouldUseArcball())
drawArcBall(uniforms);
}
else {
Picker picker(invEyeRbt, uniforms);
g_overridingMaterial = g_pickingMat;
g_world->accept(picker);
g_overridingMaterial.reset();
glFlush();
g_currentPickedRbtNode = picker.getRbtNodeAtXY(g_mouseClickX, g_mouseClickY);
if (g_currentPickedRbtNode == g_groundNode)
g_currentPickedRbtNode.reset(); // set to NULL
cout << (g_currentPickedRbtNode ? "Part picked" : "No part picked") << endl;
}
}
static void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawStuff(false);
glutSwapBuffers();
checkGlErrors();
}
static void pick() {
// We need to set the clear color to black, for pick rendering.
// so let's save the clear color
GLdouble clearColor[4];
glGetDoublev(GL_COLOR_CLEAR_VALUE, clearColor);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawStuff(true);
glutSwapBuffers();
//Now set back the clear color
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
checkGlErrors();
}
static void reshape(const int w, const int h) {
g_windowWidth = w;
g_windowHeight = h;
glViewport(0, 0, w, h);
cerr << "Size of window is now " << w << "x" << h << endl;
g_arcballScreenRadius = min(h, w) * 0.25;
updateFrustFovY();
glutPostRedisplay();
}
static Cvec3 getArcballDirection(const Cvec2& p, const double r) {
double n2 = norm2(p);
if (n2 >= r*r)
return normalize(Cvec3(p, 0));
else
return normalize(Cvec3(p, sqrt(r*r - n2)));
}
static RigTForm moveArcball(const Cvec2& p0, const Cvec2& p1) {
const Matrix4 projMatrix = makeProjectionMatrix();
const RigTForm eyeInverse = inv(getPathAccumRbt(g_world, g_currentCameraNode));
const Cvec3 arcballCenter = getArcballRbt().getTranslation();
const Cvec3 arcballCenter_ec = Cvec3(eyeInverse * Cvec4(arcballCenter, 1));
if (arcballCenter_ec[2] > -CS175_EPS)
return RigTForm();
Cvec2 ballScreenCenter = getScreenSpaceCoord(arcballCenter_ec,
projMatrix, g_frustNear, g_frustFovY, g_windowWidth, g_windowHeight);
const Cvec3 v0 = getArcballDirection(p0 - ballScreenCenter, g_arcballScreenRadius);
const Cvec3 v1 = getArcballDirection(p1 - ballScreenCenter, g_arcballScreenRadius);
return RigTForm(Quat(0.0, v1[0], v1[1], v1[2]) * Quat(0.0, -v0[0], -v0[1], -v0[2]));
}
static RigTForm doMtoOwrtA(const RigTForm& M, const RigTForm& O, const RigTForm& A) {
return A * M * inv(A) * O;
}
static RigTForm getMRbt(const double dx, const double dy) {
RigTForm M;
if (g_mouseLClickButton && !g_mouseRClickButton) {
if (shouldUseArcball())
M = moveArcball(Cvec2(g_mouseClickX, g_mouseClickY), Cvec2(g_mouseClickX + dx, g_mouseClickY + dy));
else
M = RigTForm(Quat::makeXRotation(-dy) * Quat::makeYRotation(dx));
}
else {
double movementScale = getManipMode() == EGO_MOTION ? 0.02 : g_arcballScale;
if (g_mouseRClickButton && !g_mouseLClickButton) {
M = RigTForm(Cvec3(dx, dy, 0) * movementScale);
}
else if (g_mouseMClickButton || (g_mouseLClickButton && g_mouseRClickButton)) {
M = RigTForm(Cvec3(0, 0, -dy) * movementScale);
}
}
switch (getManipMode()) {
case ARCBALL_ON_PICKED:
break;
case ARCBALL_ON_SKY:
M = inv(M);
break;
case EGO_MOTION:
if (g_mouseLClickButton && !g_mouseRClickButton) // only invert rotation
M = inv(M);
break;
}
return M;
}
static RigTForm makeMixedFrame(const RigTForm& objRbt, const RigTForm& eyeRbt) {
return transFact(objRbt) * linFact(eyeRbt);
}
static void motion(const int x, const int y) {
if (!g_mouseClickDown)
return;
const double dx = x - g_mouseClickX;
const double dy = g_windowHeight - y - 1 - g_mouseClickY;
const RigTForm M = getMRbt(dx, dy); // the "action" matrix
// the matrix for the auxiliary frame (the w.r.t.)
RigTForm A = makeMixedFrame(getArcballRbt(), getPathAccumRbt(g_world, g_currentCameraNode));
shared_ptr<SgRbtNode> target;
switch (getManipMode()) {
case ARCBALL_ON_PICKED:
target = g_currentPickedRbtNode;
break;
case ARCBALL_ON_SKY:
target = g_skyNode;
break;
case EGO_MOTION:
target = g_currentCameraNode;
break;
}
A = inv(getPathAccumRbt(g_world, target, 1)) * A;
target->setRbt(doMtoOwrtA(M, target->getRbt(), A));
g_mouseClickX += dx;
g_mouseClickY += dy;
glutPostRedisplay(); // we always redraw if we changed the scene
}
static void mouse(const int button, const int state, const int x, const int y) {
g_mouseClickX = x;
g_mouseClickY = g_windowHeight - y - 1; // conversion from GLUT window-coordinate-system to OpenGL window-coordinate-system
g_mouseLClickButton |= (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN);
g_mouseRClickButton |= (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN);
g_mouseMClickButton |= (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN);
g_mouseLClickButton &= !(button == GLUT_LEFT_BUTTON && state == GLUT_UP);
g_mouseRClickButton &= !(button == GLUT_RIGHT_BUTTON && state == GLUT_UP);
g_mouseMClickButton &= !(button == GLUT_MIDDLE_BUTTON && state == GLUT_UP);
g_mouseClickDown = g_mouseLClickButton || g_mouseRClickButton || g_mouseMClickButton;
if (g_pickingMode && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
pick();
g_pickingMode = false;
cerr << "Picking mode is off" << endl;
glutPostRedisplay(); // request redisplay since the arcball will have moved
}
glutPostRedisplay();
}
static void keyboard(const unsigned char key, const int x, const int y) {
switch (key) {
case 27:
exit(0); // ESC
case 'h':
cout << " ============== H E L P ==============\n\n"
<< "h\t\thelp menu\n"
<< "s\t\tsave screenshot\n"
<< "p\t\tUse mouse to pick a part to edit\n"
<< "drag left mouse to rotate\n"
<< "a\t\tToggle display arcball\n"
<< "space\t\tRelease cloth\n"
<< "u\t\tUnfix 2 points on cloth\n"
<< "f\t\tToggle cloth wireframe\n"
<< "w\t\tToggle wind\n"
<< "r\t\tReturn the cloth to its original position\n"
<< "b\t\tToggle between bunny and sphere\n"
<< endl;
break;
case 's':
glFlush();
writePpmScreenshot(g_windowWidth, g_windowHeight, "out.ppm");
break;
case 'p':
g_pickingMode = !g_pickingMode;
cerr << "Picking mode is " << (g_pickingMode ? "on" : "off") << endl;
break;
case 'a':
g_displayArcball = !g_displayArcball;
break;
/// FINAL PROJECT KEYS ///
// release cloth
case ' ':
g_cloth.unfix();
break;
// release 2 of cloth's corners
case 'u':
g_cloth.unfix2();
break;
// put cloth back to starting position
case 'r':
g_cloth = Cloth();
g_cloth.loadDimensions(5,5,45,45);
break;
// toggle between wirefram and filled in cloth
case 'f':
if (g_clothWire) {
g_clothMat->getRenderStates().polygonMode(GL_FRONT_AND_BACK, GL_FILL);
g_clothWire = false;
} else {
g_clothMat->getRenderStates().polygonMode(GL_FRONT_AND_BACK, GL_LINE);
g_clothWire = true;
}
break;
// toggle wind
case 'w':
g_isWind = !g_isWind;
break;
// toggle between bunny and sphere
case 'b':
if (g_bunny) {
g_spheres = g_normalSphere; // collisions with normal sphere
g_world->removeChild(g_bunnyNode); // remove bunny
addSphereNodes(g_normalSphere); // add normal sphere to scene
//removeSphereNodes(g_bunnySpheres);
g_bunny = false;
} else {
g_spheres = g_bunnySpheres; // collisions with bunny spheres
g_world->addChild(g_bunnyNode); // add bunny
removeSphereNodes(g_normalSphere);
//addSphereNodes(g_bunnySpheres);
g_bunny = true;
}
break;
}
glutPostRedisplay();
}
static void initGlutState(int argc, char * argv[]) {
glutInit(&argc, argv); // initialize Glut based on cmd-line args
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); // RGBA pixel channels and double buffering
glutInitWindowSize(g_windowWidth, g_windowHeight); // create a window
glutCreateWindow("Assignment 7"); // title the window
glutDisplayFunc(display); // display rendering callback
glutReshapeFunc(reshape); // window reshape callback
glutMotionFunc(motion); // mouse movement callback
glutMouseFunc(mouse); // mouse click callback
glutKeyboardFunc(keyboard);
}
static void initGLState() {
glClearColor(128./255., 200./255., 255./255., 0.);
glClearDepth(0.);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_GREATER);
glReadBuffer(GL_BACK);
if (!g_Gl2Compatible)
glEnable(GL_FRAMEBUFFER_SRGB);
}
static void updateBunnyMesh() {
Mesh m = g_bunnyMesh;
initNormals(g_bunnyMesh);
// dynamic vertex buffer
vector<VertexPN> verts;
verts.reserve(m.getNumFaces() * 6); // conservative estimate of num of vertices
for (int i = 0; i < m.getNumFaces(); ++i) {
for (int j = 0; j < 3; ++j) {
verts.push_back(getVertexPN(m, i, j));
}
if (m.getFace(i).getNumVertices() == 4) {
// need another triangle to finish the face
for (int j = 0; j < 3; ++j) {
verts.push_back(getVertexPN(m, i, (2 + j) % 4));
}
}
}
g_bunnyGeometry->upload(&verts[0], verts.size());
}
// New function that loads the bunny mesh and initializes the bunny shell meshes
static void initBunnyMeshes() {
g_bunnyMesh.load("bunny.mesh");
g_bunnyGeometry.reset(new SimpleGeometryPN());
updateBunnyMesh();
}
static void initMaterials() {
// Create some prototype materials
Material diffuse("./shaders/basic-gl3.vshader", "./shaders/diffuse-gl3.fshader");
Material solid("./shaders/basic-gl3.vshader", "./shaders/solid-gl3.fshader");
// normal mapping
g_bumpFloorMat.reset(new Material("./shaders/normal-gl3.vshader", "./shaders/normal-gl3.fshader"));
g_bumpFloorMat->getUniforms().put("uTexColor", shared_ptr<ImageTexture>(new ImageTexture("Fieldstone.ppm", true)));
g_bumpFloorMat->getUniforms().put("uTexNormal", shared_ptr<ImageTexture>(new ImageTexture("FieldstoneNormal.ppm", false)));
// copy solid prototype, and set to wireframed rendering
g_arcballMat.reset(new Material(solid));
g_arcballMat->getUniforms().put("uColor", Cvec3f(0.27f, 0.82f, 0.35f));
g_arcballMat->getRenderStates().polygonMode(GL_FRONT_AND_BACK, GL_LINE);
// copy solid prototype, and set to color white
g_lightMat.reset(new Material(solid));
g_lightMat->getUniforms().put("uColor", Cvec3f(1, 1, 1));
// pick shader
g_pickingMat.reset(new Material("./shaders/basic-gl3.vshader", "./shaders/pick-gl3.fshader"));
// bunny material
g_bunnyMat.reset(new Material("./shaders/basic-gl3.vshader", "./shaders/bunny-gl3.fshader"));
g_bunnyMat->getUniforms()
.put("uColorAmbient", Cvec3f(0.45f, 0.3f, 0.3f))
.put("uColorDiffuse", Cvec3f(0.2f, 0.2f, 0.2f));
// new sphere material based off diffuse
g_sphereMat.reset(new Material(diffuse));
g_sphereMat->getUniforms().put("uColor", Cvec3f(195./255., 9./255., 9./255.));
/// FINAL PROJECT MATERIALS ///
// tried to use velvet material found from web, but it didn't work
g_clothMat.reset(new Material(diffuse));
// g_clothMat.reset(new Material("./shaders/velvet-gl2.vshader", "./shaders/velvet-gl2.fshader"));
g_clothMat->getUniforms().put("uColor", Cvec3f(24./255., 0./255., 228./255.));
g_clothMat->getRenderStates().polygonMode(GL_FRONT_AND_BACK, GL_FILL);
};
static void initGeometry() {
initGround();
initSphere();
initBunnyMeshes();
initSpheres(); // create geometry for new spheres
initCloth(); // create geometry for cloth
}
static void initScene() {
g_world.reset(new SgRootNode());
g_skyNode.reset(new SgRbtNode(RigTForm(Cvec3(0.0, 0.25, 8.0))));
g_groundNode.reset(new SgRbtNode(Cvec3(0, g_groundY, 0)));
g_groundNode->addChild(shared_ptr<MyShapeNode>(
new MyShapeNode(g_ground, g_bumpFloorMat)));
g_light1.reset(new SgRbtNode(RigTForm(Cvec3(4.0, 3.0, 5.0))));
g_light2.reset(new SgRbtNode(RigTForm(Cvec3(-4, 1.0, -4.0))));
g_light1->addChild(shared_ptr<MyShapeNode>(
new MyShapeNode(g_sphere, g_lightMat, Cvec3(0), Cvec3(0), Cvec3(0.5))));
g_light2->addChild(shared_ptr<MyShapeNode>(
new MyShapeNode(g_sphere, g_lightMat, Cvec3(0), Cvec3(0), Cvec3(0.5))));
// create a single transform node for both the bunny and the bunny shells
g_bunnyNode.reset(new SgRbtNode(Cvec3(0.0,0.0,0.0)));
// add bunny as a shape nodes
g_bunnyNode->addChild(shared_ptr<MyShapeNode>(
new MyShapeNode(g_bunnyGeometry, g_bunnyMat)));
// add cloth node to scene, translated appropiately
g_clothNode.reset(new SgRbtNode(clothTranslation));
g_clothNode->addChild(shared_ptr<MyShapeNode>(
new MyShapeNode(g_clothGeometry, g_clothMat)));
initSphereNodes(); // create new nodes for objects built with spheres
g_world->addChild(g_skyNode);
g_world->addChild(g_groundNode);
g_world->addChild(g_light1);
g_world->addChild(g_light2);
g_world->addChild(g_clothNode);
g_currentCameraNode = g_skyNode;
}
// begin animation loop
static void initAnimation() {
animateCloth(0);
}
int main(int argc, char * argv[]) {
try {
initGlutState(argc,argv);
glewInit(); // load the OpenGL extensions
cout << (g_Gl2Compatible ? "Will use OpenGL 2.x / GLSL 1.0" : "Will use OpenGL 3.x / GLSL 1.3") << endl;
if ((!g_Gl2Compatible) && !GLEW_VERSION_3_0)
throw runtime_error("Error: card/driver does not support OpenGL 3.x / GLSL v1.3. Consider setting g_Gl2Compatible to true");
else if (g_Gl2Compatible && !GLEW_VERSION_2_0)
throw runtime_error("Error: card/driver does not support OpenGL 2.x / GLSL 1.0");
initGLState();
initMaterials();
initGeometry();
initScene();
initAnimation(); // startall animations
glutMainLoop();
return 0;
}
catch (const runtime_error& e) {
cout << "Exception caught: " << e.what() << endl;
return -1;
}
}