SWT and 2D programming

Started by dlamotta, December 09, 2005, 20:31:49

Previous topic - Next topic

dlamotta

Hello all, I finally turn to the forum after spending hours reading tutorials on the web and not fully comprehending what it is that I am doing wrong.  The crux of the matter is that I have an application written using the Eclipse RCP and in one of my views I want to display polygons.  These polygons in turn need to be filled with textures (coming from a .gif or .png).  While it may sound easy to some of you, I am completely new to OpenGL (but not to Java) and I am not quite sure what approach to take.

To my surprise, I was able to retrofit the code from Snippet 195 from the Eclipse site and get that running as a view in my RCP app, but when I want to do something far simpler nothing gets displayed.  This is the code that does [not do] it:

  
...
public void createPartControl(Composite parent) {
      Composite composite = new Composite(parent, SWT.NONE);
      composite.setLayout(new FillLayout());
      GLData data = new GLData();
      data.doubleBuffer = true;
      final GLCanvas canvas = new GLCanvas(composite, SWT.NONE, data);

      canvas.setCurrent();

      try {
         GLContext.useContext(canvas);
      } catch (LWJGLException e) {
         e.printStackTrace();
      }
      
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT |  GL11.GL_DEPTH_BUFFER_BIT);
      GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
      GL11.glColor3f(1.0f, 0.0f, 0.0f);      
      GL11.glLoadIdentity();
      GL11.glTranslatef(0.0f, 0.0f, -5.0f);

      GL11.glBegin(GL11.GL_TRIANGLES);
      GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
      GL11.glVertex3f(1.0f, -1.0f, 0.0f);
      GL11.glVertex3f(0.0f, 1.0f, 0.0f);
      GL11.glEnd();          
   }


Perhaps somebody can point me in the right direction for what I want to do.  I've read the tutorials at NeHe and there is one for applying textures to 3D objects, which I assume I could base myself on for applying textures to 2D objects; however, without even getting a basic triangle displayed it is harder to move on to more complex things.

I am currently using Eclipse 3.2 M3 with LWJGL 0.98 on WinXP.

Thanks for any help!

// David[/code]

CaseyB

I think you need to add a call to swapBuffers() at the end.  You are doing all of your drawing in the backBuffer and you need to put that one on the screen and pull the other down for drawing.

dlamotta

Thanks for the reply.  Unfortunately, I had already tried

canvas.swapBuffers();


...at the end of my code, but the only thing that does is it makes Eclipse draw my application twice on the screen--I have to move windows around to "erase" the first window (it's as if an impression was left on the screen).

baysmith

Yes, you must swap buffers, but you also need to setup the viewport and projection matrix by listening for resize events. After a resize event, the view must be redrawn. In Snippet195, a Runnable continously redraws. In the code below, a listener for paint events redraws.

The following works with Eclipse 3.2M5a and LWJGL 0.99 on Windows XP.

public class TestView extends ViewPart {

  private GLCanvas canvas;

  @Override
  public void createPartControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new FillLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;
    canvas = new GLCanvas(composite, SWT.NONE, data);
    canvas.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        render();
      }
    });
    canvas.addListener(SWT.Resize, new Listener() {
      public void handleEvent(Event event) {
        canvas.setCurrent();
        try {
          GLContext.useContext(canvas);
        } catch (LWJGLException e) {
          throw new RuntimeException(e);
        }
        Rectangle bounds = canvas.getBounds();
        GL11.glViewport(0, 0, bounds.width, bounds.height);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GLU.gluPerspective(35.0f, (float) bounds.width / (float) bounds.height, 1.0f, 40.0f);

        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
      }
    });

  }

  private void render() {
    canvas.setCurrent();

    try {
      GLContext.useContext(canvas);
    } catch (LWJGLException e) {
      e.printStackTrace();
    }

    GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glColor3f(1.0f, 0.0f, 0.0f);
    GL11.glLoadIdentity();
    GL11.glTranslatef(0.0f, 0.0f, -5.0f);

    GL11.glBegin(GL11.GL_TRIANGLES);
    GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
    GL11.glVertex3f(1.0f, -1.0f, 0.0f);
    GL11.glVertex3f(0.0f, 1.0f, 0.0f);
    GL11.glEnd();
    
    canvas.swapBuffers();
  }

  @Override
  public void setFocus() {
  }

}