package solarsim.gl; import solarsim.common.State; import solarsim.*; import java.awt.event.*; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; //import net.java.games.jogl.*; // If thise does not work, try net.java.games.jogl.*, or get the newest version of JOGL. import java.lang.Math.*; import java.util.LinkedList; import javax.media.opengl.*; import javax.media.opengl.glu.*; import com.sun.opengl.util.texture.*; public class GameGLEventListener implements GLEventListener,MouseListener, MouseMotionListener, KeyListener { static final int MODE_SCALE = 0; static final int MODE_ROTATE = 1; static final int MODE_NOTHING = -1; int press_x, press_y; float msX_angle = 0; float msY_angle = 0; int mouseX, mouseY; int mode ; float scale_size = 1; float rotate = 0; int MoveX = 0; int MoveY = 0; float x1[][]= new float[361][2]; float x2[][]= new float[361][2]; float x3[][]= new float[721][2]; float Xvalue = 0; float Yvalue = 0; float Angle = 0; private LinkedList states = new LinkedList(); public final int MAX_STATES = 10000; private final double SCALE_FACTOR = 150E9F; // 150 million km private GLU glu = new GLU(); // Only put stuff here that should happen once, at the beginning of the program public void init(GLAutoDrawable gld) { //states = new LinkedList(); int i = 0; for(i=0; i <= 360; i++) { x1[i][0] = (float)(Math.sin(i*3.1416/180)*3); x1[i][1] = (float)(Math.cos(i*3.1416/180)*3); } for(i=0; i <= 360; i++) { x2[i][0] = (float)(Math.sin(i*3.1416/180)*1); x2[i][1] = (float)(Math.cos(i*3.1416/180)*1); } for(i=0; i <= 720; i++) { x3[i][0] = (float)(Math.sin(i*3.1416/180)*5); x3[i][1] = (float)(Math.cos(i*3.1416/180)*5); } GL gl = gld.getGL(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glViewport(0,0,500,300); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0f, 800.0f / 600.0f, 1.0f, 500.0f); gl.glMatrixMode(GL.GL_MODELVIEW); } void drawState(State s) { // System.out.println("in draw"); //if (states != nu) synchronized( states ) { if( states.size() == MAX_STATES ) { states.removeLast(); } states.addFirst(s); } //this.repaint(); } // This function will get called repeatedly by the Animator. Think of it as your game loop. public void display(GLAutoDrawable gld) { GL gl = gld.getGL(); // GLU glu = gld.getGLU(); if (mode == MODE_ROTATE) { msX_angle += (mouseX - press_x)/5.0; // System.out.println("changed "+msX_angle); if (msX_angle > 180) msX_angle -= 360; else if (msX_angle <-180) msX_angle += 360; press_x = mouseX; msY_angle += (mouseY - press_y)/5.0; if (msY_angle > 180) msY_angle -= 360; else if (msY_angle <-180) msY_angle += 360; press_y = mouseY; } else if (mode == MODE_SCALE){ float old_size = scale_size; scale_size *= (1+ (mouseY - press_y)/60.0); if (scale_size <0) scale_size = old_size; press_y = mouseY; } gl.glClear (gl.GL_COLOR_BUFFER_BIT); gl.glColor3f ((float)1.0,(float) 1.0,(float) 1.0); //rotate scale the entire image gl.glRotatef(msX_angle, 0, 1,0); gl.glRotatef(msY_angle, 1,0,0); gl.glScalef(scale_size, scale_size, scale_size); GLUquadric qobj=null ;//= new GLUquadric(); glu.gluQuadricDrawStyle(qobj, GLU.GLU_FILL); //sun gl.glPushMatrix(); glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); gl.glTranslatef(Xvalue, 0.0f, Yvalue); gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); //glu.gluQuadricNormals(qobj, GLU.GLU_SMOOTH); glu.gluSphere (qobj,0.2f, 15, 15); gl.glPopMatrix(); //earth // System.out.println(states.size()); /* for( State s : states ) { for( Particle p : s.getParticles() ) { //System.out.println("aici"); p = s.getParticles().get(0); Vector r = p.getPosition(); int dx = +(int)(r.x/SCALE_FACTOR*300); int dy = -(int)(r.y/SCALE_FACTOR*300); gl.glPushMatrix(); glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); if ( MoveX == 360 ) MoveX = 0; gl.glTranslatef((float)dx, (float)dy, 0.0f); gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); glu.gluSphere (qobj,0.4f, 10, 10); //gl.glFfillOval(0, 0, 1, 1); gl.glTranslatef(-(float)dx, -(float)dy, 0.0f); gl.glPopMatrix(); //g.translate(dx, dy); //g.fillOval(0, 0, 1, 1); //g.translate(-dx, -dy); gl.glFlush (); } }*/ gl.glPushMatrix(); glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); if ( MoveX == 360 ) MoveX = 0; gl.glTranslatef(x1[MoveX][1], x1[MoveX][0], 0.0f); gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); glu.gluSphere (qobj,0.3f, 15, 15); gl.glTranslatef(x2[MoveX][0], x2[MoveX][1], 0.0f); glu.gluSphere (qobj ,0.1f, 10, 10); gl.glPopMatrix(); //saturn gl.glPushMatrix(); glu.gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); if( MoveY == 720 ) MoveY = 0; gl.glTranslatef(x3[MoveY/2][1], x3[MoveY/2][0], 0.0f); gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); glu.gluSphere (qobj ,0.3f, 15, 15); int i = 0; //glBegin(GL_LINE_STRIP); gl.glBegin(gl.GL_QUAD_STRIP); for(i=0; i <= 360; i++) { gl.glVertex3f((float)(Math.sin(i*3.1416/180)*0.5), (float)(Math.cos(i*3.1416/180)*0.5), 0f ); gl.glVertex3f((float)(Math.sin(i*3.1416/180)*0.7), (float)(Math.cos(i*3.1416/180)*0.7), 0f ); } gl.glEnd(); gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f); gl.glPopMatrix(); gl.glFlush (); Angle += 15.0; // glu.glutPostRedisplay(); MoveX +=1; MoveY +=1; //glutPostRedisplay(); //glu.glutTimerFunc(100, animation, 0); } public void mouseClicked(MouseEvent e) { System.out.println("clicked"); //throw new UnsupportedOperationException("Not supported yet."); } public void mousePressed(MouseEvent e) { System.out.println("pressed"); switch(e.getModifiers()) { case InputEvent.BUTTON1_MASK: { mode = MODE_ROTATE; mouseX=e.getX(); mouseY=e.getY(); System.out.println("That's the LEFT button"); break; } case InputEvent.BUTTON3_MASK: { mode = MODE_SCALE; mouseX=e.getX(); mouseY=e.getY(); System.out.println("That's the RIGHT button"); break; } //throw new UnsupportedOperationException("Not supported yet."); } } public void mouseReleased(MouseEvent e) { System.out.println("released"); mode = MODE_NOTHING; // throw new UnsupportedOperationException("Not supported yet."); } public void mouseEntered(MouseEvent e) { //throw new UnsupportedOperationException("Not supported yet."); } public void mouseExited(MouseEvent e) { //throw new UnsupportedOperationException("Not supported yet."); } public void mouseDragged(MouseEvent e) { System.out.println("dragged"); //throw new UnsupportedOperationException("Not supported yet."); } public void mouseMoved(MouseEvent e) { // throw new UnsupportedOperationException("Not supported yet."); } public void keyTyped(KeyEvent e) { System.out.println("pressed"); //throw new UnsupportedOperationException("Not supported yet."); } public void keyPressed(KeyEvent e) { System.out.println("pressed"); switch (e.getKeyCode()) { case KeyEvent.VK_ESCAPE: System.exit(0); break; default: break; } //throw new UnsupportedOperationException("Not supported yet."); } public void keyReleased(KeyEvent e) { System.out.println("pressed"); //throw new UnsupportedOperationException("Not supported yet."); } /* public void display(GLAutoDrawable drawable) { throw new UnsupportedOperationException("Not supported yet."); }*/ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { // throw new UnsupportedOperationException("Not supported yet."); } public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { //throw new UnsupportedOperationException("Not supported yet."); } }