Java JOGL2和鼠标监听器

时间:2012-03-08 18:13:08

标签: opengl jogl

我一直在使用JOGL2和java,我真的遇到了鼠标事件监听器的问题。如果我使用public void mouseDragged(MouseEvent e),我可以让鼠标监听器更新其x和y位置,但它不会在public void display(GLAutoDrawable gLDrawable)上重绘

以下是我的代码的工作原理: 这是构建窗口和监听器的主要方法

 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;

 import javax.media.opengl.GL2;
 import javax.media.opengl.GLCapabilities;
 import javax.media.opengl.GLEventListener;
 import javax.media.opengl.GLProfile;
 import javax.media.opengl.awt.GLCanvas;
 import javax.swing.JFrame;

 import javax.media.opengl.GLAutoDrawable;

 public class HelloWorld 
 {
     public static void main(String[] args) 
     {
      // setup OpenGL Version 2
    GLProfile profile = GLProfile.get(GLProfile.GL2);
    GLCapabilities capabilities = new GLCapabilities(profile);
    Renderer render = new Renderer();

    // The canvas is the widget that's drawn in the JFrame
    GLCanvas glcanvas = new GLCanvas(capabilities);
    glcanvas.addGLEventListener(render);
    glcanvas.addMouseListener(render);
    glcanvas.addMouseMotionListener(render);
    glcanvas.setSize( 800, 600 );

      JFrame frame = new JFrame( "Graphics Demo" );

      frame.getContentPane().add( glcanvas);

     // shutdown the program on windows close event
     frame.addWindowListener(new WindowAdapter() 
     {
          public void windowClosing(WindowEvent ev) {
            System.exit(0);
         }
     });

    frame.setSize( frame.getContentPane().getPreferredSize() );
    frame.setVisible( true );
   }
 }

这是我的OpenGL渲染类,它只渲染一帧,除非我出于某种原因调整窗口大小,否则它不会重新渲染。如果我决定使用鼠标侦听器,它将更新值但不会更新渲染。这是为什么?

 import java.io.*;
 import javax.imageio.ImageIO;
 import javax.media.opengl.GL2;
 import javax.media.opengl.GLAutoDrawable;
 import javax.media.opengl.GLEventListener;
 import javax.media.opengl.glu.GLU;
 import java.awt.Dimension;
 import java.awt.Frame;
 import java.awt.event.MouseEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import javax.media.opengl.GLAutoDrawable;
 import javax.media.opengl.GLCapabilities;
 import javax.media.opengl.GLEventListener;
 import javax.media.opengl.GLProfile;
 import javax.media.opengl.awt.GLCanvas;
 import com.jogamp.opengl.util.FPSAnimator;

 import javax.media.opengl.glu.GLUquadric;

 import java.awt.Graphics2D;
 import java.awt.color.ColorSpace; 
 import java.awt.image.BufferedImage;
 import java.awt.image.ComponentColorModel;
 import java.awt.image.DataBuffer;
 import java.awt.image.DataBufferByte;
 import java.awt.image.Raster;
 import java.awt.image.WritableRaster;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.awt.event.*;

 class Renderer implements GLEventListener, MouseListener, MouseMotionListener
 {

    private GLU glu = new GLU();
    float x = 1;
    float y = 1;
    float z = 1;

    private GLUquadric quadric;
    GLCanvas canvas;

public void init(GLAutoDrawable gLDrawable) 
{
    System.out.println("INIT CALLED");
}

public void display(GLAutoDrawable gLDrawable) 
{

    final GL2 gl = gLDrawable.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

    /*MODELS LOADED HERE*/
    gl.glLoadIdentity();
    gl.glTranslatef(0.0f, -9.0f, -150.0f); //-1.5f 0.0f -6.0f
    gl.glRotatef(15f+x, 45f+x, 0f+y,0f); //15 45 0 0 

    OBJloader playingField = new OBJloader("PlayingField.obj", "Crema_Timber.jpg");
    OBJloader coin = new OBJloader("coin.obj","early_shilling.jpg");
    OBJloader tower = new OBJloader("Tower1.obj","stone.jpg");
    OBJloader tower2 = new OBJloader("Tower2.obj","stone.jpg");

    /* 3D RENDERING COMPUTED HERE */
    playingField.DrawModel(gl);
    coin.DrawModel(gl);     
    tower.DrawModel(gl);
    tower2.DrawModel(gl);

 }




 public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) 
{
    System.out.println("displayChanged called");
}



public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) 
{
    System.out.println("reshape() called: x = "+x+", y = "+y+", width = "+width+", height = "+height);
     final GL2 gl = gLDrawable.getGL().getGL2();

     if (height <= 0)
     {
        height = 1;
     }

    final float h = (float) width / (float) height;

    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(45.0f, h, 1.0, 200.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}


 public void dispose(GLAutoDrawable arg0) 
 {
System.out.println("dispose() called");
 }



 @Override
 public void mouseClicked(MouseEvent arg0) 
 {  
     // TODO Auto-generated method stub
     System.out.println("Mouse Clicked");
 }


 @Override
 public void mouseEntered(MouseEvent arg0) 
 {
     // TODO Auto-generated method stub
     System.out.println("Mouse Entered Frame");
 }


 @Override
 public void mouseExited(MouseEvent arg0) 
 {
     // TODO Auto-generated method stub
     System.out.println("Mouse Exited Frame");
 }


 @Override
 public void mousePressed(MouseEvent arg0) 
 {
     // TODO Auto-generated method stub
     System.out.println("Mouse is pressed.");
 }


 @Override
 public void mouseReleased(MouseEvent arg0) 
 {
// TODO Auto-generated method stub
System.out.println("Mouse has been released");
 }

 //HERE IS THE PROBLEM, THE DISPLAY WILL NOT UPDATE THIS INPUT
 @Override
 public void mouseDragged(MouseEvent e) 
 {
// TODO Auto-generated method stub
System.out.println("Mouse is being dragged");
x = e.getX();
y = e.getY();

System.out.println("x = "+x);
System.out.println("y = "+y);   
 }


 @Override
 public void mouseMoved(MouseEvent arg0) 
 {
// TODO Auto-generated method stub
System.out.println("Mouse has moved.");
 }

 }

您可能会注意到某些功能表示: OBJloader SOMETHING =新OBJloader(“SOMETHING.obj”,“SOMETHING.jpg”);

我有另一个加载OBJ文件并对它们进行纹理化的类,它的绘图功能很简单,它只是读入数组中的数据并调用glDrawArrays(还要注意所有的导入,实际上稍后会使用它)。

我需要做的最后一件事就是找出一种让鼠标更新的方法

1 个答案:

答案 0 :(得分:0)

在你的HelloWorld课程中你应该开始一个动画师:

FPSAnimator animator = new FPSAnimator(canvas, 60);
animator.start();