Subversion Repositories shark

Compare Revisions

Regard whitespace Rev 1138 → Rev 1139

/demos/trunk/mesaref/mesaref.c
21,6 → 21,7
 
#include <GL/osmesa.h>
#include <GL/glut.h>
#include <ports/png.h>
 
#include <math.h>
#include <stdlib.h>
42,8 → 43,8
 
#define DEG2RAD (3.14159/180.0)
 
static GLint ImgWidth, ImgHeight;
static GLenum ImgFormat;
static GLint ImgWidth = 0, ImgHeight = 0;
static GLenum ImgFormat = 0;
static GLubyte *Image = NULL;
 
#define MAX_OBJECTS 2
53,8 → 54,6
static GLfloat xrot, yrot;
static GLfloat spin;
 
static GLboolean Anim = GL_TRUE;
 
OSMesaContext ctx;
 
unsigned char *rgb_565_buf = NULL; //RGB 16 bpp Buffer
63,8 → 62,6
unsigned long int VMEMLONG = WIDTH * HEIGHT * BYTES_PP / 4; // Used by copy_videomem_16to16
unsigned long int RGB565MEM = WIDTH * HEIGHT * BYTES_PP; // Total video mem
 
static GLint w = WIDTH, h = HEIGHT;
 
unsigned long int PERIOD_REFRESH = 80000; //fps = 20
unsigned long int PERIOD_DISEGNA = 80000;
 
73,14 → 70,140
TASK refesh(void);
TASK disegna(void);
 
PID refresh_PID, disegna_PID;
 
void read_png_file(char *file_name, GLubyte **buffer, GLint *width, GLint *height, png_byte *color_type)
{
 
int y;
png_byte bit_depth;
 
png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep * row_pointers;
 
char header[8]; // 8 is the maximum size that can be checked
 
/* open file and test for it being a png */
FILE *fp = fopen(file_name, "rb");
if (!fp) {
cprintf("[read_png_file] File %s could not be opened for reading", file_name);
sys_end();
}
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
cprintf("[read_png_file] File %s is not recognized as a PNG file", file_name);
/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
cprintf("[read_png_file] png_create_read_struct failed");
 
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
cprintf("[read_png_file] png_create_info_struct failed");
 
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
 
png_read_info(png_ptr, info_ptr);
 
*width = info_ptr->width;
*height = info_ptr->height;
*color_type = info_ptr->color_type;
bit_depth = info_ptr->bit_depth;
 
number_of_passes = png_set_interlace_handling(png_ptr);
cprintf("Open PNG W: %d H: %d CT: %d BD: %d\n",*width,*height,*color_type,bit_depth);
png_read_update_info(png_ptr, info_ptr);
 
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * info_ptr->height);
for (y=0; y<info_ptr->height; y++)
row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes);
 
png_read_image(png_ptr, row_pointers);
 
if(info_ptr->color_type == PNG_COLOR_TYPE_RGB) {
*buffer = malloc(info_ptr->height * info_ptr->rowbytes);
for(y=0; y<info_ptr->height; y++)
memcpy(*buffer+y*info_ptr->rowbytes,row_pointers[y],info_ptr->rowbytes);
}
 
fclose(fp);
}
 
static void make_table( void )
{
static GLfloat table_mat[] = { 1.0, 1.0, 1.0, 0.6 };
static GLfloat gray[] = { 0.4, 0.4, 0.4, 1.0 };
 
table_list = glGenLists(1);
glNewList( table_list, GL_COMPILE );
 
/* load table's texture */
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, table_mat );
/* glMaterialfv( GL_FRONT, GL_EMISSION, gray );*/
glMaterialfv( GL_FRONT, GL_DIFFUSE, table_mat );
glMaterialfv( GL_FRONT, GL_AMBIENT, gray );
/* draw textured square for the table */
glPushMatrix();
glScalef( 4.0, 4.0, 4.0 );
glBegin( GL_POLYGON );
glNormal3f( 0.0, 1.0, 0.0 );
glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, 0.0, -1.0 );
glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 0.0, 1.0 );
glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 0.0, 1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 0.0, -1.0 );
glEnd();
glPopMatrix();
 
glDisable( GL_TEXTURE_2D );
 
glEndList();
}
 
 
static void make_objects( void )
{
GLUquadricObj *q;
 
PID refresh_PID, disegna_PID;
static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
static GLfloat green[] = { 0.2, 1.0, 0.2, 1.0 };
static GLfloat black[] = { 0.0, 0.0, 0.0, 0.0 };
 
q = gluNewQuadric();
gluQuadricDrawStyle( q, GLU_FILL );
gluQuadricNormals( q, GLU_SMOOTH );
 
objects_list[0] = glGenLists(1);
glNewList( objects_list[0], GL_COMPILE );
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan );
glMaterialfv( GL_FRONT, GL_EMISSION, black );
gluCylinder( q, 0.5, 0.5, 1.0, 15, 1 );
glEndList();
 
objects_list[1] = glGenLists(1);
glNewList( objects_list[1], GL_COMPILE );
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
glMaterialfv( GL_FRONT, GL_EMISSION, black );
gluCylinder( q, 1.5, 0.0, 2.5, 15, 1 );
glEndList();
}
 
