Subversion Repositories shark

Rev

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

Rev Author Line No. Line
1129 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
#include <assert.h>
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
39
#define CARD NV3 //Video driver (Supported SAVAGE, NV3, R128 from SVGALib)
40
 
41
OSMesaContext ctx;
42
 
43
static GLuint TexObj[2];
44
static GLfloat Angle = 0.0f;
45
static GLboolean UseObj = GL_FALSE;
46
 
47
#if defined(GL_VERSION_1_1) || defined(GL_VERSION_1_2)
48
#  define TEXTURE_OBJECT 1
49
#elif defined(GL_EXT_texture_object)
50
#  define TEXTURE_OBJECT 1
51
#  define glBindTexture(A,B)     glBindTextureEXT(A,B)
52
#  define glGenTextures(A,B)     glGenTexturesEXT(A,B)
53
#  define glDeleteTextures(A,B)  glDeleteTexturesEXT(A,B)
54
#endif
55
 
56
unsigned char *rgb_565_buf = NULL; //RGB 16 bpp Buffer
57
unsigned char *video_buf = NULL; //Video Buffer
58
 
59
unsigned long int VMEMLONG = WIDTH * HEIGHT * BYTES_PP / 4; // Used by copy_videomem_16to16
60
unsigned long int RGB565MEM = WIDTH * HEIGHT * BYTES_PP; // Total video mem
61
 
62
unsigned long int PERIOD_REFRESH = 40000; //fps = 25
63
unsigned long int PERIOD_DISEGNA = 40000;
64
 
65
unsigned long int WCET_REFRESH, WCET_DISEGNA;
66
 
67
TASK refesh(void);
68
TASK disegna(void);
69
 
70
PID refresh_PID, disegna_PID;
71
 
72
static void draw( void )
73
{
74
   glDepthFunc(GL_EQUAL);
75
   /*   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
76
   glClear( GL_COLOR_BUFFER_BIT );
77
 
78
   glColor3f( 1.0, 1.0, 1.0 );
79
 
80
   /* draw first polygon */
81
   glPushMatrix();
82
   glTranslatef( -1.0, 0.0, 0.0 );
83
   glRotatef( Angle, 0.0, 0.0, 1.0 );
84
   if (UseObj) {
85
#ifdef TEXTURE_OBJECT
86
      glBindTexture( GL_TEXTURE_2D, TexObj[0] );
87
#endif
88
   }
89
   else {
90
      glCallList( TexObj[0] );
91
   }
92
   glBegin( GL_POLYGON );
93
   glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
94
   glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
95
   glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
96
   glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
97
   glEnd();
98
   glPopMatrix();
99
 
100
   /* draw second polygon */
101
   glPushMatrix();
102
   glTranslatef( 1.0, 0.0, 0.0 );
103
   glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
104
   if (UseObj) {
105
#ifdef TEXTURE_OBJECT
106
      glBindTexture( GL_TEXTURE_2D, TexObj[1] );
107
#endif
108
   }
109
   else {
110
      glCallList( TexObj[1] );
111
   }
112
   glBegin( GL_POLYGON );
113
   glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
114
   glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
115
   glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
116
   glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
117
   glEnd();
118
   glPopMatrix();
119
 
120
}
121
 
122
 
