Jogamp
Adding Tennis Demo
authorAlexey Fofonov <fofonovmath90@gmail.com>
Thu, 19 Jan 2012 17:03:51 +0000 (18:03 +0100)
committerAlexey Fofonov <fofonovmath90@gmail.com>
Thu, 19 Jan 2012 17:03:51 +0000 (18:03 +0100)
src/demos/data/images/Stars.png [new file with mode: 0644]
src/demos/data/images/TennisBottom.png [new file with mode: 0644]
src/demos/data/images/TennisEnDesk.png [new file with mode: 0644]
src/demos/data/images/TennisMyDesk.png [new file with mode: 0644]
src/demos/data/images/TennisTop.png [new file with mode: 0644]
src/demos/tennis/Tennis.java [new file with mode: 0644]

diff --git a/src/demos/data/images/Stars.png b/src/demos/data/images/Stars.png
new file mode 100644 (file)
index 0000000..49cec37
Binary files /dev/null and b/src/demos/data/images/Stars.png differ
diff --git a/src/demos/data/images/TennisBottom.png b/src/demos/data/images/TennisBottom.png
new file mode 100644 (file)
index 0000000..d712dd6
Binary files /dev/null and b/src/demos/data/images/TennisBottom.png differ
diff --git a/src/demos/data/images/TennisEnDesk.png b/src/demos/data/images/TennisEnDesk.png
new file mode 100644 (file)
index 0000000..d9c4306
Binary files /dev/null and b/src/demos/data/images/TennisEnDesk.png differ
diff --git a/src/demos/data/images/TennisMyDesk.png b/src/demos/data/images/TennisMyDesk.png
new file mode 100644 (file)
index 0000000..d4a3514
Binary files /dev/null and b/src/demos/data/images/TennisMyDesk.png differ
diff --git a/src/demos/data/images/TennisTop.png b/src/demos/data/images/TennisTop.png
new file mode 100644 (file)
index 0000000..c014744
Binary files /dev/null and b/src/demos/data/images/TennisTop.png differ
diff --git a/src/demos/tennis/Tennis.java b/src/demos/tennis/Tennis.java
new file mode 100644 (file)
index 0000000..2460f90
--- /dev/null
@@ -0,0 +1,920 @@
+package demos.tennis;\r
+\r
+import javax.media.opengl.GL2;\r
+import javax.media.opengl.GLAutoDrawable;\r
+import javax.media.opengl.GLEventListener;\r
+import javax.media.opengl.GLException;\r
+import javax.media.opengl.GLProfile;\r
+import javax.media.opengl.awt.GLCanvas;\r
+import com.jogamp.opengl.util.Animator;\r
+import com.jogamp.opengl.util.texture.Texture;\r
+import com.jogamp.opengl.util.texture.TextureCoords;\r
+import com.jogamp.opengl.util.texture.TextureIO;\r
+\r
+import javax.media.opengl.GL;\r
+//import javax.media.opengl.glu.GLU;\r
+import javax.swing.JOptionPane;\r
+\r
+//import java.io.BufferedReader;\r
+import java.io.ByteArrayOutputStream;\r
+//import java.io.File;\r
+import java.io.IOException;\r
+import java.io.PrintStream;\r
+\r
+import com.jogamp.newt.Window;\r
+//import com.jogamp.newt.event.InputEvent;\r
+import com.jogamp.newt.event.KeyAdapter;\r
+import com.jogamp.newt.event.KeyEvent;\r
+import com.jogamp.newt.event.KeyListener;\r
+import com.jogamp.newt.event.MouseAdapter;\r
+import com.jogamp.newt.event.MouseEvent;\r
+import com.jogamp.newt.event.MouseListener;\r
+import com.jogamp.newt.event.awt.AWTKeyAdapter;\r
+import com.jogamp.newt.event.awt.AWTMouseAdapter;\r
+\r
+import java.awt.AWTException;\r
+import java.awt.Cursor;\r
+import java.awt.Image;\r
+import java.awt.MouseInfo;\r
+import java.awt.Point;\r
+import java.awt.Robot;\r
+import java.awt.Toolkit;\r
+import java.awt.image.BufferedImage;\r
+//import java.io.ByteArrayOutputStream;\r
+//import java.io.IOException;\r
+//import java.io.PrintStream;\r
+\r
+/**\r
+ * Tennis.java <BR>\r
+ * author: Fofonov Alexey <P>\r
+ */\r
\r
+public class Tennis implements GLEventListener {\r
+  static {\r
+       GLProfile.initSingleton();\r
+  }\r
+  private float view_rotx = 0.0f, view_roty = 0.0f, view_rotz = 0.0f;  //View angles\r
+  private float sx = 0.0f, sy = 0.0f;                                                                  //X, Y coords of Mydesk\r
+  private float spx,spy,spz;                                                                                   //Speed of the ball\r
+  private float BallCx = 0.0f, BallCy = 0.0f, BallCz = 0.0f;                   //Coords of the ball\r
+  private float EnDeskCx = 0.0f, EnDeskCy = 0.0f;                                              //X, Y coords of Endesk\r
+  private int   cube=0, mydesk=0, endesk=0, ball=0, box=0;                             //Flags of the existence \r
+  private int   swapInterval;\r
+  private static Texture[] texture;\r
+  private float Bax=0, Bay=0;                                                                                  //Acceleration summands\r
+  private float Vec=3;                                                                                                 //Balls direction\r
+  private boolean CanF=false;                                                                                  //Ready for play\r
+  \r
+  private int WindowW=0, WindowH=0;    \r
+  \r
+  private float LPositionDX=0, NPositionDX=0;                                                  //Mouse positions \r
+  private float LPositionDY=0, NPositionDY=0;                                                  //\r
+  private float DspeedX=0, DspeedY=0;                                                                  //Speed of Mydesk\r
+\r
+  private boolean mouseButtonDown = false, control = true;\r
+  private int prevMouseX, prevMouseY;\r
+\r
+  public static void main(String[] args) {\r
+    // set argument 'NotFirstUIActionOnProcess' in the JNLP's application-desc tag for example\r
+    // <application-desc main-class="demos.j2d.TextCube"/>\r
+    //   <argument>NotFirstUIActionOnProcess</argument> \r
+    // </application-desc>\r
+    // boolean firstUIActionOnProcess = 0==args.length || !args[0].equals("NotFirstUIActionOnProcess") ;\r
+\r
+    java.awt.Frame frame = new java.awt.Frame("Tennis Demo");\r
+    frame.setSize(640, 480);\r
+    frame.setLayout(new java.awt.BorderLayout());\r
+\r
+    final Animator animator = new Animator();\r
+    frame.addWindowListener(new java.awt.event.WindowAdapter() {\r
+        public void windowClosing(java.awt.event.WindowEvent e) {\r
+          // Run this on another thread than the AWT event queue to\r
+          // make sure the call to Animator.stop() completes before\r
+          // exiting\r
+          new Thread(new Runnable() {\r
+              public void run() {\r
+                animator.stop();\r
+                System.exit(0);\r
+              }\r
+            }).start();\r
+        }\r
+      });\r
+\r
+    GLCanvas canvas = new GLCanvas();\r
+    animator.add(canvas);\r
+    // GLCapabilities caps = new GLCapabilities(GLProfile.getDefault());\r
+    // GLCanvas canvas = new GLCanvas(caps);\r
+\r
+    final Tennis tennis = new Tennis();\r
+    canvas.addGLEventListener(tennis);\r
+\r
+    frame.add(canvas, java.awt.BorderLayout.CENTER);\r
+    frame.validate();\r
+    \r
+    //Hide the mouse cursor\r
+    Toolkit t = Toolkit.getDefaultToolkit();\r
+    Image i = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);\r
+    Cursor noCursor = t.createCustomCursor(i, new Point(0, 0), "none");\r
+    frame.setCursor(noCursor);\r
+\r
+    frame.setVisible(true);\r
+    animator.start();\r
+  }\r
+  \r
+  public Tennis(int swapInterval) {\r
+       this.swapInterval = swapInterval;\r
+  }\r
+\r
+  public Tennis() {\r
+       this.swapInterval = 1;\r
+  }\r
+\r
+  public void init(GLAutoDrawable drawable) {\r
+       System.err.println("Tennis: Init: "+drawable);\r
+       // Use debug pipeline\r
+       // drawable.setGL(new DebugGL(drawable.getGL()));\r
+\r
+       GL2 gl = drawable.getGL().getGL2();\r
+\r
+    System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());\r
+    System.err.println("INIT GL IS: " + gl.getClass().getName());\r
+    System.err.println("GL_VENDOR: " + gl.glGetString(GL2.GL_VENDOR));\r
+    System.err.println("GL_RENDERER: " + gl.glGetString(GL2.GL_RENDERER));\r
+    System.err.println("GL_VERSION: " + gl.glGetString(GL2.GL_VERSION));\r
+\r
+    float mat_specular[] =\r
+        { 1.0f, 1.0f, 1.0f, 1.0f };\r
+    float mat_shininess[] =\r
+        { 25.0f };\r
+    float light_position[] =\r
+        { 1.0f, 1.0f, 1.0f, 0.0f };\r
+        \r
+    float red[] = { 0.8f, 0.1f, 0.0f, 0.7f };\r
+    float yellow[] = { 0.8f, 0.75f, 0.0f, 0.7f };\r
+    float blue[] = { 0.2f, 0.2f, 1.0f, 0.7f };\r
+    float brown[] = { 0.8f, 0.4f, 0.1f, 0.7f };\r
+    \r
+    texture = new Texture[5];\r
+    \r
+    //Load textures\r
+    try {\r
+        System.err.println("Loading texture...");\r
+        texture[0] = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream("demos/data/images/TennisTop.png"),\r
+                false,\r
+                TextureIO.PNG);\r
+        texture[1] = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream("demos/data/images/TennisBottom.png"),\r
+                       false,\r
+                TextureIO.PNG);\r
+        texture[2] = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream("demos/data/images/TennisMyDesk.png"),\r
+                       false,\r
+                TextureIO.PNG);\r
+        texture[3] = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream("demos/data/images/TennisEnDesk.png"),\r
+                       false,\r
+                TextureIO.PNG);\r
+        texture[4] = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream("demos/data/images/Stars.png"),\r
+                       false,\r
+                TextureIO.PNG);\r
+        System.err.println("Texture0 estimated memory size = " + texture[0].getEstimatedMemorySize());\r
+        System.err.println("Texture1 estimated memory size = " + texture[1].getEstimatedMemorySize());\r
+        System.err.println("Texture2 estimated memory size = " + texture[2].getEstimatedMemorySize());\r
+        System.err.println("Texture3 estimated memory size = " + texture[3].getEstimatedMemorySize());\r
+        System.err.println("Stars estimated memory size = " + texture[4].getEstimatedMemorySize());\r
+      } catch (IOException e) {\r
+        e.printStackTrace();\r
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
+        e.printStackTrace(new PrintStream(bos));\r
+        JOptionPane.showMessageDialog(null,\r
+                                      bos.toString(),\r
+                                      "Error loading texture",\r
+                                      JOptionPane.ERROR_MESSAGE);\r
+        throw new GLException(e);\r
+        //return;\r
+      }\r
+\r
+    gl.glShadeModel(GL2.GL_SMOOTH);                    // Enable Smooth Shading\r
+    gl.glClearDepth(1.0f);                             // Depth Buffer Setup\r
+    gl.glEnable(GL2.GL_DEPTH_TEST);                    // Enables Depth Testing\r
+    gl.glDepthFunc(GL2.GL_LEQUAL);                     // The Type Of Depth Testing To Do\r
+    \r
+    // Really Nice Perspective Calculations\r
+    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);  \r
+    gl.glEnable(GL2.GL_TEXTURE_2D);\r
+    \r
+    // Texture filter\r
+    gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NONE);\r
+    gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NONE);\r
+    \r
+    // Light and material\r
+    gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);  \r
+    gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);\r
+    gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);\r
+    gl.glEnable(GL2.GL_LIGHTING);\r
+    gl.glEnable(GL2.GL_LIGHT0);\r
\r
+                   \r
+    /* make the objects */\r
+    if(0>=cube) {\r
+        cube = gl.glGenLists(1);\r
+        gl.glNewList(cube, GL2.GL_COMPILE);\r
+        gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, brown, 0);\r
+        cube(gl);\r
+        gl.glEndList();\r
+        System.err.println("cube list created: "+cube);\r
+    } else {\r
+        System.err.println("cube list reused: "+cube);\r
+    }\r
+    \r
+    if(0>=box) {\r
+       box = gl.glGenLists(1);\r
+        gl.glNewList(box, GL2.GL_COMPILE);\r
+        gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, brown, 0);\r
+        box(gl);\r
+        gl.glEndList();\r
+        System.err.println("box list created: "+box);\r
+    } else {\r
+        System.err.println("box list reused: "+box);\r
+    }\r
+                   \r
+    if(0>=mydesk) {\r
+       mydesk = gl.glGenLists(1);\r
+        gl.glNewList(mydesk, GL2.GL_COMPILE);\r
+        gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, red, 0);\r
+        desk(gl, 2);\r
+        gl.glEndList();\r
+        System.err.println("mydesk list created: "+mydesk);\r
+    } else {\r
+        System.err.println("mydesk list reused: "+mydesk);\r
+    }\r
+                   \r
+    if(0>=endesk) {\r
+       endesk = gl.glGenLists(1);\r
+        gl.glNewList(endesk, GL2.GL_COMPILE);\r
+        gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, blue, 0);\r
+        desk(gl, 3);\r
+        gl.glEndList();\r
+        System.err.println("endesk list created: "+endesk);\r
+    } else {\r
+        System.err.println("endesk list reused: "+endesk);\r
+    }\r
+    \r
+    if(0>=ball) {\r
+       ball = gl.glGenLists(1);\r
+        gl.glNewList(ball, GL2.GL_COMPILE);\r
+        gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, yellow, 0);\r
+        ball(gl);\r
+        gl.glEndList();\r
+        System.err.println("ball list created: "+ball);\r
+    } else {\r
+        System.err.println("ball list reused: "+ball);\r
+    }\r
+                   \r
+    gl.glEnable(GL2.GL_NORMALIZE);\r
+                       \r
+    MouseListener tennisMouse = new TennisMouseAdapter();    \r
+    KeyListener tennisKeys = new TennisKeyAdapter();\r
+\r
+    if (drawable instanceof Window) {\r
+        Window window = (Window) drawable;\r
+        window.addMouseListener(tennisMouse);\r
+        window.addKeyListener(tennisKeys);\r
+    } else if (GLProfile.isAWTAvailable() && drawable instanceof java.awt.Component) {\r
+        java.awt.Component comp = (java.awt.Component) drawable;\r
+        new AWTMouseAdapter(tennisMouse).addTo(comp);\r
+        new AWTKeyAdapter(tennisKeys).addTo(comp);\r
+    }          \r
+  }\r
+\r
+  public void dispose(GLAutoDrawable drawable) {\r
+       System.err.println("Tennis: Dispose");\r
+  }\r
+\r
+  public void display(GLAutoDrawable drawable) {\r
+\r
+    // Get the GL corresponding to the drawable we are animating\r
+    GL2 gl = drawable.getGL().getGL2();\r
+    \r
+    if (mouseButtonDown == false && control == true)\r
+    MovMydesk();\r
+    MoveSphere();\r
+    MoveEnDesk();\r
+\r
+    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
+\r
+    // Special handling for the case where the GLJPanel is translucent\r
+    // and wants to be composited with other Java 2D content\r
+    if (GLProfile.isAWTAvailable() && \r
+        (drawable instanceof javax.media.opengl.awt.GLJPanel) &&\r
+        !((javax.media.opengl.awt.GLJPanel) drawable).isOpaque() &&\r
+        ((javax.media.opengl.awt.GLJPanel) drawable).shouldPreserveColorBufferIfTranslucent()) {\r
+      gl.glClear(GL2.GL_DEPTH_BUFFER_BIT);\r
+    } else {\r
+      gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);\r
+    }\r
+    \r
+ // Place the box and call its display list\r
+    gl.glDisable(GL2.GL_DEPTH_TEST);\r
+    gl.glCallList(box);\r
+    gl.glEnable(GL2.GL_DEPTH_TEST);\r
+                   \r
+    // Rotate the entire assembly of tennis based on how the user\r
+    // dragged the mouse around\r
+    gl.glPushMatrix();\r
+    gl.glRotatef(view_rotx, 1.0f, 0.0f, 0.0f);\r
+    gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f);\r
+    gl.glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);\r
+            \r
+    // Place the cube and call its display list\r
+    gl.glPushMatrix();\r
+    gl.glTranslatef(0.0f, 0.0f, 0.0f);\r
+    gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f);\r
+    gl.glCallList(cube);\r
+    gl.glPopMatrix();\r
+                   \r
+    // Place the mydesk and call its display list\r
+    gl.glPushMatrix();\r
+    gl.glTranslatef(sx, sy, 3.0f);\r
+    gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f);\r
+    gl.glCallList(mydesk);\r
+    gl.glPopMatrix();\r
+            \r
+    // Place the endesk and call its display list\r
+    gl.glPushMatrix();\r
+    gl.glTranslatef(EnDeskCx, EnDeskCy, -3.0f);\r
+    gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f);\r
+    gl.glCallList(endesk);\r
+    gl.glPopMatrix();\r
+    \r
+    // Place the ball and call its display list\r
+    gl.glPushMatrix();\r
+    gl.glTranslatef(BallCx, BallCy, BallCz);\r
+    gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f);\r
+    gl.glCallList(ball);\r
+    gl.glPopMatrix();\r
+                   \r
+    // Remember that every push needs a pop; this one is paired with\r
+    // rotating the entire tennis assembly\r
+    gl.glPopMatrix();\r
+  }\r
+\r
+  public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) \r
+  {\r
+         \r
+       System.err.println("Gears: Reshape "+x+"/"+y+" "+width+"x"+height);\r
+       GL2 gl = drawable.getGL().getGL2();\r
+\r
+       gl.setSwapInterval(swapInterval);\r
+\r
+       float h = (float)height / (float)width;\r
+       \r
+       WindowW = width;\r
+       WindowH = height;\r
+                   \r
+       gl.glMatrixMode(GL2.GL_PROJECTION);\r
+\r
+       gl.glLoadIdentity();\r
+       \r
+       if (h<1)\r
+               gl.glFrustum(-1.0f, 1.0f, -h, h, 1.0f, 60.0f);\r
+       else\r
+       {\r
+               h = 1.0f/h;\r
+               gl.glFrustum(-h, h, -1.0f, 1.0f, 1.0f, 60.0f);\r
+       }\r
+       \r
+       gl.glMatrixMode(GL2.GL_MODELVIEW);\r
+       gl.glLoadIdentity();\r
+       gl.glTranslatef(0.0f, 0.0f, -6.0f);\r
+       \r
+  }\r
+  \r
+  public static void cube(GL2 gl)\r
+  {\r
+\r
+       gl.glShadeModel(GL2.GL_FLAT);\r
+\r
+       /* draw left sides */\r
+       gl.glBegin(GL2.GL_QUADS);\r
+       \r
+               gl.glNormal3f(1.0f, 0.0f, 0.0f);\r
+               gl.glVertex3f(-2.0f, -1.5f, -3.0f);\r
+               gl.glVertex3f(-2.0f,  1.5f, -3.0f);\r
+               gl.glVertex3f(-2.0f,  1.5f,  3.0f);\r
+               gl.glVertex3f(-2.0f, -1.5f,  3.0f);\r
+               \r
+               gl.glNormal3f(-1.0f, 0.0f, 0.0f);\r
+               gl.glVertex3f(-2.05f, -1.55f, -3.0f);\r
+               gl.glVertex3f(-2.05f,  1.55f, -3.0f);\r
+               gl.glVertex3f(-2.05f,  1.55f,  3.0f);\r
+               gl.glVertex3f(-2.05f, -1.55f,  3.0f);\r
+\r
+       gl.glEnd();\r
+       \r
+       if (texture[0] != null) {\r
+             texture[0].enable(gl);\r
+             texture[0].bind(gl);\r
+             gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);\r
+             TextureCoords coords = texture[0].getImageTexCoords();\r
+       \r
+       /* draw up sides */\r
+       gl.glBegin(GL2.GL_QUADS);\r
+       \r
+               gl.glNormal3f(0.0f, -1.0f, 0.0f);\r
+               gl.glTexCoord2f(coords.left(), coords.top());\r
+               gl.glVertex3f(-2.0f, 1.5f, -3.0f);\r
+               gl.glTexCoord2f(coords.left(), coords.bottom());\r
+               gl.glVertex3f(-2.0f, 1.5f,  3.0f);\r
+               gl.glTexCoord2f(coords.right(), coords.bottom());\r
+               gl.glVertex3f( 2.0f, 1.5f,  3.0f);\r
+               gl.glTexCoord2f(coords.right(), coords.top());\r
+               gl.glVertex3f( 2.0f, 1.5f, -3.0f);\r
+               \r
+       gl.glEnd();\r
+       \r
+               texture[0].disable(gl);\r
+       }       \r
+               \r
+       gl.glBegin(GL2.GL_QUADS);\r
+       \r
+               gl.glNormal3f(0.0f, 1.0f, 0.0f);\r
+               gl.glVertex3f(-2.05f, 1.55f, -3.0f);\r
+               gl.glVertex3f(-2.05f, 1.55f,  3.0f);\r
+               gl.glVertex3f( 2.05f, 1.55f,  3.0f);\r
+               gl.glVertex3f( 2.05f, 1.55f, -3.0f);\r
+\r
+       gl.glEnd();\r
+       \r
+       /* draw right sides */\r
+       gl.glBegin(GL2.GL_QUADS);\r
+       \r
+               gl.glNormal3f(-1.0f, 0.0f, 0.0f);\r
+               gl.glVertex3f(2.0f, -1.5f, -3.0f);\r
+               gl.glVertex3f(2.0f,  1.5f, -3.0f);\r
+               gl.glVertex3f(2.0f,  1.5f,  3.0f);\r
+               gl.glVertex3f(2.0f, -1.5f,  3.0f);\r
+               \r
+               gl.glNormal3f(1.0f, 0.0f, 0.0f);\r
+               gl.glVertex3f(2.05f, -1.55f, -3.0f);\r
+               gl.glVertex3f(2.05f,  1.55f, -3.0f);\r
+               gl.glVertex3f(2.05f,  1.55f,  3.0f);\r
+               gl.glVertex3f(2.05f, -1.55f,  3.0f);\r
+\r
+       gl.glEnd();\r
+       \r
+       if (texture[1] != null) {\r
+             texture[1].enable(gl);\r
+             texture[1].bind(gl);\r
+             gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);\r
+             TextureCoords coords = texture[1].getImageTexCoords();\r
+       \r
+       /* draw down sides */\r
+       gl.glBegin(GL2.GL_QUADS);\r
+       \r
+               gl.glNormal3f(0.0f, 1.0f, 0.0f);\r
+               gl.glTexCoord2f(coords.left(), coords.top());\r
+               gl.glVertex3f(-2.0f, -1.5f, -3.0f);\r
+               gl.glTexCoord2f(coords.left(), coords.bottom());\r
+               gl.glVertex3f(-2.0f, -1.5f,  3.0f);\r
+               gl.glTexCoord2f(coords.right(), coords.bottom());\r
+               gl.glVertex3f( 2.0f, -1.5f,  3.0f);\r
+               gl.glTexCoord2f(coords.right(), coords.top());\r
+               gl.glVertex3f( 2.0f, -1.5f, -3.0f);\r
+               \r
+               gl.glNormal3f(0.0f, -1.0f, 0.0f);\r
+               gl.glVertex3f(-2.05f, -1.55f, -3.0f);\r
+               gl.glVertex3f(-2.05f, -1.55f,  3.0f);\r
+               gl.glVertex3f( 2.05f, -1.55f,  3.0f);\r
+               gl.glVertex3f( 2.05f, -1.55f, -3.0f);\r
+\r
+       gl.glEnd();\r
+       \r
+               texture[1].disable(gl);\r
+       }\r
+       \r
+       /* draw back sides */\r
+       gl.glBegin(GL2.GL_QUADS);\r
+       \r
+               gl.glNormal3f(0.0f, 0.0f, -1.0f);\r
+               \r
+               gl.glVertex3f(-2.05f, 1.55f, -3.0f);\r
+               gl.glVertex3f( 2.05f, 1.55f, -3.0f);\r
+               gl.glVertex3f( 2.0f, 1.5f, -3.0f);\r
+               gl.glVertex3f(-2.0f, 1.5f, -3.0f);\r
+               \r
+               gl.glVertex3f(-2.05f, -1.55f, -3.0f);\r
+               gl.glVertex3f( 2.05f, -1.55f, -3.0f);\r
+               gl.glVertex3f( 2.0f, -1.5f, -3.0f);\r
+               gl.glVertex3f(-2.0f, -1.5f, -3.0f);\r
+               \r
+               gl.glVertex3f(-2.05f, -1.55f, -3.0f);\r
+               gl.glVertex3f(-2.05f, 1.55f, -3.0f);\r
+               gl.glVertex3f(-2.0f, 1.5f, -3.0f);\r
+               gl.glVertex3f(-2.0f, -1.5f, -3.0f);\r
+               \r
+               gl.glVertex3f(2.05f, -1.55f, -3.0f);\r
+               gl.glVertex3f(2.05f, 1.55f, -3.0f);\r
+               gl.glVertex3f(2.0f, 1.5f, -3.0f);\r
+               gl.glVertex3f(2.0f, -1.5f, -3.0f);\r
+\r
+       gl.glEnd();\r
+       \r
+       /* draw front sides */\r
+       gl.glBegin(GL2.GL_QUADS);\r
+       \r
+               gl.glNormal3f(0.0f, 0.0f, 1.0f);\r
+               \r
+               gl.glVertex3f(-2.05f, 1.55f, 3.0f);\r
+               gl.glVertex3f( 2.05f, 1.55f, 3.0f);\r
+               gl.glVertex3f( 2.0f, 1.5f, 3.0f);\r
+               gl.glVertex3f(-2.0f, 1.5f, 3.0f);\r
+               \r
+               gl.glVertex3f(-2.05f, -1.55f, 3.0f);\r
+               gl.glVertex3f( 2.05f, -1.55f, 3.0f);\r
+               gl.glVertex3f( 2.0f, -1.5f, 3.0f);\r
+               gl.glVertex3f(-2.0f, -1.5f, 3.0f);\r
+               \r
+               gl.glVertex3f(-2.05f, -1.55f, 3.0f);\r
+               gl.glVertex3f(-2.05f, 1.55f, 3.0f);\r
+               gl.glVertex3f(-2.0f, 1.5f, 3.0f);\r
+               gl.glVertex3f(-2.0f, -1.5f, 3.0f);\r
+               \r
+               gl.glVertex3f(2.05f, -1.55f, 3.0f);\r
+               gl.glVertex3f(2.05f, 1.55f, 3.0f);\r
+               gl.glVertex3f(2.0f, 1.5f, 3.0f);\r
+               gl.glVertex3f(2.0f, -1.5f, 3.0f);\r
+\r
+       gl.glEnd();\r
+        \r
+  }\r
+  \r
+  public static void box(GL2 gl)       //Usually "box" mean box, but there only one side is enough\r
+  {\r
+         \r
+       gl.glShadeModel(GL2.GL_FLAT);\r
+       \r
+       if (texture[4] != null) {\r
+             texture[4].enable(gl);\r
+             texture[4].bind(gl);\r
+             gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);\r
+             gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);\r
+             gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);\r
+       \r
+       /* draw the side */\r
+       gl.glBegin(GL2.GL_QUADS);\r
+       \r
+               gl.glNormal3f(0.0f, 0.0f, 1.0f);\r
+               gl.glTexCoord2f(0, 0);\r
+               gl.glVertex3f(-8.0f, -8.0f, 0.0f);\r
+               gl.glTexCoord2f(0, 8.0f);\r
+               gl.glVertex3f(-8.0f, 8.0f,  0.0f);\r
+               gl.glTexCoord2f(8.0f, 8.0f);\r
+               gl.glVertex3f( 8.0f, 8.0f,  0.0f);\r
+               gl.glTexCoord2f(8.0f, 0);\r
+               gl.glVertex3f( 8.0f, -8.0f, 0.0f);\r
+\r
+       gl.glEnd();\r
+       \r
+               texture[4].disable(gl);\r
+       }\r
+        \r
+  }\r
+  \r
+  public static void desk(GL2 gl, int two_or_three)\r
+  {\r
+         \r
+       int i;\r
+       float temp1;\r
+\r
+       gl.glShadeModel(GL2.GL_FLAT);\r
+\r
+       if (texture[two_or_three] != null) {\r
+             texture[two_or_three].enable(gl);\r
+             texture[two_or_three].bind(gl);\r
+             gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);\r
+             gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);\r
+             gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);\r
+       \r
+       /* draw the front */\r
+       gl.glBegin(GL2.GL_QUAD_STRIP);\r
+       \r
+               gl.glNormal3f(0.0f, 0.0f, 1.0f);\r
+               for (i=0; i<25; i++)\r
+               {\r
+                       temp1 = (float) Math.pow(Math.sin(i/24.0f*Math.PI), 0.4d);\r
+                        \r
+                       gl.glTexCoord2f((i-12)/40.0f, temp1/4 + 0.75f);\r
+                       gl.glVertex3f((i-12)/40.0f, temp1/10 + 0.1f , 0.01f + temp1/25);        \r
+                       gl.glTexCoord2f((i-12)/40.0f, -temp1/4 + 0.25f);\r
+                       gl.glVertex3f((i-12)/40.0f, -temp1/10 - 0.1f, 0.01f + temp1/25);        \r
+               }\r
+\r
+       gl.glEnd();\r
+       \r
+       /* draw the back */\r
+       gl.glBegin(GL2.GL_QUAD_STRIP);\r
+       \r
+               gl.glNormal3f(0.0f, 0.0f, -1.0f);\r
+               for (i=0; i<25; i++)\r
+               {\r
+                       temp1 = (float) Math.pow(Math.sin(i/24.0f*Math.PI), 0.4d);\r
+                       \r
+                       gl.glTexCoord2f((i-12)/40.0f, temp1/4 + 0.75f);\r
+                       gl.glVertex3f((i-12)/40.0f, temp1/10 + 0.1f , -0.01f - temp1/25);       \r
+                       gl.glTexCoord2f((i-12)/40.0f, -temp1/4 + 0.25f);\r
+                       gl.glVertex3f((i-12)/40.0f, -temp1/10 - 0.1f, -0.01f - temp1/25);       \r
+                       \r
+               }\r
+\r
+       gl.glEnd();\r
+       \r
+       texture[2].disable(gl);\r
+       }\r
+       \r
+       /* draw the top side */\r
+       gl.glBegin(GL2.GL_QUAD_STRIP);\r
+       \r
+               gl.glNormal3f(0.0f, 1.0f, 0.0f);\r
+               for (i=0; i<25; i++)\r
+               {\r
+                       temp1 = (float) Math.pow(Math.sin(i/24.0f*Math.PI), 0.4d);\r
+                       \r
+                       gl.glVertex3f((i-12)/40.0f, temp1/10 + 0.1f , -0.01f - temp1/25);                                       \r
+                       gl.glVertex3f((i-12)/40.0f, temp1/10 + 0.1f , 0.01f + temp1/25);        \r
+               }\r
+               \r
+       gl.glEnd();\r
+               \r
+       /* draw the bottom side */\r
+       gl.glBegin(GL2.GL_QUAD_STRIP);\r
+                       \r
+               gl.glNormal3f(0.0f, -1.0f, 0.0f);\r
+               for (i=0; i<25; i++)\r
+               {       \r
+                       temp1 = (float) Math.pow(Math.sin(i/24.0f*Math.PI), 0.4d);\r
+                       \r
+                       gl.glVertex3f((i-12)/40.0f, -temp1/10 - 0.1f, 0.01f + temp1/25);                        \r
+                       gl.glVertex3f((i-12)/40.0f, -temp1/10 - 0.1f, -0.01f - temp1/25);       \r
+               }\r
+\r
+       gl.glEnd();\r
+       \r
+       /* draw the left and right sides */\r
+       gl.glBegin(GL2.GL_QUADS);\r
+                       \r
+                       gl.glNormal3f(-1.0f, 0.0f, 0.0f);\r
+                       gl.glVertex3f((-12)/40.0f, -0.1f, 0.01f);\r
+                       gl.glVertex3f((-12)/40.0f, +0.1f, 0.01f);                       \r
+                       gl.glVertex3f((-12)/40.0f, +0.1f, -0.01f);\r
+                       gl.glVertex3f((-12)/40.0f, -0.1f, -0.01f);      \r
+                       \r
+                       gl.glNormal3f(1.0f, 0.0f, 0.0f);\r
+                       gl.glVertex3f((+12)/40.0f, -0.1f, 0.01f);\r
+                       gl.glVertex3f((+12)/40.0f, +0.1f, 0.01f);                       \r
+                       gl.glVertex3f((+12)/40.0f, +0.1f, -0.01f);\r
+                       gl.glVertex3f((+12)/40.0f, -0.1f, -0.01f);                      \r
+\r
+\r
+       gl.glEnd();\r
+       \r
+  }\r
+  \r
+  public static void ball(GL2 gl)\r
+  {\r
+         \r
+       int i,j;\r
+       float y1,y2,r1,r2,x,z;\r
+\r
+       gl.glShadeModel(GL2.GL_FLAT);\r
+\r
+       /* draw the ball */\r
+       gl.glBegin(GL2.GL_QUAD_STRIP);\r
+       \r
+               for (i=0; i<20; i++)\r
+               {\r
+                       y1 = (float) Math.cos((i)/20.0f*Math.PI)/10;\r
+                       y2 = (float) Math.cos((i+1)/20.0f*Math.PI)/10;\r
+                       r1 = (float) Math.sqrt(Math.abs(0.01f-y1*y1));\r
+                       r2 = (float) Math.sqrt(Math.abs(0.01f-y2*y2));\r
+                       \r
+                       for (j=0; j<21; j++)\r
+                       {\r
+                               x = (float) (r1*Math.cos((float)j/21*2.0f*Math.PI));\r
+                               z = (float) (r1*Math.sin((float)j/21*2.0f*Math.PI));\r
+                               gl.glNormal3f(10*x, 10*y1, 10*z);\r
+                               gl.glVertex3f(x, y1, z);\r
+                               \r
+                               x = (float) (r2*Math.cos((float)j/21*2.0f*Math.PI));\r
+                               z = (float) (r2*Math.sin((float)j/21*2.0f*Math.PI));\r
+                               gl.glNormal3f(10*x, 10*y2, 10*z);\r
+                               gl.glVertex3f(x, y2, z);\r
+                       }\r
+               }\r
+\r
+       gl.glEnd();\r
+       \r
+  }\r
+  \r
+  public void MoveSphere()\r
+  {\r
+         \r
+       // Ball out\r
+\r
+               if ((BallCz>3)||(BallCz<-3))\r
+               {\r
+                       \r
+                       Vec=BallCz;\r
+                       \r
+                       BallCx = 0.0f;\r
+                       BallCy = 0.0f;\r
+                       BallCz = 0.0f;\r
+\r
+                       spz=0;\r
+                       spx=0;\r
+                       spy=0;\r
+\r
+                       CanF=false;\r
+                       \r
+                       Bax=0;\r
+                       Bay=0;\r
+\r
+               }\r
+\r
+       // Ball rebound\r
+\r
+               if ((spz<0)&&(BallCz+spz<-2.8)&&(BallCx+spx<EnDeskCx+0.3)&&(BallCx+spx>EnDeskCx-0.3)&&(BallCy+spy<EnDeskCy+0.2)&&(BallCy+spy>EnDeskCy-0.2))\r
+               {\r
+               \r
+                       spz=-spz+0.002f;\r
+                       spx=spx+(BallCx-EnDeskCx)/10;\r
+                       spy=spy+(BallCy-EnDeskCy)/10;   \r
+               \r
+               }\r
+\r
+               if ((spz>0)&&(BallCz+spz>2.8)&&(BallCx+spx<sx+0.3)&&(BallCx+spx>sx-0.3)&&(BallCy+spy<sy+0.2)&&(BallCy+spy>sy-0.2))\r
+               {\r
+                       \r
+                       spz=-spz-0.002f;\r
+                       spx=spx+(BallCx-sx)/10;\r
+                       spy=spy+(BallCy-sy)/10;\r
+                       \r
+                       Bax=DspeedX/100;\r
+                       Bay=DspeedY/100;\r
+\r
+               }\r
+\r
+               if ((BallCx+spx<-1.9)||(BallCx+spx>1.9))\r
+               spx=-spx;\r
+\r
+               if ((BallCy+spy<-1.4)||(BallCy+spy>1.4))\r
+               spy=-spy;\r
+\r
+       // Ball acceleration\r
+\r
+               spx=spx+Bax;\r
+               spy=spy+Bay;\r
+\r
+       // Ball move\r
+\r
+           if (CanF==true)\r
+               \r
+           BallCx += spx;\r
+               BallCy += spy;\r
+               BallCz += spz;\r
+               \r
+       //Less the acceleration\r
+\r
+               Bax=Bax-Bax/100;\r
+               Bay=Bay-Bay/100;\r
+         \r
+  }\r
+  \r
+  public void MoveEnDesk()\r
+  {\r
+\r
+         //Just follow for the ball\r
+         \r
+  float sx,sy;\r
+  double gip=Math.sqrt((BallCx-EnDeskCx)*(BallCx-EnDeskCx)+(BallCy-EnDeskCy)*(BallCy-EnDeskCy));\r
+\r
+  if (gip<0.07)\r
+  {\r
+  sx=Math.abs((BallCx-EnDeskCx));\r
+  sy=Math.abs((BallCy-EnDeskCy));\r
+  }\r
+  else\r
+  {\r
+  sx=Math.abs((BallCx-EnDeskCx))/((float) gip)*0.07f;\r
+  sy=Math.abs((BallCy-EnDeskCy))/((float) gip)*0.07f;\r
+  }\r
+\r
+  if ((BallCx-EnDeskCx>0)&&(EnDeskCx+sx<=1.7)) \r
+         EnDeskCx += sx;\r
+\r
+  if ((BallCx-EnDeskCx<0)&&(EnDeskCx-sx>=-1.7))\r
+         EnDeskCx -= sx;\r
+\r
+  if ((BallCy-EnDeskCy>0)&&(EnDeskCy+sy<=1.3))\r
+         EnDeskCy += sy;\r
+\r
+  if ((BallCy-EnDeskCy<0)&&(EnDeskCy-sy>=-1.3))\r
+         EnDeskCy -= sy;\r
+\r
+  }\r
+  \r
+  public void MovMydesk() {\r
+         \r
+         LPositionDX = sx;\r
+         LPositionDY = sy;\r
+         \r
+         int x = MouseInfo.getPointerInfo().getLocation().x; \r
+      int y = MouseInfo.getPointerInfo().getLocation().y;\r
+\r
+      sx = sx + (float)(x-500.0f)/300.0f;\r
+      sy = sy + (float)(400.0f-y)/300.0f;  \r
+      \r
+      //Check cube borders\r
+      \r
+      if (sx<-1.7f || sx>1.7f)\r
+       { \r
+         if (sx>0) sx = 1.7f;\r
+         else     sx = -1.7f;\r
+       }\r
+      \r
+      if (sy<-1.3 || sy>1.3)\r
+       { \r
+         if (sy>0) sy = 1.3f;\r
+         else     sy = -1.3f;\r
+       }\r
+       \r
+      //Return the mouse back from screen borders\r
+       try {\r
+          Robot r = new Robot();\r
+          r.mouseMove(500,400);\r
+               } catch(AWTException ex) {}\r
+\r
+       NPositionDX=sx;\r
+       NPositionDY=sy;\r
+\r
+       DspeedX=NPositionDX-LPositionDX;\r
+       DspeedY=NPositionDY-LPositionDY;      \r
+      \r
+    }\r
+  \r
+  class TennisKeyAdapter extends KeyAdapter {      \r
+           public void keyPressed(KeyEvent e) {\r
+               int kc = e.getKeyCode();\r
+               if(KeyEvent.VK_ESCAPE == kc) {\r
+                   System.exit(0);\r
+               } \r
+               if(KeyEvent.VK_CONTROL == kc) {\r
+                   control = false;\r
+               } \r
+               if(KeyEvent.VK_SPACE == kc) {           //Ready for play\r
+                       if (CanF==false)\r
+                       {       \r
+                               if (Vec<0)\r
+                                       spz=-0.07f;                     \r
+                               else\r
+                                       spz=0.07f;\r
+                       }\r
+                       CanF=true;\r
+               }\r
+           }\r
+           \r
+           public void keyReleased(KeyEvent e) {    //Give the mouse control to the user    \r
+               int kc = e.getKeyCode();\r
+               if(KeyEvent.VK_CONTROL == kc) {\r
+                   control = true;\r
+               }               \r
+           }\r
+           \r
+         }\r
+         \r
+  class TennisMouseAdapter extends MouseAdapter {\r
+      public void mousePressed(MouseEvent e) {\r
+        prevMouseX = e.getX();\r
+        prevMouseY = e.getY();\r
+        \r
+        mouseButtonDown = true;\r
+      }\r
+               \r
+      public void mouseReleased(MouseEvent e) {\r
+          mouseButtonDown = false;\r
+      }\r
+               \r
+      public void mouseDragged(MouseEvent e) {\r
+         \r
+        int x = e.getX();\r
+        int y = e.getY();\r
+        \r
+        float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)WindowW);\r
+        float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)WindowH);\r
+               \r
+        prevMouseX = x;\r
+        prevMouseY = y;       \r
+        \r
+        view_rotx += thetaX;\r
+        view_roty += thetaY;\r
+      }\r
+  }\r
+  \r
+}\r
http://JogAmp.org git info: FAQ, tutorial and man pages.