Frame resizing problems

Hello everyone,

I recently started programming in OpenGL using JOGL and I am using NeHe’s tutorial as a reference.
There seems to be a problem after I reshape the Frame in which the canvas is located.
After I resize the frame, the reshape() method() is called, but the screen turns black (the background color).
the code is

public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

		GL gl = drawable.getGL();
		GLU glu = new GLU();
		
		gl.glViewport(x, y, width, height);
		
		// If the height is 0 set it to 1 to avoid division by zero
		if (height == 0) {
			
			height++;
		}
		
		// Calculate the aspect ratio
		float fovy = (float) width / (float) height;
		
		// Setting the matrix mode
		gl.glMatrixMode(GL.GL_PROJECTION);
		
		// Calculate perspective
		glu.gluPerspective(45.0D, fovy, 0.1D, 100D);

		// Setting ModelView matrix
		gl.glMatrixMode(GL.GL_MODELVIEW);
	}

My question is, should I put the GLCanvas directly inside the frame? and should I use the glOrth() method for any type of aspect ratio calculations?

Thanks in advance.

Not having used JOGL’s GLEventListener interfaces before, I don’t know if there’s anything wrong that could be causing your screen to blank on a Frame resize. But to answer your other questions:

  • I’m pretty sure the GLCanvas should go inside the frame. Examples I’ve seen always do something like frame.add(canvas)

  • I’m thinking you’ve misunderstood what glOrtho actually does. It doesn’t do any aspect ratio calculations, but rather sets-up the ‘camera’ for orthographic projection, ie: makes it so that faraway objects maintain the same width and height at any distance. But you can use your own aspect ratio calculations to set-up an orthographic projection so that it maintans an aspect ratio.

Well, I am adding the canvas to the frame directly like I did before.
But why does the screen turn black after the frame has been resized?
I’ve seen some code examples and they all use the same code, so what is the problem?

The example of reshape() I’m looking at is very similar except for 2 differences:

  • there is no attempt to change the size of the viewport (so no gl.glViewPort(…))
  • and after gl.glMatrixMode(GL_PROJECTION), there’s a gl.glLoadIdentity().

Maybe try the last one?

Yes, it was the gl.glLoadIdentity() after the gl.glMatrixMode(GL.GL_PROJECTION) line.

Thanks for the help!! ;D