Eclipse RCP开发入门学习笔记——06.OpenGL

---------------------------------------------------------------------------------------------------

学习: http://www.blogjava.net/youxia/archive/2006/12/09/86513.html

---------------------------------------------------------------------------------------------------

 

1. 下载并安装OpenGL插件:

下载地址: http://www.eclipse.org/swt/opengl/

 

2. 给本项目添加依赖:

双击 plugin.xml 打开编辑窗口,选择 "Dependencies" 选项卡, 点击 "Required Plug-ins" 下的 "Add...",输入 "org.eclipse.opengl" , 如下图:

回到前一界面,可在项目的 "Plug-in Dependencies" 下发现加入了 ogl.jar,  如下图:

 

3. 新建视图 OpenGLView:

package hellorcp.opengl;

import org.eclipse.opengl.GL;
import org.eclipse.opengl.GLU;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class OpenGLView extends ViewPart {
	GLCanvas canvas;

	@Override
	public void createPartControl(Composite parent) {
		GLData data = new GLData();
		data.depthSize = 1;
		data.doubleBuffer = true;
		canvas = new GLCanvas(parent, SWT.NO_BACKGROUND, data);
		canvas.addControlListener(new ControlAdapter() {
			public void controlResized(ControlEvent e) {
				Rectangle rect = canvas.getClientArea();
				GL.glViewport(0, 0, rect.width, rect.height);

				// 选择投影矩阵
				GL.glMatrixMode(GL.GL_PROJECTION);
				// 重置投影矩阵
				GL.glLoadIdentity();
				// 设置窗口比例和透视图
				GLU.gluPerspective(45.0f, (float) rect.width
						/ (float) rect.height, 0.1f, 100.0f);
				// 选择模型观察矩阵
				GL.glMatrixMode(GL.GL_MODELVIEW);
				// 重置模型观察矩阵
				GL.glLoadIdentity();

				// 黑色背景
				GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
				// 设置深度缓存
				GL.glClearDepth(1.0f);
				// 启动深度测试
				GL.glEnable(GL.GL_DEPTH_TEST);
				// 选择深度测试类型
				GL.glDepthFunc(GL.GL_LESS);
				// 启用阴影平滑
				GL.glShadeModel(GL.GL_SMOOTH);
				// 精细修正透视图
				GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
				// 清除屏幕和深度缓存
				GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
				// 重置当前的模型观察矩阵
				GL.glLoadIdentity();
			}
		});
		canvas.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				dispose();
			}
		});

		Refresher rf = new Refresher(canvas);
		rf.run();
	}

	@Override
	public void setFocus() {
		// TODO 自动生成方法存根
	}

}

 

4. 修改 Perspective.java 为:

package hellorcp;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

	public void createInitialLayout(IPageLayout layout) {
		// FirstView
		String editorArea = layout.getEditorArea();
//		layout.addView("hellorcp.view.FirstView", IPageLayout.RIGHT, 0.2f,
//				editorArea);

		// SecondView
//		layout.setEditorAreaVisible(false);
//		layout.addView("hellorcp.view.SecondView", IPageLayout.RIGHT, 0.5f,
//				editorArea);
		
		// OleView
//		layout.addView("hellorcp.ole.OleView", IPageLayout.RIGHT, 0.5f,
//				editorArea);
		
		// CanvasView
//		layout.addView("hellorcp.canvas.CanvasView", IPageLayout.RIGHT, 0.5f,
//				editorArea);
		
		// OpenGLView
		layout.addView("hellorcp.opengl.OpenGLView", IPageLayout.RIGHT, 0.5f,
				editorArea);
	}

}

 

5. 在 plugin.xml 中配置view:

         <view
         	class="hellorcp.opengl.OpenGLView"
         	id="hellorcp.opengl.OpenGLView"
         	name="OpenGL" />

 

6. 保存,运行,效果如下:

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值