#include #include #include #include #include #include #include "deposition_particle.h" /********************************************************************* * Initialize the openGL viewing area as a two dimensional orthogonal * viewing window. ********************************************************************/ void initialize( void ) { //Set the background colour to white glClearColor( 1.0, 1.0, 1.0, 1.0 ); //Default drawing colour set to red glColor3f( 1.0, 0.0, 0.0 ); //Set up viewing area for a projection viewing area, (frustum) glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluOrtho2D( 0.0, SCREEN, 0.0, SCREEN ); glMatrixMode( GL_MODELVIEW ); } /******************************************************************* * OpenGL display function ******************************************************************/ void display( void ) { int /*base = BASE,*/ count = 0, count2 = 0, countX, collide; float originX = 0, originY = 0, particleX = 0, particleY; Particle_list heightField[NUM_STEPS]; float random, testVariable, height; int new_column; Particle *particle; Particle *top_particle; /* Create the openGL list for a box */ createBox(); glClear( GL_COLOR_BUFFER_BIT ); srand( time(NULL) ); /* Initialize the terrain structure */ height = (float)createTerrain( heightField ); //fprintf(stderr, "directly after createTerrain: %.2f\n", heightField[20]); //fprintf(stderr, "no. of steps: %d\n", NUM_STEPS); /* Display the height at each x increment */ for( count = 0; count < NUM_STEPS; count++ ) { //fprintf(stderr, "count = %d\n", count ); //fprintf(stderr, "x: %d, y: %.2f\n", count, heightField[count] ); //printf("still finished with creating terrain\n"); /* Have an arbitrary number of particles */ //printf("still finished with creating terrain\n"); /* Have an arbitrary number of particles */ for( count = 0; count < NUM_PARTICLES; count++ ) { fprintf(stderr, "creating new particle\n"); particle = (Particle*)malloc( sizeof(Particle)); createNewParticle( particle, heightField[0].number*RADIUS*2, height*2*RADIUS+2*RADIUS ); //while( particleY >= heightField[particleX+10] ) //fprintf(stderr, "stop at %.2f\n", (SCREEN-RADIUS*2) ); //fprintf(stderr, "particleX: %.2f\n", particleX ); //testVariable = particleX; //fprintf(stderr, "testvariable: %.2f\n", testVariable ); /*while( particle.position.xValue < (SCREEN-RADIUS*2) ) { fprintf(stderr, "blah"); break; } */ while( particle->position.xValue < (SCREEN-RADIUS*2) ) { //fprintf(stderr, "THIS IS A CHECK: position = %f\n", particle.position.xValue); glClear( GL_COLOR_BUFFER_BIT ); //printf( "Particle X now: %d, y: %d\n", particleX, particleY ); //fprintf(stderr, "display: height at 0: %d\n", heightField[0]); collide = drawParticle( particle, heightField, STANDARD_TIME_INCREMENT ); //printf("check2 - x: %d, y: %d\n", particleX, particleY ); //fprintf(stderr, "checking collide boolean: %d\n", collide ); if( collide == 1 ) { fprintf(stderr, "particle collided with mountain\n"); new_column = round(particle->position.xValue/(RADIUS*2)); fprintf(stderr, "Looking at column number %d\n", new_column ); //top_particle = find_particle_x( heightField[new_column].number, // heightField[new_column].first); //top_particle->next = &particle; //heightField[new_column].number++; fprintf(stderr, "particles in column before: %d\n",heightField[new_column].number); add_particle_to_column( particle, &heightField[new_column] ); fprintf(stderr, "particles in column after: %d\n",heightField[new_column].number); break; } /* Else need to recheck velocity, for sheltering effects of dune */ else { recheck_velocity( particle, heightField ); } //fprintf(stderr, "about to redraw terrain\n"); //fprintf(stderr, "at 0: %d\n", heightField[0] ); redrawTerrain( heightField ); //fprintf(stderr, "just redrawn terrain\n"); glutSwapBuffers(); //} //drawParticle( particleX-10, particleY ); //glutSwapBuffers(); //} //glutSwapBuffers(); //particle.position.xValue += RADIUS*2; }//closing while loop /* If the particle has blown offscreen, free the memory, and erase the particle */ if( particle->position.xValue >= (SCREEN-RADIUS*2) ) free( particle ); /*if( count % 10 == 0 ) { redrawTerrain( heightField ); glutSwapBuffers(); } */ }//closing for loop } // redrawTerrain( heightField ); glutSwapBuffers(); } /************************************************************************ * main() ***********************************************************************/ int main( int argc, char** argv ) { FILE* input; if( argc < 2 ) { NUM_STEPS = (int)SCREEN/(RADIUS*2); VELOCITY_X = RADIUS*2; fprintf(stderr, "No input file given, using defaults\n"); } else if( strcmp( argv[1], "help" )== 0 ) { printf("Input file requires initial velocity and acceleration values\n"); printf("for x, y and z coordinates, number of particles to be injected\n"); printf("into the system, length of the box side, the size of the\n"); printf("base of the initial terrain structure, eg:\n"); printf("10.0, 0.0, 0.0\n"); printf("0.0, -9.8, 0.0\n"); printf("1000, 10.0, 50\n"); exit(1); } else { input = fopen( argv[1], "r" ); if( !input ) fprintf(stderr, "input file did not open, using defaults\n"); else { fscanf( input, "%f, %f, %f\n", &VELOCITY_X, &VELOCITY_Y, &VELOCITY_Z); fscanf( input, "%f, %f, %f\n", &ACCELERATION_X, &ACCELERATION_Y, &ACCELERATION_Z ); fscanf( input, "%d, %f, %d", &NUM_PARTICLES, &RADIUS, &BASE ); fclose( input ); NUM_STEPS = (int)SCREEN/(RADIUS*2); } } glutInit( &argc, argv ); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); glutInitWindowSize( SCREEN, SCREEN ); glutInitWindowPosition( 0, 0); glutCreateWindow("Deposition simulation"); glutDisplayFunc( display ); initialize(); glutMainLoop(); }