Subversion Repositories shark

Rev

Rev 1321 | Rev 1550 | 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 <GL/osmesa.h>
20
#include <GL/glut.h>
21
 
22
#include <math.h>
23
#include <stdlib.h>
24
#include <assert.h>
25
#include <kernel/log.h>
26
#include <kernel/kern.h>
27
 
1464 giacomo 28
#include <drivers/shark_fb26.h>
29
#include <drivers/shark_keyb26.h>
30
 
1129 giacomo 31
#ifndef M_PI
32
#define M_PI 3.14159265
33
#endif
34
 
35
#define WIDTH 640
1321 giacomo 36
#define HEIGHT 430
1129 giacomo 37
#define BYTES_PP 2 //BytesPerPixel
38
 
39
OSMesaContext ctx;
40
 
41
static GLuint TexObj[2];
42
static GLfloat Angle = 0.0f;
43
static GLboolean UseObj = GL_FALSE;
44
 
1464 giacomo 45
extern void *video_memory;
46
 
1129 giacomo 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 RGB565MEM = WIDTH * HEIGHT * BYTES_PP; // Total video mem
60
 
1321 giacomo 61
unsigned long int PERIOD_REFRESH = 30000;
62
unsigned long int PERIOD_DISEGNA = 30000;
1129 giacomo 63
 
64
unsigned long int WCET_REFRESH, WCET_DISEGNA;
65
 
66
TASK refesh(void);
67
TASK disegna(void);
68
 
69
PID refresh_PID, disegna_PID;
70
 
71
static void draw( void )
72
{
73
   glDepthFunc(GL_EQUAL);
74
   /*   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
75
   glClear( GL_COLOR_BUFFER_BIT );
76
 
77
   glColor3f( 1.0, 1.0, 1.0 );
78
 
79
   /* draw first polygon */
80
   glPushMatrix();
81
   glTranslatef( -1.0, 0.0, 0.0 );
82
   glRotatef( Angle, 0.0, 0.0, 1.0 );
83
   if (UseObj) {
84
#ifdef TEXTURE_OBJECT
85
      glBindTexture( GL_TEXTURE_2D, TexObj[0] );
86
#endif
87
   }
88
   else {
89
      glCallList( TexObj[0] );
90
   }
91
   glBegin( GL_POLYGON );
92
   glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
93
   glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
94
   glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
95
   glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
96
   glEnd();
97
   glPopMatrix();
98
 
99
   /* draw second polygon */
100
   glPushMatrix();
101
   glTranslatef( 1.0, 0.0, 0.0 );
102
   glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
103
   if (UseObj) {
104
#ifdef TEXTURE_OBJECT
105
      glBindTexture( GL_TEXTURE_2D, TexObj[1] );
106
#endif
107
   }
108
   else {
109
      glCallList( TexObj[1] );
110
   }
111
   glBegin( GL_POLYGON );
112
   glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
113
   glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
114
   glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
115
   glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
116
   glEnd();
117
   glPopMatrix();
118
 
119
}
120
 
