package com.peterhi.ui.obsolete;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.lang.reflect.Method;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class Whiteboard extends Canvas implements Listener, GLEventListener {
private Frame frame;
private GLCanvas canvas;
private int vaoId;
public Whiteboard(Composite parent, int style) {
super(parent, style | SWT.EMBEDDED);
// TODO: Use GL4 instead of GL2
GLProfile profile = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(profile);
frame = SWT_AWT.new_Frame(this);
frame.setLayout(new BorderLayout());
canvas = new GLCanvas(caps);
canvas.addGLEventListener(this);
frame.add(canvas, BorderLayout.CENTER);
}
@Override
public void handleEvent(Event event) {
}
@Override
public void display(GLAutoDrawable d) {
/*GL2 gl2 = d.getGL().getGL2();
int width = d.getWidth();
int height = d.getHeight();
gl2.glClear( GL.GL_COLOR_BUFFER_BIT );
gl2.glLoadIdentity();
gl2.glBegin( GL.GL_TRIANGLES );
gl2.glColor3f( 1, 0, 0 );
gl2.glVertex2f( 0, 0 );
gl2.glColor3f( 0, 1, 0 );
gl2.glVertex2f( width, 0 );
gl2.glColor3f( 0, 0, 1 );
gl2.glVertex2f( width / 2, height );
gl2.glEnd();*/
}
@Override
public void dispose(GLAutoDrawable d) {
}
@Override
public void init(GLAutoDrawable d) {
GL gl = d.getGL();
Class<?> type = gl.getClass();
try {
if (d.getGL().isFunctionAvailable("glGenVertexArrays") &&
d.getGL().isFunctionAvailable("glBindVertexArray")) {
Method glGenVertexArrays = type.getMethod("glGenVertexArrays", int.class, int[].class, int.class);
Method glBindVertexArray = type.getMethod("glBindVertexArray", int.class);
int[] vaoIds = new int[1];
glGenVertexArrays.invoke(gl, 1, vaoIds, vaoIds.length);
vaoId = vaoIds[0];
glBindVertexArray.invoke(gl, vaoId);
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
int[] vaoIds = new int[1];
Method glGenVertexArrays = d.getGL().getClass().getMethod("glGenVertexArrays", int.class, int[].class, int.class);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void reshape(GLAutoDrawable d, int x, int y, int width, int height) {
try {
GL gl = d.getGL();
Class<?> type = gl.getClass();
Method glMatrixMode = type.getMethod("glMatrixMode", int.class);
Method glLoadIdentity = type.getMethod("glLoadIdentity");
glMatrixMode.invoke(gl, GL2.GL_PROJECTION);
glLoadIdentity.invoke(gl);
GLU glu = new GLU();
glu.gluOrtho2D(0.0f, width, 0.0, height);
glMatrixMode.invoke(gl, GL2.GL_MODELVIEW);
glLoadIdentity.invoke(gl);
gl.glViewport(0, 0, width, height);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell(display);
Whiteboard whiteboard = new Whiteboard(shell, SWT.NONE);
shell.setLayout(new FillLayout());
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}