Problem with NeHe lesson 1

I’ve been looking around the forums and checking some linkes to vairous NeHe
ports and samples, but I seem to be stuck with a strange problem.

I’ve written the following bits of code (this will look very familiar to many
of you):


@Override
public void display(GLAutoDrawable drawable) {
	gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
        gl.glLoadIdentity();
	        
        gl.glTranslatef(-1.5f, 0.0f, -6.0f);
	        
        gl.glBegin(GL.GL_TRIANGLES);
        gl.glVertex3f(0.0f, 1.0f, 0.0f);
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);
        gl.glVertex3f(1.0f, -1.0f, 0.0f);
        gl.glEnd();
	        
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
	        
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex3f(-1.0f, 1.0f, 0.0f);
        gl.glVertex3f(1.0f, 1.0f, 0.0f);
        gl.glVertex3f(1.0f, -1.0f, 0.0f);
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);
        gl.glEnd();
}


@Override
public void init(GLAutoDrawable drawable) {
	this.gl = drawable.getGL();
        this.glDrawable = drawable;
	
        drawable.setGL( new DebugGL(drawable.getGL() ));
	        
        System.out.println("Init GL is " + gl.getClass().getName());
}

I’m running this on Java 6 (Windows Vista), and I don’t get to see either the
triangle or the square. After some puzzling, I noticed the layout manager of
the canvas apparently resizes GL contents (thus, the 1.0f values are not in
any way ‘absolute’ values, but rather proportionate, where 1.0 is the full
width or height).

Is this normal behavior for a GLCanvas object? If so, I guess I should not be
using those for 3D scenes and objects that shouldn’t resize in any way other
than by changing camera distance. If it isn’t normal behavior, then what am I
doing wrong?

I would appreciate any hints or tips as to where I might find more appropriate
lessons for JOGL (it’s not very different from OpenGL, but just enough to make
NeHe’s tutorials tricky).

In your display mode, you may also want to explicitly manipulate the modelview matrix:


		GL gl = drawable.getGL();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glMatrixMode(GL.GL_MODELVIEW); // Select The Modelview Matrix
		gl.glLoadIdentity(); // Reset The Modelview Matrix

Whenever you resize, you need something like this:


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

		GL gl = drawable.getGL();
		GLU glu = new GLU();

		System.out.println("reshape to x=" + x + ", y=" + y + ", width=" + width + ", height=" + height);
		if (height == 0) // Prevent A Divide By Zero By
		{
			height = 1; // Making Height Equal One
		}

		//this is already done by the calling method, according to JOGL documentation
		//gl.glViewport(x,y,width,height);	// Reset The Current Viewport

		gl.glMatrixMode(GL.GL_PROJECTION); // Select The Projection Matrix
		gl.glLoadIdentity(); // Reset The Projection Matrix

		// Calculate The Aspect Ratio Of The Window
		//glu.gluPerspective(90.0f,(double)width/height,0.5f,500.0f);

        glu.gluOrtho2D(0, width, 0, height);
	}


Your “coordinate system” will depend very much on what you supply with the gluOrtho2D call. As the name says, this one is for an orthogonal projection into a plane. In other words: you’re drawing in 2D. There are similar calls for drawing in 3D.

Have fun :slight_smile:
I hope this gives you some pointers. Good luck :wink:

I’ve been playing around with the gluPerspective and gluOrtho functions, but
the situation I’m experiencing remains. Any translations beyond -0.75f and
+0.75f result in the triangle or square being outside of the canvas.

On top of that, adding the code demonstrated for the reshape method actually
disappears both the triangle and square. Changing the values for gluOrtho2D
and gluPerspective didn’t seem to have any effect. Obviously, I’m doing
something wrong here, but haven’t found out exactly what :stuck_out_tongue:

Anyway, I’ve found a load of NeHe examples ported to JOGL (by Kevin Duling),
allowing me to compare code and find out some of those tricky Java-versus-C
OpenGL issues. I’ll report any progress or other questions tomorrow :slight_smile:

Okay, I’ve figured out that I’ve misinterpreted the perspective thing the first
time around. I’ve taken a few lines from Duling’s lesson one code, and after
a little tweaking, I have at least got the triangle and square on screen with
the translation as per NeHe’s example.

I can’t readily find detailed information on function gluOrtho2D, so I’ll have
to keep that out for now. Whenever I add it, the triangle and square no longer
appear at all.

This is the code that runs fine at the moment:


@Override
public void display(GLAutoDrawable drawable) {
	gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
	gl.glMatrixMode(GL.GL_MODELVIEW); // Select the Model View matrix
        gl.glLoadIdentity();
	        
        gl.glTranslatef(-1.5f, 0.0f, -6.0f);
	        
        gl.glBegin(GL.GL_TRIANGLES);
        gl.glVertex3f(0.0f, 1.0f, 0.0f);
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);
        gl.glVertex3f(1.0f, -1.0f, 0.0f);
        gl.glEnd();
	        
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
	        
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex3f(-1.0f, 1.0f, 0.0f);
        gl.glVertex3f(1.0f, 1.0f, 0.0f);
        gl.glVertex3f(1.0f, -1.0f, 0.0f);
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);
        gl.glEnd();
}
		
@Override
public void displayChanged(GLAutoDrawable drawable, boolean arg1,
		boolean arg2) {
	//...
}
		
@Override
public void init(GLAutoDrawable drawable) {
	this.gl = drawable.getGL();
        this.glDrawable = drawable;
	        
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL.GL_FLAT);
	
        drawable.setGL( new DebugGL(drawable.getGL() ));        
        System.out.println("Init GL is " + gl.getClass().getName());
}
		
@Override
public void reshape(
		GLAutoDrawable drawable, 
	int x, int y, int width, int height) {

	if (height <= 0) // avoid a divide by zero error!
            height = 1;
        final float h = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        // glu.gluOrtho2D(0, width, 0, height); ... disappears scene
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
}