121
static void gl_init()
122
{
123
 
124
   static int twidth=8, theight=8;
125
   static GLubyte tex1[] = {
126
     0, 0, 0, 1, 1, 1, 0, 0,
1321 giacomo 127
     0, 0, 1, 0, 0, 0, 0, 0,
128
     0, 0, 1, 0, 0, 0, 0, 0,
129
     0, 0, 1, 0, 0, 0, 0, 0,
130
     0, 0, 1, 0, 0, 0, 0, 0,
131
     0, 0, 1, 0, 0, 0, 0, 0,
132
     0, 0, 1, 0, 0, 0, 0, 0,
133
     0, 0, 0, 1, 1, 1, 0, 0 };
1129 giacomo 134
 
135
   static GLubyte tex2[] = {
1321 giacomo 136
     0, 0, 0, 2, 2, 2, 0, 0,
137
     0, 0, 2, 0, 0, 0, 0, 0,
138
     0, 0, 2, 0, 0, 0, 0, 0,
139
     0, 0, 0, 2, 0, 0, 0, 0,
140
     0, 0, 0, 0, 2, 0, 0, 0,
1129 giacomo 141
     0, 0, 0, 0, 0, 2, 0, 0,
1321 giacomo 142
     0, 0, 0, 0, 0, 2, 0, 0,
143
     0, 0, 2, 2, 2, 0, 0, 0 };
1129 giacomo 144
 
145
   GLubyte tex[64][3];
146
   GLint i, j;
147
 
148
    //Create the OSMesa Context
149
   ctx = OSMesaCreateContext(OSMESA_RGB_565, NULL);
150
 
151
   //Make Current Context
152
   OSMesaMakeCurrent(ctx, rgb_565_buf, GL_UNSIGNED_SHORT_5_6_5, WIDTH, HEIGHT);
153
 
154
   UseObj = GL_TRUE;
155
 
156
   glDisable( GL_DITHER );
157
 
158
   /* Setup texturing */
159
   glEnable( GL_TEXTURE_2D );
160
   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
161
   glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
162
 
163
   /* generate texture object IDs */
164
   if (UseObj) {
165
#ifdef TEXTURE_OBJECT
166
      glGenTextures( 2, TexObj );
167
#endif
168
   }
169
   else {
170
      TexObj[0] = glGenLists(2);
171
      TexObj[1] = TexObj[0]+1;
172
   }
173
 
174
   /* setup first texture object */
175
   if (UseObj) {
176
#ifdef TEXTURE_OBJECT
177
      glBindTexture( GL_TEXTURE_2D, TexObj[0] );
178
      assert(glIsTexture(TexObj[0]));
179
#endif
180
   }
181
   else {
182
      glNewList( TexObj[0], GL_COMPILE );
183
   }
184
   /* red on white */
185
   for (i=0;i<theight;i++) {
186
      for (j=0;j<twidth;j++) {
187
         int p = i*twidth+j;
188
         if (tex1[(theight-i-1)*twidth+j]) {
189
            tex[p][0] = 255;   tex[p][1] = 0;     tex[p][2] = 0;
190
         }
191
         else {
192
            tex[p][0] = 255;   tex[p][1] = 255;   tex[p][2] = 255;
193
         }
194
      }
195
   }
196
 
197
   glTexImage2D( GL_TEXTURE_2D, 0, 3, twidth, theight, 0,
198
                 GL_RGB, GL_UNSIGNED_BYTE, tex );
199
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
200
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
201
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
202
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
203
   if (!UseObj) {
204
      glEndList();
205
   }
206
   /* end of texture object */
207
 
208
   /* setup second texture object */
209
   if (UseObj) {
210
#ifdef TEXTURE_OBJECT
211
      glBindTexture( GL_TEXTURE_2D, TexObj[1] );
212
      assert(glIsTexture(TexObj[1]));
213
#endif
214
      assert(!glIsTexture(TexObj[1] + 999));
215
   }
216
   else {
217
      glNewList( TexObj[1], GL_COMPILE );
218
   }
219
   /* green on blue */
220
   for (i=0;i<theight;i++) {
221
      for (j=0;j<twidth;j++) {
222
         int p = i*twidth+j;
223
         if (tex2[(theight-i-1)*twidth+j]) {
224
            tex[p][0] = 0;     tex[p][1] = 255;   tex[p][2] = 0;
225
         }
226
         else {
227
            tex[p][0] = 0;     tex[p][1] = 0;     tex[p][2] = 255;
228
         }
229
      }
230
   }
231
   glTexImage2D( GL_TEXTURE_2D, 0, 3, twidth, theight, 0,
232
                 GL_RGB, GL_UNSIGNED_BYTE, tex );
233
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
234
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
235
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
236
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
237
   if (!UseObj) {
238
      glEndList();
239
   }
240
   /* end texture object */
241
 
242
   glViewport(0, 0, (GLint)WIDTH, (GLint)HEIGHT);
243
   glMatrixMode(GL_PROJECTION);
244
   glLoadIdentity();
245
   /*   glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
246
   glFrustum( -2.0, 2.0, 2.0, -2.0, 6.0, 20.0 );
247
   glMatrixMode(GL_MODELVIEW);
248
   glLoadIdentity();
249
   glTranslatef( 0.0, 0.0, -8.0 );
250
 
251
}
252
 
253
void program_end(void *arg)
254
{
255
 
256
  OSMesaDestroyContext(ctx);
257
  free(rgb_565_buf);
258
 
259
  sys_end();
260
 
261
}
262
 
263
void program_key_end(KEY_EVT *k)
264
{
265
 
266
  sys_end();
267
 
268
}
269
 
270
TASK refresh(void)
271
{
272
 
273
  while(1) {
274
 
1321 giacomo 275
    memcpy((video_buf+40*WIDTH*2), rgb_565_buf, RGB565MEM);
1129 giacomo 276
    task_endcycle();
277
 
278
  }
279
 
280
  sys_end();
281
 
282
}
283
 
284
 
285
TASK disegna(void)
286
{
287
 
288
  char text[100];
289
  TIME disegna_TIME, refresh_TIME;
290
 
291
  while(1) {
292
 
293
    jet_gettable(refresh_PID, &refresh_TIME, 1);
294
    jet_gettable(disegna_PID, &disegna_TIME, 1);
295
 
296
    Angle += 2.0;
297
 
298
    draw();
299
 
300
    sprintf(text,"Hard Task Refresh PER:%6d us EX:%6d us",(int)PERIOD_REFRESH,(int)refresh_TIME);
301
    grx_text(text,10,5,rgb16(0,0,255),0);    
302
    sprintf(text,"Hard Task Draw    PER:%6d us EX:%6d us",(int)PERIOD_DISEGNA,(int)disegna_TIME);
303
    grx_text(text,10,15,rgb16(0,0,255),0);
304
 
305
    task_endcycle();
306
 
307
  }
308
 
309
  sys_end();
310
 
311
}
312
 
313
int main (int argc, char *argv[])
314
{
315
 
316
    HARD_TASK_MODEL ht_refresh, ht_disegna;
317
 
318
    clear();
319
 
320
    WCET_REFRESH =((long int) PERIOD_REFRESH * (0.45));
321
    WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.45));
322
 
323
    hard_task_default_model(ht_refresh);
324
    hard_task_def_wcet(ht_refresh,WCET_REFRESH);
325
    hard_task_def_mit(ht_refresh,PERIOD_REFRESH);
326
    hard_task_def_usemath(ht_refresh);
327
    hard_task_def_group(ht_refresh,1);
328
    hard_task_def_ctrl_jet(ht_refresh);
329
 
330
    refresh_PID = task_create("refresh", refresh, &ht_refresh, NULL);
331
    if (refresh_PID == -1) {
332
      sys_end();
333
      exit(4);
334
    }
335
 
336
    hard_task_default_model(ht_disegna);
337
    hard_task_def_mit(ht_disegna,PERIOD_DISEGNA);
338
    hard_task_def_wcet(ht_disegna,WCET_DISEGNA);
339
    hard_task_def_group(ht_disegna,1);
340
    hard_task_def_ctrl_jet(ht_disegna);
341
    hard_task_def_usemath(ht_disegna);
1156 giacomo 342
    hard_task_def_stack(ht_disegna,30000);
343
 
1129 giacomo 344
    disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL);
345
    if (disegna_PID == -1) {
346
      sys_end();
347
      exit(4);
348
    }
349
 
350
    {
351
      KEY_EVT k;
352
      k.flag = ALTL_BIT;
353
      k.scan = KEY_C;
354
      k.ascii = 'c';
1464 giacomo 355
      k.status = KEY_PRESSED;
356
      keyb_hook(k,program_key_end,FALSE);
1129 giacomo 357
    }
1155 giacomo 358
 
359
    rgb_565_buf = malloc(RGB565MEM);
1129 giacomo 360
 
1155 giacomo 361
    gl_init();
362
 
1464 giacomo 363
    video_buf = (unsigned char *)video_memory;
364
    //video_buf = (unsigned char *)malloc(640*480*2);
1129 giacomo 365
 
366
    memset(rgb_565_buf, 0, RGB565MEM);
367
 
368
    group_activate(1);
369
 
370
    return 0;
371
 
372
}