Subversion Repositories shark

Rev

Rev 1625 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1624 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/glib.h>
20
#include <drivers/keyb.h>
21
 
22
#include <GL/osmesa.h>
23
#include <GL/glut.h>
24
#include <png.h>
25
 
26
#include <math.h>
27
#include <stdlib.h>
28
#include <assert.h>
29
#include <kernel/log.h>
30
#include <kernel/kern.h>
31
 
32
#include <stdio.h>
33
 
34
#ifndef M_PI
35
#define M_PI 3.14159265
36
#endif
37
 
38
#define WIDTH 640
39
#define HEIGHT 480
40
#define BYTES_PP 2 //BytesPerPixel
41
 
42
#define DEG2RAD (3.14159/180.0)
43
 
44
static GLint ImgWidth = 0, ImgHeight = 0;
45
static GLenum ImgFormat = 0;
46
static GLubyte *Image = NULL;
47
 
48
#define MAX_OBJECTS 2
49
static GLint table_list;
50
static GLint objects_list[MAX_OBJECTS];
51
 
52
static GLfloat xrot, yrot;
53
static GLfloat spin;
54
 
55
OSMesaContext ctx;
56
 
57
unsigned char *rgb_565_buf = NULL; //RGB 16 bpp Buffer
58
unsigned char *video_buf = NULL; //Video Buffer
59
 
60
unsigned long int VMEMLONG = WIDTH * HEIGHT * BYTES_PP / 4; // Used by copy_videomem_16to16
61
unsigned long int RGB565MEM = WIDTH * HEIGHT * BYTES_PP; // Total video mem
62
 
63
unsigned long int PERIOD_REFRESH = 150000;
64
unsigned long int PERIOD_DISEGNA = 150000;
65
 
66
unsigned long int WCET_REFRESH, WCET_DISEGNA;
67
 
68
TASK refesh(void);
69
TASK disegna(void);
70
 
71
PID refresh_PID, disegna_PID;
72
 
73
void read_png_file(char *file_name, GLubyte **buffer, GLint *width, GLint *height, png_byte *color_type)
74
{
75
 
76
        int y;
77
        png_byte bit_depth;
78
 
79
        png_structp png_ptr;
80
        png_infop info_ptr;
81
        int number_of_passes;
82
        png_bytep * row_pointers;
83
 
84
        char header[8]; // 8 is the maximum size that can be checked
85
 
86
        /* open file and test for it being a png */
87
        FILE *fp = fopen(file_name, "rb");
88
        if (!fp) {
89
                cprintf("[read_png_file] File %s could not be opened for reading\n", file_name);
1650 pj 90
                exit(1);
1624 giacomo 91
        }
92
        fread(header, 1, 8, fp);
93
 
94
        if (png_sig_cmp(header, 0, 8))
95
                cprintf("[read_png_file] File %s is not recognized as a PNG file\n", file_name);
96
 
97
        /* initialize stuff */
98
 
99
        png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
100
 
101
        if (!png_ptr)
102
                cprintf("[read_png_file] png_create_read_struct failed\n");
103
 
104
        info_ptr = png_create_info_struct(png_ptr);
105
        if (!info_ptr)
106
                cprintf("[read_png_file] png_create_info_struct failed\n");
107
 
108
        png_init_io(png_ptr, fp);
109
        png_set_sig_bytes(png_ptr, 8);
110
 
111
        png_read_info(png_ptr, info_ptr);
112
 
113
        *width = info_ptr->width;
114
        *height = info_ptr->height;
115
        *color_type = info_ptr->color_type;
116
        bit_depth = info_ptr->bit_depth;
117
 
118
        number_of_passes = png_set_interlace_handling(png_ptr);
119
 
120
        cprintf("Open PNG W: %d H: %d CT: %d BD: %d\n",*width,*height,*color_type,bit_depth);
121
 
122
        png_read_update_info(png_ptr, info_ptr);
123
 
124
        row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * info_ptr->height);
125
        for (y=0; y<info_ptr->height; y++)
126
                row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes);
127
 
128
        png_read_image(png_ptr, row_pointers);
129
 
130
        if(info_ptr->color_type == PNG_COLOR_TYPE_RGB) {
131
                *buffer = malloc(info_ptr->height * info_ptr->rowbytes);
132
                for(y=0; y<info_ptr->height; y++)
133
                        memcpy(*buffer+y*info_ptr->rowbytes,row_pointers[y],info_ptr->rowbytes);
134
        }
135
 
