[152] | 1 | package solarsim.gl; |
---|
| 2 | |
---|
| 3 | import solarsim.common.State; |
---|
| 4 | import solarsim.*; |
---|
| 5 | import com.sun.opengl.util.Animator; |
---|
| 6 | import com.sun.opengl.util.texture.Texture; |
---|
| 7 | import com.sun.opengl.util.texture.TextureCoords; |
---|
| 8 | import com.sun.opengl.util.texture.TextureIO; |
---|
| 9 | import java.awt.Frame; |
---|
| 10 | import java.awt.event.*; |
---|
| 11 | import java.io.File; |
---|
| 12 | import java.io.IOException; |
---|
| 13 | import java.util.Iterator; |
---|
| 14 | import java.util.LinkedList; |
---|
| 15 | import java.util.logging.Level; |
---|
| 16 | import java.util.logging.Logger; |
---|
| 17 | import javax.media.opengl.GL; |
---|
| 18 | import javax.media.opengl.GLAutoDrawable; |
---|
| 19 | import javax.media.opengl.GLCanvas; |
---|
| 20 | import javax.media.opengl.GLEventListener; |
---|
| 21 | import javax.media.opengl.GLException; |
---|
| 22 | import javax.media.opengl.glu.GLU; |
---|
| 23 | import javax.media.opengl.glu.GLUquadric; |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * SolarSim.java <BR> |
---|
| 29 | * author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P> |
---|
| 30 | * |
---|
| 31 | * This version is equal to Brian Paul's version 1.2 1999/10/21 |
---|
| 32 | */ |
---|
| 33 | public class SolarSim implements GLEventListener, Runnable, KeyListener, MouseListener { |
---|
| 34 | |
---|
| 35 | float rotate = 0; |
---|
| 36 | private final double SCALE_FACTOR = 150E9F; // 150 million km |
---|
| 37 | int MoveX = 0; |
---|
| 38 | int MoveY = 0; |
---|
| 39 | |
---|
| 40 | float x1[][]= new float[361][2]; |
---|
| 41 | float x2[][]= new float[361][2]; |
---|
| 42 | float x3[][]= new float[721][2]; |
---|
| 43 | |
---|
| 44 | float Xvalue = 0; |
---|
| 45 | float Yvalue = 0; |
---|
| 46 | float Angle = 0; |
---|
| 47 | |
---|
| 48 | static final int MODE_SCALE = 0; |
---|
| 49 | static final int MODE_ROTATE = 1; |
---|
| 50 | static final int MODE_NOTHING = -1; |
---|
| 51 | |
---|
| 52 | int press_x, press_y; |
---|
| 53 | float msX_angle = 0; |
---|
| 54 | float msY_angle = 0; |
---|
| 55 | int mouseX, mouseY; |
---|
| 56 | int mode ; |
---|
| 57 | float scale_size = 1; |
---|
| 58 | |
---|
| 59 | private GLU glu = new GLU(); |
---|
| 60 | |
---|
| 61 | //private boolean newTexture; |
---|
| 62 | private boolean flushTexture; |
---|
| 63 | private File file; |
---|
| 64 | private Texture textureEarth, textureSun; |
---|
| 65 | |
---|
| 66 | private LinkedList<State> states; |
---|
| 67 | static int count = 0; |
---|
| 68 | |
---|
| 69 | private volatile boolean running; |
---|
| 70 | private Thread thread; |
---|
| 71 | Iterator<State> it; |
---|
| 72 | |
---|
| 73 | public SolarSim(){ |
---|
| 74 | this.states = new LinkedList<State>(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void drawState(State s) { |
---|
| 78 | if( count == 0 ) |
---|
| 79 | System.out.println(s); |
---|
| 80 | if( count++ % 1 != 0 ) |
---|
| 81 | return; |
---|
| 82 | if( s == null ) |
---|
| 83 | return; |
---|
| 84 | synchronized( states ) { |
---|
| 85 | states.addFirst(s); |
---|
| 86 | states.notify(); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | public void init(GLAutoDrawable drawable) { |
---|
| 91 | // Use debug pipeline |
---|
| 92 | // drawable.setGL(new DebugGL(drawable.getGL())); |
---|
| 93 | |
---|
| 94 | int i = 0; |
---|
| 95 | |
---|
| 96 | for(i=0; i <= 360; i++) |
---|
| 97 | { |
---|
| 98 | x1[i][0] = (float)(Math.sin(i*3.1416/180)*3); |
---|
| 99 | x1[i][1] = (float)(Math.cos(i*3.1416/180)*3); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | for(i=0; i <= 360; i++) |
---|
| 103 | { |
---|
| 104 | x2[i][0] = (float)(Math.sin(i*3.1416/180)*1); |
---|
| 105 | x2[i][1] = (float)(Math.cos(i*3.1416/180)*1); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | for(i=0; i <= 720; i++) |
---|
| 109 | { |
---|
| 110 | x3[i][0] = (float)(Math.sin(i*3.1416/180)*5); |
---|
| 111 | x3[i][1] = (float)(Math.cos(i*3.1416/180)*5); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | GL gl = drawable.getGL(); |
---|
| 115 | System.err.println("INIT GL IS: " + gl.getClass().getName()); |
---|
| 116 | |
---|
| 117 | // Enable VSync |
---|
| 118 | gl.setSwapInterval(1); |
---|
| 119 | |
---|
| 120 | // Setup the drawing area and shading mode |
---|
| 121 | gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
---|
| 122 | gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens. |
---|
| 123 | |
---|
| 124 | it = states.iterator(); |
---|
| 125 | |
---|
| 126 | /* gl.glViewport(0,0,500,300); |
---|
| 127 | gl.glMatrixMode(GL.GL_PROJECTION); |
---|
| 128 | gl.glLoadIdentity(); |
---|
| 129 | glu.gluPerspective(45.0f, 800.0f / 600.0f, 1.0f, 500.0f); |
---|
| 130 | gl.glMatrixMode(GL.GL_MODELVIEW);*/ |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { |
---|
| 134 | GL gl = drawable.getGL(); |
---|
| 135 | //GLU glu = new GLU(); |
---|
| 136 | |
---|
| 137 | if (height <= 0) { // avoid a divide by zero error! |
---|
| 138 | |
---|
| 139 | height = 1; |
---|
| 140 | } |
---|
| 141 | final float h = (float) width / (float) height; |
---|
| 142 | gl.glViewport(0, 0, width, height); |
---|
| 143 | gl.glMatrixMode(GL.GL_PROJECTION); |
---|
| 144 | gl.glLoadIdentity(); |
---|
| 145 | glu.gluPerspective(45.0f, h, 1.0, 20.0); |
---|
| 146 | gl.glMatrixMode(GL.GL_MODELVIEW); |
---|
| 147 | gl.glLoadIdentity(); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | public void display(GLAutoDrawable drawable) { |
---|
| 151 | /* synchronized( states ) { |
---|
| 152 | while( states.isEmpty() ) { |
---|
| 153 | try { |
---|
| 154 | states.wait(); |
---|
| 155 | } catch (InterruptedException ex) { } |
---|
| 156 | } |
---|
| 157 | }*/ |
---|
| 158 | |
---|
| 159 | //if (it.hasNext()) |
---|
| 160 | // while (it.hasNext()) |
---|
| 161 | // { |
---|
| 162 | // State s = it.next(); |
---|
| 163 | try { |
---|
| 164 | GL gl = drawable.getGL(); |
---|
| 165 | File file = new File("images/earthS.png"); |
---|
| 166 | textureEarth = TextureIO.newTexture(file, true); |
---|
| 167 | file = new File("images/sunS.jpg"); |
---|
| 168 | textureSun = TextureIO.newTexture(file, true); |
---|
| 169 | // Clear the drawing area |
---|
| 170 | gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); |
---|
| 171 | // Reset the current matrix to the "identity" |
---|
| 172 | gl.glLoadIdentity(); |
---|
| 173 | gl.glTranslatef(0.0f, 0.0f, -6.0f); |
---|
| 174 | |
---|
| 175 | if (mode == MODE_ROTATE) { |
---|
| 176 | msX_angle += (mouseX - press_x) / 5.0; |
---|
| 177 | // System.out.println("changed "+msX_angle); |
---|
| 178 | if (msX_angle > 180) { |
---|
| 179 | msX_angle -= 360; |
---|
| 180 | } else if (msX_angle < -180) { |
---|
| 181 | msX_angle += 360; |
---|
| 182 | } |
---|
| 183 | press_x = mouseX; |
---|
| 184 | msY_angle += (mouseY - press_y) / 5.0; |
---|
| 185 | if (msY_angle > 180) { |
---|
| 186 | msY_angle -= 360; |
---|
| 187 | } else if (msY_angle < -180) { |
---|
| 188 | msY_angle += 360; |
---|
| 189 | } |
---|
| 190 | press_y = mouseY; |
---|
| 191 | } else if (mode == MODE_SCALE) { |
---|
| 192 | float old_size = scale_size; |
---|
| 193 | scale_size *= (1 + (mouseY - press_y) / 60.0); |
---|
| 194 | if (scale_size < 0) { |
---|
| 195 | scale_size = old_size; |
---|
| 196 | } |
---|
| 197 | press_y = mouseY; |
---|
| 198 | } |
---|
| 199 | //glu.gluLookAt(0.0, 7.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); |
---|
| 200 | glu.gluLookAt(0,7,2, 0,0,0, 0,1,0); |
---|
| 201 | gl.glColor3f((float) 1.0, (float) 1.0, (float) 1.0); |
---|
| 202 | //rotate scale the entire image |
---|
| 203 | gl.glRotatef(msX_angle, 0, 1, 0); |
---|
| 204 | gl.glRotatef(msY_angle, 1, 0, 0); |
---|
| 205 | gl.glScalef(scale_size, scale_size, scale_size); |
---|
| 206 | |
---|
| 207 | GLUquadric qobj = glu.gluNewQuadric(); |
---|
| 208 | glu.gluQuadricDrawStyle(qobj, GLU.GLU_FILL); |
---|
| 209 | glu.gluQuadricNormals(qobj, GLU.GLU_SMOOTH); |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | /* synchronized(states) { |
---|
| 213 | // for( State s : states ) { |
---|
| 214 | Particle p = s.getParticles().get(0); |
---|
| 215 | Vector r = p.getPosition(); |
---|
| 216 | int dx = +(int)(r.x/SCALE_FACTOR*300)/20; |
---|
| 217 | int dy = -(int)(r.y/SCALE_FACTOR*300)/20; |
---|
| 218 | System.out.println(dx + " " + dy); |
---|
| 219 | //earth |
---|
| 220 | // glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 1.0, .0, 0.0); |
---|
| 221 | gl.glPushMatrix(); |
---|
| 222 | |
---|
| 223 | gl.glTranslatef(dx, dy, 0.0f); |
---|
| 224 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 225 | gl.glColor3f(0f, 0f, 1f); |
---|
| 226 | |
---|
| 227 | glu.gluSphere(qobj,0.2, 15, 15); |
---|
| 228 | gl.glTranslatef(-dx, -dy, 0.0f); |
---|
| 229 | |
---|
| 230 | gl.glPopMatrix(); |
---|
| 231 | // } |
---|
| 232 | //}*/ |
---|
| 233 | /* gl.glPushMatrix(); |
---|
| 234 | |
---|
| 235 | gl.glTranslatef(Xvalue, 0.0f, Yvalue); |
---|
| 236 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 237 | gl.glColor3f(0f, 0f, 1f); |
---|
| 238 | |
---|
| 239 | glu.gluSphere(qobj,0.5, 20, 20); |
---|
| 240 | gl.glTranslatef(Xvalue, 0.0f, Yvalue); |
---|
| 241 | |
---|
| 242 | gl.glPopMatrix();*/ |
---|
| 243 | |
---|
| 244 | if (textureSun != null) { |
---|
| 245 | textureSun.enable(); |
---|
| 246 | textureSun.bind(); |
---|
| 247 | gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE); |
---|
| 248 | TextureCoords coords = textureSun.getImageTexCoords(); |
---|
| 249 | //sun |
---|
| 250 | gl.glPushMatrix(); |
---|
| 251 | //glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); |
---|
| 252 | gl.glTranslatef(Xvalue, 0.0f, Yvalue); |
---|
| 253 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 254 | //glu.gluQuadricNormals(qobj, GLU.GLU_SMOOTH); |
---|
| 255 | // gl.glColor4f(1f, 1f, 0f, 1f); |
---|
| 256 | gl.glTexCoord2f(coords.left(), coords.bottom()); |
---|
| 257 | glu.gluQuadricTexture(qobj, true); |
---|
| 258 | glu.gluSphere(qobj, 0.2f, 15, 15); |
---|
| 259 | textureSun.disable(); |
---|
| 260 | glu.gluQuadricTexture(qobj, false); |
---|
| 261 | gl.glPopMatrix(); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | if (textureEarth != null) { |
---|
| 265 | textureEarth.enable(); |
---|
| 266 | textureEarth.bind(); |
---|
| 267 | gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE); |
---|
| 268 | TextureCoords coords = textureEarth.getImageTexCoords(); |
---|
| 269 | //earth |
---|
| 270 | //glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); |
---|
| 271 | gl.glPushMatrix(); |
---|
| 272 | if (MoveX == 360) { |
---|
| 273 | MoveX = 0; |
---|
| 274 | } |
---|
| 275 | gl.glTranslatef(x1[MoveX][1], x1[MoveX][0], 0.0f); |
---|
| 276 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 277 | //gl.glColor3f(0f, 0f, 1f); |
---|
| 278 | gl.glTexCoord2f(coords.left(), coords.bottom()); |
---|
| 279 | glu.gluQuadricTexture(qobj, true); |
---|
| 280 | glu.gluSphere(qobj, 0.3f, 15, 15); |
---|
| 281 | textureEarth.disable(); |
---|
| 282 | glu.gluQuadricTexture(qobj, false); |
---|
| 283 | |
---|
| 284 | gl.glTranslatef(x2[MoveX][0], x2[MoveX][1], 0.0f); |
---|
| 285 | glu.gluSphere(qobj, 0.1f, 10, 10); |
---|
| 286 | gl.glPopMatrix(); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | //saturn |
---|
| 291 | gl.glPushMatrix(); |
---|
| 292 | // glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); |
---|
| 293 | if (MoveY == 720) { |
---|
| 294 | MoveY = 0; |
---|
| 295 | } |
---|
| 296 | gl.glTranslatef(x3[MoveY / 2][1], x3[MoveY / 2][0], 0.0f); |
---|
| 297 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 298 | glu.gluSphere(qobj, 0.3f, 15, 15); |
---|
| 299 | int i = 0; |
---|
| 300 | //glBegin(GL_LINE_STRIP); |
---|
| 301 | gl.glBegin(gl.GL_QUAD_STRIP); |
---|
| 302 | for (i = 0; i <= 360; i++) { |
---|
| 303 | gl.glVertex3f((float) (Math.sin(i*3.1416/180)*0.5), (float) (Math.cos(i*3.1416/180)*0.5), 0f); |
---|
| 304 | gl.glVertex3f((float) (Math.sin(i*3.1416/180)*0.7), (float) (Math.cos(i*3.1416/180)*0.7), 0f); |
---|
| 305 | } |
---|
| 306 | gl.glEnd(); |
---|
| 307 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 308 | gl.glPopMatrix(); |
---|
| 309 | gl.glFlush(); |
---|
| 310 | Angle += 15.0; |
---|
| 311 | // glu.glutPostRedisplay(); |
---|
| 312 | MoveX += 1; |
---|
| 313 | MoveY += 1; |
---|
| 314 | // Flush all drawing operations to the graphics card |
---|
| 315 | } catch (Exception ex) { |
---|
| 316 | Logger.getLogger(SolarSim.class.getName()).log(Level.SEVERE, null, ex); |
---|
| 317 | } |
---|
| 318 | //} |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | public void run() { |
---|
| 325 | this.running = true; |
---|
| 326 | this.thread = Thread.currentThread(); |
---|
| 327 | while( running ) { |
---|
| 328 | synchronized( states ) { |
---|
| 329 | while( states.isEmpty() ) { |
---|
| 330 | try { |
---|
| 331 | states.wait(); |
---|
| 332 | } catch (InterruptedException ex) { } |
---|
| 333 | } |
---|
| 334 | // State s = states.removeLast(); |
---|
| 335 | |
---|
| 336 | } |
---|
| 337 | try { |
---|
| 338 | thread.sleep(40); |
---|
| 339 | } catch( Exception ex ) {} |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | public void keyTyped(KeyEvent e) { |
---|
| 344 | // throw new UnsupportedOperationException("Not supported yet."); |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | public void keyPressed(KeyEvent e) { |
---|
| 348 | // throw new UnsupportedOperationException("Not supported yet."); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | public void keyReleased(KeyEvent e) { |
---|
| 352 | // throw new UnsupportedOperationException("Not supported yet."); |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | public void mouseClicked(MouseEvent e) { |
---|
| 356 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | public void mousePressed(MouseEvent e) { |
---|
| 360 | System.out.println("pressed"); |
---|
| 361 | |
---|
| 362 | switch(e.getModifiers()) { |
---|
| 363 | case InputEvent.BUTTON1_MASK: { |
---|
| 364 | mode = MODE_ROTATE; |
---|
| 365 | mouseX=e.getX(); |
---|
| 366 | mouseY=e.getY(); |
---|
| 367 | System.out.println("That's the LEFT button"); |
---|
| 368 | break; |
---|
| 369 | } |
---|
| 370 | case InputEvent.BUTTON3_MASK: { |
---|
| 371 | mode = MODE_SCALE; |
---|
| 372 | mouseX=e.getX(); |
---|
| 373 | mouseY=e.getY(); |
---|
| 374 | System.out.println("That's the RIGHT button"); |
---|
| 375 | break; |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 379 | } |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | public void mouseReleased(MouseEvent e) { |
---|
| 383 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | public void mouseEntered(MouseEvent e) { |
---|
| 387 | // throw new UnsupportedOperationException("Not supported yet."); |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | public void mouseExited(MouseEvent e) { |
---|
| 391 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | public static void main(String[] args) { |
---|
| 395 | try{ |
---|
| 396 | SolarSim sim = new SolarSim(); |
---|
| 397 | new Thread(sim).start(); |
---|
| 398 | Frame frame = new Frame("Simple JOGL Application"); |
---|
| 399 | GLCanvas canvas = new GLCanvas(); |
---|
| 400 | |
---|
| 401 | canvas.requestFocus(); |
---|
| 402 | canvas.addGLEventListener(sim); |
---|
| 403 | canvas.addKeyListener(sim); |
---|
| 404 | canvas.addMouseListener(sim); |
---|
| 405 | |
---|
| 406 | frame.add(canvas); |
---|
| 407 | frame.setSize(800, 600); |
---|
| 408 | final Animator animator = new Animator(canvas); |
---|
| 409 | frame.addWindowListener(new WindowAdapter() { |
---|
| 410 | |
---|
| 411 | @Override |
---|
| 412 | public void windowClosing(WindowEvent e) { |
---|
| 413 | // Run this on another thread than the AWT event queue to |
---|
| 414 | // make sure the call to Animator.stop() completes before |
---|
| 415 | // exiting |
---|
| 416 | new Thread(new Runnable() { |
---|
| 417 | |
---|
| 418 | public void run() { |
---|
| 419 | animator.stop(); |
---|
| 420 | System.exit(0); |
---|
| 421 | } |
---|
| 422 | }).start(); |
---|
| 423 | } |
---|
| 424 | }); |
---|
| 425 | // Center frame |
---|
| 426 | frame.setLocationRelativeTo(null); |
---|
| 427 | frame.setVisible(true); |
---|
| 428 | animator.start(); |
---|
| 429 | |
---|
| 430 | /* InputStream is = new FileInputStream("sim.dat"); |
---|
| 431 | // TODO must have stop message |
---|
| 432 | for( int i = 0; i < 1000000; i++ ) { |
---|
| 433 | State s = State.readStateFromInputStream(is); |
---|
| 434 | sim.drawState(s); |
---|
| 435 | }*/ |
---|
| 436 | |
---|
| 437 | System.out.println("Stopping."); |
---|
| 438 | |
---|
| 439 | Thread.currentThread().sleep(1000 * 10); |
---|
| 440 | } catch(Exception e) { |
---|
| 441 | e.printStackTrace(); |
---|
| 442 | } |
---|
| 443 | } |
---|
| 444 | } |
---|
| 445 | |
---|