cloth2

view src/main.cc @ 1:dc15b741486c

exploding cloth
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 11 Jan 2016 20:24:13 +0200
parents ef0c22554406
children b4b8f736332b
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <GL/glew.h>
5 #ifdef __APPLE__
6 #include <GLUT/glut.h>
7 #else
8 #include <GL/glut.h>
9 #endif
10 #include "object.h"
11 #include "cloth.h"
13 bool init();
14 void cleanup();
15 void display();
16 void idle();
17 void reshape(int x, int y);
18 void keyboard(unsigned char key, int x, int y);
19 void mouse(int bn, int st, int x, int y);
20 void motion(int x, int y);
22 float cam_theta, cam_phi = 25, cam_dist = 10;
24 Cloth cloth;
26 int main(int argc, char **argv)
27 {
28 glutInit(&argc, argv);
29 glutInitWindowSize(800, 600);
30 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
31 glutCreateWindow("foo");
33 glutDisplayFunc(display);
34 glutIdleFunc(idle);
35 glutReshapeFunc(reshape);
36 glutKeyboardFunc(keyboard);
37 glutMouseFunc(mouse);
38 glutMotionFunc(motion);
40 if(!init()) {
41 return 1;
42 }
43 atexit(cleanup);
45 glutMainLoop();
46 }
49 bool init()
50 {
51 glewInit();
53 glEnable(GL_DEPTH_TEST);
54 glEnable(GL_CULL_FACE);
55 //glEnable(GL_LIGHTING);
56 glEnable(GL_LIGHT0);
58 cloth.create_rect(4, 4, 10, 10);
60 Matrix4x4 xform = Matrix4x4(1, 0, 0, 0,
61 0, 1, 0, 0,
62 0, 0, 1, 0,
63 0, 1, 1, 1);
64 cloth.transform(xform);
65 cloth.set_mass(0.1);
66 cloth.set_spring_constant(0.1);
67 cloth.set_gravity(Vector3(0, -0.1, 0));
68 cloth.set_damping(0.1);
71 return true;
72 }
74 void cleanup()
75 {
76 }
78 void display()
79 {
80 static unsigned int prev_upd;
81 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
83 if(!prev_upd) prev_upd = msec;
84 float dt = (msec - prev_upd) / 1000.0f;
86 cloth.step(dt);
88 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
90 glMatrixMode(GL_MODELVIEW);
91 glLoadIdentity();
92 glTranslatef(0, 0, -cam_dist);
93 glRotatef(cam_phi, 1, 0, 0);
94 glRotatef(cam_theta, 0, 1, 0);
96 cloth.draw();
98 glutSwapBuffers();
99 assert(glGetError() == GL_NO_ERROR);
100 }
102 void idle()
103 {
104 glutPostRedisplay();
105 }
107 void reshape(int x, int y)
108 {
109 glViewport(0, 0, x, y);
111 glMatrixMode(GL_PROJECTION);
112 glLoadIdentity();
113 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
114 }
116 void keyboard(unsigned char key, int x, int y)
117 {
118 switch(key) {
119 case 27:
120 exit(0);
121 }
122 }
124 static bool bnstate[16];
125 static int prev_x, prev_y;
127 void mouse(int bn, int st, int x, int y)
128 {
129 prev_x = x;
130 prev_y = y;
131 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
132 }
134 void motion(int x, int y)
135 {
136 int dx = x - prev_x;
137 int dy = y - prev_y;
138 prev_x = x;
139 prev_y = y;
141 if(!dx && !dy) return;
143 if(bnstate[0]) {
144 cam_theta += dx * 0.5;
145 cam_phi += dy * 0.5;
147 if(cam_phi < -90) cam_phi = -90;
148 if(cam_phi > 90) cam_phi = 90;
149 }
150 if(bnstate[2]) {
151 cam_dist += dy * 0.1;
152 if(cam_dist < 0.0) cam_dist = 0.0;
153 }
154 }