You are on page 1of 11

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <glut.h> #include <math.

h> #define BOUNDS 1 #define WATERSIZE 200 #define DAMP 20 float water[2][WATERSIZE][WATERSIZE];

int spin_x, spin_y, spin_z; /* x-y rotation and zoom */ int h, w; /* height, width of window */ int old_x, old_y, move_z; int depth = 3; int i = 0; int t = 0, f = 1; void calcwater() { int x, y; float n; for(y = 1; y < WATERSIZE-1; y++) { for(x = 1; x < WATERSIZE-1; x++) { n = ( water[t][x-1][y] + water[t][x+1][y] + water[t][x][y-1] + water[t][x][y+1] ) /2; n -= water[f][x][y]; n = n - (n / DAMP); water[f][x][y] = n; } } y = 0; for(x = 1; x < WATERSIZE-1; x++) { n = ( water[t][x-1][y] + water[t][x+1][y] + water[t][x][y+1] ) /2; n -= water[f][x][y]; n = n - (n / DAMP); water[f][x][y] = n; }

x = 0; for(y = 1; y < WATERSIZE-1; y++) { n = ( water[t][x+1][y] + water[t][x][y-1] + water[t][x][y+1]

) /2; n -= water[f][x][y]; n = n - (n / DAMP); water[f][x][y] = n; } x = WATERSIZE-1; for(y = 1; y < WATERSIZE-1; y++) { n = ( water[t][x-1][y] + water[t][x][y-1] + water[t][x][y+1] ) /2; n -= water[f][x][y]; n = n - (n / DAMP); water[f][x][y] = n; } y = WATERSIZE-1; for(x = 1; x < WATERSIZE-1; x++) { n = ( water[t][x-1][y] + water[t][x+1][y] + water[t][x][y-1] ) /2; n -= water[f][x][y]; n = n - (n / DAMP); water[f][x][y] = n; } } void init(); void reshape(int width, int height) { w = width; h = height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 2000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity (); } void display(void) { int i, j, tmp; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix(); glTranslatef(0, 0, spin_z-220); glRotatef(spin_x, 0, 1, 0); glRotatef(spin_y-60, 1, 0, 0);

calcwater(); glBegin(GL_POINTS); for(i = 0; i < WATERSIZE; i++) { for(j = 0; j < WATERSIZE; j++) { glColor3f(0,0,1); glVertex3f(j-WATERSIZE/2, i-WATERSIZE/2, water[t][j][i]); } } glEnd(); tmp = t; t = f; f = tmp;

glPopMatrix();

glutSwapBuffers(); } int num = 0; int delay = 70; void idle(void) { if(!(++num %delay)) { water[f][rand()%WATERSIZE][rand()%WATERSIZE] = -rand()%200; delay = rand()%100 + 50; } glutPostRedisplay(); } void mouse(int button, int state, int x, int y) { switch(button) { case 0: old_x = x - spin_x; old_y = y - spin_y; break; case 2: old_y = y - spin_z; move_z = (move_z ? 0 : 1); }

glutPostRedisplay(); }

void motion(int x, int y) { if(!move_z) { spin_x = x - old_x; spin_y = y - old_y; } else { spin_z = y - old_y; } glutPostRedisplay(); }

void keyboard(unsigned char key, int x, int y) { static int old_x = 50; static int old_y = 50; static int old_width = 512; static int old_height = 512; switch (key) { case 'x': exit(0); break; case 'm': glutPositionWindow(old_x, old_y); glutReshapeWindow(old_width, old_height); break; case 'f': if (glutGet(GLUT_WINDOW_WIDTH) < glutGet(GLUT_SCREEN_WIDTH)) { old_x = glutGet(GLUT_WINDOW_X); old_y = glutGet(GLUT_WINDOW_Y); old_width = glutGet(GLUT_WINDOW_WIDTH); old_height = glutGet(GLUT_WINDOW_HEIGHT); glutFullScreen(); } break; case ' ': water[f][WATERSIZE/2][WATERSIZE/2] = -1000; break; } }

