Subversion Repositories shark

Rev

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