Blank Window (noob question)

Hi everyone, I recently installed jogl and am trying to get the “Getting started with jogl” tutorial going. However, i noticed when compiling the code some of the items were deprecated and some completely missing. This tutorial was written in 2003, so it is fairly old. Anyhow, (I’ll post the code below) when I run the Test.java file, all I get is a blank window with no triangle. I’ve been beating my head over this one and cannot quite pinpoint where the problem is and was hoping someone might spot something wrong… thanks for any and all help!!

~Bolt

this is part 1

package tester;

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

import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLEventListener;

import com.sun.opengl.util.Animator;

public class Test
{
    public static void main( String[] args )
    {
        try
        {
            Frame testFrame = new Frame("TestFrame");
            testFrame.setSize( 512, 384 );

            GLCapabilities glCaps = new GLCapabilities();
            glCaps.setRedBits(8);
            glCaps.setBlueBits(8);
            glCaps.setGreenBits(8);
            glCaps.setAlphaBits(8);
            
            GLCanvas canvas = new GLCanvas();

            testFrame.add( canvas );

            canvas.addGLEventListener((GLEventListener) new TestRenderer());

            final Animator animator = new Animator( canvas);
            testFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                  animator.stop();
                  System.exit(0);
                }
              });
            testFrame.setVisible(true);
            animator.start();
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }
}

this is part 2


package tester;

import javax.media.opengl.DebugGL;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLDrawable;
import javax.media.opengl.GLEventListener;

public class TestRenderer implements GLEventListener
{
    private GL              gl;
    private GLDrawable      glDrawable;

    public void init(GLDrawable drawable)
    {
        this.gl = ((GLAutoDrawable) drawable).getGL();
        this.glDrawable = drawable;

        ((GLAutoDrawable) drawable).setGL( new DebugGL(((GLAutoDrawable) drawable).getGL() ));

        System.out.println("Init GL is " + gl.getClass().getName());
    }

    public void display(GLDrawable drawable)
    {
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
        gl.glLoadIdentity();

        gl.glColor3f(1.0f, 0.0f, 0.0f );
        System.out.println("Init GL is " + gl.getClass().getName());
        gl.glBegin( GL.GL_TRIANGLES );
            gl.glVertex3f( 0.0f, 0.0f, 0.0f );
            gl.glVertex3f( 1.0f, 0.0f, 0.0f );
            gl.glVertex3f( 1.0f, 1.0f, 0.0f );
        gl.glEnd();
    }

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

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

	@Override
	public void display(GLAutoDrawable arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(GLAutoDrawable arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
			int arg4) {
		// TODO Auto-generated method stub
		
	}

}

Your TestRenderer class has two “display()” methods, so I’m guessing the one where you’re drawing the triangle is never called, because it is overridden by the second one, the empty “display(GLAutoDrawable d)” method. I’m surprised your compiler doesn’t give you a warning about this?

Anyway, I think you can safely move all the drawing content to the other “display()” method, and delete all methods that mention GLDrawable. GLAutoDrawable is the most recent version, so those are the methods you should use.

EDIT: also see http://www.java-gaming.org/forums/index.php?topic=12817.0

For reference, this here works:


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

import javax.media.opengl.DebugGL;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

import com.sun.opengl.util.Animator;

/**
 * example taken from http://www.java-gaming.org/forums/index.php?topic=1474.0
 * Whenever you run this, it's important to have the java.library.path property point to a directory
 * where you put the JOGL dll files, and have the CLASSPATH point to jogl.jar
 * 
 * This is an older version that does not use the "Input" class just yet.
 */
public class TestRenderer1 implements GLEventListener {
	public static void main(String[] args) {
		System.out.println("library property = "
				+ System.getProperty("java.library.path"));

		Frame frame = new Frame("Triangle Demo");

		GLCanvas canvas = new GLCanvas();
		canvas.requestFocusInWindow();
		canvas.addGLEventListener(new TestRenderer1());

		frame.add(canvas);
		frame.setSize(512, 512);

		final Animator animator = new Animator(canvas);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				new Thread(new Runnable() {
					public void run() {
						animator.stop();
						System.exit(0);
					}
				}).start();
			}
		});
		frame.setVisible(true);
		animator.start();
	}

	public void init(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();

		drawable.setGL(new DebugGL(drawable.getGL()));

		System.out.println("Init GL is " + gl.getClass().getName());
	}

	public void display(GLAutoDrawable drawable) {
		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

		//this method should not be called in case of ortho projection
		//glu.gluLookAt(0,0,100, 0,0,0, 0,1,0);

		gl.glBegin(GL.GL_TRIANGLES);
			gl.glColor3f(1.0f, 0.0f, 0.0f);
			gl.glVertex3f(0.0f, 0.0f, 0.0f);
			gl.glColor3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(100.0f, 0.0f, 0.0f);
			gl.glColor3f(0.0f, 0.0f, 1.0f);
			gl.glVertex3f(100.0f, 100.0f, 0.0f);
		gl.glEnd();
	}

	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);

		//if you want to use Ortho2D projection, be sure to comment out the "gluLookAt" method inside the display() method
		//glu.gluOrtho2D(-width / 2, width / 2, -height / 2, height / 2);
        glu.gluOrtho2D(0, width, 0, height);
	}

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

}

Thanks alot! This helps, I cannot believe that I overided the display I had. I think i figured out why. The compiler DID complain, but it complained that i didn’t implement all of the methods in the interface, so when I had it do it it also added the ones I already had… hehe. Anyhow, I looked at your code and the one thing I DID miss was that I didn’t assign anything to gl, examinging your code showed me how. Thanks for the help!!

~Bolt

I think you’re officially the first person I could help with OpenGL. Ever ;D
quite welcome!