136
        fclose(fp);
137
 
138
}
139
 
140
static void make_table( void )
141
{
142
   static GLfloat table_mat[] = { 1.0, 1.0, 1.0, 0.6 };
143
   static GLfloat gray[] = { 0.4, 0.4, 0.4, 1.0 };
144
 
145
   table_list = glGenLists(1);
146
   glNewList( table_list, GL_COMPILE );
147
 
148
   /* load table's texture */
149
   glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, table_mat );
150
/*   glMaterialfv( GL_FRONT, GL_EMISSION, gray );*/
151
   glMaterialfv( GL_FRONT, GL_DIFFUSE, table_mat );
152
   glMaterialfv( GL_FRONT, GL_AMBIENT, gray );
153
 
154
   /* draw textured square for the table */
155
   glPushMatrix();
156
   glScalef( 4.0, 4.0, 4.0 );
157
   glBegin( GL_POLYGON );
158
   glNormal3f( 0.0, 1.0, 0.0 );
159
   glTexCoord2f( 0.0, 0.0 );   glVertex3f(  1.0, 0.0, -1.0 );
160
   glTexCoord2f( 1.0, 0.0 );   glVertex3f(  1.0, 0.0,  1.0 );
161
   glTexCoord2f( 1.0, 1.0 );   glVertex3f( -1.0, 0.0,  1.0 );
162
   glTexCoord2f( 0.0, 1.0 );   glVertex3f( -1.0, 0.0, -1.0 );
163
   glEnd();
164
   glPopMatrix();
165
 
166
   glDisable( GL_TEXTURE_2D );
167
 
168
   glEndList();
169
}
170
 
171
 
172
static void make_objects( void )
173
{
174
   GLUquadricObj *q;
175
 
176
   static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
177
   static GLfloat green[] = { 0.2, 1.0, 0.2, 1.0 };
178
   static GLfloat black[] = { 0.0, 0.0, 0.0, 0.0 };
179
 
180
   q = gluNewQuadric();
181
   gluQuadricDrawStyle( q, GLU_FILL );
182
   gluQuadricNormals( q, GLU_SMOOTH );
183
 
184
   objects_list[0] = glGenLists(1);
185
   glNewList( objects_list[0], GL_COMPILE );
186
   glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan );
187
   glMaterialfv( GL_FRONT, GL_EMISSION, black );
188
   gluCylinder( q, 0.5, 0.5,  1.0, 15, 1 );
189
   glEndList();
190
 
191
   objects_list[1] = glGenLists(1);
192
   glNewList( objects_list[1], GL_COMPILE );
193
   glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
194
   glMaterialfv( GL_FRONT, GL_EMISSION, black );
195
   gluCylinder( q, 1.5, 0.0,  2.5, 15, 1 );
196
   glEndList();
197
}
198
 
199
static void gl_init( void )
200
{
201
   png_byte img_color;
202
 
203
   GLfloat yAspect = 2.5;
204
   GLfloat xAspect = yAspect * (float) WIDTH / (float) HEIGHT;
205
 
206
   //Create the OSMesa Context
207
   ctx = OSMesaCreateContext(OSMESA_RGB_565, NULL);
208
 
209
   //Make Current Context
210
   OSMesaMakeCurrent(ctx, rgb_565_buf, GL_UNSIGNED_SHORT_5_6_5, WIDTH, HEIGHT);
211
 
212
   read_png_file("test.png",&Image,&ImgWidth,&ImgHeight,&img_color);
213
   if (img_color == PNG_COLOR_TYPE_RGB && Image != NULL)
214
           ImgFormat = GL_RGB;
215
   else {
216
           cprintf("Texture Load Falied !!\n");
1650 pj 217
           exit(1);
1624 giacomo 218
   }
219
 
220
   make_table();
221
   make_objects();
222
 
223
   gluBuild2DMipmaps(GL_TEXTURE_2D, 3, ImgWidth, ImgHeight,
224
                     ImgFormat, GL_UNSIGNED_BYTE, Image);
225
 
226
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
227
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
228
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
229
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
230
 
231
   xrot = 30.0;
232
   yrot = 50.0;
233
   spin = 0.0;
234
 
235
   glShadeModel( GL_FLAT );
236
 
237
   glEnable( GL_LIGHT0 );
238
   glEnable( GL_LIGHTING );
239
 
240
   glClearColor( 0.5, 0.5, 0.9, 0.0 );
241
 
242
   glEnable( GL_NORMALIZE );
243
 
244
   glViewport(0, 0, WIDTH, HEIGHT);
245
   glMatrixMode(GL_PROJECTION);
246
   glLoadIdentity();
247
   glFrustum( -xAspect, xAspect, yAspect, -yAspect, 10.0, 30.0 );
248
   glMatrixMode(GL_MODELVIEW);
249
   glLoadIdentity();
250
 
251
}
252
 
