-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
493 lines (360 loc) · 8.99 KB
/
main.c
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
#include <stdio.h>
#ifdef USE_GLUT
#include <GL/glut.h>
#else
#include <X11/X.h>
#include <X11/Xlib.h>
#include <GL/glx.h>
#include <unistd.h>
#endif
#include <GL/glu.h>
#include <math.h>
#include "linear.h"
#include "model.h"
#include "movement.h"
#include "draw.h"
/*
(x, y, z)
(r, t, p)
r = sqrt(x*x + y*y + z*z);
cos t = z/r;
cos p = x/sqrt(x*x + y*y);
tan p = y/x;
z = r * cos(t);
y = r * sin(t) * sin(p);
x = r * sin(t) * cos(p);
*/
double cz = -10.0;
double cy = 0.0;
double cx = 0.0;
double tz = 0.0;
double ty = 0.0;
double tx = 0.0;
GLuint cube_list;
GLuint frame_list;
void bone_draw(struct bone *b)
{
glPushMatrix();
glMultMatrixf(b->s.t_abs);
glScaled(b->dx, b->dy, b->dz);
glCallList(cube_list);
glPopMatrix();
}
static void model_draw(struct model *m)
{
int i;
glPushMatrix();
model_update_shape(m);
/* translate to center of mass coordinates */
glTranslatef(m->pos[0], m->pos[1], m->pos[2]);
glRotatef(ty, 0.0, 1.0, 0.0);
glPushMatrix();
/* rotate about the center of mass */
glMultMatrixf(m->angular_pos);
glTranslatef(-m->cm[0], -m->cm[1], -m->cm[2]);
for (i = 0; i < m->bones; i++)
bone_draw(&m->bone[i]);
glPopMatrix();
glColor3f(1, 0, 0);
vector_draw(&m->momentum);
glColor3f(0, 1, 0);
vector_draw(&m->angular_velocity);
glColor3f(0, 0, 1);
vector_draw(&m->angular_momentum);
model_movement(m);
glPopMatrix();
}
void cube_draw(vec3 p, float w, float h)
{
glPushMatrix();
glTranslatef(p[0], p[1], p[2]);
glTranslatef(-w/2.0, -h/2.0, -w/2.0);
glScaled(w, h, w);
glCallList(cube_list);
glPopMatrix();
}
static void reshape(int w, int h)
{
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
// Reset Matrix
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45.0f, ratio, 0.01, 100.0);
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}
static void display_func(void)
{
// joint_adjust_random(&model_bulky);
joint_adjust_random(&model_sticky);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(cx, cy, cz);
model_draw(&model_bulky);
model_draw(&model_sticky);
// model_draw(&model_simple);
#ifdef USE_GLUT
glutSwapBuffers();
#else
#endif
}
static void keyboard (unsigned char key, int x, int y)
{
struct model *m = &model_bulky;
float d_arm[3] = {0, 0, 0};
float d_leg[3] = {0, 0, 0};
switch (key) {
case 'y':
d_arm[0] = 1.0;
break;
case 'u':
d_arm[0] = -1.0;
break;
case 'h':
d_arm[1] = 5.0;
break;
case 'j':
d_arm[1] = -5.0;
break;
case 'n':
d_arm[2] = 5.0;
break;
case 'm':
d_arm[2] = -5.0;
break;
case 'i':
d_leg[0] = 1.0;
break;
case 'o':
d_leg[0] = -1.0;
break;
case 'k':
d_leg[1] = 5.0;
break;
case 'l':
d_leg[1] = -5.0;
break;
case ',':
d_leg[2] = 5.0;
break;
case '.':
d_leg[2] = -5.0;
break;
case 'w':
cy += 0.1;
break;
case 's':
cy -= 0.1;
break;
case 'a':
cx -= 0.1;
break;
case 'd':
cx += 0.1;
break;
case 'q':
cz -= 0.1;
break;
case 'e':
cz += 0.1;
break;
case 'r':
ty += 5;
break;
case 't':
ty -= 5;
break;
default:
break;
}
joint_adjust(&m->bone[LEFT_ARM], d_arm[0], d_arm[1], d_arm[2]);
joint_adjust(&m->bone[RIGHT_ARM], d_arm[0], d_arm[1], d_arm[2]);
joint_adjust(&m->bone[LEFT_LEG], d_leg[0], d_leg[1], d_leg[2]);
joint_adjust(&m->bone[RIGHT_LEG], d_leg[0], d_leg[1], d_leg[2]);
}
static void lighting_init(void)
{
GLfloat lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};
GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_position[] = {0.0, 0.0, 5.0, 1.0};
// GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
/* Always keep normal vectors normalized */
glEnable(GL_NORMALIZE);
}
static void antialiasing_init(void)
{
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
// glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
static void depth_init(void)
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
static void drawing_init(void)
{
glLineWidth(1);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
static void culling_init(void)
{
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
}
static void dump_info(void)
{
int val[4];
float fval;
printf("%s\n", glGetString(GL_VERSION));
#if 0
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
printf("%d.%d\n", major, minor);
#endif
glGetIntegerv(GL_RED_BITS, &val[0]);
glGetIntegerv(GL_GREEN_BITS, &val[1]);
glGetIntegerv(GL_BLUE_BITS, &val[2]);
glGetIntegerv(GL_ALPHA_BITS, &val[3]);
printf("Color buffer bits: %d %d %d %d\n", val[0], val[1], val[2], val[3]);
glGetIntegerv(GL_DEPTH_BITS, &val[0]);
printf("Depth buffer bits: %d\n", val[0]);
glGetIntegerv(GL_STENCIL_BITS, &val[0]);
printf("Stencil buffer bits: %d\n", val[0]);
glGetIntegerv(GL_ACCUM_RED_BITS, &val[0]);
glGetIntegerv(GL_ACCUM_GREEN_BITS, &val[1]);
glGetIntegerv(GL_ACCUM_BLUE_BITS, &val[2]);
glGetIntegerv(GL_ACCUM_ALPHA_BITS, &val[3]);
printf("Accumulator buffer bits: %d %d %d %d\n", val[0], val[1], val[2], val[3]);
glGetIntegerv(GL_SAMPLE_BUFFERS, &val[0]);
printf("Sample buffers: %d\n", val[0]);
glGetIntegerv(GL_SAMPLES, &val[0]);
printf("Samples: %d\n", val[0]);
glGetFloatv(GL_SAMPLE_COVERAGE_VALUE, &fval);
printf("Sample cover value: %f\n", fval);
printf("%d %d %d %d\n", glIsEnabled(GL_MULTISAMPLE),
glIsEnabled(GL_SAMPLE_ALPHA_TO_ONE),
glIsEnabled(GL_SAMPLE_ALPHA_TO_COVERAGE),
glIsEnabled(GL_SAMPLE_COVERAGE));
}
#ifdef USE_GLUT
#else
static void keypress_event_handler(XKeyEvent *xevent)
{
printf("%u %c\n", xevent->keycode, (int)XLookupKeysym(xevent, 0));
keyboard(XLookupKeysym(xevent, 0), xevent->x, xevent->y);
}
static void expose_event_handler(XExposeEvent *xevent)
{
XWindowAttributes gwa;
if (!xevent->count) {
//XGetWindowAttributes(xevent->display, xevent->window, &gwa);
//reshape(gwa.width, gwa.height);
display_func();
glXSwapBuffers(xevent->display, xevent->window);
}
}
static void event_handler(Display *display, Window window)
{
XEvent xevent;
if (XCheckWindowEvent(display, window, KeyPressMask, &xevent)) {
switch (xevent.type) {
case KeyPress:
keypress_event_handler(&xevent.xkey);
break;
default:
break;
}
}
}
#endif
#define XPOS 100
#define YPOS 100
#define XSIZE 1024
#define YSIZE 1024
int main(int argc, char *argv[])
{
#ifdef USE_GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA);
glutInitWindowPosition(XPOS, YPOS);
glutInitWindowSize(XSIZE, YSIZE);
glutCreateWindow("Skeleton");
// linear_test();
glutDisplayFunc(display_func);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(display_func);
#else
Display *display;
Window root;
Window window;
XVisualInfo *visual;
GLXContext glcontext;
Colormap colormap;
XSetWindowAttributes windowattributes;
XWindowAttributes gwa;
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, GLX_ALPHA_SIZE, 8, GLX_STENCIL_SIZE, 8, GLX_ACCUM_GREEN_SIZE, 4, None };
display = XOpenDisplay(NULL);
root = DefaultRootWindow(display);
visual = glXChooseVisual(display, 0, att);
colormap = XCreateColormap(display, root, visual->visual, AllocNone);
windowattributes.colormap = colormap;
windowattributes.event_mask = ExposureMask | KeyPressMask;
window = XCreateWindow(display, root, XPOS, YPOS, XSIZE, YSIZE, 0, visual->depth, InputOutput, visual->visual, CWColormap | CWEventMask, &windowattributes);
XMapWindow(display, window);
XStoreName(display, window, "Skeleton");
glcontext = glXCreateContext(display, visual, NULL, GL_TRUE);
glXMakeCurrent(display, window, glcontext);
#endif
dump_info();
glShadeModel(GL_SMOOTH);
model_init(&model_bulky, -1.0);
model_init(&model_sticky, 1.0);
model_init(&model_simple, 3.0);
// cube_list = cube_display_list();
cube_list = cylinder_display_list(0.5, 1.0, 60);
frame_list = frame_display_list();
drawing_init();
depth_init();
lighting_init();
antialiasing_init();
culling_init();
#ifdef USE_GLUT
glutMainLoop();
#else
XGetWindowAttributes(display, window, &gwa);
reshape(gwa.width, gwa.height);
display_func();
glXSwapBuffers(display, window);
while(1) {
event_handler(display, window);
display_func();
glXSwapBuffers(display, window);
usleep(10000);
}
#endif
return 0;
}