void init(void) { int i, j; w = glutGet(GLUT_WINDOW_WIDTH); h = glutGet(GLUT_WINDOW_HEIGHT);

// glEnable(GL_LIGHTING); // glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); for( i = 0; i < WATERSIZE; i++) for( j = 0; j < WATERSIZE; j++) { water[0][j][i] = 0; water[1][j][i] = 0; } } int main(int argc, char** argv) {

// srand(time(NULL)); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowPosition(50, 50); glutInitWindowSize(512, 512); glutInit(&argc, argv); glutCreateWindow("Simulating Water"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMouseFunc(mouse); glutMotionFunc(motion); glEnable (GL_DEPTH_TEST); if(argc == 2) { if (strcmp(argv[1], "-h") == 0) { fprintf(stderr, "%s [depth]\n", argv[0]); exit(0); } sscanf(argv[1], "%d", &depth); } printf("Water Simulation \n"); init(); glutIdleFunc(idle); glutMainLoop(); return 0; }

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <GL/glut.h> /* Some <math.h> files do not define M_PI... */ #ifndef M_PI #define M_PI 3.14159265 #endif #define TWO_PI (2*M_PI) typedef struct lightRec { float amb[4]; float diff[4]; float spec[4]; float pos[4]; float spotDir[3]; float spotExp; float spotCutoff; float atten[3]; float trans[3]; float rot[3]; float swing[3]; float arc[3]; float arcIncr[3]; } Light; static int useSAME_AMB_SPEC = 1; /* *INDENT-OFF* */ static float modelAmb[4] = {0.2, 0.2, 0.2, 1.0}; static float matAmb[4] = {0.2, 0.2, 0.2, 1.0}; static float matDiff[4] = {0.8, 0.8, 0.8, 1.0}; static float matSpec[4] = {0.4, 0.4, 0.4, 1.0}; static float matEmission[4] = {0.0, 0.0, 0.0, 1.0}; /* *INDENT-ON* */ #define NUM_LIGHTS 3 static Light spots[] = { { {0.2, 0.0, 0.0, 1.0}, /* ambient */ {0.8, 0.0, 0.0, 1.0}, /* diffuse */ {0.4, 0.0, 0.0, 1.0}, /* specular */ {0.0, 0.0, 0.0, 1.0}, /* position */ {0.0, -1.0, 0.0}, /* direction */ {20.0}, {60.0}, /* exponent, cutoff */

{1.0, 0.0, 0.0}, /* attenuation */ {0.0, 1.25, 0.0}, /* translation */ {0.0, 0.0, 0.0}, /* rotation */ {20.0, 0.0, 40.0}, /* swing */ {0.0, 0.0, 0.0}, /* arc */ {TWO_PI / 70.0, 0.0, TWO_PI / 140.0} /* arc increment */ }, { {0.0, 0.2, 0.0, 1.0}, /* ambient */ {0.0, 0.8, 0.0, 1.0}, /* diffuse */ {0.0, 0.4, 0.0, 1.0}, /* specular */ {0.0, 0.0, 0.0, 1.0}, /* position */ {0.0, -1.0, 0.0}, /* direction */ {20.0}, {60.0}, /* exponent, cutoff */ {1.0, 0.0, 0.0}, /* attenuation */ {0.0, 1.25, 0.0}, /* translation */ {0.0, 0.0, 0.0}, /* rotation */ {20.0, 0.0, 40.0}, /* swing */ {0.0, 0.0, 0.0}, /* arc */ {TWO_PI / 120.0, 0.0, TWO_PI / 60.0} /* arc increment */ }, { {0.0, 0.0, 0.2, 1.0}, /* ambient */ {0.0, 0.0, 0.8, 1.0}, /* diffuse */ {0.0, 0.0, 0.4, 1.0}, /* specular */ {0.0, 0.0, 0.0, 1.0}, /* position */ {0.0, -1.0, 0.0}, /* direction */ {20.0}, {60.0}, /* exponent, cutoff */ {1.0, 0.0, 0.0}, /* attenuation */ {0.0, 1.25, 0.0}, /* translation */ {0.0, 0.0, 0.0}, /* rotation */ {20.0, 0.0, 40.0}, /* swing */ {0.0, 0.0, 0.0}, /* arc */ {TWO_PI / 50.0, 0.0, TWO_PI / 100.0} /* arc increment */ } }; static void usage(char *name) { printf("\n"); printf("usage: %s [options]\n", name); printf("\n"); printf(" Options:\n"); printf(" -geometry Specify size and position WxH+X+Y\n"); printf(" -lm Toggle lighting(SPECULAR and AMBIENT are/not same\n"); printf("\n"); #ifndef EXIT_FAILURE /* should be defined by ANSI C <stdlib.h> */ #define EXIT_FAILURE 1 #endif exit(EXIT_FAILURE);

} static void initLights(void) { int k; for (k = 0; k < NUM_LIGHTS; ++k) { int lt = GL_LIGHT0 + k; Light *light = &spots[k]; glEnable(lt); glLightfv(lt, GL_AMBIENT, light->amb); glLightfv(lt, GL_DIFFUSE, light->diff); if (useSAME_AMB_SPEC) glLightfv(lt, GL_SPECULAR, light->amb); else glLightfv(lt, GL_SPECULAR, light->spec); glLightf(lt, GL_SPOT_EXPONENT, light->spotExp); glLightf(lt, GL_SPOT_CUTOFF, light->spotCutoff); glLightf(lt, GL_CONSTANT_ATTENUATION, light->atten[0]); glLightf(lt, GL_LINEAR_ATTENUATION, light->atten[1]); glLightf(lt, GL_QUADRATIC_ATTENUATION, light->atten[2]); } } static void aimLights(void) { int k; for (k = 0; k < NUM_LIGHTS; ++k) { Light *light = &spots[k]; light->rot[0] = light->swing[0] * sin(light->arc[0]); light->arc[0] += light->arcIncr[0]; if (light->arc[0] > TWO_PI) light->arc[0] -= TWO_PI; light->rot[1] = light->swing[1] * sin(light->arc[1]); light->arc[1] += light->arcIncr[1]; if (light->arc[1] > TWO_PI) light->arc[1] -= TWO_PI; light->rot[2] = light->swing[2] * sin(light->arc[2]); light->arc[2] += light->arcIncr[2]; if (light->arc[2] > TWO_PI) light->arc[2] -= TWO_PI; } }

