Download from gintro examples directory
/* File: oglexSimSync.c Uses double buffering of data */ #include#include #include #include #define NX 50 #define NY 50 #define MX (10.0/NX) #define MY (10.0/NX) #define BX -5.0 #define BY 0.0 struct s_data { float z[NX][NY]; float n[NX][NY][3]; }; void simInit(struct s_data *data); void simUpdate(struct s_data *data); void oglexInit(void); void oglexDraw(struct s_data *data); main(int argc, char **argv) { struct s_data *data[2]; int buf; /* Initialize the CAVE */ CAVEConfigure(&argc,argv,NULL); /* Allocate shared memory */ data[0] = CAVEMalloc(sizeof(struct s_data)); data[1] = CAVEMalloc(sizeof(struct s_data)); /* Spawn the CAVE processes */ CAVEInit(); /* Give the library a pointer to the GL initialization function */ CAVEInitApplication(oglexInit, NULL); /* Give the library a pointer to the drawing function */ buf = 0; simInit(data[buf]); CAVEDisplay(oglexDraw, 1, data[buf]); /* Wait for the escape key to be hit */ while (!CAVEgetbutton(CAVE_ESCKEY)) { buf = 1 - buf; simUpdate(data[buf]); CAVEDisplay(oglexDraw, 1, data[buf]); } /* Clean up & exit */ CAVEExit(); } void simInit(struct s_data *data) { int ix, iy; for (ix=0; ix z[ix][iy] = 0.0; data->n[ix][iy][0] = 0.0; data->n[ix][iy][1] = 0.0; data->n[ix][iy][2] = 1.0; } } void simUpdate(struct s_data *data) { static int nframes = 0; int ix, iy; double x, y, h, nx, ny, nz, l; h = ((double)nframes)/10.0; for (ix=0; ix z[ix][iy] = sin(h)*sin(x)*sin(y); nx = -sin(h)*cos(x)*sin(y); ny = -sin(h)*sin(x)*cos(y); nz = 1.0; l = sqrt(nx*nx+ny*ny+nz*nz); data->n[ix][iy][0] = nx / l; data->n[ix][iy][1] = ny / l; data->n[ix][iy][2] = nz / l; } nframes++; } void oglexInit(void) { glFrontFace(GL_CCW); glEnable(GL_CULL_FACE); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHTING); } void oglexDraw(struct s_data *data) { float plight0[] = { 0.0, 0.0, 1.0, 0.0}; float plight1[] = { 1.0, 0.0, 0.0, 0.0}; float blue[4] = { 0.0, 0.0, 1.0, 1.0 }; float cyan[4] = { 0.0, 1.0, 1.0, 1.0 }; float gray[4] = { 0.5, 0.5, 0.5, 1.0 }; float x0, x1, y; int ix, iy; glClearColor(0., 0., 0., 0.); glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); /* Initialize lighting */ glLightfv(GL_LIGHT0, GL_POSITION, plight0); glLightfv(GL_LIGHT1, GL_POSITION, plight1); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1); glDisable(GL_CULL_FACE); /* Initialize materials (front and back surface colors) */ glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue); glMaterialfv(GL_FRONT, GL_SPECULAR, gray); glMaterialf(GL_FRONT, GL_SHININESS, 10.0); glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, cyan); glMaterialfv(GL_BACK, GL_SPECULAR, gray); glMaterialf(GL_BACK, GL_SHININESS, 10.0); /* Draw the surface */ x0 = BX; for (ix=1; ix n[ix-1][iy][0], data->n[ix-1][iy][1], data->n[ix-1][iy][2]); glVertex3f(x0, y, data->z[ix-1][iy]); glNormal3f(data->n[ix][iy][0], data->n[ix][iy][1], data->n[ix][iy][2]); glVertex3f(x1, y, data->z[ix][iy]); } glEnd(); x0 = x1; } glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0); glEnable(GL_CULL_FACE); glPopMatrix(); }