配置如上篇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;
}
@Override
public 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
}
@Override
public void createPartControl(final Composite parent) {
// TODO Auto-generated method stub
final GLData data = new GLData();
data.depthSize = 1;
data.doubleBuffer = true;
canvas = new GLCanvas(parent, SWT.NO_BACKGROUND, data);
canvas.addControlListener(new ControlAdapter() {
@Override
public 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() {
@Override
public void widgetDisposed(final DisposeEvent e) {
dispose();
}
});
final Refresher rf = new Refresher(canvas);
rf.run();
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
}
转自。http://www.blogjava.net/youxia/archive/2006/12/09/86513.html