LWJGL display inside AWT frame

Started by Cornix, January 02, 2014, 13:53:51

Previous topic - Next topic

Cornix

Hi there.

I try to put an lwjgl display inside an AWT frame. I followed this example here and changed it up a bit:
http://www.lwjgl.org/wiki/index.php?title=Basic_LWJGL_Applet


I think I have it almost working, however, the canvas is only a solid black rectangle and nothing is drawn on it.

The code is fairly small, I hope its easy to read through / comprehend:
public class TestFrame extends Frame {
	private static final long serialVersionUID = -17538123780838670L;
	
	public static void main(String[] args) {
		new TestFrame();
	}
	
	private Canvas canvas;
	private Thread gameThread;
	private boolean running;
	
	public TestFrame() {
		addWindowListener(new TestWindowListener());
		setTitle("Some AWT Frame");
		setResizable(true);
		setSize(640, 480);
		setVisible(true);
		setLayout(new BorderLayout());
		
		add(new Label("Center", Label.CENTER), BorderLayout.CENTER);
		add(new Label("West", Label.CENTER), BorderLayout.WEST);
		add(new Label("North", Label.CENTER), BorderLayout.NORTH);
		add(new Label("South", Label.CENTER), BorderLayout.SOUTH);
		
		createCanvas();
	}
	
	private void createCanvas() {
		canvas = new Canvas() {
			private static final long serialVersionUID = -1069002023468669595L;
			public void removeNotify() {
				stopOpenGL();
			}
		};
		canvas.setFocusable(true);
		canvas.requestFocus();
		canvas.setIgnoreRepaint(true);
		canvas.setSize(320, 240);
		canvas.setLocation(64, 64);
		canvas.setVisible(true);
		
		add(canvas, BorderLayout.EAST);
		
		System.out.println("Is displayable? "+canvas.isDisplayable());
		
		startOpenGL();
	}
	
	private void startOpenGL() {
		System.out.println("StartOpenGL");
		
		gameThread = new Thread() {
			public void run() {
				try {
					Display.create();
					Display.setParent(canvas);
					
					running = true;
				} catch (LWJGLException e) {
					e.printStackTrace();
				}
				while (running) {
					updateGL();
				}
				if (Display.isCreated()) {
					Display.destroy();
				}
			}
		};
		gameThread.start();
	}
	
	private void updateGL() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		
		render();
		
		Display.update();
		Display.sync(60);
		
		int error = GL11.glGetError();
		if (error != GL11.GL_NO_ERROR) {
			String msg = "Unknown Error";
			switch (error) {
			case GL11.GL_INVALID_OPERATION:
				msg = "Invalid Operation"; break;
			case GL11.GL_INVALID_VALUE:
				msg = "Invalid Value"; break;
			case GL11.GL_INVALID_ENUM:
				msg = "Invalid Enum"; break;
			case GL11.GL_STACK_OVERFLOW:
				msg = "Stack Overflow"; break;
			case GL11.GL_STACK_UNDERFLOW:
				msg = "Stack Underflow"; break;
			case GL11.GL_OUT_OF_MEMORY:
				msg = "Out of memory"; break;
			}
			throw new RuntimeException(msg);
		}
	}
	
	private void render() {
		GL11.glBegin(GL11.GL_QUADS);
		
		GL11.glColor4f(1, 0, 0, 1);
		GL11.glVertex3f(0, 0, 0);
		
		GL11.glColor4f(1, 0, 0, 1);
		GL11.glVertex3f(0.1f, 0.5f, 0);
		
		GL11.glColor4f(1, 0, 0, 1);
		GL11.glVertex3f(0.8f, 0.7f, 0);
		
		GL11.glColor4f(1, 0, 0, 1);
		GL11.glVertex3f(0.65f, 0.1f, 0);
		
		GL11.glEnd();
	}
	
	private void stopOpenGL() {
		System.out.println("StopOpenGL");
		
		running = false;
		try {
			gameThread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	private void terminate() {
		dispose();
		System.exit(0);
	}
	
	class TestWindowListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			terminate();
		}
	}
	
}


I dont get any errors / exceptions and the debug messages are printed.

Thank you all for the help.

Fool Running

It doesn't look like you are specifying the view matrix at all. LWJGL defaults to a 2D view with the width and height of the display. This means that you are probably creating a polygon that is less then 1 pixel big so it is not showing up. Try making a much bigger polygon.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Cornix

As it seems glViewport is not set correctly when using a canvas. After setting glViewport I got it working.
The Projection matrix was not needed as the default was from [-1, 1] on all axes.