Subversion Repositories shark

Rev

Rev 1154 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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