Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.media.opengl.GL;
- import javax.media.opengl.GL2;
- import javax.media.opengl.GLContext;
- import javax.media.opengl.GLDrawableFactory;
- import javax.media.opengl.GLProfile;
- import javax.media.opengl.glu.GLU;
- 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.opengl.GLCanvas;
- import org.eclipse.swt.opengl.GLData;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Event;
- import org.eclipse.swt.widgets.Listener;
- public class Triangle extends Composite {
- GLCanvas glcanvas;
- /**
- * @param arg0
- * @param arg1
- */
- public Triangle(Composite arg0, int arg1) {
- super(arg0, arg1);
- this.setLayout(new FillLayout());
- GLData gldata = new GLData();
- gldata.doubleBuffer = true;
- // need SWT.NO_BACKGROUND to prevent SWT from clearing the window
- // at the wrong times (we use glClear for this instead)
- glcanvas = new GLCanvas( this, SWT.NO_BACKGROUND, gldata );
- glcanvas.setCurrent();
- GLProfile glprofile = GLProfile.getDefault();
- final GLContext glcontext = GLDrawableFactory.getFactory( glprofile ).createExternalGLContext();
- // fix the viewport when the user resizes the window
- glcanvas.addListener( SWT.Resize, new Listener() {
- public void handleEvent(Event event) {
- System.out.println("resize");
- Rectangle rectangle = glcanvas.getClientArea();
- glcanvas.setCurrent();
- glcontext.makeCurrent();
- setup( glcontext.getGL().getGL2(), rectangle.width, rectangle.height );
- glcontext.release();
- }
- });
- // draw the triangle when the OS tells us that any part of the window needs drawing
- glcanvas.addPaintListener( new PaintListener() {
- public void paintControl( PaintEvent paintevent ) {
- System.out.println("render");
- Rectangle rectangle = glcanvas.getClientArea();
- glcanvas.setCurrent();
- glcontext.makeCurrent();
- render(glcontext.getGL().getGL2(), rectangle.width, rectangle.height);
- glcanvas.swapBuffers();
- glcontext.release();
- }
- });
- }
- private void setup( GL2 gl2, int width, int height ) {
- gl2.glMatrixMode( GL2.GL_PROJECTION );
- gl2.glLoadIdentity();
- // coordinate system origin at lower left with width and height same as the window
- GLU glu = new GLU();
- glu.gluOrtho2D( 0.0f, width, 0.0f, height );
- gl2.glMatrixMode( GL2.GL_MODELVIEW );
- gl2.glLoadIdentity();
- gl2.glViewport( 0, 0, width, height );
- }
- private void render( GL2 gl2, int width, int height ) {
- gl2.glClear( GL.GL_COLOR_BUFFER_BIT );
- // draw a triangle filling the window
- gl2.glLoadIdentity();
- gl2.glBegin( GL.GL_TRIANGLES );
- gl2.glColor3f( 1, 0, 0 );
- gl2.glVertex2f( 0, 0 );
- gl2.glColor3f( 0, 1, 0 );
- gl2.glVertex2f( width, 0 );
- gl2.glColor3f( 0, 0, 1 );
- gl2.glVertex2f( width / 2, height );
- gl2.glEnd();
- }
- /* (non-Javadoc)
- * @see org.eclipse.swt.widgets.Widget#dispose()
- */
- @Override
- public void dispose() {
- glcanvas.dispose();
- super.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment