#include "NBody.hpp" extern int numBodies; // No. of particles; extern void* me; // Pointing to NBody class; bool rotate = false; bool translateW = false; bool translateZ = false; //-------------------------------- // PROTOTIPE FUNCTIONS void GLInit(); void idle(); void reShape(int w,int h); void displayfunc(); void keyboardFunc(unsigned char key, int mouseX, int mouseY); /////////////////////////////////// startOpenGLSimulation Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // This function is called to display the simulation in // OpenGL. If "display" variable is not TRUE then the // simulation in OpenGL will not take place. // /////////////////////////////////// startOpenGLSimulation Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ void startOpenGLSimulation(int argc, char * argv[]) { char * windowName = (char*)malloc(1024); sprintf (windowName, "NBody simulation : %d particles", numBodies); if(displayOpenGL) { // Run in graphical window if requested glutInit(&argc, argv); glutInitWindowPosition(100,10); glutInitWindowSize(600,600); glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); glutCreateWindow(windowName); GLInit(); glutDisplayFunc(displayfunc); glutReshapeFunc(reShape); glutIdleFunc(idle); glutKeyboardFunc(keyboardFunc); free(windowName); try { glutMainLoop(); } catch( char * str ) { std::cout << "OpenGL exited." << std::endl; } }else{ free(windowName); } } /////////////////////////////////// GLInit Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // Initialize OpenGL related stuff. // /////////////////////////////////// GLInit Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ void GLInit() { glClearColor(0.0 ,0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); } /////////////////////////////////// idle Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // Glut Idle function. // /////////////////////////////////// idle Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ void idle() { // If it's time to take a snapshot: if (takeSnapshots == TAKE_SNAPSHOTS){ if (((NBody*)me)->curr_time_step > ((NBody*)me)->curr_snap_time){ ((NBody*)me)->curr_snap_time += ((NBody*)me)->dt_snap; ((NBody*)me)->put_snapshot(); } } // If it's time to take a diagnostication: if (takeDiagnostics == TAKE_DIAGNOSTICS){ if (((NBody*)me)->curr_time_step > ((NBody*)me)->curr_diag_time){ ((NBody*)me)->curr_diag_time += ((NBody*)me)->dt_diag; ((NBody*)me)->write_diagnostics(); //std::cout << "Current time: " << ((NBody*)me)->curr_time_step << std::endl; } // If it's the first step in the simulation compute // the initial total energy of the system: if (((NBody*)me)->taken_steps == 0){ ((NBody*)me)->write_diagnostics(); } } if (((NBody*)me)->curr_time_step > ((NBody*)me)->curr_diag_time){ ((NBody*)me)->curr_diag_time += ((NBody*)me)->dt_diag; std::cout << "Current time: " << ((NBody*)me)->curr_time_step << std::endl; } // If it is not the end of the simulation: if (((NBody*)me)->curr_time_step < ((NBody*)me)->end_time){ // Move one step foreward (a step = delT): ((NBody*)me)->curr_time_step += ((NBody*)me)->delT; // Increase the number of steps by now: ((NBody*)me)->taken_steps++; //if (((NBody*)me)->taken_steps == 1800) // std::cout << "some test beep here \a\n"; //std::cout << }else{ std::cout << std::endl; std::cout << "Simulation ended..." << std::endl; std::cout << std::endl; time ( &rawtime ); timeinfo = localtime ( &rawtime ); std::cout << "End time and date: " << asctime (timeinfo) << std::endl; std::cout << "============================" << std::endl; exit(1); } glutPostRedisplay(); } /////////////////////////////////// reShape Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // Glut reshape function. // @param w width of OpenGL window // @param h height of OpenGL window // /////////////////////////////////// reShape Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ void reShape(int w,int h) { glViewport(0,0,w,h); glViewport(0,0,w,h); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluPerspective(45.0f,w/h,1.0f,1000.0f); gluLookAt (0.0, 0.0, -2.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0); } /////////////////////////////////// displayfunc Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // OpenGL display function. // /////////////////////////////////// displayfunc Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ void displayfunc() { glClearColor(0.0 ,0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT); glPointSize(1.0); glBlendFunc(GL_SRC_ALPHA, GL_ONE); glEnable(GL_BLEND); glDepthMask(GL_FALSE); glColor3f(1.0f,0.6f,0.0f); //------------------------ // Run the kernel on the choosen device: if (((NBody*)me)->curr_time_step < ((NBody*)me)->end_time){ //Calling kernel for calculatig subsequent positions if (integrator == LEAPFROG){ ((NBody*)me)->runCLKernelLeapfrog(); }else{ ((NBody*)me)->runCLKernels(); } } //------------------------ // After running the kernel we have the new updated positions: if (rotate){ glRotatef(25, 0, 1, 0); rotate = false; } if (translateW){ glTranslatef(0, 0, 1); translateW = false; } if (translateZ){ glTranslatef(0, 0, -1); translateZ = false; } glBegin(GL_POINTS); for(int i=0; i < numBodies; ++i) { //divided by 300 just for scaling glVertex3d( ((NBody*)me)->pos[i*4+ 0]/300, ((NBody*)me)->pos[i*4+1]/300, ((NBody*)me)->pos[i*4+2]/300); } glEnd(); glFlush(); glutSwapBuffers(); } /////////////////////////////////// keyboardFunc Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // OpenGL keyboard function. // /////////////////////////////////// keyboardFunc Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ void keyboardFunc(unsigned char key, int mouseX, int mouseY) { switch(key) { /* If the user hits escape or Q, then exit */ /* ESCAPE_KEY = 27 */ case 27: case 'r': rotate = true; glutPostRedisplay(); break; case 'w': translateW = true; glutPostRedisplay(); break; case 'z': translateZ = true; glutPostRedisplay(); break; case 'q': case 'Q': { if(((NBody*)me)->cleanup() != 0) // SDK_SUCCESS == 0; exit(1); else exit(0); } default: break; } }