static void gl_init( void )
{
png_byte img_color;
 
GLfloat yAspect = 2.5;
GLfloat xAspect = yAspect * (float) w / (float) h;
GLfloat xAspect = yAspect * (float) WIDTH / (float) HEIGHT;
 
//Create the OSMesa Context
ctx = OSMesaCreateContext(OSMESA_RGB_565, NULL);
88,22 → 211,17
//Make Current Context
OSMesaMakeCurrent(ctx, rgb_565_buf, GL_UNSIGNED_SHORT_5_6_5, WIDTH, HEIGHT);
 
q = gluNewQuadric();
gluQuadricDrawStyle( q, GLU_FILL );
gluQuadricNormals( q, GLU_SMOOTH );
 
ImgWidth = 100;
ImgHeight = 100;
read_png_file("test.png",&Image,&ImgWidth,&ImgHeight,&img_color);
if (img_color == PNG_COLOR_TYPE_RGB && Image != NULL)
ImgFormat = GL_RGB;
else {
cprintf("Texture Load Falied !!\n");
sys_end();
}
Image = malloc(ImgWidth * ImgHeight * 3);
make_table();
make_objects();
/*Image = LoadRGBImage( TABLE_TEXTURE, &ImgWidth, &ImgHeight, &ImgFormat );
if (!Image) {
printf("Couldn't read %s\n", TABLE_TEXTURE);
exit(0);
}*/
 
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, ImgWidth, ImgHeight,
ImgFormat, GL_UNSIGNED_BYTE, Image);
 
125,7 → 243,7
 
glEnable( GL_NORMALIZE );
 
glViewport(0, 0, w, h);
glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum( -xAspect, xAspect, yAspect, -yAspect, 10.0, 30.0 );
136,10 → 254,9
 
static void draw_objects( GLfloat eyex, GLfloat eyey, GLfloat eyez )
{
static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
static GLfloat green[] = { 0.2, 1.0, 0.2, 1.0 };
static GLfloat black[] = { 0.0, 0.0, 0.0, 0.0 };
 
(void) eyex;
(void) eyey;
(void) eyez;
#ifndef USE_ZBUFFER
if (eyex<0.5) {
#endif
147,9 → 264,7
glTranslatef( 1.0, 1.5, 0.0 );
glRotatef( spin, 1.0, 0.5, 0.0 );
glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan );
glMaterialfv( GL_FRONT, GL_EMISSION, black );
gluCylinder( q, 0.5, 0.5, 1.0, 15, 1 );
glCallList( objects_list[0] );
glPopMatrix();
glPushMatrix();
157,9 → 272,7
glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
glRotatef( spin, 1.0, 0.5, 0.0 );
glScalef( 0.5, 0.5, 0.5 );
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
glMaterialfv( GL_FRONT, GL_EMISSION, black );
gluCylinder( q, 1.5, 0.0, 2.5, 15, 1 );
glCallList( objects_list[1] );
glPopMatrix();
#ifndef USE_ZBUFFER
}
169,9 → 282,7
glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
glRotatef( spin, 1.0, 0.5, 0.0 );
glScalef( 0.5, 0.5, 0.5 );
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
glMaterialfv( GL_FRONT, GL_EMISSION, black );
gluCylinder( q, 1.5, 0.0, 2.5, 15, 1 );
glCallList( objects_list[1] );
glPopMatrix();
 
glPushMatrix();
178,48 → 289,27
glTranslatef( 1.0, 1.5, 0.0 );
glRotatef( spin, 1.0, 0.5, 0.0 );
glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan );
glMaterialfv( GL_FRONT, GL_EMISSION, black );
gluCylinder( q, 0.5, 0.5, 1.0, 15, 1 );
glCallList( objects_list[0] );
glPopMatrix();
}
#endif
 
}
 
static void draw_table( void )
{
static GLfloat table_mat[] = { 1.0, 1.0, 1.0, 0.6 };
static GLfloat gray[] = { 0.4, 0.4, 0.4, 1.0 };
 
/* load table's texture */
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, table_mat );
glMaterialfv( GL_FRONT, GL_DIFFUSE, table_mat );
glMaterialfv( GL_FRONT, GL_AMBIENT, gray );
/* draw textured square for the table */
glPushMatrix();
glScalef( 4.0, 4.0, 4.0 );
glBegin( GL_POLYGON );
glNormal3f( 0.0, 1.0, 0.0 );
glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, 0.0, 1.0 );
glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 0.0, 1.0 );
glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 0.0, -1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 0.0, -1.0 );
glEnd();
glPopMatrix();
 
glDisable( GL_TEXTURE_2D );
 
glCallList( table_list );
}
 
static void draw( void )
{
static GLfloat light_pos[] = { 0.0, 20.0, 0.0, 1.0 };
static GLfloat dist = 20.0;
static GLfloat eyex, eyey, eyez;
GLfloat dist = 20.0;
GLfloat eyex, eyey, eyez;
 
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
 
 
eyex = dist * cos(yrot*DEG2RAD) * cos(xrot*DEG2RAD);
eyez = dist * sin(yrot*DEG2RAD) * cos(xrot*DEG2RAD);
eyey = dist * sin(xrot*DEG2RAD);
381,11 → 471,9
 
sys_atrunlevel(program_end,NULL, RUNLEVEL_BEFORE_EXIT);
 
clear();
WCET_REFRESH =((long int) PERIOD_REFRESH * (0.20));
WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.70));
WCET_REFRESH =((long int) PERIOD_REFRESH * (0.22));
WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.72));
 
hard_task_default_model(ht_refresh);
hard_task_def_wcet(ht_refresh,WCET_REFRESH);
hard_task_def_mit(ht_refresh,PERIOD_REFRESH);
405,6 → 493,7
hard_task_def_group(ht_disegna,1);
hard_task_def_ctrl_jet(ht_disegna);
hard_task_def_usemath(ht_disegna);
hard_task_def_stack(ht_disegna, 60000);
 
disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL);
if (disegna_PID == -1) {