Advertisement

Display function is not called continuously to update new camera position

Started by November 09, 2017 10:35 AM
1 comment, last by aeroKittens 7 years ago

I am trying to rotate my scene by rotating the camera at the press of 'x' key. The scene is rendered correctly but fails to rotate. I am using my own transformation matrix (for good reasons), and it does work fine (it actually rotates the camera position about the x-axis) as indicated in the output below the code.  But the camera position in the display() function is not updated. In fact the problem is the display() function is not not called continuously so the rendered scene doesn't  reflect the new camera position 

The print out below the code shows that the display function is not called again, because the  x0 in the  rotateAbtX(....) function is called and show the camera position changing, but the x1 in the display is called just once and never again so it is not updated

Why is this? How can I adjust the code to allow display function to be called continuously so camera position can be updated?  Thanks


public class Game extends JFrame implements GLEventListener, KeyListener  {
	private static final long serialVersionUID = 1L;
	final private int width = 800;
	final private int height = 600;
	int  right=-100, bottom=-100, top=100, left=100,       numOfUnits;
	 GLU glu= new GLU();
     List<CreateObjVertices> dataArray;
     ...
     ...

	public Game(  int units,  List<CreateObjVertices> vertXYZ  ) {
		super("Puzzle Game");
		Globals.camera = new Point3D(0.0f, 1.4f, 0.0f); 
		Globals.view = new Point3D(0.0f,  -1.0f, -3.0f);
		System.out.println( "x  "+Globals.camera.x+"  y  "+Globals.camera.y+"  z  "+Globals.camera.z );
		dataArray = vertXYZ;
		numOfUnits  =  units;
	 	t = new Transform3D();
	   	xAxis = new Vector3D(1,0,0);
	   	yAxis = new Vector3D(0,1,0);
	   	zAxis = new Vector3D(0,0,1);

		GLProfile profile = GLProfile.get(GLProfile.GL2);
		GLCapabilities capabilities = new GLCapabilities(profile);
		GLCanvas canvas = new GLCanvas(capabilities);
		
		canvas.addGLEventListener(this);
		canvas.addKeyListener(this);    
		canvas.setFocusable(true);  // To receive key event
		canvas.requestFocus();

		this.setName("Puzzle Game");
		this.getContentPane().add(canvas);		
	    this.setSize(width, height);
		this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(false);
		canvas.requestFocusInWindow();
	}
	
	public void play() {
	}
	
    @Override
    public void init(GLAutoDrawable drawable) {
    	GL2 gl = drawable.getGL().getGL2();
    	gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        gl.glClearDepthf(1.0f);            
        gl.glEnable(GL2.GL_DEPTH_TEST);   
        gl.glDepthFunc(GL2.GL_LEQUAL);   
        gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);  
        gl.glShadeModel(GL2.GL_SMOOTH);        
	    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY); 
	}

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width,  int height) {
    	GL2 gl = drawable.getGL().getGL2();
		if (height == 0) height = 1;  
		float aspect = (float)width / height;
		gl.glViewport(0, 0, width, height);
		   
		gl.glMatrixMode(GL2.GL_PROJECTION); 
		gl.glLoadIdentity();                 
		glu.gluPerspective( 45, aspect, 0.1f, 100.0f);
		
		 System.out.println( "x2  "+Globals.camera.x+"  y2  "+Globals.camera.y+"  z2  "+Globals.camera.z );
    }

    @Override
    public void display(GLAutoDrawable drawable) {
   	 	 GL2 gl = drawable.getGL().getGL2();
		 gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
		 gl.glMatrixMode(GL2.GL_MODELVIEW);             
		 gl.glLoadIdentity();      
		 glu.gluLookAt( Globals.camera.x, Globals.camera.y, Globals.camera.z,     Globals.view.x, Globals.view.y, Globals.view.z,    0.0f, 1.0f, 0.0f);
		 System.out.println( "x1  "+Globals.camera.x+"  y1  "+Globals.camera.y+"  z1  "+Globals.camera.z );
		 gl.glTranslatef(0.0f, 0.0f, -3.0f);
		 
         gl.glBegin(GL.GL_TRIANGLE_STRIP);
       
//=========================== START ===========================================================         
//        *************     DRAWING SCENE AND OBJECTS HERE   ***************
//============================= end ============================================================
       
		 gl.glFlush();
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {

    }
  
	@Override
	public void keyPressed(KeyEvent e) {
		if( e.getKeyChar() == 'x'){
			rotateAbtX( 1 );
		}
		if( e.getKeyChar() == 'X'){
			rotateAbtX( -1 );
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void keyTyped(KeyEvent e) {		
	}    
	
	 Transform3D t;
	 Vector3D xAxis, yAxis, zAxis;
	 float rotAng = 60.0f,  cosAngle = 0.0f;
   	 Vector3D normalAxisVec = new Vector3D(0,0,0), vecObj = new Vector3D(0,0,0), baseLineVec = new Vector3D(0,0,0);


	 public void rotateAbtX( int direction ) {
	
		t.rotateCamera( xAxis, rotAng*direction, Globals.view, 0, Globals.camera );

		System.out.println( "x0  "+Globals.camera.x+"  y0  "+Globals.camera.y+"  z0  "+Globals.camera.z );
	 }
}

 


x*  0.0  y*  1.4  z*  0.0
x2  0.0  y2  1.4  z2  0.0
x1  0.0  y1  1.4  z1  0.0
x0  0.0  y0  -2.3980765  z0  0.57846117
x0  0.0  y0  -4.7980766  z0  -2.4215393
x0  0.0  y0  -3.3999999  z0  -6.000001
x0  0.0  y0  0.39807725  z0  -6.578461
x0  0.0  y0  2.7980769  z0  -3.57846
x0  0.0  y0  1.3999994  z0  1.4305115E-6
x0  0.0  y0  -2.398078  z0  0.57846117
x0  0.0  y0  -4.7980776  z0  -2.4215407
x0  0.0  y0  -3.3999991  z0  -6.000002
x0  0.0  y0  0.39807856  z0  -6.578461
x0  0.0  y0  2.7980776  z0  -3.5784588
x0  0.0  y0  1.3999987  z0  2.3841858E-6
x0  0.0  y0  -2.3980794  z0  0.57846117
x0  0.0  y0  -4.798078  z0  -2.4215417

 

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

Fixed the problem.

I doubt this solution will be useful for anyone else, since as it seems  I'm probably the only one using JOGL here, but here it is anyway::

I never realised it - since I am porting my code from Android platform (OpenGL ES) - but JOGL,  unlike ES required


animator.start();

FPSAnimator animator = new FPSAnimator(canvas, FPS, true);

for the  display(GLAutoDrawable drawable) to be called continuously.  FPSAnimator requires the header  


com.jogamp.opengl.util.FPSAnimator;

Further information/tutorial can be found here https://www.ntu.edu.sg/home/ehchua/programming/opengl/JOGL2.0.html

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

This topic is closed to new replies.

Advertisement