本文主要是介绍Eclipse下的SWT的OpenGL开发(实例及源码)RCP旋转的彩色三棱锥,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
配置如上篇Eclipse下的SWT的OpenGL开发(配置、实例及源码)正弦波
新建插件工程,选中rcp,选择带视图模板的,finish。此时就有一个完整的带有视图的rcp程序。
然后添加依赖org.eclipse.opengl;bundle-version="0.5.0",创建类Refresher
package sdsd;import org.eclipse.opengl.GL;
import org.eclipse.swt.opengl.GLCanvas;class Refresher implements Runnable {public static final int DELAY = 100;private final GLCanvas canvas;private float rotate = 0.0f;public Refresher(final GLCanvas canvas) {this.canvas = canvas;}@Overridepublic void run() {if (canvas != null && !canvas.isDisposed()) {if (!canvas.isCurrent()) {canvas.setCurrent();}// 这里添加OpenGL绘图代码GL.glLoadIdentity();GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);GL.glTranslatef(0, 4.5f, -11);// 围绕y轴转起来rotate += 0.5;GL.glRotatef(rotate, 0, 1.0f, 0);// 调用递归函数,绘制三菱锥矩阵drawPyramid(0, 0, 0, 4);canvas.swapBuffers();canvas.getDisplay().timerExec(DELAY, this);}}public void drawPyramid(final float x, final float y, final float z, final int n) {if (n == 0) {return;}// 画一个三菱锥GL.glBegin(GL.GL_TRIANGLES);// 画背面GL.glColor3f(1.0f, 0.0f, 0.0f);GL.glVertex3f(x, y, z);GL.glColor3f(0.0f, 1.0f, 0.0f);GL.glVertex3f(x + 1.0f, y - 1.63f, z - 0.57f);GL.glColor3f(0.0f, 0.0f, 1.0f);GL.glVertex3f(x - 1.0f, y - 1.63f, z - 0.57f);// 画底面GL.glColor3f(1.0f, 0.0f, 0.0f);GL.glVertex3f(x, y - 1.63f, z + 1.15f);GL.glColor3f(0.0f, 1.0f, 0.0f);GL.glVertex3f(x - 1.0f, y - 1.63f, z - 0.57f);GL.glColor3f(0.0f, 0.0f, 1.0f);GL.glVertex3f(x + 1.0f, y - 1.63f, z - 0.57f);// 画左侧面GL.glColor3f(1.0f, 0.0f, 0.0f);GL.glVertex3f(x, y, z);GL.glColor3f(0.0f, 1.0f, 0.0f);GL.glVertex3f(x - 1.0f, y - 1.63f, z - 0.57f);GL.glColor3f(0.0f, 0.0f, 1.0f);GL.glVertex3f(x, y - 1.63f, z + 1.15f);// 画右侧面GL.glColor3f(1.0f, 0.0f, 0.0f);GL.glVertex3f(x, y, z);GL.glColor3f(0.0f, 1.0f, 0.0f);GL.glVertex3f(x, y - 1.63f, z + 1.15f);GL.glColor3f(0.0f, 0.0f, 1.0f);GL.glVertex3f(x + 1.0f, y - 1.63f, z - 0.57f);GL.glEnd();// 递归调用,画多个三菱锥drawPyramid(x, y - 1.63f, z + 1.15f, n - 1);drawPyramid(x - 1.0f, y - 1.63f, z - 0.57f, n - 1);drawPyramid(x + 1.0f, y - 1.63f, z - 0.57f, n - 1);}
}
然后改写view类的代码为:
package sdsd;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 View extends ViewPart {GLCanvas canvas;public View() {// TODO Auto-generated constructor stub}@Overridepublic void createPartControl(final Composite parent) {// TODO Auto-generated method stubfinal GLData data = new GLData();data.depthSize = 1;data.doubleBuffer = true;canvas = new GLCanvas(parent, SWT.NO_BACKGROUND, data);canvas.addControlListener(new ControlAdapter() {@Overridepublic void controlResized(final ControlEvent e) {final 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() {@Overridepublic void widgetDisposed(final DisposeEvent e) {dispose();}});final Refresher rf = new Refresher(canvas);rf.run();}@Overridepublic void setFocus() {// TODO Auto-generated method stub}}
转自。http://www.blogjava.net/youxia/archive/2006/12/09/86513.html
这篇关于Eclipse下的SWT的OpenGL开发(实例及源码)RCP旋转的彩色三棱锥的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!