[152] | 1 | package solarsim.gl; |
---|
| 2 | |
---|
| 3 | import solarsim.common.State; |
---|
| 4 | import solarsim.*; |
---|
| 5 | import java.awt.event.*; |
---|
| 6 | import java.io.*; |
---|
| 7 | import java.util.logging.Level; |
---|
| 8 | import java.util.logging.Logger; |
---|
| 9 | //import net.java.games.jogl.*; // If thise does not work, try net.java.games.jogl.*, or get the newest version of JOGL. |
---|
| 10 | import java.lang.Math.*; |
---|
| 11 | import java.util.LinkedList; |
---|
| 12 | import javax.media.opengl.*; |
---|
| 13 | import javax.media.opengl.glu.*; |
---|
| 14 | import com.sun.opengl.util.texture.*; |
---|
| 15 | |
---|
| 16 | public class GameGLEventListener implements GLEventListener,MouseListener, MouseMotionListener, KeyListener { |
---|
| 17 | static final int MODE_SCALE = 0; |
---|
| 18 | static final int MODE_ROTATE = 1; |
---|
| 19 | static final int MODE_NOTHING = -1; |
---|
| 20 | |
---|
| 21 | int press_x, press_y; |
---|
| 22 | float msX_angle = 0; |
---|
| 23 | float msY_angle = 0; |
---|
| 24 | int mouseX, mouseY; |
---|
| 25 | int mode ; |
---|
| 26 | float scale_size = 1; |
---|
| 27 | |
---|
| 28 | float rotate = 0; |
---|
| 29 | |
---|
| 30 | int MoveX = 0; |
---|
| 31 | int MoveY = 0; |
---|
| 32 | |
---|
| 33 | float x1[][]= new float[361][2]; |
---|
| 34 | float x2[][]= new float[361][2]; |
---|
| 35 | float x3[][]= new float[721][2]; |
---|
| 36 | |
---|
| 37 | float Xvalue = 0; |
---|
| 38 | float Yvalue = 0; |
---|
| 39 | float Angle = 0; |
---|
| 40 | |
---|
| 41 | private LinkedList<State> states = new LinkedList<State>(); |
---|
| 42 | public final int MAX_STATES = 10000; |
---|
| 43 | private final double SCALE_FACTOR = 150E9F; // 150 million km |
---|
| 44 | private GLU glu = new GLU(); |
---|
| 45 | |
---|
| 46 | // Only put stuff here that should happen once, at the beginning of the program |
---|
| 47 | public void init(GLAutoDrawable gld) { |
---|
| 48 | //states = new LinkedList<State>(); |
---|
| 49 | int i = 0; |
---|
| 50 | |
---|
| 51 | for(i=0; i <= 360; i++) |
---|
| 52 | { |
---|
| 53 | x1[i][0] = (float)(Math.sin(i*3.1416/180)*3); |
---|
| 54 | x1[i][1] = (float)(Math.cos(i*3.1416/180)*3); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | for(i=0; i <= 360; i++) |
---|
| 58 | { |
---|
| 59 | x2[i][0] = (float)(Math.sin(i*3.1416/180)*1); |
---|
| 60 | x2[i][1] = (float)(Math.cos(i*3.1416/180)*1); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | for(i=0; i <= 720; i++) |
---|
| 64 | { |
---|
| 65 | x3[i][0] = (float)(Math.sin(i*3.1416/180)*5); |
---|
| 66 | x3[i][1] = (float)(Math.cos(i*3.1416/180)*5); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | GL gl = gld.getGL(); |
---|
| 70 | gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
---|
| 71 | |
---|
| 72 | gl.glViewport(0,0,500,300); |
---|
| 73 | gl.glMatrixMode(GL.GL_PROJECTION); |
---|
| 74 | gl.glLoadIdentity(); |
---|
| 75 | glu.gluPerspective(45.0f, 800.0f / 600.0f, 1.0f, 500.0f); |
---|
| 76 | gl.glMatrixMode(GL.GL_MODELVIEW); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void drawState(State s) { |
---|
| 80 | // System.out.println("in draw"); |
---|
| 81 | //if (states != nu) |
---|
| 82 | synchronized( states ) { |
---|
| 83 | if( states.size() == MAX_STATES ) { |
---|
| 84 | states.removeLast(); |
---|
| 85 | } |
---|
| 86 | states.addFirst(s); |
---|
| 87 | } |
---|
| 88 | //this.repaint(); |
---|
| 89 | } |
---|
| 90 | // This function will get called repeatedly by the Animator. Think of it as your game loop. |
---|
| 91 | public void display(GLAutoDrawable gld) { |
---|
| 92 | GL gl = gld.getGL(); |
---|
| 93 | // GLU glu = gld.getGLU(); |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | if (mode == MODE_ROTATE) |
---|
| 97 | { |
---|
| 98 | msX_angle += (mouseX - press_x)/5.0; |
---|
| 99 | // System.out.println("changed "+msX_angle); |
---|
| 100 | if (msX_angle > 180) msX_angle -= 360; |
---|
| 101 | else if (msX_angle <-180) msX_angle += 360; |
---|
| 102 | press_x = mouseX; |
---|
| 103 | |
---|
| 104 | msY_angle += (mouseY - press_y)/5.0; |
---|
| 105 | if (msY_angle > 180) msY_angle -= 360; |
---|
| 106 | else if (msY_angle <-180) msY_angle += 360; |
---|
| 107 | press_y = mouseY; |
---|
| 108 | } |
---|
| 109 | else if (mode == MODE_SCALE){ |
---|
| 110 | float old_size = scale_size; |
---|
| 111 | scale_size *= (1+ (mouseY - press_y)/60.0); |
---|
| 112 | if (scale_size <0) scale_size = old_size; |
---|
| 113 | press_y = mouseY; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | gl.glClear (gl.GL_COLOR_BUFFER_BIT); |
---|
| 117 | gl.glColor3f ((float)1.0,(float) 1.0,(float) 1.0); |
---|
| 118 | |
---|
| 119 | //rotate scale the entire image |
---|
| 120 | gl.glRotatef(msX_angle, 0, 1,0); |
---|
| 121 | gl.glRotatef(msY_angle, 1,0,0); |
---|
| 122 | gl.glScalef(scale_size, scale_size, scale_size); |
---|
| 123 | |
---|
| 124 | GLUquadric qobj=null ;//= new GLUquadric(); |
---|
| 125 | glu.gluQuadricDrawStyle(qobj, GLU.GLU_FILL); |
---|
| 126 | //sun |
---|
| 127 | gl.glPushMatrix(); |
---|
| 128 | glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); |
---|
| 129 | gl.glTranslatef(Xvalue, 0.0f, Yvalue); |
---|
| 130 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 131 | //glu.gluQuadricNormals(qobj, GLU.GLU_SMOOTH); |
---|
| 132 | glu.gluSphere (qobj,0.2f, 15, 15); |
---|
| 133 | gl.glPopMatrix(); |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | //earth |
---|
| 137 | // System.out.println(states.size()); |
---|
| 138 | /* for( State s : states ) { |
---|
| 139 | for( Particle p : s.getParticles() ) { |
---|
| 140 | //System.out.println("aici"); |
---|
| 141 | p = s.getParticles().get(0); |
---|
| 142 | Vector r = p.getPosition(); |
---|
| 143 | int dx = +(int)(r.x/SCALE_FACTOR*300); |
---|
| 144 | int dy = -(int)(r.y/SCALE_FACTOR*300); |
---|
| 145 | gl.glPushMatrix(); |
---|
| 146 | glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); |
---|
| 147 | if ( MoveX == 360 ) |
---|
| 148 | MoveX = 0; |
---|
| 149 | gl.glTranslatef((float)dx, (float)dy, 0.0f); |
---|
| 150 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 151 | glu.gluSphere (qobj,0.4f, 10, 10); |
---|
| 152 | //gl.glFfillOval(0, 0, 1, 1); |
---|
| 153 | gl.glTranslatef(-(float)dx, -(float)dy, 0.0f); |
---|
| 154 | gl.glPopMatrix(); |
---|
| 155 | //g.translate(dx, dy); |
---|
| 156 | //g.fillOval(0, 0, 1, 1); |
---|
| 157 | //g.translate(-dx, -dy); |
---|
| 158 | gl.glFlush (); |
---|
| 159 | } |
---|
| 160 | }*/ |
---|
| 161 | |
---|
| 162 | gl.glPushMatrix(); |
---|
| 163 | glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); |
---|
| 164 | if ( MoveX == 360 ) |
---|
| 165 | MoveX = 0; |
---|
| 166 | gl.glTranslatef(x1[MoveX][1], x1[MoveX][0], 0.0f); |
---|
| 167 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 168 | glu.gluSphere (qobj,0.3f, 15, 15); |
---|
| 169 | gl.glTranslatef(x2[MoveX][0], x2[MoveX][1], 0.0f); |
---|
| 170 | glu.gluSphere (qobj ,0.1f, 10, 10); |
---|
| 171 | gl.glPopMatrix(); |
---|
| 172 | |
---|
| 173 | //saturn |
---|
| 174 | gl.glPushMatrix(); |
---|
| 175 | glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); |
---|
| 176 | if( MoveY == 720 ) |
---|
| 177 | MoveY = 0; |
---|
| 178 | gl.glTranslatef(x3[MoveY/2][1], x3[MoveY/2][0], 0.0f); |
---|
| 179 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 180 | glu.gluSphere (qobj ,0.3f, 15, 15); |
---|
| 181 | int i = 0; |
---|
| 182 | //glBegin(GL_LINE_STRIP); |
---|
| 183 | gl.glBegin(gl.GL_QUAD_STRIP); |
---|
| 184 | for(i=0; i <= 360; i++) |
---|
| 185 | { |
---|
| 186 | gl.glVertex3f((float)(Math.sin(i*3.1416/180)*0.5), (float)(Math.cos(i*3.1416/180)*0.5), 0f ); |
---|
| 187 | gl.glVertex3f((float)(Math.sin(i*3.1416/180)*0.7), (float)(Math.cos(i*3.1416/180)*0.7), 0f ); |
---|
| 188 | } |
---|
| 189 | gl.glEnd(); |
---|
| 190 | gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); |
---|
| 191 | gl.glPopMatrix(); |
---|
| 192 | gl.glFlush (); |
---|
| 193 | Angle += 15.0; |
---|
| 194 | // glu.glutPostRedisplay(); |
---|
| 195 | MoveX +=1; |
---|
| 196 | MoveY +=1; |
---|
| 197 | //glutPostRedisplay(); |
---|
| 198 | //glu.glutTimerFunc(100, animation, 0); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | public void mouseClicked(MouseEvent e) { |
---|
| 202 | System.out.println("clicked"); |
---|
| 203 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | public void mousePressed(MouseEvent e) { |
---|
| 207 | System.out.println("pressed"); |
---|
| 208 | |
---|
| 209 | switch(e.getModifiers()) { |
---|
| 210 | case InputEvent.BUTTON1_MASK: { |
---|
| 211 | mode = MODE_ROTATE; |
---|
| 212 | mouseX=e.getX(); |
---|
| 213 | mouseY=e.getY(); |
---|
| 214 | System.out.println("That's the LEFT button"); |
---|
| 215 | break; |
---|
| 216 | } |
---|
| 217 | case InputEvent.BUTTON3_MASK: { |
---|
| 218 | mode = MODE_SCALE; |
---|
| 219 | mouseX=e.getX(); |
---|
| 220 | mouseY=e.getY(); |
---|
| 221 | System.out.println("That's the RIGHT button"); |
---|
| 222 | break; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | public void mouseReleased(MouseEvent e) { |
---|
| 230 | System.out.println("released"); |
---|
| 231 | mode = MODE_NOTHING; |
---|
| 232 | // throw new UnsupportedOperationException("Not supported yet."); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | public void mouseEntered(MouseEvent e) { |
---|
| 236 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | public void mouseExited(MouseEvent e) { |
---|
| 240 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | public void mouseDragged(MouseEvent e) { |
---|
| 244 | System.out.println("dragged"); |
---|
| 245 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | public void mouseMoved(MouseEvent e) { |
---|
| 249 | // throw new UnsupportedOperationException("Not supported yet."); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | public void keyTyped(KeyEvent e) { |
---|
| 253 | System.out.println("pressed"); |
---|
| 254 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | public void keyPressed(KeyEvent e) { |
---|
| 258 | System.out.println("pressed"); |
---|
| 259 | switch (e.getKeyCode()) { |
---|
| 260 | case KeyEvent.VK_ESCAPE: |
---|
| 261 | System.exit(0); |
---|
| 262 | break; |
---|
| 263 | |
---|
| 264 | default: |
---|
| 265 | break; |
---|
| 266 | } |
---|
| 267 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | public void keyReleased(KeyEvent e) { |
---|
| 271 | System.out.println("pressed"); |
---|
| 272 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /* public void display(GLAutoDrawable drawable) { |
---|
| 276 | throw new UnsupportedOperationException("Not supported yet."); |
---|
| 277 | }*/ |
---|
| 278 | |
---|
| 279 | public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { |
---|
| 280 | // throw new UnsupportedOperationException("Not supported yet."); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { |
---|
| 284 | //throw new UnsupportedOperationException("Not supported yet."); |
---|
| 285 | } |
---|
| 286 | } |
---|