OpenGL的SWT的控件

/*******************************************************************************
 
 ******************************************************************************/
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();
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值