Rev 1139 | Go to most recent revision | Details | 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> |
||
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 | #include <stdio.h> |
||
32 | |||
33 | #ifndef M_PI |
||
34 | #define M_PI 3.14159265 |
||
35 | #endif |
||
36 | |||
37 | #define WIDTH 640 |
||
38 | #define HEIGHT 480 |
||
39 | #define BYTES_PP 2 //BytesPerPixel |
||
40 | #define INITSTR G640x480x64K //SVGAlib standard mode definitions |
||
41 | #define CARD NV3 //Video driver (Supported SAVAGE, NV3, R128 from SVGALib) |
||
42 | |||
43 | #define DEG2RAD (3.14159/180.0) |
||
44 | |||
45 | static GLint ImgWidth, ImgHeight; |
||
46 | static GLenum ImgFormat; |
||
47 | static GLubyte *Image = NULL; |
||
48 | |||
49 | #define MAX_OBJECTS 2 |
||
50 | static GLint table_list; |
||
51 | static GLint objects_list[MAX_OBJECTS]; |
||
52 | |||
53 | static GLfloat xrot, yrot; |
||
54 | static GLfloat spin; |
||
55 | |||
56 | static GLboolean Anim = GL_TRUE; |
||
57 | |||
58 | OSMesaContext ctx; |
||
59 | |||
60 | unsigned char *rgb_565_buf = NULL; //RGB 16 bpp Buffer |
||
61 | unsigned char *video_buf = NULL; //Video Buffer |
||
62 | |||
63 | unsigned long int VMEMLONG = WIDTH * HEIGHT * BYTES_PP / 4; // Used by copy_videomem_16to16 |
||
64 | unsigned long int RGB565MEM = WIDTH * HEIGHT * BYTES_PP; // Total video mem |
||
65 | |||
66 | static GLint w = WIDTH, h = HEIGHT; |
||
67 | |||
68 | unsigned long int PERIOD_REFRESH = 80000; //fps = 20 |
||
69 | unsigned long int PERIOD_DISEGNA = 80000; |
||
70 | |||
71 | unsigned long int WCET_REFRESH, WCET_DISEGNA; |
||
72 | |||
73 | TASK refesh(void); |
||
74 | TASK disegna(void); |
||
75 | |||
76 | GLUquadricObj *q; |
||
77 | |||
78 | PID refresh_PID, disegna_PID; |
||
79 | |||
80 | static void gl_init( void ) |
||
81 | { |
||
82 | GLfloat yAspect = 2.5; |
||
83 | GLfloat xAspect = yAspect * (float) w / (float) h; |
||
84 | |||
85 | //Create the OSMesa Context |
||
86 | ctx = OSMesaCreateContext(OSMESA_RGB_565, NULL); |
||
87 | |||
88 | //Make Current Context |
||
89 | OSMesaMakeCurrent(ctx, rgb_565_buf, GL_UNSIGNED_SHORT_5_6_5, WIDTH, HEIGHT); |
||
90 | |||
91 | q = gluNewQuadric(); |
||
92 | gluQuadricDrawStyle( q, GLU_FILL ); |
||
93 | gluQuadricNormals( q, GLU_SMOOTH ); |
||
94 | |||
95 | ImgWidth = 100; |
||
96 | ImgHeight = 100; |
||
97 | ImgFormat = GL_RGB; |
||
98 | |||
99 | Image = malloc(ImgWidth * ImgHeight * 3); |
||
100 | |||
101 | /*Image = LoadRGBImage( TABLE_TEXTURE, &ImgWidth, &ImgHeight, &ImgFormat ); |
||
102 | if (!Image) { |
||
103 | printf("Couldn't read %s\n", TABLE_TEXTURE); |
||
104 | exit(0); |
||
105 | }*/ |
||
106 | |||
107 | gluBuild2DMipmaps(GL_TEXTURE_2D, 3, ImgWidth, ImgHeight, |
||
108 | ImgFormat, GL_UNSIGNED_BYTE, Image); |
||
109 | |||
110 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); |
||
111 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); |
||
112 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
||
113 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
||
114 | |||
115 | xrot = 30.0; |
||
116 | yrot = 50.0; |
||
117 | spin = 0.0; |
||
118 | |||
119 | glShadeModel( GL_FLAT ); |
||
120 | |||
121 | glEnable( GL_LIGHT0 ); |
||
122 | glEnable( GL_LIGHTING ); |
||
123 | |||
124 | glClearColor( 0.5, 0.5, 0.9, 0.0 ); |
||
125 | |||
126 | glEnable( GL_NORMALIZE ); |
||
127 | |||
128 | glViewport(0, 0, w, h); |
||
129 | glMatrixMode(GL_PROJECTION); |
||
130 | glLoadIdentity(); |
||
131 | glFrustum( -xAspect, xAspect, yAspect, -yAspect, 10.0, 30.0 ); |
||
132 | glMatrixMode(GL_MODELVIEW); |
||
133 | glLoadIdentity(); |
||
134 | |||
135 | } |
||
136 | |||
137 | static void draw_objects( GLfloat eyex, GLfloat eyey, GLfloat eyez ) |
||
138 | { |
||
139 | static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 }; |
||
140 | static GLfloat green[] = { 0.2, 1.0, 0.2, 1.0 }; |
||
141 | static GLfloat black[] = { 0.0, 0.0, 0.0, 0.0 }; |
||
142 | |||
143 | #ifndef USE_ZBUFFER |
||
144 | if (eyex<0.5) { |
||
145 | #endif |
||
146 | glPushMatrix(); |
||
147 | glTranslatef( 1.0, 1.5, 0.0 ); |
||
148 | glRotatef( spin, 1.0, 0.5, 0.0 ); |
||
149 | glRotatef( 0.5*spin, 0.0, 0.5, 1.0 ); |
||
150 | glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan ); |
||
151 | glMaterialfv( GL_FRONT, GL_EMISSION, black ); |
||
152 | gluCylinder( q, 0.5, 0.5, 1.0, 15, 1 ); |
||
153 | glPopMatrix(); |
||
154 | |||
155 | glPushMatrix(); |
||
156 | glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*spin) ), 0.0 ); |
||
157 | glRotatef( 0.5*spin, 0.0, 0.5, 1.0 ); |
||
158 | glRotatef( spin, 1.0, 0.5, 0.0 ); |
||
159 | glScalef( 0.5, 0.5, 0.5 ); |
||
160 | glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green ); |
||
161 | glMaterialfv( GL_FRONT, GL_EMISSION, black ); |
||
162 | gluCylinder( q, 1.5, 0.0, 2.5, 15, 1 ); |
||
163 | glPopMatrix(); |
||
164 | #ifndef USE_ZBUFFER |
||
165 | } |
||
166 | else { |
||
167 | glPushMatrix(); |
||
168 | glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*spin) ), 0.0 ); |
||
169 | glRotatef( 0.5*spin, 0.0, 0.5, 1.0 ); |
||
170 | glRotatef( spin, 1.0, 0.5, 0.0 ); |
||
171 | glScalef( 0.5, 0.5, 0.5 ); |
||
172 | glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green ); |
||
173 | glMaterialfv( GL_FRONT, GL_EMISSION, black ); |
||
174 | gluCylinder( q, 1.5, 0.0, 2.5, 15, 1 ); |
||
175 | glPopMatrix(); |
||
176 | |||
177 | glPushMatrix(); |
||
178 | glTranslatef( 1.0, 1.5, 0.0 ); |
||
179 | glRotatef( spin, 1.0, 0.5, 0.0 ); |
||
180 | glRotatef( 0.5*spin, 0.0, 0.5, 1.0 ); |
||
181 | glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan ); |
||
182 | glMaterialfv( GL_FRONT, GL_EMISSION, black ); |
||
183 | gluCylinder( q, 0.5, 0.5, 1.0, 15, 1 ); |
||
184 | glPopMatrix(); |
||
185 | } |
||
186 | #endif |
||
187 | } |
||
188 | |||
189 | static void draw_table( void ) |
||
190 | { |
||
191 | static GLfloat table_mat[] = { 1.0, 1.0, 1.0, 0.6 }; |
||
192 | static GLfloat gray[] = { 0.4, 0.4, 0.4, 1.0 }; |
||
193 | |||
194 | /* load table's texture */ |
||
195 | glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, table_mat ); |
||
196 | glMaterialfv( GL_FRONT, GL_DIFFUSE, table_mat ); |
||
197 | glMaterialfv( GL_FRONT, GL_AMBIENT, gray ); |
||
198 | |||
199 | /* draw textured square for the table */ |
||
200 | glPushMatrix(); |
||
201 | glScalef( 4.0, 4.0, 4.0 ); |
||
202 | glBegin( GL_POLYGON ); |
||
203 | glNormal3f( 0.0, 1.0, 0.0 ); |
||
204 | glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, 0.0, 1.0 ); |
||
205 | glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 0.0, 1.0 ); |
||
206 | glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 0.0, -1.0 ); |
||
207 | glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 0.0, -1.0 ); |
||
208 | glEnd(); |
||
209 | glPopMatrix(); |
||
210 | |||
211 | glDisable( GL_TEXTURE_2D ); |
||
212 | |||
213 | } |
||
214 | |||
215 | static void draw( void ) |
||
216 | { |
||
217 | static GLfloat light_pos[] = { 0.0, 20.0, 0.0, 1.0 }; |
||
218 | static GLfloat dist = 20.0; |
||
219 | static GLfloat eyex, eyey, eyez; |
||
220 | |||
221 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
||
222 | |||
223 | eyex = dist * cos(yrot*DEG2RAD) * cos(xrot*DEG2RAD); |
||
224 | eyez = dist * sin(yrot*DEG2RAD) * cos(xrot*DEG2RAD); |
||
225 | eyey = dist * sin(xrot*DEG2RAD); |
||
226 | |||
227 | /* view from top */ |
||
228 | glPushMatrix(); |
||
229 | gluLookAt( eyex, eyey, eyez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); |
||
230 | |||
231 | glLightfv( GL_LIGHT0, GL_POSITION, light_pos ); |
||
232 | |||
233 | /* draw table into stencil planes */ |
||
234 | glDisable( GL_DEPTH_TEST ); |
||
235 | glEnable( GL_STENCIL_TEST ); |
||
236 | glStencilFunc( GL_ALWAYS, 1, 0xffffffff ); |
||
237 | glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE ); |
||
238 | glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE ); |
||
239 | draw_table(); |
||
240 | glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE ); |
||
241 | |||
242 | glEnable( GL_DEPTH_TEST ); |
||
243 | |||
244 | /* render view from below (reflected viewport) */ |
||
245 | /* only draw where stencil==1 */ |
||
246 | if (eyey>0.0) { |
||
247 | glPushMatrix(); |
||
248 | |||
249 | glStencilFunc( GL_EQUAL, 1, 0xffffffff ); /* draw if ==1 */ |
||
250 | glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP ); |
||
251 | glScalef( 1.0, -1.0, 1.0 ); |
||
252 | |||
253 | /* Reposition light in reflected space. */ |
||
254 | glLightfv(GL_LIGHT0, GL_POSITION, light_pos); |
||
255 | |||
256 | draw_objects(eyex, eyey, eyez); |
||
257 | glPopMatrix(); |
||
258 | |||
259 | /* Restore light's original unreflected position. */ |
||
260 | glLightfv(GL_LIGHT0, GL_POSITION, light_pos); |
||
261 | } |
||
262 | |||
263 | glDisable( GL_STENCIL_TEST ); |
||
264 | |||
265 | glEnable( GL_BLEND ); |
||
266 | glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); |
||
267 | |||
268 | glEnable( GL_TEXTURE_2D ); |
||
269 | draw_table(); |
||
270 | glDisable( GL_TEXTURE_2D ); |
||
271 | glDisable( GL_BLEND ); |
||
272 | |||
273 | /* view from top */ |
||
274 | glPushMatrix(); |
||
275 | |||
276 | draw_objects(eyex, eyey, eyez); |
||
277 | |||
278 | glPopMatrix(); |
||
279 | |||
280 | glPopMatrix(); |
||
281 | |||
282 | glFinish(); |
||
283 | |||
284 | } |
||
285 | |||
286 | static int screen(int mode) |
||
287 | { |
||
288 | |||
289 | vga_modeinfo *minf; |
||
290 | |||
291 | vga_setmode(mode,CARD); |
||
292 | minf = vga_getmodeinfo(mode); |
||
293 | if(! (minf->flags & CAPABLE_LINEAR)){ |
||
294 | vga_setmode(TEXT,CARD); |
||
295 | printk(KERN_INFO "The mode %d is not capable of linear\n",mode); |
||
296 | return 1; |
||
297 | } |
||
298 | vga_setpage(0); |
||
299 | if(vga_setlinearaddressing() == -1) { |
||
300 | vga_setmode(TEXT,CARD); |
||
301 | printk(KERN_INFO "Could not set linear addressing for mode %d\n",mode); |
||
302 | return 1; |
||
303 | } |
||
304 | |||
305 | video_buf = vga_getgraphmem(); |
||
306 | rgb_565_buf = malloc(RGB565MEM); |
||
307 | |||
308 | grx_setbuffer(rgb_565_buf, WIDTH, HEIGHT); //Init of RGB16 version of grx functions |
||
309 | //created to work with Mesa buffer |
||
310 | return 0; |
||
311 | |||
312 | } |
||
313 | |||
314 | void program_end(void *arg) |
||
315 | { |
||
316 | |||
317 | OSMesaDestroyContext(ctx); |
||
318 | free(rgb_565_buf); |
||
319 | |||
320 | vga_setmode(TEXT,CARD); |
||
321 | |||
322 | sys_end(); |
||
323 | |||
324 | } |
||
325 | |||
326 | void program_key_end(KEY_EVT *k) |
||
327 | { |
||
328 | |||
329 | sys_end(); |
||
330 | |||
331 | } |
||
332 | |||
333 | TASK refresh(void) |
||
334 | { |
||
335 | |||
336 | while(1) { |
||
337 | |||
338 | copy_videomem_16to16(rgb_565_buf, video_buf, VMEMLONG); |
||
339 | task_endcycle(); |
||
340 | |||
341 | } |
||
342 | |||
343 | sys_end(); |
||
344 | |||
345 | } |
||
346 | |||
347 | |||
348 | TASK disegna(void) |
||
349 | { |
||
350 | |||
351 | char text[100]; |
||
352 | TIME disegna_TIME, refresh_TIME; |
||
353 | |||
354 | while(1) { |
||
355 | |||
356 | jet_gettable(refresh_PID, &refresh_TIME, 1); |
||
357 | jet_gettable(disegna_PID, &disegna_TIME, 1); |
||
358 | |||
359 | spin += 2.0; |
||
360 | yrot += 3.0; |
||
361 | |||
362 | draw(); |
||
363 | |||
364 | sprintf(text,"Hard Task Refresh PER:%6d us EX:%6d us",(int)PERIOD_REFRESH,(int)refresh_TIME); |
||
365 | grx_text(text,10,5,rgb16(0,0,255),0); |
||
366 | sprintf(text,"Hard Task Draw PER:%6d us EX:%6d us",(int)PERIOD_DISEGNA,(int)disegna_TIME); |
||
367 | grx_text(text,10,15,rgb16(0,0,255),0); |
||
368 | |||
369 | task_endcycle(); |
||
370 | |||
371 | } |
||
372 | |||
373 | sys_end(); |
||
374 | |||
375 | } |
||
376 | |||
377 | int main (int argc, char *argv[]) |
||
378 | { |
||
379 | |||
380 | HARD_TASK_MODEL ht_refresh, ht_disegna; |
||
381 | |||
382 | sys_atrunlevel(program_end,NULL, RUNLEVEL_BEFORE_EXIT); |
||
383 | |||
384 | clear(); |
||
385 | |||
386 | WCET_REFRESH =((long int) PERIOD_REFRESH * (0.22)); |
||
387 | WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.72)); |
||
388 | |||
389 | hard_task_default_model(ht_refresh); |
||
390 | hard_task_def_wcet(ht_refresh,WCET_REFRESH); |
||
391 | hard_task_def_mit(ht_refresh,PERIOD_REFRESH); |
||
392 | hard_task_def_usemath(ht_refresh); |
||
393 | hard_task_def_group(ht_refresh,1); |
||
394 | hard_task_def_ctrl_jet(ht_refresh); |
||
395 | |||
396 | refresh_PID = task_create("refresh", refresh, &ht_refresh, NULL); |
||
397 | if (refresh_PID == -1) { |
||
398 | sys_end(); |
||
399 | exit(4); |
||
400 | } |
||
401 | |||
402 | hard_task_default_model(ht_disegna); |
||
403 | hard_task_def_mit(ht_disegna,PERIOD_DISEGNA); |
||
404 | hard_task_def_wcet(ht_disegna,WCET_DISEGNA); |
||
405 | hard_task_def_group(ht_disegna,1); |
||
406 | hard_task_def_ctrl_jet(ht_disegna); |
||
407 | hard_task_def_usemath(ht_disegna); |
||
408 | |||
409 | disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL); |
||
410 | if (disegna_PID == -1) { |
||
411 | sys_end(); |
||
412 | exit(4); |
||
413 | } |
||
414 | |||
415 | { |
||
416 | KEY_EVT k; |
||
417 | k.flag = ALTL_BIT; |
||
418 | k.scan = KEY_C; |
||
419 | k.ascii = 'c'; |
||
420 | keyb_hook(k,program_key_end); |
||
421 | } |
||
422 | |||
423 | if (screen(INITSTR)) { |
||
424 | printk(KERN_INFO "Graphical initialization failed !!\n"); |
||
425 | sys_end(); |
||
426 | } |
||
427 | |||
428 | memset(rgb_565_buf, 0, RGB565MEM); |
||
429 | |||
430 | gl_init(); |
||
431 | |||
432 | group_activate(1); |
||
433 | |||
434 | return 0; |
||
435 | |||
436 | } |