/*******************************************************************************
******************************************************************************/
package test5.opengl.plot;
import org.eclipse.opengl.GL;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;
/**
* @Version 1.0
* @Created Mar 9, 2015 4:15:06 PM
* @Description
* <p>
* @Modification
* <p>
* Date Author Version Description
* <p>
*
*/
public class PlotOpenGL {
/**
* 在 composite 上创建一个GLCanvas,用于渲染到控件上;
* OpenGL没有窗口界面直接显示其图形,只能渲染到其他语言创建的窗口控件上。
*/
public PlotOpenGL(final Composite composite) {
composite.setLayout(new GridLayout(1, false));
final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
final Composite compositeLeft = new Composite(composite, SWT.NONE);
compositeLeft.setLayoutData(gd);
compositeLeft.setLayout(new FillLayout());
final GLData data = new GLData();
data.doubleBuffer = true;
final GLCanvas canvas = new GLCanvas(compositeLeft, SWT.NONE, data);
canvas.setCurrent();
refreshGLCanvas(canvas);
}
/**
* OpenGL画的图形必须时刻渲染canvas.swapBuffers(),才能显示出来,否则只渲染一次就被其他图形覆盖了。(新线程渲染)
*/
private void refreshGLCanvas(final GLCanvas canvas) {
final Runnable timer = new Runnable() {
@Override
public void run() {
if (canvas.isDisposed()) {
return;
}
updateRender(canvas);
canvas.getDisplay().timerExec(100, this);// 每100ms渲染一次
}
};
timer.run();
}
protected static void updateRender(final GLCanvas canvas) {
final Rectangle rect = canvas.getClientArea();
GL.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);// 背景色
GL.glColor3f(250.0f, 250f, 250f);// 画 笔 色
GL.glMatrixMode(GL.GL_PROJECTION);// 选择模型观察矩阵
GL.glLoadIdentity();
// GL.glOrtho(-rect.width / 2, rect.width / 2, -rect.height / 2, rect.height / 2, -1.0, 1.0);
GL.glViewport(0, 0, rect.width, rect.height);
GL.glClear(GL.GL_COLOR_BUFFER_BIT);
GL.glBegin(GL.GL_LINES);
GL.glVertex2d(0, -0.9);
GL.glVertex2d(0, 0.9);
GL.glVertex2d(0, 0);
GL.glVertex2d(0.9, 0);
for (double i = 0.1; i < 1; i += 0.1) {// 画刻度
GL.glVertex2d(i, 0);
GL.glVertex2d(i, 0.02);
}
GL.glEnd();
canvas.swapBuffers();
}
/**
* OpenGL画的图形必须时刻渲染canvas.swapBuffers(),才能显示出来,否则只渲染一次就被其他图形覆盖了。(监听渲染)
*/
private void refreshGLCanvasPaintListener(final GLCanvas canvas) {
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(final PaintEvent e) {
updateRender(canvas);
}
});
}
}
/*******************************************************************************
******************************************************************************/
package test5.opengl.plot;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* @Version 1.0
*
* @Created Mar 9, 2015 4:24:01 PM
* @Description
* <p>
* @Modification
* <p>
* Date Author Version Description
* <p>
*
*/
public class PlotOpenGlTest {
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Hello OpenGL");
shell.setBounds(600, 500, 300, 300);
new PlotOpenGL(shell);
shell.open();// 要放在创建控件之后。
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}