123
static void gl_init()
124
{
125
 
126
   static GLfloat h = (GLfloat) HEIGHT / (GLfloat) WIDTH;
127
   static int twidth=8, theight=8;
128
   static GLubyte tex1[] = {
129
     0, 0, 0, 0, 0, 0, 0, 0,
130
     0, 0, 0, 0, 1, 0, 0, 0,
131
     0, 0, 0, 1, 1, 0, 0, 0,
132
     0, 0, 0, 0, 1, 0, 0, 0,
133
     0, 0, 0, 0, 1, 0, 0, 0,
134
     0, 0, 0, 0, 1, 0, 0, 0,
135
     0, 0, 0, 1, 1, 1, 0, 0,
136
     0, 0, 0, 0, 0, 0, 0, 0 };
137
 
138
   static GLubyte tex2[] = {
139
     0, 0, 0, 0, 0, 0, 0, 0,
140
     0, 0, 0, 2, 2, 0, 0, 0,
141
     0, 0, 2, 0, 0, 2, 0, 0,
142
     0, 0, 0, 0, 0, 2, 0, 0,
143
     0, 0, 0, 0, 2, 0, 0, 0,
144
     0, 0, 0, 2, 0, 0, 0, 0,
145
     0, 0, 2, 2, 2, 2, 0, 0,
146
     0, 0, 0, 0, 0, 0, 0, 0 };
147
 
148
   GLubyte tex[64][3];
149
   GLint i, j;
150
 
151
    //Create the OSMesa Context
152
   ctx = OSMesaCreateContext(OSMESA_RGB_565, NULL);
153
 
154
   //Make Current Context
155
   OSMesaMakeCurrent(ctx, rgb_565_buf, GL_UNSIGNED_SHORT_5_6_5, WIDTH, HEIGHT);
156
 
157
   UseObj = GL_TRUE;
158
 
159
   glDisable( GL_DITHER );
160
 
161
   /* Setup texturing */
162
   glEnable( GL_TEXTURE_2D );
163
   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
164
   glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
165
 
166
   /* generate texture object IDs */
167
   if (UseObj) {
168
#ifdef TEXTURE_OBJECT
169
      glGenTextures( 2, TexObj );
170
#endif
171
   }
172
   else {
173
      TexObj[0] = glGenLists(2);
174
      TexObj[1] = TexObj[0]+1;
175
   }
176
 
177
   /* setup first texture object */
178
   if (UseObj) {
179
#ifdef TEXTURE_OBJECT
180
      glBindTexture( GL_TEXTURE_2D, TexObj[0] );
181
      assert(glIsTexture(TexObj[0]));
182
#endif
183
   }
184
   else {
185
      glNewList( TexObj[0], GL_COMPILE );
186
   }
187
   /* red on white */
188
   for (i=0;i<theight;i++) {
189
      for (j=0;j<twidth;j++) {
190
         int p = i*twidth+j;
191
         if (tex1[(theight-i-1)*twidth+j]) {
192
            tex[p][0] = 255;   tex[p][1] = 0;     tex[p][2] = 0;
193
         }
194
         else {
195
            tex[p][0] = 255;   tex[p][1] = 255;   tex[p][2] = 255;
196
         }
197
      }
198
   }
199
 
200
   glTexImage2D( GL_TEXTURE_2D, 0, 3, twidth, theight, 0,
201
                 GL_RGB, GL_UNSIGNED_BYTE, tex );
202
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
203
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
204
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
205
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
206
   if (!UseObj) {
207
      glEndList();
208
   }
209
   /* end of texture object */
210
 
211
   /* setup second texture object */
212
   if (UseObj) {
213
#ifdef TEXTURE_OBJECT
214
      glBindTexture( GL_TEXTURE_2D, TexObj[1] );
215
      assert(glIsTexture(TexObj[1]));
216
#endif
217
      assert(!glIsTexture(TexObj[1] + 999));
218
   }
219
   else {
220
      glNewList( TexObj[1], GL_COMPILE );
221
   }
222
   /* green on blue */
223
   for (i=0;i<theight;i++) {
224
      for (j=0;j<twidth;j++) {
225
         int p = i*twidth+j;
226
         if (tex2[(theight-i-1)*twidth+j]) {
227
            tex[p][0] = 0;     tex[p][1] = 255;   tex[p][2] = 0;
228
         }
229
         else {
230
            tex[p][0] = 0;     tex[p][1] = 0;     tex[p][2] = 255;
231
         }
232
      }
233
   }
234
   glTexImage2D( GL_TEXTURE_2D, 0, 3, twidth, theight, 0,
235
                 GL_RGB, GL_UNSIGNED_BYTE, tex );
236
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
237
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
238
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
239
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
240
   if (!UseObj) {
241
      glEndList();
242
   }
243
   /* end texture object */
244
 
245
   glViewport(0, 0, (GLint)WIDTH, (GLint)HEIGHT);
246
   glMatrixMode(GL_PROJECTION);
247
   glLoadIdentity();
248
   /*   glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
249
   glFrustum( -2.0, 2.0, 2.0, -2.0, 6.0, 20.0 );
250
   glMatrixMode(GL_MODELVIEW);
251
   glLoadIdentity();
252
   glTranslatef( 0.0, 0.0, -8.0 );
253
 
254
}
255
 
256
static int screen(int mode)
257
{
258
 
259
  vga_modeinfo *minf;
260
 
261
  vga_setmode(mode,CARD);
262
  minf = vga_getmodeinfo(mode);
263
  if(! (minf->flags & CAPABLE_LINEAR)){
264
    vga_setmode(TEXT,CARD);
265
    printk(KERN_INFO "The mode %d is not capable of linear\n",mode);
266
    return 1;
267
  }
268
  vga_setpage(0);
269
  if(vga_setlinearaddressing() == -1) {
270
    vga_setmode(TEXT,CARD);
271
    printk(KERN_INFO "Could not set linear addressing for mode %d\n",mode);
272
    return 1;
273
  }
274
 
275
  video_buf = vga_getgraphmem();
276
  rgb_565_buf = malloc(RGB565MEM);
277
 
278
  grx_setbuffer(rgb_565_buf, WIDTH, HEIGHT);    //Init of RGB16 version of grx functions
279
                                                //created to work with Mesa buffer
280
  return 0;
281
 
282
}
283
 
284
void program_end(void *arg)
285
{
286
 
287
  OSMesaDestroyContext(ctx);
288
  free(rgb_565_buf);
289
 
290
  vga_setmode(TEXT,CARD);
291
 
292
  sys_end();
293
 
294
}
295
 
296
void program_key_end(KEY_EVT *k)
297
{
298
 
299
  sys_end();
300
 
301
}
302
 
303
TASK refresh(void)
304
{
305
 
306
  while(1) {
307
 
308
    copy_videomem_16to16(rgb_565_buf, video_buf, VMEMLONG);
309
    task_endcycle();
310
 
311
  }
312
 
313
  sys_end();
314
 
315
}
316
 
317
 
318
TASK disegna(void)
319
{
320
 
321
  char text[100];
322
  TIME disegna_TIME, refresh_TIME;
323
 
324
  while(1) {
325
 
326
    jet_gettable(refresh_PID, &refresh_TIME, 1);
327
    jet_gettable(disegna_PID, &disegna_TIME, 1);
328
 
329
    Angle += 2.0;
330
 
331
    draw();
332
 
333
    sprintf(text,"Hard Task Refresh PER:%6d us EX:%6d us",(int)PERIOD_REFRESH,(int)refresh_TIME);
334
    grx_text(text,10,5,rgb16(0,0,255),0);    
335
    sprintf(text,"Hard Task Draw    PER:%6d us EX:%6d us",(int)PERIOD_DISEGNA,(int)disegna_TIME);
336
    grx_text(text,10,15,rgb16(0,0,255),0);
337
 
338
    task_endcycle();
339
 
340
  }
341
 
342
  sys_end();
343
 
344
}
345
 
346
int main (int argc, char *argv[])
347
{
348
 
349
    HARD_TASK_MODEL ht_refresh, ht_disegna;
350
 
351
    sys_atrunlevel(program_end,NULL, RUNLEVEL_BEFORE_EXIT);
352
 
353
    clear();
354
 
355
    WCET_REFRESH =((long int) PERIOD_REFRESH * (0.45));
356
    WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.45));
357
 
358
    hard_task_default_model(ht_refresh);
359
    hard_task_def_wcet(ht_refresh,WCET_REFRESH);
360
    hard_task_def_mit(ht_refresh,PERIOD_REFRESH);
361
    hard_task_def_usemath(ht_refresh);
362
    hard_task_def_group(ht_refresh,1);
363
    hard_task_def_ctrl_jet(ht_refresh);
364
 
365
    refresh_PID = task_create("refresh", refresh, &ht_refresh, NULL);
366
    if (refresh_PID == -1) {
367
      sys_end();
368
      exit(4);
369
    }
370
 
371
    hard_task_default_model(ht_disegna);
372
    hard_task_def_mit(ht_disegna,PERIOD_DISEGNA);
373
    hard_task_def_wcet(ht_disegna,WCET_DISEGNA);
374
    hard_task_def_group(ht_disegna,1);
375
    hard_task_def_ctrl_jet(ht_disegna);
376
    hard_task_def_usemath(ht_disegna);
377
 
378
    disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL);
379
    if (disegna_PID == -1) {
380
      sys_end();
381
      exit(4);
382
    }
383
 
384
    {
385
      KEY_EVT k;
386
      k.flag = ALTL_BIT;
387
      k.scan = KEY_C;
388
      k.ascii = 'c';
389
      keyb_hook(k,program_key_end);
390
    }
391
 
392
    if (screen(INITSTR)) {
393
        printk(KERN_INFO "Graphical initialization failed !!\n");
394
        sys_end();
395
    }
396
 
397
    memset(rgb_565_buf, 0, RGB565MEM);
398
 
399
    gl_init();
400
 
401
    group_activate(1);
402
 
403
    return 0;
404
 
405
}