Subversion Repositories shark

Rev

Rev 1155 | Rev 1314 | 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
 
19
#include <drivers/vga.h>
20
#include <drivers/keyb.h>
21
 
22
#include <GL/osmesa.h>
23
#include <GL/glut.h>
24
 
25
#include <math.h>
26
#include <stdlib.h>
27
 
28
#include <kernel/log.h>
29
#include <kernel/kern.h>
30
 
31
#ifndef M_PI
32
#define M_PI 3.14159265
33
#endif
34
 
35
#define WIDTH 640
36
#define HEIGHT 480
37
#define BYTES_PP 2 //BytesPerPixel
38
#define INITSTR G640x480x64K //SVGAlib standard mode definitions
1154 giacomo 39
#define CARD VESA //Video driver
1127 giacomo 40
 
41
OSMesaContext ctx;
42
 
43
unsigned char *rgb_565_buf = NULL; //RGB 16 bpp Buffer
44
unsigned char *video_buf = NULL; //Video Buffer
45
 
46
unsigned long int VMEMLONG = WIDTH * HEIGHT * BYTES_PP / 4; // Used by copy_videomem_16to16
47
unsigned long int RGB565MEM = WIDTH * HEIGHT * BYTES_PP; // Total video mem
48
 
1156 giacomo 49
unsigned long int PERIOD_REFRESH = 100000;
50
unsigned long int PERIOD_DISEGNA = 100000;
1127 giacomo 51
 
52
unsigned long int WCET_REFRESH, WCET_DISEGNA;
53
 
54
TASK refesh(void);
55
TASK disegna(void);
56
 
57
PID refresh_PID, disegna_PID;
58
 
59
static void
60
gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
61
  GLint teeth, GLfloat tooth_depth)
62
{
63
  GLint i;
64
  GLfloat r0, r1, r2;
65
  GLfloat angle, da;
66
  GLfloat u, v, len;
67
 
68
  r0 = inner_radius;
69
  r1 = outer_radius - tooth_depth / 2.0;
70
  r2 = outer_radius + tooth_depth / 2.0;
71
 
72
  da = 2.0 * M_PI / teeth / 4.0;
73
 
74
  glShadeModel(GL_FLAT);
75
 
76
  glNormal3f(0.0, 0.0, 1.0);
77
 
78
  glBegin(GL_QUAD_STRIP);
79
  for (i = 0; i <= teeth; i++) {
80
    angle = i * 2.0 * M_PI / teeth;
81
    glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
82
    glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
83
    if (i < teeth) {
84
      glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
85
      glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
86
    }
87
  }
88
  glEnd();
89
 
90
  glBegin(GL_QUADS);
91
  da = 2.0 * M_PI / teeth / 4.0;
92
  for (i = 0; i < teeth; i++) {
93
    angle = i * 2.0 * M_PI / teeth;
94
 
95
    glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
96
    glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
97
    glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
98
    glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
99
  }
100
  glEnd();
101
 
102
  glNormal3f(0.0, 0.0, -1.0);
103
 
104
  glBegin(GL_QUAD_STRIP);
105
  for (i = 0; i <= teeth; i++) {
106
    angle = i * 2.0 * M_PI / teeth;
107
    glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
108
    glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
109
    if (i < teeth) {
110
      glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
111
      glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
112
    }
113
  }
114
  glEnd();
115
 
116
  glBegin(GL_QUADS);
117
  da = 2.0 * M_PI / teeth / 4.0;
118
  for (i = 0; i < teeth; i++) {
119
    angle = i * 2.0 * M_PI / teeth;
120
 
121
    glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
122
    glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
123
    glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
124
    glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
125
  }
126
  glEnd();
127
 
128
  glBegin(GL_QUAD_STRIP);
129
  for (i = 0; i < teeth; i++) {
130
    angle = i * 2.0 * M_PI / teeth;
131
 
132
    glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
133
    glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
134
    u = r2 * cos(angle + da) - r1 * cos(angle);
135
    v = r2 * sin(angle + da) - r1 * sin(angle);
136
    len = sqrt(u * u + v * v);
137
    u /= len;
138
    v /= len;
139
    glNormal3f(v, -u, 0.0);
140
    glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
141
    glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
142
    glNormal3f(cos(angle), sin(angle), 0.0);
143
    glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
144
    glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
145
    u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
146
    v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
147
    glNormal3f(v, -u, 0.0);
148
    glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
149
    glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
150
    glNormal3f(cos(angle), sin(angle), 0.0);
151
  }
152
 
153
  glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
154
  glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
155
 
156
  glEnd();
157
 
158
  glShadeModel(GL_SMOOTH);
159
 
160
  glBegin(GL_QUAD_STRIP);
161
  for (i = 0; i <= teeth; i++) {
162
    angle = i * 2.0 * M_PI / teeth;
163
    glNormal3f(-cos(angle), -sin(angle), 0.0);
164
    glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
165
    glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
166
  }
167
  glEnd();
168
 
169
}
170
 
