rev |
line source |
nuclear@2
|
1 #include <stdio.h>
|
nuclear@2
|
2 #include <stdlib.h>
|
nuclear@2
|
3 #include <time.h>
|
nuclear@2
|
4 #include <sys/time.h>
|
nuclear@2
|
5
|
nuclear@2
|
6 #ifndef __APPLE__
|
nuclear@2
|
7 #include <GL/glut.h>
|
nuclear@2
|
8 #else
|
nuclear@2
|
9 #include <GLUT/glut.h>
|
nuclear@2
|
10 #endif
|
nuclear@2
|
11
|
nuclear@2
|
12 #include "dsys2.h"
|
nuclear@2
|
13
|
nuclear@2
|
14 void disp(void);
|
nuclear@2
|
15 void draw_teapot(float sec);
|
nuclear@2
|
16 void reshape(int x, int y);
|
nuclear@2
|
17 void keyb(unsigned char key, int x, int y);
|
nuclear@2
|
18 unsigned int get_ticks(void);
|
nuclear@2
|
19
|
nuclear@2
|
20 struct dsys_demo *demo;
|
nuclear@2
|
21
|
nuclear@2
|
22 int main(int argc, char **argv)
|
nuclear@2
|
23 {
|
nuclear@2
|
24 float lpos[] = {-100, 100, 100, 1};
|
nuclear@2
|
25
|
nuclear@2
|
26 glutInit(&argc, argv);
|
nuclear@2
|
27 glutInitWindowSize(800, 600);
|
nuclear@2
|
28 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
|
nuclear@2
|
29 glutCreateWindow("foo");
|
nuclear@2
|
30
|
nuclear@2
|
31 glutDisplayFunc(disp);
|
nuclear@2
|
32 glutReshapeFunc(reshape);
|
nuclear@2
|
33 glutKeyboardFunc(keyb);
|
nuclear@2
|
34 glutIdleFunc(glutPostRedisplay);
|
nuclear@2
|
35
|
nuclear@2
|
36 glEnable(GL_LIGHTING);
|
nuclear@2
|
37 glEnable(GL_LIGHT0);
|
nuclear@2
|
38 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
nuclear@2
|
39
|
nuclear@2
|
40 glEnable(GL_CULL_FACE);
|
nuclear@2
|
41 glEnable(GL_DEPTH_TEST);
|
nuclear@2
|
42
|
nuclear@2
|
43 if(!(demo = dsys_open("/dev/null"))) {
|
nuclear@2
|
44 return 1;
|
nuclear@2
|
45 }
|
nuclear@2
|
46
|
nuclear@2
|
47 glutMainLoop();
|
nuclear@2
|
48 return 0;
|
nuclear@2
|
49 }
|
nuclear@2
|
50
|
nuclear@2
|
51
|
nuclear@2
|
52 void disp(void)
|
nuclear@2
|
53 {
|
nuclear@2
|
54 float sec;
|
nuclear@2
|
55
|
nuclear@2
|
56 dsys_update(demo, dsys_msec_to_dtime(get_ticks()));
|
nuclear@2
|
57 sec = dsys_dtime_to_sec(dsys_time(demo));
|
nuclear@2
|
58
|
nuclear@2
|
59 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@2
|
60
|
nuclear@2
|
61 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
62 glLoadIdentity();
|
nuclear@2
|
63 glTranslatef(0, 0, -8);
|
nuclear@2
|
64 glRotatef(30.0, 1, 0, 0);
|
nuclear@2
|
65
|
nuclear@2
|
66 draw_teapot(sec);
|
nuclear@2
|
67
|
nuclear@2
|
68 glutSwapBuffers();
|
nuclear@2
|
69 }
|
nuclear@2
|
70
|
nuclear@2
|
71 void draw_teapot(float sec)
|
nuclear@2
|
72 {
|
nuclear@2
|
73 float dcol[] = {0.2, 0.4, 0.8, 1.0};
|
nuclear@2
|
74 float scol[] = {0.8, 0.8, 0.8, 1.0};
|
nuclear@2
|
75
|
nuclear@2
|
76 glPushMatrix();
|
nuclear@2
|
77 glRotatef(sec * 100.0, 0, 1, 0);
|
nuclear@2
|
78
|
nuclear@2
|
79 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
|
nuclear@2
|
80 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
|
nuclear@2
|
81 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
|
nuclear@2
|
82
|
nuclear@2
|
83 glFrontFace(GL_CW);
|
nuclear@2
|
84 glutSolidTeapot(1.0);
|
nuclear@2
|
85 glFrontFace(GL_CCW);
|
nuclear@2
|
86
|
nuclear@2
|
87 glPopMatrix();
|
nuclear@2
|
88 }
|
nuclear@2
|
89
|
nuclear@2
|
90 void reshape(int x, int y)
|
nuclear@2
|
91 {
|
nuclear@2
|
92 glViewport(0, 0, x, y);
|
nuclear@2
|
93
|
nuclear@2
|
94 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
95 glLoadIdentity();
|
nuclear@2
|
96 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
|
nuclear@2
|
97 }
|
nuclear@2
|
98
|
nuclear@2
|
99 void keyb(unsigned char key, int x, int y)
|
nuclear@2
|
100 {
|
nuclear@2
|
101 switch(key) {
|
nuclear@2
|
102 case 27:
|
nuclear@2
|
103 exit(0);
|
nuclear@2
|
104
|
nuclear@2
|
105 case ' ':
|
nuclear@2
|
106 if(dsys_is_running(demo)) {
|
nuclear@2
|
107 dsys_stop(demo);
|
nuclear@2
|
108 } else {
|
nuclear@2
|
109 dsys_start(demo);
|
nuclear@2
|
110 }
|
nuclear@2
|
111 break;
|
nuclear@2
|
112
|
nuclear@2
|
113 default:
|
nuclear@2
|
114 break;
|
nuclear@2
|
115 }
|
nuclear@2
|
116 }
|
nuclear@2
|
117
|
nuclear@2
|
118 unsigned int get_ticks(void)
|
nuclear@2
|
119 {
|
nuclear@2
|
120 static struct timeval tv0;
|
nuclear@2
|
121 struct timeval tv;
|
nuclear@2
|
122
|
nuclear@2
|
123 gettimeofday(&tv, 0);
|
nuclear@2
|
124
|
nuclear@2
|
125 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
|
nuclear@2
|
126 tv0 = tv;
|
nuclear@2
|
127 }
|
nuclear@2
|
128 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
|
nuclear@2
|
129 }
|