Subversion Repositories shark

Rev

Rev 1154 | Rev 1156 | Go to most recent revision | Details | Compare with Previous | 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
1154 giacomo 39
#define CARD VESA //Video driver
1129 giacomo 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
 
1155 giacomo 62
unsigned long int PERIOD_REFRESH = 60000;
63
unsigned long int PERIOD_DISEGNA = 60000;
1129 giacomo 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
 
277
  grx_setbuffer(rgb_565_buf, WIDTH, HEIGHT);    //Init of RGB16 version of grx functions
278
                                                //created to work with Mesa buffer
279
  return 0;
280
 
281
}
282
 
283
void program_end(void *arg)
284
{
285
 
286
  OSMesaDestroyContext(ctx);
287
  free(rgb_565_buf);
288
 
289
  vga_setmode(TEXT,CARD);
290
 
291
  sys_end();
292
 
293
}
294
 
295
void program_key_end(KEY_EVT *k)
296
{
297
 
298
  sys_end();
299
 
300
}
301
 
302
TASK refresh(void)
303
{
304
 
305
  while(1) {
306
 
307
    copy_videomem_16to16(rgb_565_buf, video_buf, VMEMLONG);
308
    task_endcycle();
309
 
310
  }
311
 
312
  sys_end();
313
 
314
}
315
 
316
 
317
TASK disegna(void)
318
{
319
 
320
  char text[100];
321
  TIME disegna_TIME, refresh_TIME;
322
 
323
  while(1) {
324
 
325
    jet_gettable(refresh_PID, &refresh_TIME, 1);
326
    jet_gettable(disegna_PID, &disegna_TIME, 1);
327
 
328
    Angle += 2.0;
329
 
330
    draw();
331
 
332
    sprintf(text,"Hard Task Refresh PER:%6d us EX:%6d us",(int)PERIOD_REFRESH,(int)refresh_TIME);
333
    grx_text(text,10,5,rgb16(0,0,255),0);    
334
    sprintf(text,"Hard Task Draw    PER:%6d us EX:%6d us",(int)PERIOD_DISEGNA,(int)disegna_TIME);
335
    grx_text(text,10,15,rgb16(0,0,255),0);
336
 
337
    task_endcycle();
338
 
339
  }
340
 
341
  sys_end();
342
 
343
}
344
 
345
int main (int argc, char *argv[])
346
{
347
 
348
    HARD_TASK_MODEL ht_refresh, ht_disegna;
349
 
350
    sys_atrunlevel(program_end,NULL, RUNLEVEL_BEFORE_EXIT);
351
 
352
    clear();
353
 
354
    WCET_REFRESH =((long int) PERIOD_REFRESH * (0.45));
355
    WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.45));
356
 
357
    hard_task_default_model(ht_refresh);
358
    hard_task_def_wcet(ht_refresh,WCET_REFRESH);
359
    hard_task_def_mit(ht_refresh,PERIOD_REFRESH);
360
    hard_task_def_usemath(ht_refresh);
361
    hard_task_def_group(ht_refresh,1);
362
    hard_task_def_ctrl_jet(ht_refresh);
363
 
364
    refresh_PID = task_create("refresh", refresh, &ht_refresh, NULL);
365
    if (refresh_PID == -1) {
366
      sys_end();
367
      exit(4);
368
    }
369
 
370
    hard_task_default_model(ht_disegna);
371
    hard_task_def_mit(ht_disegna,PERIOD_DISEGNA);
372
    hard_task_def_wcet(ht_disegna,WCET_DISEGNA);
373
    hard_task_def_group(ht_disegna,1);
374
    hard_task_def_ctrl_jet(ht_disegna);
375
    hard_task_def_usemath(ht_disegna);
376
 
377
    disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL);
378
    if (disegna_PID == -1) {
379
      sys_end();
380
      exit(4);
381
    }
382
 
383
    {
384
      KEY_EVT k;
385
      k.flag = ALTL_BIT;
386
      k.scan = KEY_C;
387
      k.ascii = 'c';
388
      keyb_hook(k,program_key_end);
389
    }
1155 giacomo 390
 
391
    rgb_565_buf = malloc(RGB565MEM);
1129 giacomo 392
 
1155 giacomo 393
    gl_init();
394
 
1129 giacomo 395
    if (screen(INITSTR)) {
396
        printk(KERN_INFO "Graphical initialization failed !!\n");
397
        sys_end();
398
    }
399
 
400
    memset(rgb_565_buf, 0, RGB565MEM);
401
 
402
    group_activate(1);
403
 
404
    return 0;
405
 
406
}