171
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
172
static GLint gear1, gear2, gear3;
173
static GLfloat angle = 0.0;
174
 
175
static void draw(void)
176
{
177
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
178
 
179
  glPushMatrix();
180
    glRotatef(view_rotx, 1.0, 0.0, 0.0);
181
    glRotatef(view_roty, 0.0, 1.0, 0.0);
182
    glRotatef(view_rotz, 0.0, 0.0, 1.0);
183
 
184
    glPushMatrix();
185
      glTranslatef(-3.0, -2.0, 0.0);
186
      glRotatef(angle, 0.0, 0.0, 1.0);
187
      glCallList(gear1);
188
    glPopMatrix();
189
 
190
    glPushMatrix();
191
      glTranslatef(3.1, -2.0, 0.0);
192
      glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
193
      glCallList(gear2);
194
    glPopMatrix();
195
 
196
    glPushMatrix();
197
      glTranslatef(-3.1, 4.2, 0.0);
198
      glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
199
      glCallList(gear3);
200
    glPopMatrix();
201
 
202
  glPopMatrix();
203
 
204
  glFinish();
205
 
206
}
207
 
208
static void gl_init()
209
{
210
 
211
  static GLfloat red[4] = {1.0, 0.0, 0.0, 1.0};
212
  static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
213
  static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
214
  static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0};
215
  static GLfloat h = (GLfloat) HEIGHT / (GLfloat) WIDTH;
216
 
217
  //Create the OSMesa Context
218
  ctx = OSMesaCreateContext(OSMESA_RGB_565, NULL);
219
 
220
  //Make Current Context
221
  OSMesaMakeCurrent(ctx, rgb_565_buf, GL_UNSIGNED_SHORT_5_6_5, WIDTH, HEIGHT);
222
 
223
  glLightfv(GL_LIGHT0, GL_POSITION, pos);
224
  glEnable(GL_CULL_FACE);
225
  glEnable(GL_LIGHTING);
226
  glEnable(GL_LIGHT0);
227
  glEnable(GL_DEPTH_TEST);
228
 
229
  /* make the gears */
230
  gear1 = glGenLists(1);
231
  glNewList(gear1, GL_COMPILE);
232
  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
233
  gear(1.0, 4.0, 1.0, 20, 0.7);
234
  glEndList();
235
 
236
  gear2 = glGenLists(1);
237
  glNewList(gear2, GL_COMPILE);
238
  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
239
  gear(0.5, 2.0, 2.0, 10, 0.7);
240
  glEndList();
241
 
242
  gear3 = glGenLists(1);
243
  glNewList(gear3, GL_COMPILE);
244
  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
245
  gear(1.3, 2.0, 0.5, 10, 0.7);
246
  glEndList();
247
 
248
  glEnable(GL_NORMALIZE);
249
 
250
  glViewport(0, 0, (GLint) WIDTH, (GLint) HEIGHT);
251
  glMatrixMode(GL_PROJECTION);
252
  glLoadIdentity();
253
  glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
254
  glMatrixMode(GL_MODELVIEW);
255
  glLoadIdentity();
256
  glTranslatef(0.0, 0.0, -40.0);
257
 
258
}
259
 
260
static int screen(int mode)
261
{
262
 
263
  vga_modeinfo *minf;
264
 
265
  vga_setmode(mode,CARD);
266
  minf = vga_getmodeinfo(mode);
267
  if(! (minf->flags & CAPABLE_LINEAR)){
268
    vga_setmode(TEXT,CARD);
269
    printk(KERN_INFO "The mode %d is not capable of linear\n",mode);
270
    return 1;
271
  }
272
  vga_setpage(0);
273
  if(vga_setlinearaddressing() == -1) {
274
    vga_setmode(TEXT,CARD);
275
    printk(KERN_INFO "Could not set linear addressing for mode %d\n",mode);
276
    return 1;
277
  }
278
 
279
  video_buf = vga_getgraphmem();
280
 
281
  grx_setbuffer(rgb_565_buf, WIDTH, HEIGHT);    //Init of RGB16 version of grx functions
282
                                                //created to work with Mesa buffer
283
  return 0;
284
 
285
}
286
 
