RiverRat  - An MIT iCampus project http://web.mit.edu/riverrat/

Main Page | Software Documentation | Hardware Documentation | People | Contact | Wiki

Main Page | Class Hierarchy | Class List | File List | Class Members

RiverRatGL.java

00001 package riverrat;
00002 
00003 import java.awt.*;
00004 import java.awt.event.*;
00005 import net.java.games.jogl.*;
00006 
00011 public class RiverRatGL implements RiverRatClient {
00012         static Animator animator = null;
00013 
00014         public static void main(String[] args) {
00015                 Frame frame = new Frame("RiverRatGL: The coolest sailing you've ever rendered.");
00016                 GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
00017                 
00018                 canvas.addGLEventListener(new RiverRatRenderer());
00019                 frame.add(canvas);
00020                 frame.setSize(500, 500);
00021                 final Animator animator = new Animator(canvas);
00022                 frame.addWindowListener(new WindowAdapter()
00023                 {
00024                         public void windowClosing(WindowEvent e) {
00025                                 System.exit(0);
00026                         }
00027                 });
00028                 frame.show();
00029                 animator.start();
00030                 canvas.requestFocus();
00031         }
00032         
00033         static class RiverRatRenderer implements GLEventListener, KeyListener, MouseListener, MouseMotionListener {
00034                 private float view_rotx = 20.0f, view_roty = 30.0f, view_rotz = 0.0f;
00035                 private int prevMouseX, prevMouseY;
00036                 private boolean mouseRButtonDown = false;
00037                 
00038         
00039                 public void drawBase(GL gl) {
00040                         gl.glTranslatef(0.0f, -2.0f, -15.0f);           //move back
00041                         gl.glBegin(GL.GL_QUADS);                                                        //draw using quads
00042                                 gl.glColor3f(0.0f, 1.0f, 0.0f);                 //set color to a greenish blue
00043                                 gl.glVertex3f(-10.0f, 0.0f, 10.0f);             //front left corner
00044                                 gl.glColor3f(0.0f, 0.0f, 1.0f);                 //different shade of green-blue
00045                                 gl.glVertex3f(-10.0f, 0.0f, -10.0f);    //back left corner
00046                                 gl.glColor3f(0.0f, 0.4f, 0.9f);                 //new shade
00047                                 gl.glVertex3f(10.0f, 0.0f, -10.0f);             //back right corner
00048                                 gl.glColor3f(0.0f, 0.0f, 1.0f);                 //new shade
00049                                 gl.glVertex3f(10.0f, 0.0f, 10.0f);              //front right corner
00050                         gl.glEnd();                                                                                                             //done drawing quad             
00051                 }
00052                 
00053                 public void drawTest(GL gl) {
00054                         //draw a test triangle
00055                         gl.glTranslatef(-1.5f,0.0f,-6.0f);
00056                         gl.glBegin(GL.GL_TRIANGLES);
00057                                 gl.glColor3f(1.0f, 0.0f, 0.0f);
00058                                 gl.glVertex3f(0.0f, 1.0f, 0.0f);
00059                                 gl.glColor3f(0.0f, 1.0f, 0.0f);
00060                                 gl.glVertex3f(-1.0f, -1.0f, 0.0f);
00061                                 gl.glColor3f(0.0f, 0.0f, 1.0f);
00062                                 gl.glVertex3f(1.0f, -1.0f, 0.0f);
00063                         gl.glEnd();
00064                 }
00065                 
00066                 public void display(GLDrawable gLDrawable) {
00067                         final GL gl = gLDrawable.getGL();
00068                         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
00069                         //gl.glLoadIdentity();
00070                         
00071                         gl.glPushMatrix();
00072                         System.out.println("view_rotx: "+view_rotx);
00073                         System.out.println("view_roty: "+view_roty);
00074                         System.out.println("view_rotz: "+view_rotz);
00075                         //gl.glRotatef(view_rotx, 0.0f, 0.0f, 0.0f);
00076                         gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
00077                         //gl.glRotatef(view_rotz, 0.0f, 0.0f, 0.0f);
00078 
00079                         //drawTest(gl);
00080                         drawBase(gl);
00081                         
00082                         gl.glPopMatrix();
00083                         
00084                         gl.glFlush();
00085                 }
00086                 
00087                 public void displayChanged(GLDrawable glDrawable, boolean modeChanged, boolean deviceChanged) {}
00088                 
00089                 public void init(GLDrawable gLDrawable) {
00090                         final GL gl = gLDrawable.getGL();
00091                         gl.glShadeModel(GL.GL_SMOOTH);
00092                         gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
00093                         
00094                         gLDrawable.addMouseListener(this);
00095                         gLDrawable.addMouseMotionListener(this);
00096                         gLDrawable.addKeyListener(this);
00097                 }
00098                 
00099                 public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height) {
00100                         final GL gl = gLDrawable.getGL();
00101                         final GLU glu = gLDrawable.getGLU();
00102                         
00103                         if(height <= 0) height = 1;
00104                         final float h = (float)width / (float)height;
00105                         gl.glViewport(0,0,width,height);
00106                         gl.glMatrixMode(GL.GL_PROJECTION);
00107                         gl.glLoadIdentity();
00108                         glu.gluPerspective(45.0f,h,1.0,20.0);
00109                         gl.glMatrixMode(GL.GL_MODELVIEW);
00110                         gl.glLoadIdentity();
00111                 }
00112                 
00113                 public void mouseMoved(MouseEvent e) {}
00114                 public void mouseEntered(MouseEvent e) {}
00115                 public void mouseExited(MouseEvent e) {}
00116                 public void mouseClicked(MouseEvent e) {}
00117                 
00118                 public void mousePressed(MouseEvent e) {
00119                         prevMouseX = e.getX();
00120                         prevMouseY = e.getY();
00121                         if((e.getModifiers() & e.BUTTON3_MASK) != 0) {
00122                                 mouseRButtonDown = true;
00123                         }
00124                 } //mousePressed()
00125                 
00126                 public void mouseReleased(MouseEvent e) {
00127                         if((e.getModifiers() & e.BUTTON3_MASK) != 0) {
00128                                 mouseRButtonDown = false;
00129                         }
00130                 } //mouseReleased()
00131                 
00132                 public void mouseDragged(MouseEvent e) {
00133                         int x = e.getX();
00134                         int y = e.getY();
00135                         Dimension size = e.getComponent().getSize();
00136                         
00137                         float thetaY = 360.0f * ((float)(x-prevMouseX)/(float)size.width);
00138                         float thetaX = 360.0f * ((float)(prevMouseY-y)/(float)size.height);
00139                         
00140                         prevMouseX = x;
00141                         prevMouseY = y;
00142                         
00143                         view_rotx += thetaX;
00144                         view_roty += thetaY;
00145                 } //mouseDragged()
00146                 
00147                 public void keyPressed(KeyEvent e) {
00148                         if(e.getKeyCode() == KeyEvent.VK_ESCAPE) System.exit(0);
00149                 }
00150                 
00151                 public void keyReleased(KeyEvent e) {
00152                 }
00153                 
00154                 public void keyTyped(KeyEvent e) {
00155                 }
00156         }
00157 
00158 }
 

Brought to you by the RiverRat team.