253
static void draw_objects( GLfloat eyex, GLfloat eyey, GLfloat eyez )
254
{
255
   (void) eyex;
256
   (void) eyey;
257
   (void) eyez;
258
#ifndef USE_ZBUFFER
259
   if (eyex<0.5) {
260
#endif
261
           glPushMatrix();
262
           glTranslatef( 1.0, 1.5, 0.0 );
263
           glRotatef( spin, 1.0, 0.5, 0.0 );
264
           glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
265
           glCallList( objects_list[0] );
266
           glPopMatrix();
267
 
268
           glPushMatrix();
269
           glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*spin) ), 0.0 );
270
           glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
271
           glRotatef( spin, 1.0, 0.5, 0.0 );
272
           glScalef( 0.5, 0.5, 0.5 );
273
           glCallList( objects_list[1] );
274
           glPopMatrix();
275
#ifndef USE_ZBUFFER
276
   }
277
   else {      
278
           glPushMatrix();
279
           glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*spin) ), 0.0 );
280
           glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
281
           glRotatef( spin, 1.0, 0.5, 0.0 );
282
           glScalef( 0.5, 0.5, 0.5 );
283
           glCallList( objects_list[1] );
284
           glPopMatrix();
285
 
286
           glPushMatrix();
287
           glTranslatef( 1.0, 1.5, 0.0 );
288
           glRotatef( spin, 1.0, 0.5, 0.0 );
289
           glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
290
           glCallList( objects_list[0] );
291
           glPopMatrix();
292
   }
293
#endif
294
 
295
}
296
 
297
static void draw_table( void )
298
{
299
  glCallList( table_list );
300
}
301
 
302
static void draw( void )
303
{
304
   static GLfloat light_pos[] = { 0.0, 20.0, 0.0, 1.0 };
305
   GLfloat dist = 20.0;
306
   GLfloat eyex, eyey, eyez;
307
 
308
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
309
 
310
 
311
   eyex = dist * cos(yrot*DEG2RAD) * cos(xrot*DEG2RAD);
312
   eyez = dist * sin(yrot*DEG2RAD) * cos(xrot*DEG2RAD);
313
   eyey = dist * sin(xrot*DEG2RAD);
314
 
315
   /* view from top */
316
   glPushMatrix();
317
   gluLookAt( eyex, eyey, eyez, 0.0, 0.0, 0.0,  0.0, 1.0, 0.0 );
318
 
319
   glLightfv( GL_LIGHT0, GL_POSITION, light_pos );
320
 
321
   /* draw table into stencil planes */
322
   glDisable( GL_DEPTH_TEST );
323
   glEnable( GL_STENCIL_TEST );
324
   glStencilFunc( GL_ALWAYS, 1, 0xffffffff );
325
   glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );
326
   glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
327
   draw_table();
328
   glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
329
 
330
   glEnable( GL_DEPTH_TEST );
331
 
332
   /* render view from below (reflected viewport) */
333
   /* only draw where stencil==1 */
334
   if (eyey>0.0) {
335
      glPushMatrix();
336
 
337
      glStencilFunc( GL_EQUAL, 1, 0xffffffff );  /* draw if ==1 */
338
      glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
339
      glScalef( 1.0, -1.0, 1.0 );
340
 
341
      /* Reposition light in reflected space. */
342
      glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
343
 
344
      draw_objects(eyex, eyey, eyez);
345
      glPopMatrix();
346
 
347
      /* Restore light's original unreflected position. */
348
      glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
349
   }
350
 
351
   glDisable( GL_STENCIL_TEST );
352
 
353
   glEnable( GL_BLEND );
354
   glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
355
 
356
   glEnable( GL_TEXTURE_2D );
357
   draw_table();
358
   glDisable( GL_TEXTURE_2D );
359
   glDisable( GL_BLEND );
360
 
361
   /* view from top */
362
   glPushMatrix();
363
 
364
   draw_objects(eyex, eyey, eyez);
365
 
366
   glPopMatrix();
367
 
368
   glPopMatrix();
369
 
370
   glFinish();
371
 
372
}
373
 