287
void program_end(void *arg)
288
{
289
 
290
  OSMesaDestroyContext(ctx);
291
  free(rgb_565_buf);
292
 
293
  vga_setmode(TEXT,CARD);
294
 
295
  sys_end();
296
 
297
}
298
 
299
void program_key_end(KEY_EVT *k)
300
{
301
 
302
  sys_end();
303
 
304
}
305
 
306
TASK refresh(void)
307
{
308
 
309
  while(1) {
310
 
311
    copy_videomem_16to16(rgb_565_buf, video_buf, VMEMLONG);
312
    task_endcycle();
313
 
314
  }
315
 
316
  sys_end();
317
 
318
}
319
 
320
 
321
TASK disegna(void)
322
{
323
 
324
  char text[100];
325
  TIME disegna_TIME, refresh_TIME;
326
 
327
  while(1) {
328
 
329
    jet_gettable(refresh_PID, &refresh_TIME, 1);
330
    jet_gettable(disegna_PID, &disegna_TIME, 1);
331
 
332
    angle += 1.0;
333
 
334
    draw();
335
 
336
    sprintf(text,"Hard Task Refresh PER:%6d us EX:%6d us",(int)PERIOD_REFRESH,(int)refresh_TIME);
337
    grx_text(text,10,5,rgb16(0,0,255),0);    
338
    sprintf(text,"Hard Task Draw    PER:%6d us EX:%6d us",(int)PERIOD_DISEGNA,(int)disegna_TIME);
339
    grx_text(text,10,15,rgb16(0,0,255),0);
340
 
341
    task_endcycle();
342
 
343
  }
344
 
345
  sys_end();
346
 
347
}
348
 
349
int main (int argc, char *argv[])
350
{
351
 
352
    HARD_TASK_MODEL ht_refresh, ht_disegna;
353
 
354
    sys_atrunlevel(program_end,NULL, RUNLEVEL_BEFORE_EXIT);
355
 
356
    clear();
357
 
1156 giacomo 358
    WCET_REFRESH =((long int) PERIOD_REFRESH * (0.25));
1155 giacomo 359
    WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.6));
1127 giacomo 360
 
361
    hard_task_default_model(ht_refresh);
362
    hard_task_def_wcet(ht_refresh,WCET_REFRESH);
363
    hard_task_def_mit(ht_refresh,PERIOD_REFRESH);
364
    hard_task_def_usemath(ht_refresh);
365
    hard_task_def_group(ht_refresh,1);
366
    hard_task_def_ctrl_jet(ht_refresh);
367
 
368
    refresh_PID = task_create("refresh", refresh, &ht_refresh, NULL);
369
    if (refresh_PID == -1) {
370
      sys_end();
371
      exit(4);
372
    }
373
 
374
    hard_task_default_model(ht_disegna);
375
    hard_task_def_mit(ht_disegna,PERIOD_DISEGNA);
376
    hard_task_def_wcet(ht_disegna,WCET_DISEGNA);
377
    hard_task_def_group(ht_disegna,1);
378
    hard_task_def_ctrl_jet(ht_disegna);
379
    hard_task_def_usemath(ht_disegna);
1156 giacomo 380
    hard_task_def_stack(ht_disegna,30000); //VERY IMPORTANT FOR glCallList !!
1127 giacomo 381
 
382
    disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL);
383
    if (disegna_PID == -1) {
384
      sys_end();
385
      exit(4);
386
    }
387
 
388
    {
389
      KEY_EVT k;
390
      k.flag = ALTL_BIT;
391
      k.scan = KEY_C;
392
      k.ascii = 'c';
393
      keyb_hook(k,program_key_end);
394
    }
1155 giacomo 395
 
396
    rgb_565_buf = malloc(RGB565MEM);
397
 
398
    gl_init();
399
 
1127 giacomo 400
    if (screen(INITSTR)) {
401
        printk(KERN_INFO "Graphical initialization failed !!\n");
402
        sys_end();
403
    }
404
 
405
    memset(rgb_565_buf, 0, RGB565MEM);
406
 
407
    group_activate(1);
408
 
409
    return 0;
410
 
411
}