OpenGL的SWT的控件

来源:互联网 发布:韩春雨 知乎精华 编辑:程序博客网 时间:2024/04/30 05:41
/*******************************************************************************  ******************************************************************************/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() {@Overridepublic 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() {@Overridepublic 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();}}


0 0
原创粉丝点击