Guest User

Untitled

a guest
Aug 2nd, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import javax.media.opengl.GL;
  2. import javax.media.opengl.GL2;
  3. import javax.media.opengl.GLContext;
  4. import javax.media.opengl.GLDrawableFactory;
  5. import javax.media.opengl.GLProfile;
  6. import javax.media.opengl.glu.GLU;
  7.  
  8. import org.eclipse.swt.SWT;
  9. import org.eclipse.swt.events.PaintEvent;
  10. import org.eclipse.swt.events.PaintListener;
  11. import org.eclipse.swt.graphics.Rectangle;
  12. import org.eclipse.swt.layout.FillLayout;
  13. import org.eclipse.swt.opengl.GLCanvas;
  14. import org.eclipse.swt.opengl.GLData;
  15. import org.eclipse.swt.widgets.Composite;
  16. import org.eclipse.swt.widgets.Event;
  17. import org.eclipse.swt.widgets.Listener;
  18.  
  19.  
  20. public class Triangle extends Composite {
  21.  
  22. GLCanvas glcanvas;
  23.  
  24. /**
  25. * @param arg0
  26. * @param arg1
  27. */
  28. public Triangle(Composite arg0, int arg1) {
  29. super(arg0, arg1);
  30. this.setLayout(new FillLayout());
  31.  
  32. GLData gldata = new GLData();
  33. gldata.doubleBuffer = true;
  34. // need SWT.NO_BACKGROUND to prevent SWT from clearing the window
  35. // at the wrong times (we use glClear for this instead)
  36. glcanvas = new GLCanvas( this, SWT.NO_BACKGROUND, gldata );
  37. glcanvas.setCurrent();
  38. GLProfile glprofile = GLProfile.getDefault();
  39. final GLContext glcontext = GLDrawableFactory.getFactory( glprofile ).createExternalGLContext();
  40.  
  41. // fix the viewport when the user resizes the window
  42. glcanvas.addListener( SWT.Resize, new Listener() {
  43. public void handleEvent(Event event) {
  44. System.out.println("resize");
  45. Rectangle rectangle = glcanvas.getClientArea();
  46. glcanvas.setCurrent();
  47. glcontext.makeCurrent();
  48. setup( glcontext.getGL().getGL2(), rectangle.width, rectangle.height );
  49. glcontext.release();
  50. }
  51. });
  52.  
  53. // draw the triangle when the OS tells us that any part of the window needs drawing
  54. glcanvas.addPaintListener( new PaintListener() {
  55. public void paintControl( PaintEvent paintevent ) {
  56. System.out.println("render");
  57. Rectangle rectangle = glcanvas.getClientArea();
  58. glcanvas.setCurrent();
  59. glcontext.makeCurrent();
  60. render(glcontext.getGL().getGL2(), rectangle.width, rectangle.height);
  61. glcanvas.swapBuffers();
  62. glcontext.release();
  63. }
  64. });
  65. }
  66.  
  67. private void setup( GL2 gl2, int width, int height ) {
  68. gl2.glMatrixMode( GL2.GL_PROJECTION );
  69. gl2.glLoadIdentity();
  70.  
  71. // coordinate system origin at lower left with width and height same as the window
  72. GLU glu = new GLU();
  73. glu.gluOrtho2D( 0.0f, width, 0.0f, height );
  74.  
  75. gl2.glMatrixMode( GL2.GL_MODELVIEW );
  76. gl2.glLoadIdentity();
  77.  
  78. gl2.glViewport( 0, 0, width, height );
  79. }
  80.  
  81. private void render( GL2 gl2, int width, int height ) {
  82. gl2.glClear( GL.GL_COLOR_BUFFER_BIT );
  83.  
  84. // draw a triangle filling the window
  85. gl2.glLoadIdentity();
  86. gl2.glBegin( GL.GL_TRIANGLES );
  87. gl2.glColor3f( 1, 0, 0 );
  88. gl2.glVertex2f( 0, 0 );
  89. gl2.glColor3f( 0, 1, 0 );
  90. gl2.glVertex2f( width, 0 );
  91. gl2.glColor3f( 0, 0, 1 );
  92. gl2.glVertex2f( width / 2, height );
  93. gl2.glEnd();
  94. }
  95.  
  96. /* (non-Javadoc)
  97. * @see org.eclipse.swt.widgets.Widget#dispose()
  98. */
  99. @Override
  100. public void dispose() {
  101. glcanvas.dispose();
  102. super.dispose();
  103. }
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment