-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFace.cpp
More file actions
371 lines (315 loc) · 9.7 KB
/
SimpleFace.cpp
File metadata and controls
371 lines (315 loc) · 9.7 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
// ==========================================================================
// SIMPLEFACE.C
// ==========================================================================
//
// This is an OpenGL implementation of the simple face from Appendix 1 in
// Computer Facial Animation. It demonstrates the use of the muscle model
// to create facial expressions.
//
//
// HISTORY
// 16-Dec-94 Keith Waters (waters@crl.dec.com) Created at Digital Equipment
// Cambridge Research Laboratory
//
// 10-Aug-98 Keith Waters (waters@crl.dec.com) Modified for OpenGL on W95 and
// WNT at Compaq Cambridge Research Laboratory. This code attempts
// to be as close to the original code as possible. Module names
// and function definitions have been maintained for clarity. A
// few extra components have been added in particular expression I/O
// so you can make your own expressions. In addition transparency
// has been added to observe the location of facial muscles.
//
// ============================================================================
#include "stdio.h"
#include <GL/gl.h> // OpenGL includes
#include <GL/glu.h>
#include <SFML/Window.hpp>
#include "src/MainWindow.h"
#include "head.h" // Face includes
void OpenGLInit(void);
// Forward declarations
void calc_normal ( float*, float*, float*, float* ) ;
void calculate_polygon_vertex_normal( HEAD * ) ;
void FaceInit ( );
void paint_muscles ( HEAD* ) ;
void paint_polygons ( HEAD*, int, int ) ;
void data_struct ( HEAD* ) ;
void read_muscles ( char* , HEAD* ) ;
void read_expression_macros ( char* , HEAD* ) ;
void activate_muscle ( HEAD*, float*, float*, float, float, float, float ) ;
void face_reset ( HEAD* ) ;
void make_expression ( HEAD*, int ) ;
HEAD *create_face ( char*, char* ) ;
GLfloat rotateX, rotateY ;
HEAD *face ;
// ========================================================================
// Key bindings for callback proceedures
// ========================================================================
//
static void Key_e (void)
{
face_reset ( face ) ;
make_expression ( face, face->current_exp ) ;
face->current_exp++ ;
if ( face->current_exp >= face->nexpressions )
face->current_exp = 0 ;
}
static void Key_b (void)
{
face->rendermode++;
if ( face->rendermode > 3 )
face->rendermode = 0 ;
}
static void Key_c (void)
{
int cm ;
cm = face->current_muscle ;
// Record the muscle activation
face->muscle[cm]->mstat += 0.1 ;
activate_muscle ( face,
face->muscle[cm]->head,
face->muscle[cm]->tail,
face->muscle[cm]->fs,
face->muscle[cm]->fe,
face->muscle[cm]->zone,
0.1 ) ;
}
static void Key_C (void)
{
int cm ;
cm = face->current_muscle ;
// Record the muscle activation
face->muscle[cm]->mstat -= 0.1 ;
activate_muscle ( face,
face->muscle[cm]->head,
face->muscle[cm]->tail,
face->muscle[cm]->fs,
face->muscle[cm]->fe,
face->muscle[cm]->zone,
-0.1 ) ;
}
static void Key_n (void)
{
face->current_muscle++ ;
if ( face->current_muscle >= face->nmuscles )
face->current_muscle = 0 ;
}
static void Key_up (void)
{
rotateX -= 2.0f;
}
static void Key_down(void)
{
rotateX += 2.0f;
}
static void Key_left(void)
{
rotateY -= 2.0f;
}
static void Key_right(void)
{
rotateY += 2.0f;
}
static void Key_r (void)
{
face_reset ( face ) ;
}
static void Key_R (void)
{
face_reset ( face ) ;
read_expression_macros ("../face-data/expression-macros.dat", face ) ;
face->current_exp = 0 ;
}
static void Key_w (void)
{
FILE *OutFile ;
int i;
// Check the FileName
if (( OutFile = fopen ( "../face-data/single-expression.dat", "w+" )) == 0 ) {
fprintf (stderr, "Can't open:../face-data/single-expression.dat\n");
return;
}
for ( i=0; i<face->nmuscles; i++) {
fprintf (OutFile, "%s %2.2f\t", face->muscle[i]->name, face->muscle[i]->mstat ) ;
}
fclose ( OutFile ) ;
}
static void Key_h (void)
{
fprintf(stderr,"\n");
fprintf(stderr,"b: toggles the drawing modes (Wireframe, facet, smooth & transparent)\n");
fprintf(stderr,"c: contract the current facial muscle\n");
fprintf(stderr,"C: relax the current facial muscle\n");
fprintf(stderr,"e: next expression\n");
fprintf(stderr,"h: outputs this message\n");
fprintf(stderr,"n: next muscle (to select another facial muscle to manipulate)\n");
fprintf(stderr,"r: reset the expression\n");
fprintf(stderr,"R: reread the expression file (../face-data/expression-macros.dat)\n");
fprintf(stderr,"w: \t outputs muscles state (../face-data/single-expression.dat)\n");
fprintf(stderr,"Up arrow: \t rotates the face up\n");
fprintf(stderr,"Down arrow: \t rotates the face down\n");
fprintf(stderr,"Right arrow: \t rotates the face right\n");
fprintf(stderr,"Left arrow: \t rotates the face left\n");
fprintf(stderr,"\n");
}
void input(sf::Event event){
if(event.type == sf::Event::KeyPressed){
if(event.key.code == sf::Keyboard::E){
Key_e();
} else if(event.key.code == sf::Keyboard::B){
Key_b();
} else if(event.key.code == sf::Keyboard::C){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)){
Key_C();
} else {
Key_c();
}
} else if(event.key.code == sf::Keyboard::N){
Key_n();
} else if(event.key.code == sf::Keyboard::Up){
Key_up();
} else if(event.key.code == sf::Keyboard::Down){
Key_down();
} else if(event.key.code == sf::Keyboard::Left){
Key_left();
} else if(event.key.code == sf::Keyboard::Right){
Key_right();
} else if(event.key.code == sf::Keyboard::R){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)){
Key_R();
} else {
Key_r();
}
} else if(event.key.code == sf::Keyboard::W){
Key_w();
} else if(event.key.code == sf::Keyboard::H){
Key_h();
}
}
}
// ========================================================================
// Animate ()
// ========================================================================
//
// The big enchilda. The animation callback
//
static void Animate(void)
{
GLfloat light_position [] = { 30.0, 70.0, 100.0, 1.0 } ;
// clear the rendering window
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// clear current matrix (Modelview)
glLoadIdentity ( );
// back off thirty units down the Z axis
glTranslatef ( 0.0f, 0.0f, -30.0f );
// Use the keyboard to grab the rotations
glRotatef ( rotateX, 1.0f, 0.0f, 0.0f );
glRotatef ( rotateY, 0.0f, 1.0f, 0.0f );
glPushMatrix ( ) ;
glLightfv ( GL_LIGHT0, GL_POSITION, light_position ) ;
glTranslated ( 0.0, 0.0, 50.0 ) ;
glEnable ( GL_LIGHTING ) ;
glPopMatrix ( ) ;
// Transparancy
if ( face->rendermode == 3 ) {
glEnable ( GL_BLEND );
glDepthMask ( GL_FALSE );
glBlendFunc ( GL_SRC_ALPHA, GL_ONE );
} else {
glDisable ( GL_BLEND );
glDepthMask ( GL_TRUE );
}
// Re-calculate the polygon normals as
// the face could have been distorted
calculate_polygon_vertex_normal ( face ) ;
paint_polygons ( face, face->rendermode, 0 ) ;
// if the rendering mode is wireframe
// or transparent then display the muscles
if ( face->rendermode == 0 ||
face->rendermode == 3 ) {
glDisable ( GL_LIGHTING );
paint_muscles ( face ) ;
}
// Now flush the pipeline and
// swap the buffers
glFlush ( );
}
// ========================================================================
// OpenGLInit ()
// ========================================================================
//
// Initialize OpenGL
//
void OpenGLInit(void)
{
glEnable ( GL_LIGHTING );
glEnable ( GL_LIGHT0 );
glClearDepth( 1.0f );
glDepthFunc ( GL_LEQUAL );
glEnable ( GL_DEPTH_TEST );
glClearColor(1.0, 0, 0, 1);
glViewport(0, 0, 320, 512);
}
// ========================================================================
// ResizeWindow ()
// ========================================================================
//
// This get calls when the window gets resized.
//
static void ResizeWindow(GLsizei w, GLsizei h)
{
h = (h == 0) ? 1 : h;
w = (w == 0) ? 1 : w;
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
gluPerspective ( 40.0, (GLfloat)w/(GLfloat)h, 1.0f, 200.0f );
// select the Modelview matrix
glMatrixMode ( GL_MODELVIEW );
}
// ========================================================================
// Main ()
// ========================================================================
//
// Setup OpenGL, setup the face, hook up callbacks and start the main loop.
//
int main( int argc, char** argv )
{
// MainWindow mainWindow(320, 512, "Simple Face Example");
// mainWindow.run();
sf::Window window;
window.create(sf::VideoMode(320, 512), "Simple Face Example");
window.setFramerateLimit(60);
ResizeWindow(320, 512);
// Initialize OpenGL
OpenGLInit ( );
// Initialize the face
FaceInit ( );
bool running = true;
while(running){
sf::Event event;
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed){
running = false;
break;
}
input(event);
}
Animate();
window.display();
}
return(0);
}
// ========================================================================
// FaceInit
// ========================================================================
//
// Read in the datafiles and initialize the face data structures.
//
void FaceInit ( void )
{
face = create_face ("face-data/index.dat", "face-data/faceline.dat");
read_muscles ("face-data/muscle.dat", face );
read_expression_macros ("face-data/expression-macros.dat", face );
data_struct ( face );
}