static void setLights(void) { int k; for (k = 0; k < NUM_LIGHTS; ++k) { int lt = GL_LIGHT0 + k; Light *light = &spots[k]; glPushMatrix(); glTranslatef(light->trans[0], light->trans[1], light->trans[2]); glRotatef(light->rot[0], 1, 0, 0); glRotatef(light->rot[1], 0, 1, 0); glRotatef(light->rot[2], 0, 0, 1); glLightfv(lt, GL_POSITION, light->pos); glLightfv(lt, GL_SPOT_DIRECTION, light->spotDir); glPopMatrix(); } } static void drawLights(void) { int k; glDisable(GL_LIGHTING); for (k = 0; k < NUM_LIGHTS; ++k) { Light *light = &spots[k]; glColor4fv(light->diff); glPushMatrix(); glTranslatef(light->trans[0], light->trans[1], light->trans[2]); glRotatef(light->rot[0], 1, 0, 0); glRotatef(light->rot[1], 0, 1, 0); glRotatef(light->rot[2], 0, 0, 1); glBegin(GL_LINES); glVertex3f(light->pos[0], light->pos[1], light->pos[2]); glVertex3f(light->spotDir[0], light->spotDir[1], light->spotDir[2]); glEnd(); glPopMatrix(); } glEnable(GL_LIGHTING); } static void drawPlane(int w, int h) { int i, j; float dw = 1.0 / w; float dh = 1.0 / h; glNormal3f(0.0, 0.0, 1.0);

for (j = 0; j < h; ++j) { glBegin(GL_TRIANGLE_STRIP); for (i = 0; i <= w; ++i) { glVertex2f(dw * i, dh * (j + 1)); glVertex2f(dw * i, dh * j); } glEnd(); } } int spin = 0; void display(void) { glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glRotatef(spin, 0, 1, 0); aimLights(); setLights(); glPushMatrix(); glRotatef(-90.0, 1, 0, 0); glScalef(1.9, 1.9, 1.0); glTranslatef(-0.5, -0.5, 0.0); drawPlane(16, 16); glPopMatrix(); drawLights(); glPopMatrix(); glutSwapBuffers(); } void animate(void) { spin += 0.5; if (spin > 360.0) spin -= 360.0; glutPostRedisplay(); } void visibility(int state) { if (state == GLUT_VISIBLE) { glutIdleFunc(animate); } else { glutIdleFunc(NULL); }

} int main(int argc, char **argv) { int i; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); /* process commmand line args */ for (i = 1; i < argc; ++i) { if (!strcmp("-lm", argv[i])) { useSAME_AMB_SPEC = !useSAME_AMB_SPEC; } else { usage(argv[0]); } } glutCreateWindow("GLUT spotlight swing"); glutDisplayFunc(display); glutVisibilityFunc(visibility); glMatrixMode(GL_PROJECTION); glFrustum(-1, 1, -1, 1, 2, 6); glMatrixMode(GL_MODELVIEW); glTranslatef(0.0, 0.0, -3.0); glRotatef(45.0, 1, 0, 0); glEnable(GL_LIGHTING); glEnable(GL_NORMALIZE); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, modelAmb); glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); glMaterialfv(GL_FRONT, GL_AMBIENT, matAmb); glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiff); glMaterialfv(GL_FRONT, GL_SPECULAR, matSpec); glMaterialfv(GL_FRONT, GL_EMISSION, matEmission); glMaterialf(GL_FRONT, GL_SHININESS, 10.0); initLights(); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }

You might also like