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