374
static int screen()
375
{
376
 
377
  extern DWORD flbaddr;
378
 
379
  /* graphic card Initialization */
380
  if (grx_init() < 1) {
1650 pj 381
     exit(1);
1624 giacomo 382
  }
383
 
384
  if (grx_open(640, 480, 16) < 0) {
385
     cprintf("GRX Err\n");
1650 pj 386
     exit(1);
1624 giacomo 387
  }
388
 
389
  video_buf = (unsigned char *)flbaddr;
390
 
391
  return 0;
392
 
393
}
394
 
395
void program_end(void *arg)
396
{
397
 
398
  OSMesaDestroyContext(ctx);
399
  free(rgb_565_buf);
400
 
401
  grx_close();
402
 
1650 pj 403
  exit(1);
1624 giacomo 404
 
405
}
406
 
407
void program_key_end(KEY_EVT *k)
408
{
409
 
1650 pj 410
  exit(1);
1624 giacomo 411
 
412
}
413
 
414
TASK refresh(void)
415
{
416
 
417
  while(1) {
418
 
419
    memcpy(rgb_565_buf, video_buf, RGB565MEM);
420
    task_endcycle();
421
 
422
  }
423
 
1650 pj 424
  exit(1);
1624 giacomo 425
 
426
}
427
 
428
 
429
TASK disegna(void)
430
{
431
 
432
  char text[100];
433
  TIME disegna_TIME, refresh_TIME;
434
 
435
  while(1) {
436
 
437
    jet_gettable(refresh_PID, &refresh_TIME, 1);
438
    jet_gettable(disegna_PID, &disegna_TIME, 1);
439
 
440
    spin += 2.0;
441
    yrot += 3.0;
442
 
443
    draw();
444
 
445
    sprintf(text,"Hard Task Refresh PER:%6d us EX:%6d us",(int)PERIOD_REFRESH,(int)refresh_TIME);
446
    grx_text(text,10,5,rgb16(0,0,255),0);    
447
    sprintf(text,"Hard Task Draw    PER:%6d us EX:%6d us",(int)PERIOD_DISEGNA,(int)disegna_TIME);
448
    grx_text(text,10,15,rgb16(0,0,255),0);
449
 
450
    task_endcycle();
451
 
452
  }
453
 
1650 pj 454
  exit(1);
1624 giacomo 455
 
456
}
457
 
458
int main (int argc, char *argv[])
459
{
460
 
461
    HARD_TASK_MODEL ht_refresh, ht_disegna;
462
 
463
    sys_atrunlevel(program_end,NULL, RUNLEVEL_BEFORE_EXIT);
464
 
465
    WCET_REFRESH =((long int) PERIOD_REFRESH * (0.2));
466
    WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.7));
467
 
468
    hard_task_default_model(ht_refresh);
469
    hard_task_def_wcet(ht_refresh,WCET_REFRESH);
470
    hard_task_def_mit(ht_refresh,PERIOD_REFRESH);
471
    hard_task_def_usemath(ht_refresh);
472
    hard_task_def_group(ht_refresh,1);
473
    hard_task_def_ctrl_jet(ht_refresh);
474
 
475
    refresh_PID = task_create("refresh", refresh, &ht_refresh, NULL);
476
    if (refresh_PID == -1) {
477
      exit(4);
478
    }
479
 
480
    hard_task_default_model(ht_disegna);
481
    hard_task_def_mit(ht_disegna,PERIOD_DISEGNA);
482
    hard_task_def_wcet(ht_disegna,WCET_DISEGNA);
483
    hard_task_def_group(ht_disegna,1);
484
    hard_task_def_ctrl_jet(ht_disegna);
485
    hard_task_def_usemath(ht_disegna);
486
    hard_task_def_stack(ht_disegna, 30000);
487
 
488
    disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL);
489
    if (disegna_PID == -1) {
490
      exit(4);
491
    }
492
 
493
    {
494
      KEY_EVT k;
495
      k.flag = ALTL_BIT;
496
      k.scan = KEY_C;
497
      k.ascii = 'c';
498
      keyb_hook(k,program_key_end);
499
    }
500
 
501
    rgb_565_buf = malloc(RGB565MEM);
502
 
503
    gl_init();
504
 
505
    if (screen()) {
506
        printk(KERN_INFO "Graphical initialization failed !!\n");
1650 pj 507
        exit(1);
1624 giacomo 508
    }
509
 
510
    group_activate(1);
511
 
512
    return 0;
513
 
514
}