-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (120 loc) · 2.62 KB
/
main.cpp
File metadata and controls
140 lines (120 loc) · 2.62 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
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include "glut.h"
#include <math.h>
#include <vector>
#include <iostream>
#include "vector3.h"
#include "particle.h"
#include "constraint.h"
#include "cloth.h"
Cloth cloth(15,15,55,55);
Sphere sphere(Vector3(7,-5,0), 3);
Plane plane(Vector3(0,1,0), -8);
void init(GLvoid) {
glShadeModel(GL_SMOOTH);
glClearColor(0.2f, 0.2f, 0.4f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_COLOR_MATERIAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void display(void) {
cloth.addForce(Vector3(0,-0.1,0));
cloth.timeStep();
cloth.ballCollision(sphere);
cloth.planeCollision(plane);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_POLYGON);
glColor3f(0.0f,0.8f,1.0f);
glVertex3f(-200.0f,-100.0f,-100.0f);
glVertex3f(200.0f,-100.0f,-100.0f);
glColor3f(0.4f,0.9f,0.3f);
glVertex3f(200.0f,100.0f,-100.0f);
glVertex3f(-200.0f,100.0f,-100.0f);
glEnd();
glTranslatef(-5 ,6,-10);
glRotatef(45,0,1,0);
cloth.draw();
glPushMatrix();
glTranslatef(sphere.pos.f[0],sphere.pos.f[1],sphere.pos.f[2]);
glColor3f(0.9f,1.0f,0.0f);
glutSolidSphere(sphere.radius-0.1,50,50);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (80,(float)w /(float)h,1.0,100 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y) {
}
void keyboard( unsigned char key, int x, int y ) {
switch ( key ) {
case 27:
exit ( 0 );
break;
case 'w':
sphere.pos.f[2] -= 0.3;
break;
case 's':
sphere.pos.f[2] += 0.3;
break;
case 'q':
sphere.pos.f[1] -= 0.3;
break;
case 'e':
sphere.pos.f[1] += 0.3;
break;
case 'a':
sphere.pos.f[0] -= 0.3;
break;
case 'd':
sphere.pos.f[0] += 0.3;
break;
case 'z':
plane.dist -= 0.1;
break;
case 'x':
plane.dist += 0.1;
break;
default:
break;
}
}
void arrow_keys( int a_keys, int x, int y ) {
switch(a_keys) {
case GLUT_KEY_UP:
glutFullScreen();
break;
case GLUT_KEY_DOWN:
glutReshapeWindow(800, 600);
break;
default:
break;
}
}
int main ( int argc, char** argv ) {
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize(800, 600);
glutCreateWindow( "Cloth Simulation W S Q E A D" );
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutSpecialFunc(arrow_keys);
glutMainLoop();
}