java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4095
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use gluUnProject in JOGL E-mail
User Rating: / 19
PoorBest 

This program demonstrates gluUnProject by reading the mouse position and determining the three-dimensional points at the near and far clipping planes from which it was transformed. The computed world coordinates are printed to standard output, but the rendered window itself is just black.

The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").


Image

-> Copyright and Permission Notice

package glredbook11;

import java.awt.event.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import javax.swing.*;

/**
 * When the left mouse button is pressed, this program reads the mouse position
 * and determines two 3D points from which it was transformed. Very little is
 * displayed.
 
 @author Kiet Le (Java port)
 */
public class unproject
  extends JFrame
    implements GLEventListener, KeyListener, MouseListener
{
  private GLU glu;
  private GLCapabilities caps;
  private GLCanvas canvas;
  private MouseEvent mouse;


  public unproject()
  {
    super("unproject");

    caps = new GLCapabilities();
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);
    canvas.addMouseListener(this);

    getContentPane().add(canvas);
  }

  public void run()
  {
    setSize(500500);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    canvas.requestFocusInWindow();
  }

  public static void main(String[] args)
  {
    new unproject().run();
  }

  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    glu = new GLU();    
  }

  public void display(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
   
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);

    int viewport[] new int[4];
    double mvmatrix[] new double[16];
    double projmatrix[] new double[16];
    int realy = 0;// GL y coord pos
    double wcoord[] new double[4];// wx, wy, wz;// returned xyz coords
    
    if (mouse != null)
    {
      int x = mouse.getX(), y = mouse.getY();
      switch (mouse.getButton()) {
        case MouseEvent.BUTTON1:
          gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
          gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix, 0);
          gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix, 0);
          /* note viewport[3] is height of window in pixels */
          realy = viewport[3(inty - 1;
          System.out.println("Coordinates at cursor are (" + x + ", " + realy);
          glu.gluUnProject((doublex, (doublerealy, 0.0//
              mvmatrix, 0,
              projmatrix, 0
              viewport, 0
              wcoord, 0);
          System.out.println("World coords at z=0.0 are ( " //
                             + wcoord[0", " + wcoord[1", " + wcoord[2]
                             ")");
          glu.gluUnProject((doublex, (doublerealy, 1.0//
              mvmatrix, 0,
              projmatrix, 0,
              viewport, 0
              wcoord, 0);
          System.out.println("World coords at z=1.0 are (" //
                             + wcoord[0", " + wcoord[1", " + wcoord[2]
                             ")");
          break;
        case MouseEvent.BUTTON2:
          break;
        default:
          break;
      }
    }

    gl.glFlush();
  }

  /* Change these values for a different transformation */
  public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
  {
    GL gl = drawable.getGL();
    
    gl.glViewport(00, w, h);
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(45.0(floatw / (floath, 1.0100.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
  }

  public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
      boolean deviceChanged)
  {
  }

  public void keyTyped(KeyEvent key)
  {
  }

  public void keyPressed(KeyEvent key)
  {

    switch (key.getKeyCode()) {
      case KeyEvent.VK_ESCAPE:
        System.exit(0);
        break;

      default:
        break;
    }

  }

  public void keyReleased(KeyEvent key)
  {
  }

  public void mouseClicked(MouseEvent e)
  {
  }

  public void mousePressed(MouseEvent e)
  {
    mouse = e;
    canvas.display();
  }

  public void mouseReleased(MouseEvent e)
  {
  }

  public void mouseEntered(MouseEvent e)
  {
  }

  public void mouseExited(MouseEvent e)
  {
  }

}

Source: Kiet Le's The Red Book Examples using JOGL


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.