SWT and LWJGL: A correct way to use several similar/identical GLCanvas

Started by krasse, June 11, 2010, 06:30:43

Previous topic - Next topic

krasse

Hi!

I am trying to use SWT in harmony with LWJGL and I am a little confused how this should be done correctly.

I would like the canvases to be very similar or even identical if that helps to reduce waste of memory.

Thanks!

krasse

I have "solved" the problem by reusing a single canvas in a modified GUI design. It might be good to have an SWT snippet for multiple GLCanvases though. I don't know if any of you have been working with the SWT project, so I might have posted this in the wrong forum :P Thanks anyway for this great piece of software, I will remember to donate in christmas :)

tris

You can use multiple canvases. Just set the current cavas to be the current gl contex before you draw each one like so:


package com.tscribble.game.examples.swtopengl;

import static org.lwjgl.opengl.GL11.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

import com.tscribble.game.opengl.swt.GLComposite2D;

/**
 * @author triston
 * 
 */
public class SWTMultiComposite {

	private Display display;
	private GLComposite2D canvas,canvas2;
	private float ang,ang2 = 0;
	
	public static void main(String[] args) {
		SWTMultiComposite window = new SWTMultiComposite();
		window.open();
	}

	public void open() {
		display = Display.getDefault();
		Shell shell = new Shell();
		shell.setSize(1024, 600);
		shell.setText("SWT GL Mutiple Composites");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Menu mBar = new Menu(shell, SWT.BAR);
		shell.setMenuBar(mBar);
		
		MenuItem fileItem = new MenuItem(mBar, SWT.CASCADE);
		fileItem.setText("File");
		
		Menu fileMenu = new Menu(fileItem);
		fileItem.setMenu(fileMenu);
		
		for (int i = 0; i < 5; i++) {
			final MenuItem importItem = new MenuItem(fileMenu, SWT.NONE);
			importItem.setText("Item " + i);
		}
		
		canvas = new GLComposite2D(shell);	
		canvas.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {				
				doResize(canvas);
		}});	
		
		canvas2 = new GLComposite2D(shell);			
		canvas2.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {				
				doResize(canvas2);
		}});	

		shell.open();		
		start(); // render loop thread

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
	protected void initGL() {		
		glShadeModel(GL_SMOOTH); // Enable Smooth Shading, GL_SMOOTH (the default) or GL_FLAT. 
	}

	private void doResize(GLComposite2D comp) {
		Rectangle rec = comp.getBounds();
		float w = Math.max(rec.width, 1);
		float h = Math.max(rec.height, 1);
		
		comp.makeCurrent();

		glViewport(0, 0, (int) w, (int) h);

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();

		glOrtho(0, w, h, 0, 1, -1);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	}
	
	public void start() {
		display.syncExec(new Runnable() {
			public void run() {
				if (!canvas.isDisposed() || !canvas2.isDisposed()) {					
					try {Thread.sleep(10);}catch (InterruptedException e){}
					logic();
					logic2();					
					
					canvas.makeCurrent();
					draw();					
				
					canvas2.makeCurrent();
					draw2();
					
					canvas.swapBuffers();
					canvas2.swapBuffers();
					
					display.asyncExec(this);
				}
			}
		});
	}

	public void logic() {
		ang += 0.5f % 360;
	}
	
	public void logic2() {
		ang2 -= 0.5f % 360;
	}

	private void draw() {
		glClear(GL_COLOR_BUFFER_BIT);
		glClearColor(1.0f, 0.0f, 0.0f, 0.0f);  // Red Background

		glPushMatrix();
		{
			glTranslatef(10, 10, 0);
			glBegin(GL_TRIANGLES);
			glColor3f(1.0f, 0.0f, 0.0f); // red
			glVertex2f(5.0f, 5.0f);
			
			glColor3f(0.0f, 1.0f, 0.0f); // green
			glVertex2f(400.0f, 5.0f);
			
			glColor3f(0.0f, 0.0f, 1.0f); // blue
			glVertex2f(5.0f, 400.0f);
			glEnd();
		}
		glPopMatrix();
	}
	
	private void draw2() {
		glClear(GL_COLOR_BUFFER_BIT);
		glClearColor(0.0f, 0.0f, 1.0f, 0.0f);  // Blue Background

		glPushMatrix();
		{
			glTranslatef(10, 10, 0);
			glBegin(GL_TRIANGLES);
			glColor3f(1.0f, 0.0f, 0.0f); // red
			glVertex2f(5.0f, 5.0f);
			glColor3f(0.0f, 1.0f, 0.0f); // green
			glVertex2f(100.0f, 5.0f);
			glColor3f(0.0f, 0.0f, 1.0f); // blue
			glVertex2f(5.0f, 100.0f);
			glEnd();
		}
		glPopMatrix();
	}
		
}


The GLComposite
package com.tscribble.game.opengl.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.*;
import org.lwjgl.LWJGLException;

import org.lwjgl.opengl.*;

/**
 * @author triston
 */
public class GLComposite2D extends GLCanvas {
	private static GLData gd1;
	protected Rectangle rec;

	static {
		gd1 = new GLData();
		gd1.doubleBuffer = true;
	}

	public GLComposite2D(Composite parent) {
		super(parent, SWT.NONE, gd1);	
	}

	public void makeCurrent() {
		setCurrent();
		try {
			GLContext.useContext(this);
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
	}	
}


hope that helps