Rev 1401 | Rev 1594 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1127 | giacomo | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Giacomo Guidi <giacomo@gandalf.sssup.it> |
||
10 | * |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
1400 | giacomo | 19 | #include <kernel/log.h> |
20 | #include <kernel/kern.h> |
||
21 | #include <math.h> |
||
22 | #include <stdlib.h> |
||
1127 | giacomo | 23 | |
1400 | giacomo | 24 | #include <drivers/shark_fb26.h> |
25 | #include <drivers/shark_keyb26.h> |
||
26 | |||
1127 | giacomo | 27 | #include <GL/osmesa.h> |
28 | #include <GL/glut.h> |
||
1400 | giacomo | 29 | |
1127 | giacomo | 30 | #ifndef M_PI |
31 | #define M_PI 3.14159265 |
||
32 | #endif |
||
33 | |||
34 | #define WIDTH 640 |
||
1321 | giacomo | 35 | #define HEIGHT 430 |
1127 | giacomo | 36 | #define BYTES_PP 2 //BytesPerPixel |
37 | |||
38 | OSMesaContext ctx; |
||
39 | |||
1400 | giacomo | 40 | extern void *video_memory; |
41 | |||
1127 | giacomo | 42 | unsigned char *rgb_565_buf = NULL; //RGB 16 bpp Buffer |
43 | unsigned char *video_buf = NULL; //Video Buffer |
||
44 | |||
45 | unsigned long int VMEMLONG = WIDTH * HEIGHT * BYTES_PP / 4; // Used by copy_videomem_16to16 |
||
46 | unsigned long int RGB565MEM = WIDTH * HEIGHT * BYTES_PP; // Total video mem |
||
47 | |||
1401 | giacomo | 48 | unsigned long int PERIOD_REFRESH = 100000; |
49 | unsigned long int PERIOD_DISEGNA = 100000; |
||
1127 | giacomo | 50 | |
51 | unsigned long int WCET_REFRESH, WCET_DISEGNA; |
||
52 | |||
53 | TASK refesh(void); |
||
54 | TASK disegna(void); |
||
55 | |||
56 | PID refresh_PID, disegna_PID; |
||
57 | |||
58 | static void |
||
59 | gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, |
||
60 | GLint teeth, GLfloat tooth_depth) |
||
61 | { |
||
62 | GLint i; |
||
63 | GLfloat r0, r1, r2; |
||
64 | GLfloat angle, da; |
||
65 | GLfloat u, v, len; |
||
66 | |||
67 | r0 = inner_radius; |
||
68 | r1 = outer_radius - tooth_depth / 2.0; |
||
69 | r2 = outer_radius + tooth_depth / 2.0; |
||
70 | |||
71 | da = 2.0 * M_PI / teeth / 4.0; |
||
72 | |||
73 | glShadeModel(GL_FLAT); |
||
74 | |||
75 | glNormal3f(0.0, 0.0, 1.0); |
||
76 | |||
77 | glBegin(GL_QUAD_STRIP); |
||
78 | for (i = 0; i <= teeth; i++) { |
||
79 | angle = i * 2.0 * M_PI / teeth; |
||
80 | glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); |
||
81 | glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); |
||
82 | if (i < teeth) { |
||
83 | glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); |
||
84 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5); |
||
85 | } |
||
86 | } |
||
87 | glEnd(); |
||
88 | |||
89 | glBegin(GL_QUADS); |
||
90 | da = 2.0 * M_PI / teeth / 4.0; |
||
91 | for (i = 0; i < teeth; i++) { |
||
92 | angle = i * 2.0 * M_PI / teeth; |
||
93 | |||
94 | glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); |
||
95 | glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5); |
||
96 | glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5); |
||
97 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5); |
||
98 | } |
||
99 | glEnd(); |
||
100 | |||
101 | glNormal3f(0.0, 0.0, -1.0); |
||
102 | |||
103 | glBegin(GL_QUAD_STRIP); |
||
104 | for (i = 0; i <= teeth; i++) { |
||
105 | angle = i * 2.0 * M_PI / teeth; |
||
106 | glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); |
||
107 | glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); |
||
108 | if (i < teeth) { |
||
109 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5); |
||
110 | glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); |
||
111 | } |
||
112 | } |
||
113 | glEnd(); |
||
114 | |||
115 | glBegin(GL_QUADS); |
||
116 | da = 2.0 * M_PI / teeth / 4.0; |
||
117 | for (i = 0; i < teeth; i++) { |
||
118 | angle = i * 2.0 * M_PI / teeth; |
||
119 | |||
120 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5); |
||
121 | glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5); |
||
122 | glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5); |
||
123 | glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); |
||
124 | } |
||
125 | glEnd(); |
||
126 | |||
127 | glBegin(GL_QUAD_STRIP); |
||
128 | for (i = 0; i < teeth; i++) { |
||
129 | angle = i * 2.0 * M_PI / teeth; |
||
130 | |||
131 | glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); |
||
132 | glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); |
||
133 | u = r2 * cos(angle + da) - r1 * cos(angle); |
||
134 | v = r2 * sin(angle + da) - r1 * sin(angle); |
||
135 | len = sqrt(u * u + v * v); |
||
136 | u /= len; |
||
137 | v /= len; |
||
138 | glNormal3f(v, -u, 0.0); |
||
139 | glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5); |
||
140 | glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5); |
||
141 | glNormal3f(cos(angle), sin(angle), 0.0); |
||
142 | glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5); |
||
143 | glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5); |
||
144 | u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da); |
||
145 | v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da); |
||
146 | glNormal3f(v, -u, 0.0); |
||
147 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5); |
||
148 | glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5); |
||
149 | glNormal3f(cos(angle), sin(angle), 0.0); |
||
150 | } |
||
151 | |||
152 | glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5); |
||
153 | glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5); |
||
154 | |||
155 | glEnd(); |
||
156 | |||
157 | glShadeModel(GL_SMOOTH); |
||
158 | |||
159 | glBegin(GL_QUAD_STRIP); |
||
160 | for (i = 0; i <= teeth; i++) { |
||
161 | angle = i * 2.0 * M_PI / teeth; |
||
162 | glNormal3f(-cos(angle), -sin(angle), 0.0); |
||
163 | glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); |
||
164 | glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); |
||
165 | } |
||
166 | glEnd(); |
||
167 | |||
168 | } |
||
169 | |||
170 | static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0; |
||
171 | static GLint gear1, gear2, gear3; |
||
172 | static GLfloat angle = 0.0; |
||
173 | |||
174 | static void draw(void) |
||
175 | { |
||
176 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
||
177 | |||
178 | glPushMatrix(); |
||
179 | glRotatef(view_rotx, 1.0, 0.0, 0.0); |
||
180 | glRotatef(view_roty, 0.0, 1.0, 0.0); |
||
181 | glRotatef(view_rotz, 0.0, 0.0, 1.0); |
||
182 | |||
183 | glPushMatrix(); |
||
184 | glTranslatef(-3.0, -2.0, 0.0); |
||
185 | glRotatef(angle, 0.0, 0.0, 1.0); |
||
186 | glCallList(gear1); |
||
187 | glPopMatrix(); |
||
188 | |||
189 | glPushMatrix(); |
||
190 | glTranslatef(3.1, -2.0, 0.0); |
||
191 | glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0); |
||
192 | glCallList(gear2); |
||
193 | glPopMatrix(); |
||
194 | |||
195 | glPushMatrix(); |
||
196 | glTranslatef(-3.1, 4.2, 0.0); |
||
197 | glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0); |
||
198 | glCallList(gear3); |
||
199 | glPopMatrix(); |
||
200 | |||
201 | glPopMatrix(); |
||
202 | |||
203 | glFinish(); |
||
204 | |||
205 | } |
||
206 | |||
207 | static void gl_init() |
||
208 | { |
||
209 | |||
210 | static GLfloat red[4] = {1.0, 0.0, 0.0, 1.0}; |
||
211 | static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0}; |
||
212 | static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0}; |
||
213 | static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0}; |
||
214 | static GLfloat h = (GLfloat) HEIGHT / (GLfloat) WIDTH; |
||
215 | |||
216 | //Create the OSMesa Context |
||
217 | ctx = OSMesaCreateContext(OSMESA_RGB_565, NULL); |
||
218 | |||
219 | //Make Current Context |
||
220 | OSMesaMakeCurrent(ctx, rgb_565_buf, GL_UNSIGNED_SHORT_5_6_5, WIDTH, HEIGHT); |
||
221 | |||
222 | glLightfv(GL_LIGHT0, GL_POSITION, pos); |
||
223 | glEnable(GL_CULL_FACE); |
||
224 | glEnable(GL_LIGHTING); |
||
225 | glEnable(GL_LIGHT0); |
||
226 | glEnable(GL_DEPTH_TEST); |
||
227 | |||
228 | /* make the gears */ |
||
229 | gear1 = glGenLists(1); |
||
230 | glNewList(gear1, GL_COMPILE); |
||
231 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); |
||
232 | gear(1.0, 4.0, 1.0, 20, 0.7); |
||
233 | glEndList(); |
||
234 | |||
235 | gear2 = glGenLists(1); |
||
236 | glNewList(gear2, GL_COMPILE); |
||
237 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green); |
||
238 | gear(0.5, 2.0, 2.0, 10, 0.7); |
||
239 | glEndList(); |
||
240 | |||
241 | gear3 = glGenLists(1); |
||
242 | glNewList(gear3, GL_COMPILE); |
||
243 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue); |
||
244 | gear(1.3, 2.0, 0.5, 10, 0.7); |
||
245 | glEndList(); |
||
246 | |||
247 | glEnable(GL_NORMALIZE); |
||
248 | |||
249 | glViewport(0, 0, (GLint) WIDTH, (GLint) HEIGHT); |
||
250 | glMatrixMode(GL_PROJECTION); |
||
251 | glLoadIdentity(); |
||
252 | glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0); |
||
253 | glMatrixMode(GL_MODELVIEW); |
||
254 | glLoadIdentity(); |
||
255 | glTranslatef(0.0, 0.0, -40.0); |
||
256 | |||
257 | } |
||
258 | |||
1400 | giacomo | 259 | void program_key_end(KEY_EVT *k) |
1127 | giacomo | 260 | { |
261 | |||
1550 | pj | 262 | exit(0); |
1127 | giacomo | 263 | |
264 | } |
||
265 | |||
266 | TASK refresh(void) |
||
267 | { |
||
268 | |||
269 | while(1) { |
||
270 | |||
1401 | giacomo | 271 | task_testcancel(); |
1321 | giacomo | 272 | memcpy((video_buf+40*WIDTH*2), rgb_565_buf, RGB565MEM); |
1127 | giacomo | 273 | task_endcycle(); |
274 | |||
275 | } |
||
276 | |||
1550 | pj | 277 | exit(0); |
1127 | giacomo | 278 | |
279 | } |
||
280 | |||
281 | |||
282 | TASK disegna(void) |
||
283 | { |
||
284 | |||
285 | char text[100]; |
||
286 | TIME disegna_TIME, refresh_TIME; |
||
287 | |||
288 | while(1) { |
||
1401 | giacomo | 289 | |
290 | task_testcancel(); |
||
1127 | giacomo | 291 | |
292 | jet_gettable(refresh_PID, &refresh_TIME, 1); |
||
293 | jet_gettable(disegna_PID, &disegna_TIME, 1); |
||
294 | |||
295 | angle += 1.0; |
||
296 | |||
297 | draw(); |
||
298 | |||
299 | sprintf(text,"Hard Task Refresh PER:%6d us EX:%6d us",(int)PERIOD_REFRESH,(int)refresh_TIME); |
||
1321 | giacomo | 300 | grx_text(text,10,5,color16(0,0,255),0); |
1127 | giacomo | 301 | sprintf(text,"Hard Task Draw PER:%6d us EX:%6d us",(int)PERIOD_DISEGNA,(int)disegna_TIME); |
1321 | giacomo | 302 | grx_text(text,10,15,color16(0,0,255),0); |
1127 | giacomo | 303 | |
304 | task_endcycle(); |
||
305 | |||
306 | } |
||
307 | |||
1550 | pj | 308 | exit(0); |
1127 | giacomo | 309 | |
310 | } |
||
311 | |||
312 | int main (int argc, char *argv[]) |
||
313 | { |
||
314 | |||
315 | HARD_TASK_MODEL ht_refresh, ht_disegna; |
||
316 | |||
1401 | giacomo | 317 | WCET_REFRESH =((long int) PERIOD_REFRESH * (0.30)); |
318 | WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.55)); |
||
1127 | giacomo | 319 | |
320 | hard_task_default_model(ht_refresh); |
||
321 | hard_task_def_wcet(ht_refresh,WCET_REFRESH); |
||
322 | hard_task_def_mit(ht_refresh,PERIOD_REFRESH); |
||
323 | hard_task_def_usemath(ht_refresh); |
||
324 | hard_task_def_group(ht_refresh,1); |
||
325 | hard_task_def_ctrl_jet(ht_refresh); |
||
326 | |||
327 | refresh_PID = task_create("refresh", refresh, &ht_refresh, NULL); |
||
328 | if (refresh_PID == -1) { |
||
1550 | pj | 329 | exit(0); |
1127 | giacomo | 330 | } |
331 | |||
332 | hard_task_default_model(ht_disegna); |
||
333 | hard_task_def_mit(ht_disegna,PERIOD_DISEGNA); |
||
334 | hard_task_def_wcet(ht_disegna,WCET_DISEGNA); |
||
335 | hard_task_def_group(ht_disegna,1); |
||
336 | hard_task_def_ctrl_jet(ht_disegna); |
||
337 | hard_task_def_usemath(ht_disegna); |
||
1156 | giacomo | 338 | hard_task_def_stack(ht_disegna,30000); //VERY IMPORTANT FOR glCallList !! |
1127 | giacomo | 339 | |
340 | disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL); |
||
341 | if (disegna_PID == -1) { |
||
1550 | pj | 342 | exit(0); |
1127 | giacomo | 343 | } |
344 | |||
345 | { |
||
346 | KEY_EVT k; |
||
347 | k.flag = ALTL_BIT; |
||
348 | k.scan = KEY_C; |
||
349 | k.ascii = 'c'; |
||
1400 | giacomo | 350 | k.status = KEY_PRESSED; |
351 | keyb_hook(k,program_key_end,FALSE); |
||
1127 | giacomo | 352 | } |
1155 | giacomo | 353 | |
354 | rgb_565_buf = malloc(RGB565MEM); |
||
1400 | giacomo | 355 | video_buf = (unsigned char *)video_memory; |
1155 | giacomo | 356 | |
357 | gl_init(); |
||
358 | |||
1127 | giacomo | 359 | memset(rgb_565_buf, 0, RGB565MEM); |
360 | |||
361 | group_activate(1); |
||
362 | |||
363 | return 0; |
||
364 | |||
365 | } |