[34] | 1 | #include "NBody.hpp" |
---|
| 2 | |
---|
| 3 | extern int numBodies; // No. of particles; |
---|
| 4 | extern void* me; // Pointing to NBody class; |
---|
| 5 | |
---|
| 6 | bool rotate = false; |
---|
| 7 | bool translateW = false; |
---|
| 8 | bool translateZ = false; |
---|
| 9 | |
---|
| 10 | //-------------------------------- |
---|
| 11 | // PROTOTIPE FUNCTIONS |
---|
| 12 | |
---|
| 13 | void GLInit(); |
---|
| 14 | void idle(); |
---|
| 15 | void reShape(int w,int h); |
---|
| 16 | void displayfunc(); |
---|
| 17 | void keyboardFunc(unsigned char key, int mouseX, int mouseY); |
---|
| 18 | |
---|
| 19 | /////////////////////////////////// startOpenGLSimulation Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 20 | // |
---|
| 21 | // This function is called to display the simulation in |
---|
| 22 | // OpenGL. If "display" variable is not TRUE then the |
---|
| 23 | // simulation in OpenGL will not take place. |
---|
| 24 | // |
---|
| 25 | /////////////////////////////////// startOpenGLSimulation Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 26 | |
---|
| 27 | void startOpenGLSimulation(int argc, char * argv[]) |
---|
| 28 | { |
---|
| 29 | char * windowName = (char*)malloc(1024); |
---|
| 30 | |
---|
| 31 | sprintf (windowName, "NBody simulation : %d particles", numBodies); |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | if(displayOpenGL) |
---|
| 35 | { |
---|
| 36 | // Run in graphical window if requested |
---|
| 37 | glutInit(&argc, argv); |
---|
| 38 | glutInitWindowPosition(100,10); |
---|
| 39 | glutInitWindowSize(600,600); |
---|
| 40 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
---|
| 41 | glutCreateWindow(windowName); |
---|
| 42 | GLInit(); |
---|
| 43 | glutDisplayFunc(displayfunc); |
---|
| 44 | glutReshapeFunc(reShape); |
---|
| 45 | glutIdleFunc(idle); |
---|
| 46 | glutKeyboardFunc(keyboardFunc); |
---|
| 47 | |
---|
| 48 | free(windowName); |
---|
| 49 | |
---|
| 50 | try { |
---|
| 51 | glutMainLoop(); |
---|
| 52 | } |
---|
| 53 | catch( char * str ) { |
---|
| 54 | std::cout << "OpenGL exited." << std::endl; |
---|
| 55 | } |
---|
| 56 | }else{ |
---|
| 57 | free(windowName); |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /////////////////////////////////// GLInit Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 62 | // |
---|
| 63 | // Initialize OpenGL related stuff. |
---|
| 64 | // |
---|
| 65 | /////////////////////////////////// GLInit Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 66 | |
---|
| 67 | void GLInit() |
---|
| 68 | { |
---|
| 69 | glClearColor(0.0 ,0.0, 0.0, 0.0); |
---|
| 70 | glClear(GL_COLOR_BUFFER_BIT); |
---|
| 71 | glClear(GL_DEPTH_BUFFER_BIT); |
---|
| 72 | glMatrixMode(GL_PROJECTION); |
---|
| 73 | glLoadIdentity(); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /////////////////////////////////// idle Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 77 | // |
---|
| 78 | // Glut Idle function. |
---|
| 79 | // |
---|
| 80 | /////////////////////////////////// idle Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 81 | |
---|
| 82 | void idle() |
---|
| 83 | { |
---|
| 84 | // If it's time to take a snapshot: |
---|
| 85 | if (takeSnapshots == TAKE_SNAPSHOTS){ |
---|
| 86 | if (((NBody*)me)->curr_time_step > ((NBody*)me)->curr_snap_time){ |
---|
| 87 | ((NBody*)me)->curr_snap_time += ((NBody*)me)->dt_snap; |
---|
| 88 | ((NBody*)me)->put_snapshot(); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | // If it's time to take a diagnostication: |
---|
| 93 | if (takeDiagnostics == TAKE_DIAGNOSTICS){ |
---|
| 94 | if (((NBody*)me)->curr_time_step > ((NBody*)me)->curr_diag_time){ |
---|
| 95 | ((NBody*)me)->curr_diag_time += ((NBody*)me)->dt_diag; |
---|
| 96 | ((NBody*)me)->write_diagnostics(); |
---|
| 97 | //std::cout << "Current time: " << ((NBody*)me)->curr_time_step << std::endl; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | // If it's the first step in the simulation compute |
---|
| 101 | // the initial total energy of the system: |
---|
| 102 | if (((NBody*)me)->taken_steps == 0){ |
---|
| 103 | ((NBody*)me)->write_diagnostics(); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if (((NBody*)me)->curr_time_step > ((NBody*)me)->curr_diag_time){ |
---|
| 108 | ((NBody*)me)->curr_diag_time += ((NBody*)me)->dt_diag; |
---|
| 109 | std::cout << "Current time: " << ((NBody*)me)->curr_time_step << std::endl; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | // If it is not the end of the simulation: |
---|
| 113 | if (((NBody*)me)->curr_time_step < ((NBody*)me)->end_time){ |
---|
| 114 | |
---|
| 115 | // Move one step foreward (a step = delT): |
---|
| 116 | ((NBody*)me)->curr_time_step += ((NBody*)me)->delT; |
---|
| 117 | |
---|
| 118 | // Increase the number of steps by now: |
---|
| 119 | ((NBody*)me)->taken_steps++; |
---|
| 120 | |
---|
| 121 | //if (((NBody*)me)->taken_steps == 1800) |
---|
| 122 | // std::cout << "some test beep here \a\n"; |
---|
| 123 | |
---|
| 124 | //std::cout << |
---|
| 125 | |
---|
| 126 | }else{ |
---|
| 127 | std::cout << std::endl; |
---|
| 128 | std::cout << "Simulation ended..." << std::endl; |
---|
| 129 | std::cout << std::endl; |
---|
| 130 | time ( &rawtime ); |
---|
| 131 | timeinfo = localtime ( &rawtime ); |
---|
| 132 | std::cout << "End time and date: " << asctime (timeinfo) << std::endl; |
---|
| 133 | std::cout << "============================" << std::endl; |
---|
| 134 | |
---|
| 135 | exit(1); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | glutPostRedisplay(); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /////////////////////////////////// reShape Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 142 | // |
---|
| 143 | // Glut reshape function. |
---|
| 144 | // @param w width of OpenGL window |
---|
| 145 | // @param h height of OpenGL window |
---|
| 146 | // |
---|
| 147 | /////////////////////////////////// reShape Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 148 | |
---|
| 149 | void reShape(int w,int h) |
---|
| 150 | { |
---|
| 151 | glViewport(0,0,w,h); |
---|
| 152 | |
---|
| 153 | glViewport(0,0,w,h); |
---|
| 154 | glMatrixMode(GL_MODELVIEW); |
---|
| 155 | glLoadIdentity(); |
---|
| 156 | gluPerspective(45.0f,w/h,1.0f,1000.0f); |
---|
| 157 | gluLookAt (0.0, 0.0, -2.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /////////////////////////////////// displayfunc Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 161 | // |
---|
| 162 | // OpenGL display function. |
---|
| 163 | // |
---|
| 164 | /////////////////////////////////// displayfunc Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 165 | |
---|
| 166 | void displayfunc() |
---|
| 167 | { |
---|
| 168 | glClearColor(0.0 ,0.0, 0.0, 0.0); |
---|
| 169 | glClear(GL_COLOR_BUFFER_BIT); |
---|
| 170 | glClear(GL_DEPTH_BUFFER_BIT); |
---|
| 171 | |
---|
| 172 | glPointSize(1.0); |
---|
| 173 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 174 | glEnable(GL_BLEND); |
---|
| 175 | glDepthMask(GL_FALSE); |
---|
| 176 | |
---|
| 177 | glColor3f(1.0f,0.6f,0.0f); |
---|
| 178 | |
---|
| 179 | //------------------------ |
---|
| 180 | // Run the kernel on the choosen device: |
---|
| 181 | if (((NBody*)me)->curr_time_step < ((NBody*)me)->end_time){ |
---|
| 182 | //Calling kernel for calculatig subsequent positions |
---|
| 183 | if (integrator == LEAPFROG){ |
---|
| 184 | ((NBody*)me)->runCLKernelLeapfrog(); |
---|
| 185 | }else{ |
---|
| 186 | ((NBody*)me)->runCLKernels(); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | //------------------------ |
---|
| 191 | // After running the kernel we have the new updated positions: |
---|
| 192 | |
---|
| 193 | if (rotate){ |
---|
| 194 | glRotatef(25, 0, 1, 0); |
---|
| 195 | rotate = false; |
---|
| 196 | } |
---|
| 197 | if (translateW){ |
---|
| 198 | glTranslatef(0, 0, 1); |
---|
| 199 | translateW = false; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | if (translateZ){ |
---|
| 203 | glTranslatef(0, 0, -1); |
---|
| 204 | translateZ = false; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | glBegin(GL_POINTS); |
---|
| 208 | for(int i=0; i < numBodies; ++i) |
---|
| 209 | { |
---|
| 210 | //divided by 300 just for scaling |
---|
| 211 | glVertex3d( ((NBody*)me)->pos[i*4+ 0]/300, |
---|
| 212 | ((NBody*)me)->pos[i*4+1]/300, |
---|
| 213 | ((NBody*)me)->pos[i*4+2]/300); |
---|
| 214 | } |
---|
| 215 | glEnd(); |
---|
| 216 | |
---|
| 217 | glFlush(); |
---|
| 218 | glutSwapBuffers(); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | /////////////////////////////////// keyboardFunc Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 222 | // |
---|
| 223 | // OpenGL keyboard function. |
---|
| 224 | // |
---|
| 225 | /////////////////////////////////// keyboardFunc Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 226 | |
---|
| 227 | void keyboardFunc(unsigned char key, int mouseX, int mouseY) |
---|
| 228 | { |
---|
| 229 | switch(key) |
---|
| 230 | { |
---|
| 231 | /* If the user hits escape or Q, then exit */ |
---|
| 232 | /* ESCAPE_KEY = 27 */ |
---|
| 233 | case 27: |
---|
| 234 | case 'r': rotate = true; |
---|
| 235 | glutPostRedisplay(); |
---|
| 236 | break; |
---|
| 237 | case 'w': translateW = true; |
---|
| 238 | glutPostRedisplay(); |
---|
| 239 | break; |
---|
| 240 | case 'z': translateZ = true; |
---|
| 241 | glutPostRedisplay(); |
---|
| 242 | break; |
---|
| 243 | |
---|
| 244 | case 'q': |
---|
| 245 | case 'Q': |
---|
| 246 | { |
---|
| 247 | if(((NBody*)me)->cleanup() != 0) // SDK_SUCCESS == 0; |
---|
| 248 | exit(1); |
---|
| 249 | else |
---|
| 250 | exit(0); |
---|
| 251 | } |
---|
| 252 | default: |
---|
| 253 | break; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|